Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [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. |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 8 | * Copyright (C) 2008 Qumranet, Inc. |
| 9 | * Copyright IBM Corporation, 2008 |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 11 | * |
| 12 | * Authors: |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 15 | * Amit Shah <amit.shah@qumranet.com> |
| 16 | * Ben-Ami Yassour <benami@il.ibm.com> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 19 | #include <linux/kvm_host.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 20 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 21 | #include "mmu.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 22 | #include "i8254.h" |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 23 | #include "tss.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Sean Christopherson | 2f728d6 | 2020-02-18 15:29:49 -0800 | [diff] [blame] | 25 | #include "kvm_emulate.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 26 | #include "x86.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 27 | #include "cpuid.h" |
Wei Huang | 474a5bb | 2015-06-19 13:54:23 +0200 | [diff] [blame] | 28 | #include "pmu.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 29 | #include "hyperv.h" |
Suravee Suthikulpanit | 8df14af | 2019-11-14 14:15:06 -0600 | [diff] [blame] | 30 | #include "lapic.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 31 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 32 | #include <linux/clocksource.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 33 | #include <linux/interrupt.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 34 | #include <linux/kvm.h> |
| 35 | #include <linux/fs.h> |
| 36 | #include <linux/vmalloc.h> |
Paul Gortmaker | 1767e93 | 2016-07-13 20:19:00 -0400 | [diff] [blame] | 37 | #include <linux/export.h> |
| 38 | #include <linux/moduleparam.h> |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 39 | #include <linux/mman.h> |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 40 | #include <linux/highmem.h> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 41 | #include <linux/iommu.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 42 | #include <linux/intel-iommu.h> |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 43 | #include <linux/cpufreq.h> |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 44 | #include <linux/user-return-notifier.h> |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 45 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 47 | #include <linux/perf_event.h> |
Lai Jiangshan | 7bee342 | 2010-06-02 17:06:03 +0800 | [diff] [blame] | 48 | #include <linux/uaccess.h> |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 49 | #include <linux/hash.h> |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 50 | #include <linux/pci.h> |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 51 | #include <linux/timekeeper_internal.h> |
| 52 | #include <linux/pvclock_gtod.h> |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 53 | #include <linux/kvm_irqfd.h> |
| 54 | #include <linux/irqbypass.h> |
Ingo Molnar | 3905f9a | 2017-02-05 12:07:04 +0100 | [diff] [blame] | 55 | #include <linux/sched/stat.h> |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 56 | #include <linux/sched/isolation.h> |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 57 | #include <linux/mem_encrypt.h> |
Ingo Molnar | 3905f9a | 2017-02-05 12:07:04 +0100 | [diff] [blame] | 58 | |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 59 | #include <trace/events/kvm.h> |
Xiao Guangrong | 2ed152a | 2010-03-10 19:00:43 +0800 | [diff] [blame] | 60 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 61 | #include <asm/debugreg.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 62 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 63 | #include <asm/desc.h> |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 64 | #include <asm/mce.h> |
Ingo Molnar | f89e32e | 2015-04-22 10:58:10 +0200 | [diff] [blame] | 65 | #include <linux/kernel_stat.h> |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 66 | #include <asm/fpu/internal.h> /* Ugh! */ |
Zachary Amsden | 1d5f066 | 2010-08-19 22:07:30 -1000 | [diff] [blame] | 67 | #include <asm/pvclock.h> |
Avi Kivity | 217fc9c | 2010-08-26 13:38:03 +0300 | [diff] [blame] | 68 | #include <asm/div64.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 69 | #include <asm/irq_remapping.h> |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 70 | #include <asm/mshyperv.h> |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 71 | #include <asm/hypervisor.h> |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 72 | #include <asm/intel_pt.h> |
Masami Hiramatsu | b3dc069 | 2019-09-06 22:13:59 +0900 | [diff] [blame] | 73 | #include <asm/emulate_prefix.h> |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 74 | #include <clocksource/hyperv_timer.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 75 | |
Dave Hansen | d1898b7 | 2016-06-01 10:42:20 -0700 | [diff] [blame] | 76 | #define CREATE_TRACE_POINTS |
| 77 | #include "trace.h" |
| 78 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 79 | #define MAX_IO_MSRS 256 |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 80 | #define KVM_MAX_MCE_BANKS 32 |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 81 | u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; |
| 82 | EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 83 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 84 | #define emul_to_vcpu(ctxt) \ |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 85 | ((struct kvm_vcpu *)(ctxt)->vcpu) |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 86 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 87 | /* EFER defaults: |
| 88 | * - enable syscall per default because its emulated by KVM |
| 89 | * - enable LME and LMA per default on 64 bit KVM |
| 90 | */ |
| 91 | #ifdef CONFIG_X86_64 |
Lai Jiangshan | 1260edbe | 2011-02-21 11:51:35 +0800 | [diff] [blame] | 92 | static |
| 93 | u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 94 | #else |
Lai Jiangshan | 1260edbe | 2011-02-21 11:51:35 +0800 | [diff] [blame] | 95 | static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 96 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 97 | |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 98 | static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; |
| 99 | |
Paolo Bonzini | 833b45d | 2019-09-30 18:48:44 +0200 | [diff] [blame] | 100 | #define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__ |
| 101 | #define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__ |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 102 | |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 103 | #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ |
| 104 | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 105 | |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 106 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 107 | static void process_nmi(struct kvm_vcpu *vcpu); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 108 | static void enter_smm(struct kvm_vcpu *vcpu); |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 109 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 110 | static void store_regs(struct kvm_vcpu *vcpu); |
| 111 | static int sync_regs(struct kvm_vcpu *vcpu); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 112 | |
Paolo Bonzini | 893590c | 2015-11-06 11:46:24 +0100 | [diff] [blame] | 113 | struct kvm_x86_ops *kvm_x86_ops __read_mostly; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 115 | |
Paolo Bonzini | 893590c | 2015-11-06 11:46:24 +0100 | [diff] [blame] | 116 | static bool __read_mostly ignore_msrs = 0; |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 117 | module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 118 | |
Eyal Moscovici | fab0aa3 | 2017-11-08 14:32:08 +0200 | [diff] [blame] | 119 | static bool __read_mostly report_ignored_msrs = true; |
| 120 | module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); |
| 121 | |
Wanpeng Li | 4c27625 | 2018-05-05 04:02:32 -0700 | [diff] [blame] | 122 | unsigned int min_timer_period_us = 200; |
Marcelo Tosatti | 9ed96e8 | 2014-01-06 12:00:02 -0200 | [diff] [blame] | 123 | module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); |
| 124 | |
Marcelo Tosatti | 630994b | 2015-05-12 22:42:04 -0300 | [diff] [blame] | 125 | static bool __read_mostly kvmclock_periodic_sync = true; |
| 126 | module_param(kvmclock_periodic_sync, bool, S_IRUGO); |
| 127 | |
Paolo Bonzini | 893590c | 2015-11-06 11:46:24 +0100 | [diff] [blame] | 128 | bool __read_mostly kvm_has_tsc_control; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 129 | EXPORT_SYMBOL_GPL(kvm_has_tsc_control); |
Paolo Bonzini | 893590c | 2015-11-06 11:46:24 +0100 | [diff] [blame] | 130 | u32 __read_mostly kvm_max_guest_tsc_khz; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 131 | EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz); |
Haozhong Zhang | bc9b961 | 2015-10-20 15:39:01 +0800 | [diff] [blame] | 132 | u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; |
| 133 | EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits); |
| 134 | u64 __read_mostly kvm_max_tsc_scaling_ratio; |
| 135 | EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 136 | u64 __read_mostly kvm_default_tsc_scaling_ratio; |
| 137 | EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 138 | |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 139 | /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ |
Paolo Bonzini | 893590c | 2015-11-06 11:46:24 +0100 | [diff] [blame] | 140 | static u32 __read_mostly tsc_tolerance_ppm = 250; |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 141 | module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); |
| 142 | |
Sean Christopherson | c3941d9 | 2019-04-17 10:15:33 -0700 | [diff] [blame] | 143 | /* |
| 144 | * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables |
| 145 | * adaptive tuning starting from default advancment of 1000ns. '0' disables |
| 146 | * advancement entirely. Any other value is used as-is and disables adaptive |
| 147 | * tuning, i.e. allows priveleged userspace to set an exact advancement time. |
| 148 | */ |
| 149 | static int __read_mostly lapic_timer_advance_ns = -1; |
Wanpeng Li | 0e6edce | 2019-05-20 16:18:06 +0800 | [diff] [blame] | 150 | module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR); |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 151 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 152 | static bool __read_mostly vector_hashing = true; |
| 153 | module_param(vector_hashing, bool, S_IRUGO); |
| 154 | |
Liran Alon | c4ae60e | 2018-03-12 13:12:47 +0200 | [diff] [blame] | 155 | bool __read_mostly enable_vmware_backdoor = false; |
| 156 | module_param(enable_vmware_backdoor, bool, S_IRUGO); |
| 157 | EXPORT_SYMBOL_GPL(enable_vmware_backdoor); |
| 158 | |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 159 | static bool __read_mostly force_emulation_prefix = false; |
| 160 | module_param(force_emulation_prefix, bool, S_IRUGO); |
| 161 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 162 | int __read_mostly pi_inject_timer = -1; |
| 163 | module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); |
| 164 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 165 | #define KVM_NR_SHARED_MSRS 16 |
| 166 | |
| 167 | struct kvm_shared_msrs_global { |
| 168 | int nr; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 169 | u32 msrs[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct kvm_shared_msrs { |
| 173 | struct user_return_notifier urn; |
| 174 | bool registered; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 175 | struct kvm_shared_msr_values { |
| 176 | u64 host; |
| 177 | u64 curr; |
| 178 | } values[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 182 | static struct kvm_shared_msrs __percpu *shared_msrs; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 183 | |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 184 | #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ |
| 185 | | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ |
| 186 | | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ |
| 187 | | XFEATURE_MASK_PKRU) |
| 188 | |
Sean Christopherson | 9166198 | 2020-03-02 15:57:06 -0800 | [diff] [blame] | 189 | u64 __read_mostly host_efer; |
| 190 | EXPORT_SYMBOL_GPL(host_efer); |
| 191 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 192 | static u64 __read_mostly host_xss; |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 193 | u64 __read_mostly supported_xss; |
| 194 | EXPORT_SYMBOL_GPL(supported_xss); |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 195 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 196 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 197 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 198 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 199 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 200 | { "invlpg", VCPU_STAT(invlpg) }, |
| 201 | { "exits", VCPU_STAT(exits) }, |
| 202 | { "io_exits", VCPU_STAT(io_exits) }, |
| 203 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 204 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 205 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 206 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 207 | { "halt_exits", VCPU_STAT(halt_exits) }, |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 208 | { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, |
Paolo Bonzini | 62bea5b | 2015-09-15 18:27:57 +0200 | [diff] [blame] | 209 | { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 210 | { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 211 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 212 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 213 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 214 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 215 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 216 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 217 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 218 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 219 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 220 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Paolo Bonzini | 0f1e261 | 2016-12-17 16:05:19 +0100 | [diff] [blame] | 221 | { "req_event", VCPU_STAT(req_event) }, |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 222 | { "l1d_flush", VCPU_STAT(l1d_flush) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 223 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 224 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 225 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 226 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 227 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 228 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 229 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 230 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 231 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Paolo Bonzini | 833b45d | 2019-09-30 18:48:44 +0200 | [diff] [blame] | 232 | { "largepages", VM_STAT(lpages, .mode = 0444) }, |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 233 | { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) }, |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 234 | { "max_mmu_page_hash_collisions", |
| 235 | VM_STAT(max_mmu_page_hash_collisions) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 236 | { NULL } |
| 237 | }; |
| 238 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 239 | u64 __read_mostly host_xcr0; |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 240 | u64 __read_mostly supported_xcr0; |
| 241 | EXPORT_SYMBOL_GPL(supported_xcr0); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 242 | |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 243 | struct kmem_cache *x86_fpu_cache; |
| 244 | EXPORT_SYMBOL_GPL(x86_fpu_cache); |
| 245 | |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 246 | static struct kmem_cache *x86_emulator_cache; |
| 247 | |
| 248 | static struct kmem_cache *kvm_alloc_emulator_cache(void) |
| 249 | { |
Sean Christopherson | 06add25 | 2020-02-18 15:29:50 -0800 | [diff] [blame] | 250 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
| 251 | unsigned int size = sizeof(struct x86_emulate_ctxt); |
| 252 | |
| 253 | return kmem_cache_create_usercopy("x86_emulator", size, |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 254 | __alignof__(struct x86_emulate_ctxt), |
Sean Christopherson | 06add25 | 2020-02-18 15:29:50 -0800 | [diff] [blame] | 255 | SLAB_ACCOUNT, useroffset, |
| 256 | size - useroffset, NULL); |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Jan Kiszka | b6785de | 2012-09-20 07:43:17 +0200 | [diff] [blame] | 259 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | d6aa100 | 2011-04-20 15:47:13 +0300 | [diff] [blame] | 260 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 261 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
| 262 | { |
| 263 | int i; |
| 264 | for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++) |
| 265 | vcpu->arch.apf.gfns[i] = ~0; |
| 266 | } |
| 267 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 268 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 269 | { |
| 270 | unsigned slot; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 271 | struct kvm_shared_msrs *locals |
| 272 | = container_of(urn, struct kvm_shared_msrs, urn); |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 273 | struct kvm_shared_msr_values *values; |
Ignacio Alvarado | 1650b4e | 2016-11-04 12:15:55 -0700 | [diff] [blame] | 274 | unsigned long flags; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 275 | |
Ignacio Alvarado | 1650b4e | 2016-11-04 12:15:55 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Disabling irqs at this point since the following code could be |
| 278 | * interrupted and executed through kvm_arch_hardware_disable() |
| 279 | */ |
| 280 | local_irq_save(flags); |
| 281 | if (locals->registered) { |
| 282 | locals->registered = false; |
| 283 | user_return_notifier_unregister(urn); |
| 284 | } |
| 285 | local_irq_restore(flags); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 286 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 287 | values = &locals->values[slot]; |
| 288 | if (values->host != values->curr) { |
| 289 | wrmsrl(shared_msrs_global.msrs[slot], values->host); |
| 290 | values->curr = values->host; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 291 | } |
| 292 | } |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 293 | } |
| 294 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 295 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
| 296 | { |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 297 | BUG_ON(slot >= KVM_NR_SHARED_MSRS); |
Paolo Bonzini | c847fe8 | 2015-07-29 11:06:34 +0200 | [diff] [blame] | 298 | shared_msrs_global.msrs[slot] = msr; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 299 | if (slot >= shared_msrs_global.nr) |
| 300 | shared_msrs_global.nr = slot + 1; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 301 | } |
| 302 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); |
| 303 | |
| 304 | static void kvm_shared_msr_cpu_online(void) |
| 305 | { |
Sean Christopherson | 05c19c2 | 2019-11-22 12:04:50 -0800 | [diff] [blame] | 306 | unsigned int cpu = smp_processor_id(); |
| 307 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); |
| 308 | u64 value; |
| 309 | int i; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 310 | |
Sean Christopherson | 05c19c2 | 2019-11-22 12:04:50 -0800 | [diff] [blame] | 311 | for (i = 0; i < shared_msrs_global.nr; ++i) { |
| 312 | rdmsrl_safe(shared_msrs_global.msrs[i], &value); |
| 313 | smsr->values[i].host = value; |
| 314 | smsr->values[i].curr = value; |
| 315 | } |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 316 | } |
| 317 | |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 318 | int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 319 | { |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 320 | unsigned int cpu = smp_processor_id(); |
| 321 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 322 | int err; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 323 | |
Paolo Bonzini | de1fca5 | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 324 | value = (value & mask) | (smsr->values[slot].host & ~mask); |
| 325 | if (value == smsr->values[slot].curr) |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 326 | return 0; |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 327 | err = wrmsrl_safe(shared_msrs_global.msrs[slot], value); |
| 328 | if (err) |
| 329 | return 1; |
| 330 | |
Paolo Bonzini | de1fca5 | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 331 | smsr->values[slot].curr = value; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 332 | if (!smsr->registered) { |
| 333 | smsr->urn.on_user_return = kvm_on_user_return; |
| 334 | user_return_notifier_register(&smsr->urn); |
| 335 | smsr->registered = true; |
| 336 | } |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 337 | return 0; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 338 | } |
| 339 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); |
| 340 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 341 | static void drop_user_return_notifiers(void) |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 342 | { |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 343 | unsigned int cpu = smp_processor_id(); |
| 344 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 345 | |
| 346 | if (smsr->registered) |
| 347 | kvm_on_user_return(&smsr->urn); |
| 348 | } |
| 349 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 350 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 351 | { |
Gleb Natapov | 8a5a87d | 2012-08-05 15:58:26 +0300 | [diff] [blame] | 352 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 353 | } |
| 354 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 355 | |
Jim Mattson | 5887164 | 2018-05-09 16:56:04 -0400 | [diff] [blame] | 356 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
| 357 | { |
| 358 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); |
| 359 | } |
| 360 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); |
| 361 | |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 362 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 363 | { |
Jim Mattson | 5887164 | 2018-05-09 16:56:04 -0400 | [diff] [blame] | 364 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
| 365 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 366 | u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff | |
| 367 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 368 | |
Jim Mattson | 5887164 | 2018-05-09 16:56:04 -0400 | [diff] [blame] | 369 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
Jim Mattson | d380228 | 2017-08-10 10:14:13 -0700 | [diff] [blame] | 370 | return 1; |
Jim Mattson | 5887164 | 2018-05-09 16:56:04 -0400 | [diff] [blame] | 371 | if (!msr_info->host_initiated) { |
| 372 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) |
| 373 | return 1; |
| 374 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) |
| 375 | return 1; |
| 376 | } |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 377 | |
| 378 | kvm_lapic_set_base(vcpu, msr_info->data); |
Wanpeng Li | 4abaffc | 2020-02-26 10:41:02 +0800 | [diff] [blame] | 379 | kvm_recalculate_apic_map(vcpu->kvm); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 380 | return 0; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 383 | |
Andi Kleen | 2605fc2 | 2014-05-02 00:44:37 +0200 | [diff] [blame] | 384 | asmlinkage __visible void kvm_spurious_fault(void) |
Geoff Levand | e3ba45b | 2013-04-05 19:20:30 +0000 | [diff] [blame] | 385 | { |
| 386 | /* Fault while not rebooting. We want the trace. */ |
kbuild test robot | b4fdcf6 | 2019-09-29 18:43:28 +0200 | [diff] [blame] | 387 | BUG_ON(!kvm_rebooting); |
Geoff Levand | e3ba45b | 2013-04-05 19:20:30 +0000 | [diff] [blame] | 388 | } |
| 389 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); |
| 390 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 391 | #define EXCPT_BENIGN 0 |
| 392 | #define EXCPT_CONTRIBUTORY 1 |
| 393 | #define EXCPT_PF 2 |
| 394 | |
| 395 | static int exception_class(int vector) |
| 396 | { |
| 397 | switch (vector) { |
| 398 | case PF_VECTOR: |
| 399 | return EXCPT_PF; |
| 400 | case DE_VECTOR: |
| 401 | case TS_VECTOR: |
| 402 | case NP_VECTOR: |
| 403 | case SS_VECTOR: |
| 404 | case GP_VECTOR: |
| 405 | return EXCPT_CONTRIBUTORY; |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | return EXCPT_BENIGN; |
| 410 | } |
| 411 | |
Nadav Amit | d6e8c85 | 2014-07-24 14:51:24 +0300 | [diff] [blame] | 412 | #define EXCPT_FAULT 0 |
| 413 | #define EXCPT_TRAP 1 |
| 414 | #define EXCPT_ABORT 2 |
| 415 | #define EXCPT_INTERRUPT 3 |
| 416 | |
| 417 | static int exception_type(int vector) |
| 418 | { |
| 419 | unsigned int mask; |
| 420 | |
| 421 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) |
| 422 | return EXCPT_INTERRUPT; |
| 423 | |
| 424 | mask = 1 << vector; |
| 425 | |
| 426 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ |
| 427 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) |
| 428 | return EXCPT_TRAP; |
| 429 | |
| 430 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) |
| 431 | return EXCPT_ABORT; |
| 432 | |
| 433 | /* Reserved exceptions will result in fault */ |
| 434 | return EXCPT_FAULT; |
| 435 | } |
| 436 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 437 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
| 438 | { |
| 439 | unsigned nr = vcpu->arch.exception.nr; |
| 440 | bool has_payload = vcpu->arch.exception.has_payload; |
| 441 | unsigned long payload = vcpu->arch.exception.payload; |
| 442 | |
| 443 | if (!has_payload) |
| 444 | return; |
| 445 | |
| 446 | switch (nr) { |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 447 | case DB_VECTOR: |
| 448 | /* |
| 449 | * "Certain debug exceptions may clear bit 0-3. The |
| 450 | * remaining contents of the DR6 register are never |
| 451 | * cleared by the processor". |
| 452 | */ |
| 453 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; |
| 454 | /* |
| 455 | * DR6.RTM is set by all #DB exceptions that don't clear it. |
| 456 | */ |
| 457 | vcpu->arch.dr6 |= DR6_RTM; |
| 458 | vcpu->arch.dr6 |= payload; |
| 459 | /* |
| 460 | * Bit 16 should be set in the payload whenever the #DB |
| 461 | * exception should clear DR6.RTM. This makes the payload |
| 462 | * compatible with the pending debug exceptions under VMX. |
| 463 | * Though not currently documented in the SDM, this also |
| 464 | * makes the payload compatible with the exit qualification |
| 465 | * for #DB exceptions under VMX. |
| 466 | */ |
| 467 | vcpu->arch.dr6 ^= payload & DR6_RTM; |
Oliver Upton | 307f1cf | 2020-02-07 02:36:04 -0800 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * The #DB payload is defined as compatible with the 'pending |
| 471 | * debug exceptions' field under VMX, not DR6. While bit 12 is |
| 472 | * defined in the 'pending debug exceptions' field (enabled |
| 473 | * breakpoint), it is reserved and must be zero in DR6. |
| 474 | */ |
| 475 | vcpu->arch.dr6 &= ~BIT(12); |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 476 | break; |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 477 | case PF_VECTOR: |
| 478 | vcpu->arch.cr2 = payload; |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | vcpu->arch.exception.has_payload = false; |
| 483 | vcpu->arch.exception.payload = 0; |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); |
| 486 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 487 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 488 | unsigned nr, bool has_error, u32 error_code, |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 489 | bool has_payload, unsigned long payload, bool reinject) |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 490 | { |
| 491 | u32 prev_nr; |
| 492 | int class1, class2; |
| 493 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 494 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 495 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 496 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 497 | queue: |
Nadav Amit | 3ffb246 | 2014-11-02 11:54:42 +0200 | [diff] [blame] | 498 | if (has_error && !is_protmode(vcpu)) |
| 499 | has_error = false; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 500 | if (reinject) { |
| 501 | /* |
| 502 | * On vmentry, vcpu->arch.exception.pending is only |
| 503 | * true if an event injection was blocked by |
| 504 | * nested_run_pending. In that case, however, |
| 505 | * vcpu_enter_guest requests an immediate exit, |
| 506 | * and the guest shouldn't proceed far enough to |
| 507 | * need reinjection. |
| 508 | */ |
| 509 | WARN_ON_ONCE(vcpu->arch.exception.pending); |
| 510 | vcpu->arch.exception.injected = true; |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 511 | if (WARN_ON_ONCE(has_payload)) { |
| 512 | /* |
| 513 | * A reinjected event has already |
| 514 | * delivered its payload. |
| 515 | */ |
| 516 | has_payload = false; |
| 517 | payload = 0; |
| 518 | } |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 519 | } else { |
| 520 | vcpu->arch.exception.pending = true; |
| 521 | vcpu->arch.exception.injected = false; |
| 522 | } |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 523 | vcpu->arch.exception.has_error_code = has_error; |
| 524 | vcpu->arch.exception.nr = nr; |
| 525 | vcpu->arch.exception.error_code = error_code; |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 526 | vcpu->arch.exception.has_payload = has_payload; |
| 527 | vcpu->arch.exception.payload = payload; |
Oliver Upton | a06230b | 2020-02-07 02:36:06 -0800 | [diff] [blame] | 528 | if (!is_guest_mode(vcpu)) |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 529 | kvm_deliver_exception_payload(vcpu); |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | |
| 533 | /* to check exception */ |
| 534 | prev_nr = vcpu->arch.exception.nr; |
| 535 | if (prev_nr == DF_VECTOR) { |
| 536 | /* triple fault -> shutdown */ |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 537 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 538 | return; |
| 539 | } |
| 540 | class1 = exception_class(prev_nr); |
| 541 | class2 = exception_class(nr); |
| 542 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 543 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 544 | /* |
| 545 | * Generate double fault per SDM Table 5-5. Set |
| 546 | * exception.pending = true so that the double fault |
| 547 | * can trigger a nested vmexit. |
| 548 | */ |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 549 | vcpu->arch.exception.pending = true; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 550 | vcpu->arch.exception.injected = false; |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 551 | vcpu->arch.exception.has_error_code = true; |
| 552 | vcpu->arch.exception.nr = DF_VECTOR; |
| 553 | vcpu->arch.exception.error_code = 0; |
Jim Mattson | c851436 | 2018-10-16 14:29:19 -0700 | [diff] [blame] | 554 | vcpu->arch.exception.has_payload = false; |
| 555 | vcpu->arch.exception.payload = 0; |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 556 | } else |
| 557 | /* replace previous exception with a new one in a hope |
| 558 | that instruction re-execution will regenerate lost |
| 559 | exception */ |
| 560 | goto queue; |
| 561 | } |
| 562 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 563 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 564 | { |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 565 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 568 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 569 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 570 | { |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 571 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 572 | } |
| 573 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); |
| 574 | |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 575 | static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
| 576 | unsigned long payload) |
| 577 | { |
| 578 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); |
| 579 | } |
| 580 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 581 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
| 582 | u32 error_code, unsigned long payload) |
| 583 | { |
| 584 | kvm_multiple_exception(vcpu, nr, true, error_code, |
| 585 | true, payload, false); |
| 586 | } |
| 587 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 588 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 589 | { |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 590 | if (err) |
| 591 | kvm_inject_gp(vcpu, 0); |
| 592 | else |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 593 | return kvm_skip_emulated_instruction(vcpu); |
| 594 | |
| 595 | return 1; |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 596 | } |
| 597 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); |
Joerg Roedel | 8df25a3 | 2010-09-10 17:30:46 +0200 | [diff] [blame] | 598 | |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 599 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 600 | { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 601 | ++vcpu->stat.pf_guest; |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 602 | vcpu->arch.exception.nested_apf = |
| 603 | is_guest_mode(vcpu) && fault->async_page_fault; |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 604 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 605 | vcpu->arch.apf.nested_apf_token = fault->address; |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 606 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
| 607 | } else { |
| 608 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, |
| 609 | fault->address); |
| 610 | } |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 611 | } |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 612 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 613 | |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 614 | static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
Joerg Roedel | d4f8cf6 | 2010-09-10 17:30:55 +0200 | [diff] [blame] | 615 | { |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 616 | if (mmu_is_nested(vcpu) && !fault->nested_page_fault) |
| 617 | vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault); |
Joerg Roedel | d4f8cf6 | 2010-09-10 17:30:55 +0200 | [diff] [blame] | 618 | else |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 619 | vcpu->arch.mmu->inject_page_fault(vcpu, fault); |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 620 | |
| 621 | return fault->nested_page_fault; |
Joerg Roedel | d4f8cf6 | 2010-09-10 17:30:55 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 624 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 625 | { |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 626 | atomic_inc(&vcpu->arch.nmi_queued); |
| 627 | kvm_make_request(KVM_REQ_NMI, vcpu); |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 628 | } |
| 629 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 630 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 631 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 632 | { |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 633 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 634 | } |
| 635 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 636 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 637 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 638 | { |
Jim Mattson | 91e86d2 | 2018-10-16 14:29:21 -0700 | [diff] [blame] | 639 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 640 | } |
| 641 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); |
| 642 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 643 | /* |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 644 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 645 | * a #GP and return false. |
| 646 | */ |
| 647 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 648 | { |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 649 | if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl) |
| 650 | return true; |
| 651 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 652 | return false; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 653 | } |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 654 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 655 | |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 656 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
| 657 | { |
| 658 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
| 659 | return true; |
| 660 | |
| 661 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 662 | return false; |
| 663 | } |
| 664 | EXPORT_SYMBOL_GPL(kvm_require_dr); |
| 665 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 666 | /* |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 667 | * This function will be used to read from the physical memory of the currently |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 668 | * running guest. The difference to kvm_vcpu_read_guest_page is that this function |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 669 | * can read from guest physical or from the guest's guest physical memory. |
| 670 | */ |
| 671 | int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, |
| 672 | gfn_t ngfn, void *data, int offset, int len, |
| 673 | u32 access) |
| 674 | { |
Paolo Bonzini | 54987b7 | 2014-09-02 13:23:06 +0200 | [diff] [blame] | 675 | struct x86_exception exception; |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 676 | gfn_t real_gfn; |
| 677 | gpa_t ngpa; |
| 678 | |
| 679 | ngpa = gfn_to_gpa(ngfn); |
Paolo Bonzini | 54987b7 | 2014-09-02 13:23:06 +0200 | [diff] [blame] | 680 | real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception); |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 681 | if (real_gfn == UNMAPPED_GVA) |
| 682 | return -EFAULT; |
| 683 | |
| 684 | real_gfn = gpa_to_gfn(real_gfn); |
| 685 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 686 | return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len); |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 687 | } |
| 688 | EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu); |
| 689 | |
Fengguang Wu | 69b0049 | 2015-01-19 22:33:39 +0800 | [diff] [blame] | 690 | static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
Joerg Roedel | 3d06b8b | 2010-09-10 17:30:53 +0200 | [diff] [blame] | 691 | void *data, int offset, int len, u32 access) |
| 692 | { |
| 693 | return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn, |
| 694 | data, offset, len, access); |
| 695 | } |
| 696 | |
Sean Christopherson | 16cfacc | 2019-09-03 16:36:45 -0700 | [diff] [blame] | 697 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
| 698 | { |
| 699 | return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) | |
| 700 | rsvd_bits(1, 2); |
| 701 | } |
| 702 | |
Joerg Roedel | ec92fe4 | 2010-09-10 17:30:51 +0200 | [diff] [blame] | 703 | /* |
Sean Christopherson | 16cfacc | 2019-09-03 16:36:45 -0700 | [diff] [blame] | 704 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 705 | */ |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 706 | int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 707 | { |
| 708 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 709 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 710 | int i; |
| 711 | int ret; |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 712 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 713 | |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 714 | ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte, |
| 715 | offset * sizeof(u64), sizeof(pdpte), |
| 716 | PFERR_USER_MASK|PFERR_WRITE_MASK); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 717 | if (ret < 0) { |
| 718 | ret = 0; |
| 719 | goto out; |
| 720 | } |
| 721 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
Bandan Das | 812f30b | 2016-07-12 18:18:50 -0400 | [diff] [blame] | 722 | if ((pdpte[i] & PT_PRESENT_MASK) && |
Sean Christopherson | 16cfacc | 2019-09-03 16:36:45 -0700 | [diff] [blame] | 723 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 724 | ret = 0; |
| 725 | goto out; |
| 726 | } |
| 727 | } |
| 728 | ret = 1; |
| 729 | |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 730 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 731 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); |
| 732 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 733 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 734 | |
| 735 | return ret; |
| 736 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 737 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 738 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 739 | bool pdptrs_changed(struct kvm_vcpu *vcpu) |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 740 | { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 741 | u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)]; |
Joerg Roedel | 3d06b8b | 2010-09-10 17:30:53 +0200 | [diff] [blame] | 742 | int offset; |
| 743 | gfn_t gfn; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 744 | int r; |
| 745 | |
Paolo Bonzini | bf03d4f | 2019-06-06 18:52:44 +0200 | [diff] [blame] | 746 | if (!is_pae_paging(vcpu)) |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 747 | return false; |
| 748 | |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 749 | if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR)) |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 750 | return true; |
| 751 | |
Paolo Bonzini | a512177 | 2017-07-24 18:54:38 +0200 | [diff] [blame] | 752 | gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT; |
| 753 | offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1); |
Joerg Roedel | 3d06b8b | 2010-09-10 17:30:53 +0200 | [diff] [blame] | 754 | r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte), |
| 755 | PFERR_USER_MASK | PFERR_WRITE_MASK); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 756 | if (r < 0) |
Miaohe Lin | 7f7f0d9 | 2019-10-25 18:54:34 +0800 | [diff] [blame] | 757 | return true; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 758 | |
Miaohe Lin | 7f7f0d9 | 2019-10-25 18:54:34 +0800 | [diff] [blame] | 759 | return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 760 | } |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 761 | EXPORT_SYMBOL_GPL(pdptrs_changed); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 762 | |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 763 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 764 | { |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 765 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
Xiao Guangrong | d81135a | 2015-05-13 14:42:28 +0800 | [diff] [blame] | 766 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP; |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 767 | |
Avi Kivity | f9a48e6 | 2010-01-06 19:10:22 +0200 | [diff] [blame] | 768 | cr0 |= X86_CR0_ET; |
| 769 | |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 770 | #ifdef CONFIG_X86_64 |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 771 | if (cr0 & 0xffffffff00000000UL) |
| 772 | return 1; |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 773 | #endif |
| 774 | |
| 775 | cr0 &= ~CR0_RESERVED_BITS; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 776 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 777 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
| 778 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 779 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 780 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
| 781 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 782 | |
| 783 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 784 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 785 | if ((vcpu->arch.efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 786 | int cs_db, cs_l; |
| 787 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 788 | if (!is_pae(vcpu)) |
| 789 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 790 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 791 | if (cs_l) |
| 792 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 793 | } else |
| 794 | #endif |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 795 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 796 | kvm_read_cr3(vcpu))) |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 797 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 800 | if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) |
| 801 | return 1; |
| 802 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 803 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 804 | |
Lai Jiangshan | d170c41 | 2011-02-21 11:21:30 +0800 | [diff] [blame] | 805 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { |
Xiao Guangrong | e5f3f02 | 2010-11-12 14:47:01 +0800 | [diff] [blame] | 806 | kvm_clear_async_pf_completion_queue(vcpu); |
Lai Jiangshan | d170c41 | 2011-02-21 11:21:30 +0800 | [diff] [blame] | 807 | kvm_async_pf_hash_reset(vcpu); |
| 808 | } |
Xiao Guangrong | e5f3f02 | 2010-11-12 14:47:01 +0800 | [diff] [blame] | 809 | |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 810 | if ((cr0 ^ old_cr0) & update_bits) |
| 811 | kvm_mmu_reset_context(vcpu); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 812 | |
Laszlo Ersek | 879ae18 | 2015-11-04 12:54:41 +0100 | [diff] [blame] | 813 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && |
| 814 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && |
| 815 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 816 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); |
| 817 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 818 | return 0; |
| 819 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 820 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 821 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 822 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 823 | { |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 824 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 825 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 826 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 827 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 828 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42bdf99 | 2013-04-15 23:30:13 -0300 | [diff] [blame] | 829 | { |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 830 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
Marcelo Tosatti | 42bdf99 | 2013-04-15 23:30:13 -0300 | [diff] [blame] | 831 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 832 | if (vcpu->arch.xcr0 != host_xcr0) |
| 833 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); |
| 834 | |
| 835 | if (vcpu->arch.xsaves_enabled && |
| 836 | vcpu->arch.ia32_xss != host_xss) |
| 837 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); |
| 838 | } |
Marcelo Tosatti | 42bdf99 | 2013-04-15 23:30:13 -0300 | [diff] [blame] | 839 | } |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 840 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
| 841 | |
| 842 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
| 843 | { |
| 844 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
| 845 | |
| 846 | if (vcpu->arch.xcr0 != host_xcr0) |
| 847 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); |
| 848 | |
| 849 | if (vcpu->arch.xsaves_enabled && |
| 850 | vcpu->arch.ia32_xss != host_xss) |
| 851 | wrmsrl(MSR_IA32_XSS, host_xss); |
| 852 | } |
| 853 | |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
Marcelo Tosatti | 42bdf99 | 2013-04-15 23:30:13 -0300 | [diff] [blame] | 856 | |
Fengguang Wu | 69b0049 | 2015-01-19 22:33:39 +0800 | [diff] [blame] | 857 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 858 | { |
Liu, Jinsong | 56c103e | 2014-02-21 17:39:02 +0000 | [diff] [blame] | 859 | u64 xcr0 = xcr; |
| 860 | u64 old_xcr0 = vcpu->arch.xcr0; |
Paolo Bonzini | 46c34cb | 2013-10-17 16:50:46 +0200 | [diff] [blame] | 861 | u64 valid_bits; |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 862 | |
| 863 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ |
| 864 | if (index != XCR_XFEATURE_ENABLED_MASK) |
| 865 | return 1; |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 866 | if (!(xcr0 & XFEATURE_MASK_FP)) |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 867 | return 1; |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 868 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 869 | return 1; |
Paolo Bonzini | 46c34cb | 2013-10-17 16:50:46 +0200 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * Do not allow the guest to set bits that we do not support |
| 873 | * saving. However, xcr0 bit 0 is always set, even if the |
| 874 | * emulated CPU does not support XSAVE (see fx_init). |
| 875 | */ |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 876 | valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; |
Paolo Bonzini | 46c34cb | 2013-10-17 16:50:46 +0200 | [diff] [blame] | 877 | if (xcr0 & ~valid_bits) |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 878 | return 1; |
Paolo Bonzini | 46c34cb | 2013-10-17 16:50:46 +0200 | [diff] [blame] | 879 | |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 880 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
| 881 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) |
Liu, Jinsong | 390bd52 | 2014-02-24 10:58:09 +0000 | [diff] [blame] | 882 | return 1; |
| 883 | |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 884 | if (xcr0 & XFEATURE_MASK_AVX512) { |
| 885 | if (!(xcr0 & XFEATURE_MASK_YMM)) |
Chao Peng | 612263b | 2014-10-22 17:35:24 +0800 | [diff] [blame] | 886 | return 1; |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 887 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
Chao Peng | 612263b | 2014-10-22 17:35:24 +0800 | [diff] [blame] | 888 | return 1; |
| 889 | } |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 890 | vcpu->arch.xcr0 = xcr0; |
Liu, Jinsong | 56c103e | 2014-02-21 17:39:02 +0000 | [diff] [blame] | 891 | |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 892 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
Liu, Jinsong | 56c103e | 2014-02-21 17:39:02 +0000 | [diff] [blame] | 893 | kvm_update_cpuid(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
| 898 | { |
Zhanghaoyu (A) | 764bcbc | 2013-06-14 07:36:13 +0000 | [diff] [blame] | 899 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || |
| 900 | __kvm_set_xcr(vcpu, index, xcr)) { |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 901 | kvm_inject_gp(vcpu, 0); |
| 902 | return 1; |
| 903 | } |
| 904 | return 0; |
| 905 | } |
| 906 | EXPORT_SYMBOL_GPL(kvm_set_xcr); |
| 907 | |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 908 | #define __cr4_reserved_bits(__cpu_has, __c) \ |
| 909 | ({ \ |
| 910 | u64 __reserved_bits = CR4_RESERVED_BITS; \ |
| 911 | \ |
| 912 | if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ |
| 913 | __reserved_bits |= X86_CR4_OSXSAVE; \ |
| 914 | if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ |
| 915 | __reserved_bits |= X86_CR4_SMEP; \ |
| 916 | if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ |
| 917 | __reserved_bits |= X86_CR4_SMAP; \ |
| 918 | if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ |
| 919 | __reserved_bits |= X86_CR4_FSGSBASE; \ |
| 920 | if (!__cpu_has(__c, X86_FEATURE_PKU)) \ |
| 921 | __reserved_bits |= X86_CR4_PKE; \ |
| 922 | if (!__cpu_has(__c, X86_FEATURE_LA57)) \ |
| 923 | __reserved_bits |= X86_CR4_LA57; \ |
Sean Christopherson | d76c7fb | 2020-01-28 15:53:44 -0800 | [diff] [blame] | 924 | if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ |
| 925 | __reserved_bits |= X86_CR4_UMIP; \ |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 926 | __reserved_bits; \ |
| 927 | }) |
| 928 | |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 929 | static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c) |
| 930 | { |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 931 | u64 reserved_bits = __cr4_reserved_bits(cpu_has, c); |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 932 | |
Sean Christopherson | c10398b | 2020-03-02 15:56:46 -0800 | [diff] [blame] | 933 | if (kvm_cpu_cap_has(X86_FEATURE_LA57)) |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 934 | reserved_bits &= ~X86_CR4_LA57; |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 935 | |
Sean Christopherson | 90d2f60 | 2020-03-02 15:56:47 -0800 | [diff] [blame] | 936 | if (kvm_cpu_cap_has(X86_FEATURE_UMIP)) |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 937 | reserved_bits &= ~X86_CR4_UMIP; |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 938 | |
| 939 | return reserved_bits; |
| 940 | } |
| 941 | |
Wanpeng Li | 3ca9419 | 2019-09-18 17:50:10 +0800 | [diff] [blame] | 942 | static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 943 | { |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 944 | if (cr4 & cr4_reserved_bits) |
Wanpeng Li | 3ca9419 | 2019-09-18 17:50:10 +0800 | [diff] [blame] | 945 | return -EINVAL; |
| 946 | |
Sean Christopherson | 345599f | 2019-12-10 14:44:16 -0800 | [diff] [blame] | 947 | if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu)) |
Wanpeng Li | 3ca9419 | 2019-09-18 17:50:10 +0800 | [diff] [blame] | 948 | return -EINVAL; |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 953 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 954 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 955 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
Xiao Guangrong | 0be0226 | 2015-05-11 22:55:21 +0800 | [diff] [blame] | 956 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | |
Huaitong Han | b9baba8 | 2016-03-22 16:51:21 +0800 | [diff] [blame] | 957 | X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE; |
Xiao Guangrong | 0be0226 | 2015-05-11 22:55:21 +0800 | [diff] [blame] | 958 | |
Wanpeng Li | 3ca9419 | 2019-09-18 17:50:10 +0800 | [diff] [blame] | 959 | if (kvm_valid_cr4(vcpu, cr4)) |
Paolo Bonzini | ae3e61e | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 960 | return 1; |
| 961 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 962 | if (is_long_mode(vcpu)) { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 963 | if (!(cr4 & X86_CR4_PAE)) |
| 964 | return 1; |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 965 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 966 | && ((cr4 ^ old_cr4) & pdptr_bits) |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 967 | && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
| 968 | kvm_read_cr3(vcpu))) |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 969 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 970 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 971 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 972 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 973 | return 1; |
| 974 | |
| 975 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ |
| 976 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) |
| 977 | return 1; |
| 978 | } |
| 979 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 980 | if (kvm_x86_ops->set_cr4(vcpu, cr4)) |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 981 | return 1; |
| 982 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 983 | if (((cr4 ^ old_cr4) & pdptr_bits) || |
| 984 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 985 | kvm_mmu_reset_context(vcpu); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 986 | |
Huaitong Han | b9baba8 | 2016-03-22 16:51:21 +0800 | [diff] [blame] | 987 | if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 988 | kvm_update_cpuid(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 989 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 990 | return 0; |
| 991 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 992 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 993 | |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 994 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 995 | { |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 996 | bool skip_tlb_flush = false; |
Paolo Bonzini | ac14623 | 2014-11-10 13:53:25 +0100 | [diff] [blame] | 997 | #ifdef CONFIG_X86_64 |
Junaid Shahid | c19986f | 2018-05-04 11:37:13 -0700 | [diff] [blame] | 998 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 999 | |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 1000 | if (pcid_enabled) { |
Junaid Shahid | 208320b | 2018-06-27 14:59:21 -0700 | [diff] [blame] | 1001 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
| 1002 | cr3 &= ~X86_CR3_PCID_NOFLUSH; |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 1003 | } |
Paolo Bonzini | ac14623 | 2014-11-10 13:53:25 +0100 | [diff] [blame] | 1004 | #endif |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 1005 | |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 1006 | if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { |
Junaid Shahid | 956bf35 | 2018-06-27 14:59:18 -0700 | [diff] [blame] | 1007 | if (!skip_tlb_flush) { |
| 1008 | kvm_mmu_sync_roots(vcpu); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 1009 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Junaid Shahid | 956bf35 | 2018-06-27 14:59:18 -0700 | [diff] [blame] | 1010 | } |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 1011 | return 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Yu Zhang | d1cd3ce | 2017-08-24 20:27:53 +0800 | [diff] [blame] | 1014 | if (is_long_mode(vcpu) && |
Wanpeng Li | a780a3e | 2018-05-13 02:24:47 -0700 | [diff] [blame] | 1015 | (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))) |
Yu Zhang | d1cd3ce | 2017-08-24 20:27:53 +0800 | [diff] [blame] | 1016 | return 1; |
Paolo Bonzini | bf03d4f | 2019-06-06 18:52:44 +0200 | [diff] [blame] | 1017 | else if (is_pae_paging(vcpu) && |
| 1018 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) |
Nadav Amit | 346874c | 2014-04-18 03:35:09 +0300 | [diff] [blame] | 1019 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1020 | |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 1021 | kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 1022 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1023 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Junaid Shahid | 7c390d3 | 2018-06-27 14:59:06 -0700 | [diff] [blame] | 1024 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 1025 | return 0; |
| 1026 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 1027 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1028 | |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 1029 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1030 | { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 1031 | if (cr8 & CR8_RESERVED_BITS) |
| 1032 | return 1; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1033 | if (lapic_in_kernel(vcpu)) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1034 | kvm_lapic_set_tpr(vcpu, cr8); |
| 1035 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1036 | vcpu->arch.cr8 = cr8; |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1040 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 1041 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1042 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1043 | if (lapic_in_kernel(vcpu)) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1044 | return kvm_lapic_get_cr8(vcpu); |
| 1045 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1046 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1047 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 1048 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 1049 | |
Nadav Amit | ae561ed | 2015-04-02 03:10:37 +0300 | [diff] [blame] | 1050 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
| 1051 | { |
| 1052 | int i; |
| 1053 | |
| 1054 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 1055 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 1056 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 1057 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1061 | static void kvm_update_dr6(struct kvm_vcpu *vcpu) |
| 1062 | { |
| 1063 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1064 | kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6); |
| 1065 | } |
| 1066 | |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 1067 | static void kvm_update_dr7(struct kvm_vcpu *vcpu) |
| 1068 | { |
| 1069 | unsigned long dr7; |
| 1070 | |
| 1071 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1072 | dr7 = vcpu->arch.guest_debug_dr7; |
| 1073 | else |
| 1074 | dr7 = vcpu->arch.dr7; |
| 1075 | kvm_x86_ops->set_dr7(vcpu, dr7); |
Paolo Bonzini | 360b948 | 2014-02-21 09:55:56 +0100 | [diff] [blame] | 1076 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
| 1077 | if (dr7 & DR7_BP_EN_MASK) |
| 1078 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 1081 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
| 1082 | { |
| 1083 | u64 fixed = DR6_FIXED_1; |
| 1084 | |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1085 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 1086 | fixed |= DR6_RTM; |
| 1087 | return fixed; |
| 1088 | } |
| 1089 | |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1090 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1091 | { |
Marios Pomonis | ea74005 | 2019-12-11 12:47:52 -0800 | [diff] [blame] | 1092 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
| 1093 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1094 | switch (dr) { |
| 1095 | case 0 ... 3: |
Marios Pomonis | ea74005 | 2019-12-11 12:47:52 -0800 | [diff] [blame] | 1096 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1097 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1098 | vcpu->arch.eff_db[dr] = val; |
| 1099 | break; |
| 1100 | case 4: |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1101 | /* fall through */ |
| 1102 | case 6: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1103 | if (val & 0xffffffff00000000ULL) |
| 1104 | return -1; /* #GP */ |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 1105 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1106 | kvm_update_dr6(vcpu); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1107 | break; |
| 1108 | case 5: |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1109 | /* fall through */ |
| 1110 | default: /* 7 */ |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 1111 | if (!kvm_dr7_valid(val)) |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1112 | return -1; /* #GP */ |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1113 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 1114 | kvm_update_dr7(vcpu); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | return 0; |
| 1119 | } |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1120 | |
| 1121 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
| 1122 | { |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 1123 | if (__kvm_set_dr(vcpu, dr, val)) { |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1124 | kvm_inject_gp(vcpu, 0); |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 1125 | return 1; |
| 1126 | } |
| 1127 | return 0; |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1128 | } |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1129 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
| 1130 | |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 1131 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1132 | { |
Marios Pomonis | ea74005 | 2019-12-11 12:47:52 -0800 | [diff] [blame] | 1133 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
| 1134 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1135 | switch (dr) { |
| 1136 | case 0 ... 3: |
Marios Pomonis | ea74005 | 2019-12-11 12:47:52 -0800 | [diff] [blame] | 1137 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1138 | break; |
| 1139 | case 4: |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1140 | /* fall through */ |
| 1141 | case 6: |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1142 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1143 | *val = vcpu->arch.dr6; |
| 1144 | else |
| 1145 | *val = kvm_x86_ops->get_dr6(vcpu); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1146 | break; |
| 1147 | case 5: |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1148 | /* fall through */ |
| 1149 | default: /* 7 */ |
| 1150 | *val = vcpu->arch.dr7; |
| 1151 | break; |
| 1152 | } |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1155 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
| 1156 | |
Avi Kivity | 022cd0e | 2011-11-10 14:57:23 +0200 | [diff] [blame] | 1157 | bool kvm_rdpmc(struct kvm_vcpu *vcpu) |
| 1158 | { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1159 | u32 ecx = kvm_rcx_read(vcpu); |
Avi Kivity | 022cd0e | 2011-11-10 14:57:23 +0200 | [diff] [blame] | 1160 | u64 data; |
| 1161 | int err; |
| 1162 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 1163 | err = kvm_pmu_rdpmc(vcpu, ecx, &data); |
Avi Kivity | 022cd0e | 2011-11-10 14:57:23 +0200 | [diff] [blame] | 1164 | if (err) |
| 1165 | return err; |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1166 | kvm_rax_write(vcpu, (u32)data); |
| 1167 | kvm_rdx_write(vcpu, data >> 32); |
Avi Kivity | 022cd0e | 2011-11-10 14:57:23 +0200 | [diff] [blame] | 1168 | return err; |
| 1169 | } |
| 1170 | EXPORT_SYMBOL_GPL(kvm_rdpmc); |
| 1171 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1172 | /* |
| 1173 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 1174 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 1175 | * |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1176 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
| 1177 | * extract the supported MSRs from the related const lists. |
| 1178 | * msrs_to_save is selected from the msrs_to_save_all to reflect the |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 1179 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1180 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 1181 | * may depend on host virtualization features rather than host cpu features. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1182 | */ |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 1183 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1184 | static const u32 msrs_to_save_all[] = { |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1185 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1186 | MSR_STAR, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1187 | #ifdef CONFIG_X86_64 |
| 1188 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 1189 | #endif |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 1190 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 1191 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
Xiaoyao Li | 2bdb76c | 2019-03-08 15:57:20 +0800 | [diff] [blame] | 1192 | MSR_IA32_SPEC_CTRL, |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1193 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
| 1194 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, |
| 1195 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, |
| 1196 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, |
| 1197 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, |
| 1198 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, |
Tao Xu | 6e3ba4a | 2019-07-16 14:55:50 +0800 | [diff] [blame] | 1199 | MSR_IA32_UMWAIT_CONTROL, |
| 1200 | |
Jim Mattson | e2ada66 | 2019-08-21 11:20:04 -0700 | [diff] [blame] | 1201 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
| 1202 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3, |
| 1203 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, |
| 1204 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, |
| 1205 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, |
| 1206 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, |
| 1207 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, |
| 1208 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, |
| 1209 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, |
| 1210 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, |
| 1211 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, |
| 1212 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, |
| 1213 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, |
Jim Mattson | e2ada66 | 2019-08-21 11:20:04 -0700 | [diff] [blame] | 1214 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
| 1215 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, |
| 1216 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, |
| 1217 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, |
| 1218 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, |
| 1219 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, |
| 1220 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, |
| 1221 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, |
| 1222 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1223 | }; |
| 1224 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1225 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1226 | static unsigned num_msrs_to_save; |
| 1227 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1228 | static const u32 emulated_msrs_all[] = { |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 1229 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
| 1230 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, |
| 1231 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
| 1232 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, |
Ladi Prosek | 72c139b | 2017-07-26 13:32:59 +0200 | [diff] [blame] | 1233 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1234 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
| 1235 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1236 | HV_X64_MSR_RESET, |
Andrey Smetanin | 11c4b1c | 2015-09-16 12:29:49 +0300 | [diff] [blame] | 1237 | HV_X64_MSR_VP_INDEX, |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1238 | HV_X64_MSR_VP_RUNTIME, |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1239 | HV_X64_MSR_SCONTROL, |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1240 | HV_X64_MSR_STIMER0_CONFIG, |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1241 | HV_X64_MSR_VP_ASSIST_PAGE, |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1242 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
| 1243 | HV_X64_MSR_TSC_EMULATION_STATUS, |
| 1244 | |
| 1245 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 1246 | MSR_KVM_PV_EOI_EN, |
| 1247 | |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1248 | MSR_IA32_TSC_ADJUST, |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1249 | MSR_IA32_TSCDEADLINE, |
Xiaoyao Li | 2bdb76c | 2019-03-08 15:57:20 +0800 | [diff] [blame] | 1250 | MSR_IA32_ARCH_CAPABILITIES, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1251 | MSR_IA32_MISC_ENABLE, |
Avi Kivity | 908e75f | 2010-07-07 14:09:38 +0300 | [diff] [blame] | 1252 | MSR_IA32_MCG_STATUS, |
| 1253 | MSR_IA32_MCG_CTL, |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1254 | MSR_IA32_MCG_EXT_CTL, |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 1255 | MSR_IA32_SMBASE, |
Liran Alon | 52797bf | 2017-11-15 13:43:14 +0200 | [diff] [blame] | 1256 | MSR_SMI_COUNT, |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 1257 | MSR_PLATFORM_INFO, |
| 1258 | MSR_MISC_FEATURES_ENABLES, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 1259 | MSR_AMD64_VIRT_SPEC_CTRL, |
Liran Alon | 6c6a2ab | 2019-04-15 18:45:26 +0300 | [diff] [blame] | 1260 | MSR_IA32_POWER_CTL, |
Paolo Bonzini | 99634e3 | 2020-01-20 14:22:55 +0100 | [diff] [blame] | 1261 | MSR_IA32_UCODE_REV, |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 1262 | |
Paolo Bonzini | 95c5c7c | 2019-07-02 14:45:24 +0200 | [diff] [blame] | 1263 | /* |
| 1264 | * The following list leaves out MSRs whose values are determined |
| 1265 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. |
| 1266 | * We always support the "true" VMX control MSRs, even if the host |
| 1267 | * processor does not, so I am putting these registers here rather |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1268 | * than in msrs_to_save_all. |
Paolo Bonzini | 95c5c7c | 2019-07-02 14:45:24 +0200 | [diff] [blame] | 1269 | */ |
| 1270 | MSR_IA32_VMX_BASIC, |
| 1271 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, |
| 1272 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, |
| 1273 | MSR_IA32_VMX_TRUE_EXIT_CTLS, |
| 1274 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, |
| 1275 | MSR_IA32_VMX_MISC, |
| 1276 | MSR_IA32_VMX_CR0_FIXED0, |
| 1277 | MSR_IA32_VMX_CR4_FIXED0, |
| 1278 | MSR_IA32_VMX_VMCS_ENUM, |
| 1279 | MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1280 | MSR_IA32_VMX_EPT_VPID_CAP, |
| 1281 | MSR_IA32_VMX_VMFUNC, |
| 1282 | |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 1283 | MSR_K7_HWCR, |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 1284 | MSR_KVM_POLL_CONTROL, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1285 | }; |
| 1286 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1287 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 1288 | static unsigned num_emulated_msrs; |
| 1289 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1290 | /* |
| 1291 | * List of msr numbers which are used to expose MSR-based features that |
| 1292 | * can be used by a hypervisor to validate requested CPU features. |
| 1293 | */ |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1294 | static const u32 msr_based_features_all[] = { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1295 | MSR_IA32_VMX_BASIC, |
| 1296 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, |
| 1297 | MSR_IA32_VMX_PINBASED_CTLS, |
| 1298 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, |
| 1299 | MSR_IA32_VMX_PROCBASED_CTLS, |
| 1300 | MSR_IA32_VMX_TRUE_EXIT_CTLS, |
| 1301 | MSR_IA32_VMX_EXIT_CTLS, |
| 1302 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, |
| 1303 | MSR_IA32_VMX_ENTRY_CTLS, |
| 1304 | MSR_IA32_VMX_MISC, |
| 1305 | MSR_IA32_VMX_CR0_FIXED0, |
| 1306 | MSR_IA32_VMX_CR0_FIXED1, |
| 1307 | MSR_IA32_VMX_CR4_FIXED0, |
| 1308 | MSR_IA32_VMX_CR4_FIXED1, |
| 1309 | MSR_IA32_VMX_VMCS_ENUM, |
| 1310 | MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1311 | MSR_IA32_VMX_EPT_VPID_CAP, |
| 1312 | MSR_IA32_VMX_VMFUNC, |
| 1313 | |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 1314 | MSR_F10H_DECFG, |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 1315 | MSR_IA32_UCODE_REV, |
Paolo Bonzini | cd28325 | 2018-06-25 14:04:37 +0200 | [diff] [blame] | 1316 | MSR_IA32_ARCH_CAPABILITIES, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1317 | }; |
| 1318 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 1319 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1320 | static unsigned int num_msr_based_features; |
| 1321 | |
Xiaoyao Li | 4d22c17 | 2019-04-19 10:16:24 +0800 | [diff] [blame] | 1322 | static u64 kvm_get_arch_capabilities(void) |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1323 | { |
Xiaoyao Li | 4d22c17 | 2019-04-19 10:16:24 +0800 | [diff] [blame] | 1324 | u64 data = 0; |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1325 | |
Xiaoyao Li | 4d22c17 | 2019-04-19 10:16:24 +0800 | [diff] [blame] | 1326 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 1327 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1328 | |
| 1329 | /* |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 1330 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that |
| 1331 | * the nested hypervisor runs with NX huge pages. If it is not, |
| 1332 | * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other |
| 1333 | * L1 guests, so it need not worry about its own (L2) guests. |
| 1334 | */ |
| 1335 | data |= ARCH_CAP_PSCHANGE_MC_NO; |
| 1336 | |
| 1337 | /* |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1338 | * If we're doing cache flushes (either "always" or "cond") |
| 1339 | * we will do one whenever the guest does a vmlaunch/vmresume. |
| 1340 | * If an outer hypervisor is doing the cache flush for us |
| 1341 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that |
| 1342 | * capability to the guest too, and if EPT is disabled we're not |
| 1343 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will |
| 1344 | * require a nested hypervisor to do a flush of its own. |
| 1345 | */ |
| 1346 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) |
| 1347 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; |
| 1348 | |
Paolo Bonzini | 0c54914 | 2019-08-19 17:24:07 +0200 | [diff] [blame] | 1349 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
| 1350 | data |= ARCH_CAP_RDCL_NO; |
| 1351 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) |
| 1352 | data |= ARCH_CAP_SSB_NO; |
| 1353 | if (!boot_cpu_has_bug(X86_BUG_MDS)) |
| 1354 | data |= ARCH_CAP_MDS_NO; |
| 1355 | |
Pawan Gupta | e1d38b6 | 2019-10-23 12:23:33 +0200 | [diff] [blame] | 1356 | /* |
Paolo Bonzini | c11f83e | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 1357 | * On TAA affected systems: |
| 1358 | * - nothing to do if TSX is disabled on the host. |
| 1359 | * - we emulate TSX_CTRL if present on the host. |
| 1360 | * This lets the guest use VERW to clear CPU buffers. |
Pawan Gupta | e1d38b6 | 2019-10-23 12:23:33 +0200 | [diff] [blame] | 1361 | */ |
Paolo Bonzini | cbbaa27 | 2019-11-18 18:58:26 +0100 | [diff] [blame] | 1362 | if (!boot_cpu_has(X86_FEATURE_RTM)) |
Paolo Bonzini | c11f83e | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 1363 | data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR); |
Paolo Bonzini | cbbaa27 | 2019-11-18 18:58:26 +0100 | [diff] [blame] | 1364 | else if (!boot_cpu_has_bug(X86_BUG_TAA)) |
| 1365 | data |= ARCH_CAP_TAA_NO; |
Pawan Gupta | e1d38b6 | 2019-10-23 12:23:33 +0200 | [diff] [blame] | 1366 | |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1367 | return data; |
| 1368 | } |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1369 | |
Wanpeng Li | 66421c1 | 2018-02-28 14:03:30 +0800 | [diff] [blame] | 1370 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
| 1371 | { |
| 1372 | switch (msr->index) { |
Paolo Bonzini | cd28325 | 2018-06-25 14:04:37 +0200 | [diff] [blame] | 1373 | case MSR_IA32_ARCH_CAPABILITIES: |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 1374 | msr->data = kvm_get_arch_capabilities(); |
| 1375 | break; |
| 1376 | case MSR_IA32_UCODE_REV: |
Paolo Bonzini | cd28325 | 2018-06-25 14:04:37 +0200 | [diff] [blame] | 1377 | rdmsrl_safe(msr->index, &msr->data); |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 1378 | break; |
Wanpeng Li | 66421c1 | 2018-02-28 14:03:30 +0800 | [diff] [blame] | 1379 | default: |
| 1380 | if (kvm_x86_ops->get_msr_feature(msr)) |
| 1381 | return 1; |
| 1382 | } |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1386 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1387 | { |
| 1388 | struct kvm_msr_entry msr; |
Wanpeng Li | 66421c1 | 2018-02-28 14:03:30 +0800 | [diff] [blame] | 1389 | int r; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1390 | |
| 1391 | msr.index = index; |
Wanpeng Li | 66421c1 | 2018-02-28 14:03:30 +0800 | [diff] [blame] | 1392 | r = kvm_get_msr_feature(&msr); |
| 1393 | if (r) |
| 1394 | return r; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1395 | |
| 1396 | *data = msr.data; |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1401 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1402 | { |
| 1403 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
| 1404 | return false; |
| 1405 | |
| 1406 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
| 1407 | return false; |
| 1408 | |
Sean Christopherson | 0a62956 | 2019-04-02 08:19:16 -0700 | [diff] [blame] | 1409 | if (efer & (EFER_LME | EFER_LMA) && |
| 1410 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
| 1411 | return false; |
| 1412 | |
| 1413 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) |
| 1414 | return false; |
| 1415 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1416 | return true; |
| 1417 | |
| 1418 | } |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1419 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1420 | { |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 1421 | if (efer & efer_reserved_bits) |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1422 | return false; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1423 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1424 | return __kvm_valid_efer(vcpu, efer); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1425 | } |
| 1426 | EXPORT_SYMBOL_GPL(kvm_valid_efer); |
| 1427 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1428 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1429 | { |
| 1430 | u64 old_efer = vcpu->arch.efer; |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1431 | u64 efer = msr_info->data; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1432 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1433 | if (efer & efer_reserved_bits) |
Paolo Bonzini | 66f61c9 | 2019-05-24 21:52:46 +0200 | [diff] [blame] | 1434 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1435 | |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 1436 | if (!msr_info->host_initiated) { |
| 1437 | if (!__kvm_valid_efer(vcpu, efer)) |
| 1438 | return 1; |
| 1439 | |
| 1440 | if (is_paging(vcpu) && |
| 1441 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) |
| 1442 | return 1; |
| 1443 | } |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 1444 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1445 | efer &= ~EFER_LMA; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1446 | efer |= vcpu->arch.efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1447 | |
Sheng Yang | a3d204e | 2010-05-12 16:40:40 +0800 | [diff] [blame] | 1448 | kvm_x86_ops->set_efer(vcpu, efer); |
| 1449 | |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 1450 | /* Update reserved bits */ |
| 1451 | if ((efer ^ old_efer) & EFER_NX) |
| 1452 | kvm_mmu_reset_context(vcpu); |
| 1453 | |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 1454 | return 0; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 1457 | void kvm_enable_efer_bits(u64 mask) |
| 1458 | { |
| 1459 | efer_reserved_bits &= ~mask; |
| 1460 | } |
| 1461 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 1462 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1463 | /* |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1464 | * Write @data into the MSR specified by @index. Select MSR specific fault |
| 1465 | * checks are bypassed if @host_initiated is %true. |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1466 | * Returns 0 on success, non-0 otherwise. |
| 1467 | * Assumes vcpu_load() was already called. |
| 1468 | */ |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1469 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
| 1470 | bool host_initiated) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1471 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1472 | struct msr_data msr; |
| 1473 | |
| 1474 | switch (index) { |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 1475 | case MSR_FS_BASE: |
| 1476 | case MSR_GS_BASE: |
| 1477 | case MSR_KERNEL_GS_BASE: |
| 1478 | case MSR_CSTAR: |
| 1479 | case MSR_LSTAR: |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1480 | if (is_noncanonical_address(data, vcpu)) |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 1481 | return 1; |
| 1482 | break; |
| 1483 | case MSR_IA32_SYSENTER_EIP: |
| 1484 | case MSR_IA32_SYSENTER_ESP: |
| 1485 | /* |
| 1486 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if |
| 1487 | * non-canonical address is written on Intel but not on |
| 1488 | * AMD (which ignores the top 32-bits, because it does |
| 1489 | * not implement 64-bit SYSENTER). |
| 1490 | * |
| 1491 | * 64-bit code should hence be able to write a non-canonical |
| 1492 | * value on AMD. Making the address canonical ensures that |
| 1493 | * vmentry does not fail on Intel after writing a non-canonical |
| 1494 | * value, and that something deterministic happens if the guest |
| 1495 | * invokes 64-bit SYSENTER. |
| 1496 | */ |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1497 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 1498 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1499 | |
| 1500 | msr.data = data; |
| 1501 | msr.index = index; |
| 1502 | msr.host_initiated = host_initiated; |
| 1503 | |
| 1504 | return kvm_x86_ops->set_msr(vcpu, &msr); |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Read the MSR specified by @index into @data. Select MSR specific fault |
| 1509 | * checks are bypassed if @host_initiated is %true. |
| 1510 | * Returns 0 on success, non-0 otherwise. |
| 1511 | * Assumes vcpu_load() was already called. |
| 1512 | */ |
Paolo Bonzini | edef5c3 | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 1513 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
| 1514 | bool host_initiated) |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1515 | { |
| 1516 | struct msr_data msr; |
| 1517 | int ret; |
| 1518 | |
| 1519 | msr.index = index; |
| 1520 | msr.host_initiated = host_initiated; |
| 1521 | |
| 1522 | ret = kvm_x86_ops->get_msr(vcpu, &msr); |
| 1523 | if (!ret) |
| 1524 | *data = msr.data; |
| 1525 | return ret; |
| 1526 | } |
| 1527 | |
| 1528 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
| 1529 | { |
| 1530 | return __kvm_get_msr(vcpu, index, data, false); |
| 1531 | } |
| 1532 | EXPORT_SYMBOL_GPL(kvm_get_msr); |
| 1533 | |
| 1534 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
| 1535 | { |
| 1536 | return __kvm_set_msr(vcpu, index, data, false); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1537 | } |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 1538 | EXPORT_SYMBOL_GPL(kvm_set_msr); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1539 | |
Sean Christopherson | 1edce0a | 2019-09-05 14:22:55 -0700 | [diff] [blame] | 1540 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
| 1541 | { |
| 1542 | u32 ecx = kvm_rcx_read(vcpu); |
| 1543 | u64 data; |
| 1544 | |
| 1545 | if (kvm_get_msr(vcpu, ecx, &data)) { |
| 1546 | trace_kvm_msr_read_ex(ecx); |
| 1547 | kvm_inject_gp(vcpu, 0); |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
| 1551 | trace_kvm_msr_read(ecx, data); |
| 1552 | |
| 1553 | kvm_rax_write(vcpu, data & -1u); |
| 1554 | kvm_rdx_write(vcpu, (data >> 32) & -1u); |
| 1555 | return kvm_skip_emulated_instruction(vcpu); |
| 1556 | } |
| 1557 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); |
| 1558 | |
| 1559 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) |
| 1560 | { |
| 1561 | u32 ecx = kvm_rcx_read(vcpu); |
| 1562 | u64 data = kvm_read_edx_eax(vcpu); |
| 1563 | |
| 1564 | if (kvm_set_msr(vcpu, ecx, data)) { |
| 1565 | trace_kvm_msr_write_ex(ecx, data); |
| 1566 | kvm_inject_gp(vcpu, 0); |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | trace_kvm_msr_write(ecx, data); |
| 1571 | return kvm_skip_emulated_instruction(vcpu); |
| 1572 | } |
| 1573 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); |
| 1574 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1575 | /* |
Wanpeng Li | 1e9e262 | 2019-11-21 11:17:11 +0800 | [diff] [blame] | 1576 | * The fast path for frequent and performance sensitive wrmsr emulation, |
| 1577 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces |
| 1578 | * the latency of virtual IPI by avoiding the expensive bits of transitioning |
| 1579 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the |
| 1580 | * other cases which must be called after interrupts are enabled on the host. |
| 1581 | */ |
| 1582 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) |
| 1583 | { |
| 1584 | if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) && |
| 1585 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
| 1586 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED)) { |
| 1587 | |
| 1588 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32)); |
| 1589 | return kvm_lapic_reg_write(vcpu->arch.apic, APIC_ICR, (u32)data); |
| 1590 | } |
| 1591 | |
| 1592 | return 1; |
| 1593 | } |
| 1594 | |
| 1595 | enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
| 1596 | { |
| 1597 | u32 msr = kvm_rcx_read(vcpu); |
| 1598 | u64 data = kvm_read_edx_eax(vcpu); |
| 1599 | int ret = 0; |
| 1600 | |
| 1601 | switch (msr) { |
| 1602 | case APIC_BASE_MSR + (APIC_ICR >> 4): |
| 1603 | ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data); |
| 1604 | break; |
| 1605 | default: |
| 1606 | return EXIT_FASTPATH_NONE; |
| 1607 | } |
| 1608 | |
| 1609 | if (!ret) { |
| 1610 | trace_kvm_msr_write(msr, data); |
| 1611 | return EXIT_FASTPATH_SKIP_EMUL_INS; |
| 1612 | } |
| 1613 | |
| 1614 | return EXIT_FASTPATH_NONE; |
| 1615 | } |
| 1616 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); |
| 1617 | |
| 1618 | /* |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1619 | * Adapt set_msr() to msr_io()'s calling convention |
| 1620 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1621 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1622 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1623 | return __kvm_get_msr(vcpu, index, data, true); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1626 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1627 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1628 | return __kvm_set_msr(vcpu, index, *data, true); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1631 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 1632 | struct pvclock_clock { |
| 1633 | int vclock_mode; |
| 1634 | u64 cycle_last; |
| 1635 | u64 mask; |
| 1636 | u32 mult; |
| 1637 | u32 shift; |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 1638 | u64 base_cycles; |
| 1639 | u64 offset; |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 1640 | }; |
| 1641 | |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1642 | struct pvclock_gtod_data { |
| 1643 | seqcount_t seq; |
| 1644 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 1645 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
| 1646 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1647 | |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 1648 | ktime_t offs_boot; |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 1649 | u64 wall_time_sec; |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1650 | }; |
| 1651 | |
| 1652 | static struct pvclock_gtod_data pvclock_gtod_data; |
| 1653 | |
| 1654 | static void update_pvclock_gtod(struct timekeeper *tk) |
| 1655 | { |
| 1656 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; |
| 1657 | |
| 1658 | write_seqcount_begin(&vdata->seq); |
| 1659 | |
| 1660 | /* copy pvclock gtod data */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1661 | vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode; |
| 1662 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
| 1663 | vdata->clock.mask = tk->tkr_mono.mask; |
| 1664 | vdata->clock.mult = tk->tkr_mono.mult; |
| 1665 | vdata->clock.shift = tk->tkr_mono.shift; |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 1666 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
| 1667 | vdata->clock.offset = tk->tkr_mono.base; |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1668 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 1669 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode; |
| 1670 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
| 1671 | vdata->raw_clock.mask = tk->tkr_raw.mask; |
| 1672 | vdata->raw_clock.mult = tk->tkr_raw.mult; |
| 1673 | vdata->raw_clock.shift = tk->tkr_raw.shift; |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 1674 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
| 1675 | vdata->raw_clock.offset = tk->tkr_raw.base; |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1676 | |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 1677 | vdata->wall_time_sec = tk->xtime_sec; |
| 1678 | |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 1679 | vdata->offs_boot = tk->offs_boot; |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 1680 | |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1681 | write_seqcount_end(&vdata->seq); |
| 1682 | } |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1683 | |
| 1684 | static s64 get_kvmclock_base_ns(void) |
| 1685 | { |
| 1686 | /* Count up from boot time, but with the frequency of the raw clock. */ |
| 1687 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); |
| 1688 | } |
| 1689 | #else |
| 1690 | static s64 get_kvmclock_base_ns(void) |
| 1691 | { |
| 1692 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ |
| 1693 | return ktime_get_boottime_ns(); |
| 1694 | } |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1695 | #endif |
| 1696 | |
Nicholas Krause | bab5bb3 | 2015-01-01 22:05:18 -0500 | [diff] [blame] | 1697 | void kvm_set_pending_timer(struct kvm_vcpu *vcpu) |
| 1698 | { |
Nicholas Krause | bab5bb3 | 2015-01-01 22:05:18 -0500 | [diff] [blame] | 1699 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
Wanpeng Li | 4d151bf | 2019-07-06 09:26:50 +0800 | [diff] [blame] | 1700 | kvm_vcpu_kick(vcpu); |
Nicholas Krause | bab5bb3 | 2015-01-01 22:05:18 -0500 | [diff] [blame] | 1701 | } |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1702 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1703 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 1704 | { |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 1705 | int version; |
| 1706 | int r; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1707 | struct pvclock_wall_clock wc; |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1708 | u64 wall_nsec; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1709 | |
| 1710 | if (!wall_clock) |
| 1711 | return; |
| 1712 | |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 1713 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
| 1714 | if (r) |
| 1715 | return; |
| 1716 | |
| 1717 | if (version & 1) |
| 1718 | ++version; /* first time write, random junk */ |
| 1719 | |
| 1720 | ++version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1721 | |
Nicholas Krause | 1dab134 | 2015-12-30 13:08:46 -0500 | [diff] [blame] | 1722 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
| 1723 | return; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1724 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1725 | /* |
| 1726 | * The guest calculates current wall clock time by adding |
Zachary Amsden | 34c238a | 2010-09-18 14:38:14 -1000 | [diff] [blame] | 1727 | * system time (updated by kvm_guest_time_update below) to the |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1728 | * wall clock specified here. We do the reverse here. |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1729 | */ |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1730 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1731 | |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1732 | wc.nsec = do_div(wall_nsec, 1000000000); |
| 1733 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1734 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1735 | |
| 1736 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 1737 | |
| 1738 | version++; |
| 1739 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1742 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 1743 | { |
Paolo Bonzini | b51012d | 2016-01-22 11:39:22 +0100 | [diff] [blame] | 1744 | do_shl32_div32(dividend, divisor); |
| 1745 | return dividend; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1746 | } |
| 1747 | |
Paolo Bonzini | 3ae13fa | 2016-02-08 15:11:15 +0100 | [diff] [blame] | 1748 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
Zachary Amsden | 5f4e3f8 | 2010-09-18 14:38:13 -1000 | [diff] [blame] | 1749 | s8 *pshift, u32 *pmultiplier) |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1750 | { |
Zachary Amsden | 5f4e3f8 | 2010-09-18 14:38:13 -1000 | [diff] [blame] | 1751 | uint64_t scaled64; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1752 | int32_t shift = 0; |
| 1753 | uint64_t tps64; |
| 1754 | uint32_t tps32; |
| 1755 | |
Paolo Bonzini | 3ae13fa | 2016-02-08 15:11:15 +0100 | [diff] [blame] | 1756 | tps64 = base_hz; |
| 1757 | scaled64 = scaled_hz; |
Jan Kiszka | 5093362 | 2010-09-26 13:00:53 +0200 | [diff] [blame] | 1758 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1759 | tps64 >>= 1; |
| 1760 | shift--; |
| 1761 | } |
| 1762 | |
| 1763 | tps32 = (uint32_t)tps64; |
Jan Kiszka | 5093362 | 2010-09-26 13:00:53 +0200 | [diff] [blame] | 1764 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
| 1765 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) |
Zachary Amsden | 5f4e3f8 | 2010-09-18 14:38:13 -1000 | [diff] [blame] | 1766 | scaled64 >>= 1; |
| 1767 | else |
| 1768 | tps32 <<= 1; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1769 | shift++; |
| 1770 | } |
| 1771 | |
Zachary Amsden | 5f4e3f8 | 2010-09-18 14:38:13 -1000 | [diff] [blame] | 1772 | *pshift = shift; |
| 1773 | *pmultiplier = div_frac(scaled64, tps32); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 1776 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1777 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 1778 | #endif |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 1779 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1780 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
Fengguang Wu | 69b0049 | 2015-01-19 22:33:39 +0800 | [diff] [blame] | 1781 | static unsigned long max_tsc_khz; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1782 | |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1783 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 1784 | { |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1785 | u64 v = (u64)khz * (1000000 + ppm); |
| 1786 | do_div(v, 1000000); |
| 1787 | return v; |
| 1788 | } |
| 1789 | |
Haozhong Zhang | 381d585 | 2015-10-20 15:39:04 +0800 | [diff] [blame] | 1790 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
| 1791 | { |
| 1792 | u64 ratio; |
| 1793 | |
| 1794 | /* Guest TSC same frequency as host TSC? */ |
| 1795 | if (!scale) { |
| 1796 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | /* TSC scaling supported? */ |
| 1801 | if (!kvm_has_tsc_control) { |
| 1802 | if (user_tsc_khz > tsc_khz) { |
| 1803 | vcpu->arch.tsc_catchup = 1; |
| 1804 | vcpu->arch.tsc_always_catchup = 1; |
| 1805 | return 0; |
| 1806 | } else { |
Paolo Bonzini | 3f16a5c | 2019-06-26 14:16:13 +0200 | [diff] [blame] | 1807 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
Haozhong Zhang | 381d585 | 2015-10-20 15:39:04 +0800 | [diff] [blame] | 1808 | return -1; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | /* TSC scaling required - calculate ratio */ |
| 1813 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, |
| 1814 | user_tsc_khz, tsc_khz); |
| 1815 | |
| 1816 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { |
Paolo Bonzini | 3f16a5c | 2019-06-26 14:16:13 +0200 | [diff] [blame] | 1817 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
| 1818 | user_tsc_khz); |
Haozhong Zhang | 381d585 | 2015-10-20 15:39:04 +0800 | [diff] [blame] | 1819 | return -1; |
| 1820 | } |
| 1821 | |
| 1822 | vcpu->arch.tsc_scaling_ratio = ratio; |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Paolo Bonzini | 4941b8c | 2016-02-08 14:51:12 +0100 | [diff] [blame] | 1826 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1827 | { |
| 1828 | u32 thresh_lo, thresh_hi; |
| 1829 | int use_scaling = 0; |
| 1830 | |
Marcelo Tosatti | 03ba32c | 2013-03-11 23:10:24 -0300 | [diff] [blame] | 1831 | /* tsc_khz can be zero if TSC calibration fails */ |
Paolo Bonzini | 4941b8c | 2016-02-08 14:51:12 +0100 | [diff] [blame] | 1832 | if (user_tsc_khz == 0) { |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 1833 | /* set tsc_scaling_ratio to a safe value */ |
| 1834 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; |
Haozhong Zhang | 381d585 | 2015-10-20 15:39:04 +0800 | [diff] [blame] | 1835 | return -1; |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 1836 | } |
Marcelo Tosatti | 03ba32c | 2013-03-11 23:10:24 -0300 | [diff] [blame] | 1837 | |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 1838 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
Paolo Bonzini | 3ae13fa | 2016-02-08 15:11:15 +0100 | [diff] [blame] | 1839 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1840 | &vcpu->arch.virtual_tsc_shift, |
| 1841 | &vcpu->arch.virtual_tsc_mult); |
Paolo Bonzini | 4941b8c | 2016-02-08 14:51:12 +0100 | [diff] [blame] | 1842 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1843 | |
| 1844 | /* |
| 1845 | * Compute the variation in TSC rate which is acceptable |
| 1846 | * within the range of tolerance and decide if the |
| 1847 | * rate being applied is within that bounds of the hardware |
| 1848 | * rate. If so, no scaling or compensation need be done. |
| 1849 | */ |
| 1850 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); |
| 1851 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); |
Paolo Bonzini | 4941b8c | 2016-02-08 14:51:12 +0100 | [diff] [blame] | 1852 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
| 1853 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1854 | use_scaling = 1; |
| 1855 | } |
Paolo Bonzini | 4941b8c | 2016-02-08 14:51:12 +0100 | [diff] [blame] | 1856 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) |
| 1860 | { |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 1861 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1862 | vcpu->arch.virtual_tsc_mult, |
| 1863 | vcpu->arch.virtual_tsc_shift); |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 1864 | tsc += vcpu->arch.this_tsc_write; |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 1865 | return tsc; |
| 1866 | } |
| 1867 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 1868 | static inline int gtod_is_based_on_tsc(int mode) |
| 1869 | { |
| 1870 | return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK; |
| 1871 | } |
| 1872 | |
Fengguang Wu | 69b0049 | 2015-01-19 22:33:39 +0800 | [diff] [blame] | 1873 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 1874 | { |
| 1875 | #ifdef CONFIG_X86_64 |
| 1876 | bool vcpus_matched; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 1877 | struct kvm_arch *ka = &vcpu->kvm->arch; |
| 1878 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 1879 | |
| 1880 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == |
| 1881 | atomic_read(&vcpu->kvm->online_vcpus)); |
| 1882 | |
Marcelo Tosatti | 7f18792 | 2014-11-04 21:30:44 -0200 | [diff] [blame] | 1883 | /* |
| 1884 | * Once the masterclock is enabled, always perform request in |
| 1885 | * order to update it. |
| 1886 | * |
| 1887 | * In order to enable masterclock, the host clocksource must be TSC |
| 1888 | * and the vcpus need to have matched TSCs. When that happens, |
| 1889 | * perform request to enable masterclock. |
| 1890 | */ |
| 1891 | if (ka->use_master_clock || |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 1892 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 1893 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
| 1894 | |
| 1895 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, |
| 1896 | atomic_read(&vcpu->kvm->online_vcpus), |
| 1897 | ka->use_master_clock, gtod->clock.vclock_mode); |
| 1898 | #endif |
| 1899 | } |
| 1900 | |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1901 | static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset) |
| 1902 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1903 | u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu); |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1904 | vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset; |
| 1905 | } |
| 1906 | |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 1907 | /* |
| 1908 | * Multiply tsc by a fixed point number represented by ratio. |
| 1909 | * |
| 1910 | * The most significant 64-N bits (mult) of ratio represent the |
| 1911 | * integral part of the fixed point number; the remaining N bits |
| 1912 | * (frac) represent the fractional part, ie. ratio represents a fixed |
| 1913 | * point number (mult + frac * 2^(-N)). |
| 1914 | * |
| 1915 | * N equals to kvm_tsc_scaling_ratio_frac_bits. |
| 1916 | */ |
| 1917 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) |
| 1918 | { |
| 1919 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); |
| 1920 | } |
| 1921 | |
| 1922 | u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) |
| 1923 | { |
| 1924 | u64 _tsc = tsc; |
| 1925 | u64 ratio = vcpu->arch.tsc_scaling_ratio; |
| 1926 | |
| 1927 | if (ratio != kvm_default_tsc_scaling_ratio) |
| 1928 | _tsc = __scale_tsc(ratio, tsc); |
| 1929 | |
| 1930 | return _tsc; |
| 1931 | } |
| 1932 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); |
| 1933 | |
Haozhong Zhang | 07c1419 | 2015-10-20 15:39:05 +0800 | [diff] [blame] | 1934 | static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 1935 | { |
| 1936 | u64 tsc; |
| 1937 | |
| 1938 | tsc = kvm_scale_tsc(vcpu, rdtsc()); |
| 1939 | |
| 1940 | return target_tsc - tsc; |
| 1941 | } |
| 1942 | |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 1943 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
| 1944 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1945 | u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu); |
| 1946 | |
| 1947 | return tsc_offset + kvm_scale_tsc(vcpu, host_tsc); |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 1948 | } |
| 1949 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); |
| 1950 | |
Luiz Capitulino | a545ab6 | 2016-09-07 14:47:19 -0400 | [diff] [blame] | 1951 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 1952 | { |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1953 | vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset); |
Luiz Capitulino | a545ab6 | 2016-09-07 14:47:19 -0400 | [diff] [blame] | 1954 | } |
| 1955 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 1956 | static inline bool kvm_check_tsc_unstable(void) |
| 1957 | { |
| 1958 | #ifdef CONFIG_X86_64 |
| 1959 | /* |
| 1960 | * TSC is marked unstable when we're running on Hyper-V, |
| 1961 | * 'TSC page' clocksource is good. |
| 1962 | */ |
| 1963 | if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK) |
| 1964 | return false; |
| 1965 | #endif |
| 1966 | return check_tsc_unstable(); |
| 1967 | } |
| 1968 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1969 | void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1970 | { |
| 1971 | struct kvm *kvm = vcpu->kvm; |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 1972 | u64 offset, ns, elapsed; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1973 | unsigned long flags; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 1974 | bool matched; |
Tomasz Grabiec | 0d3da0d | 2014-06-24 09:42:43 +0200 | [diff] [blame] | 1975 | bool already_matched; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1976 | u64 data = msr->data; |
Denis Plotnikov | c5e8ec8 | 2017-04-07 12:09:52 +0300 | [diff] [blame] | 1977 | bool synchronizing = false; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1978 | |
Jan Kiszka | 038f8c1 | 2011-02-04 10:49:11 +0100 | [diff] [blame] | 1979 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
Haozhong Zhang | 07c1419 | 2015-10-20 15:39:05 +0800 | [diff] [blame] | 1980 | offset = kvm_compute_tsc_offset(vcpu, data); |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 1981 | ns = get_kvmclock_base_ns(); |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 1982 | elapsed = ns - kvm->arch.last_tsc_nsec; |
Zachary Amsden | 5d3cb0f6 | 2012-02-03 15:43:51 -0200 | [diff] [blame] | 1983 | |
Marcelo Tosatti | 03ba32c | 2013-03-11 23:10:24 -0300 | [diff] [blame] | 1984 | if (vcpu->arch.virtual_tsc_khz) { |
Denis Plotnikov | bd8fab3 | 2017-04-07 12:09:53 +0300 | [diff] [blame] | 1985 | if (data == 0 && msr->host_initiated) { |
| 1986 | /* |
| 1987 | * detection of vcpu initialization -- need to sync |
| 1988 | * with other vCPUs. This particularly helps to keep |
| 1989 | * kvm_clock stable after CPU hotplug |
| 1990 | */ |
| 1991 | synchronizing = true; |
| 1992 | } else { |
| 1993 | u64 tsc_exp = kvm->arch.last_tsc_write + |
| 1994 | nsec_to_cycles(vcpu, elapsed); |
| 1995 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; |
| 1996 | /* |
| 1997 | * Special case: TSC write with a small delta (1 second) |
| 1998 | * of virtual cycle time against real time is |
| 1999 | * interpreted as an attempt to synchronize the CPU. |
| 2000 | */ |
| 2001 | synchronizing = data < tsc_exp + tsc_hz && |
| 2002 | data + tsc_hz > tsc_exp; |
| 2003 | } |
Denis Plotnikov | c5e8ec8 | 2017-04-07 12:09:52 +0300 | [diff] [blame] | 2004 | } |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 2005 | |
| 2006 | /* |
Zachary Amsden | 5d3cb0f6 | 2012-02-03 15:43:51 -0200 | [diff] [blame] | 2007 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
| 2008 | * TSC, we add elapsed time in this computation. We could let the |
| 2009 | * compensation code attempt to catch up if we fall behind, but |
| 2010 | * it's better to try to match offsets from the beginning. |
| 2011 | */ |
Denis Plotnikov | c5e8ec8 | 2017-04-07 12:09:52 +0300 | [diff] [blame] | 2012 | if (synchronizing && |
Zachary Amsden | 5d3cb0f6 | 2012-02-03 15:43:51 -0200 | [diff] [blame] | 2013 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2014 | if (!kvm_check_tsc_unstable()) { |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2015 | offset = kvm->arch.cur_tsc_offset; |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 2016 | } else { |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 2017 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
Zachary Amsden | 5d3cb0f6 | 2012-02-03 15:43:51 -0200 | [diff] [blame] | 2018 | data += delta; |
Haozhong Zhang | 07c1419 | 2015-10-20 15:39:05 +0800 | [diff] [blame] | 2019 | offset = kvm_compute_tsc_offset(vcpu, data); |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 2020 | } |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2021 | matched = true; |
Tomasz Grabiec | 0d3da0d | 2014-06-24 09:42:43 +0200 | [diff] [blame] | 2022 | already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2023 | } else { |
| 2024 | /* |
| 2025 | * We split periods of matched TSC writes into generations. |
| 2026 | * For each generation, we track the original measured |
| 2027 | * nanosecond time, offset, and write, so if TSCs are in |
| 2028 | * sync, we can match exact offset, and if not, we can match |
Guo Chao | 4a96998 | 2012-06-28 15:17:27 +0800 | [diff] [blame] | 2029 | * exact software computation in compute_guest_tsc() |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2030 | * |
| 2031 | * These values are tracked in kvm->arch.cur_xxx variables. |
| 2032 | */ |
| 2033 | kvm->arch.cur_tsc_generation++; |
| 2034 | kvm->arch.cur_tsc_nsec = ns; |
| 2035 | kvm->arch.cur_tsc_write = data; |
| 2036 | kvm->arch.cur_tsc_offset = offset; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2037 | matched = false; |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 2038 | } |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2039 | |
| 2040 | /* |
| 2041 | * We also track th most recent recorded KHZ, write and time to |
| 2042 | * allow the matching interval to be extended at each write. |
| 2043 | */ |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 2044 | kvm->arch.last_tsc_nsec = ns; |
| 2045 | kvm->arch.last_tsc_write = data; |
Zachary Amsden | 5d3cb0f6 | 2012-02-03 15:43:51 -0200 | [diff] [blame] | 2046 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2047 | |
Zachary Amsden | b183aa5 | 2012-02-03 15:43:53 -0200 | [diff] [blame] | 2048 | vcpu->arch.last_guest_tsc = data; |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2049 | |
| 2050 | /* Keep track of which generation this VCPU has synchronized to */ |
| 2051 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; |
| 2052 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; |
| 2053 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; |
| 2054 | |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2055 | if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2056 | update_ia32_tsc_adjust_msr(vcpu, offset); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2057 | |
Luiz Capitulino | a545ab6 | 2016-09-07 14:47:19 -0400 | [diff] [blame] | 2058 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2059 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2060 | |
| 2061 | spin_lock(&kvm->arch.pvclock_gtod_sync_lock); |
Tomasz Grabiec | 0d3da0d | 2014-06-24 09:42:43 +0200 | [diff] [blame] | 2062 | if (!matched) { |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2063 | kvm->arch.nr_vcpus_matched_tsc = 0; |
Tomasz Grabiec | 0d3da0d | 2014-06-24 09:42:43 +0200 | [diff] [blame] | 2064 | } else if (!already_matched) { |
| 2065 | kvm->arch.nr_vcpus_matched_tsc++; |
| 2066 | } |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2067 | |
| 2068 | kvm_track_tsc_matching(vcpu); |
| 2069 | spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2070 | } |
Zachary Amsden | e26101b | 2012-02-03 15:43:57 -0200 | [diff] [blame] | 2071 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2072 | EXPORT_SYMBOL_GPL(kvm_write_tsc); |
| 2073 | |
Haozhong Zhang | 58ea676 | 2015-10-20 15:39:06 +0800 | [diff] [blame] | 2074 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
| 2075 | s64 adjustment) |
| 2076 | { |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 2077 | u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu); |
| 2078 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
Haozhong Zhang | 58ea676 | 2015-10-20 15:39:06 +0800 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) |
| 2082 | { |
| 2083 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) |
| 2084 | WARN_ON(adjustment < 0); |
| 2085 | adjustment = kvm_scale_tsc(vcpu, (u64) adjustment); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 2086 | adjust_tsc_offset_guest(vcpu, adjustment); |
Haozhong Zhang | 58ea676 | 2015-10-20 15:39:06 +0800 | [diff] [blame] | 2087 | } |
| 2088 | |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2089 | #ifdef CONFIG_X86_64 |
| 2090 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2091 | static u64 read_tsc(void) |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2092 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2093 | u64 ret = (u64)rdtsc_ordered(); |
Andy Lutomirski | 03b9730 | 2015-06-25 18:44:08 +0200 | [diff] [blame] | 2094 | u64 last = pvclock_gtod_data.clock.cycle_last; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2095 | |
| 2096 | if (likely(ret >= last)) |
| 2097 | return ret; |
| 2098 | |
| 2099 | /* |
| 2100 | * GCC likes to generate cmov here, but this branch is extremely |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 2101 | * predictable (it's just a function of time and the likely is |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2102 | * very likely) and there's a data dependence, so force GCC |
| 2103 | * to generate a branch instead. I don't barrier() because |
| 2104 | * we don't actually need a barrier, and if this function |
| 2105 | * ever gets inlined it will generate worse code. |
| 2106 | */ |
| 2107 | asm volatile (""); |
| 2108 | return last; |
| 2109 | } |
| 2110 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2111 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
| 2112 | int *mode) |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2113 | { |
| 2114 | long v; |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2115 | u64 tsc_pg_val; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2116 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2117 | switch (clock->vclock_mode) { |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2118 | case VCLOCK_HVCLOCK: |
| 2119 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
| 2120 | tsc_timestamp); |
| 2121 | if (tsc_pg_val != U64_MAX) { |
| 2122 | /* TSC page valid */ |
| 2123 | *mode = VCLOCK_HVCLOCK; |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2124 | v = (tsc_pg_val - clock->cycle_last) & |
| 2125 | clock->mask; |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2126 | } else { |
| 2127 | /* TSC page invalid */ |
| 2128 | *mode = VCLOCK_NONE; |
| 2129 | } |
| 2130 | break; |
| 2131 | case VCLOCK_TSC: |
| 2132 | *mode = VCLOCK_TSC; |
| 2133 | *tsc_timestamp = read_tsc(); |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2134 | v = (*tsc_timestamp - clock->cycle_last) & |
| 2135 | clock->mask; |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2136 | break; |
| 2137 | default: |
| 2138 | *mode = VCLOCK_NONE; |
| 2139 | } |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2140 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2141 | if (*mode == VCLOCK_NONE) |
| 2142 | *tsc_timestamp = v = 0; |
| 2143 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2144 | return v * clock->mult; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2145 | } |
| 2146 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2147 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2148 | { |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2149 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
Thomas Gleixner | cbcf2dd | 2014-07-16 21:04:54 +0000 | [diff] [blame] | 2150 | unsigned long seq; |
| 2151 | int mode; |
| 2152 | u64 ns; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2153 | |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2154 | do { |
| 2155 | seq = read_seqcount_begin(>od->seq); |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 2156 | ns = gtod->raw_clock.base_cycles; |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2157 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 2158 | ns >>= gtod->raw_clock.shift; |
| 2159 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2160 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
Thomas Gleixner | cbcf2dd | 2014-07-16 21:04:54 +0000 | [diff] [blame] | 2161 | *t = ns; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2162 | |
| 2163 | return mode; |
| 2164 | } |
| 2165 | |
Arnd Bergmann | 899a31f | 2018-04-23 10:04:26 +0200 | [diff] [blame] | 2166 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2167 | { |
| 2168 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 2169 | unsigned long seq; |
| 2170 | int mode; |
| 2171 | u64 ns; |
| 2172 | |
| 2173 | do { |
| 2174 | seq = read_seqcount_begin(>od->seq); |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2175 | ts->tv_sec = gtod->wall_time_sec; |
Paolo Bonzini | 917f947 | 2020-01-22 14:32:20 +0100 | [diff] [blame] | 2176 | ns = gtod->clock.base_cycles; |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2177 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2178 | ns >>= gtod->clock.shift; |
| 2179 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
| 2180 | |
| 2181 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 2182 | ts->tv_nsec = ns; |
| 2183 | |
| 2184 | return mode; |
| 2185 | } |
| 2186 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2187 | /* returns true if host is using TSC based clocksource */ |
| 2188 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2189 | { |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2190 | /* checked again under seqlock below */ |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2191 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2192 | return false; |
| 2193 | |
Marcelo Tosatti | 53fafdb | 2019-10-28 12:36:22 -0200 | [diff] [blame] | 2194 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2195 | tsc_timestamp)); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2196 | } |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2197 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2198 | /* returns true if host is using TSC based clocksource */ |
Arnd Bergmann | 899a31f | 2018-04-23 10:04:26 +0200 | [diff] [blame] | 2199 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2200 | u64 *tsc_timestamp) |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2201 | { |
| 2202 | /* checked again under seqlock below */ |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2203 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2204 | return false; |
| 2205 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 2206 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 2207 | } |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2208 | #endif |
| 2209 | |
| 2210 | /* |
| 2211 | * |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2212 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
| 2213 | * across virtual CPUs, the following condition is possible. |
| 2214 | * Each numbered line represents an event visible to both |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2215 | * CPUs at the next numbered event. |
| 2216 | * |
| 2217 | * "timespecX" represents host monotonic time. "tscX" represents |
| 2218 | * RDTSC value. |
| 2219 | * |
| 2220 | * VCPU0 on CPU0 | VCPU1 on CPU1 |
| 2221 | * |
| 2222 | * 1. read timespec0,tsc0 |
| 2223 | * 2. | timespec1 = timespec0 + N |
| 2224 | * | tsc1 = tsc0 + M |
| 2225 | * 3. transition to guest | transition to guest |
| 2226 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | |
| 2227 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) |
| 2228 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) |
| 2229 | * |
| 2230 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: |
| 2231 | * |
| 2232 | * - ret0 < ret1 |
| 2233 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) |
| 2234 | * ... |
| 2235 | * - 0 < N - M => M < N |
| 2236 | * |
| 2237 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not |
| 2238 | * always the case (the difference between two distinct xtime instances |
| 2239 | * might be smaller then the difference between corresponding TSC reads, |
| 2240 | * when updating guest vcpus pvclock areas). |
| 2241 | * |
| 2242 | * To avoid that problem, do not allow visibility of distinct |
| 2243 | * system_timestamp/tsc_timestamp values simultaneously: use a master |
| 2244 | * copy of host monotonic time values. Update that master copy |
| 2245 | * in lockstep. |
| 2246 | * |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2247 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2248 | * |
| 2249 | */ |
| 2250 | |
| 2251 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) |
| 2252 | { |
| 2253 | #ifdef CONFIG_X86_64 |
| 2254 | struct kvm_arch *ka = &kvm->arch; |
| 2255 | int vclock_mode; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2256 | bool host_tsc_clocksource, vcpus_matched; |
| 2257 | |
| 2258 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == |
| 2259 | atomic_read(&kvm->online_vcpus)); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2260 | |
| 2261 | /* |
| 2262 | * If the host uses TSC clock, then passthrough TSC as stable |
| 2263 | * to the guest. |
| 2264 | */ |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2265 | host_tsc_clocksource = kvm_get_time_and_clockread( |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2266 | &ka->master_kernel_ns, |
| 2267 | &ka->master_cycle_now); |
| 2268 | |
Marcelo Tosatti | 16a9602 | 2014-05-14 12:43:24 -0300 | [diff] [blame] | 2269 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
Ladi Prosek | a826faf | 2017-06-26 09:56:43 +0200 | [diff] [blame] | 2270 | && !ka->backwards_tsc_observed |
Marcelo Tosatti | 54750f2 | 2015-01-20 15:54:52 -0200 | [diff] [blame] | 2271 | && !ka->boot_vcpu_runs_old_kvmclock; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2272 | |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2273 | if (ka->use_master_clock) |
| 2274 | atomic_set(&kvm_guest_has_master_clock, 1); |
| 2275 | |
| 2276 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; |
Marcelo Tosatti | b48aa97 | 2012-11-27 23:29:03 -0200 | [diff] [blame] | 2277 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
| 2278 | vcpus_matched); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2279 | #endif |
| 2280 | } |
| 2281 | |
Paolo Bonzini | 2860c4b | 2016-01-07 15:05:10 +0100 | [diff] [blame] | 2282 | void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
| 2283 | { |
| 2284 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); |
| 2285 | } |
| 2286 | |
Marcelo Tosatti | 2e762ff | 2013-08-27 23:55:29 -0300 | [diff] [blame] | 2287 | static void kvm_gen_update_masterclock(struct kvm *kvm) |
| 2288 | { |
| 2289 | #ifdef CONFIG_X86_64 |
| 2290 | int i; |
| 2291 | struct kvm_vcpu *vcpu; |
| 2292 | struct kvm_arch *ka = &kvm->arch; |
| 2293 | |
| 2294 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2295 | kvm_make_mclock_inprogress_request(kvm); |
| 2296 | /* no guest entries from this point */ |
| 2297 | pvclock_update_vm_gtod_copy(kvm); |
| 2298 | |
| 2299 | kvm_for_each_vcpu(i, vcpu, kvm) |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 2300 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Marcelo Tosatti | 2e762ff | 2013-08-27 23:55:29 -0300 | [diff] [blame] | 2301 | |
| 2302 | /* guest entries allowed */ |
| 2303 | kvm_for_each_vcpu(i, vcpu, kvm) |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 2304 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
Marcelo Tosatti | 2e762ff | 2013-08-27 23:55:29 -0300 | [diff] [blame] | 2305 | |
| 2306 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 2307 | #endif |
| 2308 | } |
| 2309 | |
Marcelo Tosatti | e891a32 | 2017-04-17 12:51:37 -0300 | [diff] [blame] | 2310 | u64 get_kvmclock_ns(struct kvm *kvm) |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 2311 | { |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 2312 | struct kvm_arch *ka = &kvm->arch; |
Paolo Bonzini | 8b95344 | 2016-11-16 18:31:30 +0100 | [diff] [blame] | 2313 | struct pvclock_vcpu_time_info hv_clock; |
Wanpeng Li | e2c2206 | 2017-05-11 18:12:05 -0700 | [diff] [blame] | 2314 | u64 ret; |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 2315 | |
Paolo Bonzini | 8b95344 | 2016-11-16 18:31:30 +0100 | [diff] [blame] | 2316 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2317 | if (!ka->use_master_clock) { |
| 2318 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 2319 | return get_kvmclock_base_ns() + ka->kvmclock_offset; |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
Paolo Bonzini | 8b95344 | 2016-11-16 18:31:30 +0100 | [diff] [blame] | 2322 | hv_clock.tsc_timestamp = ka->master_cycle_now; |
| 2323 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; |
| 2324 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 2325 | |
Wanpeng Li | e2c2206 | 2017-05-11 18:12:05 -0700 | [diff] [blame] | 2326 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
| 2327 | get_cpu(); |
| 2328 | |
Wanpeng Li | e70b57a | 2017-11-20 14:55:05 -0800 | [diff] [blame] | 2329 | if (__this_cpu_read(cpu_tsc_khz)) { |
| 2330 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, |
| 2331 | &hv_clock.tsc_shift, |
| 2332 | &hv_clock.tsc_to_system_mul); |
| 2333 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); |
| 2334 | } else |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 2335 | ret = get_kvmclock_base_ns() + ka->kvmclock_offset; |
Wanpeng Li | e2c2206 | 2017-05-11 18:12:05 -0700 | [diff] [blame] | 2336 | |
| 2337 | put_cpu(); |
| 2338 | |
| 2339 | return ret; |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2342 | static void kvm_setup_pvclock_page(struct kvm_vcpu *v) |
| 2343 | { |
| 2344 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 2345 | struct pvclock_vcpu_time_info guest_hv_clock; |
| 2346 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2347 | if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2348 | &guest_hv_clock, sizeof(guest_hv_clock)))) |
| 2349 | return; |
| 2350 | |
| 2351 | /* This VCPU is paused, but it's legal for a guest to read another |
| 2352 | * VCPU's kvmclock, so we really have to follow the specification where |
| 2353 | * it says that version is odd if data is being modified, and even after |
| 2354 | * it is consistent. |
| 2355 | * |
| 2356 | * Version field updates must be kept separate. This is because |
| 2357 | * kvm_write_guest_cached might use a "rep movs" instruction, and |
| 2358 | * writes within a string instruction are weakly ordered. So there |
| 2359 | * are three writes overall. |
| 2360 | * |
| 2361 | * As a small optimization, only write the version field in the first |
| 2362 | * and third write. The vcpu->pv_time cache is still valid, because the |
| 2363 | * version field is the first in the struct. |
| 2364 | */ |
| 2365 | BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0); |
| 2366 | |
Liran Alon | 51c4b8b | 2017-11-05 16:11:30 +0200 | [diff] [blame] | 2367 | if (guest_hv_clock.version & 1) |
| 2368 | ++guest_hv_clock.version; /* first time write, random junk */ |
| 2369 | |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2370 | vcpu->hv_clock.version = guest_hv_clock.version + 1; |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2371 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2372 | &vcpu->hv_clock, |
| 2373 | sizeof(vcpu->hv_clock.version)); |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2374 | |
| 2375 | smp_wmb(); |
| 2376 | |
| 2377 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ |
| 2378 | vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); |
| 2379 | |
| 2380 | if (vcpu->pvclock_set_guest_stopped_request) { |
| 2381 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; |
| 2382 | vcpu->pvclock_set_guest_stopped_request = false; |
| 2383 | } |
| 2384 | |
| 2385 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); |
| 2386 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2387 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2388 | &vcpu->hv_clock, |
| 2389 | sizeof(vcpu->hv_clock)); |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2390 | |
| 2391 | smp_wmb(); |
| 2392 | |
| 2393 | vcpu->hv_clock.version++; |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2394 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2395 | &vcpu->hv_clock, |
| 2396 | sizeof(vcpu->hv_clock.version)); |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
Zachary Amsden | 34c238a | 2010-09-18 14:38:14 -1000 | [diff] [blame] | 2399 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2400 | { |
Paolo Bonzini | 78db6a5 | 2016-02-08 14:51:40 +0100 | [diff] [blame] | 2401 | unsigned long flags, tgt_tsc_khz; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2402 | struct kvm_vcpu_arch *vcpu = &v->arch; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2403 | struct kvm_arch *ka = &v->kvm->arch; |
Marcelo Tosatti | f25e656 | 2014-01-06 12:18:59 -0200 | [diff] [blame] | 2404 | s64 kernel_ns; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2405 | u64 tsc_timestamp, host_tsc; |
Marcelo Tosatti | 51d59c6 | 2012-08-03 15:57:49 -0300 | [diff] [blame] | 2406 | u8 pvclock_flags; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2407 | bool use_master_clock; |
| 2408 | |
| 2409 | kernel_ns = 0; |
| 2410 | host_tsc = 0; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 2411 | |
Zachary Amsden | 1d5f066 | 2010-08-19 22:07:30 -1000 | [diff] [blame] | 2412 | /* |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2413 | * If the host uses TSC clock, then passthrough TSC as stable |
| 2414 | * to the guest. |
| 2415 | */ |
| 2416 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2417 | use_master_clock = ka->use_master_clock; |
| 2418 | if (use_master_clock) { |
| 2419 | host_tsc = ka->master_cycle_now; |
| 2420 | kernel_ns = ka->master_kernel_ns; |
| 2421 | } |
| 2422 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
Marcelo Tosatti | c09664b | 2013-03-18 13:54:32 -0300 | [diff] [blame] | 2423 | |
| 2424 | /* Keep irq disabled to prevent changes to the clock */ |
| 2425 | local_irq_save(flags); |
Paolo Bonzini | 78db6a5 | 2016-02-08 14:51:40 +0100 | [diff] [blame] | 2426 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
| 2427 | if (unlikely(tgt_tsc_khz == 0)) { |
Marcelo Tosatti | c09664b | 2013-03-18 13:54:32 -0300 | [diff] [blame] | 2428 | local_irq_restore(flags); |
| 2429 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
| 2430 | return 1; |
| 2431 | } |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2432 | if (!use_master_clock) { |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2433 | host_tsc = rdtsc(); |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 2434 | kernel_ns = get_kvmclock_base_ns(); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 2437 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2438 | |
| 2439 | /* |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 2440 | * We may have to catch up the TSC to match elapsed wall clock |
| 2441 | * time for two reasons, even if kvmclock is used. |
| 2442 | * 1) CPU could have been running below the maximum TSC rate |
| 2443 | * 2) Broken TSC compensation resets the base at each VCPU |
| 2444 | * entry to avoid unknown leaps of TSC even when running |
| 2445 | * again on the same CPU. This may cause apparent elapsed |
| 2446 | * time to disappear, and the guest to stand still or run |
| 2447 | * very slowly. |
| 2448 | */ |
| 2449 | if (vcpu->tsc_catchup) { |
| 2450 | u64 tsc = compute_guest_tsc(v, kernel_ns); |
| 2451 | if (tsc > tsc_timestamp) { |
Marcelo Tosatti | f1e2b26 | 2012-02-03 15:43:55 -0200 | [diff] [blame] | 2452 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 2453 | tsc_timestamp = tsc; |
| 2454 | } |
| 2455 | } |
| 2456 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2457 | local_irq_restore(flags); |
| 2458 | |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2459 | /* With all the info we got, fill in the values */ |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 2460 | |
Paolo Bonzini | 78db6a5 | 2016-02-08 14:51:40 +0100 | [diff] [blame] | 2461 | if (kvm_has_tsc_control) |
| 2462 | tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz); |
| 2463 | |
| 2464 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { |
Paolo Bonzini | 3ae13fa | 2016-02-08 15:11:15 +0100 | [diff] [blame] | 2465 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
Zachary Amsden | 5f4e3f8 | 2010-09-18 14:38:13 -1000 | [diff] [blame] | 2466 | &vcpu->hv_clock.tsc_shift, |
| 2467 | &vcpu->hv_clock.tsc_to_system_mul); |
Paolo Bonzini | 78db6a5 | 2016-02-08 14:51:40 +0100 | [diff] [blame] | 2468 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 2469 | } |
| 2470 | |
Zachary Amsden | 1d5f066 | 2010-08-19 22:07:30 -1000 | [diff] [blame] | 2471 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
Zachary Amsden | 759379d | 2010-08-19 22:07:25 -1000 | [diff] [blame] | 2472 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
Zachary Amsden | 28e4639 | 2010-09-18 14:38:12 -1000 | [diff] [blame] | 2473 | vcpu->last_guest_tsc = tsc_timestamp; |
Paolo Bonzini | 9446e6f | 2020-02-12 13:27:10 +0100 | [diff] [blame] | 2474 | WARN_ON((s64)vcpu->hv_clock.system_time < 0); |
Marcelo Tosatti | 51d59c6 | 2012-08-03 15:57:49 -0300 | [diff] [blame] | 2475 | |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2476 | /* If the host uses TSC clocksource, then it is stable */ |
Paolo Bonzini | 0d6dd2f | 2016-09-01 14:20:09 +0200 | [diff] [blame] | 2477 | pvclock_flags = 0; |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 2478 | if (use_master_clock) |
| 2479 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; |
| 2480 | |
Marcelo Tosatti | 78c0337 | 2012-11-27 23:28:47 -0200 | [diff] [blame] | 2481 | vcpu->hv_clock.flags = pvclock_flags; |
| 2482 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 2483 | if (vcpu->pv_time_enabled) |
| 2484 | kvm_setup_pvclock_page(v); |
| 2485 | if (v == kvm_get_vcpu(v->kvm, 0)) |
| 2486 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 2487 | return 0; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2490 | /* |
| 2491 | * kvmclock updates which are isolated to a given vcpu, such as |
| 2492 | * vcpu->cpu migration, should not allow system_timestamp from |
| 2493 | * the rest of the vcpus to remain static. Otherwise ntp frequency |
| 2494 | * correction applies to one vcpu's system_timestamp but not |
| 2495 | * the others. |
| 2496 | * |
| 2497 | * So in those cases, request a kvmclock update for all vcpus. |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 2498 | * We need to rate-limit these requests though, as they can |
| 2499 | * considerably slow guests that have a large number of vcpus. |
| 2500 | * The time for a remote vcpu to update its kvmclock is bound |
| 2501 | * by the delay we use to rate-limit the updates. |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2502 | */ |
| 2503 | |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 2504 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
| 2505 | |
| 2506 | static void kvmclock_update_fn(struct work_struct *work) |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2507 | { |
| 2508 | int i; |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 2509 | struct delayed_work *dwork = to_delayed_work(work); |
| 2510 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, |
| 2511 | kvmclock_update_work); |
| 2512 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2513 | struct kvm_vcpu *vcpu; |
| 2514 | |
| 2515 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 2516 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2517 | kvm_vcpu_kick(vcpu); |
| 2518 | } |
| 2519 | } |
| 2520 | |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 2521 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
| 2522 | { |
| 2523 | struct kvm *kvm = v->kvm; |
| 2524 | |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 2525 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 2526 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
| 2527 | KVMCLOCK_UPDATE_DELAY); |
| 2528 | } |
| 2529 | |
Andrew Jones | 332967a | 2014-02-28 12:52:55 +0100 | [diff] [blame] | 2530 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
| 2531 | |
| 2532 | static void kvmclock_sync_fn(struct work_struct *work) |
| 2533 | { |
| 2534 | struct delayed_work *dwork = to_delayed_work(work); |
| 2535 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, |
| 2536 | kvmclock_sync_work); |
| 2537 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 2538 | |
Marcelo Tosatti | 630994b | 2015-05-12 22:42:04 -0300 | [diff] [blame] | 2539 | if (!kvmclock_periodic_sync) |
| 2540 | return; |
| 2541 | |
Andrew Jones | 332967a | 2014-02-28 12:52:55 +0100 | [diff] [blame] | 2542 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
| 2543 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, |
| 2544 | KVMCLOCK_SYNC_PERIOD); |
| 2545 | } |
| 2546 | |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 2547 | /* |
| 2548 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. |
| 2549 | */ |
| 2550 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) |
| 2551 | { |
| 2552 | /* McStatusWrEn enabled? */ |
Sean Christopherson | 23493d0 | 2020-03-04 17:34:33 -0800 | [diff] [blame] | 2553 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 2554 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
| 2555 | |
| 2556 | return false; |
| 2557 | } |
| 2558 | |
Wanpeng Li | 9ffd986 | 2017-10-19 06:47:56 -0700 | [diff] [blame] | 2559 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2560 | { |
| 2561 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2562 | unsigned bank_num = mcg_cap & 0xff; |
Wanpeng Li | 9ffd986 | 2017-10-19 06:47:56 -0700 | [diff] [blame] | 2563 | u32 msr = msr_info->index; |
| 2564 | u64 data = msr_info->data; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2565 | |
| 2566 | switch (msr) { |
| 2567 | case MSR_IA32_MCG_STATUS: |
| 2568 | vcpu->arch.mcg_status = data; |
| 2569 | break; |
| 2570 | case MSR_IA32_MCG_CTL: |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 2571 | if (!(mcg_cap & MCG_CTL_P) && |
| 2572 | (data || !msr_info->host_initiated)) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2573 | return 1; |
| 2574 | if (data != 0 && data != ~(u64)0) |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 2575 | return 1; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2576 | vcpu->arch.mcg_ctl = data; |
| 2577 | break; |
| 2578 | default: |
| 2579 | if (msr >= MSR_IA32_MC0_CTL && |
Chen Yucong | 81760dc | 2014-09-23 10:44:35 +0800 | [diff] [blame] | 2580 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
Marios Pomonis | 6ec4c5e | 2019-12-11 12:47:49 -0800 | [diff] [blame] | 2581 | u32 offset = array_index_nospec( |
| 2582 | msr - MSR_IA32_MC0_CTL, |
| 2583 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); |
| 2584 | |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 2585 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 2586 | * some Linux kernels though clear bit 10 in bank 4 to |
| 2587 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 2588 | * this to avoid an uncatched #GP in the guest |
| 2589 | */ |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2590 | if ((offset & 0x3) == 0 && |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 2591 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2592 | return -1; |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 2593 | |
| 2594 | /* MCi_STATUS */ |
Wanpeng Li | 9ffd986 | 2017-10-19 06:47:56 -0700 | [diff] [blame] | 2595 | if (!msr_info->host_initiated && |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 2596 | (offset & 0x3) == 1 && data != 0) { |
| 2597 | if (!can_set_mci_status(vcpu)) |
| 2598 | return -1; |
| 2599 | } |
| 2600 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2601 | vcpu->arch.mce_banks[offset] = data; |
| 2602 | break; |
| 2603 | } |
| 2604 | return 1; |
| 2605 | } |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2609 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 2610 | { |
| 2611 | struct kvm *kvm = vcpu->kvm; |
| 2612 | int lm = is_long_mode(vcpu); |
| 2613 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 2614 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 2615 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 2616 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 2617 | u32 page_num = data & ~PAGE_MASK; |
| 2618 | u64 page_addr = data & PAGE_MASK; |
| 2619 | u8 *page; |
| 2620 | int r; |
| 2621 | |
| 2622 | r = -E2BIG; |
| 2623 | if (page_num >= blob_size) |
| 2624 | goto out; |
| 2625 | r = -ENOMEM; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 2626 | page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE); |
| 2627 | if (IS_ERR(page)) { |
| 2628 | r = PTR_ERR(page); |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2629 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 2630 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2631 | if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2632 | goto out_free; |
| 2633 | r = 0; |
| 2634 | out_free: |
| 2635 | kfree(page); |
| 2636 | out: |
| 2637 | return r; |
| 2638 | } |
| 2639 | |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 2640 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
| 2641 | { |
| 2642 | gpa_t gpa = data & ~0x3f; |
| 2643 | |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 2644 | /* Bits 3:5 are reserved, Should be zero */ |
| 2645 | if (data & 0x38) |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 2646 | return 1; |
| 2647 | |
| 2648 | vcpu->arch.apf.msr_val = data; |
| 2649 | |
| 2650 | if (!(data & KVM_ASYNC_PF_ENABLED)) { |
| 2651 | kvm_clear_async_pf_completion_queue(vcpu); |
| 2652 | kvm_async_pf_hash_reset(vcpu); |
| 2653 | return 0; |
| 2654 | } |
| 2655 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2656 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2657 | sizeof(u32))) |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 2658 | return 1; |
| 2659 | |
Gleb Natapov | 6adba52 | 2010-10-14 11:22:55 +0200 | [diff] [blame] | 2660 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 2661 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 2662 | kvm_async_pf_wakeup_all(vcpu); |
| 2663 | return 0; |
| 2664 | } |
| 2665 | |
Glauber Costa | 12f9a48 | 2011-02-01 14:16:40 -0500 | [diff] [blame] | 2666 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
| 2667 | { |
Andy Honig | 0b79459 | 2013-02-20 14:48:10 -0800 | [diff] [blame] | 2668 | vcpu->arch.pv_time_enabled = false; |
Paolo Bonzini | 49dedf0 | 2019-10-10 12:49:22 +0200 | [diff] [blame] | 2669 | vcpu->arch.time = 0; |
Glauber Costa | 12f9a48 | 2011-02-01 14:16:40 -0500 | [diff] [blame] | 2670 | } |
| 2671 | |
Wanpeng Li | f38a7b7 | 2017-12-12 17:33:04 -0800 | [diff] [blame] | 2672 | static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
| 2673 | { |
| 2674 | ++vcpu->stat.tlb_flush; |
| 2675 | kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa); |
| 2676 | } |
| 2677 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2678 | static void record_steal_time(struct kvm_vcpu *vcpu) |
| 2679 | { |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2680 | struct kvm_host_map map; |
| 2681 | struct kvm_steal_time *st; |
| 2682 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2683 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
| 2684 | return; |
| 2685 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2686 | /* -EAGAIN is returned in atomic context so we can just return. */ |
| 2687 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, |
| 2688 | &map, &vcpu->arch.st.cache, false)) |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2689 | return; |
| 2690 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2691 | st = map.hva + |
| 2692 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); |
| 2693 | |
Wanpeng Li | f38a7b7 | 2017-12-12 17:33:04 -0800 | [diff] [blame] | 2694 | /* |
| 2695 | * Doing a TLB flush here, on the guest's behalf, can avoid |
| 2696 | * expensive IPIs. |
| 2697 | */ |
Wanpeng Li | b382f44 | 2019-08-05 10:03:24 +0800 | [diff] [blame] | 2698 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2699 | st->preempted & KVM_VCPU_FLUSH_TLB); |
| 2700 | if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) |
Wanpeng Li | f38a7b7 | 2017-12-12 17:33:04 -0800 | [diff] [blame] | 2701 | kvm_vcpu_flush_tlb(vcpu, false); |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 2702 | |
Boris Ostrovsky | a6bd811 | 2019-12-06 15:36:12 +0000 | [diff] [blame] | 2703 | vcpu->arch.st.preempted = 0; |
Wanpeng Li | 35f3fae1 | 2016-05-03 11:43:10 +0800 | [diff] [blame] | 2704 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2705 | if (st->version & 1) |
| 2706 | st->version += 1; /* first time write, random junk */ |
Wanpeng Li | 35f3fae1 | 2016-05-03 11:43:10 +0800 | [diff] [blame] | 2707 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2708 | st->version += 1; |
Wanpeng Li | 35f3fae1 | 2016-05-03 11:43:10 +0800 | [diff] [blame] | 2709 | |
| 2710 | smp_wmb(); |
| 2711 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2712 | st->steal += current->sched_info.run_delay - |
Liang Chen | c54cdf1 | 2016-03-16 19:33:16 +0800 | [diff] [blame] | 2713 | vcpu->arch.st.last_steal; |
| 2714 | vcpu->arch.st.last_steal = current->sched_info.run_delay; |
Wanpeng Li | 35f3fae1 | 2016-05-03 11:43:10 +0800 | [diff] [blame] | 2715 | |
Wanpeng Li | 35f3fae1 | 2016-05-03 11:43:10 +0800 | [diff] [blame] | 2716 | smp_wmb(); |
| 2717 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2718 | st->version += 1; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2719 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 2720 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2721 | } |
| 2722 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2723 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2724 | { |
Gleb Natapov | 5753785 | 2012-01-15 14:17:22 +0200 | [diff] [blame] | 2725 | bool pr = false; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2726 | u32 msr = msr_info->index; |
| 2727 | u64 data = msr_info->data; |
Gleb Natapov | 5753785 | 2012-01-15 14:17:22 +0200 | [diff] [blame] | 2728 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2729 | switch (msr) { |
Borislav Petkov | 2e32b71 | 2013-02-19 19:33:13 +0100 | [diff] [blame] | 2730 | case MSR_AMD64_NB_CFG: |
Borislav Petkov | 2e32b71 | 2013-02-19 19:33:13 +0100 | [diff] [blame] | 2731 | case MSR_IA32_UCODE_WRITE: |
| 2732 | case MSR_VM_HSAVE_PA: |
| 2733 | case MSR_AMD64_PATCH_LOADER: |
| 2734 | case MSR_AMD64_BU_CFG2: |
Ladi Prosek | 405a353 | 2017-04-06 15:22:20 +0200 | [diff] [blame] | 2735 | case MSR_AMD64_DC_CFG: |
Eduardo Habkost | 0e1b869 | 2018-12-17 22:34:18 -0200 | [diff] [blame] | 2736 | case MSR_F15H_EX_CFG: |
Borislav Petkov | 2e32b71 | 2013-02-19 19:33:13 +0100 | [diff] [blame] | 2737 | break; |
| 2738 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 2739 | case MSR_IA32_UCODE_REV: |
| 2740 | if (msr_info->host_initiated) |
| 2741 | vcpu->arch.microcode_version = data; |
| 2742 | break; |
Sean Christopherson | 0cf9135 | 2019-03-07 15:43:02 -0800 | [diff] [blame] | 2743 | case MSR_IA32_ARCH_CAPABILITIES: |
| 2744 | if (!msr_info->host_initiated) |
| 2745 | return 1; |
| 2746 | vcpu->arch.arch_capabilities = data; |
| 2747 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2748 | case MSR_EFER: |
Sean Christopherson | 1198849 | 2019-04-02 08:19:15 -0700 | [diff] [blame] | 2749 | return set_efer(vcpu, msr_info); |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 2750 | case MSR_K7_HWCR: |
| 2751 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
Joerg Roedel | 8249402 | 2010-02-24 18:59:16 +0100 | [diff] [blame] | 2752 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
Nicolae Mogoreanu | a223c31 | 2012-02-21 13:44:21 -0800 | [diff] [blame] | 2753 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 2754 | |
| 2755 | /* Handle McStatusWrEn */ |
| 2756 | if (data == BIT_ULL(18)) { |
| 2757 | vcpu->arch.msr_hwcr = data; |
| 2758 | } else if (data != 0) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2759 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 2760 | data); |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 2761 | return 1; |
| 2762 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2763 | break; |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 2764 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 2765 | if (data != 0) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2766 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 2767 | "0x%llx\n", data); |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 2768 | return 1; |
| 2769 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2770 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 2771 | case MSR_IA32_DEBUGCTLMSR: |
| 2772 | if (!data) { |
| 2773 | /* We support the non-activated case already */ |
| 2774 | break; |
| 2775 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 2776 | /* Values other than LBR and BTF are vendor-specific, |
| 2777 | thus reserved and should throw a #GP */ |
| 2778 | return 1; |
| 2779 | } |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2780 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 2781 | __func__, data); |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 2782 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 2783 | case 0x200 ... 0x2ff: |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 2784 | return kvm_mtrr_set_msr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2785 | case MSR_IA32_APICBASE: |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 2786 | return kvm_set_apic_base(vcpu, msr_info); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2787 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 2788 | return kvm_x2apic_msr_write(vcpu, msr, data); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2789 | case MSR_IA32_TSCDEADLINE: |
| 2790 | kvm_set_lapic_tscdeadline_msr(vcpu, data); |
| 2791 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2792 | case MSR_IA32_TSC_ADJUST: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2793 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2794 | if (!msr_info->host_initiated) { |
Chris J Arges | d913b90 | 2014-11-12 21:00:39 -0600 | [diff] [blame] | 2795 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
Haozhong Zhang | d7add05 | 2015-08-07 11:24:32 +0800 | [diff] [blame] | 2796 | adjust_tsc_offset_guest(vcpu, adj); |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2797 | } |
| 2798 | vcpu->arch.ia32_tsc_adjust_msr = data; |
| 2799 | } |
| 2800 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2801 | case MSR_IA32_MISC_ENABLE: |
Wanpeng Li | 511a8556 | 2019-05-21 14:06:54 +0800 | [diff] [blame] | 2802 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
| 2803 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { |
| 2804 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) |
| 2805 | return 1; |
| 2806 | vcpu->arch.ia32_misc_enable_msr = data; |
| 2807 | kvm_update_cpuid(vcpu); |
| 2808 | } else { |
| 2809 | vcpu->arch.ia32_misc_enable_msr = data; |
| 2810 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2811 | break; |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 2812 | case MSR_IA32_SMBASE: |
| 2813 | if (!msr_info->host_initiated) |
| 2814 | return 1; |
| 2815 | vcpu->arch.smbase = data; |
| 2816 | break; |
Paolo Bonzini | 73f624f | 2019-06-06 14:32:59 +0200 | [diff] [blame] | 2817 | case MSR_IA32_POWER_CTL: |
| 2818 | vcpu->arch.msr_ia32_power_ctl = data; |
| 2819 | break; |
Paolo Bonzini | dd25993 | 2018-04-13 11:38:35 +0200 | [diff] [blame] | 2820 | case MSR_IA32_TSC: |
| 2821 | kvm_write_tsc(vcpu, msr_info); |
| 2822 | break; |
Aaron Lewis | 864e2ab | 2019-10-21 16:30:26 -0700 | [diff] [blame] | 2823 | case MSR_IA32_XSS: |
| 2824 | if (!msr_info->host_initiated && |
| 2825 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) |
| 2826 | return 1; |
| 2827 | /* |
Sean Christopherson | a1bead2 | 2020-03-02 15:57:00 -0800 | [diff] [blame] | 2828 | * KVM supports exposing PT to the guest, but does not support |
| 2829 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than |
| 2830 | * XSAVES/XRSTORS to save/restore PT MSRs. |
Aaron Lewis | 864e2ab | 2019-10-21 16:30:26 -0700 | [diff] [blame] | 2831 | */ |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 2832 | if (data & ~supported_xss) |
Aaron Lewis | 864e2ab | 2019-10-21 16:30:26 -0700 | [diff] [blame] | 2833 | return 1; |
| 2834 | vcpu->arch.ia32_xss = data; |
| 2835 | break; |
Liran Alon | 52797bf | 2017-11-15 13:43:14 +0200 | [diff] [blame] | 2836 | case MSR_SMI_COUNT: |
| 2837 | if (!msr_info->host_initiated) |
| 2838 | return 1; |
| 2839 | vcpu->arch.smi_count = data; |
| 2840 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 2841 | case MSR_KVM_WALL_CLOCK_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2842 | case MSR_KVM_WALL_CLOCK: |
| 2843 | vcpu->kvm->arch.wall_clock = data; |
| 2844 | kvm_write_wall_clock(vcpu->kvm, data); |
| 2845 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 2846 | case MSR_KVM_SYSTEM_TIME_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2847 | case MSR_KVM_SYSTEM_TIME: { |
Marcelo Tosatti | 54750f2 | 2015-01-20 15:54:52 -0200 | [diff] [blame] | 2848 | struct kvm_arch *ka = &vcpu->kvm->arch; |
| 2849 | |
Marcelo Tosatti | 54750f2 | 2015-01-20 15:54:52 -0200 | [diff] [blame] | 2850 | if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) { |
| 2851 | bool tmp = (msr == MSR_KVM_SYSTEM_TIME); |
| 2852 | |
| 2853 | if (ka->boot_vcpu_runs_old_kvmclock != tmp) |
Radim Krčmář | 1bd2009 | 2017-04-26 22:32:20 +0200 | [diff] [blame] | 2854 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Marcelo Tosatti | 54750f2 | 2015-01-20 15:54:52 -0200 | [diff] [blame] | 2855 | |
| 2856 | ka->boot_vcpu_runs_old_kvmclock = tmp; |
| 2857 | } |
| 2858 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2859 | vcpu->arch.time = data; |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 2860 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2861 | |
| 2862 | /* we verify if the enable bit is set... */ |
Paolo Bonzini | 49dedf0 | 2019-10-10 12:49:22 +0200 | [diff] [blame] | 2863 | vcpu->arch.pv_time_enabled = false; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2864 | if (!(data & 1)) |
| 2865 | break; |
| 2866 | |
Paolo Bonzini | 49dedf0 | 2019-10-10 12:49:22 +0200 | [diff] [blame] | 2867 | if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2868 | &vcpu->arch.pv_time, data & ~1ULL, |
| 2869 | sizeof(struct pvclock_vcpu_time_info))) |
Andy Honig | 0b79459 | 2013-02-20 14:48:10 -0800 | [diff] [blame] | 2870 | vcpu->arch.pv_time_enabled = true; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 2871 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 2872 | break; |
| 2873 | } |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 2874 | case MSR_KVM_ASYNC_PF_EN: |
| 2875 | if (kvm_pv_enable_async_pf(vcpu, data)) |
| 2876 | return 1; |
| 2877 | break; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2878 | case MSR_KVM_STEAL_TIME: |
| 2879 | |
| 2880 | if (unlikely(!sched_info_on())) |
| 2881 | return 1; |
| 2882 | |
| 2883 | if (data & KVM_STEAL_RESERVED_MASK) |
| 2884 | return 1; |
| 2885 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2886 | vcpu->arch.st.msr_val = data; |
| 2887 | |
| 2888 | if (!(data & KVM_MSR_ENABLED)) |
| 2889 | break; |
| 2890 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2891 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
| 2892 | |
| 2893 | break; |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2894 | case MSR_KVM_PV_EOI_EN: |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 2895 | if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2896 | return 1; |
| 2897 | break; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 2898 | |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 2899 | case MSR_KVM_POLL_CONTROL: |
| 2900 | /* only enable bit supported */ |
| 2901 | if (data & (-1ULL << 1)) |
| 2902 | return 1; |
| 2903 | |
| 2904 | vcpu->arch.msr_kvm_poll_control = data; |
| 2905 | break; |
| 2906 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2907 | case MSR_IA32_MCG_CTL: |
| 2908 | case MSR_IA32_MCG_STATUS: |
Chen Yucong | 81760dc | 2014-09-23 10:44:35 +0800 | [diff] [blame] | 2909 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
Wanpeng Li | 9ffd986 | 2017-10-19 06:47:56 -0700 | [diff] [blame] | 2910 | return set_msr_mce(vcpu, msr_info); |
Andre Przywara | 71db602 | 2009-06-12 22:01:29 +0200 | [diff] [blame] | 2911 | |
Wei Huang | 6912ac3 | 2015-06-12 01:34:56 -0400 | [diff] [blame] | 2912 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
| 2913 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: |
| 2914 | pr = true; /* fall through */ |
| 2915 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
| 2916 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 2917 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 2918 | return kvm_pmu_set_msr(vcpu, msr_info); |
Gleb Natapov | 5753785 | 2012-01-15 14:17:22 +0200 | [diff] [blame] | 2919 | |
| 2920 | if (pr || data != 0) |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2921 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
| 2922 | "0x%x data 0x%llx\n", msr, data); |
Gleb Natapov | 5753785 | 2012-01-15 14:17:22 +0200 | [diff] [blame] | 2923 | break; |
Jes Sorensen | 84e0cef | 2010-09-01 11:42:04 +0200 | [diff] [blame] | 2924 | case MSR_K7_CLK_CTL: |
| 2925 | /* |
| 2926 | * Ignore all writes to this no longer documented MSR. |
| 2927 | * Writes are only relevant for old K7 processors, |
| 2928 | * all pre-dating SVM, but a recommended workaround from |
Guo Chao | 4a96998 | 2012-06-28 15:17:27 +0800 | [diff] [blame] | 2929 | * AMD for these chips. It is possible to specify the |
Jes Sorensen | 84e0cef | 2010-09-01 11:42:04 +0200 | [diff] [blame] | 2930 | * affected processor models on the command line, hence |
| 2931 | * the need to ignore the workaround. |
| 2932 | */ |
| 2933 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 2934 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 2935 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 2936 | case HV_X64_MSR_CRASH_CTL: |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 2937 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 2938 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 2939 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 2940 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 2941 | return kvm_hv_set_msr_common(vcpu, msr, data, |
| 2942 | msr_info->host_initiated); |
john cooper | 91c9c3e | 2011-01-21 00:21:00 -0500 | [diff] [blame] | 2943 | case MSR_IA32_BBL_CR_CTL3: |
| 2944 | /* Drop writes to this legacy MSR -- see rdmsr |
| 2945 | * counterpart for further detail. |
| 2946 | */ |
Eyal Moscovici | fab0aa3 | 2017-11-08 14:32:08 +0200 | [diff] [blame] | 2947 | if (report_ignored_msrs) |
| 2948 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", |
| 2949 | msr, data); |
john cooper | 91c9c3e | 2011-01-21 00:21:00 -0500 | [diff] [blame] | 2950 | break; |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 2951 | case MSR_AMD64_OSVW_ID_LENGTH: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2952 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 2953 | return 1; |
| 2954 | vcpu->arch.osvw.length = data; |
| 2955 | break; |
| 2956 | case MSR_AMD64_OSVW_STATUS: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2957 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 2958 | return 1; |
| 2959 | vcpu->arch.osvw.status = data; |
| 2960 | break; |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 2961 | case MSR_PLATFORM_INFO: |
| 2962 | if (!msr_info->host_initiated || |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 2963 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
| 2964 | cpuid_fault_enabled(vcpu))) |
| 2965 | return 1; |
| 2966 | vcpu->arch.msr_platform_info = data; |
| 2967 | break; |
| 2968 | case MSR_MISC_FEATURES_ENABLES: |
| 2969 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || |
| 2970 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && |
| 2971 | !supports_cpuid_fault(vcpu))) |
| 2972 | return 1; |
| 2973 | vcpu->arch.msr_misc_features_enables = data; |
| 2974 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2975 | default: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2976 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 2977 | return xen_hvm_config(vcpu, data); |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 2978 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 2979 | return kvm_pmu_set_msr(vcpu, msr_info); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 2980 | if (!ignore_msrs) { |
Bandan Das | ae0f549 | 2016-11-15 01:36:18 -0500 | [diff] [blame] | 2981 | vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n", |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2982 | msr, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 2983 | return 1; |
| 2984 | } else { |
Eyal Moscovici | fab0aa3 | 2017-11-08 14:32:08 +0200 | [diff] [blame] | 2985 | if (report_ignored_msrs) |
| 2986 | vcpu_unimpl(vcpu, |
| 2987 | "ignored wrmsr: 0x%x data 0x%llx\n", |
| 2988 | msr, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 2989 | break; |
| 2990 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 2991 | } |
| 2992 | return 0; |
| 2993 | } |
| 2994 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 2995 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 2996 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2997 | { |
| 2998 | u64 data; |
| 2999 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 3000 | unsigned bank_num = mcg_cap & 0xff; |
| 3001 | |
| 3002 | switch (msr) { |
| 3003 | case MSR_IA32_P5_MC_ADDR: |
| 3004 | case MSR_IA32_P5_MC_TYPE: |
| 3005 | data = 0; |
| 3006 | break; |
| 3007 | case MSR_IA32_MCG_CAP: |
| 3008 | data = vcpu->arch.mcg_cap; |
| 3009 | break; |
| 3010 | case MSR_IA32_MCG_CTL: |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 3011 | if (!(mcg_cap & MCG_CTL_P) && !host) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3012 | return 1; |
| 3013 | data = vcpu->arch.mcg_ctl; |
| 3014 | break; |
| 3015 | case MSR_IA32_MCG_STATUS: |
| 3016 | data = vcpu->arch.mcg_status; |
| 3017 | break; |
| 3018 | default: |
| 3019 | if (msr >= MSR_IA32_MC0_CTL && |
Chen Yucong | 81760dc | 2014-09-23 10:44:35 +0800 | [diff] [blame] | 3020 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
Marios Pomonis | 6ec4c5e | 2019-12-11 12:47:49 -0800 | [diff] [blame] | 3021 | u32 offset = array_index_nospec( |
| 3022 | msr - MSR_IA32_MC0_CTL, |
| 3023 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); |
| 3024 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3025 | data = vcpu->arch.mce_banks[offset]; |
| 3026 | break; |
| 3027 | } |
| 3028 | return 1; |
| 3029 | } |
| 3030 | *pdata = data; |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3034 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3035 | { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3036 | switch (msr_info->index) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3037 | case MSR_IA32_PLATFORM_ID: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3038 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 3039 | case MSR_IA32_DEBUGCTLMSR: |
| 3040 | case MSR_IA32_LASTBRANCHFROMIP: |
| 3041 | case MSR_IA32_LASTBRANCHTOIP: |
| 3042 | case MSR_IA32_LASTINTFROMIP: |
| 3043 | case MSR_IA32_LASTINTTOIP: |
Jaswinder Singh Rajput | 60af2ec | 2009-05-14 11:00:10 +0530 | [diff] [blame] | 3044 | case MSR_K8_SYSCFG: |
Paolo Bonzini | 3afb112 | 2015-09-18 17:33:04 +0200 | [diff] [blame] | 3045 | case MSR_K8_TSEG_ADDR: |
| 3046 | case MSR_K8_TSEG_MASK: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 3047 | case MSR_VM_HSAVE_PA: |
Andre Przywara | 1fdbd48 | 2009-06-24 12:44:34 +0200 | [diff] [blame] | 3048 | case MSR_K8_INT_PENDING_MSG: |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 3049 | case MSR_AMD64_NB_CFG: |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 3050 | case MSR_FAM10H_MMIO_CONF_BASE: |
Borislav Petkov | 2e32b71 | 2013-02-19 19:33:13 +0100 | [diff] [blame] | 3051 | case MSR_AMD64_BU_CFG2: |
Dmitry Bilunov | 0c2df2a | 2016-05-31 17:38:24 +0300 | [diff] [blame] | 3052 | case MSR_IA32_PERF_CTL: |
Ladi Prosek | 405a353 | 2017-04-06 15:22:20 +0200 | [diff] [blame] | 3053 | case MSR_AMD64_DC_CFG: |
Eduardo Habkost | 0e1b869 | 2018-12-17 22:34:18 -0200 | [diff] [blame] | 3054 | case MSR_F15H_EX_CFG: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3055 | msr_info->data = 0; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3056 | break; |
Janakarajan Natarajan | c51eb52 | 2018-02-05 13:24:52 -0600 | [diff] [blame] | 3057 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
Wei Huang | 6912ac3 | 2015-06-12 01:34:56 -0400 | [diff] [blame] | 3058 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
| 3059 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
| 3060 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: |
| 3061 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 3062 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3063 | return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data); |
| 3064 | msr_info->data = 0; |
Gleb Natapov | 5753785 | 2012-01-15 14:17:22 +0200 | [diff] [blame] | 3065 | break; |
Marcelo Tosatti | 742bc67 | 2011-07-29 19:44:21 -0300 | [diff] [blame] | 3066 | case MSR_IA32_UCODE_REV: |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 3067 | msr_info->data = vcpu->arch.microcode_version; |
Marcelo Tosatti | 742bc67 | 2011-07-29 19:44:21 -0300 | [diff] [blame] | 3068 | break; |
Sean Christopherson | 0cf9135 | 2019-03-07 15:43:02 -0800 | [diff] [blame] | 3069 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3070 | if (!msr_info->host_initiated && |
| 3071 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3072 | return 1; |
| 3073 | msr_info->data = vcpu->arch.arch_capabilities; |
| 3074 | break; |
Paolo Bonzini | 73f624f | 2019-06-06 14:32:59 +0200 | [diff] [blame] | 3075 | case MSR_IA32_POWER_CTL: |
| 3076 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; |
| 3077 | break; |
Paolo Bonzini | dd25993 | 2018-04-13 11:38:35 +0200 | [diff] [blame] | 3078 | case MSR_IA32_TSC: |
| 3079 | msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset; |
| 3080 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 3081 | case MSR_MTRRcap: |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 3082 | case 0x200 ... 0x2ff: |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 3083 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3084 | case 0xcd: /* fsb frequency */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3085 | msr_info->data = 3; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3086 | break; |
Jes Sorensen | 7b91409 | 2010-09-09 12:06:46 +0200 | [diff] [blame] | 3087 | /* |
| 3088 | * MSR_EBC_FREQUENCY_ID |
| 3089 | * Conservative value valid for even the basic CPU models. |
| 3090 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of |
| 3091 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, |
| 3092 | * and 266MHz for model 3, or 4. Set Core Clock |
| 3093 | * Frequency to System Bus Frequency Ratio to 1 (bits |
| 3094 | * 31:24) even though these are only valid for CPU |
| 3095 | * models > 2, however guests may end up dividing or |
| 3096 | * multiplying by zero otherwise. |
| 3097 | */ |
| 3098 | case MSR_EBC_FREQUENCY_ID: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3099 | msr_info->data = 1 << 24; |
Jes Sorensen | 7b91409 | 2010-09-09 12:06:46 +0200 | [diff] [blame] | 3100 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3101 | case MSR_IA32_APICBASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3102 | msr_info->data = kvm_get_apic_base(vcpu); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3103 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 3104 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3105 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 3106 | case MSR_IA32_TSCDEADLINE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3107 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 3108 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3109 | case MSR_IA32_TSC_ADJUST: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3110 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3111 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3112 | case MSR_IA32_MISC_ENABLE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3113 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3114 | break; |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 3115 | case MSR_IA32_SMBASE: |
| 3116 | if (!msr_info->host_initiated) |
| 3117 | return 1; |
| 3118 | msr_info->data = vcpu->arch.smbase; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3119 | break; |
Liran Alon | 52797bf | 2017-11-15 13:43:14 +0200 | [diff] [blame] | 3120 | case MSR_SMI_COUNT: |
| 3121 | msr_info->data = vcpu->arch.smi_count; |
| 3122 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 3123 | case MSR_IA32_PERF_STATUS: |
| 3124 | /* TSC increment by tick */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3125 | msr_info->data = 1000ULL; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 3126 | /* CPU multiplier */ |
Nicolas Iooss | b0996ae | 2015-06-29 18:39:23 +0800 | [diff] [blame] | 3127 | msr_info->data |= (((uint64_t)4ULL) << 40); |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 3128 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3129 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3130 | msr_info->data = vcpu->arch.efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3131 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 3132 | case MSR_KVM_WALL_CLOCK: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 3133 | case MSR_KVM_WALL_CLOCK_NEW: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3134 | msr_info->data = vcpu->kvm->arch.wall_clock; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 3135 | break; |
| 3136 | case MSR_KVM_SYSTEM_TIME: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 3137 | case MSR_KVM_SYSTEM_TIME_NEW: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3138 | msr_info->data = vcpu->arch.time; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 3139 | break; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 3140 | case MSR_KVM_ASYNC_PF_EN: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3141 | msr_info->data = vcpu->arch.apf.msr_val; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 3142 | break; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 3143 | case MSR_KVM_STEAL_TIME: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3144 | msr_info->data = vcpu->arch.st.msr_val; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 3145 | break; |
Michael S. Tsirkin | 1d92128 | 2012-08-26 18:00:29 +0300 | [diff] [blame] | 3146 | case MSR_KVM_PV_EOI_EN: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3147 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
Michael S. Tsirkin | 1d92128 | 2012-08-26 18:00:29 +0300 | [diff] [blame] | 3148 | break; |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 3149 | case MSR_KVM_POLL_CONTROL: |
| 3150 | msr_info->data = vcpu->arch.msr_kvm_poll_control; |
| 3151 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3152 | case MSR_IA32_P5_MC_ADDR: |
| 3153 | case MSR_IA32_P5_MC_TYPE: |
| 3154 | case MSR_IA32_MCG_CAP: |
| 3155 | case MSR_IA32_MCG_CTL: |
| 3156 | case MSR_IA32_MCG_STATUS: |
Chen Yucong | 81760dc | 2014-09-23 10:44:35 +0800 | [diff] [blame] | 3157 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 3158 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
| 3159 | msr_info->host_initiated); |
Aaron Lewis | 864e2ab | 2019-10-21 16:30:26 -0700 | [diff] [blame] | 3160 | case MSR_IA32_XSS: |
| 3161 | if (!msr_info->host_initiated && |
| 3162 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) |
| 3163 | return 1; |
| 3164 | msr_info->data = vcpu->arch.ia32_xss; |
| 3165 | break; |
Jes Sorensen | 84e0cef | 2010-09-01 11:42:04 +0200 | [diff] [blame] | 3166 | case MSR_K7_CLK_CTL: |
| 3167 | /* |
| 3168 | * Provide expected ramp-up count for K7. All other |
| 3169 | * are set to zero, indicating minimum divisors for |
| 3170 | * every field. |
| 3171 | * |
| 3172 | * This prevents guest kernels on AMD host with CPU |
| 3173 | * type 6, model 8 and higher from exploding due to |
| 3174 | * the rdmsr failing. |
| 3175 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3176 | msr_info->data = 0x20000000; |
Jes Sorensen | 84e0cef | 2010-09-01 11:42:04 +0200 | [diff] [blame] | 3177 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3178 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 3179 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 3180 | case HV_X64_MSR_CRASH_CTL: |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 3181 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 3182 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 3183 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 3184 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 3185 | return kvm_hv_get_msr_common(vcpu, |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 3186 | msr_info->index, &msr_info->data, |
| 3187 | msr_info->host_initiated); |
john cooper | 91c9c3e | 2011-01-21 00:21:00 -0500 | [diff] [blame] | 3188 | case MSR_IA32_BBL_CR_CTL3: |
| 3189 | /* This legacy MSR exists but isn't fully documented in current |
| 3190 | * silicon. It is however accessed by winxp in very narrow |
| 3191 | * scenarios where it sets bit #19, itself documented as |
| 3192 | * a "reserved" bit. Best effort attempt to source coherent |
| 3193 | * read data here should the balance of the register be |
| 3194 | * interpreted by the guest: |
| 3195 | * |
| 3196 | * L2 cache control register 3: 64GB range, 256KB size, |
| 3197 | * enabled, latency 0x1, configured |
| 3198 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3199 | msr_info->data = 0xbe702111; |
john cooper | 91c9c3e | 2011-01-21 00:21:00 -0500 | [diff] [blame] | 3200 | break; |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 3201 | case MSR_AMD64_OSVW_ID_LENGTH: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3202 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 3203 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3204 | msr_info->data = vcpu->arch.osvw.length; |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 3205 | break; |
| 3206 | case MSR_AMD64_OSVW_STATUS: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3207 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 3208 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3209 | msr_info->data = vcpu->arch.osvw.status; |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 3210 | break; |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 3211 | case MSR_PLATFORM_INFO: |
Drew Schmitt | 6fbbde9 | 2018-08-20 10:32:15 -0700 | [diff] [blame] | 3212 | if (!msr_info->host_initiated && |
| 3213 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) |
| 3214 | return 1; |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 3215 | msr_info->data = vcpu->arch.msr_platform_info; |
| 3216 | break; |
| 3217 | case MSR_MISC_FEATURES_ENABLES: |
| 3218 | msr_info->data = vcpu->arch.msr_misc_features_enables; |
| 3219 | break; |
Borislav Petkov | 191c813 | 2019-04-18 18:32:50 +0200 | [diff] [blame] | 3220 | case MSR_K7_HWCR: |
| 3221 | msr_info->data = vcpu->arch.msr_hwcr; |
| 3222 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3223 | default: |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 3224 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3225 | return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 3226 | if (!ignore_msrs) { |
Bandan Das | ae0f549 | 2016-11-15 01:36:18 -0500 | [diff] [blame] | 3227 | vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n", |
| 3228 | msr_info->index); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 3229 | return 1; |
| 3230 | } else { |
Eyal Moscovici | fab0aa3 | 2017-11-08 14:32:08 +0200 | [diff] [blame] | 3231 | if (report_ignored_msrs) |
| 3232 | vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", |
| 3233 | msr_info->index); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3234 | msr_info->data = 0; |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 3235 | } |
| 3236 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3237 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 3238 | return 0; |
| 3239 | } |
| 3240 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 3241 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3242 | /* |
| 3243 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 3244 | * |
| 3245 | * @return number of msrs set successfully. |
| 3246 | */ |
| 3247 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 3248 | struct kvm_msr_entry *entries, |
| 3249 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 3250 | unsigned index, u64 *data)) |
| 3251 | { |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3252 | int i; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3253 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3254 | for (i = 0; i < msrs->nmsrs; ++i) |
| 3255 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 3256 | break; |
| 3257 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3258 | return i; |
| 3259 | } |
| 3260 | |
| 3261 | /* |
| 3262 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 3263 | * |
| 3264 | * @return number of msrs set successfully. |
| 3265 | */ |
| 3266 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 3267 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 3268 | unsigned index, u64 *data), |
| 3269 | int writeback) |
| 3270 | { |
| 3271 | struct kvm_msrs msrs; |
| 3272 | struct kvm_msr_entry *entries; |
| 3273 | int r, n; |
| 3274 | unsigned size; |
| 3275 | |
| 3276 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 3277 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3278 | goto out; |
| 3279 | |
| 3280 | r = -E2BIG; |
| 3281 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 3282 | goto out; |
| 3283 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3284 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3285 | entries = memdup_user(user_msrs->entries, size); |
| 3286 | if (IS_ERR(entries)) { |
| 3287 | r = PTR_ERR(entries); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3288 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3289 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3290 | |
| 3291 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 3292 | if (r < 0) |
| 3293 | goto out_free; |
| 3294 | |
| 3295 | r = -EFAULT; |
| 3296 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 3297 | goto out_free; |
| 3298 | |
| 3299 | r = n; |
| 3300 | |
| 3301 | out_free: |
Avi Kivity | 7a73c02 | 2010-07-22 23:24:52 +0300 | [diff] [blame] | 3302 | kfree(entries); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3303 | out: |
| 3304 | return r; |
| 3305 | } |
| 3306 | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3307 | static inline bool kvm_can_mwait_in_guest(void) |
| 3308 | { |
| 3309 | return boot_cpu_has(X86_FEATURE_MWAIT) && |
KarimAllah Ahmed | 8e9b29b | 2018-04-11 11:16:03 +0200 | [diff] [blame] | 3310 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
| 3311 | boot_cpu_has(X86_FEATURE_ARAT); |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 3314 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3315 | { |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3316 | int r = 0; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3317 | |
| 3318 | switch (ext) { |
| 3319 | case KVM_CAP_IRQCHIP: |
| 3320 | case KVM_CAP_HLT: |
| 3321 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3322 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 3323 | case KVM_CAP_EXT_CPUID: |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 3324 | case KVM_CAP_EXT_EMUL_CPUID: |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3325 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 3326 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 3327 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3328 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 3329 | case KVM_CAP_SYNC_MMU: |
Lai Jiangshan | a355c85 | 2010-12-14 17:57:47 +0800 | [diff] [blame] | 3330 | case KVM_CAP_USER_NMI: |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 3331 | case KVM_CAP_REINJECT_CONTROL: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3332 | case KVM_CAP_IRQ_INJECT_STATUS: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 3333 | case KVM_CAP_IOEVENTFD: |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 3334 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 3335 | case KVM_CAP_PIT2: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 3336 | case KVM_CAP_PIT_STATE2: |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3337 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 3338 | case KVM_CAP_XEN_HVM: |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3339 | case KVM_CAP_VCPU_EVENTS: |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3340 | case KVM_CAP_HYPERV: |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 3341 | case KVM_CAP_HYPERV_VAPIC: |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 3342 | case KVM_CAP_HYPERV_SPIN: |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 3343 | case KVM_CAP_HYPERV_SYNIC: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 3344 | case KVM_CAP_HYPERV_SYNIC2: |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 3345 | case KVM_CAP_HYPERV_VP_INDEX: |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 3346 | case KVM_CAP_HYPERV_EVENTFD: |
Vitaly Kuznetsov | c1aea91 | 2018-05-16 17:21:31 +0200 | [diff] [blame] | 3347 | case KVM_CAP_HYPERV_TLBFLUSH: |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 3348 | case KVM_CAP_HYPERV_SEND_IPI: |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 3349 | case KVM_CAP_HYPERV_CPUID: |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 3350 | case KVM_CAP_PCI_SEGMENT: |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 3351 | case KVM_CAP_DEBUGREGS: |
Jan Kiszka | d2be165 | 2010-02-23 17:47:57 +0100 | [diff] [blame] | 3352 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 3353 | case KVM_CAP_XSAVE: |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 3354 | case KVM_CAP_ASYNC_PF: |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 3355 | case KVM_CAP_GET_TSC_KHZ: |
Eric B Munson | 1c0b28c | 2012-03-10 14:37:27 -0500 | [diff] [blame] | 3356 | case KVM_CAP_KVMCLOCK_CTRL: |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 3357 | case KVM_CAP_READONLY_MEM: |
Paolo Bonzini | 5f66b62 | 2014-01-29 18:10:45 +0100 | [diff] [blame] | 3358 | case KVM_CAP_HYPERV_TIME: |
Gabriel L. Somlo | 100943c | 2014-02-27 23:06:17 -0500 | [diff] [blame] | 3359 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
Radim Krčmář | defcf51 | 2015-01-08 15:59:30 +0100 | [diff] [blame] | 3360 | case KVM_CAP_TSC_DEADLINE_TIMER: |
Nadav Amit | 90de4a1 | 2015-04-13 01:53:41 +0300 | [diff] [blame] | 3361 | case KVM_CAP_DISABLE_QUIRKS: |
Paolo Bonzini | d71ba78 | 2015-07-29 11:56:48 +0200 | [diff] [blame] | 3362 | case KVM_CAP_SET_BOOT_CPU_ID: |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 3363 | case KVM_CAP_SPLIT_IRQCHIP: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 3364 | case KVM_CAP_IMMEDIATE_EXIT: |
Eric Hankland | 66bb8a0 | 2019-07-10 18:25:15 -0700 | [diff] [blame] | 3365 | case KVM_CAP_PMU_EVENT_FILTER: |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3366 | case KVM_CAP_GET_MSR_FEATURES: |
Drew Schmitt | 6fbbde9 | 2018-08-20 10:32:15 -0700 | [diff] [blame] | 3367 | case KVM_CAP_MSR_PLATFORM_INFO: |
Jim Mattson | c4f5519 | 2018-10-16 14:29:24 -0700 | [diff] [blame] | 3368 | case KVM_CAP_EXCEPTION_PAYLOAD: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3369 | r = 1; |
| 3370 | break; |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 3371 | case KVM_CAP_SYNC_REGS: |
| 3372 | r = KVM_SYNC_X86_VALID_FIELDS; |
| 3373 | break; |
Paolo Bonzini | e3fd9a9 | 2016-11-09 17:48:15 +0100 | [diff] [blame] | 3374 | case KVM_CAP_ADJUST_CLOCK: |
| 3375 | r = KVM_CLOCK_TSC_STABLE; |
| 3376 | break; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3377 | case KVM_CAP_X86_DISABLE_EXITS: |
Wanpeng Li | b517006 | 2019-05-21 14:06:53 +0800 | [diff] [blame] | 3378 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
| 3379 | KVM_X86_DISABLE_EXITS_CSTATE; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3380 | if(kvm_can_mwait_in_guest()) |
| 3381 | r |= KVM_X86_DISABLE_EXITS_MWAIT; |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 3382 | break; |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 3383 | case KVM_CAP_X86_SMM: |
| 3384 | /* SMBASE is usually relocated above 1M on modern chipsets, |
| 3385 | * and SMM handlers might indeed rely on 4G segment limits, |
| 3386 | * so do not report SMM to be available if real mode is |
| 3387 | * emulated via vm86 mode. Still, do not go to great lengths |
| 3388 | * to avoid userspace's usage of the feature, because it is a |
| 3389 | * fringe case that is not enabled except via specific settings |
| 3390 | * of the module parameters. |
| 3391 | */ |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 3392 | r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE); |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 3393 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3394 | case KVM_CAP_VAPIC: |
| 3395 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 3396 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 3397 | case KVM_CAP_NR_VCPUS: |
Sasha Levin | 8c3ba33 | 2011-07-18 17:17:15 +0300 | [diff] [blame] | 3398 | r = KVM_SOFT_MAX_VCPUS; |
| 3399 | break; |
| 3400 | case KVM_CAP_MAX_VCPUS: |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 3401 | r = KVM_MAX_VCPUS; |
| 3402 | break; |
Thomas Huth | a86cb41 | 2019-05-23 18:43:08 +0200 | [diff] [blame] | 3403 | case KVM_CAP_MAX_VCPU_ID: |
| 3404 | r = KVM_MAX_VCPU_ID; |
| 3405 | break; |
Marcelo Tosatti | a68a6a7 | 2009-10-01 19:28:39 -0300 | [diff] [blame] | 3406 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 3407 | r = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3408 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3409 | case KVM_CAP_MCE: |
| 3410 | r = KVM_MAX_MCE_BANKS; |
| 3411 | break; |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 3412 | case KVM_CAP_XCRS: |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 3413 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 3414 | break; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 3415 | case KVM_CAP_TSC_CONTROL: |
| 3416 | r = kvm_has_tsc_control; |
| 3417 | break; |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 3418 | case KVM_CAP_X2APIC_API: |
| 3419 | r = KVM_X2APIC_API_VALID_FLAGS; |
| 3420 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 3421 | case KVM_CAP_NESTED_STATE: |
| 3422 | r = kvm_x86_ops->get_nested_state ? |
Hariprasad Kelam | be43c44 | 2019-04-06 15:06:58 +0530 | [diff] [blame] | 3423 | kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 3424 | break; |
Tianyu Lan | 344c6c8 | 2019-08-22 22:30:20 +0800 | [diff] [blame] | 3425 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
Vitaly Kuznetsov | 5a0165f | 2019-08-28 09:59:05 +0200 | [diff] [blame] | 3426 | r = kvm_x86_ops->enable_direct_tlbflush != NULL; |
| 3427 | break; |
| 3428 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
| 3429 | r = kvm_x86_ops->nested_enable_evmcs != NULL; |
Tianyu Lan | 344c6c8 | 2019-08-22 22:30:20 +0800 | [diff] [blame] | 3430 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3431 | default: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3432 | break; |
| 3433 | } |
| 3434 | return r; |
| 3435 | |
| 3436 | } |
| 3437 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3438 | long kvm_arch_dev_ioctl(struct file *filp, |
| 3439 | unsigned int ioctl, unsigned long arg) |
| 3440 | { |
| 3441 | void __user *argp = (void __user *)arg; |
| 3442 | long r; |
| 3443 | |
| 3444 | switch (ioctl) { |
| 3445 | case KVM_GET_MSR_INDEX_LIST: { |
| 3446 | struct kvm_msr_list __user *user_msr_list = argp; |
| 3447 | struct kvm_msr_list msr_list; |
| 3448 | unsigned n; |
| 3449 | |
| 3450 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 3451 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3452 | goto out; |
| 3453 | n = msr_list.nmsrs; |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 3454 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 3455 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3456 | goto out; |
| 3457 | r = -E2BIG; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 3458 | if (n < msr_list.nmsrs) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3459 | goto out; |
| 3460 | r = -EFAULT; |
| 3461 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 3462 | num_msrs_to_save * sizeof(u32))) |
| 3463 | goto out; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 3464 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3465 | &emulated_msrs, |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 3466 | num_emulated_msrs * sizeof(u32))) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3467 | goto out; |
| 3468 | r = 0; |
| 3469 | break; |
| 3470 | } |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 3471 | case KVM_GET_SUPPORTED_CPUID: |
| 3472 | case KVM_GET_EMULATED_CPUID: { |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 3473 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 3474 | struct kvm_cpuid2 cpuid; |
| 3475 | |
| 3476 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 3477 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 3478 | goto out; |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 3479 | |
| 3480 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, |
| 3481 | ioctl); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 3482 | if (r) |
| 3483 | goto out; |
| 3484 | |
| 3485 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 3486 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 3487 | goto out; |
| 3488 | r = 0; |
| 3489 | break; |
| 3490 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3491 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3492 | r = -EFAULT; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3493 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
| 3494 | sizeof(kvm_mce_cap_supported))) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3495 | goto out; |
| 3496 | r = 0; |
| 3497 | break; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3498 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
| 3499 | struct kvm_msr_list __user *user_msr_list = argp; |
| 3500 | struct kvm_msr_list msr_list; |
| 3501 | unsigned int n; |
| 3502 | |
| 3503 | r = -EFAULT; |
| 3504 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
| 3505 | goto out; |
| 3506 | n = msr_list.nmsrs; |
| 3507 | msr_list.nmsrs = num_msr_based_features; |
| 3508 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
| 3509 | goto out; |
| 3510 | r = -E2BIG; |
| 3511 | if (n < msr_list.nmsrs) |
| 3512 | goto out; |
| 3513 | r = -EFAULT; |
| 3514 | if (copy_to_user(user_msr_list->indices, &msr_based_features, |
| 3515 | num_msr_based_features * sizeof(u32))) |
| 3516 | goto out; |
| 3517 | r = 0; |
| 3518 | break; |
| 3519 | } |
| 3520 | case KVM_GET_MSRS: |
| 3521 | r = msr_io(NULL, argp, do_get_msr_feature, 1); |
| 3522 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3523 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3524 | default: |
| 3525 | r = -EINVAL; |
| 3526 | } |
| 3527 | out: |
| 3528 | return r; |
| 3529 | } |
| 3530 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3531 | static void wbinvd_ipi(void *garbage) |
| 3532 | { |
| 3533 | wbinvd(); |
| 3534 | } |
| 3535 | |
| 3536 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 3537 | { |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 3538 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3539 | } |
| 3540 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3541 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 3542 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3543 | /* Address WBINVD may be executed by guest */ |
| 3544 | if (need_emulate_wbinvd(vcpu)) { |
| 3545 | if (kvm_x86_ops->has_wbinvd_exit()) |
| 3546 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
| 3547 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) |
| 3548 | smp_call_function_single(vcpu->cpu, |
| 3549 | wbinvd_ipi, NULL, 1); |
| 3550 | } |
| 3551 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3552 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 3553 | |
| 3554 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
| 3555 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { |
| 3556 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); |
| 3557 | vcpu->arch.tsc_offset_adjustment = 0; |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 3558 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 3559 | } |
| 3560 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 3561 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
Zachary Amsden | 6f526ec | 2012-02-03 15:43:54 -0200 | [diff] [blame] | 3562 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 3563 | rdtsc() - vcpu->arch.last_host_tsc; |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 3564 | if (tsc_delta < 0) |
| 3565 | mark_tsc_unstable("KVM discovered backwards TSC"); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 3566 | |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 3567 | if (kvm_check_tsc_unstable()) { |
Haozhong Zhang | 07c1419 | 2015-10-20 15:39:05 +0800 | [diff] [blame] | 3568 | u64 offset = kvm_compute_tsc_offset(vcpu, |
Zachary Amsden | b183aa5 | 2012-02-03 15:43:53 -0200 | [diff] [blame] | 3569 | vcpu->arch.last_guest_tsc); |
Luiz Capitulino | a545ab6 | 2016-09-07 14:47:19 -0400 | [diff] [blame] | 3570 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 3571 | vcpu->arch.tsc_catchup = 1; |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 3572 | } |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 3573 | |
| 3574 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 3575 | kvm_lapic_restart_hv_timer(vcpu); |
| 3576 | |
Marcelo Tosatti | d98d07c | 2012-11-27 23:29:04 -0200 | [diff] [blame] | 3577 | /* |
| 3578 | * On a host with synchronized TSC, there is no need to update |
| 3579 | * kvmclock on vcpu->cpu migration |
| 3580 | */ |
| 3581 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 3582 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 3583 | if (vcpu->cpu != cpu) |
Radim Krčmář | 1bd2009 | 2017-04-26 22:32:20 +0200 | [diff] [blame] | 3584 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 3585 | vcpu->cpu = cpu; |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 3586 | } |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 3587 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 3588 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3589 | } |
| 3590 | |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3591 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
| 3592 | { |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 3593 | struct kvm_host_map map; |
| 3594 | struct kvm_steal_time *st; |
| 3595 | |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3596 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
| 3597 | return; |
| 3598 | |
Boris Ostrovsky | a6bd811 | 2019-12-06 15:36:12 +0000 | [diff] [blame] | 3599 | if (vcpu->arch.st.preempted) |
Boris Ostrovsky | 8c6de56 | 2019-10-30 19:01:31 +0000 | [diff] [blame] | 3600 | return; |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3601 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 3602 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, |
| 3603 | &vcpu->arch.st.cache, true)) |
| 3604 | return; |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3605 | |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 3606 | st = map.hva + |
| 3607 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); |
| 3608 | |
Boris Ostrovsky | a6bd811 | 2019-12-06 15:36:12 +0000 | [diff] [blame] | 3609 | st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; |
Boris Ostrovsky | b043138 | 2019-12-05 03:45:32 +0000 | [diff] [blame] | 3610 | |
| 3611 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3612 | } |
| 3613 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3614 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 3615 | { |
Andrea Arcangeli | cc0d907 | 2016-12-17 19:13:32 +0100 | [diff] [blame] | 3616 | int idx; |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 3617 | |
| 3618 | if (vcpu->preempted) |
| 3619 | vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu); |
| 3620 | |
Andrea Arcangeli | 931f261 | 2016-12-17 18:43:52 +0100 | [diff] [blame] | 3621 | /* |
| 3622 | * Disable page faults because we're in atomic context here. |
| 3623 | * kvm_write_guest_offset_cached() would call might_fault() |
| 3624 | * that relies on pagefault_disable() to tell if there's a |
| 3625 | * bug. NOTE: the write to guest memory may not go through if |
| 3626 | * during postcopy live migration or if there's heavy guest |
| 3627 | * paging. |
| 3628 | */ |
| 3629 | pagefault_disable(); |
Andrea Arcangeli | cc0d907 | 2016-12-17 19:13:32 +0100 | [diff] [blame] | 3630 | /* |
| 3631 | * kvm_memslots() will be called by |
| 3632 | * kvm_write_guest_offset_cached() so take the srcu lock. |
| 3633 | */ |
| 3634 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Pan Xinhui | 0b9f6c4 | 2016-11-02 05:08:35 -0400 | [diff] [blame] | 3635 | kvm_steal_time_set_preempted(vcpu); |
Andrea Arcangeli | cc0d907 | 2016-12-17 19:13:32 +0100 | [diff] [blame] | 3636 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Andrea Arcangeli | 931f261 | 2016-12-17 18:43:52 +0100 | [diff] [blame] | 3637 | pagefault_enable(); |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3638 | kvm_x86_ops->vcpu_put(vcpu); |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 3639 | vcpu->arch.last_host_tsc = rdtsc(); |
Wanpeng Li | efdab99 | 2017-12-13 10:46:40 +0100 | [diff] [blame] | 3640 | /* |
Radim Krčmář | f9dcf08 | 2018-10-23 16:31:38 +0200 | [diff] [blame] | 3641 | * If userspace has set any breakpoints or watchpoints, dr6 is restored |
| 3642 | * on every vmexit, but if not, we might have a stale dr6 from the |
| 3643 | * guest. do_debug expects dr6 to be cleared after it runs, do the same. |
Wanpeng Li | efdab99 | 2017-12-13 10:46:40 +0100 | [diff] [blame] | 3644 | */ |
Radim Krčmář | f9dcf08 | 2018-10-23 16:31:38 +0200 | [diff] [blame] | 3645 | set_debugreg(0, 6); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3646 | } |
| 3647 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3648 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 3649 | struct kvm_lapic_state *s) |
| 3650 | { |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 3651 | if (vcpu->arch.apicv_active) |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3652 | kvm_x86_ops->sync_pir_to_irr(vcpu); |
| 3653 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 3654 | return kvm_apic_get_state(vcpu, s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 3658 | struct kvm_lapic_state *s) |
| 3659 | { |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 3660 | int r; |
| 3661 | |
| 3662 | r = kvm_apic_set_state(vcpu, s); |
| 3663 | if (r) |
| 3664 | return r; |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 3665 | update_cr8_intercept(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3666 | |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
Matt Gingell | 127a457 | 2015-11-17 17:32:05 +0100 | [diff] [blame] | 3670 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
| 3671 | { |
| 3672 | return (!lapic_in_kernel(vcpu) || |
| 3673 | kvm_apic_accept_pic_intr(vcpu)); |
| 3674 | } |
| 3675 | |
Matt Gingell | 782d422 | 2015-11-16 15:26:00 -0800 | [diff] [blame] | 3676 | /* |
| 3677 | * if userspace requested an interrupt window, check that the |
| 3678 | * interrupt window is open. |
| 3679 | * |
| 3680 | * No need to exit to userspace if we already have an interrupt queued. |
| 3681 | */ |
| 3682 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) |
| 3683 | { |
| 3684 | return kvm_arch_interrupt_allowed(vcpu) && |
| 3685 | !kvm_cpu_has_interrupt(vcpu) && |
| 3686 | !kvm_event_needs_reinjection(vcpu) && |
| 3687 | kvm_cpu_accept_dm_intr(vcpu); |
| 3688 | } |
| 3689 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3690 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 3691 | struct kvm_interrupt *irq) |
| 3692 | { |
Chen Gang | 02cdb50 | 2013-02-27 11:33:25 +0800 | [diff] [blame] | 3693 | if (irq->irq >= KVM_NR_INTERRUPTS) |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3694 | return -EINVAL; |
Steve Rutherford | 1c1a9ce | 2015-07-30 11:27:16 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (!irqchip_in_kernel(vcpu->kvm)) { |
| 3697 | kvm_queue_interrupt(vcpu, irq->irq, false); |
| 3698 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
| 3703 | * With in-kernel LAPIC, we only use this to inject EXTINT, so |
| 3704 | * fail for in-kernel 8259. |
| 3705 | */ |
| 3706 | if (pic_in_kernel(vcpu->kvm)) |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3707 | return -ENXIO; |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3708 | |
Steve Rutherford | 1c1a9ce | 2015-07-30 11:27:16 +0200 | [diff] [blame] | 3709 | if (vcpu->arch.pending_external_vector != -1) |
| 3710 | return -EEXIST; |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3711 | |
Steve Rutherford | 1c1a9ce | 2015-07-30 11:27:16 +0200 | [diff] [blame] | 3712 | vcpu->arch.pending_external_vector = irq->irq; |
Matt Gingell | 934bf65 | 2015-11-16 15:26:05 -0800 | [diff] [blame] | 3713 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 3714 | return 0; |
| 3715 | } |
| 3716 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 3717 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 3718 | { |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 3719 | kvm_inject_nmi(vcpu); |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 3720 | |
| 3721 | return 0; |
| 3722 | } |
| 3723 | |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3724 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
| 3725 | { |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 3726 | kvm_make_request(KVM_REQ_SMI, vcpu); |
| 3727 | |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3728 | return 0; |
| 3729 | } |
| 3730 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 3731 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 3732 | struct kvm_tpr_access_ctl *tac) |
| 3733 | { |
| 3734 | if (tac->flags) |
| 3735 | return -EINVAL; |
| 3736 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3740 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 3741 | u64 mcg_cap) |
| 3742 | { |
| 3743 | int r; |
| 3744 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 3745 | |
| 3746 | r = -EINVAL; |
Jan Kiszka | a9e38c3e | 2009-10-23 09:37:00 +0200 | [diff] [blame] | 3747 | if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3748 | goto out; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3749 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3750 | goto out; |
| 3751 | r = 0; |
| 3752 | vcpu->arch.mcg_cap = mcg_cap; |
| 3753 | /* Init IA32_MCG_CTL to all 1s */ |
| 3754 | if (mcg_cap & MCG_CTL_P) |
| 3755 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 3756 | /* Init IA32_MCi_CTL to all 1s */ |
| 3757 | for (bank = 0; bank < bank_num; bank++) |
| 3758 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3759 | |
Sean Christopherson | 92735b1 | 2019-08-02 15:06:17 -0700 | [diff] [blame] | 3760 | kvm_x86_ops->setup_mce(vcpu); |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3761 | out: |
| 3762 | return r; |
| 3763 | } |
| 3764 | |
| 3765 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 3766 | struct kvm_x86_mce *mce) |
| 3767 | { |
| 3768 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 3769 | unsigned bank_num = mcg_cap & 0xff; |
| 3770 | u64 *banks = vcpu->arch.mce_banks; |
| 3771 | |
| 3772 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 3773 | return -EINVAL; |
| 3774 | /* |
| 3775 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 3776 | * reporting is disabled |
| 3777 | */ |
| 3778 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 3779 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 3780 | return 0; |
| 3781 | banks += 4 * mce->bank; |
| 3782 | /* |
| 3783 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 3784 | * reporting is disabled for the bank |
| 3785 | */ |
| 3786 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 3787 | return 0; |
| 3788 | if (mce->status & MCI_STATUS_UC) { |
| 3789 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3790 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 3791 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 3792 | return 0; |
| 3793 | } |
| 3794 | if (banks[1] & MCI_STATUS_VAL) |
| 3795 | mce->status |= MCI_STATUS_OVER; |
| 3796 | banks[2] = mce->addr; |
| 3797 | banks[3] = mce->misc; |
| 3798 | vcpu->arch.mcg_status = mce->mcg_status; |
| 3799 | banks[1] = mce->status; |
| 3800 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 3801 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 3802 | || !(banks[1] & MCI_STATUS_UC)) { |
| 3803 | if (banks[1] & MCI_STATUS_VAL) |
| 3804 | mce->status |= MCI_STATUS_OVER; |
| 3805 | banks[2] = mce->addr; |
| 3806 | banks[3] = mce->misc; |
| 3807 | banks[1] = mce->status; |
| 3808 | } else |
| 3809 | banks[1] |= MCI_STATUS_OVER; |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3813 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 3814 | struct kvm_vcpu_events *events) |
| 3815 | { |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 3816 | process_nmi(vcpu); |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3817 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 3818 | /* |
Oliver Upton | a06230b | 2020-02-07 02:36:06 -0800 | [diff] [blame] | 3819 | * In guest mode, payload delivery should be deferred, |
| 3820 | * so that the L1 hypervisor can intercept #PF before |
| 3821 | * CR2 is modified (or intercept #DB before DR6 is |
| 3822 | * modified under nVMX). Unless the per-VM capability, |
| 3823 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of |
| 3824 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we |
| 3825 | * opportunistically defer the exception payload, deliver it if the |
| 3826 | * capability hasn't been requested before processing a |
| 3827 | * KVM_GET_VCPU_EVENTS. |
| 3828 | */ |
| 3829 | if (!vcpu->kvm->arch.exception_payload_enabled && |
| 3830 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) |
| 3831 | kvm_deliver_exception_payload(vcpu); |
| 3832 | |
| 3833 | /* |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3834 | * The API doesn't provide the instruction length for software |
| 3835 | * exceptions, so don't report them. As long as the guest RIP |
| 3836 | * isn't advanced, we should expect to encounter the exception |
| 3837 | * again. |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 3838 | */ |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3839 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
| 3840 | events->exception.injected = 0; |
| 3841 | events->exception.pending = 0; |
| 3842 | } else { |
| 3843 | events->exception.injected = vcpu->arch.exception.injected; |
| 3844 | events->exception.pending = vcpu->arch.exception.pending; |
| 3845 | /* |
| 3846 | * For ABI compatibility, deliberately conflate |
| 3847 | * pending and injected exceptions when |
| 3848 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. |
| 3849 | */ |
| 3850 | if (!vcpu->kvm->arch.exception_payload_enabled) |
| 3851 | events->exception.injected |= |
| 3852 | vcpu->arch.exception.pending; |
| 3853 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3854 | events->exception.nr = vcpu->arch.exception.nr; |
| 3855 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
| 3856 | events->exception.error_code = vcpu->arch.exception.error_code; |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3857 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
| 3858 | events->exception_payload = vcpu->arch.exception.payload; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3859 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 3860 | events->interrupt.injected = |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 3861 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3862 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 3863 | events->interrupt.soft = 0; |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 3864 | events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3865 | |
| 3866 | events->nmi.injected = vcpu->arch.nmi_injected; |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 3867 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3868 | events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); |
Vasiliy Kulikov | 97e69aa | 2010-10-30 22:54:47 +0400 | [diff] [blame] | 3869 | events->nmi.pad = 0; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3870 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 3871 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3872 | |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3873 | events->smi.smm = is_smm(vcpu); |
| 3874 | events->smi.pending = vcpu->arch.smi_pending; |
| 3875 | events->smi.smm_inside_nmi = |
| 3876 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); |
| 3877 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); |
| 3878 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 3879 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3880 | | KVM_VCPUEVENT_VALID_SHADOW |
| 3881 | | KVM_VCPUEVENT_VALID_SMM); |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3882 | if (vcpu->kvm->arch.exception_payload_enabled) |
| 3883 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; |
| 3884 | |
Vasiliy Kulikov | 97e69aa | 2010-10-30 22:54:47 +0400 | [diff] [blame] | 3885 | memset(&events->reserved, 0, sizeof(events->reserved)); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3886 | } |
| 3887 | |
Sean Christopherson | c5833c7 | 2019-04-02 08:03:10 -0700 | [diff] [blame] | 3888 | static void kvm_smm_changed(struct kvm_vcpu *vcpu); |
Xiao Guangrong | 6ef4e07 | 2016-12-24 10:00:42 +0100 | [diff] [blame] | 3889 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3890 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 3891 | struct kvm_vcpu_events *events) |
| 3892 | { |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 3893 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3894 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3895 | | KVM_VCPUEVENT_VALID_SHADOW |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3896 | | KVM_VCPUEVENT_VALID_SMM |
| 3897 | | KVM_VCPUEVENT_VALID_PAYLOAD)) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3898 | return -EINVAL; |
| 3899 | |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3900 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
| 3901 | if (!vcpu->kvm->arch.exception_payload_enabled) |
| 3902 | return -EINVAL; |
| 3903 | if (events->exception.pending) |
| 3904 | events->exception.injected = 0; |
| 3905 | else |
| 3906 | events->exception_has_payload = 0; |
| 3907 | } else { |
| 3908 | events->exception.pending = 0; |
| 3909 | events->exception_has_payload = 0; |
| 3910 | } |
| 3911 | |
| 3912 | if ((events->exception.injected || events->exception.pending) && |
| 3913 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) |
Paolo Bonzini | 78e546c | 2016-06-01 14:09:20 +0200 | [diff] [blame] | 3914 | return -EINVAL; |
| 3915 | |
David Hildenbrand | 28bf288 | 2017-03-23 11:46:03 +0100 | [diff] [blame] | 3916 | /* INITs are latched while in SMM */ |
| 3917 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && |
| 3918 | (events->smi.smm || events->smi.pending) && |
| 3919 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3920 | return -EINVAL; |
| 3921 | |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 3922 | process_nmi(vcpu); |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3923 | vcpu->arch.exception.injected = events->exception.injected; |
| 3924 | vcpu->arch.exception.pending = events->exception.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3925 | vcpu->arch.exception.nr = events->exception.nr; |
| 3926 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 3927 | vcpu->arch.exception.error_code = events->exception.error_code; |
Jim Mattson | 59073aa | 2018-10-16 14:29:20 -0700 | [diff] [blame] | 3928 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
| 3929 | vcpu->arch.exception.payload = events->exception_payload; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3930 | |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 3931 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3932 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 3933 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3934 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
| 3935 | kvm_x86_ops->set_interrupt_shadow(vcpu, |
| 3936 | events->interrupt.shadow); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3937 | |
| 3938 | vcpu->arch.nmi_injected = events->nmi.injected; |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 3939 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 3940 | vcpu->arch.nmi_pending = events->nmi.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3941 | kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked); |
| 3942 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 3943 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 3944 | lapic_in_kernel(vcpu)) |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 3945 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3946 | |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3947 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
Sean Christopherson | c5833c7 | 2019-04-02 08:03:10 -0700 | [diff] [blame] | 3948 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
| 3949 | if (events->smi.smm) |
| 3950 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 3951 | else |
| 3952 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
| 3953 | kvm_smm_changed(vcpu); |
| 3954 | } |
Xiao Guangrong | 6ef4e07 | 2016-12-24 10:00:42 +0100 | [diff] [blame] | 3955 | |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3956 | vcpu->arch.smi_pending = events->smi.pending; |
Wanpeng Li | f4ef191 | 2017-08-01 16:05:25 -0700 | [diff] [blame] | 3957 | |
| 3958 | if (events->smi.smm) { |
| 3959 | if (events->smi.smm_inside_nmi) |
| 3960 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3961 | else |
Wanpeng Li | f4ef191 | 2017-08-01 16:05:25 -0700 | [diff] [blame] | 3962 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
Liran Alon | ff90afa | 2019-11-11 11:16:39 +0200 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | if (lapic_in_kernel(vcpu)) { |
| 3966 | if (events->smi.latched_init) |
| 3967 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); |
| 3968 | else |
| 3969 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3973 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3974 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3975 | return 0; |
| 3976 | } |
| 3977 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 3978 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 3979 | struct kvm_debugregs *dbgregs) |
| 3980 | { |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 3981 | unsigned long val; |
| 3982 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 3983 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 3984 | kvm_get_dr(vcpu, 6, &val); |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 3985 | dbgregs->dr6 = val; |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 3986 | dbgregs->dr7 = vcpu->arch.dr7; |
| 3987 | dbgregs->flags = 0; |
Vasiliy Kulikov | 97e69aa | 2010-10-30 22:54:47 +0400 | [diff] [blame] | 3988 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 3992 | struct kvm_debugregs *dbgregs) |
| 3993 | { |
| 3994 | if (dbgregs->flags) |
| 3995 | return -EINVAL; |
| 3996 | |
Paolo Bonzini | d14bdb5 | 2016-06-01 14:09:23 +0200 | [diff] [blame] | 3997 | if (dbgregs->dr6 & ~0xffffffffull) |
| 3998 | return -EINVAL; |
| 3999 | if (dbgregs->dr7 & ~0xffffffffull) |
| 4000 | return -EINVAL; |
| 4001 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4002 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
Nadav Amit | ae561ed | 2015-04-02 03:10:37 +0300 | [diff] [blame] | 4003 | kvm_update_dr0123(vcpu); |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4004 | vcpu->arch.dr6 = dbgregs->dr6; |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 4005 | kvm_update_dr6(vcpu); |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4006 | vcpu->arch.dr7 = dbgregs->dr7; |
Jan Kiszka | 9926c9f | 2014-01-04 18:47:15 +0100 | [diff] [blame] | 4007 | kvm_update_dr7(vcpu); |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4008 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4009 | return 0; |
| 4010 | } |
| 4011 | |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4012 | #define XSTATE_COMPACTION_ENABLED (1ULL << 63) |
| 4013 | |
| 4014 | static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) |
| 4015 | { |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 4016 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 4017 | u64 xstate_bv = xsave->header.xfeatures; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4018 | u64 valid; |
| 4019 | |
| 4020 | /* |
| 4021 | * Copy legacy XSAVE area, to avoid complications with CPUID |
| 4022 | * leaves 0 and 1 in the loop below. |
| 4023 | */ |
| 4024 | memcpy(dest, xsave, XSAVE_HDR_OFFSET); |
| 4025 | |
| 4026 | /* Set XSTATE_BV */ |
Radim Krčmář | 00c87e9 | 2017-02-01 14:19:53 +0100 | [diff] [blame] | 4027 | xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4028 | *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv; |
| 4029 | |
| 4030 | /* |
| 4031 | * Copy each region from the possibly compacted offset to the |
| 4032 | * non-compacted offset. |
| 4033 | */ |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 4034 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4035 | while (valid) { |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4036 | u64 xfeature_mask = valid & -valid; |
| 4037 | int xfeature_nr = fls64(xfeature_mask) - 1; |
| 4038 | void *src = get_xsave_addr(xsave, xfeature_nr); |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4039 | |
| 4040 | if (src) { |
| 4041 | u32 size, offset, ecx, edx; |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4042 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4043 | &size, &offset, &ecx, &edx); |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4044 | if (xfeature_nr == XFEATURE_PKRU) |
Paolo Bonzini | 38cfd5e | 2017-08-23 23:16:29 +0200 | [diff] [blame] | 4045 | memcpy(dest + offset, &vcpu->arch.pkru, |
| 4046 | sizeof(vcpu->arch.pkru)); |
| 4047 | else |
| 4048 | memcpy(dest + offset, src, size); |
| 4049 | |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4050 | } |
| 4051 | |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4052 | valid -= xfeature_mask; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) |
| 4057 | { |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 4058 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4059 | u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); |
| 4060 | u64 valid; |
| 4061 | |
| 4062 | /* |
| 4063 | * Copy legacy XSAVE area, to avoid complications with CPUID |
| 4064 | * leaves 0 and 1 in the loop below. |
| 4065 | */ |
| 4066 | memcpy(xsave, src, XSAVE_HDR_OFFSET); |
| 4067 | |
| 4068 | /* Set XSTATE_BV and possibly XCOMP_BV. */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 4069 | xsave->header.xfeatures = xstate_bv; |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4070 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Ingo Molnar | 3a54450 | 2015-04-24 10:14:36 +0200 | [diff] [blame] | 4071 | xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4072 | |
| 4073 | /* |
| 4074 | * Copy each region from the non-compacted offset to the |
| 4075 | * possibly compacted offset. |
| 4076 | */ |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 4077 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4078 | while (valid) { |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4079 | u64 xfeature_mask = valid & -valid; |
| 4080 | int xfeature_nr = fls64(xfeature_mask) - 1; |
| 4081 | void *dest = get_xsave_addr(xsave, xfeature_nr); |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4082 | |
| 4083 | if (dest) { |
| 4084 | u32 size, offset, ecx, edx; |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4085 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4086 | &size, &offset, &ecx, &edx); |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4087 | if (xfeature_nr == XFEATURE_PKRU) |
Paolo Bonzini | 38cfd5e | 2017-08-23 23:16:29 +0200 | [diff] [blame] | 4088 | memcpy(&vcpu->arch.pkru, src + offset, |
| 4089 | sizeof(vcpu->arch.pkru)); |
| 4090 | else |
| 4091 | memcpy(dest, src + offset, size); |
Wanpeng Li | ee4100d | 2015-07-09 15:44:52 +0800 | [diff] [blame] | 4092 | } |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4093 | |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 4094 | valid -= xfeature_mask; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4098 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
| 4099 | struct kvm_xsave *guest_xsave) |
| 4100 | { |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 4101 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4102 | memset(guest_xsave, 0, sizeof(struct kvm_xsave)); |
| 4103 | fill_xsave((u8 *) guest_xsave->region, vcpu); |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 4104 | } else { |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4105 | memcpy(guest_xsave->region, |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 4106 | &vcpu->arch.guest_fpu->state.fxsave, |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 4107 | sizeof(struct fxregs_state)); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4108 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 4109 | XFEATURE_MASK_FPSSE; |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | |
Wanpeng Li | a575813 | 2017-05-11 02:58:55 -0700 | [diff] [blame] | 4113 | #define XSAVE_MXCSR_OFFSET 24 |
| 4114 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4115 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
| 4116 | struct kvm_xsave *guest_xsave) |
| 4117 | { |
| 4118 | u64 xstate_bv = |
| 4119 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; |
Wanpeng Li | a575813 | 2017-05-11 02:58:55 -0700 | [diff] [blame] | 4120 | u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)]; |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4121 | |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 4122 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 4123 | /* |
| 4124 | * Here we allow setting states that are not present in |
| 4125 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility |
| 4126 | * with old userspace. |
| 4127 | */ |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 4128 | if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask) |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 4129 | return -EINVAL; |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 4130 | load_xsave(vcpu, (u8 *)guest_xsave->region); |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 4131 | } else { |
Wanpeng Li | a575813 | 2017-05-11 02:58:55 -0700 | [diff] [blame] | 4132 | if (xstate_bv & ~XFEATURE_MASK_FPSSE || |
| 4133 | mxcsr & ~mxcsr_feature_mask) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4134 | return -EINVAL; |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 4135 | memcpy(&vcpu->arch.guest_fpu->state.fxsave, |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 4136 | guest_xsave->region, sizeof(struct fxregs_state)); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4137 | } |
| 4138 | return 0; |
| 4139 | } |
| 4140 | |
| 4141 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, |
| 4142 | struct kvm_xcrs *guest_xcrs) |
| 4143 | { |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 4144 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4145 | guest_xcrs->nr_xcrs = 0; |
| 4146 | return; |
| 4147 | } |
| 4148 | |
| 4149 | guest_xcrs->nr_xcrs = 1; |
| 4150 | guest_xcrs->flags = 0; |
| 4151 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; |
| 4152 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; |
| 4153 | } |
| 4154 | |
| 4155 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, |
| 4156 | struct kvm_xcrs *guest_xcrs) |
| 4157 | { |
| 4158 | int i, r = 0; |
| 4159 | |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 4160 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4161 | return -EINVAL; |
| 4162 | |
| 4163 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) |
| 4164 | return -EINVAL; |
| 4165 | |
| 4166 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) |
| 4167 | /* Only support XCR0 currently */ |
Paolo Bonzini | c67a04c | 2013-10-17 16:50:47 +0200 | [diff] [blame] | 4168 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4169 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
Paolo Bonzini | c67a04c | 2013-10-17 16:50:47 +0200 | [diff] [blame] | 4170 | guest_xcrs->xcrs[i].value); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4171 | break; |
| 4172 | } |
| 4173 | if (r) |
| 4174 | r = -EINVAL; |
| 4175 | return r; |
| 4176 | } |
| 4177 | |
Eric B Munson | 1c0b28c | 2012-03-10 14:37:27 -0500 | [diff] [blame] | 4178 | /* |
| 4179 | * kvm_set_guest_paused() indicates to the guest kernel that it has been |
| 4180 | * stopped by the hypervisor. This function will be called from the host only. |
| 4181 | * EINVAL is returned when the host attempts to set the flag for a guest that |
| 4182 | * does not support pv clocks. |
| 4183 | */ |
| 4184 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) |
| 4185 | { |
Andy Honig | 0b79459 | 2013-02-20 14:48:10 -0800 | [diff] [blame] | 4186 | if (!vcpu->arch.pv_time_enabled) |
Eric B Munson | 1c0b28c | 2012-03-10 14:37:27 -0500 | [diff] [blame] | 4187 | return -EINVAL; |
Marcelo Tosatti | 51d59c6 | 2012-08-03 15:57:49 -0300 | [diff] [blame] | 4188 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
Eric B Munson | 1c0b28c | 2012-03-10 14:37:27 -0500 | [diff] [blame] | 4189 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 4190 | return 0; |
| 4191 | } |
| 4192 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 4193 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 4194 | struct kvm_enable_cap *cap) |
| 4195 | { |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 4196 | int r; |
| 4197 | uint16_t vmcs_version; |
| 4198 | void __user *user_ptr; |
| 4199 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 4200 | if (cap->flags) |
| 4201 | return -EINVAL; |
| 4202 | |
| 4203 | switch (cap->cap) { |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 4204 | case KVM_CAP_HYPERV_SYNIC2: |
| 4205 | if (cap->args[0]) |
| 4206 | return -EINVAL; |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 4207 | /* fall through */ |
| 4208 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 4209 | case KVM_CAP_HYPERV_SYNIC: |
Wanpeng Li | 546d87e | 2017-01-03 18:56:19 -0800 | [diff] [blame] | 4210 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 4211 | return -EINVAL; |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 4212 | return kvm_hv_activate_synic(vcpu, cap->cap == |
| 4213 | KVM_CAP_HYPERV_SYNIC2); |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 4214 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
Sean Christopherson | 5158917 | 2018-12-03 13:53:10 -0800 | [diff] [blame] | 4215 | if (!kvm_x86_ops->nested_enable_evmcs) |
| 4216 | return -ENOTTY; |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 4217 | r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version); |
| 4218 | if (!r) { |
| 4219 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; |
| 4220 | if (copy_to_user(user_ptr, &vmcs_version, |
| 4221 | sizeof(vmcs_version))) |
| 4222 | r = -EFAULT; |
| 4223 | } |
| 4224 | return r; |
Tianyu Lan | 344c6c8 | 2019-08-22 22:30:20 +0800 | [diff] [blame] | 4225 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
| 4226 | if (!kvm_x86_ops->enable_direct_tlbflush) |
| 4227 | return -ENOTTY; |
| 4228 | |
| 4229 | return kvm_x86_ops->enable_direct_tlbflush(vcpu); |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 4230 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 4231 | default: |
| 4232 | return -EINVAL; |
| 4233 | } |
| 4234 | } |
| 4235 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4236 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 4237 | unsigned int ioctl, unsigned long arg) |
| 4238 | { |
| 4239 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4240 | void __user *argp = (void __user *)arg; |
| 4241 | int r; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4242 | union { |
| 4243 | struct kvm_lapic_state *lapic; |
| 4244 | struct kvm_xsave *xsave; |
| 4245 | struct kvm_xcrs *xcrs; |
| 4246 | void *buffer; |
| 4247 | } u; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4248 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4249 | vcpu_load(vcpu); |
| 4250 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4251 | u.buffer = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4252 | switch (ioctl) { |
| 4253 | case KVM_GET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 4254 | r = -EINVAL; |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 4255 | if (!lapic_in_kernel(vcpu)) |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 4256 | goto out; |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 4257 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
| 4258 | GFP_KERNEL_ACCOUNT); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4259 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 4260 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4261 | if (!u.lapic) |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 4262 | goto out; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4263 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4264 | if (r) |
| 4265 | goto out; |
| 4266 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4267 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4268 | goto out; |
| 4269 | r = 0; |
| 4270 | break; |
| 4271 | } |
| 4272 | case KVM_SET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 4273 | r = -EINVAL; |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 4274 | if (!lapic_in_kernel(vcpu)) |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 4275 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4276 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4277 | if (IS_ERR(u.lapic)) { |
| 4278 | r = PTR_ERR(u.lapic); |
| 4279 | goto out_nofree; |
| 4280 | } |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4281 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4282 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4283 | break; |
| 4284 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 4285 | case KVM_INTERRUPT: { |
| 4286 | struct kvm_interrupt irq; |
| 4287 | |
| 4288 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4289 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 4290 | goto out; |
| 4291 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 4292 | break; |
| 4293 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 4294 | case KVM_NMI: { |
| 4295 | r = kvm_vcpu_ioctl_nmi(vcpu); |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 4296 | break; |
| 4297 | } |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 4298 | case KVM_SMI: { |
| 4299 | r = kvm_vcpu_ioctl_smi(vcpu); |
| 4300 | break; |
| 4301 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4302 | case KVM_SET_CPUID: { |
| 4303 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 4304 | struct kvm_cpuid cpuid; |
| 4305 | |
| 4306 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4307 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4308 | goto out; |
| 4309 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4310 | break; |
| 4311 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4312 | case KVM_SET_CPUID2: { |
| 4313 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 4314 | struct kvm_cpuid2 cpuid; |
| 4315 | |
| 4316 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4317 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4318 | goto out; |
| 4319 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4320 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4321 | break; |
| 4322 | } |
| 4323 | case KVM_GET_CPUID2: { |
| 4324 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 4325 | struct kvm_cpuid2 cpuid; |
| 4326 | |
| 4327 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4328 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4329 | goto out; |
| 4330 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4331 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4332 | if (r) |
| 4333 | goto out; |
| 4334 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4335 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4336 | goto out; |
| 4337 | r = 0; |
| 4338 | break; |
| 4339 | } |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4340 | case KVM_GET_MSRS: { |
| 4341 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4342 | r = msr_io(vcpu, argp, do_get_msr, 1); |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4343 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4344 | break; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4345 | } |
| 4346 | case KVM_SET_MSRS: { |
| 4347 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4348 | r = msr_io(vcpu, argp, do_set_msr, 0); |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4349 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4350 | break; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4351 | } |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 4352 | case KVM_TPR_ACCESS_REPORTING: { |
| 4353 | struct kvm_tpr_access_ctl tac; |
| 4354 | |
| 4355 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4356 | if (copy_from_user(&tac, argp, sizeof(tac))) |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 4357 | goto out; |
| 4358 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 4359 | if (r) |
| 4360 | goto out; |
| 4361 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4362 | if (copy_to_user(argp, &tac, sizeof(tac))) |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 4363 | goto out; |
| 4364 | r = 0; |
| 4365 | break; |
| 4366 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4367 | case KVM_SET_VAPIC_ADDR: { |
| 4368 | struct kvm_vapic_addr va; |
Paolo Bonzini | 7301d6a | 2016-11-17 15:55:46 +0100 | [diff] [blame] | 4369 | int idx; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4370 | |
| 4371 | r = -EINVAL; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4372 | if (!lapic_in_kernel(vcpu)) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4373 | goto out; |
| 4374 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4375 | if (copy_from_user(&va, argp, sizeof(va))) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4376 | goto out; |
Paolo Bonzini | 7301d6a | 2016-11-17 15:55:46 +0100 | [diff] [blame] | 4377 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 4378 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
Paolo Bonzini | 7301d6a | 2016-11-17 15:55:46 +0100 | [diff] [blame] | 4379 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4380 | break; |
| 4381 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 4382 | case KVM_X86_SETUP_MCE: { |
| 4383 | u64 mcg_cap; |
| 4384 | |
| 4385 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4386 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 4387 | goto out; |
| 4388 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 4389 | break; |
| 4390 | } |
| 4391 | case KVM_X86_SET_MCE: { |
| 4392 | struct kvm_x86_mce mce; |
| 4393 | |
| 4394 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4395 | if (copy_from_user(&mce, argp, sizeof(mce))) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 4396 | goto out; |
| 4397 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 4398 | break; |
| 4399 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4400 | case KVM_GET_VCPU_EVENTS: { |
| 4401 | struct kvm_vcpu_events events; |
| 4402 | |
| 4403 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 4404 | |
| 4405 | r = -EFAULT; |
| 4406 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 4407 | break; |
| 4408 | r = 0; |
| 4409 | break; |
| 4410 | } |
| 4411 | case KVM_SET_VCPU_EVENTS: { |
| 4412 | struct kvm_vcpu_events events; |
| 4413 | |
| 4414 | r = -EFAULT; |
| 4415 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 4416 | break; |
| 4417 | |
| 4418 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 4419 | break; |
| 4420 | } |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 4421 | case KVM_GET_DEBUGREGS: { |
| 4422 | struct kvm_debugregs dbgregs; |
| 4423 | |
| 4424 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 4425 | |
| 4426 | r = -EFAULT; |
| 4427 | if (copy_to_user(argp, &dbgregs, |
| 4428 | sizeof(struct kvm_debugregs))) |
| 4429 | break; |
| 4430 | r = 0; |
| 4431 | break; |
| 4432 | } |
| 4433 | case KVM_SET_DEBUGREGS: { |
| 4434 | struct kvm_debugregs dbgregs; |
| 4435 | |
| 4436 | r = -EFAULT; |
| 4437 | if (copy_from_user(&dbgregs, argp, |
| 4438 | sizeof(struct kvm_debugregs))) |
| 4439 | break; |
| 4440 | |
| 4441 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 4442 | break; |
| 4443 | } |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4444 | case KVM_GET_XSAVE: { |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 4445 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4446 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4447 | if (!u.xsave) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4448 | break; |
| 4449 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4450 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4451 | |
| 4452 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4453 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4454 | break; |
| 4455 | r = 0; |
| 4456 | break; |
| 4457 | } |
| 4458 | case KVM_SET_XSAVE: { |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4459 | u.xsave = memdup_user(argp, sizeof(*u.xsave)); |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4460 | if (IS_ERR(u.xsave)) { |
| 4461 | r = PTR_ERR(u.xsave); |
| 4462 | goto out_nofree; |
| 4463 | } |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4464 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4465 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4466 | break; |
| 4467 | } |
| 4468 | case KVM_GET_XCRS: { |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 4469 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4470 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4471 | if (!u.xcrs) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4472 | break; |
| 4473 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4474 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4475 | |
| 4476 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4477 | if (copy_to_user(argp, u.xcrs, |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4478 | sizeof(struct kvm_xcrs))) |
| 4479 | break; |
| 4480 | r = 0; |
| 4481 | break; |
| 4482 | } |
| 4483 | case KVM_SET_XCRS: { |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4484 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4485 | if (IS_ERR(u.xcrs)) { |
| 4486 | r = PTR_ERR(u.xcrs); |
| 4487 | goto out_nofree; |
| 4488 | } |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4489 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4490 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 4491 | break; |
| 4492 | } |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 4493 | case KVM_SET_TSC_KHZ: { |
| 4494 | u32 user_tsc_khz; |
| 4495 | |
| 4496 | r = -EINVAL; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 4497 | user_tsc_khz = (u32)arg; |
| 4498 | |
| 4499 | if (user_tsc_khz >= kvm_max_guest_tsc_khz) |
| 4500 | goto out; |
| 4501 | |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 4502 | if (user_tsc_khz == 0) |
| 4503 | user_tsc_khz = tsc_khz; |
| 4504 | |
Haozhong Zhang | 381d585 | 2015-10-20 15:39:04 +0800 | [diff] [blame] | 4505 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
| 4506 | r = 0; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 4507 | |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 4508 | goto out; |
| 4509 | } |
| 4510 | case KVM_GET_TSC_KHZ: { |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 4511 | r = vcpu->arch.virtual_tsc_khz; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 4512 | goto out; |
| 4513 | } |
Eric B Munson | 1c0b28c | 2012-03-10 14:37:27 -0500 | [diff] [blame] | 4514 | case KVM_KVMCLOCK_CTRL: { |
| 4515 | r = kvm_set_guest_paused(vcpu); |
| 4516 | goto out; |
| 4517 | } |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 4518 | case KVM_ENABLE_CAP: { |
| 4519 | struct kvm_enable_cap cap; |
| 4520 | |
| 4521 | r = -EFAULT; |
| 4522 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 4523 | goto out; |
| 4524 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 4525 | break; |
| 4526 | } |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4527 | case KVM_GET_NESTED_STATE: { |
| 4528 | struct kvm_nested_state __user *user_kvm_nested_state = argp; |
| 4529 | u32 user_data_size; |
| 4530 | |
| 4531 | r = -EINVAL; |
| 4532 | if (!kvm_x86_ops->get_nested_state) |
| 4533 | break; |
| 4534 | |
| 4535 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4536 | r = -EFAULT; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4537 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4538 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4539 | |
| 4540 | r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state, |
| 4541 | user_data_size); |
| 4542 | if (r < 0) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4543 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4544 | |
| 4545 | if (r > user_data_size) { |
| 4546 | if (put_user(r, &user_kvm_nested_state->size)) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4547 | r = -EFAULT; |
| 4548 | else |
| 4549 | r = -E2BIG; |
| 4550 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4551 | } |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4552 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4553 | r = 0; |
| 4554 | break; |
| 4555 | } |
| 4556 | case KVM_SET_NESTED_STATE: { |
| 4557 | struct kvm_nested_state __user *user_kvm_nested_state = argp; |
| 4558 | struct kvm_nested_state kvm_state; |
Sean Christopherson | ad5996d | 2019-11-22 08:58:18 -0800 | [diff] [blame] | 4559 | int idx; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4560 | |
| 4561 | r = -EINVAL; |
| 4562 | if (!kvm_x86_ops->set_nested_state) |
| 4563 | break; |
| 4564 | |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4565 | r = -EFAULT; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4566 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4567 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4568 | |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4569 | r = -EINVAL; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4570 | if (kvm_state.size < sizeof(kvm_state)) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4571 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4572 | |
| 4573 | if (kvm_state.flags & |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 4574 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
| 4575 | | KVM_STATE_NESTED_EVMCS)) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4576 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4577 | |
| 4578 | /* nested_run_pending implies guest_mode. */ |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 4579 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
| 4580 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) |
Liran Alon | 26b471c | 2018-09-16 14:28:20 +0300 | [diff] [blame] | 4581 | break; |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4582 | |
Sean Christopherson | ad5996d | 2019-11-22 08:58:18 -0800 | [diff] [blame] | 4583 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4584 | r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state); |
Sean Christopherson | ad5996d | 2019-11-22 08:58:18 -0800 | [diff] [blame] | 4585 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 4586 | break; |
| 4587 | } |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 4588 | case KVM_GET_SUPPORTED_HV_CPUID: { |
| 4589 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 4590 | struct kvm_cpuid2 cpuid; |
| 4591 | |
| 4592 | r = -EFAULT; |
| 4593 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
| 4594 | goto out; |
| 4595 | |
| 4596 | r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid, |
| 4597 | cpuid_arg->entries); |
| 4598 | if (r) |
| 4599 | goto out; |
| 4600 | |
| 4601 | r = -EFAULT; |
| 4602 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
| 4603 | goto out; |
| 4604 | r = 0; |
| 4605 | break; |
| 4606 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4607 | default: |
| 4608 | r = -EINVAL; |
| 4609 | } |
| 4610 | out: |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 4611 | kfree(u.buffer); |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4612 | out_nofree: |
| 4613 | vcpu_put(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 4614 | return r; |
| 4615 | } |
| 4616 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 4617 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 4618 | { |
| 4619 | return VM_FAULT_SIGBUS; |
| 4620 | } |
| 4621 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4622 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 4623 | { |
| 4624 | int ret; |
| 4625 | |
| 4626 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
Guo Chao | 951179c | 2012-11-02 18:33:22 +0800 | [diff] [blame] | 4627 | return -EINVAL; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4628 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 4629 | return ret; |
| 4630 | } |
| 4631 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4632 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 4633 | u64 ident_addr) |
| 4634 | { |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4635 | return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr); |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4636 | } |
| 4637 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4638 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 4639 | unsigned long kvm_nr_mmu_pages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4640 | { |
| 4641 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 4642 | return -EINVAL; |
| 4643 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4644 | mutex_lock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4645 | |
| 4646 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 4647 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4648 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4649 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4650 | return 0; |
| 4651 | } |
| 4652 | |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 4653 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4654 | { |
Dave Hansen | 39de71e | 2010-08-19 18:11:14 -0700 | [diff] [blame] | 4655 | return kvm->arch.n_max_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4658 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 4659 | { |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4660 | struct kvm_pic *pic = kvm->arch.vpic; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4661 | int r; |
| 4662 | |
| 4663 | r = 0; |
| 4664 | switch (chip->chip_id) { |
| 4665 | case KVM_IRQCHIP_PIC_MASTER: |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4666 | memcpy(&chip->chip.pic, &pic->pics[0], |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4667 | sizeof(struct kvm_pic_state)); |
| 4668 | break; |
| 4669 | case KVM_IRQCHIP_PIC_SLAVE: |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4670 | memcpy(&chip->chip.pic, &pic->pics[1], |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4671 | sizeof(struct kvm_pic_state)); |
| 4672 | break; |
| 4673 | case KVM_IRQCHIP_IOAPIC: |
David Hildenbrand | 33392b4 | 2017-04-07 10:50:27 +0200 | [diff] [blame] | 4674 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4675 | break; |
| 4676 | default: |
| 4677 | r = -EINVAL; |
| 4678 | break; |
| 4679 | } |
| 4680 | return r; |
| 4681 | } |
| 4682 | |
| 4683 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 4684 | { |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4685 | struct kvm_pic *pic = kvm->arch.vpic; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4686 | int r; |
| 4687 | |
| 4688 | r = 0; |
| 4689 | switch (chip->chip_id) { |
| 4690 | case KVM_IRQCHIP_PIC_MASTER: |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4691 | spin_lock(&pic->lock); |
| 4692 | memcpy(&pic->pics[0], &chip->chip.pic, |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4693 | sizeof(struct kvm_pic_state)); |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4694 | spin_unlock(&pic->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4695 | break; |
| 4696 | case KVM_IRQCHIP_PIC_SLAVE: |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4697 | spin_lock(&pic->lock); |
| 4698 | memcpy(&pic->pics[1], &chip->chip.pic, |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4699 | sizeof(struct kvm_pic_state)); |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4700 | spin_unlock(&pic->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4701 | break; |
| 4702 | case KVM_IRQCHIP_IOAPIC: |
David Hildenbrand | 33392b4 | 2017-04-07 10:50:27 +0200 | [diff] [blame] | 4703 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4704 | break; |
| 4705 | default: |
| 4706 | r = -EINVAL; |
| 4707 | break; |
| 4708 | } |
David Hildenbrand | 90bca05 | 2017-04-07 10:50:23 +0200 | [diff] [blame] | 4709 | kvm_pic_update_irq(pic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4710 | return r; |
| 4711 | } |
| 4712 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 4713 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 4714 | { |
Radim Krčmář | 34f3941 | 2016-03-02 22:56:50 +0100 | [diff] [blame] | 4715 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
| 4716 | |
| 4717 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); |
| 4718 | |
| 4719 | mutex_lock(&kps->lock); |
| 4720 | memcpy(ps, &kps->channels, sizeof(*ps)); |
| 4721 | mutex_unlock(&kps->lock); |
Saurabh Sengar | 2da29bc | 2015-10-30 12:56:11 +0530 | [diff] [blame] | 4722 | return 0; |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 4726 | { |
Andrew Honig | 0185604 | 2015-11-18 14:50:23 -0800 | [diff] [blame] | 4727 | int i; |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4728 | struct kvm_pit *pit = kvm->arch.vpit; |
| 4729 | |
| 4730 | mutex_lock(&pit->pit_state.lock); |
Radim Krčmář | 34f3941 | 2016-03-02 22:56:50 +0100 | [diff] [blame] | 4731 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
Andrew Honig | 0185604 | 2015-11-18 14:50:23 -0800 | [diff] [blame] | 4732 | for (i = 0; i < 3; i++) |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4733 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
| 4734 | mutex_unlock(&pit->pit_state.lock); |
Saurabh Sengar | 2da29bc | 2015-10-30 12:56:11 +0530 | [diff] [blame] | 4735 | return 0; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 4739 | { |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4740 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 4741 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 4742 | sizeof(ps->channels)); |
| 4743 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 4744 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Vasiliy Kulikov | 97e69aa | 2010-10-30 22:54:47 +0400 | [diff] [blame] | 4745 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
Saurabh Sengar | 2da29bc | 2015-10-30 12:56:11 +0530 | [diff] [blame] | 4746 | return 0; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 4750 | { |
Saurabh Sengar | 2da29bc | 2015-10-30 12:56:11 +0530 | [diff] [blame] | 4751 | int start = 0; |
Andrew Honig | 0185604 | 2015-11-18 14:50:23 -0800 | [diff] [blame] | 4752 | int i; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4753 | u32 prev_legacy, cur_legacy; |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4754 | struct kvm_pit *pit = kvm->arch.vpit; |
| 4755 | |
| 4756 | mutex_lock(&pit->pit_state.lock); |
| 4757 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4758 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 4759 | if (!prev_legacy && cur_legacy) |
| 4760 | start = 1; |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4761 | memcpy(&pit->pit_state.channels, &ps->channels, |
| 4762 | sizeof(pit->pit_state.channels)); |
| 4763 | pit->pit_state.flags = ps->flags; |
Andrew Honig | 0185604 | 2015-11-18 14:50:23 -0800 | [diff] [blame] | 4764 | for (i = 0; i < 3; i++) |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4765 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
Paolo Bonzini | e5e57e7 | 2016-01-07 13:50:38 +0100 | [diff] [blame] | 4766 | start && i == 0); |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 4767 | mutex_unlock(&pit->pit_state.lock); |
Saurabh Sengar | 2da29bc | 2015-10-30 12:56:11 +0530 | [diff] [blame] | 4768 | return 0; |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 4769 | } |
| 4770 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 4771 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 4772 | struct kvm_reinject_control *control) |
| 4773 | { |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 4774 | struct kvm_pit *pit = kvm->arch.vpit; |
| 4775 | |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 4776 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
| 4777 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL |
| 4778 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. |
| 4779 | */ |
| 4780 | mutex_lock(&pit->pit_state.lock); |
| 4781 | kvm_pit_set_reinject(pit, control->pit_reinject); |
| 4782 | mutex_unlock(&pit->pit_state.lock); |
Radim Krčmář | b39c90b | 2016-03-02 22:56:44 +0100 | [diff] [blame] | 4783 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 4784 | return 0; |
| 4785 | } |
| 4786 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 4787 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 4788 | { |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 4789 | /* |
| 4790 | * Flush potentially hardware-cached dirty pages to dirty_bitmap. |
| 4791 | */ |
| 4792 | if (kvm_x86_ops->flush_log_dirty) |
| 4793 | kvm_x86_ops->flush_log_dirty(kvm); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 4794 | } |
| 4795 | |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 4796 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
| 4797 | bool line_status) |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 4798 | { |
| 4799 | if (!irqchip_in_kernel(kvm)) |
| 4800 | return -ENXIO; |
| 4801 | |
| 4802 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 4803 | irq_event->irq, irq_event->level, |
| 4804 | line_status); |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 4805 | return 0; |
| 4806 | } |
| 4807 | |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 4808 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 4809 | struct kvm_enable_cap *cap) |
Nadav Amit | 90de4a1 | 2015-04-13 01:53:41 +0300 | [diff] [blame] | 4810 | { |
| 4811 | int r; |
| 4812 | |
| 4813 | if (cap->flags) |
| 4814 | return -EINVAL; |
| 4815 | |
| 4816 | switch (cap->cap) { |
| 4817 | case KVM_CAP_DISABLE_QUIRKS: |
| 4818 | kvm->arch.disabled_quirks = cap->args[0]; |
| 4819 | r = 0; |
| 4820 | break; |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 4821 | case KVM_CAP_SPLIT_IRQCHIP: { |
| 4822 | mutex_lock(&kvm->lock); |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 4823 | r = -EINVAL; |
| 4824 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) |
| 4825 | goto split_irqchip_unlock; |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 4826 | r = -EEXIST; |
| 4827 | if (irqchip_in_kernel(kvm)) |
| 4828 | goto split_irqchip_unlock; |
Paolo Bonzini | 557abc4 | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 4829 | if (kvm->created_vcpus) |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 4830 | goto split_irqchip_unlock; |
| 4831 | r = kvm_setup_empty_irq_routing(kvm); |
David Hildenbrand | 5c0aea0 | 2017-04-28 17:06:20 +0200 | [diff] [blame] | 4832 | if (r) |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 4833 | goto split_irqchip_unlock; |
| 4834 | /* Pairs with irqchip_in_kernel. */ |
| 4835 | smp_wmb(); |
Radim Krčmář | 49776fa | 2016-12-16 16:10:02 +0100 | [diff] [blame] | 4836 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 4837 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 4838 | r = 0; |
| 4839 | split_irqchip_unlock: |
| 4840 | mutex_unlock(&kvm->lock); |
| 4841 | break; |
| 4842 | } |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 4843 | case KVM_CAP_X2APIC_API: |
| 4844 | r = -EINVAL; |
| 4845 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) |
| 4846 | break; |
| 4847 | |
| 4848 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) |
| 4849 | kvm->arch.x2apic_format = true; |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 4850 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
| 4851 | kvm->arch.x2apic_broadcast_quirk_disabled = true; |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 4852 | |
| 4853 | r = 0; |
| 4854 | break; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4855 | case KVM_CAP_X86_DISABLE_EXITS: |
| 4856 | r = -EINVAL; |
| 4857 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) |
| 4858 | break; |
| 4859 | |
| 4860 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && |
| 4861 | kvm_can_mwait_in_guest()) |
| 4862 | kvm->arch.mwait_in_guest = true; |
Michael S. Tsirkin | 766d357 | 2018-06-08 02:19:53 +0300 | [diff] [blame] | 4863 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 4864 | kvm->arch.hlt_in_guest = true; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 4865 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
| 4866 | kvm->arch.pause_in_guest = true; |
Wanpeng Li | b517006 | 2019-05-21 14:06:53 +0800 | [diff] [blame] | 4867 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
| 4868 | kvm->arch.cstate_in_guest = true; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4869 | r = 0; |
| 4870 | break; |
Drew Schmitt | 6fbbde9 | 2018-08-20 10:32:15 -0700 | [diff] [blame] | 4871 | case KVM_CAP_MSR_PLATFORM_INFO: |
| 4872 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; |
| 4873 | r = 0; |
| 4874 | break; |
Jim Mattson | c4f5519 | 2018-10-16 14:29:24 -0700 | [diff] [blame] | 4875 | case KVM_CAP_EXCEPTION_PAYLOAD: |
| 4876 | kvm->arch.exception_payload_enabled = cap->args[0]; |
| 4877 | r = 0; |
| 4878 | break; |
Nadav Amit | 90de4a1 | 2015-04-13 01:53:41 +0300 | [diff] [blame] | 4879 | default: |
| 4880 | r = -EINVAL; |
| 4881 | break; |
| 4882 | } |
| 4883 | return r; |
| 4884 | } |
| 4885 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4886 | long kvm_arch_vm_ioctl(struct file *filp, |
| 4887 | unsigned int ioctl, unsigned long arg) |
| 4888 | { |
| 4889 | struct kvm *kvm = filp->private_data; |
| 4890 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 4891 | int r = -ENOTTY; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 4892 | /* |
| 4893 | * This union makes it completely explicit to gcc-3.x |
| 4894 | * that these two variables' stack usage should be |
| 4895 | * combined, not added together. |
| 4896 | */ |
| 4897 | union { |
| 4898 | struct kvm_pit_state ps; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 4899 | struct kvm_pit_state2 ps2; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 4900 | struct kvm_pit_config pit_config; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 4901 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4902 | |
| 4903 | switch (ioctl) { |
| 4904 | case KVM_SET_TSS_ADDR: |
| 4905 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4906 | break; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4907 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 4908 | u64 ident_addr; |
| 4909 | |
David Hildenbrand | 1af1ac9 | 2017-08-24 20:51:36 +0200 | [diff] [blame] | 4910 | mutex_lock(&kvm->lock); |
| 4911 | r = -EINVAL; |
| 4912 | if (kvm->created_vcpus) |
| 4913 | goto set_identity_unlock; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4914 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4915 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
David Hildenbrand | 1af1ac9 | 2017-08-24 20:51:36 +0200 | [diff] [blame] | 4916 | goto set_identity_unlock; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4917 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
David Hildenbrand | 1af1ac9 | 2017-08-24 20:51:36 +0200 | [diff] [blame] | 4918 | set_identity_unlock: |
| 4919 | mutex_unlock(&kvm->lock); |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4920 | break; |
| 4921 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4922 | case KVM_SET_NR_MMU_PAGES: |
| 4923 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4924 | break; |
| 4925 | case KVM_GET_NR_MMU_PAGES: |
| 4926 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 4927 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4928 | case KVM_CREATE_IRQCHIP: { |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4929 | mutex_lock(&kvm->lock); |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4930 | |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4931 | r = -EEXIST; |
Radim Krčmář | 35e6eaa | 2016-12-16 16:10:01 +0100 | [diff] [blame] | 4932 | if (irqchip_in_kernel(kvm)) |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4933 | goto create_irqchip_unlock; |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4934 | |
Avi Kivity | 3e51570 | 2012-03-05 14:23:29 +0200 | [diff] [blame] | 4935 | r = -EINVAL; |
Paolo Bonzini | 557abc4 | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 4936 | if (kvm->created_vcpus) |
Avi Kivity | 3e51570 | 2012-03-05 14:23:29 +0200 | [diff] [blame] | 4937 | goto create_irqchip_unlock; |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4938 | |
| 4939 | r = kvm_pic_init(kvm); |
| 4940 | if (r) |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4941 | goto create_irqchip_unlock; |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4942 | |
| 4943 | r = kvm_ioapic_init(kvm); |
| 4944 | if (r) { |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4945 | kvm_pic_destroy(kvm); |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4946 | goto create_irqchip_unlock; |
| 4947 | } |
| 4948 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 4949 | r = kvm_setup_default_irq_routing(kvm); |
| 4950 | if (r) { |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 4951 | kvm_ioapic_destroy(kvm); |
Radim Krčmář | 0994136 | 2016-12-16 16:10:03 +0100 | [diff] [blame] | 4952 | kvm_pic_destroy(kvm); |
Paolo Bonzini | 71ba994 | 2015-07-29 12:31:15 +0200 | [diff] [blame] | 4953 | goto create_irqchip_unlock; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 4954 | } |
Radim Krčmář | 49776fa | 2016-12-16 16:10:02 +0100 | [diff] [blame] | 4955 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
Paolo Bonzini | 71ba994 | 2015-07-29 12:31:15 +0200 | [diff] [blame] | 4956 | smp_wmb(); |
Radim Krčmář | 49776fa | 2016-12-16 16:10:02 +0100 | [diff] [blame] | 4957 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4958 | create_irqchip_unlock: |
| 4959 | mutex_unlock(&kvm->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4960 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 4961 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4962 | case KVM_CREATE_PIT: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 4963 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 4964 | goto create_pit; |
| 4965 | case KVM_CREATE_PIT2: |
| 4966 | r = -EFAULT; |
| 4967 | if (copy_from_user(&u.pit_config, argp, |
| 4968 | sizeof(struct kvm_pit_config))) |
| 4969 | goto out; |
| 4970 | create_pit: |
Paolo Bonzini | 250715a | 2016-06-01 14:09:24 +0200 | [diff] [blame] | 4971 | mutex_lock(&kvm->lock); |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 4972 | r = -EEXIST; |
| 4973 | if (kvm->arch.vpit) |
| 4974 | goto create_pit_unlock; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4975 | r = -ENOMEM; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 4976 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4977 | if (kvm->arch.vpit) |
| 4978 | r = 0; |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 4979 | create_pit_unlock: |
Paolo Bonzini | 250715a | 2016-06-01 14:09:24 +0200 | [diff] [blame] | 4980 | mutex_unlock(&kvm->lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4981 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4982 | case KVM_GET_IRQCHIP: { |
| 4983 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4984 | struct kvm_irqchip *chip; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4985 | |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4986 | chip = memdup_user(argp, sizeof(*chip)); |
| 4987 | if (IS_ERR(chip)) { |
| 4988 | r = PTR_ERR(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4989 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 4990 | } |
| 4991 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 4992 | r = -ENXIO; |
Radim Krčmář | 826da32 | 2016-12-16 16:10:06 +0100 | [diff] [blame] | 4993 | if (!irqchip_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 4994 | goto get_irqchip_out; |
| 4995 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 4996 | if (r) |
| 4997 | goto get_irqchip_out; |
| 4998 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 4999 | if (copy_to_user(argp, chip, sizeof(*chip))) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5000 | goto get_irqchip_out; |
| 5001 | r = 0; |
| 5002 | get_irqchip_out: |
| 5003 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5004 | break; |
| 5005 | } |
| 5006 | case KVM_SET_IRQCHIP: { |
| 5007 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 5008 | struct kvm_irqchip *chip; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5009 | |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 5010 | chip = memdup_user(argp, sizeof(*chip)); |
| 5011 | if (IS_ERR(chip)) { |
| 5012 | r = PTR_ERR(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5013 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 5014 | } |
| 5015 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5016 | r = -ENXIO; |
Radim Krčmář | 826da32 | 2016-12-16 16:10:06 +0100 | [diff] [blame] | 5017 | if (!irqchip_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5018 | goto set_irqchip_out; |
| 5019 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5020 | set_irqchip_out: |
| 5021 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5022 | break; |
| 5023 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5024 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5025 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5026 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5027 | goto out; |
| 5028 | r = -ENXIO; |
| 5029 | if (!kvm->arch.vpit) |
| 5030 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5031 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5032 | if (r) |
| 5033 | goto out; |
| 5034 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5035 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5036 | goto out; |
| 5037 | r = 0; |
| 5038 | break; |
| 5039 | } |
| 5040 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5041 | r = -EFAULT; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 5042 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5043 | goto out; |
| 5044 | r = -ENXIO; |
| 5045 | if (!kvm->arch.vpit) |
| 5046 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 5047 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 5048 | break; |
| 5049 | } |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 5050 | case KVM_GET_PIT2: { |
| 5051 | r = -ENXIO; |
| 5052 | if (!kvm->arch.vpit) |
| 5053 | goto out; |
| 5054 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 5055 | if (r) |
| 5056 | goto out; |
| 5057 | r = -EFAULT; |
| 5058 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 5059 | goto out; |
| 5060 | r = 0; |
| 5061 | break; |
| 5062 | } |
| 5063 | case KVM_SET_PIT2: { |
| 5064 | r = -EFAULT; |
| 5065 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 5066 | goto out; |
| 5067 | r = -ENXIO; |
| 5068 | if (!kvm->arch.vpit) |
| 5069 | goto out; |
| 5070 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 5071 | break; |
| 5072 | } |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 5073 | case KVM_REINJECT_CONTROL: { |
| 5074 | struct kvm_reinject_control control; |
| 5075 | r = -EFAULT; |
| 5076 | if (copy_from_user(&control, argp, sizeof(control))) |
| 5077 | goto out; |
Miaohe Lin | cad23e7 | 2019-12-07 17:25:22 +0800 | [diff] [blame] | 5078 | r = -ENXIO; |
| 5079 | if (!kvm->arch.vpit) |
| 5080 | goto out; |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 5081 | r = kvm_vm_ioctl_reinject(kvm, &control); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 5082 | break; |
| 5083 | } |
Paolo Bonzini | d71ba78 | 2015-07-29 11:56:48 +0200 | [diff] [blame] | 5084 | case KVM_SET_BOOT_CPU_ID: |
| 5085 | r = 0; |
| 5086 | mutex_lock(&kvm->lock); |
Paolo Bonzini | 557abc4 | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 5087 | if (kvm->created_vcpus) |
Paolo Bonzini | d71ba78 | 2015-07-29 11:56:48 +0200 | [diff] [blame] | 5088 | r = -EBUSY; |
| 5089 | else |
| 5090 | kvm->arch.bsp_vcpu_id = arg; |
| 5091 | mutex_unlock(&kvm->lock); |
| 5092 | break; |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 5093 | case KVM_XEN_HVM_CONFIG: { |
Paolo Bonzini | 5177604 | 2017-10-26 15:45:47 +0200 | [diff] [blame] | 5094 | struct kvm_xen_hvm_config xhc; |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 5095 | r = -EFAULT; |
Paolo Bonzini | 5177604 | 2017-10-26 15:45:47 +0200 | [diff] [blame] | 5096 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 5097 | goto out; |
| 5098 | r = -EINVAL; |
Paolo Bonzini | 5177604 | 2017-10-26 15:45:47 +0200 | [diff] [blame] | 5099 | if (xhc.flags) |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 5100 | goto out; |
Paolo Bonzini | 5177604 | 2017-10-26 15:45:47 +0200 | [diff] [blame] | 5101 | memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc)); |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 5102 | r = 0; |
| 5103 | break; |
| 5104 | } |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5105 | case KVM_SET_CLOCK: { |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5106 | struct kvm_clock_data user_ns; |
| 5107 | u64 now_ns; |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5108 | |
| 5109 | r = -EFAULT; |
| 5110 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 5111 | goto out; |
| 5112 | |
| 5113 | r = -EINVAL; |
| 5114 | if (user_ns.flags) |
| 5115 | goto out; |
| 5116 | |
| 5117 | r = 0; |
Radim Krčmář | 0bc48be | 2017-05-16 22:50:00 +0200 | [diff] [blame] | 5118 | /* |
| 5119 | * TODO: userspace has to take care of races with VCPU_RUN, so |
| 5120 | * kvm_gen_update_masterclock() can be cut down to locked |
| 5121 | * pvclock_update_vm_gtod_copy(). |
| 5122 | */ |
| 5123 | kvm_gen_update_masterclock(kvm); |
Marcelo Tosatti | e891a32 | 2017-04-17 12:51:37 -0300 | [diff] [blame] | 5124 | now_ns = get_kvmclock_ns(kvm); |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 5125 | kvm->arch.kvmclock_offset += user_ns.clock - now_ns; |
Radim Krčmář | 0bc48be | 2017-05-16 22:50:00 +0200 | [diff] [blame] | 5126 | kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5127 | break; |
| 5128 | } |
| 5129 | case KVM_GET_CLOCK: { |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5130 | struct kvm_clock_data user_ns; |
| 5131 | u64 now_ns; |
| 5132 | |
Marcelo Tosatti | e891a32 | 2017-04-17 12:51:37 -0300 | [diff] [blame] | 5133 | now_ns = get_kvmclock_ns(kvm); |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 5134 | user_ns.clock = now_ns; |
Paolo Bonzini | e3fd9a9 | 2016-11-09 17:48:15 +0100 | [diff] [blame] | 5135 | user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; |
Vasiliy Kulikov | 97e69aa | 2010-10-30 22:54:47 +0400 | [diff] [blame] | 5136 | memset(&user_ns.pad, 0, sizeof(user_ns.pad)); |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 5137 | |
| 5138 | r = -EFAULT; |
| 5139 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 5140 | goto out; |
| 5141 | r = 0; |
| 5142 | break; |
| 5143 | } |
Brijesh Singh | 5acc5c0 | 2017-12-04 10:57:26 -0600 | [diff] [blame] | 5144 | case KVM_MEMORY_ENCRYPT_OP: { |
| 5145 | r = -ENOTTY; |
| 5146 | if (kvm_x86_ops->mem_enc_op) |
| 5147 | r = kvm_x86_ops->mem_enc_op(kvm, argp); |
| 5148 | break; |
| 5149 | } |
Brijesh Singh | 69eaede | 2017-12-04 10:57:26 -0600 | [diff] [blame] | 5150 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
| 5151 | struct kvm_enc_region region; |
| 5152 | |
| 5153 | r = -EFAULT; |
| 5154 | if (copy_from_user(®ion, argp, sizeof(region))) |
| 5155 | goto out; |
| 5156 | |
| 5157 | r = -ENOTTY; |
| 5158 | if (kvm_x86_ops->mem_enc_reg_region) |
| 5159 | r = kvm_x86_ops->mem_enc_reg_region(kvm, ®ion); |
| 5160 | break; |
| 5161 | } |
| 5162 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { |
| 5163 | struct kvm_enc_region region; |
| 5164 | |
| 5165 | r = -EFAULT; |
| 5166 | if (copy_from_user(®ion, argp, sizeof(region))) |
| 5167 | goto out; |
| 5168 | |
| 5169 | r = -ENOTTY; |
| 5170 | if (kvm_x86_ops->mem_enc_unreg_region) |
| 5171 | r = kvm_x86_ops->mem_enc_unreg_region(kvm, ®ion); |
| 5172 | break; |
| 5173 | } |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 5174 | case KVM_HYPERV_EVENTFD: { |
| 5175 | struct kvm_hyperv_eventfd hvevfd; |
| 5176 | |
| 5177 | r = -EFAULT; |
| 5178 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) |
| 5179 | goto out; |
| 5180 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); |
| 5181 | break; |
| 5182 | } |
Eric Hankland | 66bb8a0 | 2019-07-10 18:25:15 -0700 | [diff] [blame] | 5183 | case KVM_SET_PMU_EVENT_FILTER: |
| 5184 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); |
| 5185 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5186 | default: |
Paolo Bonzini | ad6260d | 2017-03-27 14:30:40 +0200 | [diff] [blame] | 5187 | r = -ENOTTY; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 5188 | } |
| 5189 | out: |
| 5190 | return r; |
| 5191 | } |
| 5192 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 5193 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5194 | { |
Paolo Bonzini | 24c29b7 | 2019-10-01 15:18:26 +0200 | [diff] [blame] | 5195 | struct x86_pmu_capability x86_pmu; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5196 | u32 dummy[2]; |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5197 | unsigned i; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5198 | |
Jim Mattson | e2ada66 | 2019-08-21 11:20:04 -0700 | [diff] [blame] | 5199 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5200 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
Paolo Bonzini | 24c29b7 | 2019-10-01 15:18:26 +0200 | [diff] [blame] | 5201 | |
| 5202 | perf_get_x86_pmu_capability(&x86_pmu); |
Jim Mattson | e2ada66 | 2019-08-21 11:20:04 -0700 | [diff] [blame] | 5203 | |
Xiaoyao Li | 6cbee2b | 2019-11-13 09:15:21 +0800 | [diff] [blame] | 5204 | num_msrs_to_save = 0; |
| 5205 | num_emulated_msrs = 0; |
| 5206 | num_msr_based_features = 0; |
| 5207 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5208 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
| 5209 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5210 | continue; |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5211 | |
| 5212 | /* |
| 5213 | * Even MSRs that are valid in the host may not be exposed |
Paolo Bonzini | 9dbe6cf | 2015-11-12 14:49:17 +0100 | [diff] [blame] | 5214 | * to the guests in some cases. |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5215 | */ |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5216 | switch (msrs_to_save_all[i]) { |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5217 | case MSR_IA32_BNDCFGS: |
Liran Alon | 503234b | 2018-09-14 03:25:53 +0300 | [diff] [blame] | 5218 | if (!kvm_mpx_supported()) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5219 | continue; |
| 5220 | break; |
Paolo Bonzini | 9dbe6cf | 2015-11-12 14:49:17 +0100 | [diff] [blame] | 5221 | case MSR_TSC_AUX: |
Sean Christopherson | 1390851 | 2020-03-02 15:56:57 -0800 | [diff] [blame] | 5222 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
Paolo Bonzini | 9dbe6cf | 2015-11-12 14:49:17 +0100 | [diff] [blame] | 5223 | continue; |
| 5224 | break; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5225 | case MSR_IA32_RTIT_CTL: |
| 5226 | case MSR_IA32_RTIT_STATUS: |
Sean Christopherson | 7b874c2 | 2020-03-02 15:56:59 -0800 | [diff] [blame] | 5227 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5228 | continue; |
| 5229 | break; |
| 5230 | case MSR_IA32_RTIT_CR3_MATCH: |
Sean Christopherson | 7b874c2 | 2020-03-02 15:56:59 -0800 | [diff] [blame] | 5231 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5232 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
| 5233 | continue; |
| 5234 | break; |
| 5235 | case MSR_IA32_RTIT_OUTPUT_BASE: |
| 5236 | case MSR_IA32_RTIT_OUTPUT_MASK: |
Sean Christopherson | 7b874c2 | 2020-03-02 15:56:59 -0800 | [diff] [blame] | 5237 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5238 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
| 5239 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) |
| 5240 | continue; |
| 5241 | break; |
| 5242 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: { |
Sean Christopherson | 7b874c2 | 2020-03-02 15:56:59 -0800 | [diff] [blame] | 5243 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5244 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5245 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
| 5246 | continue; |
| 5247 | break; |
Paolo Bonzini | cf05a67 | 2019-10-01 15:33:07 +0200 | [diff] [blame] | 5248 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5249 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
Paolo Bonzini | 24c29b7 | 2019-10-01 15:18:26 +0200 | [diff] [blame] | 5250 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
| 5251 | continue; |
| 5252 | break; |
Paolo Bonzini | cf05a67 | 2019-10-01 15:33:07 +0200 | [diff] [blame] | 5253 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5254 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
Paolo Bonzini | 24c29b7 | 2019-10-01 15:18:26 +0200 | [diff] [blame] | 5255 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
| 5256 | continue; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 5257 | } |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5258 | default: |
| 5259 | break; |
| 5260 | } |
| 5261 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5262 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5263 | } |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 5264 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5265 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
| 5266 | if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i])) |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 5267 | continue; |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 5268 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5269 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
Paolo Bonzini | 62ef68b | 2015-05-05 12:08:55 +0200 | [diff] [blame] | 5270 | } |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 5271 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5272 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 5273 | struct kvm_msr_entry msr; |
| 5274 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5275 | msr.index = msr_based_features_all[i]; |
Wanpeng Li | 66421c1 | 2018-02-28 14:03:30 +0800 | [diff] [blame] | 5276 | if (kvm_get_msr_feature(&msr)) |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 5277 | continue; |
| 5278 | |
Chenyi Qiang | 7a5ee6e | 2019-11-06 14:35:20 +0800 | [diff] [blame] | 5279 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 5280 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 5281 | } |
| 5282 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 5283 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 5284 | const void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5285 | { |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5286 | int handled = 0; |
| 5287 | int n; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5288 | |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5289 | do { |
| 5290 | n = min(len, 8); |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 5291 | if (!(lapic_in_kernel(vcpu) && |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 5292 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
| 5293 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5294 | break; |
| 5295 | handled += n; |
| 5296 | addr += n; |
| 5297 | len -= n; |
| 5298 | v += n; |
| 5299 | } while (len); |
| 5300 | |
| 5301 | return handled; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5302 | } |
| 5303 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 5304 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5305 | { |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5306 | int handled = 0; |
| 5307 | int n; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5308 | |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5309 | do { |
| 5310 | n = min(len, 8); |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 5311 | if (!(lapic_in_kernel(vcpu) && |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 5312 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
| 5313 | addr, n, v)) |
| 5314 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5315 | break; |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 5316 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5317 | handled += n; |
| 5318 | addr += n; |
| 5319 | len -= n; |
| 5320 | v += n; |
| 5321 | } while (len); |
| 5322 | |
| 5323 | return handled; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 5326 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
| 5327 | struct kvm_segment *var, int seg) |
| 5328 | { |
| 5329 | kvm_x86_ops->set_segment(vcpu, var, seg); |
| 5330 | } |
| 5331 | |
| 5332 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 5333 | struct kvm_segment *var, int seg) |
| 5334 | { |
| 5335 | kvm_x86_ops->get_segment(vcpu, var, seg); |
| 5336 | } |
| 5337 | |
Paolo Bonzini | 54987b7 | 2014-09-02 13:23:06 +0200 | [diff] [blame] | 5338 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
| 5339 | struct x86_exception *exception) |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 5340 | { |
| 5341 | gpa_t t_gpa; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 5342 | |
| 5343 | BUG_ON(!mmu_is_nested(vcpu)); |
| 5344 | |
| 5345 | /* NPT walks are always user-walks */ |
| 5346 | access |= PFERR_USER_MASK; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5347 | t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 5348 | |
| 5349 | return t_gpa; |
| 5350 | } |
| 5351 | |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5352 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
| 5353 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5354 | { |
| 5355 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5356 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5357 | } |
| 5358 | |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5359 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
| 5360 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5361 | { |
| 5362 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 5363 | access |= PFERR_FETCH_MASK; |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5364 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5365 | } |
| 5366 | |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5367 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
| 5368 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5369 | { |
| 5370 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 5371 | access |= PFERR_WRITE_MASK; |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5372 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | /* uses this to access any guest's mapped memory without checking CPL */ |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5376 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
| 5377 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5378 | { |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5379 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 5383 | struct kvm_vcpu *vcpu, u32 access, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5384 | struct x86_exception *exception) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5385 | { |
| 5386 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5387 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5388 | |
| 5389 | while (bytes) { |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 5390 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5391 | exception); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5392 | unsigned offset = addr & (PAGE_SIZE-1); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5393 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5394 | int ret; |
| 5395 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5396 | if (gpa == UNMAPPED_GVA) |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5397 | return X86EMUL_PROPAGATE_FAULT; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5398 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
| 5399 | offset, toread); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5400 | if (ret < 0) { |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 5401 | r = X86EMUL_IO_NEEDED; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5402 | goto out; |
| 5403 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5404 | |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5405 | bytes -= toread; |
| 5406 | data += toread; |
| 5407 | addr += toread; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5408 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5409 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5410 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5411 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5412 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5413 | /* used for instruction fetching */ |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5414 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
| 5415 | gva_t addr, void *val, unsigned int bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5416 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5417 | { |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5418 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5419 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Paolo Bonzini | 44583cb | 2014-05-13 14:02:13 +0200 | [diff] [blame] | 5420 | unsigned offset; |
| 5421 | int ret; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5422 | |
Paolo Bonzini | 44583cb | 2014-05-13 14:02:13 +0200 | [diff] [blame] | 5423 | /* Inline kvm_read_guest_virt_helper for speed. */ |
| 5424 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK, |
| 5425 | exception); |
| 5426 | if (unlikely(gpa == UNMAPPED_GVA)) |
| 5427 | return X86EMUL_PROPAGATE_FAULT; |
| 5428 | |
| 5429 | offset = addr & (PAGE_SIZE-1); |
| 5430 | if (WARN_ON(offset + bytes > PAGE_SIZE)) |
| 5431 | bytes = (unsigned)PAGE_SIZE - offset; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5432 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
| 5433 | offset, bytes); |
Paolo Bonzini | 44583cb | 2014-05-13 14:02:13 +0200 | [diff] [blame] | 5434 | if (unlikely(ret < 0)) |
| 5435 | return X86EMUL_IO_NEEDED; |
| 5436 | |
| 5437 | return X86EMUL_CONTINUE; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5438 | } |
| 5439 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5440 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5441 | gva_t addr, void *val, unsigned int bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5442 | struct x86_exception *exception) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5443 | { |
| 5444 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5445 | |
Paolo Bonzini | 353c095 | 2019-01-29 18:41:16 +0100 | [diff] [blame] | 5446 | /* |
| 5447 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED |
| 5448 | * is returned, but our callers are not ready for that and they blindly |
| 5449 | * call kvm_inject_page_fault. Ensure that they at least do not leak |
| 5450 | * uninitialized kernel stack memory into cr2 and error code. |
| 5451 | */ |
| 5452 | memset(exception, 0, sizeof(*exception)); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5453 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5454 | exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5455 | } |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 5456 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5457 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5458 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
| 5459 | gva_t addr, void *val, unsigned int bytes, |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5460 | struct x86_exception *exception, bool system) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5461 | { |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5462 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5463 | u32 access = 0; |
| 5464 | |
| 5465 | if (!system && kvm_x86_ops->get_cpl(vcpu) == 3) |
| 5466 | access |= PFERR_USER_MASK; |
| 5467 | |
| 5468 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5469 | } |
| 5470 | |
Radim Krčmář | 7a036a6 | 2015-10-30 16:36:24 +0100 | [diff] [blame] | 5471 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
| 5472 | unsigned long addr, void *val, unsigned int bytes) |
| 5473 | { |
| 5474 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 5475 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); |
| 5476 | |
| 5477 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; |
| 5478 | } |
| 5479 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5480 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 5481 | struct kvm_vcpu *vcpu, u32 access, |
| 5482 | struct x86_exception *exception) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5483 | { |
| 5484 | void *data = val; |
| 5485 | int r = X86EMUL_CONTINUE; |
| 5486 | |
| 5487 | while (bytes) { |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 5488 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5489 | access, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5490 | exception); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5491 | unsigned offset = addr & (PAGE_SIZE-1); |
| 5492 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 5493 | int ret; |
| 5494 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5495 | if (gpa == UNMAPPED_GVA) |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 5496 | return X86EMUL_PROPAGATE_FAULT; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5497 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5498 | if (ret < 0) { |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 5499 | r = X86EMUL_IO_NEEDED; |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5500 | goto out; |
| 5501 | } |
| 5502 | |
| 5503 | bytes -= towrite; |
| 5504 | data += towrite; |
| 5505 | addr += towrite; |
| 5506 | } |
| 5507 | out: |
| 5508 | return r; |
| 5509 | } |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5510 | |
| 5511 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5512 | unsigned int bytes, struct x86_exception *exception, |
| 5513 | bool system) |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5514 | { |
| 5515 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5516 | u32 access = PFERR_WRITE_MASK; |
| 5517 | |
| 5518 | if (!system && kvm_x86_ops->get_cpl(vcpu) == 3) |
| 5519 | access |= PFERR_USER_MASK; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5520 | |
| 5521 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5522 | access, exception); |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, |
| 5526 | unsigned int bytes, struct x86_exception *exception) |
| 5527 | { |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5528 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
| 5529 | vcpu->arch.l1tf_flush_l1d = true; |
| 5530 | |
Fuqian Huang | 541ab2a | 2019-09-12 12:18:17 +0800 | [diff] [blame] | 5531 | /* |
| 5532 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED |
| 5533 | * is returned, but our callers are not ready for that and they blindly |
| 5534 | * call kvm_inject_page_fault. Ensure that they at least do not leak |
| 5535 | * uninitialized kernel stack memory into cr2 and error code. |
| 5536 | */ |
| 5537 | memset(exception, 0, sizeof(*exception)); |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 5538 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
| 5539 | PFERR_WRITE_MASK, exception); |
| 5540 | } |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5541 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 5542 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 5543 | int handle_ud(struct kvm_vcpu *vcpu) |
| 5544 | { |
Masami Hiramatsu | b3dc069 | 2019-09-06 22:13:59 +0900 | [diff] [blame] | 5545 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 5546 | int emul_type = EMULTYPE_TRAP_UD; |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 5547 | char sig[5]; /* ud2; .ascii "kvm" */ |
| 5548 | struct x86_exception e; |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 5549 | |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 5550 | if (force_emulation_prefix && |
Paolo Bonzini | 3c9fa24 | 2018-06-06 17:38:09 +0200 | [diff] [blame] | 5551 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
| 5552 | sig, sizeof(sig), &e) == 0 && |
Masami Hiramatsu | b3dc069 | 2019-09-06 22:13:59 +0900 | [diff] [blame] | 5553 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 5554 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
Sean Christopherson | b400060 | 2019-08-27 14:40:32 -0700 | [diff] [blame] | 5555 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
Wanpeng Li | 6c86eed | 2018-04-03 16:28:49 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 5558 | return kvm_emulate_instruction(vcpu, emul_type); |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 5559 | } |
| 5560 | EXPORT_SYMBOL_GPL(handle_ud); |
| 5561 | |
Tom Lendacky | 0f89b20 | 2016-12-14 14:59:23 -0500 | [diff] [blame] | 5562 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
| 5563 | gpa_t gpa, bool write) |
| 5564 | { |
| 5565 | /* For APIC access vmexit */ |
| 5566 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 5567 | return 1; |
| 5568 | |
| 5569 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { |
| 5570 | trace_vcpu_match_mmio(gva, gpa, write, true); |
| 5571 | return 1; |
| 5572 | } |
| 5573 | |
| 5574 | return 0; |
| 5575 | } |
| 5576 | |
Xiao Guangrong | af7cc7d | 2011-07-12 03:22:46 +0800 | [diff] [blame] | 5577 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
| 5578 | gpa_t *gpa, struct x86_exception *exception, |
| 5579 | bool write) |
| 5580 | { |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 5581 | u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0) |
| 5582 | | (write ? PFERR_WRITE_MASK : 0); |
Xiao Guangrong | af7cc7d | 2011-07-12 03:22:46 +0800 | [diff] [blame] | 5583 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 5584 | /* |
| 5585 | * currently PKRU is only applied to ept enabled guest so |
| 5586 | * there is no pkey in EPT page table for L1 guest or EPT |
| 5587 | * shadow page table for L2 guest. |
| 5588 | */ |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 5589 | if (vcpu_match_mmio_gva(vcpu, gva) |
Feng Wu | 97ec8c0 | 2014-04-01 17:46:34 +0800 | [diff] [blame] | 5590 | && !permission_fault(vcpu, vcpu->arch.walk_mmu, |
Sean Christopherson | 871bd03 | 2019-08-01 13:35:21 -0700 | [diff] [blame] | 5591 | vcpu->arch.mmio_access, 0, access)) { |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 5592 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
| 5593 | (gva & (PAGE_SIZE - 1)); |
Xiao Guangrong | 4f02264 | 2011-07-12 03:34:24 +0800 | [diff] [blame] | 5594 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 5595 | return 1; |
| 5596 | } |
| 5597 | |
Xiao Guangrong | af7cc7d | 2011-07-12 03:22:46 +0800 | [diff] [blame] | 5598 | *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
| 5599 | |
| 5600 | if (*gpa == UNMAPPED_GVA) |
| 5601 | return -1; |
| 5602 | |
Tom Lendacky | 0f89b20 | 2016-12-14 14:59:23 -0500 | [diff] [blame] | 5603 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
Xiao Guangrong | af7cc7d | 2011-07-12 03:22:46 +0800 | [diff] [blame] | 5604 | } |
| 5605 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 5606 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 5607 | const void *val, int bytes) |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 5608 | { |
| 5609 | int ret; |
| 5610 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5611 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 5612 | if (ret < 0) |
| 5613 | return 0; |
Xiao Guangrong | 0eb05bf | 2016-02-24 17:51:13 +0800 | [diff] [blame] | 5614 | kvm_page_track_write(vcpu, gpa, val, bytes); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 5615 | return 1; |
| 5616 | } |
| 5617 | |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5618 | struct read_write_emulator_ops { |
| 5619 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, |
| 5620 | int bytes); |
| 5621 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5622 | void *val, int bytes); |
| 5623 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5624 | int bytes, void *val); |
| 5625 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5626 | void *val, int bytes); |
| 5627 | bool write; |
| 5628 | }; |
| 5629 | |
| 5630 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) |
| 5631 | { |
| 5632 | if (vcpu->mmio_read_completed) { |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5633 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 5634 | vcpu->mmio_fragments[0].gpa, val); |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5635 | vcpu->mmio_read_completed = 0; |
| 5636 | return 1; |
| 5637 | } |
| 5638 | |
| 5639 | return 0; |
| 5640 | } |
| 5641 | |
| 5642 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5643 | void *val, int bytes) |
| 5644 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5645 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5649 | void *val, int bytes) |
| 5650 | { |
| 5651 | return emulator_write_phys(vcpu, gpa, val, bytes); |
| 5652 | } |
| 5653 | |
| 5654 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) |
| 5655 | { |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 5656 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5657 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
| 5658 | } |
| 5659 | |
| 5660 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5661 | void *val, int bytes) |
| 5662 | { |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 5663 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5664 | return X86EMUL_IO_NEEDED; |
| 5665 | } |
| 5666 | |
| 5667 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 5668 | void *val, int bytes) |
| 5669 | { |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5670 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
| 5671 | |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 5672 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5673 | return X86EMUL_CONTINUE; |
| 5674 | } |
| 5675 | |
Mathias Krause | 0fbe9b0 | 2012-08-30 01:30:17 +0200 | [diff] [blame] | 5676 | static const struct read_write_emulator_ops read_emultor = { |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5677 | .read_write_prepare = read_prepare, |
| 5678 | .read_write_emulate = read_emulate, |
| 5679 | .read_write_mmio = vcpu_mmio_read, |
| 5680 | .read_write_exit_mmio = read_exit_mmio, |
| 5681 | }; |
| 5682 | |
Mathias Krause | 0fbe9b0 | 2012-08-30 01:30:17 +0200 | [diff] [blame] | 5683 | static const struct read_write_emulator_ops write_emultor = { |
Xiao Guangrong | 77d197b | 2011-07-13 14:31:50 +0800 | [diff] [blame] | 5684 | .read_write_emulate = write_emulate, |
| 5685 | .read_write_mmio = write_mmio, |
| 5686 | .read_write_exit_mmio = write_exit_mmio, |
| 5687 | .write = true, |
| 5688 | }; |
| 5689 | |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5690 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
| 5691 | unsigned int bytes, |
| 5692 | struct x86_exception *exception, |
| 5693 | struct kvm_vcpu *vcpu, |
Mathias Krause | 0fbe9b0 | 2012-08-30 01:30:17 +0200 | [diff] [blame] | 5694 | const struct read_write_emulator_ops *ops) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5695 | { |
Xiao Guangrong | af7cc7d | 2011-07-12 03:22:46 +0800 | [diff] [blame] | 5696 | gpa_t gpa; |
| 5697 | int handled, ret; |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5698 | bool write = ops->write; |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5699 | struct kvm_mmio_fragment *frag; |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 5700 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Tom Lendacky | 0f89b20 | 2016-12-14 14:59:23 -0500 | [diff] [blame] | 5701 | |
| 5702 | /* |
| 5703 | * If the exit was due to a NPF we may already have a GPA. |
| 5704 | * If the GPA is present, use it to avoid the GVA to GPA table walk. |
| 5705 | * Note, this cannot be used on string operations since string |
| 5706 | * operation using rep will only have the initial GPA from the NPF |
| 5707 | * occurred. |
| 5708 | */ |
Sean Christopherson | 744e699 | 2020-02-18 15:03:09 -0800 | [diff] [blame] | 5709 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
| 5710 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { |
| 5711 | gpa = ctxt->gpa_val; |
Brijesh Singh | 618232e | 2017-08-17 18:36:57 +0200 | [diff] [blame] | 5712 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
| 5713 | } else { |
| 5714 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); |
| 5715 | if (ret < 0) |
| 5716 | return X86EMUL_PROPAGATE_FAULT; |
Tom Lendacky | 0f89b20 | 2016-12-14 14:59:23 -0500 | [diff] [blame] | 5717 | } |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5718 | |
Brijesh Singh | 618232e | 2017-08-17 18:36:57 +0200 | [diff] [blame] | 5719 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5720 | return X86EMUL_CONTINUE; |
| 5721 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5722 | /* |
| 5723 | * Is this MMIO handled locally? |
| 5724 | */ |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5725 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5726 | if (handled == bytes) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5727 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5728 | |
Avi Kivity | 70252a1 | 2010-01-19 12:51:22 +0200 | [diff] [blame] | 5729 | gpa += handled; |
| 5730 | bytes -= handled; |
| 5731 | val += handled; |
| 5732 | |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 5733 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
| 5734 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; |
| 5735 | frag->gpa = gpa; |
| 5736 | frag->data = val; |
| 5737 | frag->len = bytes; |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5738 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5739 | } |
| 5740 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 5741 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
| 5742 | unsigned long addr, |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5743 | void *val, unsigned int bytes, |
| 5744 | struct x86_exception *exception, |
Mathias Krause | 0fbe9b0 | 2012-08-30 01:30:17 +0200 | [diff] [blame] | 5745 | const struct read_write_emulator_ops *ops) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5746 | { |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5747 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5748 | gpa_t gpa; |
| 5749 | int rc; |
| 5750 | |
| 5751 | if (ops->read_write_prepare && |
| 5752 | ops->read_write_prepare(vcpu, val, bytes)) |
| 5753 | return X86EMUL_CONTINUE; |
| 5754 | |
| 5755 | vcpu->mmio_nr_fragments = 0; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5756 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5757 | /* Crossing a page boundary? */ |
| 5758 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5759 | int now; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5760 | |
| 5761 | now = -addr & ~PAGE_MASK; |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5762 | rc = emulator_read_write_onepage(addr, val, now, exception, |
| 5763 | vcpu, ops); |
| 5764 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5765 | if (rc != X86EMUL_CONTINUE) |
| 5766 | return rc; |
| 5767 | addr += now; |
Nadav Amit | bac15531 | 2015-01-26 09:32:26 +0200 | [diff] [blame] | 5768 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
| 5769 | addr = (u32)addr; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5770 | val += now; |
| 5771 | bytes -= now; |
| 5772 | } |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5773 | |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5774 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
| 5775 | vcpu, ops); |
| 5776 | if (rc != X86EMUL_CONTINUE) |
| 5777 | return rc; |
| 5778 | |
| 5779 | if (!vcpu->mmio_nr_fragments) |
| 5780 | return rc; |
| 5781 | |
| 5782 | gpa = vcpu->mmio_fragments[0].gpa; |
| 5783 | |
| 5784 | vcpu->mmio_needed = 1; |
| 5785 | vcpu->mmio_cur_fragment = 0; |
| 5786 | |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 5787 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 5788 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
| 5789 | vcpu->run->exit_reason = KVM_EXIT_MMIO; |
| 5790 | vcpu->run->mmio.phys_addr = gpa; |
| 5791 | |
| 5792 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, |
| 5796 | unsigned long addr, |
| 5797 | void *val, |
| 5798 | unsigned int bytes, |
| 5799 | struct x86_exception *exception) |
| 5800 | { |
| 5801 | return emulator_read_write(ctxt, addr, val, bytes, |
| 5802 | exception, &read_emultor); |
| 5803 | } |
| 5804 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 5805 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
Xiao Guangrong | 22388a3 | 2011-07-13 14:32:31 +0800 | [diff] [blame] | 5806 | unsigned long addr, |
| 5807 | const void *val, |
| 5808 | unsigned int bytes, |
| 5809 | struct x86_exception *exception) |
| 5810 | { |
| 5811 | return emulator_read_write(ctxt, addr, (void *)val, bytes, |
| 5812 | exception, &write_emultor); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5813 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5814 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5815 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
| 5816 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) |
| 5817 | |
| 5818 | #ifdef CONFIG_X86_64 |
| 5819 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) |
| 5820 | #else |
| 5821 | # define CMPXCHG64(ptr, old, new) \ |
Jan Kiszka | 9749a6c | 2010-03-20 10:14:13 +0100 | [diff] [blame] | 5822 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5823 | #endif |
| 5824 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5825 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
| 5826 | unsigned long addr, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5827 | const void *old, |
| 5828 | const void *new, |
| 5829 | unsigned int bytes, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5830 | struct x86_exception *exception) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5831 | { |
KarimAllah Ahmed | 42e35f8 | 2019-01-31 21:24:39 +0100 | [diff] [blame] | 5832 | struct kvm_host_map map; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5833 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5834 | gpa_t gpa; |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5835 | char *kaddr; |
| 5836 | bool exchanged; |
| 5837 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5838 | /* guests cmpxchg8b have to be emulated atomically */ |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5839 | if (bytes > 8 || (bytes & (bytes - 1))) |
| 5840 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5841 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5842 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5843 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5844 | if (gpa == UNMAPPED_GVA || |
| 5845 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 5846 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5847 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5848 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 5849 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5850 | |
KarimAllah Ahmed | 42e35f8 | 2019-01-31 21:24:39 +0100 | [diff] [blame] | 5851 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
Wei Yongjun | c19b8bd | 2010-07-15 08:51:58 +0800 | [diff] [blame] | 5852 | goto emul_write; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5853 | |
KarimAllah Ahmed | 42e35f8 | 2019-01-31 21:24:39 +0100 | [diff] [blame] | 5854 | kaddr = map.hva + offset_in_page(gpa); |
| 5855 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5856 | switch (bytes) { |
| 5857 | case 1: |
| 5858 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); |
| 5859 | break; |
| 5860 | case 2: |
| 5861 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); |
| 5862 | break; |
| 5863 | case 4: |
| 5864 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); |
| 5865 | break; |
| 5866 | case 8: |
| 5867 | exchanged = CMPXCHG64(kaddr, old, new); |
| 5868 | break; |
| 5869 | default: |
| 5870 | BUG(); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5871 | } |
KarimAllah Ahmed | 42e35f8 | 2019-01-31 21:24:39 +0100 | [diff] [blame] | 5872 | |
| 5873 | kvm_vcpu_unmap(vcpu, &map, true); |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5874 | |
| 5875 | if (!exchanged) |
| 5876 | return X86EMUL_CMPXCHG_FAILED; |
| 5877 | |
Xiao Guangrong | 0eb05bf | 2016-02-24 17:51:13 +0800 | [diff] [blame] | 5878 | kvm_page_track_write(vcpu, gpa, new, bytes); |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 5879 | |
| 5880 | return X86EMUL_CONTINUE; |
Avi Kivity | 4a5f48f | 2010-03-15 13:59:55 +0200 | [diff] [blame] | 5881 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 5882 | emul_write: |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 5883 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 5884 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5885 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5886 | } |
| 5887 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5888 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
| 5889 | { |
Wanpeng Li | cbfc6c9 | 2017-05-19 02:46:56 -0700 | [diff] [blame] | 5890 | int r = 0, i; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5891 | |
Wanpeng Li | cbfc6c9 | 2017-05-19 02:46:56 -0700 | [diff] [blame] | 5892 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
| 5893 | if (vcpu->arch.pio.in) |
| 5894 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, |
| 5895 | vcpu->arch.pio.size, pd); |
| 5896 | else |
| 5897 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, |
| 5898 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 5899 | pd); |
| 5900 | if (r) |
| 5901 | break; |
| 5902 | pd += vcpu->arch.pio.size; |
| 5903 | } |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5904 | return r; |
| 5905 | } |
| 5906 | |
Xiao Guangrong | 6f6fbe9 | 2011-09-22 16:55:10 +0800 | [diff] [blame] | 5907 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
| 5908 | unsigned short port, void *val, |
| 5909 | unsigned int count, bool in) |
| 5910 | { |
Xiao Guangrong | 6f6fbe9 | 2011-09-22 16:55:10 +0800 | [diff] [blame] | 5911 | vcpu->arch.pio.port = port; |
| 5912 | vcpu->arch.pio.in = in; |
| 5913 | vcpu->arch.pio.count = count; |
| 5914 | vcpu->arch.pio.size = size; |
| 5915 | |
| 5916 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
| 5917 | vcpu->arch.pio.count = 0; |
| 5918 | return 1; |
| 5919 | } |
| 5920 | |
| 5921 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 5922 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 5923 | vcpu->run->io.size = size; |
| 5924 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 5925 | vcpu->run->io.count = count; |
| 5926 | vcpu->run->io.port = port; |
| 5927 | |
| 5928 | return 0; |
| 5929 | } |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5930 | |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 5931 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
| 5932 | unsigned short port, void *val, unsigned int count) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5933 | { |
Xiao Guangrong | 6f6fbe9 | 2011-09-22 16:55:10 +0800 | [diff] [blame] | 5934 | int ret; |
Avi Kivity | ca1d4a9 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5935 | |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 5936 | if (vcpu->arch.pio.count) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5937 | goto data_avail; |
| 5938 | |
Wanpeng Li | cbfc6c9 | 2017-05-19 02:46:56 -0700 | [diff] [blame] | 5939 | memset(vcpu->arch.pio_data, 0, size * count); |
| 5940 | |
Xiao Guangrong | 6f6fbe9 | 2011-09-22 16:55:10 +0800 | [diff] [blame] | 5941 | ret = emulator_pio_in_out(vcpu, size, port, val, count, true); |
| 5942 | if (ret) { |
| 5943 | data_avail: |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5944 | memcpy(val, vcpu->arch.pio_data, size * count); |
Ulrich Obergfell | 1171903 | 2014-05-02 17:57:47 +0200 | [diff] [blame] | 5945 | trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data); |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 5946 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5947 | return 1; |
| 5948 | } |
| 5949 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5950 | return 0; |
| 5951 | } |
| 5952 | |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 5953 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
| 5954 | int size, unsigned short port, void *val, |
| 5955 | unsigned int count) |
| 5956 | { |
| 5957 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
| 5958 | |
| 5959 | } |
| 5960 | |
| 5961 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
| 5962 | unsigned short port, const void *val, |
| 5963 | unsigned int count) |
| 5964 | { |
| 5965 | memcpy(vcpu->arch.pio_data, val, size * count); |
| 5966 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
| 5967 | return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); |
| 5968 | } |
| 5969 | |
Avi Kivity | ca1d4a9 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 5970 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
| 5971 | int size, unsigned short port, |
| 5972 | const void *val, unsigned int count) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5973 | { |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 5974 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5975 | } |
| 5976 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5977 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5978 | { |
| 5979 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 5980 | } |
| 5981 | |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 5982 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5983 | { |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 5984 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Jiang Biao | ae6a237 | 2016-11-07 08:54:51 +0800 | [diff] [blame] | 5987 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5988 | { |
| 5989 | if (!need_emulate_wbinvd(vcpu)) |
| 5990 | return X86EMUL_CONTINUE; |
| 5991 | |
| 5992 | if (kvm_x86_ops->has_wbinvd_exit()) { |
Jan Kiszka | 2eec734 | 2010-11-01 14:01:29 +0100 | [diff] [blame] | 5993 | int cpu = get_cpu(); |
| 5994 | |
| 5995 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5996 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
| 5997 | wbinvd_ipi, NULL, 1); |
Jan Kiszka | 2eec734 | 2010-11-01 14:01:29 +0100 | [diff] [blame] | 5998 | put_cpu(); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5999 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
Jan Kiszka | 2eec734 | 2010-11-01 14:01:29 +0100 | [diff] [blame] | 6000 | } else |
| 6001 | wbinvd(); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6002 | return X86EMUL_CONTINUE; |
| 6003 | } |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6004 | |
| 6005 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 6006 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6007 | kvm_emulate_wbinvd_noskip(vcpu); |
| 6008 | return kvm_skip_emulated_instruction(vcpu); |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6009 | } |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6010 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
| 6011 | |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6012 | |
| 6013 | |
Avi Kivity | bcaf5cc | 2011-04-20 15:53:23 +0300 | [diff] [blame] | 6014 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
| 6015 | { |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6016 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
Avi Kivity | bcaf5cc | 2011-04-20 15:53:23 +0300 | [diff] [blame] | 6017 | } |
| 6018 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 6019 | static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
| 6020 | unsigned long *dest) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6021 | { |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6022 | return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 6025 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
| 6026 | unsigned long value) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6027 | { |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 6028 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6029 | return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6030 | } |
| 6031 | |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6032 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 6033 | { |
| 6034 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 6035 | } |
| 6036 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6037 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6038 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6039 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6040 | unsigned long value; |
| 6041 | |
| 6042 | switch (cr) { |
| 6043 | case 0: |
| 6044 | value = kvm_read_cr0(vcpu); |
| 6045 | break; |
| 6046 | case 2: |
| 6047 | value = vcpu->arch.cr2; |
| 6048 | break; |
| 6049 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6050 | value = kvm_read_cr3(vcpu); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6051 | break; |
| 6052 | case 4: |
| 6053 | value = kvm_read_cr4(vcpu); |
| 6054 | break; |
| 6055 | case 8: |
| 6056 | value = kvm_get_cr8(vcpu); |
| 6057 | break; |
| 6058 | default: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6059 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6060 | return 0; |
| 6061 | } |
| 6062 | |
| 6063 | return value; |
| 6064 | } |
| 6065 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6066 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6067 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6068 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 6069 | int res = 0; |
| 6070 | |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6071 | switch (cr) { |
| 6072 | case 0: |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 6073 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6074 | break; |
| 6075 | case 2: |
| 6076 | vcpu->arch.cr2 = val; |
| 6077 | break; |
| 6078 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 6079 | res = kvm_set_cr3(vcpu, val); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6080 | break; |
| 6081 | case 4: |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 6082 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6083 | break; |
| 6084 | case 8: |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 6085 | res = kvm_set_cr8(vcpu, val); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6086 | break; |
| 6087 | default: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6088 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 6089 | res = -1; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6090 | } |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 6091 | |
| 6092 | return res; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6093 | } |
| 6094 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6095 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 6096 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6097 | return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt)); |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 6098 | } |
| 6099 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6100 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6101 | { |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6102 | kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt); |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6105 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
Mohammed Gamal | 160ce1f | 2010-08-04 05:44:24 +0300 | [diff] [blame] | 6106 | { |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6107 | kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt); |
Mohammed Gamal | 160ce1f | 2010-08-04 05:44:24 +0300 | [diff] [blame] | 6108 | } |
| 6109 | |
Avi Kivity | 1ac9d0c | 2011-04-20 15:12:00 +0300 | [diff] [blame] | 6110 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6111 | { |
| 6112 | kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt); |
| 6113 | } |
| 6114 | |
| 6115 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6116 | { |
| 6117 | kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt); |
| 6118 | } |
| 6119 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6120 | static unsigned long emulator_get_cached_segment_base( |
| 6121 | struct x86_emulate_ctxt *ctxt, int seg) |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 6122 | { |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6123 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 6124 | } |
| 6125 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 6126 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
| 6127 | struct desc_struct *desc, u32 *base3, |
| 6128 | int seg) |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6129 | { |
| 6130 | struct kvm_segment var; |
| 6131 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6132 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 6133 | *selector = var.selector; |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6134 | |
Gleb Natapov | 378a8b0 | 2013-01-21 15:36:48 +0200 | [diff] [blame] | 6135 | if (var.unusable) { |
| 6136 | memset(desc, 0, sizeof(*desc)); |
Radim Krčmář | f0367ee | 2017-05-18 19:37:30 +0200 | [diff] [blame] | 6137 | if (base3) |
| 6138 | *base3 = 0; |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6139 | return false; |
Gleb Natapov | 378a8b0 | 2013-01-21 15:36:48 +0200 | [diff] [blame] | 6140 | } |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6141 | |
| 6142 | if (var.g) |
| 6143 | var.limit >>= 12; |
| 6144 | set_desc_limit(desc, var.limit); |
| 6145 | set_desc_base(desc, (unsigned long)var.base); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 6146 | #ifdef CONFIG_X86_64 |
| 6147 | if (base3) |
| 6148 | *base3 = var.base >> 32; |
| 6149 | #endif |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6150 | desc->type = var.type; |
| 6151 | desc->s = var.s; |
| 6152 | desc->dpl = var.dpl; |
| 6153 | desc->p = var.present; |
| 6154 | desc->avl = var.avl; |
| 6155 | desc->l = var.l; |
| 6156 | desc->d = var.db; |
| 6157 | desc->g = var.g; |
| 6158 | |
| 6159 | return true; |
| 6160 | } |
| 6161 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 6162 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 6163 | struct desc_struct *desc, u32 base3, |
| 6164 | int seg) |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6165 | { |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6166 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6167 | struct kvm_segment var; |
| 6168 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 6169 | var.selector = selector; |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6170 | var.base = get_desc_base(desc); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 6171 | #ifdef CONFIG_X86_64 |
| 6172 | var.base |= ((u64)base3) << 32; |
| 6173 | #endif |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6174 | var.limit = get_desc_limit(desc); |
| 6175 | if (desc->g) |
| 6176 | var.limit = (var.limit << 12) | 0xfff; |
| 6177 | var.type = desc->type; |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6178 | var.dpl = desc->dpl; |
| 6179 | var.db = desc->d; |
| 6180 | var.s = desc->s; |
| 6181 | var.l = desc->l; |
| 6182 | var.g = desc->g; |
| 6183 | var.avl = desc->avl; |
| 6184 | var.present = desc->p; |
| 6185 | var.unusable = !var.present; |
| 6186 | var.padding = 0; |
| 6187 | |
| 6188 | kvm_set_segment(vcpu, &var, seg); |
| 6189 | return; |
| 6190 | } |
| 6191 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6192 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
| 6193 | u32 msr_index, u64 *pdata) |
| 6194 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 6195 | return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata); |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6196 | } |
| 6197 | |
| 6198 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, |
| 6199 | u32 msr_index, u64 data) |
| 6200 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 6201 | return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data); |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6202 | } |
| 6203 | |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6204 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
| 6205 | { |
| 6206 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6207 | |
| 6208 | return vcpu->arch.smbase; |
| 6209 | } |
| 6210 | |
| 6211 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) |
| 6212 | { |
| 6213 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6214 | |
| 6215 | vcpu->arch.smbase = smbase; |
| 6216 | } |
| 6217 | |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 6218 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
| 6219 | u32 pmc) |
| 6220 | { |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 6221 | return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc); |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 6222 | } |
| 6223 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 6224 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
| 6225 | u32 pmc, u64 *pdata) |
| 6226 | { |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 6227 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 6228 | } |
| 6229 | |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 6230 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
| 6231 | { |
| 6232 | emul_to_vcpu(ctxt)->arch.halt_request = 1; |
| 6233 | } |
| 6234 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6235 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6236 | struct x86_instruction_info *info, |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 6237 | enum x86_intercept_stage stage) |
| 6238 | { |
Sean Christopherson | 21f1b8f | 2020-02-18 15:29:42 -0800 | [diff] [blame] | 6239 | return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage, |
| 6240 | &ctxt->exception); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 6241 | } |
| 6242 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 6243 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 6244 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
| 6245 | bool exact_only) |
Stephan Bärwolf | bdb42f5 | 2012-01-12 16:43:03 +0100 | [diff] [blame] | 6246 | { |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 6247 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
Stephan Bärwolf | bdb42f5 | 2012-01-12 16:43:03 +0100 | [diff] [blame] | 6248 | } |
| 6249 | |
Sean Christopherson | 5ae78e9 | 2019-12-17 13:32:38 -0800 | [diff] [blame] | 6250 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
| 6251 | { |
| 6252 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); |
| 6253 | } |
| 6254 | |
| 6255 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) |
| 6256 | { |
| 6257 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); |
| 6258 | } |
| 6259 | |
| 6260 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) |
| 6261 | { |
| 6262 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); |
| 6263 | } |
| 6264 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 6265 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
| 6266 | { |
| 6267 | return kvm_register_read(emul_to_vcpu(ctxt), reg); |
| 6268 | } |
| 6269 | |
| 6270 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) |
| 6271 | { |
| 6272 | kvm_register_write(emul_to_vcpu(ctxt), reg, val); |
| 6273 | } |
| 6274 | |
Nadav Amit | 801806d | 2015-01-26 09:32:23 +0200 | [diff] [blame] | 6275 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
| 6276 | { |
| 6277 | kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked); |
| 6278 | } |
| 6279 | |
Ladi Prosek | 6ed071f | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 6280 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
| 6281 | { |
| 6282 | return emul_to_vcpu(ctxt)->arch.hflags; |
| 6283 | } |
| 6284 | |
| 6285 | static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) |
| 6286 | { |
Sean Christopherson | c5833c7 | 2019-04-02 08:03:10 -0700 | [diff] [blame] | 6287 | emul_to_vcpu(ctxt)->arch.hflags = emul_flags; |
Ladi Prosek | 6ed071f | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 6288 | } |
| 6289 | |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6290 | static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, |
| 6291 | const char *smstate) |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6292 | { |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6293 | return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6294 | } |
| 6295 | |
Sean Christopherson | c5833c7 | 2019-04-02 08:03:10 -0700 | [diff] [blame] | 6296 | static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) |
| 6297 | { |
| 6298 | kvm_smm_changed(emul_to_vcpu(ctxt)); |
| 6299 | } |
| 6300 | |
Vitaly Kuznetsov | 02d4160 | 2019-08-13 15:53:32 +0200 | [diff] [blame] | 6301 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
| 6302 | { |
| 6303 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); |
| 6304 | } |
| 6305 | |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 6306 | static const struct x86_emulate_ops emulate_ops = { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 6307 | .read_gpr = emulator_read_gpr, |
| 6308 | .write_gpr = emulator_write_gpr, |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 6309 | .read_std = emulator_read_std, |
| 6310 | .write_std = emulator_write_std, |
Radim Krčmář | 7a036a6 | 2015-10-30 16:36:24 +0100 | [diff] [blame] | 6311 | .read_phys = kvm_read_guest_phys_system, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 6312 | .fetch = kvm_fetch_guest_virt, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6313 | .read_emulated = emulator_read_emulated, |
| 6314 | .write_emulated = emulator_write_emulated, |
| 6315 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 6316 | .invlpg = emulator_invlpg, |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6317 | .pio_in_emulated = emulator_pio_in_emulated, |
| 6318 | .pio_out_emulated = emulator_pio_out_emulated, |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 6319 | .get_segment = emulator_get_segment, |
| 6320 | .set_segment = emulator_set_segment, |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 6321 | .get_cached_segment_base = emulator_get_cached_segment_base, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 6322 | .get_gdt = emulator_get_gdt, |
Mohammed Gamal | 160ce1f | 2010-08-04 05:44:24 +0300 | [diff] [blame] | 6323 | .get_idt = emulator_get_idt, |
Avi Kivity | 1ac9d0c | 2011-04-20 15:12:00 +0300 | [diff] [blame] | 6324 | .set_gdt = emulator_set_gdt, |
| 6325 | .set_idt = emulator_set_idt, |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 6326 | .get_cr = emulator_get_cr, |
| 6327 | .set_cr = emulator_set_cr, |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 6328 | .cpl = emulator_get_cpl, |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 6329 | .get_dr = emulator_get_dr, |
| 6330 | .set_dr = emulator_set_dr, |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6331 | .get_smbase = emulator_get_smbase, |
| 6332 | .set_smbase = emulator_set_smbase, |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 6333 | .set_msr = emulator_set_msr, |
| 6334 | .get_msr = emulator_get_msr, |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 6335 | .check_pmc = emulator_check_pmc, |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 6336 | .read_pmc = emulator_read_pmc, |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 6337 | .halt = emulator_halt, |
Avi Kivity | bcaf5cc | 2011-04-20 15:53:23 +0300 | [diff] [blame] | 6338 | .wbinvd = emulator_wbinvd, |
Avi Kivity | d6aa100 | 2011-04-20 15:47:13 +0300 | [diff] [blame] | 6339 | .fix_hypercall = emulator_fix_hypercall, |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 6340 | .intercept = emulator_intercept, |
Stephan Bärwolf | bdb42f5 | 2012-01-12 16:43:03 +0100 | [diff] [blame] | 6341 | .get_cpuid = emulator_get_cpuid, |
Sean Christopherson | 5ae78e9 | 2019-12-17 13:32:38 -0800 | [diff] [blame] | 6342 | .guest_has_long_mode = emulator_guest_has_long_mode, |
| 6343 | .guest_has_movbe = emulator_guest_has_movbe, |
| 6344 | .guest_has_fxsr = emulator_guest_has_fxsr, |
Nadav Amit | 801806d | 2015-01-26 09:32:23 +0200 | [diff] [blame] | 6345 | .set_nmi_mask = emulator_set_nmi_mask, |
Ladi Prosek | 6ed071f | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 6346 | .get_hflags = emulator_get_hflags, |
| 6347 | .set_hflags = emulator_set_hflags, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6348 | .pre_leave_smm = emulator_pre_leave_smm, |
Sean Christopherson | c5833c7 | 2019-04-02 08:03:10 -0700 | [diff] [blame] | 6349 | .post_leave_smm = emulator_post_leave_smm, |
Vitaly Kuznetsov | 02d4160 | 2019-08-13 15:53:32 +0200 | [diff] [blame] | 6350 | .set_xcr = emulator_set_xcr, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6351 | }; |
| 6352 | |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 6353 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
| 6354 | { |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 6355 | u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu); |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 6356 | /* |
| 6357 | * an sti; sti; sequence only disable interrupts for the first |
| 6358 | * instruction. So, if the last instruction, be it emulated or |
| 6359 | * not, left the system with the INT_STI flag enabled, it |
| 6360 | * means that the last instruction is an sti. We should not |
| 6361 | * leave the flag on in this case. The same goes for mov ss |
| 6362 | */ |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 6363 | if (int_shadow & mask) |
| 6364 | mask = 0; |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 6365 | if (unlikely(int_shadow || mask)) { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 6366 | kvm_x86_ops->set_interrupt_shadow(vcpu, mask); |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 6367 | if (!mask) |
| 6368 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6369 | } |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 6370 | } |
| 6371 | |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 6372 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 6373 | { |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 6374 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 6375 | if (ctxt->exception.vector == PF_VECTOR) |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 6376 | return kvm_propagate_fault(vcpu, &ctxt->exception); |
| 6377 | |
| 6378 | if (ctxt->exception.error_code_valid) |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 6379 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
| 6380 | ctxt->exception.error_code); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 6381 | else |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 6382 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 6383 | return false; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 6384 | } |
| 6385 | |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 6386 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
| 6387 | { |
| 6388 | struct x86_emulate_ctxt *ctxt; |
| 6389 | |
| 6390 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); |
| 6391 | if (!ctxt) { |
| 6392 | pr_err("kvm: failed to allocate vcpu's emulator\n"); |
| 6393 | return NULL; |
| 6394 | } |
| 6395 | |
| 6396 | ctxt->vcpu = vcpu; |
| 6397 | ctxt->ops = &emulate_ops; |
| 6398 | vcpu->arch.emulate_ctxt = ctxt; |
| 6399 | |
| 6400 | return ctxt; |
| 6401 | } |
| 6402 | |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 6403 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
| 6404 | { |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 6405 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 6406 | int cs_db, cs_l; |
| 6407 | |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 6408 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 6409 | |
Sean Christopherson | 744e699 | 2020-02-18 15:03:09 -0800 | [diff] [blame] | 6410 | ctxt->gpa_available = false; |
Takuya Yoshikawa | adf5223 | 2011-05-25 11:06:16 +0900 | [diff] [blame] | 6411 | ctxt->eflags = kvm_get_rflags(vcpu); |
Paolo Bonzini | c8401dd | 2017-06-07 15:13:14 +0200 | [diff] [blame] | 6412 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
| 6413 | |
Takuya Yoshikawa | adf5223 | 2011-05-25 11:06:16 +0900 | [diff] [blame] | 6414 | ctxt->eip = kvm_rip_read(vcpu); |
| 6415 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
| 6416 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : |
Nadav Amit | 42bf549 | 2014-04-18 07:11:34 +0300 | [diff] [blame] | 6417 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
Takuya Yoshikawa | adf5223 | 2011-05-25 11:06:16 +0900 | [diff] [blame] | 6418 | cs_db ? X86EMUL_MODE_PROT32 : |
| 6419 | X86EMUL_MODE_PROT16; |
Paolo Bonzini | a584539 | 2015-04-01 18:18:53 +0200 | [diff] [blame] | 6420 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6421 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
| 6422 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); |
Takuya Yoshikawa | adf5223 | 2011-05-25 11:06:16 +0900 | [diff] [blame] | 6423 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 6424 | init_decode_cache(ctxt); |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6425 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 6426 | } |
| 6427 | |
Sean Christopherson | 9497e1f | 2019-08-27 14:40:36 -0700 | [diff] [blame] | 6428 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
Mohammed Gamal | 6399565 | 2010-09-19 14:34:06 +0200 | [diff] [blame] | 6429 | { |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 6430 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Mohammed Gamal | 6399565 | 2010-09-19 14:34:06 +0200 | [diff] [blame] | 6431 | int ret; |
| 6432 | |
| 6433 | init_emulate_ctxt(vcpu); |
| 6434 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 6435 | ctxt->op_bytes = 2; |
| 6436 | ctxt->ad_bytes = 2; |
| 6437 | ctxt->_eip = ctxt->eip + inc_eip; |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6438 | ret = emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6399565 | 2010-09-19 14:34:06 +0200 | [diff] [blame] | 6439 | |
Sean Christopherson | 9497e1f | 2019-08-27 14:40:36 -0700 | [diff] [blame] | 6440 | if (ret != X86EMUL_CONTINUE) { |
| 6441 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 6442 | } else { |
| 6443 | ctxt->eip = ctxt->_eip; |
| 6444 | kvm_rip_write(vcpu, ctxt->eip); |
| 6445 | kvm_set_rflags(vcpu, ctxt->eflags); |
| 6446 | } |
Mohammed Gamal | 6399565 | 2010-09-19 14:34:06 +0200 | [diff] [blame] | 6447 | } |
| 6448 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); |
| 6449 | |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6450 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6451 | { |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6452 | ++vcpu->stat.insn_emulation_fail; |
| 6453 | trace_kvm_emulate_insn_failed(vcpu); |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6454 | |
Sean Christopherson | 42cbf06 | 2019-08-27 14:40:31 -0700 | [diff] [blame] | 6455 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
| 6456 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6457 | return 1; |
Sean Christopherson | 42cbf06 | 2019-08-27 14:40:31 -0700 | [diff] [blame] | 6458 | } |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6459 | |
Sean Christopherson | 738fece | 2019-08-27 14:40:34 -0700 | [diff] [blame] | 6460 | if (emulation_type & EMULTYPE_SKIP) { |
| 6461 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6462 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6463 | vcpu->run->internal.ndata = 0; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6464 | return 0; |
Sean Christopherson | 738fece | 2019-08-27 14:40:34 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
Sean Christopherson | 22da61c | 2019-08-27 14:40:28 -0700 | [diff] [blame] | 6467 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6468 | |
Nadav Amit | a2b9e6c | 2014-09-17 02:50:50 +0300 | [diff] [blame] | 6469 | if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) { |
Joerg Roedel | fc3a915 | 2010-11-29 17:51:49 +0100 | [diff] [blame] | 6470 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6471 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6472 | vcpu->run->internal.ndata = 0; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6473 | return 0; |
Joerg Roedel | fc3a915 | 2010-11-29 17:51:49 +0100 | [diff] [blame] | 6474 | } |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6475 | |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6476 | return 1; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6477 | } |
| 6478 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6479 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 6480 | bool write_fault_to_shadow_pgtable, |
| 6481 | int emulation_type) |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6482 | { |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6483 | gpa_t gpa = cr2_or_gpa; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 6484 | kvm_pfn_t pfn; |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6485 | |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6486 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 6487 | return false; |
| 6488 | |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6489 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
| 6490 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) |
Sean Christopherson | 6c3dfeb | 2018-08-23 13:56:51 -0700 | [diff] [blame] | 6491 | return false; |
| 6492 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 6493 | if (!vcpu->arch.mmu->direct_map) { |
Xiao Guangrong | 95b3cf6 | 2013-01-13 23:46:52 +0800 | [diff] [blame] | 6494 | /* |
| 6495 | * Write permission should be allowed since only |
| 6496 | * write access need to be emulated. |
| 6497 | */ |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6498 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
Gleb Natapov | 68be080 | 2010-07-14 19:05:45 +0300 | [diff] [blame] | 6499 | |
Xiao Guangrong | 95b3cf6 | 2013-01-13 23:46:52 +0800 | [diff] [blame] | 6500 | /* |
| 6501 | * If the mapping is invalid in guest, let cpu retry |
| 6502 | * it to generate fault. |
| 6503 | */ |
| 6504 | if (gpa == UNMAPPED_GVA) |
| 6505 | return true; |
| 6506 | } |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6507 | |
Xiao Guangrong | 8e3d9d0 | 2012-08-21 10:57:42 +0800 | [diff] [blame] | 6508 | /* |
| 6509 | * Do not retry the unhandleable instruction if it faults on the |
| 6510 | * readonly host memory, otherwise it will goto a infinite loop: |
| 6511 | * retry instruction -> write #PF -> emulation fail -> retry |
| 6512 | * instruction -> ... |
| 6513 | */ |
| 6514 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); |
Xiao Guangrong | 95b3cf6 | 2013-01-13 23:46:52 +0800 | [diff] [blame] | 6515 | |
| 6516 | /* |
| 6517 | * If the instruction failed on the error pfn, it can not be fixed, |
| 6518 | * report the error to userspace. |
| 6519 | */ |
| 6520 | if (is_error_noslot_pfn(pfn)) |
| 6521 | return false; |
| 6522 | |
| 6523 | kvm_release_pfn_clean(pfn); |
| 6524 | |
| 6525 | /* The instructions are well-emulated on direct mmu. */ |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 6526 | if (vcpu->arch.mmu->direct_map) { |
Xiao Guangrong | 95b3cf6 | 2013-01-13 23:46:52 +0800 | [diff] [blame] | 6527 | unsigned int indirect_shadow_pages; |
| 6528 | |
| 6529 | spin_lock(&vcpu->kvm->mmu_lock); |
| 6530 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; |
| 6531 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 6532 | |
| 6533 | if (indirect_shadow_pages) |
| 6534 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
| 6535 | |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6536 | return true; |
Xiao Guangrong | 8e3d9d0 | 2012-08-21 10:57:42 +0800 | [diff] [blame] | 6537 | } |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6538 | |
Xiao Guangrong | 95b3cf6 | 2013-01-13 23:46:52 +0800 | [diff] [blame] | 6539 | /* |
| 6540 | * if emulation was due to access to shadowed page table |
| 6541 | * and it failed try to unshadow page and re-enter the |
| 6542 | * guest to let CPU execute the instruction. |
| 6543 | */ |
| 6544 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
Xiao Guangrong | 93c05d3 | 2013-01-13 23:49:07 +0800 | [diff] [blame] | 6545 | |
| 6546 | /* |
| 6547 | * If the access faults on its page table, it can not |
| 6548 | * be fixed by unprotecting shadow page and it should |
| 6549 | * be reported to userspace. |
| 6550 | */ |
| 6551 | return !write_fault_to_shadow_pgtable; |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 6552 | } |
| 6553 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6554 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6555 | gpa_t cr2_or_gpa, int emulation_type) |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6556 | { |
| 6557 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6558 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6559 | |
| 6560 | last_retry_eip = vcpu->arch.last_retry_eip; |
| 6561 | last_retry_addr = vcpu->arch.last_retry_addr; |
| 6562 | |
| 6563 | /* |
| 6564 | * If the emulation is caused by #PF and it is non-page_table |
| 6565 | * writing instruction, it means the VM-EXIT is caused by shadow |
| 6566 | * page protected, we can zap the shadow page and retry this |
| 6567 | * instruction directly. |
| 6568 | * |
| 6569 | * Note: if the guest uses a non-page-table modifying instruction |
| 6570 | * on the PDE that points to the instruction, then we will unmap |
| 6571 | * the instruction and go to an infinite loop. So, we cache the |
| 6572 | * last retried eip and the last fault address, if we meet the eip |
| 6573 | * and the address again, we can break out of the potential infinite |
| 6574 | * loop. |
| 6575 | */ |
| 6576 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; |
| 6577 | |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6578 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6579 | return false; |
| 6580 | |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6581 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
| 6582 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) |
Sean Christopherson | 6c3dfeb | 2018-08-23 13:56:51 -0700 | [diff] [blame] | 6583 | return false; |
| 6584 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6585 | if (x86_page_table_writing_insn(ctxt)) |
| 6586 | return false; |
| 6587 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6588 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6589 | return false; |
| 6590 | |
| 6591 | vcpu->arch.last_retry_eip = ctxt->eip; |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6592 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6593 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 6594 | if (!vcpu->arch.mmu->direct_map) |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6595 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6596 | |
Xiao Guangrong | 2236802 | 2013-01-13 23:44:12 +0800 | [diff] [blame] | 6597 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6598 | |
| 6599 | return true; |
| 6600 | } |
| 6601 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 6602 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
| 6603 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); |
| 6604 | |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6605 | static void kvm_smm_changed(struct kvm_vcpu *vcpu) |
Paolo Bonzini | a584539 | 2015-04-01 18:18:53 +0200 | [diff] [blame] | 6606 | { |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6607 | if (!(vcpu->arch.hflags & HF_SMM_MASK)) { |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 6608 | /* This is a good place to trace that we are exiting SMM. */ |
| 6609 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false); |
| 6610 | |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 6611 | /* Process a latched INIT or SMI, if any. */ |
| 6612 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6613 | } |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 6614 | |
| 6615 | kvm_mmu_reset_context(vcpu); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 6616 | } |
| 6617 | |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6618 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
| 6619 | unsigned long *db) |
| 6620 | { |
| 6621 | u32 dr6 = 0; |
| 6622 | int i; |
| 6623 | u32 enable, rwlen; |
| 6624 | |
| 6625 | enable = dr7; |
| 6626 | rwlen = dr7 >> 16; |
| 6627 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) |
| 6628 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) |
| 6629 | dr6 |= (1 << i); |
| 6630 | return dr6; |
| 6631 | } |
| 6632 | |
Sean Christopherson | 120c2c4 | 2019-08-27 14:40:29 -0700 | [diff] [blame] | 6633 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 663f4c6 | 2013-06-25 18:32:07 +0200 | [diff] [blame] | 6634 | { |
| 6635 | struct kvm_run *kvm_run = vcpu->run; |
| 6636 | |
Paolo Bonzini | c8401dd | 2017-06-07 15:13:14 +0200 | [diff] [blame] | 6637 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
| 6638 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM; |
| 6639 | kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip; |
| 6640 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 6641 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6642 | return 0; |
Paolo Bonzini | 663f4c6 | 2013-06-25 18:32:07 +0200 | [diff] [blame] | 6643 | } |
Sean Christopherson | 120c2c4 | 2019-08-27 14:40:29 -0700 | [diff] [blame] | 6644 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6645 | return 1; |
Paolo Bonzini | 663f4c6 | 2013-06-25 18:32:07 +0200 | [diff] [blame] | 6646 | } |
| 6647 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6648 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 6649 | { |
| 6650 | unsigned long rflags = kvm_x86_ops->get_rflags(vcpu); |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 6651 | int r; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6652 | |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 6653 | r = kvm_x86_ops->skip_emulated_instruction(vcpu); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6654 | if (unlikely(!r)) |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 6655 | return 0; |
Paolo Bonzini | c8401dd | 2017-06-07 15:13:14 +0200 | [diff] [blame] | 6656 | |
| 6657 | /* |
| 6658 | * rflags is the old, "raw" value of the flags. The new value has |
| 6659 | * not been saved yet. |
| 6660 | * |
| 6661 | * This is correct even for TF set by the guest, because "the |
| 6662 | * processor will not generate this exception after the instruction |
| 6663 | * that sets the TF flag". |
| 6664 | */ |
| 6665 | if (unlikely(rflags & X86_EFLAGS_TF)) |
Sean Christopherson | 120c2c4 | 2019-08-27 14:40:29 -0700 | [diff] [blame] | 6666 | r = kvm_vcpu_do_singlestep(vcpu); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6667 | return r; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6668 | } |
| 6669 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); |
| 6670 | |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6671 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
| 6672 | { |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6673 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
| 6674 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6675 | struct kvm_run *kvm_run = vcpu->run; |
| 6676 | unsigned long eip = kvm_get_linear_rip(vcpu); |
| 6677 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6678 | vcpu->arch.guest_debug_dr7, |
| 6679 | vcpu->arch.eff_db); |
| 6680 | |
| 6681 | if (dr6 != 0) { |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6682 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6683 | kvm_run->debug.arch.pc = eip; |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6684 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 6685 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6686 | *r = 0; |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6687 | return true; |
| 6688 | } |
| 6689 | } |
| 6690 | |
Nadav Amit | 4161a56 | 2014-07-17 01:19:31 +0300 | [diff] [blame] | 6691 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
| 6692 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6693 | unsigned long eip = kvm_get_linear_rip(vcpu); |
| 6694 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6695 | vcpu->arch.dr7, |
| 6696 | vcpu->arch.db); |
| 6697 | |
| 6698 | if (dr6 != 0) { |
Liran Alon | 1fc5d194 | 2019-06-06 01:54:47 +0300 | [diff] [blame] | 6699 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6700 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6701 | kvm_queue_exception(vcpu, DB_VECTOR); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6702 | *r = 1; |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6703 | return true; |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | return false; |
| 6708 | } |
| 6709 | |
Liran Alon | 04789b6 | 2018-03-12 13:12:50 +0200 | [diff] [blame] | 6710 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
| 6711 | { |
Arbel Moshe | 2d7921c | 2018-03-12 13:12:53 +0200 | [diff] [blame] | 6712 | switch (ctxt->opcode_len) { |
| 6713 | case 1: |
| 6714 | switch (ctxt->b) { |
| 6715 | case 0xe4: /* IN */ |
| 6716 | case 0xe5: |
| 6717 | case 0xec: |
| 6718 | case 0xed: |
| 6719 | case 0xe6: /* OUT */ |
| 6720 | case 0xe7: |
| 6721 | case 0xee: |
| 6722 | case 0xef: |
| 6723 | case 0x6c: /* INS */ |
| 6724 | case 0x6d: |
| 6725 | case 0x6e: /* OUTS */ |
| 6726 | case 0x6f: |
| 6727 | return true; |
| 6728 | } |
| 6729 | break; |
| 6730 | case 2: |
| 6731 | switch (ctxt->b) { |
| 6732 | case 0x33: /* RDPMC */ |
| 6733 | return true; |
| 6734 | } |
| 6735 | break; |
Liran Alon | 04789b6 | 2018-03-12 13:12:50 +0200 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | return false; |
| 6739 | } |
| 6740 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6741 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
| 6742 | int emulation_type, void *insn, int insn_len) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6743 | { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 6744 | int r; |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 6745 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6746 | bool writeback = true; |
Xiao Guangrong | 93c05d3 | 2013-01-13 23:49:07 +0800 | [diff] [blame] | 6747 | bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6748 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 6749 | vcpu->arch.l1tf_flush_l1d = true; |
| 6750 | |
Xiao Guangrong | 93c05d3 | 2013-01-13 23:49:07 +0800 | [diff] [blame] | 6751 | /* |
| 6752 | * Clear write_fault_to_shadow_pgtable here to ensure it is |
| 6753 | * never reused. |
| 6754 | */ |
| 6755 | vcpu->arch.write_fault_to_shadow_pgtable = false; |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 6756 | kvm_clear_exception_queue(vcpu); |
Gleb Natapov | 8d7d8102 | 2011-04-12 12:36:21 +0300 | [diff] [blame] | 6757 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 6758 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 6759 | init_emulate_ctxt(vcpu); |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6760 | |
| 6761 | /* |
| 6762 | * We will reenter on the same instruction since |
| 6763 | * we do not set complete_userspace_io. This does not |
| 6764 | * handle watchpoints yet, those would be handled in |
| 6765 | * the emulate_ops. |
| 6766 | */ |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 6767 | if (!(emulation_type & EMULTYPE_SKIP) && |
| 6768 | kvm_vcpu_check_breakpoint(vcpu, &r)) |
Paolo Bonzini | 4a1e10d | 2013-05-30 11:48:30 +0200 | [diff] [blame] | 6769 | return r; |
| 6770 | |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6771 | ctxt->interruptibility = 0; |
| 6772 | ctxt->have_exception = false; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 6773 | ctxt->exception.vector = -1; |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6774 | ctxt->perm_ok = false; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6775 | |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 6776 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; |
Avi Kivity | 4005996 | 2011-02-01 16:32:04 +0200 | [diff] [blame] | 6777 | |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6778 | r = x86_decode_insn(ctxt, insn, insn_len); |
Joerg Roedel | d47f00a | 2010-09-10 17:30:56 +0200 | [diff] [blame] | 6779 | |
Avi Kivity | e46479f | 2010-04-11 13:05:16 +0300 | [diff] [blame] | 6780 | trace_kvm_emulate_insn_start(vcpu); |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 6781 | ++vcpu->stat.insn_emulation; |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 6782 | if (r != EMULATION_OK) { |
Sean Christopherson | b400060 | 2019-08-27 14:40:32 -0700 | [diff] [blame] | 6783 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
Sean Christopherson | c83fad65e | 2019-08-27 14:40:33 -0700 | [diff] [blame] | 6784 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
| 6785 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6786 | return 1; |
Sean Christopherson | c83fad65e | 2019-08-27 14:40:33 -0700 | [diff] [blame] | 6787 | } |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6788 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
| 6789 | write_fault_to_spt, |
| 6790 | emulation_type)) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6791 | return 1; |
Jan Dakinevich | 8530a79 | 2019-08-27 13:07:09 +0000 | [diff] [blame] | 6792 | if (ctxt->have_exception) { |
Jan Dakinevich | c8848ce | 2019-08-27 13:07:08 +0000 | [diff] [blame] | 6793 | /* |
| 6794 | * #UD should result in just EMULATION_FAILED, and trap-like |
| 6795 | * exception should not be encountered during decode. |
| 6796 | */ |
| 6797 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || |
| 6798 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); |
Jan Dakinevich | 8530a79 | 2019-08-27 13:07:09 +0000 | [diff] [blame] | 6799 | inject_emulated_exception(vcpu); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6800 | return 1; |
Jan Dakinevich | 8530a79 | 2019-08-27 13:07:09 +0000 | [diff] [blame] | 6801 | } |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6802 | return handle_emulation_failure(vcpu, emulation_type); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6803 | } |
| 6804 | } |
| 6805 | |
Sean Christopherson | 42cbf06 | 2019-08-27 14:40:31 -0700 | [diff] [blame] | 6806 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
| 6807 | !is_vmware_backdoor_opcode(ctxt)) { |
| 6808 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6809 | return 1; |
Sean Christopherson | 42cbf06 | 2019-08-27 14:40:31 -0700 | [diff] [blame] | 6810 | } |
Liran Alon | 04789b6 | 2018-03-12 13:12:50 +0200 | [diff] [blame] | 6811 | |
Sean Christopherson | 1957aa6 | 2019-08-27 14:40:39 -0700 | [diff] [blame] | 6812 | /* |
| 6813 | * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks |
| 6814 | * for kvm_skip_emulated_instruction(). The caller is responsible for |
| 6815 | * updating interruptibility state and injecting single-step #DBs. |
| 6816 | */ |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 6817 | if (emulation_type & EMULTYPE_SKIP) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 6818 | kvm_rip_write(vcpu, ctxt->_eip); |
Nadav Amit | bb663c7 | 2014-07-21 14:37:26 +0300 | [diff] [blame] | 6819 | if (ctxt->eflags & X86_EFLAGS_RF) |
| 6820 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6821 | return 1; |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 6822 | } |
| 6823 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6824 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6825 | return 1; |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 6826 | |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6827 | /* this is needed for vmware backdoor interface to work since it |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 6828 | changes registers values during IO operation */ |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6829 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
| 6830 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 6831 | emulator_invalidate_register_cache(ctxt); |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6832 | } |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 6833 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 6834 | restart: |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6835 | if (emulation_type & EMULTYPE_PF) { |
| 6836 | /* Save the faulting GPA (cr2) in the address field */ |
| 6837 | ctxt->exception.address = cr2_or_gpa; |
| 6838 | |
| 6839 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ |
| 6840 | if (vcpu->arch.mmu->direct_map) { |
Sean Christopherson | 744e699 | 2020-02-18 15:03:09 -0800 | [diff] [blame] | 6841 | ctxt->gpa_available = true; |
| 6842 | ctxt->gpa_val = cr2_or_gpa; |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 6843 | } |
| 6844 | } else { |
| 6845 | /* Sanitize the address out of an abundance of paranoia. */ |
| 6846 | ctxt->exception.address = 0; |
| 6847 | } |
Tom Lendacky | 0f89b20 | 2016-12-14 14:59:23 -0500 | [diff] [blame] | 6848 | |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6849 | r = x86_emulate_insn(ctxt); |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 6850 | |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 6851 | if (r == EMULATION_INTERCEPTED) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6852 | return 1; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 6853 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 6854 | if (r == EMULATION_FAILED) { |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 6855 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 6856 | emulation_type)) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6857 | return 1; |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 6858 | |
Liran Alon | e236617 | 2018-03-12 13:12:49 +0200 | [diff] [blame] | 6859 | return handle_emulation_failure(vcpu, emulation_type); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6860 | } |
| 6861 | |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6862 | if (ctxt->have_exception) { |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6863 | r = 1; |
Paolo Bonzini | ef54bcf | 2014-09-04 19:46:15 +0200 | [diff] [blame] | 6864 | if (inject_emulated_exception(vcpu)) |
| 6865 | return r; |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 6866 | } else if (vcpu->arch.pio.count) { |
Paolo Bonzini | 0912c97 | 2013-08-27 15:41:43 +0200 | [diff] [blame] | 6867 | if (!vcpu->arch.pio.in) { |
| 6868 | /* FIXME: return into emulator if single-stepping. */ |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 6869 | vcpu->arch.pio.count = 0; |
Paolo Bonzini | 0912c97 | 2013-08-27 15:41:43 +0200 | [diff] [blame] | 6870 | } else { |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6871 | writeback = false; |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 6872 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
| 6873 | } |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6874 | r = 0; |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6875 | } else if (vcpu->mmio_needed) { |
Sean Christopherson | bc8a0aa | 2019-08-27 14:40:27 -0700 | [diff] [blame] | 6876 | ++vcpu->stat.mmio_exits; |
| 6877 | |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6878 | if (!vcpu->mmio_is_write) |
| 6879 | writeback = false; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6880 | r = 0; |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 6881 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6882 | } else if (r == EMULATION_RESTART) |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 6883 | goto restart; |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 6884 | else |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6885 | r = 1; |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 6886 | |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6887 | if (writeback) { |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 6888 | unsigned long rflags = kvm_x86_ops->get_rflags(vcpu); |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 6889 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6890 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
Nadav Amit | 38827db | 2014-11-02 11:54:53 +0200 | [diff] [blame] | 6891 | if (!ctxt->have_exception || |
Sean Christopherson | 75ee23b | 2019-08-23 13:55:44 -0700 | [diff] [blame] | 6892 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
| 6893 | kvm_rip_write(vcpu, ctxt->eip); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 6894 | if (r && ctxt->tf) |
Sean Christopherson | 120c2c4 | 2019-08-27 14:40:29 -0700 | [diff] [blame] | 6895 | r = kvm_vcpu_do_singlestep(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6896 | if (kvm_x86_ops->update_emulated_instruction) |
| 6897 | kvm_x86_ops->update_emulated_instruction(vcpu); |
Nadav Amit | 38827db | 2014-11-02 11:54:53 +0200 | [diff] [blame] | 6898 | __kvm_set_rflags(vcpu, ctxt->eflags); |
Sean Christopherson | 75ee23b | 2019-08-23 13:55:44 -0700 | [diff] [blame] | 6899 | } |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 6900 | |
| 6901 | /* |
| 6902 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will |
| 6903 | * do nothing, and it will be requested again as soon as |
| 6904 | * the shadow expires. But we still need to check here, |
| 6905 | * because POPF has no interrupt shadow. |
| 6906 | */ |
| 6907 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) |
| 6908 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 6909 | } else |
| 6910 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; |
Gleb Natapov | 3457e41 | 2010-04-28 19:15:38 +0300 | [diff] [blame] | 6911 | |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 6912 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6913 | } |
Sean Christopherson | c60658d | 2018-08-23 13:56:53 -0700 | [diff] [blame] | 6914 | |
| 6915 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) |
| 6916 | { |
| 6917 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); |
| 6918 | } |
| 6919 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); |
| 6920 | |
| 6921 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, |
| 6922 | void *insn, int insn_len) |
| 6923 | { |
| 6924 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); |
| 6925 | } |
| 6926 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 6927 | |
Sean Christopherson | 8764ed5 | 2019-04-29 07:04:15 -0700 | [diff] [blame] | 6928 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
| 6929 | { |
| 6930 | vcpu->arch.pio.count = 0; |
| 6931 | return 1; |
| 6932 | } |
| 6933 | |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 6934 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
| 6935 | { |
| 6936 | vcpu->arch.pio.count = 0; |
| 6937 | |
| 6938 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) |
| 6939 | return 1; |
| 6940 | |
| 6941 | return kvm_skip_emulated_instruction(vcpu); |
| 6942 | } |
| 6943 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6944 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
| 6945 | unsigned short port) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 6946 | { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 6947 | unsigned long val = kvm_rax_read(vcpu); |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 6948 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
| 6949 | |
Sean Christopherson | 8764ed5 | 2019-04-29 07:04:15 -0700 | [diff] [blame] | 6950 | if (ret) |
| 6951 | return ret; |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 6952 | |
Sean Christopherson | 8764ed5 | 2019-04-29 07:04:15 -0700 | [diff] [blame] | 6953 | /* |
| 6954 | * Workaround userspace that relies on old KVM behavior of %rip being |
| 6955 | * incremented prior to exiting to userspace to handle "OUT 0x7e". |
| 6956 | */ |
| 6957 | if (port == 0x7e && |
| 6958 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { |
| 6959 | vcpu->arch.complete_userspace_io = |
| 6960 | complete_fast_pio_out_port_0x7e; |
| 6961 | kvm_skip_emulated_instruction(vcpu); |
| 6962 | } else { |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 6963 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
| 6964 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; |
| 6965 | } |
Sean Christopherson | 8764ed5 | 2019-04-29 07:04:15 -0700 | [diff] [blame] | 6966 | return 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 6967 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 6968 | |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6969 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
| 6970 | { |
| 6971 | unsigned long val; |
| 6972 | |
| 6973 | /* We should only ever be called with arch.pio.count equal to 1 */ |
| 6974 | BUG_ON(vcpu->arch.pio.count != 1); |
| 6975 | |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 6976 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
| 6977 | vcpu->arch.pio.count = 0; |
| 6978 | return 1; |
| 6979 | } |
| 6980 | |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6981 | /* For size less than 4 we merge, else we zero extend */ |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 6982 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6983 | |
| 6984 | /* |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 6985 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6986 | * the copy and tracing |
| 6987 | */ |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 6988 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 6989 | kvm_rax_write(vcpu, val); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6990 | |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 6991 | return kvm_skip_emulated_instruction(vcpu); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6992 | } |
| 6993 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6994 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
| 6995 | unsigned short port) |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 6996 | { |
| 6997 | unsigned long val; |
| 6998 | int ret; |
| 6999 | |
| 7000 | /* For size less than 4 we merge, else we zero extend */ |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 7001 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 7002 | |
Sean Christopherson | 2e3bb4d | 2020-02-18 15:29:41 -0800 | [diff] [blame] | 7003 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 7004 | if (ret) { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 7005 | kvm_rax_write(vcpu, val); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 7006 | return ret; |
| 7007 | } |
| 7008 | |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 7009 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 7010 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
| 7011 | |
| 7012 | return 0; |
| 7013 | } |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7014 | |
| 7015 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) |
| 7016 | { |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 7017 | int ret; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7018 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7019 | if (in) |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 7020 | ret = kvm_fast_pio_in(vcpu, size, port); |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7021 | else |
Sean Christopherson | 45def77 | 2019-03-11 20:01:05 -0700 | [diff] [blame] | 7022 | ret = kvm_fast_pio_out(vcpu, size, port); |
| 7023 | return ret && kvm_skip_emulated_instruction(vcpu); |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7024 | } |
| 7025 | EXPORT_SYMBOL_GPL(kvm_fast_pio); |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 7026 | |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7027 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7028 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 7029 | __this_cpu_write(cpu_tsc_khz, 0); |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7030 | return 0; |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | static void tsc_khz_changed(void *data) |
| 7034 | { |
| 7035 | struct cpufreq_freqs *freq = data; |
| 7036 | unsigned long khz = 0; |
| 7037 | |
| 7038 | if (data) |
| 7039 | khz = freq->new; |
| 7040 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 7041 | khz = cpufreq_quick_get(raw_smp_processor_id()); |
| 7042 | if (!khz) |
| 7043 | khz = tsc_khz; |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 7044 | __this_cpu_write(cpu_tsc_khz, khz); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7045 | } |
| 7046 | |
Thomas Gleixner | 5fa4ec9 | 2018-01-31 09:41:40 +0100 | [diff] [blame] | 7047 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7048 | static void kvm_hyperv_tsc_notifier(void) |
| 7049 | { |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7050 | struct kvm *kvm; |
| 7051 | struct kvm_vcpu *vcpu; |
| 7052 | int cpu; |
| 7053 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7054 | mutex_lock(&kvm_lock); |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7055 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 7056 | kvm_make_mclock_inprogress_request(kvm); |
| 7057 | |
| 7058 | hyperv_stop_tsc_emulation(); |
| 7059 | |
| 7060 | /* TSC frequency always matches when on Hyper-V */ |
| 7061 | for_each_present_cpu(cpu) |
| 7062 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; |
| 7063 | kvm_max_guest_tsc_khz = tsc_khz; |
| 7064 | |
| 7065 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 7066 | struct kvm_arch *ka = &kvm->arch; |
| 7067 | |
| 7068 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 7069 | |
| 7070 | pvclock_update_vm_gtod_copy(kvm); |
| 7071 | |
| 7072 | kvm_for_each_vcpu(cpu, vcpu, kvm) |
| 7073 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 7074 | |
| 7075 | kvm_for_each_vcpu(cpu, vcpu, kvm) |
| 7076 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
| 7077 | |
| 7078 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 7079 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7080 | mutex_unlock(&kvm_lock); |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7081 | } |
Thomas Gleixner | 5fa4ec9 | 2018-01-31 09:41:40 +0100 | [diff] [blame] | 7082 | #endif |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7083 | |
Viresh Kumar | df24014 | 2019-04-29 15:03:58 +0530 | [diff] [blame] | 7084 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7085 | { |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7086 | struct kvm *kvm; |
| 7087 | struct kvm_vcpu *vcpu; |
| 7088 | int i, send_ipi = 0; |
| 7089 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7090 | /* |
| 7091 | * We allow guests to temporarily run on slowing clocks, |
| 7092 | * provided we notify them after, or to run on accelerating |
| 7093 | * clocks, provided we notify them before. Thus time never |
| 7094 | * goes backwards. |
| 7095 | * |
| 7096 | * However, we have a problem. We can't atomically update |
| 7097 | * the frequency of a given CPU from this function; it is |
| 7098 | * merely a notifier, which can be called from any CPU. |
| 7099 | * Changing the TSC frequency at arbitrary points in time |
| 7100 | * requires a recomputation of local variables related to |
| 7101 | * the TSC for each VCPU. We must flag these local variables |
| 7102 | * to be updated and be sure the update takes place with the |
| 7103 | * new frequency before any guests proceed. |
| 7104 | * |
| 7105 | * Unfortunately, the combination of hotplug CPU and frequency |
| 7106 | * change creates an intractable locking scenario; the order |
| 7107 | * of when these callouts happen is undefined with respect to |
| 7108 | * CPU hotplug, and they can race with each other. As such, |
| 7109 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is |
| 7110 | * undefined; you can actually have a CPU frequency change take |
| 7111 | * place in between the computation of X and the setting of the |
| 7112 | * variable. To protect against this problem, all updates of |
| 7113 | * the per_cpu tsc_khz variable are done in an interrupt |
| 7114 | * protected IPI, and all callers wishing to update the value |
| 7115 | * must wait for a synchronous IPI to complete (which is trivial |
| 7116 | * if the caller is on the CPU already). This establishes the |
| 7117 | * necessary total order on variable updates. |
| 7118 | * |
| 7119 | * Note that because a guest time update may take place |
| 7120 | * anytime after the setting of the VCPU's request bit, the |
| 7121 | * correct TSC value must be set before the request. However, |
| 7122 | * to ensure the update actually makes it to any guest which |
| 7123 | * starts running in hardware virtualization between the set |
| 7124 | * and the acquisition of the spinlock, we must also ping the |
| 7125 | * CPU after setting the request bit. |
| 7126 | * |
| 7127 | */ |
| 7128 | |
Viresh Kumar | df24014 | 2019-04-29 15:03:58 +0530 | [diff] [blame] | 7129 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7130 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7131 | mutex_lock(&kvm_lock); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7132 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 7133 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Viresh Kumar | df24014 | 2019-04-29 15:03:58 +0530 | [diff] [blame] | 7134 | if (vcpu->cpu != cpu) |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7135 | continue; |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7136 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7137 | if (vcpu->cpu != raw_smp_processor_id()) |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7138 | send_ipi = 1; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7139 | } |
| 7140 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7141 | mutex_unlock(&kvm_lock); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7142 | |
| 7143 | if (freq->old < freq->new && send_ipi) { |
| 7144 | /* |
| 7145 | * We upscale the frequency. Must make the guest |
| 7146 | * doesn't see old kvmclock values while running with |
| 7147 | * the new frequency, otherwise we risk the guest sees |
| 7148 | * time go backwards. |
| 7149 | * |
| 7150 | * In case we update the frequency for another cpu |
| 7151 | * (which might be in guest context) send an interrupt |
| 7152 | * to kick the cpu out of guest context. Next time |
| 7153 | * guest context is entered kvmclock will be updated, |
| 7154 | * so the guest will not see stale values. |
| 7155 | */ |
Viresh Kumar | df24014 | 2019-04-29 15:03:58 +0530 | [diff] [blame] | 7156 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7157 | } |
Viresh Kumar | df24014 | 2019-04-29 15:03:58 +0530 | [diff] [blame] | 7158 | } |
| 7159 | |
| 7160 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 7161 | void *data) |
| 7162 | { |
| 7163 | struct cpufreq_freqs *freq = data; |
| 7164 | int cpu; |
| 7165 | |
| 7166 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 7167 | return 0; |
| 7168 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 7169 | return 0; |
| 7170 | |
| 7171 | for_each_cpu(cpu, freq->policy->cpus) |
| 7172 | __kvmclock_cpufreq_notifier(freq, cpu); |
| 7173 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7174 | return 0; |
| 7175 | } |
| 7176 | |
| 7177 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7178 | .notifier_call = kvmclock_cpufreq_notifier |
| 7179 | }; |
| 7180 | |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7181 | static int kvmclock_cpu_online(unsigned int cpu) |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7182 | { |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7183 | tsc_khz_changed(NULL); |
| 7184 | return 0; |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 7185 | } |
| 7186 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7187 | static void kvm_timer_init(void) |
| 7188 | { |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7189 | max_tsc_khz = tsc_khz; |
Srivatsa S. Bhat | 460dd42 | 2014-03-11 02:09:01 +0530 | [diff] [blame] | 7190 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7191 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7192 | #ifdef CONFIG_CPU_FREQ |
| 7193 | struct cpufreq_policy policy; |
Borislav Petkov | 758f588 | 2016-09-04 19:13:57 +0200 | [diff] [blame] | 7194 | int cpu; |
| 7195 | |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7196 | memset(&policy, 0, sizeof(policy)); |
Avi Kivity | 3e26f23 | 2010-12-16 12:16:34 +0200 | [diff] [blame] | 7197 | cpu = get_cpu(); |
| 7198 | cpufreq_get_policy(&policy, cpu); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7199 | if (policy.cpuinfo.max_freq) |
| 7200 | max_tsc_khz = policy.cpuinfo.max_freq; |
Avi Kivity | 3e26f23 | 2010-12-16 12:16:34 +0200 | [diff] [blame] | 7201 | put_cpu(); |
Zachary Amsden | c285545 | 2010-09-18 14:38:15 -1000 | [diff] [blame] | 7202 | #endif |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7203 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 7204 | CPUFREQ_TRANSITION_NOTIFIER); |
| 7205 | } |
Srivatsa S. Bhat | 460dd42 | 2014-03-11 02:09:01 +0530 | [diff] [blame] | 7206 | |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 7207 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7208 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7209 | } |
| 7210 | |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 7211 | DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
| 7212 | EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7213 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 7214 | int kvm_is_in_guest(void) |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7215 | { |
Alex,Shi | 086c985 | 2011-10-20 15:34:01 +0800 | [diff] [blame] | 7216 | return __this_cpu_read(current_vcpu) != NULL; |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | static int kvm_is_user_mode(void) |
| 7220 | { |
| 7221 | int user_mode = 3; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 7222 | |
Alex,Shi | 086c985 | 2011-10-20 15:34:01 +0800 | [diff] [blame] | 7223 | if (__this_cpu_read(current_vcpu)) |
| 7224 | user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 7225 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7226 | return user_mode != 0; |
| 7227 | } |
| 7228 | |
| 7229 | static unsigned long kvm_get_guest_ip(void) |
| 7230 | { |
| 7231 | unsigned long ip = 0; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 7232 | |
Alex,Shi | 086c985 | 2011-10-20 15:34:01 +0800 | [diff] [blame] | 7233 | if (__this_cpu_read(current_vcpu)) |
| 7234 | ip = kvm_rip_read(__this_cpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 7235 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7236 | return ip; |
| 7237 | } |
| 7238 | |
Luwei Kang | 8479e04 | 2019-02-18 19:26:07 -0500 | [diff] [blame] | 7239 | static void kvm_handle_intel_pt_intr(void) |
| 7240 | { |
| 7241 | struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu); |
| 7242 | |
| 7243 | kvm_make_request(KVM_REQ_PMI, vcpu); |
| 7244 | __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, |
| 7245 | (unsigned long *)&vcpu->arch.pmu.global_status); |
| 7246 | } |
| 7247 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7248 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
| 7249 | .is_in_guest = kvm_is_in_guest, |
| 7250 | .is_user_mode = kvm_is_user_mode, |
| 7251 | .get_guest_ip = kvm_get_guest_ip, |
Luwei Kang | 8479e04 | 2019-02-18 19:26:07 -0500 | [diff] [blame] | 7252 | .handle_intel_pt_intr = kvm_handle_intel_pt_intr, |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7253 | }; |
| 7254 | |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7255 | #ifdef CONFIG_X86_64 |
| 7256 | static void pvclock_gtod_update_fn(struct work_struct *work) |
| 7257 | { |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 7258 | struct kvm *kvm; |
| 7259 | |
| 7260 | struct kvm_vcpu *vcpu; |
| 7261 | int i; |
| 7262 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7263 | mutex_lock(&kvm_lock); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 7264 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 7265 | kvm_for_each_vcpu(i, vcpu, kvm) |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 7266 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 7267 | atomic_set(&kvm_guest_has_master_clock, 0); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 7268 | mutex_unlock(&kvm_lock); |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7269 | } |
| 7270 | |
| 7271 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); |
| 7272 | |
| 7273 | /* |
| 7274 | * Notification about pvclock gtod data update. |
| 7275 | */ |
| 7276 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, |
| 7277 | void *priv) |
| 7278 | { |
| 7279 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 7280 | struct timekeeper *tk = priv; |
| 7281 | |
| 7282 | update_pvclock_gtod(tk); |
| 7283 | |
| 7284 | /* disable master clock if host does not trust, or does not |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 7285 | * use, TSC based clocksource. |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7286 | */ |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 7287 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7288 | atomic_read(&kvm_guest_has_master_clock) != 0) |
| 7289 | queue_work(system_long_wq, &pvclock_gtod_work); |
| 7290 | |
| 7291 | return 0; |
| 7292 | } |
| 7293 | |
| 7294 | static struct notifier_block pvclock_gtod_notifier = { |
| 7295 | .notifier_call = pvclock_gtod_notify, |
| 7296 | }; |
| 7297 | #endif |
| 7298 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7299 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 7300 | { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7301 | int r; |
Mathias Krause | 6b61edf | 2013-06-26 20:36:23 +0200 | [diff] [blame] | 7302 | struct kvm_x86_ops *ops = opaque; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7303 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7304 | if (kvm_x86_ops) { |
| 7305 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7306 | r = -EEXIST; |
| 7307 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7308 | } |
| 7309 | |
| 7310 | if (!ops->cpu_has_kvm_support()) { |
| 7311 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7312 | r = -EOPNOTSUPP; |
| 7313 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7314 | } |
| 7315 | if (ops->disabled_by_bios()) { |
| 7316 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7317 | r = -EOPNOTSUPP; |
| 7318 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7319 | } |
| 7320 | |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 7321 | /* |
| 7322 | * KVM explicitly assumes that the guest has an FPU and |
| 7323 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the |
| 7324 | * vCPU's FPU state as a fxregs_state struct. |
| 7325 | */ |
| 7326 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { |
| 7327 | printk(KERN_ERR "kvm: inadequate fpu\n"); |
| 7328 | r = -EOPNOTSUPP; |
| 7329 | goto out; |
| 7330 | } |
| 7331 | |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7332 | r = -ENOMEM; |
Paolo Bonzini | ed8e481 | 2018-12-21 11:25:59 +0100 | [diff] [blame] | 7333 | x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu), |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 7334 | __alignof__(struct fpu), SLAB_ACCOUNT, |
| 7335 | NULL); |
| 7336 | if (!x86_fpu_cache) { |
| 7337 | printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); |
| 7338 | goto out; |
| 7339 | } |
| 7340 | |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 7341 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
| 7342 | if (!x86_emulator_cache) { |
| 7343 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); |
| 7344 | goto out_free_x86_fpu_cache; |
| 7345 | } |
| 7346 | |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7347 | shared_msrs = alloc_percpu(struct kvm_shared_msrs); |
| 7348 | if (!shared_msrs) { |
| 7349 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n"); |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 7350 | goto out_free_x86_emulator_cache; |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7351 | } |
| 7352 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 7353 | r = kvm_mmu_module_init(); |
| 7354 | if (r) |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7355 | goto out_free_percpu; |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 7356 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7357 | kvm_x86_ops = ops; |
Paolo Bonzini | 920c837 | 2014-03-26 15:54:00 +0100 | [diff] [blame] | 7358 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 7359 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Bandan Das | ffb128c | 2016-07-12 18:18:49 -0400 | [diff] [blame] | 7360 | PT_DIRTY_MASK, PT64_NX_MASK, 0, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7361 | PT_PRESENT_MASK, 0, sme_me_mask); |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 7362 | kvm_timer_init(); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 7363 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7364 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
| 7365 | |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 7366 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7367 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 7368 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
| 7369 | } |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7370 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 7371 | kvm_lapic_init(); |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 7372 | if (pi_inject_timer == -1) |
| 7373 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7374 | #ifdef CONFIG_X86_64 |
| 7375 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7376 | |
Thomas Gleixner | 5fa4ec9 | 2018-01-31 09:41:40 +0100 | [diff] [blame] | 7377 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7378 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7379 | #endif |
| 7380 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7381 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7382 | |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7383 | out_free_percpu: |
| 7384 | free_percpu(shared_msrs); |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 7385 | out_free_x86_emulator_cache: |
| 7386 | kmem_cache_destroy(x86_emulator_cache); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 7387 | out_free_x86_fpu_cache: |
| 7388 | kmem_cache_destroy(x86_fpu_cache); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7389 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7390 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 7391 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7392 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7393 | void kvm_arch_exit(void) |
| 7394 | { |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7395 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 5fa4ec9 | 2018-01-31 09:41:40 +0100 | [diff] [blame] | 7396 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
Vitaly Kuznetsov | 0092e43 | 2018-01-24 14:23:37 +0100 | [diff] [blame] | 7397 | clear_hv_tscchange_cb(); |
| 7398 | #endif |
David Matlack | cef84c3 | 2016-12-16 14:30:36 -0800 | [diff] [blame] | 7399 | kvm_lapic_exit(); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7400 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
| 7401 | |
Jan Kiszka | 888d256 | 2009-04-17 19:24:58 +0200 | [diff] [blame] | 7402 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 7403 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 7404 | CPUFREQ_TRANSITION_NOTIFIER); |
Sebastian Andrzej Siewior | 251a5fd | 2016-07-13 17:16:33 +0000 | [diff] [blame] | 7405 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
Marcelo Tosatti | 16e8d74 | 2012-11-27 23:29:00 -0200 | [diff] [blame] | 7406 | #ifdef CONFIG_X86_64 |
| 7407 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); |
| 7408 | #endif |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7409 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7410 | kvm_mmu_module_exit(); |
Marcelo Tosatti | 013f6a5 | 2013-01-03 11:41:39 -0200 | [diff] [blame] | 7411 | free_percpu(shared_msrs); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 7412 | kmem_cache_destroy(x86_fpu_cache); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 7413 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 7414 | |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7415 | int kvm_vcpu_halt(struct kvm_vcpu *vcpu) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7416 | { |
| 7417 | ++vcpu->stat.halt_exits; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 7418 | if (lapic_in_kernel(vcpu)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 7419 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7420 | return 1; |
| 7421 | } else { |
| 7422 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 7423 | return 0; |
| 7424 | } |
| 7425 | } |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7426 | EXPORT_SYMBOL_GPL(kvm_vcpu_halt); |
| 7427 | |
| 7428 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 7429 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7430 | int ret = kvm_skip_emulated_instruction(vcpu); |
| 7431 | /* |
| 7432 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered |
| 7433 | * KVM_EXIT_DEBUG here. |
| 7434 | */ |
| 7435 | return kvm_vcpu_halt(vcpu) && ret; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7436 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7437 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 7438 | |
Arnd Bergmann | 8ef81a9 | 2017-02-09 16:10:42 +0100 | [diff] [blame] | 7439 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 7440 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
| 7441 | unsigned long clock_type) |
| 7442 | { |
| 7443 | struct kvm_clock_pairing clock_pairing; |
Arnd Bergmann | 899a31f | 2018-04-23 10:04:26 +0200 | [diff] [blame] | 7444 | struct timespec64 ts; |
Paolo Bonzini | 80fbd89 | 2017-02-08 10:57:24 +0100 | [diff] [blame] | 7445 | u64 cycle; |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 7446 | int ret; |
| 7447 | |
| 7448 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) |
| 7449 | return -KVM_EOPNOTSUPP; |
| 7450 | |
| 7451 | if (kvm_get_walltime_and_clockread(&ts, &cycle) == false) |
| 7452 | return -KVM_EOPNOTSUPP; |
| 7453 | |
| 7454 | clock_pairing.sec = ts.tv_sec; |
| 7455 | clock_pairing.nsec = ts.tv_nsec; |
| 7456 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); |
| 7457 | clock_pairing.flags = 0; |
Liran Alon | bcbfbd8 | 2018-11-08 00:43:06 +0200 | [diff] [blame] | 7458 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 7459 | |
| 7460 | ret = 0; |
| 7461 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, |
| 7462 | sizeof(struct kvm_clock_pairing))) |
| 7463 | ret = -KVM_EFAULT; |
| 7464 | |
| 7465 | return ret; |
| 7466 | } |
Arnd Bergmann | 8ef81a9 | 2017-02-09 16:10:42 +0100 | [diff] [blame] | 7467 | #endif |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 7468 | |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7469 | /* |
| 7470 | * kvm_pv_kick_cpu_op: Kick a vcpu. |
| 7471 | * |
| 7472 | * @apicid - apicid of vcpu to be kicked. |
| 7473 | */ |
| 7474 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid) |
| 7475 | { |
Raghavendra K T | 24d2166 | 2013-08-26 14:18:35 +0530 | [diff] [blame] | 7476 | struct kvm_lapic_irq lapic_irq; |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7477 | |
Peter Xu | 150a84f | 2019-12-04 20:07:21 +0100 | [diff] [blame] | 7478 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
Peter Xu | c96001c | 2019-12-04 20:07:18 +0100 | [diff] [blame] | 7479 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
Longpeng(Mike) | ebd28fc | 2017-08-02 11:20:51 +0800 | [diff] [blame] | 7480 | lapic_irq.level = 0; |
Raghavendra K T | 24d2166 | 2013-08-26 14:18:35 +0530 | [diff] [blame] | 7481 | lapic_irq.dest_id = apicid; |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 7482 | lapic_irq.msi_redir_hint = false; |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7483 | |
Raghavendra K T | 24d2166 | 2013-08-26 14:18:35 +0530 | [diff] [blame] | 7484 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
Xiubo Li | 795a149 | 2015-03-13 17:39:44 +0800 | [diff] [blame] | 7485 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7486 | } |
| 7487 | |
Suravee Suthikulpanit | 4e19c36 | 2019-11-14 14:15:05 -0600 | [diff] [blame] | 7488 | bool kvm_apicv_activated(struct kvm *kvm) |
| 7489 | { |
| 7490 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); |
| 7491 | } |
| 7492 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); |
| 7493 | |
| 7494 | void kvm_apicv_init(struct kvm *kvm, bool enable) |
| 7495 | { |
| 7496 | if (enable) |
| 7497 | clear_bit(APICV_INHIBIT_REASON_DISABLE, |
| 7498 | &kvm->arch.apicv_inhibit_reasons); |
| 7499 | else |
| 7500 | set_bit(APICV_INHIBIT_REASON_DISABLE, |
| 7501 | &kvm->arch.apicv_inhibit_reasons); |
| 7502 | } |
| 7503 | EXPORT_SYMBOL_GPL(kvm_apicv_init); |
| 7504 | |
Wanpeng Li | 7150629 | 2019-06-11 20:23:49 +0800 | [diff] [blame] | 7505 | static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) |
| 7506 | { |
| 7507 | struct kvm_vcpu *target = NULL; |
| 7508 | struct kvm_apic_map *map; |
| 7509 | |
| 7510 | rcu_read_lock(); |
| 7511 | map = rcu_dereference(kvm->arch.apic_map); |
| 7512 | |
| 7513 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) |
| 7514 | target = map->phys_map[dest_id]->vcpu; |
| 7515 | |
| 7516 | rcu_read_unlock(); |
| 7517 | |
Wanpeng Li | 266e85a | 2019-07-24 17:43:13 +0800 | [diff] [blame] | 7518 | if (target && READ_ONCE(target->ready)) |
Wanpeng Li | 7150629 | 2019-06-11 20:23:49 +0800 | [diff] [blame] | 7519 | kvm_vcpu_yield_to(target); |
| 7520 | } |
| 7521 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7522 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 7523 | { |
| 7524 | unsigned long nr, a0, a1, a2, a3, ret; |
Marian Rotariu | 6356ee0 | 2018-04-30 12:23:01 +0300 | [diff] [blame] | 7525 | int op_64_bit; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7526 | |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 7527 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 7528 | return kvm_hv_hypercall(vcpu); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 7529 | |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 7530 | nr = kvm_rax_read(vcpu); |
| 7531 | a0 = kvm_rbx_read(vcpu); |
| 7532 | a1 = kvm_rcx_read(vcpu); |
| 7533 | a2 = kvm_rdx_read(vcpu); |
| 7534 | a3 = kvm_rsi_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7535 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7536 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7537 | |
Nadav Amit | a449c7a | 2014-06-18 17:19:24 +0300 | [diff] [blame] | 7538 | op_64_bit = is_64_bit_mode(vcpu); |
| 7539 | if (!op_64_bit) { |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7540 | nr &= 0xFFFFFFFF; |
| 7541 | a0 &= 0xFFFFFFFF; |
| 7542 | a1 &= 0xFFFFFFFF; |
| 7543 | a2 &= 0xFFFFFFFF; |
| 7544 | a3 &= 0xFFFFFFFF; |
| 7545 | } |
| 7546 | |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 7547 | if (kvm_x86_ops->get_cpl(vcpu) != 0) { |
| 7548 | ret = -KVM_EPERM; |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 7549 | goto out; |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7552 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 7553 | case KVM_HC_VAPIC_POLL_IRQ: |
| 7554 | ret = 0; |
| 7555 | break; |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7556 | case KVM_HC_KICK_CPU: |
| 7557 | kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); |
Wanpeng Li | 266e85a | 2019-07-24 17:43:13 +0800 | [diff] [blame] | 7558 | kvm_sched_yield(vcpu->kvm, a1); |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 7559 | ret = 0; |
| 7560 | break; |
Arnd Bergmann | 8ef81a9 | 2017-02-09 16:10:42 +0100 | [diff] [blame] | 7561 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 55dd00a | 2017-01-24 15:09:39 -0200 | [diff] [blame] | 7562 | case KVM_HC_CLOCK_PAIRING: |
| 7563 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); |
| 7564 | break; |
Sean Christopherson | 1ed199a | 2019-01-23 09:22:39 -0800 | [diff] [blame] | 7565 | #endif |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 7566 | case KVM_HC_SEND_IPI: |
| 7567 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); |
| 7568 | break; |
Wanpeng Li | 7150629 | 2019-06-11 20:23:49 +0800 | [diff] [blame] | 7569 | case KVM_HC_SCHED_YIELD: |
| 7570 | kvm_sched_yield(vcpu->kvm, a0); |
| 7571 | ret = 0; |
| 7572 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7573 | default: |
| 7574 | ret = -KVM_ENOSYS; |
| 7575 | break; |
| 7576 | } |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 7577 | out: |
Nadav Amit | a449c7a | 2014-06-18 17:19:24 +0300 | [diff] [blame] | 7578 | if (!op_64_bit) |
| 7579 | ret = (u32)ret; |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 7580 | kvm_rax_write(vcpu, ret); |
Marian Rotariu | 6356ee0 | 2018-04-30 12:23:01 +0300 | [diff] [blame] | 7581 | |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 7582 | ++vcpu->stat.hypercalls; |
Marian Rotariu | 6356ee0 | 2018-04-30 12:23:01 +0300 | [diff] [blame] | 7583 | return kvm_skip_emulated_instruction(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7584 | } |
| 7585 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 7586 | |
Jan Kiszka | b6785de | 2012-09-20 07:43:17 +0200 | [diff] [blame] | 7587 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7588 | { |
Avi Kivity | d6aa100 | 2011-04-20 15:47:13 +0300 | [diff] [blame] | 7589 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7590 | char instruction[3]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7591 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7592 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7593 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7594 | |
Dmitry Vyukov | ce2e852 | 2017-01-17 14:51:04 +0100 | [diff] [blame] | 7595 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
| 7596 | &ctxt->exception); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 7597 | } |
| 7598 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7599 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 7600 | { |
Matt Gingell | 782d422 | 2015-11-16 15:26:00 -0800 | [diff] [blame] | 7601 | return vcpu->run->request_interrupt_window && |
| 7602 | likely(!pic_in_kernel(vcpu->kvm)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 7603 | } |
| 7604 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7605 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 7606 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7607 | struct kvm_run *kvm_run = vcpu->run; |
| 7608 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 7609 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Paolo Bonzini | f077825 | 2015-04-01 15:06:40 +0200 | [diff] [blame] | 7610 | kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 7611 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 7612 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Matt Gingell | 127a457 | 2015-11-17 17:32:05 +0100 | [diff] [blame] | 7613 | kvm_run->ready_for_interrupt_injection = |
| 7614 | pic_in_kernel(vcpu->kvm) || |
Matt Gingell | 782d422 | 2015-11-16 15:26:00 -0800 | [diff] [blame] | 7615 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 7616 | } |
| 7617 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7618 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 7619 | { |
| 7620 | int max_irr, tpr; |
| 7621 | |
| 7622 | if (!kvm_x86_ops->update_cr8_intercept) |
| 7623 | return; |
| 7624 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 7625 | if (!lapic_in_kernel(vcpu)) |
Avi Kivity | 88c808f | 2009-08-17 22:49:40 +0300 | [diff] [blame] | 7626 | return; |
| 7627 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 7628 | if (vcpu->arch.apicv_active) |
| 7629 | return; |
| 7630 | |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 7631 | if (!vcpu->arch.apic->vapic_addr) |
| 7632 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 7633 | else |
| 7634 | max_irr = -1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7635 | |
| 7636 | if (max_irr != -1) |
| 7637 | max_irr >>= 4; |
| 7638 | |
| 7639 | tpr = kvm_lapic_get_cr8(vcpu); |
| 7640 | |
| 7641 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); |
| 7642 | } |
| 7643 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 7644 | static int inject_pending_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7645 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 7646 | int r; |
| 7647 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7648 | /* try to reinject previous events if any */ |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7649 | |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7650 | if (vcpu->arch.exception.injected) |
| 7651 | kvm_x86_ops->queue_exception(vcpu); |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7652 | /* |
Liran Alon | a042c26 | 2018-03-23 03:01:32 +0300 | [diff] [blame] | 7653 | * Do not inject an NMI or interrupt if there is a pending |
| 7654 | * exception. Exceptions and interrupts are recognized at |
| 7655 | * instruction boundaries, i.e. the start of an instruction. |
| 7656 | * Trap-like exceptions, e.g. #DB, have higher priority than |
| 7657 | * NMIs and interrupts, i.e. traps are recognized before an |
| 7658 | * NMI/interrupt that's pending on the same instruction. |
| 7659 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest |
| 7660 | * priority, but are only generated (pended) during instruction |
| 7661 | * execution, i.e. a pending fault-like exception means the |
| 7662 | * fault occurred on the *previous* instruction and must be |
| 7663 | * serviced prior to recognizing any new events in order to |
| 7664 | * fully complete the previous instruction. |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7665 | */ |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7666 | else if (!vcpu->arch.exception.pending) { |
| 7667 | if (vcpu->arch.nmi_injected) |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7668 | kvm_x86_ops->set_nmi(vcpu); |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7669 | else if (vcpu->arch.interrupt.injected) |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7670 | kvm_x86_ops->set_irq(vcpu); |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7671 | } |
| 7672 | |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7673 | /* |
| 7674 | * Call check_nested_events() even if we reinjected a previous event |
| 7675 | * in order for caller to determine if it should require immediate-exit |
| 7676 | * from L2 to L1 due to pending L1 events which require exit |
| 7677 | * from L2 to L1. |
| 7678 | */ |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7679 | if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) { |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 7680 | r = kvm_x86_ops->check_nested_events(vcpu); |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7681 | if (r != 0) |
| 7682 | return r; |
| 7683 | } |
| 7684 | |
| 7685 | /* try to inject new event if pending */ |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 7686 | if (vcpu->arch.exception.pending) { |
Avi Kivity | 5c1c85d0 | 2010-03-11 13:01:59 +0200 | [diff] [blame] | 7687 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
| 7688 | vcpu->arch.exception.has_error_code, |
| 7689 | vcpu->arch.exception.error_code); |
Nadav Amit | d6e8c85 | 2014-07-24 14:51:24 +0300 | [diff] [blame] | 7690 | |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7691 | WARN_ON_ONCE(vcpu->arch.exception.injected); |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 7692 | vcpu->arch.exception.pending = false; |
| 7693 | vcpu->arch.exception.injected = true; |
| 7694 | |
Nadav Amit | d6e8c85 | 2014-07-24 14:51:24 +0300 | [diff] [blame] | 7695 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
| 7696 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | |
| 7697 | X86_EFLAGS_RF); |
| 7698 | |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 7699 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
| 7700 | /* |
| 7701 | * This code assumes that nSVM doesn't use |
| 7702 | * check_nested_events(). If it does, the |
| 7703 | * DR6/DR7 changes should happen before L1 |
| 7704 | * gets a #VMEXIT for an intercepted #DB in |
| 7705 | * L2. (Under VMX, on the other hand, the |
| 7706 | * DR6/DR7 changes should not happen in the |
| 7707 | * event of a VM-exit to L1 for an intercepted |
| 7708 | * #DB in L2.) |
| 7709 | */ |
| 7710 | kvm_deliver_exception_payload(vcpu); |
| 7711 | if (vcpu->arch.dr7 & DR7_GD) { |
| 7712 | vcpu->arch.dr7 &= ~DR7_GD; |
| 7713 | kvm_update_dr7(vcpu); |
| 7714 | } |
Nadav Amit | 6bdf066 | 2014-09-30 20:49:14 +0300 | [diff] [blame] | 7715 | } |
| 7716 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 7717 | kvm_x86_ops->queue_exception(vcpu); |
Liran Alon | 1a680e3 | 2018-03-23 03:01:33 +0300 | [diff] [blame] | 7718 | } |
| 7719 | |
| 7720 | /* Don't consider new event if we re-injected an event */ |
| 7721 | if (kvm_event_needs_reinjection(vcpu)) |
| 7722 | return 0; |
| 7723 | |
| 7724 | if (vcpu->arch.smi_pending && !is_smm(vcpu) && |
| 7725 | kvm_x86_ops->smi_allowed(vcpu)) { |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 7726 | vcpu->arch.smi_pending = false; |
Liran Alon | 52797bf | 2017-11-15 13:43:14 +0200 | [diff] [blame] | 7727 | ++vcpu->arch.smi_count; |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7728 | enter_smm(vcpu); |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 7729 | } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) { |
Yuki Shibuya | 321c565 | 2016-03-24 05:17:03 +0000 | [diff] [blame] | 7730 | --vcpu->arch.nmi_pending; |
| 7731 | vcpu->arch.nmi_injected = true; |
| 7732 | kvm_x86_ops->set_nmi(vcpu); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7733 | } else if (kvm_cpu_has_injectable_intr(vcpu)) { |
Bandan Das | 9242b5b | 2014-07-08 00:30:23 -0400 | [diff] [blame] | 7734 | /* |
| 7735 | * Because interrupts can be injected asynchronously, we are |
| 7736 | * calling check_nested_events again here to avoid a race condition. |
| 7737 | * See https://lkml.org/lkml/2014/7/2/60 for discussion about this |
| 7738 | * proposal and current concerns. Perhaps we should be setting |
| 7739 | * KVM_REQ_EVENT only on certain events and not unconditionally? |
| 7740 | */ |
| 7741 | if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) { |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 7742 | r = kvm_x86_ops->check_nested_events(vcpu); |
Bandan Das | 9242b5b | 2014-07-08 00:30:23 -0400 | [diff] [blame] | 7743 | if (r != 0) |
| 7744 | return r; |
| 7745 | } |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7746 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7747 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
| 7748 | false); |
| 7749 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7750 | } |
| 7751 | } |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7752 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 7753 | return 0; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7754 | } |
| 7755 | |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 7756 | static void process_nmi(struct kvm_vcpu *vcpu) |
| 7757 | { |
| 7758 | unsigned limit = 2; |
| 7759 | |
| 7760 | /* |
| 7761 | * x86 is limited to one NMI running, and one NMI pending after it. |
| 7762 | * If an NMI is already in progress, limit further NMIs to just one. |
| 7763 | * Otherwise, allow two (and we'll inject the first one immediately). |
| 7764 | */ |
| 7765 | if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected) |
| 7766 | limit = 1; |
| 7767 | |
| 7768 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); |
| 7769 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); |
| 7770 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7771 | } |
| 7772 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7773 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7774 | { |
| 7775 | u32 flags = 0; |
| 7776 | flags |= seg->g << 23; |
| 7777 | flags |= seg->db << 22; |
| 7778 | flags |= seg->l << 21; |
| 7779 | flags |= seg->avl << 20; |
| 7780 | flags |= seg->present << 15; |
| 7781 | flags |= seg->dpl << 13; |
| 7782 | flags |= seg->s << 12; |
| 7783 | flags |= seg->type << 8; |
| 7784 | return flags; |
| 7785 | } |
| 7786 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7787 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7788 | { |
| 7789 | struct kvm_segment seg; |
| 7790 | int offset; |
| 7791 | |
| 7792 | kvm_get_segment(vcpu, &seg, n); |
| 7793 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); |
| 7794 | |
| 7795 | if (n < 3) |
| 7796 | offset = 0x7f84 + n * 12; |
| 7797 | else |
| 7798 | offset = 0x7f2c + (n - 3) * 12; |
| 7799 | |
| 7800 | put_smstate(u32, buf, offset + 8, seg.base); |
| 7801 | put_smstate(u32, buf, offset + 4, seg.limit); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7802 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7803 | } |
| 7804 | |
Alexander Kuleshov | efbb288 | 2015-09-06 19:35:41 +0600 | [diff] [blame] | 7805 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7806 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7807 | { |
| 7808 | struct kvm_segment seg; |
| 7809 | int offset; |
| 7810 | u16 flags; |
| 7811 | |
| 7812 | kvm_get_segment(vcpu, &seg, n); |
| 7813 | offset = 0x7e00 + n * 16; |
| 7814 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7815 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7816 | put_smstate(u16, buf, offset, seg.selector); |
| 7817 | put_smstate(u16, buf, offset + 2, flags); |
| 7818 | put_smstate(u32, buf, offset + 4, seg.limit); |
| 7819 | put_smstate(u64, buf, offset + 8, seg.base); |
| 7820 | } |
Alexander Kuleshov | efbb288 | 2015-09-06 19:35:41 +0600 | [diff] [blame] | 7821 | #endif |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7822 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7823 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7824 | { |
| 7825 | struct desc_ptr dt; |
| 7826 | struct kvm_segment seg; |
| 7827 | unsigned long val; |
| 7828 | int i; |
| 7829 | |
| 7830 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); |
| 7831 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); |
| 7832 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); |
| 7833 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); |
| 7834 | |
| 7835 | for (i = 0; i < 8; i++) |
| 7836 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i)); |
| 7837 | |
| 7838 | kvm_get_dr(vcpu, 6, &val); |
| 7839 | put_smstate(u32, buf, 0x7fcc, (u32)val); |
| 7840 | kvm_get_dr(vcpu, 7, &val); |
| 7841 | put_smstate(u32, buf, 0x7fc8, (u32)val); |
| 7842 | |
| 7843 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); |
| 7844 | put_smstate(u32, buf, 0x7fc4, seg.selector); |
| 7845 | put_smstate(u32, buf, 0x7f64, seg.base); |
| 7846 | put_smstate(u32, buf, 0x7f60, seg.limit); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7847 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7848 | |
| 7849 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); |
| 7850 | put_smstate(u32, buf, 0x7fc0, seg.selector); |
| 7851 | put_smstate(u32, buf, 0x7f80, seg.base); |
| 7852 | put_smstate(u32, buf, 0x7f7c, seg.limit); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7853 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7854 | |
| 7855 | kvm_x86_ops->get_gdt(vcpu, &dt); |
| 7856 | put_smstate(u32, buf, 0x7f74, dt.address); |
| 7857 | put_smstate(u32, buf, 0x7f70, dt.size); |
| 7858 | |
| 7859 | kvm_x86_ops->get_idt(vcpu, &dt); |
| 7860 | put_smstate(u32, buf, 0x7f58, dt.address); |
| 7861 | put_smstate(u32, buf, 0x7f54, dt.size); |
| 7862 | |
| 7863 | for (i = 0; i < 6; i++) |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7864 | enter_smm_save_seg_32(vcpu, buf, i); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7865 | |
| 7866 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); |
| 7867 | |
| 7868 | /* revision id */ |
| 7869 | put_smstate(u32, buf, 0x7efc, 0x00020000); |
| 7870 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); |
| 7871 | } |
| 7872 | |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 7873 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7874 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7875 | { |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7876 | struct desc_ptr dt; |
| 7877 | struct kvm_segment seg; |
| 7878 | unsigned long val; |
| 7879 | int i; |
| 7880 | |
| 7881 | for (i = 0; i < 16; i++) |
| 7882 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i)); |
| 7883 | |
| 7884 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); |
| 7885 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); |
| 7886 | |
| 7887 | kvm_get_dr(vcpu, 6, &val); |
| 7888 | put_smstate(u64, buf, 0x7f68, val); |
| 7889 | kvm_get_dr(vcpu, 7, &val); |
| 7890 | put_smstate(u64, buf, 0x7f60, val); |
| 7891 | |
| 7892 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); |
| 7893 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); |
| 7894 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); |
| 7895 | |
| 7896 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); |
| 7897 | |
| 7898 | /* revision id */ |
| 7899 | put_smstate(u32, buf, 0x7efc, 0x00020064); |
| 7900 | |
| 7901 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); |
| 7902 | |
| 7903 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); |
| 7904 | put_smstate(u16, buf, 0x7e90, seg.selector); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7905 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7906 | put_smstate(u32, buf, 0x7e94, seg.limit); |
| 7907 | put_smstate(u64, buf, 0x7e98, seg.base); |
| 7908 | |
| 7909 | kvm_x86_ops->get_idt(vcpu, &dt); |
| 7910 | put_smstate(u32, buf, 0x7e84, dt.size); |
| 7911 | put_smstate(u64, buf, 0x7e88, dt.address); |
| 7912 | |
| 7913 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); |
| 7914 | put_smstate(u16, buf, 0x7e70, seg.selector); |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7915 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7916 | put_smstate(u32, buf, 0x7e74, seg.limit); |
| 7917 | put_smstate(u64, buf, 0x7e78, seg.base); |
| 7918 | |
| 7919 | kvm_x86_ops->get_gdt(vcpu, &dt); |
| 7920 | put_smstate(u32, buf, 0x7e64, dt.size); |
| 7921 | put_smstate(u64, buf, 0x7e68, dt.address); |
| 7922 | |
| 7923 | for (i = 0; i < 6; i++) |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7924 | enter_smm_save_seg_64(vcpu, buf, i); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7925 | } |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 7926 | #endif |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7927 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7928 | static void enter_smm(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 7929 | { |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7930 | struct kvm_segment cs, ds; |
Paolo Bonzini | 18c3626 | 2015-08-07 12:27:54 +0200 | [diff] [blame] | 7931 | struct desc_ptr dt; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7932 | char buf[512]; |
| 7933 | u32 cr0; |
| 7934 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7935 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7936 | memset(buf, 0, 512); |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 7937 | #ifdef CONFIG_X86_64 |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 7938 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7939 | enter_smm_save_state_64(vcpu, buf); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7940 | else |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 7941 | #endif |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 7942 | enter_smm_save_state_32(vcpu, buf); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7943 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7944 | /* |
| 7945 | * Give pre_enter_smm() a chance to make ISA-specific changes to the |
| 7946 | * vCPU state (e.g. leave guest mode) after we've saved the state into |
| 7947 | * the SMM state-save area. |
| 7948 | */ |
| 7949 | kvm_x86_ops->pre_enter_smm(vcpu, buf); |
| 7950 | |
| 7951 | vcpu->arch.hflags |= HF_SMM_MASK; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7952 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7953 | |
| 7954 | if (kvm_x86_ops->get_nmi_mask(vcpu)) |
| 7955 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
| 7956 | else |
| 7957 | kvm_x86_ops->set_nmi_mask(vcpu, true); |
| 7958 | |
| 7959 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 7960 | kvm_rip_write(vcpu, 0x8000); |
| 7961 | |
| 7962 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); |
| 7963 | kvm_x86_ops->set_cr0(vcpu, cr0); |
| 7964 | vcpu->arch.cr0 = cr0; |
| 7965 | |
| 7966 | kvm_x86_ops->set_cr4(vcpu, 0); |
| 7967 | |
Paolo Bonzini | 18c3626 | 2015-08-07 12:27:54 +0200 | [diff] [blame] | 7968 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
| 7969 | dt.address = dt.size = 0; |
| 7970 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 7971 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 7972 | __kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 7973 | |
| 7974 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; |
| 7975 | cs.base = vcpu->arch.smbase; |
| 7976 | |
| 7977 | ds.selector = 0; |
| 7978 | ds.base = 0; |
| 7979 | |
| 7980 | cs.limit = ds.limit = 0xffffffff; |
| 7981 | cs.type = ds.type = 0x3; |
| 7982 | cs.dpl = ds.dpl = 0; |
| 7983 | cs.db = ds.db = 0; |
| 7984 | cs.s = ds.s = 1; |
| 7985 | cs.l = ds.l = 0; |
| 7986 | cs.g = ds.g = 1; |
| 7987 | cs.avl = ds.avl = 0; |
| 7988 | cs.present = ds.present = 1; |
| 7989 | cs.unusable = ds.unusable = 0; |
| 7990 | cs.padding = ds.padding = 0; |
| 7991 | |
| 7992 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7993 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); |
| 7994 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); |
| 7995 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); |
| 7996 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); |
| 7997 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); |
| 7998 | |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 7999 | #ifdef CONFIG_X86_64 |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 8000 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 8001 | kvm_x86_ops->set_efer(vcpu, 0); |
Sean Christopherson | b68f3cc | 2019-04-02 08:10:48 -0700 | [diff] [blame] | 8002 | #endif |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 8003 | |
| 8004 | kvm_update_cpuid(vcpu); |
| 8005 | kvm_mmu_reset_context(vcpu); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 8006 | } |
| 8007 | |
Paolo Bonzini | ee2cd4b | 2016-06-01 22:26:01 +0200 | [diff] [blame] | 8008 | static void process_smi(struct kvm_vcpu *vcpu) |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 8009 | { |
| 8010 | vcpu->arch.smi_pending = true; |
| 8011 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 8012 | } |
| 8013 | |
Nitesh Narayan Lal | 7ee30bc | 2019-11-07 07:53:43 -0500 | [diff] [blame] | 8014 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
| 8015 | unsigned long *vcpu_bitmap) |
| 8016 | { |
| 8017 | cpumask_var_t cpus; |
Nitesh Narayan Lal | 7ee30bc | 2019-11-07 07:53:43 -0500 | [diff] [blame] | 8018 | |
| 8019 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); |
| 8020 | |
Mao Wenan | db5a95e | 2019-11-19 11:06:40 +0800 | [diff] [blame] | 8021 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, |
| 8022 | vcpu_bitmap, cpus); |
Nitesh Narayan Lal | 7ee30bc | 2019-11-07 07:53:43 -0500 | [diff] [blame] | 8023 | |
| 8024 | free_cpumask_var(cpus); |
| 8025 | } |
| 8026 | |
Paolo Bonzini | 2860c4b | 2016-01-07 15:05:10 +0100 | [diff] [blame] | 8027 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
| 8028 | { |
| 8029 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); |
| 8030 | } |
| 8031 | |
Suravee Suthikulpanit | 8df14af | 2019-11-14 14:15:06 -0600 | [diff] [blame] | 8032 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
| 8033 | { |
| 8034 | if (!lapic_in_kernel(vcpu)) |
| 8035 | return; |
| 8036 | |
| 8037 | vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm); |
| 8038 | kvm_apic_update_apicv(vcpu); |
| 8039 | kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu); |
| 8040 | } |
| 8041 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); |
| 8042 | |
| 8043 | /* |
| 8044 | * NOTE: Do not hold any lock prior to calling this. |
| 8045 | * |
| 8046 | * In particular, kvm_request_apicv_update() expects kvm->srcu not to be |
| 8047 | * locked, because it calls __x86_set_memory_region() which does |
| 8048 | * synchronize_srcu(&kvm->srcu). |
| 8049 | */ |
| 8050 | void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit) |
| 8051 | { |
Paolo Bonzini | 8e205a6 | 2020-03-14 12:29:23 +0100 | [diff] [blame^] | 8052 | unsigned long old, new, expected; |
| 8053 | |
Suravee Suthikulpanit | ef8efd7 | 2019-11-14 14:15:10 -0600 | [diff] [blame] | 8054 | if (!kvm_x86_ops->check_apicv_inhibit_reasons || |
| 8055 | !kvm_x86_ops->check_apicv_inhibit_reasons(bit)) |
| 8056 | return; |
| 8057 | |
Paolo Bonzini | 8e205a6 | 2020-03-14 12:29:23 +0100 | [diff] [blame^] | 8058 | old = READ_ONCE(kvm->arch.apicv_inhibit_reasons); |
| 8059 | do { |
| 8060 | expected = new = old; |
| 8061 | if (activate) |
| 8062 | __clear_bit(bit, &new); |
| 8063 | else |
| 8064 | __set_bit(bit, &new); |
| 8065 | if (new == old) |
| 8066 | break; |
| 8067 | old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new); |
| 8068 | } while (old != expected); |
| 8069 | |
| 8070 | if (!!old == !!new) |
| 8071 | return; |
Suravee Suthikulpanit | 8df14af | 2019-11-14 14:15:06 -0600 | [diff] [blame] | 8072 | |
Suravee Suthikulpanit | 24bbf74 | 2019-11-14 14:15:07 -0600 | [diff] [blame] | 8073 | trace_kvm_apicv_update_request(activate, bit); |
Suravee Suthikulpanit | 2de9d0c | 2019-11-14 14:15:11 -0600 | [diff] [blame] | 8074 | if (kvm_x86_ops->pre_update_apicv_exec_ctrl) |
| 8075 | kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate); |
Suravee Suthikulpanit | 8df14af | 2019-11-14 14:15:06 -0600 | [diff] [blame] | 8076 | kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE); |
| 8077 | } |
| 8078 | EXPORT_SYMBOL_GPL(kvm_request_apicv_update); |
| 8079 | |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8080 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8081 | { |
Wanpeng Li | dcbd3e4 | 2018-12-17 10:43:23 +0800 | [diff] [blame] | 8082 | if (!kvm_apic_present(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8083 | return; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8084 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8085 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8086 | |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 8087 | if (irqchip_split(vcpu->kvm)) |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8088 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
Radim Krčmář | db2bdcb | 2015-10-08 20:23:34 +0200 | [diff] [blame] | 8089 | else { |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 8090 | if (vcpu->arch.apicv_active) |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8091 | kvm_x86_ops->sync_pir_to_irr(vcpu); |
Wanpeng Li | e97f852 | 2018-11-20 16:34:18 +0800 | [diff] [blame] | 8092 | if (ioapic_in_kernel(vcpu->kvm)) |
| 8093 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); |
Radim Krčmář | db2bdcb | 2015-10-08 20:23:34 +0200 | [diff] [blame] | 8094 | } |
Liran Alon | e40ff1d | 2018-03-21 02:50:31 +0200 | [diff] [blame] | 8095 | |
| 8096 | if (is_guest_mode(vcpu)) |
| 8097 | vcpu->arch.load_eoi_exitmap_pending = true; |
| 8098 | else |
| 8099 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); |
| 8100 | } |
| 8101 | |
| 8102 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) |
| 8103 | { |
| 8104 | u64 eoi_exit_bitmap[4]; |
| 8105 | |
| 8106 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) |
| 8107 | return; |
| 8108 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 8109 | bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors, |
| 8110 | vcpu_to_synic(vcpu)->vec_bitmap, 256); |
| 8111 | kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8112 | } |
| 8113 | |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 8114 | int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
| 8115 | unsigned long start, unsigned long end, |
| 8116 | bool blockable) |
Radim Krčmář | b1394e7 | 2017-11-30 19:05:45 +0100 | [diff] [blame] | 8117 | { |
| 8118 | unsigned long apic_address; |
| 8119 | |
| 8120 | /* |
| 8121 | * The physical address of apic access page is stored in the VMCS. |
| 8122 | * Update it when it becomes invalid. |
| 8123 | */ |
| 8124 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
| 8125 | if (start <= apic_address && apic_address < end) |
| 8126 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 8127 | |
| 8128 | return 0; |
Radim Krčmář | b1394e7 | 2017-11-30 19:05:45 +0100 | [diff] [blame] | 8129 | } |
| 8130 | |
Tang Chen | 4256f43 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8131 | void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
| 8132 | { |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 8133 | struct page *page = NULL; |
| 8134 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8135 | if (!lapic_in_kernel(vcpu)) |
Paolo Bonzini | f439ed2 | 2014-10-02 13:53:24 +0200 | [diff] [blame] | 8136 | return; |
| 8137 | |
Tang Chen | 4256f43 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8138 | if (!kvm_x86_ops->set_apic_access_page_addr) |
| 8139 | return; |
| 8140 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 8141 | page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Andrea Arcangeli | e8fd5e9 | 2015-05-08 14:32:56 +0200 | [diff] [blame] | 8142 | if (is_error_page(page)) |
| 8143 | return; |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 8144 | kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page)); |
| 8145 | |
| 8146 | /* |
| 8147 | * Do not pin apic access page in memory, the MMU notifier |
| 8148 | * will call us again if it is migrated or swapped out. |
| 8149 | */ |
| 8150 | put_page(page); |
Tang Chen | 4256f43 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8151 | } |
Tang Chen | 4256f43 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8152 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 8153 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
| 8154 | { |
| 8155 | smp_send_reschedule(vcpu->cpu); |
| 8156 | } |
| 8157 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); |
| 8158 | |
Takuya Yoshikawa | 9357d93 | 2013-12-13 15:08:38 +0900 | [diff] [blame] | 8159 | /* |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8160 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
Takuya Yoshikawa | 9357d93 | 2013-12-13 15:08:38 +0900 | [diff] [blame] | 8161 | * exiting to the userspace. Otherwise, the value will be returned to the |
| 8162 | * userspace. |
| 8163 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8164 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8165 | { |
| 8166 | int r; |
Matt Gingell | 62a193e | 2015-11-16 15:26:07 -0800 | [diff] [blame] | 8167 | bool req_int_win = |
| 8168 | dm_request_for_irq_injection(vcpu) && |
| 8169 | kvm_cpu_accept_dm_intr(vcpu); |
Wanpeng Li | 1e9e262 | 2019-11-21 11:17:11 +0800 | [diff] [blame] | 8170 | enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE; |
Matt Gingell | 62a193e | 2015-11-16 15:26:07 -0800 | [diff] [blame] | 8171 | |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 8172 | bool req_immediate_exit = false; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8173 | |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 8174 | if (kvm_request_pending(vcpu)) { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 8175 | if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) { |
| 8176 | if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) { |
| 8177 | r = 0; |
| 8178 | goto out; |
| 8179 | } |
| 8180 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8181 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 8182 | kvm_mmu_unload(vcpu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8183 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 8184 | __kvm_migrate_timers(vcpu); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 8185 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
| 8186 | kvm_gen_update_masterclock(vcpu->kvm); |
Marcelo Tosatti | 0061d53d | 2013-05-09 20:21:41 -0300 | [diff] [blame] | 8187 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
| 8188 | kvm_gen_kvmclock_update(vcpu); |
Zachary Amsden | 34c238a | 2010-09-18 14:38:14 -1000 | [diff] [blame] | 8189 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
| 8190 | r = kvm_guest_time_update(vcpu); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 8191 | if (unlikely(r)) |
| 8192 | goto out; |
| 8193 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8194 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 8195 | kvm_mmu_sync_roots(vcpu); |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 8196 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
| 8197 | kvm_mmu_load_pgd(vcpu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8198 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 8199 | kvm_vcpu_flush_tlb(vcpu, true); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8200 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8201 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 8202 | r = 0; |
| 8203 | goto out; |
| 8204 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 8205 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8206 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 8207 | vcpu->mmio_needed = 0; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 8208 | r = 0; |
| 8209 | goto out; |
| 8210 | } |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 8211 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
| 8212 | /* Page is swapped out. Do synthetic halt */ |
| 8213 | vcpu->arch.apf.halted = true; |
| 8214 | r = 1; |
| 8215 | goto out; |
| 8216 | } |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 8217 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
| 8218 | record_steal_time(vcpu); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 8219 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
| 8220 | process_smi(vcpu); |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 8221 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
| 8222 | process_nmi(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 8223 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 8224 | kvm_pmu_handle_event(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 8225 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 8226 | kvm_pmu_deliver_pmi(vcpu); |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 8227 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
| 8228 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); |
| 8229 | if (test_bit(vcpu->arch.pending_ioapic_eoi, |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8230 | vcpu->arch.ioapic_handled_vectors)) { |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 8231 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
| 8232 | vcpu->run->eoi.vector = |
| 8233 | vcpu->arch.pending_ioapic_eoi; |
| 8234 | r = 0; |
| 8235 | goto out; |
| 8236 | } |
| 8237 | } |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8238 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
| 8239 | vcpu_scan_ioapic(vcpu); |
Liran Alon | e40ff1d | 2018-03-21 02:50:31 +0200 | [diff] [blame] | 8240 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
| 8241 | vcpu_load_eoi_exitmap(vcpu); |
Tang Chen | 4256f43 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8242 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
| 8243 | kvm_vcpu_reload_apic_access_page(vcpu); |
Andrey Smetanin | 2ce7918 | 2015-07-03 15:01:41 +0300 | [diff] [blame] | 8244 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
| 8245 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 8246 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; |
| 8247 | r = 0; |
| 8248 | goto out; |
| 8249 | } |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 8250 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
| 8251 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 8252 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; |
| 8253 | r = 0; |
| 8254 | goto out; |
| 8255 | } |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 8256 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
| 8257 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 8258 | vcpu->run->hyperv = vcpu->arch.hyperv.exit; |
| 8259 | r = 0; |
| 8260 | goto out; |
| 8261 | } |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 8262 | |
| 8263 | /* |
| 8264 | * KVM_REQ_HV_STIMER has to be processed after |
| 8265 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers |
| 8266 | * depend on the guest clock being up-to-date |
| 8267 | */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 8268 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
| 8269 | kvm_hv_process_stimers(vcpu); |
Suravee Suthikulpanit | 8df14af | 2019-11-14 14:15:06 -0600 | [diff] [blame] | 8270 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
| 8271 | kvm_vcpu_update_apicv(vcpu); |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 8272 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 8273 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8274 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { |
Paolo Bonzini | 0f1e261 | 2016-12-17 16:05:19 +0100 | [diff] [blame] | 8275 | ++vcpu->stat.req_event; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8276 | kvm_apic_accept_events(vcpu); |
| 8277 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { |
| 8278 | r = 1; |
| 8279 | goto out; |
| 8280 | } |
| 8281 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 8282 | if (inject_pending_event(vcpu) != 0) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 8283 | req_immediate_exit = true; |
Yuki Shibuya | 321c565 | 2016-03-24 05:17:03 +0000 | [diff] [blame] | 8284 | else { |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 8285 | /* Enable SMI/NMI/IRQ window open exits if needed. |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 8286 | * |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 8287 | * SMIs have three cases: |
| 8288 | * 1) They can be nested, and then there is nothing to |
| 8289 | * do here because RSM will cause a vmexit anyway. |
| 8290 | * 2) There is an ISA-specific reason why SMI cannot be |
| 8291 | * injected, and the moment when this changes can be |
| 8292 | * intercepted. |
| 8293 | * 3) Or the SMI can be pending because |
| 8294 | * inject_pending_event has completed the injection |
| 8295 | * of an IRQ or NMI from the previous vmexit, and |
| 8296 | * then we request an immediate exit to inject the |
| 8297 | * SMI. |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 8298 | */ |
| 8299 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 8300 | if (!kvm_x86_ops->enable_smi_window(vcpu)) |
| 8301 | req_immediate_exit = true; |
Yuki Shibuya | 321c565 | 2016-03-24 05:17:03 +0000 | [diff] [blame] | 8302 | if (vcpu->arch.nmi_pending) |
| 8303 | kvm_x86_ops->enable_nmi_window(vcpu); |
| 8304 | if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win) |
| 8305 | kvm_x86_ops->enable_irq_window(vcpu); |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 8306 | WARN_ON(vcpu->arch.exception.pending); |
Yuki Shibuya | 321c565 | 2016-03-24 05:17:03 +0000 | [diff] [blame] | 8307 | } |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 8308 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8309 | if (kvm_lapic_enabled(vcpu)) { |
| 8310 | update_cr8_intercept(vcpu); |
| 8311 | kvm_lapic_sync_to_vapic(vcpu); |
| 8312 | } |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8313 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 8314 | |
Avi Kivity | d8368af | 2012-05-14 18:07:56 +0300 | [diff] [blame] | 8315 | r = kvm_mmu_reload(vcpu); |
| 8316 | if (unlikely(r)) { |
Michael S. Tsirkin | d905c06 | 2012-06-24 19:25:00 +0300 | [diff] [blame] | 8317 | goto cancel_injection; |
Avi Kivity | d8368af | 2012-05-14 18:07:56 +0300 | [diff] [blame] | 8318 | } |
| 8319 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 8320 | preempt_disable(); |
| 8321 | |
| 8322 | kvm_x86_ops->prepare_guest_switch(vcpu); |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 8323 | |
| 8324 | /* |
| 8325 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt |
| 8326 | * IPI are then delayed after guest entry, which ensures that they |
| 8327 | * result in virtual interrupt delivery. |
| 8328 | */ |
| 8329 | local_irq_disable(); |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 8330 | vcpu->mode = IN_GUEST_MODE; |
| 8331 | |
Michael S. Tsirkin | 01b7191 | 2013-11-04 22:36:25 +0200 | [diff] [blame] | 8332 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 8333 | |
Lan Tianyu | 0f127d1 | 2016-03-13 11:10:29 +0800 | [diff] [blame] | 8334 | /* |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 8335 | * 1) We should set ->mode before checking ->requests. Please see |
Andrew Jones | cde9af6 | 2017-04-26 22:32:24 +0200 | [diff] [blame] | 8336 | * the comment in kvm_vcpu_exiting_guest_mode(). |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 8337 | * |
Luwei Kang | 81b0166 | 2019-01-31 16:52:02 +0800 | [diff] [blame] | 8338 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 8339 | * pairs with the memory barrier implicit in pi_test_and_set_on |
| 8340 | * (see vmx_deliver_posted_interrupt). |
| 8341 | * |
| 8342 | * 3) This also orders the write to mode from any reads to the page |
| 8343 | * tables done while the VCPU is running. Please see the comment |
| 8344 | * in kvm_flush_remote_tlbs. |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 8345 | */ |
Michael S. Tsirkin | 01b7191 | 2013-11-04 22:36:25 +0200 | [diff] [blame] | 8346 | smp_mb__after_srcu_read_unlock(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8347 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 8348 | /* |
| 8349 | * This handles the case where a posted interrupt was |
| 8350 | * notified with kvm_vcpu_kick. |
| 8351 | */ |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 8352 | if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active) |
| 8353 | kvm_x86_ops->sync_pir_to_irr(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8354 | |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 8355 | if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8356 | || need_resched() || signal_pending(current)) { |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 8357 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8358 | smp_wmb(); |
| 8359 | local_irq_enable(); |
| 8360 | preempt_enable(); |
Michael S. Tsirkin | 01b7191 | 2013-11-04 22:36:25 +0200 | [diff] [blame] | 8361 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8362 | r = 1; |
Michael S. Tsirkin | d905c06 | 2012-06-24 19:25:00 +0300 | [diff] [blame] | 8363 | goto cancel_injection; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8364 | } |
| 8365 | |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 8366 | if (req_immediate_exit) { |
| 8367 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 8368 | kvm_x86_ops->request_immediate_exit(vcpu); |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 8369 | } |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 8370 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8371 | trace_kvm_entry(vcpu->vcpu_id); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 8372 | guest_enter_irqoff(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8373 | |
Sean Christopherson | 2620fe2 | 2020-01-17 11:30:51 -0800 | [diff] [blame] | 8374 | fpregs_assert_state_consistent(); |
| 8375 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) |
| 8376 | switch_fpu_return(); |
Rik van Riel | 5f409e2 | 2019-04-03 18:41:52 +0200 | [diff] [blame] | 8377 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8378 | if (unlikely(vcpu->arch.switch_db_regs)) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8379 | set_debugreg(0, 7); |
| 8380 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 8381 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 8382 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 8383 | set_debugreg(vcpu->arch.eff_db[3], 3); |
Paolo Bonzini | c77fb5f | 2014-02-21 10:17:24 +0100 | [diff] [blame] | 8384 | set_debugreg(vcpu->arch.dr6, 6); |
Nadav Amit | ae561ed | 2015-04-02 03:10:37 +0300 | [diff] [blame] | 8385 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8386 | } |
| 8387 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8388 | kvm_x86_ops->run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8389 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 8390 | /* |
Paolo Bonzini | c77fb5f | 2014-02-21 10:17:24 +0100 | [diff] [blame] | 8391 | * Do this here before restoring debug registers on the host. And |
| 8392 | * since we do this before handling the vmexit, a DR access vmexit |
| 8393 | * can (a) read the correct value of the debug registers, (b) set |
| 8394 | * KVM_DEBUGREG_WONT_EXIT again. |
| 8395 | */ |
| 8396 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { |
Paolo Bonzini | c77fb5f | 2014-02-21 10:17:24 +0100 | [diff] [blame] | 8397 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
| 8398 | kvm_x86_ops->sync_dirty_debug_regs(vcpu); |
Paolo Bonzini | 70e4da7 | 2016-02-26 12:28:40 +0100 | [diff] [blame] | 8399 | kvm_update_dr0123(vcpu); |
| 8400 | kvm_update_dr6(vcpu); |
| 8401 | kvm_update_dr7(vcpu); |
| 8402 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
Paolo Bonzini | c77fb5f | 2014-02-21 10:17:24 +0100 | [diff] [blame] | 8403 | } |
| 8404 | |
| 8405 | /* |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 8406 | * If the guest has used debug registers, at least dr7 |
| 8407 | * will be disabled while returning to the host. |
| 8408 | * If we don't have active breakpoints in the host, we don't |
| 8409 | * care about the messed up debug address registers. But if |
| 8410 | * we have some of them active, restore the old state. |
| 8411 | */ |
Frederic Weisbecker | 59d8eb5 | 2009-11-10 11:03:12 +0100 | [diff] [blame] | 8412 | if (hw_breakpoint_active()) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 8413 | hw_breakpoint_restore(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8414 | |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 8415 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
Zachary Amsden | 1d5f066 | 2010-08-19 22:07:30 -1000 | [diff] [blame] | 8416 | |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 8417 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Avi Kivity | d94e1dc | 2010-05-03 16:54:48 +0300 | [diff] [blame] | 8418 | smp_wmb(); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8419 | |
Wanpeng Li | 1e9e262 | 2019-11-21 11:17:11 +0800 | [diff] [blame] | 8420 | kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8421 | |
Sean Christopherson | d7a0888 | 2019-07-10 09:07:34 -0700 | [diff] [blame] | 8422 | /* |
| 8423 | * Consume any pending interrupts, including the possible source of |
| 8424 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. |
| 8425 | * An instruction is required after local_irq_enable() to fully unblock |
| 8426 | * interrupts on processors that implement an interrupt shadow, the |
| 8427 | * stat.exits increment will do nicely. |
| 8428 | */ |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8429 | kvm_before_interrupt(vcpu); |
Sean Christopherson | d7a0888 | 2019-07-10 09:07:34 -0700 | [diff] [blame] | 8430 | local_irq_enable(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8431 | ++vcpu->stat.exits; |
Sean Christopherson | d7a0888 | 2019-07-10 09:07:34 -0700 | [diff] [blame] | 8432 | local_irq_disable(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8433 | kvm_after_interrupt(vcpu); |
| 8434 | |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8435 | guest_exit_irqoff(); |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 8436 | if (lapic_in_kernel(vcpu)) { |
| 8437 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; |
| 8438 | if (delta != S64_MIN) { |
| 8439 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); |
| 8440 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; |
| 8441 | } |
| 8442 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8443 | |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8444 | local_irq_enable(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8445 | preempt_enable(); |
| 8446 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8447 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 8448 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8449 | /* |
| 8450 | * Profile KVM exit RIPs: |
| 8451 | */ |
| 8452 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 8453 | unsigned long rip = kvm_rip_read(vcpu); |
| 8454 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8455 | } |
| 8456 | |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 8457 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
| 8458 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 8459 | |
Michael S. Tsirkin | 5cfb1d5 | 2012-06-24 19:24:54 +0300 | [diff] [blame] | 8460 | if (vcpu->arch.apic_attention) |
| 8461 | kvm_lapic_sync_from_vapic(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 8462 | |
Wanpeng Li | 1e9e262 | 2019-11-21 11:17:11 +0800 | [diff] [blame] | 8463 | r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath); |
Michael S. Tsirkin | d905c06 | 2012-06-24 19:25:00 +0300 | [diff] [blame] | 8464 | return r; |
| 8465 | |
| 8466 | cancel_injection: |
| 8467 | kvm_x86_ops->cancel_injection(vcpu); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 8468 | if (unlikely(vcpu->arch.apic_attention)) |
| 8469 | kvm_lapic_sync_from_vapic(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8470 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 8471 | return r; |
| 8472 | } |
| 8473 | |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8474 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
| 8475 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8476 | if (!kvm_arch_vcpu_runnable(vcpu) && |
| 8477 | (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) { |
Paolo Bonzini | 9c8fd1b | 2015-02-06 12:58:42 +0100 | [diff] [blame] | 8478 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
| 8479 | kvm_vcpu_block(vcpu); |
| 8480 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8481 | |
| 8482 | if (kvm_x86_ops->post_block) |
| 8483 | kvm_x86_ops->post_block(vcpu); |
| 8484 | |
Paolo Bonzini | 9c8fd1b | 2015-02-06 12:58:42 +0100 | [diff] [blame] | 8485 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
| 8486 | return 1; |
| 8487 | } |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8488 | |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8489 | kvm_apic_accept_events(vcpu); |
| 8490 | switch(vcpu->arch.mp_state) { |
| 8491 | case KVM_MP_STATE_HALTED: |
| 8492 | vcpu->arch.pv.pv_unhalted = false; |
| 8493 | vcpu->arch.mp_state = |
| 8494 | KVM_MP_STATE_RUNNABLE; |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 8495 | /* fall through */ |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8496 | case KVM_MP_STATE_RUNNABLE: |
| 8497 | vcpu->arch.apf.halted = false; |
| 8498 | break; |
| 8499 | case KVM_MP_STATE_INIT_RECEIVED: |
| 8500 | break; |
| 8501 | default: |
| 8502 | return -EINTR; |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8503 | } |
| 8504 | return 1; |
| 8505 | } |
| 8506 | |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 8507 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
| 8508 | { |
Paolo Bonzini | 0ad3bed | 2016-12-19 15:23:54 +0100 | [diff] [blame] | 8509 | if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 8510 | kvm_x86_ops->check_nested_events(vcpu); |
Paolo Bonzini | 0ad3bed | 2016-12-19 15:23:54 +0100 | [diff] [blame] | 8511 | |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 8512 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
| 8513 | !vcpu->arch.apf.halted); |
| 8514 | } |
| 8515 | |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8516 | static int vcpu_run(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 8517 | { |
| 8518 | int r; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8519 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 8520 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8521 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 8522 | vcpu->arch.l1tf_flush_l1d = true; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 8523 | |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8524 | for (;;) { |
Paolo Bonzini | 58f800d | 2015-10-13 21:32:50 +0200 | [diff] [blame] | 8525 | if (kvm_vcpu_running(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8526 | r = vcpu_enter_guest(vcpu); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8527 | } else { |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8528 | r = vcpu_block(kvm, vcpu); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8529 | } |
| 8530 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8531 | if (r <= 0) |
| 8532 | break; |
| 8533 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 8534 | kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu); |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8535 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 8536 | kvm_inject_pending_timer_irqs(vcpu); |
| 8537 | |
Matt Gingell | 782d422 | 2015-11-16 15:26:00 -0800 | [diff] [blame] | 8538 | if (dm_request_for_irq_injection(vcpu) && |
| 8539 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { |
Paolo Bonzini | 4ca7dd8 | 2015-07-30 10:32:16 +0200 | [diff] [blame] | 8540 | r = 0; |
| 8541 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8542 | ++vcpu->stat.request_irq_exits; |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8543 | break; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8544 | } |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 8545 | |
| 8546 | kvm_check_async_pf_completion(vcpu); |
| 8547 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8548 | if (signal_pending(current)) { |
| 8549 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8550 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8551 | ++vcpu->stat.signal_exits; |
Paolo Bonzini | 362c698 | 2015-02-06 12:48:04 +0100 | [diff] [blame] | 8552 | break; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 8553 | } |
| 8554 | if (need_resched()) { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8555 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Takuya Yoshikawa | c08ac06 | 2013-12-13 15:07:21 +0900 | [diff] [blame] | 8556 | cond_resched(); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8557 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 8561 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8562 | |
| 8563 | return r; |
| 8564 | } |
| 8565 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8566 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
| 8567 | { |
| 8568 | int r; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 8569 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8570 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 8571 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8572 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 8573 | return r; |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8574 | } |
| 8575 | |
| 8576 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) |
| 8577 | { |
| 8578 | BUG_ON(!vcpu->arch.pio.count); |
| 8579 | |
| 8580 | return complete_emulated_io(vcpu); |
| 8581 | } |
| 8582 | |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 8583 | /* |
| 8584 | * Implements the following, as a state machine: |
| 8585 | * |
| 8586 | * read: |
| 8587 | * for each fragment |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8588 | * for each mmio piece in the fragment |
| 8589 | * write gpa, len |
| 8590 | * exit |
| 8591 | * copy data |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 8592 | * execute insn |
| 8593 | * |
| 8594 | * write: |
| 8595 | * for each fragment |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8596 | * for each mmio piece in the fragment |
| 8597 | * write gpa, len |
| 8598 | * copy data |
| 8599 | * exit |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 8600 | */ |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8601 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8602 | { |
| 8603 | struct kvm_run *run = vcpu->run; |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 8604 | struct kvm_mmio_fragment *frag; |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8605 | unsigned len; |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8606 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8607 | BUG_ON(!vcpu->mmio_needed); |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8608 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8609 | /* Complete previous fragment */ |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8610 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
| 8611 | len = min(8u, frag->len); |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8612 | if (!vcpu->mmio_is_write) |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8613 | memcpy(frag->data, run->mmio.data, len); |
| 8614 | |
| 8615 | if (frag->len <= 8) { |
| 8616 | /* Switch to the next fragment. */ |
| 8617 | frag++; |
| 8618 | vcpu->mmio_cur_fragment++; |
| 8619 | } else { |
| 8620 | /* Go forward to the next mmio piece. */ |
| 8621 | frag->data += len; |
| 8622 | frag->gpa += len; |
| 8623 | frag->len -= len; |
| 8624 | } |
| 8625 | |
Andrew Honig | a08d3b3 | 2014-02-27 19:35:14 +0100 | [diff] [blame] | 8626 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8627 | vcpu->mmio_needed = 0; |
Paolo Bonzini | 0912c97 | 2013-08-27 15:41:43 +0200 | [diff] [blame] | 8628 | |
| 8629 | /* FIXME: return into emulator if single-stepping. */ |
Avi Kivity | cef4dea | 2010-01-20 12:01:20 +0200 | [diff] [blame] | 8630 | if (vcpu->mmio_is_write) |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8631 | return 1; |
| 8632 | vcpu->mmio_read_completed = 1; |
| 8633 | return complete_emulated_io(vcpu); |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8634 | } |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8635 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8636 | run->exit_reason = KVM_EXIT_MMIO; |
| 8637 | run->mmio.phys_addr = frag->gpa; |
| 8638 | if (vcpu->mmio_is_write) |
Xiao Guangrong | 87da7e6 | 2012-10-24 14:07:59 +0800 | [diff] [blame] | 8639 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
| 8640 | run->mmio.len = min(8u, frag->len); |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8641 | run->mmio.is_write = vcpu->mmio_is_write; |
| 8642 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
| 8643 | return 0; |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8644 | } |
| 8645 | |
Sean Christopherson | c9aef3b | 2020-01-17 11:30:49 -0800 | [diff] [blame] | 8646 | static void kvm_save_current_fpu(struct fpu *fpu) |
| 8647 | { |
| 8648 | /* |
| 8649 | * If the target FPU state is not resident in the CPU registers, just |
| 8650 | * memcpy() from current, else save CPU state directly to the target. |
| 8651 | */ |
| 8652 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) |
| 8653 | memcpy(&fpu->state, ¤t->thread.fpu.state, |
| 8654 | fpu_kernel_xstate_size); |
| 8655 | else |
| 8656 | copy_fpregs_to_fpstate(fpu); |
| 8657 | } |
| 8658 | |
Sebastian Andrzej Siewior | 822f312 | 2018-09-12 15:33:45 +0200 | [diff] [blame] | 8659 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
| 8660 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 8661 | { |
Rik van Riel | 5f409e2 | 2019-04-03 18:41:52 +0200 | [diff] [blame] | 8662 | fpregs_lock(); |
| 8663 | |
Sean Christopherson | c9aef3b | 2020-01-17 11:30:49 -0800 | [diff] [blame] | 8664 | kvm_save_current_fpu(vcpu->arch.user_fpu); |
| 8665 | |
Sebastian Andrzej Siewior | 822f312 | 2018-09-12 15:33:45 +0200 | [diff] [blame] | 8666 | /* PKRU is separately restored in kvm_x86_ops->run. */ |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 8667 | __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, |
Sebastian Andrzej Siewior | 822f312 | 2018-09-12 15:33:45 +0200 | [diff] [blame] | 8668 | ~XFEATURE_MASK_PKRU); |
Rik van Riel | 5f409e2 | 2019-04-03 18:41:52 +0200 | [diff] [blame] | 8669 | |
| 8670 | fpregs_mark_activate(); |
| 8671 | fpregs_unlock(); |
| 8672 | |
Sebastian Andrzej Siewior | 822f312 | 2018-09-12 15:33:45 +0200 | [diff] [blame] | 8673 | trace_kvm_fpu(1); |
| 8674 | } |
| 8675 | |
| 8676 | /* When vcpu_run ends, restore user space FPU context. */ |
| 8677 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 8678 | { |
Rik van Riel | 5f409e2 | 2019-04-03 18:41:52 +0200 | [diff] [blame] | 8679 | fpregs_lock(); |
| 8680 | |
Sean Christopherson | c9aef3b | 2020-01-17 11:30:49 -0800 | [diff] [blame] | 8681 | kvm_save_current_fpu(vcpu->arch.guest_fpu); |
| 8682 | |
Wanpeng Li | d9a710e | 2019-07-22 12:26:21 +0800 | [diff] [blame] | 8683 | copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state); |
Rik van Riel | 5f409e2 | 2019-04-03 18:41:52 +0200 | [diff] [blame] | 8684 | |
| 8685 | fpregs_mark_activate(); |
| 8686 | fpregs_unlock(); |
| 8687 | |
Sebastian Andrzej Siewior | 822f312 | 2018-09-12 15:33:45 +0200 | [diff] [blame] | 8688 | ++vcpu->stat.fpu_reload; |
| 8689 | trace_kvm_fpu(0); |
| 8690 | } |
| 8691 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8692 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 8693 | { |
| 8694 | int r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8695 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 8696 | vcpu_load(vcpu); |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 8697 | kvm_sigset_activate(vcpu); |
Peter Xu | 5663d8f | 2017-12-12 17:15:02 +0100 | [diff] [blame] | 8698 | kvm_load_guest_fpu(vcpu); |
| 8699 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 8700 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
Jan H. Schönherr | 2f173d2 | 2017-09-06 18:34:06 +0200 | [diff] [blame] | 8701 | if (kvm_run->immediate_exit) { |
| 8702 | r = -EINTR; |
| 8703 | goto out; |
| 8704 | } |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 8705 | kvm_vcpu_block(vcpu); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8706 | kvm_apic_accept_events(vcpu); |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 8707 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 8708 | r = -EAGAIN; |
Jan H. Schönherr | a059500 | 2017-09-06 00:27:19 +0200 | [diff] [blame] | 8709 | if (signal_pending(current)) { |
| 8710 | r = -EINTR; |
| 8711 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
| 8712 | ++vcpu->stat.signal_exits; |
| 8713 | } |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 8714 | goto out; |
| 8715 | } |
| 8716 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8717 | if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) { |
| 8718 | r = -EINVAL; |
| 8719 | goto out; |
| 8720 | } |
| 8721 | |
| 8722 | if (vcpu->run->kvm_dirty_regs) { |
| 8723 | r = sync_regs(vcpu); |
| 8724 | if (r != 0) |
| 8725 | goto out; |
| 8726 | } |
| 8727 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8728 | /* re-sync apic's tpr */ |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8729 | if (!lapic_in_kernel(vcpu)) { |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 8730 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
| 8731 | r = -EINVAL; |
| 8732 | goto out; |
| 8733 | } |
| 8734 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8735 | |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8736 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
| 8737 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; |
| 8738 | vcpu->arch.complete_userspace_io = NULL; |
| 8739 | r = cui(vcpu); |
| 8740 | if (r <= 0) |
Peter Xu | 5663d8f | 2017-12-12 17:15:02 +0100 | [diff] [blame] | 8741 | goto out; |
Gleb Natapov | 716d51a | 2012-09-03 15:24:26 +0300 | [diff] [blame] | 8742 | } else |
| 8743 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); |
Avi Kivity | 5287f19 | 2010-01-19 14:20:10 +0200 | [diff] [blame] | 8744 | |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 8745 | if (kvm_run->immediate_exit) |
| 8746 | r = -EINTR; |
| 8747 | else |
| 8748 | r = vcpu_run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8749 | |
| 8750 | out: |
Peter Xu | 5663d8f | 2017-12-12 17:15:02 +0100 | [diff] [blame] | 8751 | kvm_put_guest_fpu(vcpu); |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8752 | if (vcpu->run->kvm_valid_regs) |
| 8753 | store_regs(vcpu); |
Marcelo Tosatti | f1d86e4 | 2010-05-03 23:04:27 -0300 | [diff] [blame] | 8754 | post_kvm_run_save(vcpu); |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 8755 | kvm_sigset_deactivate(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8756 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 8757 | vcpu_put(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8758 | return r; |
| 8759 | } |
| 8760 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8761 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8762 | { |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 8763 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
| 8764 | /* |
| 8765 | * We are here if userspace calls get_regs() in the middle of |
| 8766 | * instruction emulation. Registers state needs to be copied |
Guo Chao | 4a96998 | 2012-06-28 15:17:27 +0800 | [diff] [blame] | 8767 | * back from emulation context to vcpu. Userspace shouldn't do |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 8768 | * that usually, but some bad designed PV devices (vmware |
| 8769 | * backdoor interface) need this to work |
| 8770 | */ |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 8771 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 8772 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
| 8773 | } |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8774 | regs->rax = kvm_rax_read(vcpu); |
| 8775 | regs->rbx = kvm_rbx_read(vcpu); |
| 8776 | regs->rcx = kvm_rcx_read(vcpu); |
| 8777 | regs->rdx = kvm_rdx_read(vcpu); |
| 8778 | regs->rsi = kvm_rsi_read(vcpu); |
| 8779 | regs->rdi = kvm_rdi_read(vcpu); |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 8780 | regs->rsp = kvm_rsp_read(vcpu); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8781 | regs->rbp = kvm_rbp_read(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8782 | #ifdef CONFIG_X86_64 |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8783 | regs->r8 = kvm_r8_read(vcpu); |
| 8784 | regs->r9 = kvm_r9_read(vcpu); |
| 8785 | regs->r10 = kvm_r10_read(vcpu); |
| 8786 | regs->r11 = kvm_r11_read(vcpu); |
| 8787 | regs->r12 = kvm_r12_read(vcpu); |
| 8788 | regs->r13 = kvm_r13_read(vcpu); |
| 8789 | regs->r14 = kvm_r14_read(vcpu); |
| 8790 | regs->r15 = kvm_r15_read(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8791 | #endif |
| 8792 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 8793 | regs->rip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 8794 | regs->rflags = kvm_get_rflags(vcpu); |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8795 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8796 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8797 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 8798 | { |
| 8799 | vcpu_load(vcpu); |
| 8800 | __get_regs(vcpu, regs); |
Christoffer Dall | 1fc9b76 | 2017-12-04 21:35:26 +0100 | [diff] [blame] | 8801 | vcpu_put(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8802 | return 0; |
| 8803 | } |
| 8804 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8805 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8806 | { |
Gleb Natapov | 7ae441e | 2011-03-31 12:06:41 +0200 | [diff] [blame] | 8807 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
| 8808 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
| 8809 | |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8810 | kvm_rax_write(vcpu, regs->rax); |
| 8811 | kvm_rbx_write(vcpu, regs->rbx); |
| 8812 | kvm_rcx_write(vcpu, regs->rcx); |
| 8813 | kvm_rdx_write(vcpu, regs->rdx); |
| 8814 | kvm_rsi_write(vcpu, regs->rsi); |
| 8815 | kvm_rdi_write(vcpu, regs->rdi); |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 8816 | kvm_rsp_write(vcpu, regs->rsp); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8817 | kvm_rbp_write(vcpu, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8818 | #ifdef CONFIG_X86_64 |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 8819 | kvm_r8_write(vcpu, regs->r8); |
| 8820 | kvm_r9_write(vcpu, regs->r9); |
| 8821 | kvm_r10_write(vcpu, regs->r10); |
| 8822 | kvm_r11_write(vcpu, regs->r11); |
| 8823 | kvm_r12_write(vcpu, regs->r12); |
| 8824 | kvm_r13_write(vcpu, regs->r13); |
| 8825 | kvm_r14_write(vcpu, regs->r14); |
| 8826 | kvm_r15_write(vcpu, regs->r15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8827 | #endif |
| 8828 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 8829 | kvm_rip_write(vcpu, regs->rip); |
Wanpeng Li | d73235d | 2017-12-07 00:30:08 -0800 | [diff] [blame] | 8830 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8831 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 8832 | vcpu->arch.exception.pending = false; |
| 8833 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8834 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8835 | } |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8836 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8837 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 8838 | { |
| 8839 | vcpu_load(vcpu); |
| 8840 | __set_regs(vcpu, regs); |
Christoffer Dall | 875656f | 2017-12-04 21:35:27 +0100 | [diff] [blame] | 8841 | vcpu_put(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8842 | return 0; |
| 8843 | } |
| 8844 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8845 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 8846 | { |
| 8847 | struct kvm_segment cs; |
| 8848 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 8849 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8850 | *db = cs.db; |
| 8851 | *l = cs.l; |
| 8852 | } |
| 8853 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 8854 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8855 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8856 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 8857 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8858 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 8859 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 8860 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 8861 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 8862 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 8863 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 8864 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8865 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 8866 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 8867 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8868 | |
| 8869 | kvm_x86_ops->get_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 8870 | sregs->idt.limit = dt.size; |
| 8871 | sregs->idt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8872 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 8873 | sregs->gdt.limit = dt.size; |
| 8874 | sregs->gdt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8875 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 8876 | sregs->cr0 = kvm_read_cr0(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8877 | sregs->cr2 = vcpu->arch.cr2; |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 8878 | sregs->cr3 = kvm_read_cr3(vcpu); |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 8879 | sregs->cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 8880 | sregs->cr8 = kvm_get_cr8(vcpu); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 8881 | sregs->efer = vcpu->arch.efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8882 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 8883 | |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 8884 | memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8885 | |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 8886 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
Gleb Natapov | 14d0bc1 | 2009-04-21 17:45:11 +0300 | [diff] [blame] | 8887 | set_bit(vcpu->arch.interrupt.nr, |
| 8888 | (unsigned long *)sregs->interrupt_bitmap); |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8889 | } |
Gleb Natapov | 16d7a19 | 2009-04-21 17:45:10 +0300 | [diff] [blame] | 8890 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 8891 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 8892 | struct kvm_sregs *sregs) |
| 8893 | { |
| 8894 | vcpu_load(vcpu); |
| 8895 | __get_sregs(vcpu, sregs); |
Christoffer Dall | bcdec41 | 2017-12-04 21:35:28 +0100 | [diff] [blame] | 8896 | vcpu_put(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8897 | return 0; |
| 8898 | } |
| 8899 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 8900 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 8901 | struct kvm_mp_state *mp_state) |
| 8902 | { |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 8903 | vcpu_load(vcpu); |
Sean Christopherson | f958bd2 | 2019-12-09 12:19:31 -0800 | [diff] [blame] | 8904 | if (kvm_mpx_supported()) |
| 8905 | kvm_load_guest_fpu(vcpu); |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 8906 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8907 | kvm_apic_accept_events(vcpu); |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 8908 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED && |
| 8909 | vcpu->arch.pv.pv_unhalted) |
| 8910 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; |
| 8911 | else |
| 8912 | mp_state->mp_state = vcpu->arch.mp_state; |
| 8913 | |
Sean Christopherson | f958bd2 | 2019-12-09 12:19:31 -0800 | [diff] [blame] | 8914 | if (kvm_mpx_supported()) |
| 8915 | kvm_put_guest_fpu(vcpu); |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 8916 | vcpu_put(vcpu); |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 8917 | return 0; |
| 8918 | } |
| 8919 | |
| 8920 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 8921 | struct kvm_mp_state *mp_state) |
| 8922 | { |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 8923 | int ret = -EINVAL; |
| 8924 | |
| 8925 | vcpu_load(vcpu); |
| 8926 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 8927 | if (!lapic_in_kernel(vcpu) && |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8928 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 8929 | goto out; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8930 | |
Liran Alon | 27cbe7d | 2019-11-11 11:16:40 +0200 | [diff] [blame] | 8931 | /* |
| 8932 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in |
| 8933 | * INIT state; latched init should be reported using |
| 8934 | * KVM_SET_VCPU_EVENTS, so reject it here. |
| 8935 | */ |
| 8936 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && |
David Hildenbrand | 28bf288 | 2017-03-23 11:46:03 +0100 | [diff] [blame] | 8937 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
| 8938 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 8939 | goto out; |
David Hildenbrand | 28bf288 | 2017-03-23 11:46:03 +0100 | [diff] [blame] | 8940 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 8941 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
| 8942 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 8943 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); |
| 8944 | } else |
| 8945 | vcpu->arch.mp_state = mp_state->mp_state; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8946 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 8947 | |
| 8948 | ret = 0; |
| 8949 | out: |
| 8950 | vcpu_put(vcpu); |
| 8951 | return ret; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 8952 | } |
| 8953 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 8954 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
| 8955 | int reason, bool has_error_code, u32 error_code) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8956 | { |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 8957 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 8958 | int ret; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 8959 | |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 8960 | init_emulate_ctxt(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 8961 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 8962 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 8963 | has_error_code, error_code); |
Sean Christopherson | 1051778 | 2019-08-27 14:40:35 -0700 | [diff] [blame] | 8964 | if (ret) { |
| 8965 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8966 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 8967 | vcpu->run->internal.ndata = 0; |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 8968 | return 0; |
Sean Christopherson | 1051778 | 2019-08-27 14:40:35 -0700 | [diff] [blame] | 8969 | } |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 8970 | |
Takuya Yoshikawa | 9d74191 | 2011-05-29 21:53:48 +0900 | [diff] [blame] | 8971 | kvm_rip_write(vcpu, ctxt->eip); |
| 8972 | kvm_set_rflags(vcpu, ctxt->eflags); |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 8973 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 8974 | } |
| 8975 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 8976 | |
Peng Hao | 3140c15 | 2018-04-02 09:15:32 +0800 | [diff] [blame] | 8977 | static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 8978 | { |
Tianyu Lan | 37b9595 | 2018-01-16 17:34:07 +0800 | [diff] [blame] | 8979 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 8980 | /* |
| 8981 | * When EFER.LME and CR0.PG are set, the processor is in |
| 8982 | * 64-bit mode (though maybe in a 32-bit code segment). |
| 8983 | * CR4.PAE and EFER.LMA must be set. |
| 8984 | */ |
Tianyu Lan | 37b9595 | 2018-01-16 17:34:07 +0800 | [diff] [blame] | 8985 | if (!(sregs->cr4 & X86_CR4_PAE) |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 8986 | || !(sregs->efer & EFER_LMA)) |
| 8987 | return -EINVAL; |
| 8988 | } else { |
| 8989 | /* |
| 8990 | * Not in 64-bit mode: EFER.LMA is clear and the code |
| 8991 | * segment cannot be 64-bit. |
| 8992 | */ |
| 8993 | if (sregs->efer & EFER_LMA || sregs->cs.l) |
| 8994 | return -EINVAL; |
| 8995 | } |
| 8996 | |
Wanpeng Li | 3ca9419 | 2019-09-18 17:50:10 +0800 | [diff] [blame] | 8997 | return kvm_valid_cr4(vcpu, sregs->cr4); |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 8998 | } |
| 8999 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 9000 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9001 | { |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 9002 | struct msr_data apic_base_msr; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9003 | int mmu_reset_needed = 0; |
Wei Huang | c4d2188 | 2018-05-01 09:49:54 -0500 | [diff] [blame] | 9004 | int cpuid_update_needed = 0; |
Xiao Guangrong | 63f42e0 | 2011-01-12 15:39:18 +0800 | [diff] [blame] | 9005 | int pending_vec, max_bits, idx; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 9006 | struct desc_ptr dt; |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 9007 | int ret = -EINVAL; |
| 9008 | |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 9009 | if (kvm_valid_sregs(vcpu, sregs)) |
Eric Biggers | 8dbfb2b | 2017-12-20 16:24:27 -0800 | [diff] [blame] | 9010 | goto out; |
Lan Tianyu | f298103 | 2017-12-14 03:01:52 -0500 | [diff] [blame] | 9011 | |
Jim Mattson | d380228 | 2017-08-10 10:14:13 -0700 | [diff] [blame] | 9012 | apic_base_msr.data = sregs->apic_base; |
| 9013 | apic_base_msr.host_initiated = true; |
| 9014 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 9015 | goto out; |
Petr Matousek | 6d1068b | 2012-11-06 19:24:07 +0100 | [diff] [blame] | 9016 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 9017 | dt.size = sregs->idt.limit; |
| 9018 | dt.address = sregs->idt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9019 | kvm_x86_ops->set_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 9020 | dt.size = sregs->gdt.limit; |
| 9021 | dt.address = sregs->gdt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9022 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 9023 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9024 | vcpu->arch.cr2 = sregs->cr2; |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 9025 | mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
Jan Kiszka | dc7e795 | 2009-07-01 20:52:03 +0200 | [diff] [blame] | 9026 | vcpu->arch.cr3 = sregs->cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 9027 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9028 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 9029 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9030 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 9031 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9032 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9033 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 9034 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9035 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 9036 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9037 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 9038 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
Wei Huang | c4d2188 | 2018-05-01 09:49:54 -0500 | [diff] [blame] | 9039 | cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) & |
| 9040 | (X86_CR4_OSXSAVE | X86_CR4_PKE)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9041 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Wei Huang | c4d2188 | 2018-05-01 09:49:54 -0500 | [diff] [blame] | 9042 | if (cpuid_update_needed) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 9043 | kvm_update_cpuid(vcpu); |
Xiao Guangrong | 63f42e0 | 2011-01-12 15:39:18 +0800 | [diff] [blame] | 9044 | |
| 9045 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Paolo Bonzini | bf03d4f | 2019-06-06 18:52:44 +0200 | [diff] [blame] | 9046 | if (is_pae_paging(vcpu)) { |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 9047 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 9048 | mmu_reset_needed = 1; |
| 9049 | } |
Xiao Guangrong | 63f42e0 | 2011-01-12 15:39:18 +0800 | [diff] [blame] | 9050 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9051 | |
| 9052 | if (mmu_reset_needed) |
| 9053 | kvm_mmu_reset_context(vcpu); |
| 9054 | |
Michael S. Tsirkin | a50abc3 | 2012-09-05 20:00:52 +0300 | [diff] [blame] | 9055 | max_bits = KVM_NR_INTERRUPTS; |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 9056 | pending_vec = find_first_bit( |
| 9057 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 9058 | if (pending_vec < max_bits) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9059 | kvm_queue_interrupt(vcpu, pending_vec, false); |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 9060 | pr_debug("Set back pending irq %d\n", pending_vec); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9061 | } |
| 9062 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 9063 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 9064 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 9065 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 9066 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 9067 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 9068 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9069 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 9070 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 9071 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9072 | |
Mikhail Ershov | 5f0269f | 2009-08-03 14:58:25 +0300 | [diff] [blame] | 9073 | update_cr8_intercept(vcpu); |
| 9074 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 9075 | /* Older userspace won't unhalt the vcpu on reset. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 9076 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 9077 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 9078 | !is_protmode(vcpu)) |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 9079 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 9080 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 9081 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9082 | |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 9083 | ret = 0; |
| 9084 | out: |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 9085 | return ret; |
| 9086 | } |
| 9087 | |
| 9088 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 9089 | struct kvm_sregs *sregs) |
| 9090 | { |
| 9091 | int ret; |
| 9092 | |
| 9093 | vcpu_load(vcpu); |
| 9094 | ret = __set_sregs(vcpu, sregs); |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 9095 | vcpu_put(vcpu); |
| 9096 | return ret; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9097 | } |
| 9098 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9099 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 9100 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9101 | { |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 9102 | unsigned long rflags; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9103 | int i, r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9104 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 9105 | vcpu_load(vcpu); |
| 9106 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 9107 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 9108 | r = -EBUSY; |
| 9109 | if (vcpu->arch.exception.pending) |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 9110 | goto out; |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 9111 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 9112 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 9113 | else |
| 9114 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 9115 | } |
| 9116 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 9117 | /* |
| 9118 | * Read rflags as long as potentially injected trace flags are still |
| 9119 | * filtered out. |
| 9120 | */ |
| 9121 | rflags = kvm_get_rflags(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 9122 | |
| 9123 | vcpu->guest_debug = dbg->control; |
| 9124 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 9125 | vcpu->guest_debug = 0; |
| 9126 | |
| 9127 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9128 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 9129 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 9130 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9131 | } else { |
| 9132 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 9133 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9134 | } |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 9135 | kvm_update_dr7(vcpu); |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9136 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 9137 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9138 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 9139 | get_segment_base(vcpu, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9140 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 9141 | /* |
| 9142 | * Trigger an rflags update that will inject or remove the trace |
| 9143 | * flags. |
| 9144 | */ |
| 9145 | kvm_set_rflags(vcpu, rflags); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9146 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 9147 | kvm_x86_ops->update_bp_intercept(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 9148 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 9149 | r = 0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9150 | |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 9151 | out: |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 9152 | vcpu_put(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 9153 | return r; |
| 9154 | } |
| 9155 | |
| 9156 | /* |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 9157 | * Translate a guest virtual address to a guest physical address. |
| 9158 | */ |
| 9159 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 9160 | struct kvm_translation *tr) |
| 9161 | { |
| 9162 | unsigned long vaddr = tr->linear_address; |
| 9163 | gpa_t gpa; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 9164 | int idx; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 9165 | |
Christoffer Dall | 1da5b61 | 2017-12-04 21:35:32 +0100 | [diff] [blame] | 9166 | vcpu_load(vcpu); |
| 9167 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 9168 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 9169 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 9170 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 9171 | tr->physical_address = gpa; |
| 9172 | tr->valid = gpa != UNMAPPED_GVA; |
| 9173 | tr->writeable = 1; |
| 9174 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 9175 | |
Christoffer Dall | 1da5b61 | 2017-12-04 21:35:32 +0100 | [diff] [blame] | 9176 | vcpu_put(vcpu); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 9177 | return 0; |
| 9178 | } |
| 9179 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9180 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 9181 | { |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 9182 | struct fxregs_state *fxsave; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9183 | |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 9184 | vcpu_load(vcpu); |
| 9185 | |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9186 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9187 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 9188 | fpu->fcw = fxsave->cwd; |
| 9189 | fpu->fsw = fxsave->swd; |
| 9190 | fpu->ftwx = fxsave->twd; |
| 9191 | fpu->last_opcode = fxsave->fop; |
| 9192 | fpu->last_ip = fxsave->rip; |
| 9193 | fpu->last_dp = fxsave->rdp; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 9194 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9195 | |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 9196 | vcpu_put(vcpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9197 | return 0; |
| 9198 | } |
| 9199 | |
| 9200 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 9201 | { |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 9202 | struct fxregs_state *fxsave; |
| 9203 | |
| 9204 | vcpu_load(vcpu); |
| 9205 | |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9206 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9207 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9208 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 9209 | fxsave->cwd = fpu->fcw; |
| 9210 | fxsave->swd = fpu->fsw; |
| 9211 | fxsave->twd = fpu->ftwx; |
| 9212 | fxsave->fop = fpu->last_opcode; |
| 9213 | fxsave->rip = fpu->last_ip; |
| 9214 | fxsave->rdp = fpu->last_dp; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 9215 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9216 | |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 9217 | vcpu_put(vcpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9218 | return 0; |
| 9219 | } |
| 9220 | |
Ken Hofsass | 01643c5 | 2018-01-31 16:03:36 -0800 | [diff] [blame] | 9221 | static void store_regs(struct kvm_vcpu *vcpu) |
| 9222 | { |
| 9223 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); |
| 9224 | |
| 9225 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) |
| 9226 | __get_regs(vcpu, &vcpu->run->s.regs.regs); |
| 9227 | |
| 9228 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) |
| 9229 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); |
| 9230 | |
| 9231 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) |
| 9232 | kvm_vcpu_ioctl_x86_get_vcpu_events( |
| 9233 | vcpu, &vcpu->run->s.regs.events); |
| 9234 | } |
| 9235 | |
| 9236 | static int sync_regs(struct kvm_vcpu *vcpu) |
| 9237 | { |
| 9238 | if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS) |
| 9239 | return -EINVAL; |
| 9240 | |
| 9241 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { |
| 9242 | __set_regs(vcpu, &vcpu->run->s.regs.regs); |
| 9243 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; |
| 9244 | } |
| 9245 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { |
| 9246 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) |
| 9247 | return -EINVAL; |
| 9248 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; |
| 9249 | } |
| 9250 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { |
| 9251 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( |
| 9252 | vcpu, &vcpu->run->s.regs.events)) |
| 9253 | return -EINVAL; |
| 9254 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; |
| 9255 | } |
| 9256 | |
| 9257 | return 0; |
| 9258 | } |
| 9259 | |
Ingo Molnar | 0ee6a51 | 2015-04-27 06:58:22 +0200 | [diff] [blame] | 9260 | static void fx_init(struct kvm_vcpu *vcpu) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9261 | { |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9262 | fpstate_init(&vcpu->arch.guest_fpu->state); |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 9263 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9264 | vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = |
Paolo Bonzini | df1daba | 2014-11-21 19:05:07 +0100 | [diff] [blame] | 9265 | host_xcr0 | XSTATE_COMPACTION_ENABLED; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9266 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9267 | /* |
| 9268 | * Ensure guest xcr0 is valid for loading |
| 9269 | */ |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 9270 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9271 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9272 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9273 | } |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 9274 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 9275 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9276 | { |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 9277 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 9278 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " |
| 9279 | "guest TSC will not be reliable\n"); |
Liang Li | c447e76 | 2015-05-21 04:41:25 +0800 | [diff] [blame] | 9280 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 9281 | return 0; |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 9282 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9283 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 9284 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 9285 | { |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9286 | struct page *page; |
| 9287 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9288 | |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9289 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
| 9290 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 9291 | else |
| 9292 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
| 9293 | |
| 9294 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
| 9295 | |
| 9296 | r = kvm_mmu_create(vcpu); |
| 9297 | if (r < 0) |
| 9298 | return r; |
| 9299 | |
| 9300 | if (irqchip_in_kernel(vcpu->kvm)) { |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9301 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
| 9302 | if (r < 0) |
| 9303 | goto fail_mmu_destroy; |
Suravee Suthikulpanit | 4e19c36 | 2019-11-14 14:15:05 -0600 | [diff] [blame] | 9304 | if (kvm_apicv_activated(vcpu->kvm)) |
| 9305 | vcpu->arch.apicv_active = true; |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9306 | } else |
| 9307 | static_key_slow_inc(&kvm_no_apic_vcpu); |
| 9308 | |
| 9309 | r = -ENOMEM; |
| 9310 | |
| 9311 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9312 | if (!page) |
| 9313 | goto fail_free_lapic; |
| 9314 | vcpu->arch.pio_data = page_address(page); |
| 9315 | |
| 9316 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 9317 | GFP_KERNEL_ACCOUNT); |
| 9318 | if (!vcpu->arch.mce_banks) |
| 9319 | goto fail_free_pio_data; |
| 9320 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 9321 | |
| 9322 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, |
| 9323 | GFP_KERNEL_ACCOUNT)) |
| 9324 | goto fail_free_mce_banks; |
| 9325 | |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 9326 | if (!alloc_emulate_ctxt(vcpu)) |
| 9327 | goto free_wbinvd_dirty_mask; |
| 9328 | |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9329 | vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 9330 | GFP_KERNEL_ACCOUNT); |
| 9331 | if (!vcpu->arch.user_fpu) { |
| 9332 | pr_err("kvm: failed to allocate userspace's fpu\n"); |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 9333 | goto free_emulate_ctxt; |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9334 | } |
| 9335 | |
| 9336 | vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 9337 | GFP_KERNEL_ACCOUNT); |
| 9338 | if (!vcpu->arch.guest_fpu) { |
| 9339 | pr_err("kvm: failed to allocate vcpu's fpu\n"); |
| 9340 | goto free_user_fpu; |
| 9341 | } |
| 9342 | fx_init(vcpu); |
| 9343 | |
| 9344 | vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; |
| 9345 | |
| 9346 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
| 9347 | |
| 9348 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; |
| 9349 | |
| 9350 | kvm_async_pf_hash_reset(vcpu); |
| 9351 | kvm_pmu_init(vcpu); |
| 9352 | |
| 9353 | vcpu->arch.pending_external_vector = -1; |
| 9354 | vcpu->arch.preempted_in_kernel = false; |
| 9355 | |
| 9356 | kvm_hv_vcpu_init(vcpu); |
| 9357 | |
| 9358 | r = kvm_x86_ops->vcpu_create(vcpu); |
| 9359 | if (r) |
| 9360 | goto free_guest_fpu; |
Sean Christopherson | 5f73db1 | 2019-12-18 13:55:18 -0800 | [diff] [blame] | 9361 | |
Sean Christopherson | 0cf9135 | 2019-03-07 15:43:02 -0800 | [diff] [blame] | 9362 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
Jim Mattson | e53d88af | 2018-10-30 12:20:21 -0700 | [diff] [blame] | 9363 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
Xiao Guangrong | 19efffa | 2015-06-15 16:55:31 +0800 | [diff] [blame] | 9364 | kvm_vcpu_mtrr_init(vcpu); |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9365 | vcpu_load(vcpu); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 9366 | kvm_vcpu_reset(vcpu, false); |
Paolo Bonzini | e173299 | 2018-10-08 21:28:09 +0200 | [diff] [blame] | 9367 | kvm_init_mmu(vcpu, false); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9368 | vcpu_put(vcpu); |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9369 | return 0; |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9370 | |
| 9371 | free_guest_fpu: |
| 9372 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); |
| 9373 | free_user_fpu: |
| 9374 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 9375 | free_emulate_ctxt: |
| 9376 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9377 | free_wbinvd_dirty_mask: |
| 9378 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
| 9379 | fail_free_mce_banks: |
| 9380 | kfree(vcpu->arch.mce_banks); |
| 9381 | fail_free_pio_data: |
| 9382 | free_page((unsigned long)vcpu->arch.pio_data); |
| 9383 | fail_free_lapic: |
| 9384 | kvm_free_lapic(vcpu); |
| 9385 | fail_mmu_destroy: |
| 9386 | kvm_mmu_destroy(vcpu); |
| 9387 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9388 | } |
| 9389 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 9390 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 9391 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 9392 | struct msr_data msr; |
Andrew Jones | 332967a | 2014-02-28 12:52:55 +0100 | [diff] [blame] | 9393 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 9394 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 9395 | kvm_hv_vcpu_postcreate(vcpu); |
| 9396 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9397 | if (mutex_lock_killable(&vcpu->mutex)) |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 9398 | return; |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9399 | vcpu_load(vcpu); |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 9400 | msr.data = 0x0; |
| 9401 | msr.index = MSR_IA32_TSC; |
| 9402 | msr.host_initiated = true; |
| 9403 | kvm_write_tsc(vcpu, &msr); |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 9404 | vcpu_put(vcpu); |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 9405 | |
| 9406 | /* poll control enabled by default */ |
| 9407 | vcpu->arch.msr_kvm_poll_control = 1; |
| 9408 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9409 | mutex_unlock(&vcpu->mutex); |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 9410 | |
Wanpeng Li | b34de57 | 2020-02-28 11:18:41 +0800 | [diff] [blame] | 9411 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
| 9412 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, |
| 9413 | KVMCLOCK_SYNC_PERIOD); |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 9414 | } |
| 9415 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 9416 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9417 | { |
Paolo Bonzini | 4cbc418 | 2020-01-30 18:47:38 +0100 | [diff] [blame] | 9418 | struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache; |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9419 | int idx; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 9420 | |
Paolo Bonzini | 4cbc418 | 2020-01-30 18:47:38 +0100 | [diff] [blame] | 9421 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
| 9422 | |
Sean Christopherson | 50b143e | 2019-12-18 13:55:07 -0800 | [diff] [blame] | 9423 | kvmclock_reset(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9424 | |
| 9425 | kvm_x86_ops->vcpu_free(vcpu); |
Sean Christopherson | 50b143e | 2019-12-18 13:55:07 -0800 | [diff] [blame] | 9426 | |
Sean Christopherson | c9b8b07 | 2020-02-18 15:29:48 -0800 | [diff] [blame] | 9427 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
Sean Christopherson | 50b143e | 2019-12-18 13:55:07 -0800 | [diff] [blame] | 9428 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
| 9429 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); |
| 9430 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); |
Sean Christopherson | 95a0d01 | 2019-12-18 13:55:23 -0800 | [diff] [blame] | 9431 | |
| 9432 | kvm_hv_vcpu_uninit(vcpu); |
| 9433 | kvm_pmu_destroy(vcpu); |
| 9434 | kfree(vcpu->arch.mce_banks); |
| 9435 | kvm_free_lapic(vcpu); |
| 9436 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 9437 | kvm_mmu_destroy(vcpu); |
| 9438 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 9439 | free_page((unsigned long)vcpu->arch.pio_data); |
| 9440 | if (!lapic_in_kernel(vcpu)) |
| 9441 | static_key_slow_dec(&kvm_no_apic_vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9442 | } |
| 9443 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 9444 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9445 | { |
Radim Krčmář | b7e31be | 2018-03-01 15:24:25 +0100 | [diff] [blame] | 9446 | kvm_lapic_reset(vcpu, init_event); |
| 9447 | |
Paolo Bonzini | e69fab5 | 2015-06-04 10:44:44 +0200 | [diff] [blame] | 9448 | vcpu->arch.hflags = 0; |
| 9449 | |
Paolo Bonzini | c43203c | 2016-06-01 22:26:00 +0200 | [diff] [blame] | 9450 | vcpu->arch.smi_pending = 0; |
Liran Alon | 52797bf | 2017-11-15 13:43:14 +0200 | [diff] [blame] | 9451 | vcpu->arch.smi_count = 0; |
Avi Kivity | 7460fb4a | 2011-09-20 13:43:14 +0300 | [diff] [blame] | 9452 | atomic_set(&vcpu->arch.nmi_queued, 0); |
| 9453 | vcpu->arch.nmi_pending = 0; |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 9454 | vcpu->arch.nmi_injected = false; |
Nadav Amit | 5f7552d | 2014-06-30 12:03:02 +0300 | [diff] [blame] | 9455 | kvm_clear_interrupt_queue(vcpu); |
| 9456 | kvm_clear_exception_queue(vcpu); |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 9457 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9458 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
Nadav Amit | ae561ed | 2015-04-02 03:10:37 +0300 | [diff] [blame] | 9459 | kvm_update_dr0123(vcpu); |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 9460 | vcpu->arch.dr6 = DR6_INIT; |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 9461 | kvm_update_dr6(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9462 | vcpu->arch.dr7 = DR7_FIXED_1; |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 9463 | kvm_update_dr7(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 9464 | |
Nadav Amit | 1119022 | 2015-04-02 03:10:38 +0300 | [diff] [blame] | 9465 | vcpu->arch.cr2 = 0; |
| 9466 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 9467 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 9468 | vcpu->arch.apf.msr_val = 0; |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 9469 | vcpu->arch.st.msr_val = 0; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 9470 | |
Glauber Costa | 12f9a48 | 2011-02-01 14:16:40 -0500 | [diff] [blame] | 9471 | kvmclock_reset(vcpu); |
| 9472 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 9473 | kvm_clear_async_pf_completion_queue(vcpu); |
| 9474 | kvm_async_pf_hash_reset(vcpu); |
| 9475 | vcpu->arch.apf.halted = false; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9476 | |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9477 | if (kvm_mpx_supported()) { |
| 9478 | void *mpx_state_buffer; |
| 9479 | |
| 9480 | /* |
| 9481 | * To avoid have the INIT path from kvm_apic_has_events() that be |
| 9482 | * called with loaded FPU and does not let userspace fix the state. |
| 9483 | */ |
Rik van Riel | f775b13 | 2017-11-14 16:54:23 -0500 | [diff] [blame] | 9484 | if (init_event) |
| 9485 | kvm_put_guest_fpu(vcpu); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9486 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 9487 | XFEATURE_BNDREGS); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9488 | if (mpx_state_buffer) |
| 9489 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 9490 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
Sebastian Andrzej Siewior | abd16d6 | 2019-04-03 18:41:40 +0200 | [diff] [blame] | 9491 | XFEATURE_BNDCSR); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9492 | if (mpx_state_buffer) |
| 9493 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); |
Rik van Riel | f775b13 | 2017-11-14 16:54:23 -0500 | [diff] [blame] | 9494 | if (init_event) |
| 9495 | kvm_load_guest_fpu(vcpu); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9496 | } |
| 9497 | |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 9498 | if (!init_event) { |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 9499 | kvm_pmu_reset(vcpu); |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 9500 | vcpu->arch.smbase = 0x30000; |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 9501 | |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 9502 | vcpu->arch.msr_misc_features_enables = 0; |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9503 | |
| 9504 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 9505 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 9506 | |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 9507 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
| 9508 | vcpu->arch.regs_avail = ~0; |
| 9509 | vcpu->arch.regs_dirty = ~0; |
| 9510 | |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 9511 | vcpu->arch.ia32_xss = 0; |
| 9512 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 9513 | kvm_x86_ops->vcpu_reset(vcpu, init_event); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9514 | } |
| 9515 | |
Paolo Bonzini | 2b4a273 | 2014-11-24 14:35:24 +0100 | [diff] [blame] | 9516 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 9517 | { |
| 9518 | struct kvm_segment cs; |
| 9519 | |
| 9520 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 9521 | cs.selector = vector << 8; |
| 9522 | cs.base = vector << 12; |
| 9523 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); |
| 9524 | kvm_rip_write(vcpu, 0); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9525 | } |
| 9526 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 9527 | int kvm_arch_hardware_enable(void) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9528 | { |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 9529 | struct kvm *kvm; |
| 9530 | struct kvm_vcpu *vcpu; |
| 9531 | int i; |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9532 | int ret; |
| 9533 | u64 local_tsc; |
| 9534 | u64 max_tsc = 0; |
| 9535 | bool stable, backwards_tsc = false; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 9536 | |
| 9537 | kvm_shared_msr_cpu_online(); |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 9538 | ret = kvm_x86_ops->hardware_enable(); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9539 | if (ret != 0) |
| 9540 | return ret; |
| 9541 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 9542 | local_tsc = rdtsc(); |
Vitaly Kuznetsov | b0c39dc | 2018-01-24 14:23:36 +0100 | [diff] [blame] | 9543 | stable = !kvm_check_tsc_unstable(); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9544 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 9545 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 9546 | if (!stable && vcpu->cpu == smp_processor_id()) |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 9547 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9548 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
| 9549 | backwards_tsc = true; |
| 9550 | if (vcpu->arch.last_host_tsc > max_tsc) |
| 9551 | max_tsc = vcpu->arch.last_host_tsc; |
| 9552 | } |
| 9553 | } |
| 9554 | } |
| 9555 | |
| 9556 | /* |
| 9557 | * Sometimes, even reliable TSCs go backwards. This happens on |
| 9558 | * platforms that reset TSC during suspend or hibernate actions, but |
| 9559 | * maintain synchronization. We must compensate. Fortunately, we can |
| 9560 | * detect that condition here, which happens early in CPU bringup, |
| 9561 | * before any KVM threads can be running. Unfortunately, we can't |
| 9562 | * bring the TSCs fully up to date with real time, as we aren't yet far |
| 9563 | * enough into CPU bringup that we know how much real time has actually |
Jason A. Donenfeld | 9285ec4 | 2019-06-21 22:32:48 +0200 | [diff] [blame] | 9564 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9565 | * variables that haven't been updated yet. |
| 9566 | * |
| 9567 | * So we simply find the maximum observed TSC above, then record the |
| 9568 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, |
| 9569 | * the adjustment will be applied. Note that we accumulate |
| 9570 | * adjustments, in case multiple suspend cycles happen before some VCPU |
| 9571 | * gets a chance to run again. In the event that no KVM threads get a |
| 9572 | * chance to run, we will miss the entire elapsed period, as we'll have |
| 9573 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may |
| 9574 | * loose cycle time. This isn't too big a deal, since the loss will be |
| 9575 | * uniform across all VCPUs (not to mention the scenario is extremely |
| 9576 | * unlikely). It is possible that a second hibernate recovery happens |
| 9577 | * much faster than a first, causing the observed TSC here to be |
| 9578 | * smaller; this would require additional padding adjustment, which is |
| 9579 | * why we set last_host_tsc to the local tsc observed here. |
| 9580 | * |
| 9581 | * N.B. - this code below runs only on platforms with reliable TSC, |
| 9582 | * as that is the only way backwards_tsc is set above. Also note |
| 9583 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should |
| 9584 | * have the same delta_cyc adjustment applied if backwards_tsc |
| 9585 | * is detected. Note further, this adjustment is only done once, |
| 9586 | * as we reset last_host_tsc on all VCPUs to stop this from being |
| 9587 | * called multiple times (one for each physical CPU bringup). |
| 9588 | * |
Guo Chao | 4a96998 | 2012-06-28 15:17:27 +0800 | [diff] [blame] | 9589 | * Platforms with unreliable TSCs don't have to deal with this, they |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9590 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
| 9591 | * catchup mode. This will catchup all VCPUs to real time, but cannot |
| 9592 | * guarantee that they stay in perfect synchronization. |
| 9593 | */ |
| 9594 | if (backwards_tsc) { |
| 9595 | u64 delta_cyc = max_tsc - local_tsc; |
| 9596 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Ladi Prosek | a826faf | 2017-06-26 09:56:43 +0200 | [diff] [blame] | 9597 | kvm->arch.backwards_tsc_observed = true; |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9598 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 9599 | vcpu->arch.tsc_offset_adjustment += delta_cyc; |
| 9600 | vcpu->arch.last_host_tsc = local_tsc; |
Guo Hui Liu | 105b21b | 2014-09-12 13:43:19 +0800 | [diff] [blame] | 9601 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Zachary Amsden | 0dd6a6e | 2012-02-03 15:43:56 -0200 | [diff] [blame] | 9602 | } |
| 9603 | |
| 9604 | /* |
| 9605 | * We have to disable TSC offset matching.. if you were |
| 9606 | * booting a VM while issuing an S4 host suspend.... |
| 9607 | * you may have some problem. Solving this issue is |
| 9608 | * left as an exercise to the reader. |
| 9609 | */ |
| 9610 | kvm->arch.last_tsc_nsec = 0; |
| 9611 | kvm->arch.last_tsc_write = 0; |
| 9612 | } |
| 9613 | |
| 9614 | } |
| 9615 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9616 | } |
| 9617 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 9618 | void kvm_arch_hardware_disable(void) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9619 | { |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 9620 | kvm_x86_ops->hardware_disable(); |
| 9621 | drop_user_return_notifiers(); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9622 | } |
| 9623 | |
| 9624 | int kvm_arch_hardware_setup(void) |
| 9625 | { |
Nadav Amit | 9e9c3fe | 2015-04-12 21:47:15 +0300 | [diff] [blame] | 9626 | int r; |
| 9627 | |
Sean Christopherson | 9166198 | 2020-03-02 15:57:06 -0800 | [diff] [blame] | 9628 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 9629 | |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 9630 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 9631 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 9632 | |
Nadav Amit | 9e9c3fe | 2015-04-12 21:47:15 +0300 | [diff] [blame] | 9633 | r = kvm_x86_ops->hardware_setup(); |
| 9634 | if (r != 0) |
| 9635 | return r; |
| 9636 | |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 9637 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
| 9638 | supported_xss = 0; |
| 9639 | |
Sean Christopherson | b11306b | 2019-12-10 14:44:13 -0800 | [diff] [blame] | 9640 | cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data); |
| 9641 | |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 9642 | if (kvm_has_tsc_control) { |
| 9643 | /* |
| 9644 | * Make sure the user can only configure tsc_khz values that |
| 9645 | * fit into a signed integer. |
Marcelo Tosatti | 273ba45 | 2018-06-11 14:12:10 -0300 | [diff] [blame] | 9646 | * A min value is not calculated because it will always |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 9647 | * be 1 on all machines. |
| 9648 | */ |
| 9649 | u64 max = min(0x7fffffffULL, |
| 9650 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); |
| 9651 | kvm_max_guest_tsc_khz = max; |
| 9652 | |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 9653 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 9654 | } |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 9655 | |
Nadav Amit | 9e9c3fe | 2015-04-12 21:47:15 +0300 | [diff] [blame] | 9656 | kvm_init_msr_list(); |
| 9657 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9658 | } |
| 9659 | |
| 9660 | void kvm_arch_hardware_unsetup(void) |
| 9661 | { |
| 9662 | kvm_x86_ops->hardware_unsetup(); |
| 9663 | } |
| 9664 | |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 9665 | int kvm_arch_check_processor_compat(void) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9666 | { |
Sean Christopherson | f1cdecf | 2019-12-10 14:44:14 -0800 | [diff] [blame] | 9667 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
| 9668 | |
| 9669 | WARN_ON(!irqs_disabled()); |
| 9670 | |
| 9671 | if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits) |
| 9672 | return -EIO; |
| 9673 | |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 9674 | return kvm_x86_ops->check_processor_compatibility(); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 9675 | } |
| 9676 | |
Paolo Bonzini | d71ba78 | 2015-07-29 11:56:48 +0200 | [diff] [blame] | 9677 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) |
| 9678 | { |
| 9679 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; |
| 9680 | } |
| 9681 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); |
| 9682 | |
| 9683 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) |
| 9684 | { |
| 9685 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; |
| 9686 | } |
| 9687 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 9688 | struct static_key kvm_no_apic_vcpu __read_mostly; |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 9689 | EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu); |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 9690 | |
Radim Krčmář | e790d9e | 2014-08-21 18:08:05 +0200 | [diff] [blame] | 9691 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 9692 | { |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 9693 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 9694 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9695 | vcpu->arch.l1tf_flush_l1d = true; |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 9696 | if (pmu->version && unlikely(pmu->event_count)) { |
| 9697 | pmu->need_cleanup = true; |
| 9698 | kvm_make_request(KVM_REQ_PMU, vcpu); |
| 9699 | } |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 9700 | kvm_x86_ops->sched_in(vcpu, cpu); |
Radim Krčmář | e790d9e | 2014-08-21 18:08:05 +0200 | [diff] [blame] | 9701 | } |
| 9702 | |
Sean Christopherson | 562b6b0 | 2020-01-26 16:41:13 -0800 | [diff] [blame] | 9703 | void kvm_arch_free_vm(struct kvm *kvm) |
| 9704 | { |
| 9705 | kfree(kvm->arch.hyperv.hv_pa_pg); |
| 9706 | vfree(kvm); |
| 9707 | } |
| 9708 | |
| 9709 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 9710 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9711 | { |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 9712 | if (type) |
| 9713 | return -EINVAL; |
| 9714 | |
Paolo Bonzini | 6ef768f | 2014-11-20 13:45:31 +0100 | [diff] [blame] | 9715 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 9716 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 9717 | INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 9718 | INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 9719 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 9720 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9721 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 9722 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 9723 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 9724 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
| 9725 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
| 9726 | &kvm->arch.irq_sources_bitmap); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 9727 | |
Jan Kiszka | 038f8c1 | 2011-02-04 10:49:11 +0100 | [diff] [blame] | 9728 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 9729 | mutex_init(&kvm->arch.apic_map_lock); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 9730 | spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock); |
| 9731 | |
Paolo Bonzini | 8171cd6 | 2020-01-22 14:36:09 +0100 | [diff] [blame] | 9732 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
Marcelo Tosatti | d828199 | 2012-11-27 23:29:01 -0200 | [diff] [blame] | 9733 | pvclock_update_vm_gtod_copy(kvm); |
Marcelo Tosatti | 53f658b3 | 2008-12-11 20:45:05 +0100 | [diff] [blame] | 9734 | |
Drew Schmitt | 6fbbde9 | 2018-08-20 10:32:15 -0700 | [diff] [blame] | 9735 | kvm->arch.guest_can_read_msr_platform_info = true; |
| 9736 | |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 9737 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
Andrew Jones | 332967a | 2014-02-28 12:52:55 +0100 | [diff] [blame] | 9738 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 9739 | |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 9740 | kvm_hv_init_vm(kvm); |
Xiao Guangrong | 0eb05bf | 2016-02-24 17:51:13 +0800 | [diff] [blame] | 9741 | kvm_page_track_init(kvm); |
Xiao Guangrong | 13d268c | 2016-02-24 17:51:16 +0800 | [diff] [blame] | 9742 | kvm_mmu_init_vm(kvm); |
Xiao Guangrong | 0eb05bf | 2016-02-24 17:51:13 +0800 | [diff] [blame] | 9743 | |
Sean Christopherson | 92735b1 | 2019-08-02 15:06:17 -0700 | [diff] [blame] | 9744 | return kvm_x86_ops->vm_init(kvm); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9745 | } |
| 9746 | |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 9747 | int kvm_arch_post_init_vm(struct kvm *kvm) |
| 9748 | { |
| 9749 | return kvm_mmu_post_init_vm(kvm); |
| 9750 | } |
| 9751 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9752 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 9753 | { |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9754 | vcpu_load(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9755 | kvm_mmu_unload(vcpu); |
| 9756 | vcpu_put(vcpu); |
| 9757 | } |
| 9758 | |
| 9759 | static void kvm_free_vcpus(struct kvm *kvm) |
| 9760 | { |
| 9761 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 9762 | struct kvm_vcpu *vcpu; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9763 | |
| 9764 | /* |
| 9765 | * Unpin any mmu pages first. |
| 9766 | */ |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 9767 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 9768 | kvm_clear_async_pf_completion_queue(vcpu); |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 9769 | kvm_unload_vcpu_mmu(vcpu); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 9770 | } |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 9771 | kvm_for_each_vcpu(i, vcpu, kvm) |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 9772 | kvm_vcpu_destroy(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9773 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 9774 | mutex_lock(&kvm->lock); |
| 9775 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 9776 | kvm->vcpus[i] = NULL; |
| 9777 | |
| 9778 | atomic_set(&kvm->online_vcpus, 0); |
| 9779 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9780 | } |
| 9781 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 9782 | void kvm_arch_sync_events(struct kvm *kvm) |
| 9783 | { |
Andrew Jones | 332967a | 2014-02-28 12:52:55 +0100 | [diff] [blame] | 9784 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
Andrew Jones | 7e44e44 | 2014-02-28 12:52:54 +0100 | [diff] [blame] | 9785 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
Xiao Guangrong | aea924f | 2010-07-10 17:37:56 +0800 | [diff] [blame] | 9786 | kvm_free_pit(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 9787 | } |
| 9788 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 9789 | int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9790 | { |
| 9791 | int i, r; |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9792 | unsigned long hva, uninitialized_var(old_npages); |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9793 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9794 | struct kvm_memory_slot *slot; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9795 | |
| 9796 | /* Called with kvm->slots_lock held. */ |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 9797 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
| 9798 | return -EINVAL; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9799 | |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9800 | slot = id_to_memslot(slots, id); |
| 9801 | if (size) { |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9802 | if (slot && slot->npages) |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9803 | return -EEXIST; |
| 9804 | |
| 9805 | /* |
| 9806 | * MAP_SHARED to prevent internal slot pages from being moved |
| 9807 | * by fork()/COW. |
| 9808 | */ |
| 9809 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, |
| 9810 | MAP_SHARED | MAP_ANONYMOUS, 0); |
| 9811 | if (IS_ERR((void *)hva)) |
| 9812 | return PTR_ERR((void *)hva); |
| 9813 | } else { |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9814 | if (!slot || !slot->npages) |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9815 | return 0; |
| 9816 | |
Sean Christopherson | abbed4f | 2020-03-04 16:24:22 -0800 | [diff] [blame] | 9817 | /* |
| 9818 | * Stuff a non-canonical value to catch use-after-delete. This |
| 9819 | * ends up being 0 on 32-bit KVM, but there's no better |
| 9820 | * alternative. |
| 9821 | */ |
| 9822 | hva = (unsigned long)(0xdeadull << 48); |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9823 | old_npages = slot->npages; |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9824 | } |
| 9825 | |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9826 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 9827 | struct kvm_userspace_memory_region m; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9828 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 9829 | m.slot = id | (i << 16); |
| 9830 | m.flags = 0; |
| 9831 | m.guest_phys_addr = gpa; |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9832 | m.userspace_addr = hva; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 9833 | m.memory_size = size; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9834 | r = __kvm_set_memory_region(kvm, &m); |
| 9835 | if (r < 0) |
| 9836 | return r; |
| 9837 | } |
| 9838 | |
Eric Biggers | 103c763 | 2018-01-31 17:30:21 -0800 | [diff] [blame] | 9839 | if (!size) |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 9840 | vm_munmap(hva, old_npages * PAGE_SIZE); |
Paolo Bonzini | f0d648b | 2015-10-12 13:56:27 +0200 | [diff] [blame] | 9841 | |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 9842 | return 0; |
| 9843 | } |
| 9844 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); |
| 9845 | |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 9846 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
| 9847 | { |
| 9848 | kvm_mmu_pre_destroy_vm(kvm); |
| 9849 | } |
| 9850 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9851 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 9852 | { |
Andrew Honig | 27469d2 | 2013-04-18 09:38:14 -0700 | [diff] [blame] | 9853 | if (current->mm == kvm->mm) { |
| 9854 | /* |
| 9855 | * Free memory regions allocated on behalf of userspace, |
| 9856 | * unless the the memory map has changed due to process exit |
| 9857 | * or fd copying. |
| 9858 | */ |
Peter Xu | 6a3c623 | 2020-01-09 09:57:16 -0500 | [diff] [blame] | 9859 | mutex_lock(&kvm->slots_lock); |
| 9860 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 9861 | 0, 0); |
| 9862 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 9863 | 0, 0); |
| 9864 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); |
| 9865 | mutex_unlock(&kvm->slots_lock); |
Andrew Honig | 27469d2 | 2013-04-18 09:38:14 -0700 | [diff] [blame] | 9866 | } |
Suravee Suthikulpanit | 0354313 | 2016-05-04 14:09:42 -0500 | [diff] [blame] | 9867 | if (kvm_x86_ops->vm_destroy) |
| 9868 | kvm_x86_ops->vm_destroy(kvm); |
Peter Xu | c761159 | 2017-03-15 16:01:19 +0800 | [diff] [blame] | 9869 | kvm_pic_destroy(kvm); |
| 9870 | kvm_ioapic_destroy(kvm); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9871 | kvm_free_vcpus(kvm); |
Radim Krčmář | af1bae5 | 2016-07-12 22:09:30 +0200 | [diff] [blame] | 9872 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
Eric Hankland | 66bb8a0 | 2019-07-10 18:25:15 -0700 | [diff] [blame] | 9873 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
Xiao Guangrong | 13d268c | 2016-02-24 17:51:16 +0800 | [diff] [blame] | 9874 | kvm_mmu_uninit_vm(kvm); |
Paolo Bonzini | 2beb6dad | 2017-03-27 17:53:50 +0200 | [diff] [blame] | 9875 | kvm_page_track_cleanup(kvm); |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 9876 | kvm_hv_destroy_vm(kvm); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 9877 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 9878 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 9879 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9880 | { |
| 9881 | int i; |
| 9882 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9883 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 9884 | kvfree(slot->arch.rmap[i]); |
| 9885 | slot->arch.rmap[i] = NULL; |
| 9886 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9887 | if (i == 0) |
| 9888 | continue; |
| 9889 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 9890 | kvfree(slot->arch.lpage_info[i - 1]); |
| 9891 | slot->arch.lpage_info[i - 1] = NULL; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9892 | } |
Xiao Guangrong | 21ebbed | 2016-02-24 17:51:09 +0800 | [diff] [blame] | 9893 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 9894 | kvm_page_track_free_memslot(slot); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9895 | } |
| 9896 | |
Sean Christopherson | 0dab98b | 2020-02-18 13:07:19 -0800 | [diff] [blame] | 9897 | static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, |
| 9898 | unsigned long npages) |
| 9899 | { |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9900 | int i; |
| 9901 | |
Sean Christopherson | edd4fa3 | 2020-02-18 13:07:15 -0800 | [diff] [blame] | 9902 | /* |
| 9903 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The |
| 9904 | * old arrays will be freed by __kvm_set_memory_region() if installing |
| 9905 | * the new memslot is successful. |
| 9906 | */ |
| 9907 | memset(&slot->arch, 0, sizeof(slot->arch)); |
| 9908 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9909 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9910 | struct kvm_lpage_info *linfo; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9911 | unsigned long ugfn; |
| 9912 | int lpages; |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9913 | int level = i + 1; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9914 | |
| 9915 | lpages = gfn_to_index(slot->base_gfn + npages - 1, |
| 9916 | slot->base_gfn, level) + 1; |
| 9917 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9918 | slot->arch.rmap[i] = |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 9919 | kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 9920 | GFP_KERNEL_ACCOUNT); |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9921 | if (!slot->arch.rmap[i]) |
Takuya Yoshikawa | 77d1130 | 2012-07-02 17:57:17 +0900 | [diff] [blame] | 9922 | goto out_free; |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9923 | if (i == 0) |
| 9924 | continue; |
Takuya Yoshikawa | 77d1130 | 2012-07-02 17:57:17 +0900 | [diff] [blame] | 9925 | |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 9926 | linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9927 | if (!linfo) |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9928 | goto out_free; |
| 9929 | |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9930 | slot->arch.lpage_info[i - 1] = linfo; |
| 9931 | |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9932 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9933 | linfo[0].disallow_lpage = 1; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9934 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9935 | linfo[lpages - 1].disallow_lpage = 1; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9936 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
| 9937 | /* |
| 9938 | * If the gfn and userspace address are not aligned wrt each |
Sean Christopherson | 600087b | 2020-03-02 15:57:05 -0800 | [diff] [blame] | 9939 | * other, disable large page support for this slot. |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9940 | */ |
Sean Christopherson | 600087b | 2020-03-02 15:57:05 -0800 | [diff] [blame] | 9941 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9942 | unsigned long j; |
| 9943 | |
| 9944 | for (j = 0; j < lpages; ++j) |
Xiao Guangrong | 92f94f1 | 2016-02-24 17:51:06 +0800 | [diff] [blame] | 9945 | linfo[j].disallow_lpage = 1; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9946 | } |
| 9947 | } |
| 9948 | |
Xiao Guangrong | 21ebbed | 2016-02-24 17:51:09 +0800 | [diff] [blame] | 9949 | if (kvm_page_track_create_memslot(slot, npages)) |
| 9950 | goto out_free; |
| 9951 | |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9952 | return 0; |
| 9953 | |
| 9954 | out_free: |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9955 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
Thomas Huth | 548ef28 | 2015-02-24 21:29:25 +0100 | [diff] [blame] | 9956 | kvfree(slot->arch.rmap[i]); |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9957 | slot->arch.rmap[i] = NULL; |
| 9958 | if (i == 0) |
| 9959 | continue; |
| 9960 | |
Thomas Huth | 548ef28 | 2015-02-24 21:29:25 +0100 | [diff] [blame] | 9961 | kvfree(slot->arch.lpage_info[i - 1]); |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 9962 | slot->arch.lpage_info[i - 1] = NULL; |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 9963 | } |
| 9964 | return -ENOMEM; |
| 9965 | } |
| 9966 | |
Sean Christopherson | 1524825 | 2019-02-05 12:54:17 -0800 | [diff] [blame] | 9967 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 9968 | { |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 9969 | struct kvm_vcpu *vcpu; |
| 9970 | int i; |
| 9971 | |
Takuya Yoshikawa | e6dff7d | 2013-07-04 13:41:26 +0900 | [diff] [blame] | 9972 | /* |
| 9973 | * memslots->generation has been incremented. |
| 9974 | * mmio generation may have reached its maximum value. |
| 9975 | */ |
Sean Christopherson | 1524825 | 2019-02-05 12:54:17 -0800 | [diff] [blame] | 9976 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 9977 | |
| 9978 | /* Force re-initialization of steal_time cache */ |
| 9979 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9980 | kvm_vcpu_kick(vcpu); |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 9981 | } |
| 9982 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 9983 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 9984 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 9985 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 7b6195a | 2013-02-27 19:44:34 +0900 | [diff] [blame] | 9986 | enum kvm_mr_change change) |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 9987 | { |
Sean Christopherson | 0dab98b | 2020-02-18 13:07:19 -0800 | [diff] [blame] | 9988 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
| 9989 | return kvm_alloc_memslot_metadata(memslot, |
| 9990 | mem->memory_size >> PAGE_SHIFT); |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 9991 | return 0; |
| 9992 | } |
| 9993 | |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 9994 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
| 9995 | struct kvm_memory_slot *new) |
| 9996 | { |
| 9997 | /* Still write protect RO slot */ |
| 9998 | if (new->flags & KVM_MEM_READONLY) { |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 9999 | kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL); |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10000 | return; |
| 10001 | } |
| 10002 | |
| 10003 | /* |
| 10004 | * Call kvm_x86_ops dirty logging hooks when they are valid. |
| 10005 | * |
| 10006 | * kvm_x86_ops->slot_disable_log_dirty is called when: |
| 10007 | * |
| 10008 | * - KVM_MR_CREATE with dirty logging is disabled |
| 10009 | * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag |
| 10010 | * |
| 10011 | * The reason is, in case of PML, we need to set D-bit for any slots |
| 10012 | * with dirty logging disabled in order to eliminate unnecessary GPA |
Miaohe Lin | 0a03cbd | 2019-12-06 16:20:18 +0800 | [diff] [blame] | 10013 | * logging in PML buffer (and potential PML buffer full VMEXIT). This |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10014 | * guarantees leaving PML enabled during guest's lifetime won't have |
Wei Yang | bdd303c | 2018-11-05 14:45:03 +0800 | [diff] [blame] | 10015 | * any additional overhead from PML when guest is running with dirty |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10016 | * logging disabled for memory slots. |
| 10017 | * |
| 10018 | * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot |
| 10019 | * to dirty logging mode. |
| 10020 | * |
| 10021 | * If kvm_x86_ops dirty logging hooks are invalid, use write protect. |
| 10022 | * |
| 10023 | * In case of write protect: |
| 10024 | * |
| 10025 | * Write protect all pages for dirty logging. |
| 10026 | * |
| 10027 | * All the sptes including the large sptes which point to this |
| 10028 | * slot are set to readonly. We can not create any new large |
| 10029 | * spte on this slot until the end of the logging. |
| 10030 | * |
| 10031 | * See the comments in fast_page_fault(). |
| 10032 | */ |
| 10033 | if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) { |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 10034 | if (kvm_x86_ops->slot_enable_log_dirty) { |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10035 | kvm_x86_ops->slot_enable_log_dirty(kvm, new); |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 10036 | } else { |
| 10037 | int level = |
| 10038 | kvm_dirty_log_manual_protect_and_init_set(kvm) ? |
| 10039 | PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL; |
| 10040 | |
| 10041 | /* |
| 10042 | * If we're with initial-all-set, we don't need |
| 10043 | * to write protect any small page because |
| 10044 | * they're reported as dirty already. However |
| 10045 | * we still need to write-protect huge pages |
| 10046 | * so that the page split can happen lazily on |
| 10047 | * the first write to the huge page. |
| 10048 | */ |
| 10049 | kvm_mmu_slot_remove_write_access(kvm, new, level); |
| 10050 | } |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10051 | } else { |
| 10052 | if (kvm_x86_ops->slot_disable_log_dirty) |
| 10053 | kvm_x86_ops->slot_disable_log_dirty(kvm, new); |
| 10054 | } |
| 10055 | } |
| 10056 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 10057 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 10058 | const struct kvm_userspace_memory_region *mem, |
Sean Christopherson | 9d4c197 | 2020-02-18 13:07:24 -0800 | [diff] [blame] | 10059 | struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 10060 | const struct kvm_memory_slot *new, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 10061 | enum kvm_mr_change change) |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 10062 | { |
Xiao Guangrong | 48c0e4e | 2011-03-04 18:59:21 +0800 | [diff] [blame] | 10063 | if (!kvm->arch.n_requested_mmu_pages) |
Wei Yang | 4d66623 | 2018-09-27 08:31:26 +0800 | [diff] [blame] | 10064 | kvm_mmu_change_mmu_pages(kvm, |
| 10065 | kvm_mmu_calculate_default_mmu_pages(kvm)); |
Kai Huang | 1c91cad4 | 2015-01-28 10:54:26 +0800 | [diff] [blame] | 10066 | |
Takuya Yoshikawa | c972f3b | 2013-01-08 19:43:28 +0900 | [diff] [blame] | 10067 | /* |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 10068 | * Dirty logging tracks sptes in 4k granularity, meaning that large |
| 10069 | * sptes have to be split. If live migration is successful, the guest |
| 10070 | * in the source machine will be destroyed and large sptes will be |
| 10071 | * created in the destination. However, if the guest continues to run |
| 10072 | * in the source machine (for example if live migration fails), small |
| 10073 | * sptes will remain around and cause bad performance. |
| 10074 | * |
| 10075 | * Scan sptes if dirty logging has been stopped, dropping those |
| 10076 | * which can be collapsed into a single large-page spte. Later |
| 10077 | * page faults will create the large-page sptes. |
Sean Christopherson | 319109a | 2019-09-11 12:19:52 -0700 | [diff] [blame] | 10078 | * |
| 10079 | * There is no need to do this in any of the following cases: |
| 10080 | * CREATE: No dirty mappings will already exist. |
| 10081 | * MOVE/DELETE: The old mappings will already have been cleaned up by |
| 10082 | * kvm_arch_flush_shadow_memslot() |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 10083 | */ |
Sean Christopherson | 319109a | 2019-09-11 12:19:52 -0700 | [diff] [blame] | 10084 | if (change == KVM_MR_FLAGS_ONLY && |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 10085 | (old->flags & KVM_MEM_LOG_DIRTY_PAGES) && |
| 10086 | !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) |
| 10087 | kvm_mmu_zap_collapsible_sptes(kvm, new); |
| 10088 | |
| 10089 | /* |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10090 | * Set up write protection and/or dirty logging for the new slot. |
Xiao Guangrong | c126d94 | 2014-04-17 17:06:14 +0800 | [diff] [blame] | 10091 | * |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10092 | * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have |
| 10093 | * been zapped so no dirty logging staff is needed for old slot. For |
| 10094 | * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the |
| 10095 | * new and it's also covered when dealing with the new slot. |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 10096 | * |
| 10097 | * FIXME: const-ify all uses of struct kvm_memory_slot. |
Takuya Yoshikawa | c972f3b | 2013-01-08 19:43:28 +0900 | [diff] [blame] | 10098 | */ |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 10099 | if (change != KVM_MR_DELETE) |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 10100 | kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new); |
Sean Christopherson | 2119884 | 2020-02-18 13:07:25 -0800 | [diff] [blame] | 10101 | |
| 10102 | /* Free the arrays associated with the old memslot. */ |
| 10103 | if (change == KVM_MR_MOVE) |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 10104 | kvm_arch_free_memslot(kvm, old); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 10105 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 10106 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 10107 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 10108 | { |
Sean Christopherson | 7390de1 | 2019-02-05 13:01:31 -0800 | [diff] [blame] | 10109 | kvm_mmu_zap_all(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 10110 | } |
| 10111 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 10112 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 10113 | struct kvm_memory_slot *slot) |
| 10114 | { |
Xiaoguang Chen | ae7cd87 | 2016-10-09 15:41:44 +0800 | [diff] [blame] | 10115 | kvm_page_track_flush_slot(kvm, slot); |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 10116 | } |
| 10117 | |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 10118 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 10119 | { |
| 10120 | return (is_guest_mode(vcpu) && |
| 10121 | kvm_x86_ops->guest_apic_has_interrupt && |
| 10122 | kvm_x86_ops->guest_apic_has_interrupt(vcpu)); |
| 10123 | } |
| 10124 | |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10125 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
| 10126 | { |
| 10127 | if (!list_empty_careful(&vcpu->async_pf.done)) |
| 10128 | return true; |
| 10129 | |
| 10130 | if (kvm_apic_has_events(vcpu)) |
| 10131 | return true; |
| 10132 | |
| 10133 | if (vcpu->arch.pv.pv_unhalted) |
| 10134 | return true; |
| 10135 | |
Wanpeng Li | a5f01f8 | 2017-09-13 04:04:01 -0700 | [diff] [blame] | 10136 | if (vcpu->arch.exception.pending) |
| 10137 | return true; |
| 10138 | |
ZhuangYanying | 47a66ee | 2017-05-26 13:16:48 +0800 | [diff] [blame] | 10139 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
| 10140 | (vcpu->arch.nmi_pending && |
| 10141 | kvm_x86_ops->nmi_allowed(vcpu))) |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10142 | return true; |
| 10143 | |
ZhuangYanying | 47a66ee | 2017-05-26 13:16:48 +0800 | [diff] [blame] | 10144 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
| 10145 | (vcpu->arch.smi_pending && !is_smm(vcpu))) |
Paolo Bonzini | 7391773 | 2015-10-13 10:19:35 +0200 | [diff] [blame] | 10146 | return true; |
| 10147 | |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10148 | if (kvm_arch_interrupt_allowed(vcpu) && |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 10149 | (kvm_cpu_has_interrupt(vcpu) || |
| 10150 | kvm_guest_apic_has_interrupt(vcpu))) |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10151 | return true; |
| 10152 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 10153 | if (kvm_hv_has_stimer_pending(vcpu)) |
| 10154 | return true; |
| 10155 | |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10156 | return false; |
| 10157 | } |
| 10158 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 10159 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 10160 | { |
Paolo Bonzini | 5d9bc64 | 2015-10-13 10:18:53 +0200 | [diff] [blame] | 10161 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 10162 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 10163 | |
Wanpeng Li | 17e433b | 2019-08-05 10:03:19 +0800 | [diff] [blame] | 10164 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
| 10165 | { |
| 10166 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) |
| 10167 | return true; |
| 10168 | |
| 10169 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
| 10170 | kvm_test_request(KVM_REQ_SMI, vcpu) || |
| 10171 | kvm_test_request(KVM_REQ_EVENT, vcpu)) |
| 10172 | return true; |
| 10173 | |
| 10174 | if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu)) |
| 10175 | return true; |
| 10176 | |
| 10177 | return false; |
| 10178 | } |
| 10179 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 10180 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 10181 | { |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 10182 | return vcpu->arch.preempted_in_kernel; |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 10183 | } |
| 10184 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 10185 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 10186 | { |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 10187 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 10188 | } |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 10189 | |
| 10190 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 10191 | { |
| 10192 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 10193 | } |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 10194 | |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 10195 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
| 10196 | { |
| 10197 | if (is_64_bit_mode(vcpu)) |
| 10198 | return kvm_rip_read(vcpu); |
| 10199 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + |
| 10200 | kvm_rip_read(vcpu)); |
| 10201 | } |
| 10202 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); |
| 10203 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 10204 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 10205 | { |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 10206 | return kvm_get_linear_rip(vcpu) == linear_rip; |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 10207 | } |
| 10208 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 10209 | |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 10210 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 10211 | { |
| 10212 | unsigned long rflags; |
| 10213 | |
| 10214 | rflags = kvm_x86_ops->get_rflags(vcpu); |
| 10215 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 10216 | rflags &= ~X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 10217 | return rflags; |
| 10218 | } |
| 10219 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 10220 | |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 10221 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 10222 | { |
| 10223 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 10224 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 10225 | rflags |= X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 10226 | kvm_x86_ops->set_rflags(vcpu, rflags); |
Paolo Bonzini | 6addfc4 | 2014-03-27 11:29:28 +0100 | [diff] [blame] | 10227 | } |
| 10228 | |
| 10229 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 10230 | { |
| 10231 | __kvm_set_rflags(vcpu, rflags); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 10232 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 10233 | } |
| 10234 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 10235 | |
Gleb Natapov | 56028d0 | 2010-10-17 18:13:42 +0200 | [diff] [blame] | 10236 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
| 10237 | { |
| 10238 | int r; |
| 10239 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 10240 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
chai wen | f2e1066 | 2013-10-14 22:22:33 +0800 | [diff] [blame] | 10241 | work->wakeup_all) |
Gleb Natapov | 56028d0 | 2010-10-17 18:13:42 +0200 | [diff] [blame] | 10242 | return; |
| 10243 | |
| 10244 | r = kvm_mmu_reload(vcpu); |
| 10245 | if (unlikely(r)) |
| 10246 | return; |
| 10247 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 10248 | if (!vcpu->arch.mmu->direct_map && |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 10249 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
Xiao Guangrong | fb67e14 | 2010-12-07 10:35:25 +0800 | [diff] [blame] | 10250 | return; |
| 10251 | |
Sean Christopherson | 7a02674 | 2020-02-06 14:14:34 -0800 | [diff] [blame] | 10252 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
Gleb Natapov | 56028d0 | 2010-10-17 18:13:42 +0200 | [diff] [blame] | 10253 | } |
| 10254 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10255 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
| 10256 | { |
| 10257 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
| 10258 | } |
| 10259 | |
| 10260 | static inline u32 kvm_async_pf_next_probe(u32 key) |
| 10261 | { |
| 10262 | return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1); |
| 10263 | } |
| 10264 | |
| 10265 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10266 | { |
| 10267 | u32 key = kvm_async_pf_hash_fn(gfn); |
| 10268 | |
| 10269 | while (vcpu->arch.apf.gfns[key] != ~0) |
| 10270 | key = kvm_async_pf_next_probe(key); |
| 10271 | |
| 10272 | vcpu->arch.apf.gfns[key] = gfn; |
| 10273 | } |
| 10274 | |
| 10275 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10276 | { |
| 10277 | int i; |
| 10278 | u32 key = kvm_async_pf_hash_fn(gfn); |
| 10279 | |
| 10280 | for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) && |
Xiao Guangrong | c7d28c2 | 2010-11-01 17:00:30 +0800 | [diff] [blame] | 10281 | (vcpu->arch.apf.gfns[key] != gfn && |
| 10282 | vcpu->arch.apf.gfns[key] != ~0); i++) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10283 | key = kvm_async_pf_next_probe(key); |
| 10284 | |
| 10285 | return key; |
| 10286 | } |
| 10287 | |
| 10288 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10289 | { |
| 10290 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; |
| 10291 | } |
| 10292 | |
| 10293 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10294 | { |
| 10295 | u32 i, j, k; |
| 10296 | |
| 10297 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); |
| 10298 | while (true) { |
| 10299 | vcpu->arch.apf.gfns[i] = ~0; |
| 10300 | do { |
| 10301 | j = kvm_async_pf_next_probe(j); |
| 10302 | if (vcpu->arch.apf.gfns[j] == ~0) |
| 10303 | return; |
| 10304 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); |
| 10305 | /* |
| 10306 | * k lies cyclically in ]i,j] |
| 10307 | * | i.k.j | |
| 10308 | * |....j i.k.| or |.k..j i...| |
| 10309 | */ |
| 10310 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); |
| 10311 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; |
| 10312 | i = j; |
| 10313 | } |
| 10314 | } |
| 10315 | |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10316 | static int apf_put_user(struct kvm_vcpu *vcpu, u32 val) |
| 10317 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 10318 | |
| 10319 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val, |
| 10320 | sizeof(val)); |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10321 | } |
| 10322 | |
Wanpeng Li | 9a6e7c3 | 2017-09-14 03:54:16 -0700 | [diff] [blame] | 10323 | static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val) |
| 10324 | { |
| 10325 | |
| 10326 | return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val, |
| 10327 | sizeof(u32)); |
| 10328 | } |
| 10329 | |
Paolo Bonzini | 1dfdb45 | 2019-06-05 16:46:44 +0200 | [diff] [blame] | 10330 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
| 10331 | { |
| 10332 | if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu)) |
| 10333 | return false; |
| 10334 | |
| 10335 | if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) || |
| 10336 | (vcpu->arch.apf.send_user_only && |
| 10337 | kvm_x86_ops->get_cpl(vcpu) == 0)) |
| 10338 | return false; |
| 10339 | |
| 10340 | return true; |
| 10341 | } |
| 10342 | |
| 10343 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) |
| 10344 | { |
| 10345 | if (unlikely(!lapic_in_kernel(vcpu) || |
| 10346 | kvm_event_needs_reinjection(vcpu) || |
| 10347 | vcpu->arch.exception.pending)) |
| 10348 | return false; |
| 10349 | |
| 10350 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) |
| 10351 | return false; |
| 10352 | |
| 10353 | /* |
| 10354 | * If interrupts are off we cannot even use an artificial |
| 10355 | * halt state. |
| 10356 | */ |
| 10357 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 10358 | } |
| 10359 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10360 | void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
| 10361 | struct kvm_async_pf *work) |
| 10362 | { |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 10363 | struct x86_exception fault; |
| 10364 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 10365 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10366 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10367 | |
Paolo Bonzini | 1dfdb45 | 2019-06-05 16:46:44 +0200 | [diff] [blame] | 10368 | if (kvm_can_deliver_async_pf(vcpu) && |
| 10369 | !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) { |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 10370 | fault.vector = PF_VECTOR; |
| 10371 | fault.error_code_valid = true; |
| 10372 | fault.error_code = 0; |
| 10373 | fault.nested_page_fault = false; |
| 10374 | fault.address = work->arch.token; |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 10375 | fault.async_page_fault = true; |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 10376 | kvm_inject_page_fault(vcpu, &fault); |
Paolo Bonzini | 1dfdb45 | 2019-06-05 16:46:44 +0200 | [diff] [blame] | 10377 | } else { |
| 10378 | /* |
| 10379 | * It is not possible to deliver a paravirtualized asynchronous |
| 10380 | * page fault, but putting the guest in an artificial halt state |
| 10381 | * can be beneficial nevertheless: if an interrupt arrives, we |
| 10382 | * can deliver it timely and perhaps the guest will schedule |
| 10383 | * another process. When the instruction that triggered a page |
| 10384 | * fault is retried, hopefully the page will be ready in the host. |
| 10385 | */ |
| 10386 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10387 | } |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10388 | } |
| 10389 | |
| 10390 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, |
| 10391 | struct kvm_async_pf *work) |
| 10392 | { |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 10393 | struct x86_exception fault; |
Wanpeng Li | 9a6e7c3 | 2017-09-14 03:54:16 -0700 | [diff] [blame] | 10394 | u32 val; |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 10395 | |
chai wen | f2e1066 | 2013-10-14 22:22:33 +0800 | [diff] [blame] | 10396 | if (work->wakeup_all) |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10397 | work->arch.token = ~0; /* broadcast wakeup */ |
| 10398 | else |
| 10399 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 10400 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10401 | |
Wanpeng Li | 9a6e7c3 | 2017-09-14 03:54:16 -0700 | [diff] [blame] | 10402 | if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED && |
| 10403 | !apf_get_user(vcpu, &val)) { |
| 10404 | if (val == KVM_PV_REASON_PAGE_NOT_PRESENT && |
| 10405 | vcpu->arch.exception.pending && |
| 10406 | vcpu->arch.exception.nr == PF_VECTOR && |
| 10407 | !apf_put_user(vcpu, 0)) { |
| 10408 | vcpu->arch.exception.injected = false; |
| 10409 | vcpu->arch.exception.pending = false; |
| 10410 | vcpu->arch.exception.nr = 0; |
| 10411 | vcpu->arch.exception.has_error_code = false; |
| 10412 | vcpu->arch.exception.error_code = 0; |
Jim Mattson | c851436 | 2018-10-16 14:29:19 -0700 | [diff] [blame] | 10413 | vcpu->arch.exception.has_payload = false; |
| 10414 | vcpu->arch.exception.payload = 0; |
Wanpeng Li | 9a6e7c3 | 2017-09-14 03:54:16 -0700 | [diff] [blame] | 10415 | } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) { |
| 10416 | fault.vector = PF_VECTOR; |
| 10417 | fault.error_code_valid = true; |
| 10418 | fault.error_code = 0; |
| 10419 | fault.nested_page_fault = false; |
| 10420 | fault.address = work->arch.token; |
| 10421 | fault.async_page_fault = true; |
| 10422 | kvm_inject_page_fault(vcpu, &fault); |
| 10423 | } |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10424 | } |
Xiao Guangrong | e6d53e3 | 2010-11-01 17:01:28 +0800 | [diff] [blame] | 10425 | vcpu->arch.apf.halted = false; |
Gleb Natapov | a4fa163 | 2012-05-03 11:36:39 +0300 | [diff] [blame] | 10426 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 10427 | } |
| 10428 | |
| 10429 | bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) |
| 10430 | { |
| 10431 | if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED)) |
| 10432 | return true; |
| 10433 | else |
Wanpeng Li | 9bc1f09 | 2017-06-08 20:13:40 -0700 | [diff] [blame] | 10434 | return kvm_can_do_async_pf(vcpu); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 10435 | } |
| 10436 | |
Paolo Bonzini | 5544eb9 | 2015-07-07 15:41:58 +0200 | [diff] [blame] | 10437 | void kvm_arch_start_assignment(struct kvm *kvm) |
| 10438 | { |
| 10439 | atomic_inc(&kvm->arch.assigned_device_count); |
| 10440 | } |
| 10441 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); |
| 10442 | |
| 10443 | void kvm_arch_end_assignment(struct kvm *kvm) |
| 10444 | { |
| 10445 | atomic_dec(&kvm->arch.assigned_device_count); |
| 10446 | } |
| 10447 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); |
| 10448 | |
| 10449 | bool kvm_arch_has_assigned_device(struct kvm *kvm) |
| 10450 | { |
| 10451 | return atomic_read(&kvm->arch.assigned_device_count); |
| 10452 | } |
| 10453 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); |
| 10454 | |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 10455 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
| 10456 | { |
| 10457 | atomic_inc(&kvm->arch.noncoherent_dma_count); |
| 10458 | } |
| 10459 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); |
| 10460 | |
| 10461 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) |
| 10462 | { |
| 10463 | atomic_dec(&kvm->arch.noncoherent_dma_count); |
| 10464 | } |
| 10465 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); |
| 10466 | |
| 10467 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) |
| 10468 | { |
| 10469 | return atomic_read(&kvm->arch.noncoherent_dma_count); |
| 10470 | } |
| 10471 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); |
| 10472 | |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 10473 | bool kvm_arch_has_irq_bypass(void) |
| 10474 | { |
Sean Christopherson | 92735b1 | 2019-08-02 15:06:17 -0700 | [diff] [blame] | 10475 | return true; |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 10476 | } |
| 10477 | |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10478 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 10479 | struct irq_bypass_producer *prod) |
| 10480 | { |
| 10481 | struct kvm_kernel_irqfd *irqfd = |
| 10482 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 10483 | |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 10484 | irqfd->producer = prod; |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10485 | |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 10486 | return kvm_x86_ops->update_pi_irte(irqfd->kvm, |
| 10487 | prod->irq, irqfd->gsi, 1); |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10488 | } |
| 10489 | |
| 10490 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 10491 | struct irq_bypass_producer *prod) |
| 10492 | { |
| 10493 | int ret; |
| 10494 | struct kvm_kernel_irqfd *irqfd = |
| 10495 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 10496 | |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10497 | WARN_ON(irqfd->producer != prod); |
| 10498 | irqfd->producer = NULL; |
| 10499 | |
| 10500 | /* |
| 10501 | * When producer of consumer is unregistered, we change back to |
| 10502 | * remapped mode, so we can re-use the current implementation |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 10503 | * when the irq is masked/disabled or the consumer side (KVM |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10504 | * int this case doesn't want to receive the interrupts. |
| 10505 | */ |
| 10506 | ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
| 10507 | if (ret) |
| 10508 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" |
| 10509 | " fails: %d\n", irqfd->consumer.token, ret); |
| 10510 | } |
| 10511 | |
| 10512 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, |
| 10513 | uint32_t guest_irq, bool set) |
| 10514 | { |
Feng Wu | 8727688 | 2015-09-18 22:29:40 +0800 | [diff] [blame] | 10515 | return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set); |
| 10516 | } |
| 10517 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 10518 | bool kvm_vector_hashing_enabled(void) |
| 10519 | { |
| 10520 | return vector_hashing; |
| 10521 | } |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 10522 | |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 10523 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
| 10524 | { |
| 10525 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; |
| 10526 | } |
| 10527 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); |
| 10528 | |
Paolo Bonzini | 6441fa6 | 2020-01-20 16:33:06 +0100 | [diff] [blame] | 10529 | u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu) |
| 10530 | { |
| 10531 | uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD; |
| 10532 | |
| 10533 | /* The STIBP bit doesn't fault even if it's not advertised */ |
| 10534 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) && |
| 10535 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS)) |
| 10536 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); |
| 10537 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) && |
| 10538 | !boot_cpu_has(X86_FEATURE_AMD_IBRS)) |
| 10539 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); |
| 10540 | |
| 10541 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) && |
| 10542 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) |
| 10543 | bits &= ~SPEC_CTRL_SSBD; |
| 10544 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) && |
| 10545 | !boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
| 10546 | bits &= ~SPEC_CTRL_SSBD; |
| 10547 | |
| 10548 | return bits; |
| 10549 | } |
| 10550 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits); |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 10551 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 10552 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 10553 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 10554 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 10555 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 10556 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 10557 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 10558 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 10559 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 10560 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 10561 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 10562 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 10563 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 10564 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
Joerg Roedel | 2e554e8 | 2010-02-24 18:59:14 +0100 | [diff] [blame] | 10565 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 10566 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 10567 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10568 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 10569 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 10570 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
| 10571 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); |
Suravee Suthikulpanit | ab56f8e | 2020-03-12 05:39:28 -0500 | [diff] [blame] | 10572 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
Suravee Suthikulpanit | 24bbf74 | 2019-11-14 14:15:07 -0600 | [diff] [blame] | 10573 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request); |