blob: 375d3fc0a4a6f25de649dedb865e176a1c58f9a5 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Carsten Otte043405e2007-10-10 17:16:19 +02002/*
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 Yassour4d5c5d02008-07-28 19:26:26 +03008 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020011 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030015 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020020#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080022#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020023#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -080025#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030026#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020027#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020028#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030029#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060030#include "lapic.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020031
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020032#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030033#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020034#include <linux/kvm.h>
35#include <linux/fs.h>
36#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040037#include <linux/export.h>
38#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080039#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050040#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010041#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010043#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030044#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020045#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080047#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080048#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020049#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020050#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020051#include <linux/timekeeper_internal.h>
52#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080053#include <linux/kvm_irqfd.h>
54#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010055#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080056#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050057#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010058
Avi Kivityaec51dc2009-07-01 16:01:02 +030059#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010070#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010071#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080072#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090073#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000074#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020075
Dave Hansend1898b72016-06-01 10:42:20 -070076#define CREATE_TRACE_POINTS
77#include "trace.h"
78
Carsten Otte313a3dc2007-10-11 19:16:52 +020079#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080080#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080081u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
82EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080083
Avi Kivity0f65dd72011-04-20 13:37:53 +030084#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080085 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030086
Joerg Roedel50a37eb2008-01-31 14:57:38 +010087/* 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 Jiangshan1260edbe2011-02-21 11:51:35 +080092static
93u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080095static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010096#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020097
Sean Christophersonb11306b2019-12-10 14:44:13 -080098static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
99
Radim Krčmářc5192652016-07-12 22:09:28 +0200100#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
101 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200102
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300103static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300104static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200105static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100106static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800107static void store_regs(struct kvm_vcpu *vcpu);
108static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200109
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700110struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300111EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800112
Paolo Bonzini893590c2015-11-06 11:46:24 +0100113static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030114module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200115
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200116static bool __read_mostly report_ignored_msrs = true;
117module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
118
Wanpeng Li4c276252018-05-05 04:02:32 -0700119unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200120module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
121
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300122static bool __read_mostly kvmclock_periodic_sync = true;
123module_param(kvmclock_periodic_sync, bool, S_IRUGO);
124
Paolo Bonzini893590c2015-11-06 11:46:24 +0100125bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100126EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100127u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100128EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800129u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
130EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
131u64 __read_mostly kvm_max_tsc_scaling_ratio;
132EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700133u64 __read_mostly kvm_default_tsc_scaling_ratio;
134EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100135
Zachary Amsdencc578282012-02-03 15:43:50 -0200136/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100137static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200138module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
139
Sean Christophersonc3941d92019-04-17 10:15:33 -0700140/*
141 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
142 * adaptive tuning starting from default advancment of 1000ns. '0' disables
143 * advancement entirely. Any other value is used as-is and disables adaptive
144 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
145 */
146static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800147module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500148
Feng Wu520040142016-01-25 16:53:33 +0800149static bool __read_mostly vector_hashing = true;
150module_param(vector_hashing, bool, S_IRUGO);
151
Liran Alonc4ae60e2018-03-12 13:12:47 +0200152bool __read_mostly enable_vmware_backdoor = false;
153module_param(enable_vmware_backdoor, bool, S_IRUGO);
154EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
155
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700156static bool __read_mostly force_emulation_prefix = false;
157module_param(force_emulation_prefix, bool, S_IRUGO);
158
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800159int __read_mostly pi_inject_timer = -1;
160module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
161
Avi Kivity18863bd2009-09-07 11:12:18 +0300162#define KVM_NR_SHARED_MSRS 16
163
164struct kvm_shared_msrs_global {
165 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800166 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300167};
168
169struct kvm_shared_msrs {
170 struct user_return_notifier urn;
171 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800172 struct kvm_shared_msr_values {
173 u64 host;
174 u64 curr;
175 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300176};
177
178static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200179static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300180
Sean Christophersoncfc48182020-03-02 15:56:23 -0800181#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
182 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
183 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
184 | XFEATURE_MASK_PKRU)
185
Sean Christopherson91661982020-03-02 15:57:06 -0800186u64 __read_mostly host_efer;
187EXPORT_SYMBOL_GPL(host_efer);
188
Aaron Lewis139a12c2019-10-21 16:30:25 -0700189static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100190u64 __read_mostly supported_xss;
191EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700192
Hollis Blanchard417bc302007-10-31 17:24:23 -0500193struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200194 VCPU_STAT("pf_fixed", pf_fixed),
195 VCPU_STAT("pf_guest", pf_guest),
196 VCPU_STAT("tlb_flush", tlb_flush),
197 VCPU_STAT("invlpg", invlpg),
198 VCPU_STAT("exits", exits),
199 VCPU_STAT("io_exits", io_exits),
200 VCPU_STAT("mmio_exits", mmio_exits),
201 VCPU_STAT("signal_exits", signal_exits),
202 VCPU_STAT("irq_window", irq_window_exits),
203 VCPU_STAT("nmi_window", nmi_window_exits),
204 VCPU_STAT("halt_exits", halt_exits),
205 VCPU_STAT("halt_successful_poll", halt_successful_poll),
206 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
207 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
208 VCPU_STAT("halt_wakeup", halt_wakeup),
209 VCPU_STAT("hypercalls", hypercalls),
210 VCPU_STAT("request_irq", request_irq_exits),
211 VCPU_STAT("irq_exits", irq_exits),
212 VCPU_STAT("host_state_reload", host_state_reload),
213 VCPU_STAT("fpu_reload", fpu_reload),
214 VCPU_STAT("insn_emulation", insn_emulation),
215 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
216 VCPU_STAT("irq_injections", irq_injections),
217 VCPU_STAT("nmi_injections", nmi_injections),
218 VCPU_STAT("req_event", req_event),
219 VCPU_STAT("l1d_flush", l1d_flush),
David Matlackcb953122020-05-08 11:22:40 -0700220 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
221 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200222 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
223 VM_STAT("mmu_pte_write", mmu_pte_write),
224 VM_STAT("mmu_pte_updated", mmu_pte_updated),
225 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
226 VM_STAT("mmu_flooded", mmu_flooded),
227 VM_STAT("mmu_recycled", mmu_recycled),
228 VM_STAT("mmu_cache_miss", mmu_cache_miss),
229 VM_STAT("mmu_unsync", mmu_unsync),
230 VM_STAT("remote_tlb_flush", remote_tlb_flush),
231 VM_STAT("largepages", lpages, .mode = 0444),
232 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
233 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500234 { NULL }
235};
236
Dexuan Cui2acf9232010-06-10 11:27:12 +0800237u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800238u64 __read_mostly supported_xcr0;
239EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800240
Marc Orrb666a4b2018-11-06 14:53:56 -0800241struct kmem_cache *x86_fpu_cache;
242EXPORT_SYMBOL_GPL(x86_fpu_cache);
243
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800244static struct kmem_cache *x86_emulator_cache;
245
246static struct kmem_cache *kvm_alloc_emulator_cache(void)
247{
Sean Christopherson06add252020-02-18 15:29:50 -0800248 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
249 unsigned int size = sizeof(struct x86_emulate_ctxt);
250
251 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800252 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800253 SLAB_ACCOUNT, useroffset,
254 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800255}
256
Jan Kiszkab6785de2012-09-20 07:43:17 +0200257static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300258
Gleb Natapovaf585b92010-10-14 11:22:46 +0200259static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
260{
261 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400262 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200263 vcpu->arch.apf.gfns[i] = ~0;
264}
265
Avi Kivity18863bd2009-09-07 11:12:18 +0300266static void kvm_on_user_return(struct user_return_notifier *urn)
267{
268 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300269 struct kvm_shared_msrs *locals
270 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800271 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700272 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300273
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700274 /*
275 * Disabling irqs at this point since the following code could be
276 * interrupted and executed through kvm_arch_hardware_disable()
277 */
278 local_irq_save(flags);
279 if (locals->registered) {
280 locals->registered = false;
281 user_return_notifier_unregister(urn);
282 }
283 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300284 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800285 values = &locals->values[slot];
286 if (values->host != values->curr) {
287 wrmsrl(shared_msrs_global.msrs[slot], values->host);
288 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300289 }
290 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300291}
292
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800293void kvm_define_shared_msr(unsigned slot, u32 msr)
294{
Nadav Amit0123be42014-07-24 15:06:56 +0300295 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200296 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300297 if (slot >= shared_msrs_global.nr)
298 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300299}
300EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
301
302static void kvm_shared_msr_cpu_online(void)
303{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800304 unsigned int cpu = smp_processor_id();
305 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
306 u64 value;
307 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300308
Sean Christopherson05c19c22019-11-22 12:04:50 -0800309 for (i = 0; i < shared_msrs_global.nr; ++i) {
310 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
311 smsr->values[i].host = value;
312 smsr->values[i].curr = value;
313 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300314}
315
Andy Honig8b3c3102014-08-27 11:16:44 -0700316int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300317{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200318 unsigned int cpu = smp_processor_id();
319 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700320 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300321
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500322 value = (value & mask) | (smsr->values[slot].host & ~mask);
323 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700324 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700325 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
326 if (err)
327 return 1;
328
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500329 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300330 if (!smsr->registered) {
331 smsr->urn.on_user_return = kvm_on_user_return;
332 user_return_notifier_register(&smsr->urn);
333 smsr->registered = true;
334 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700335 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300336}
337EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
338
Radim Krčmář13a34e02014-08-28 15:13:03 +0200339static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200340{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200341 unsigned int cpu = smp_processor_id();
342 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200343
344 if (smsr->registered)
345 kvm_on_user_return(&smsr->urn);
346}
347
Carsten Otte6866b832007-10-29 16:09:10 +0100348u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
349{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300350 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100351}
352EXPORT_SYMBOL_GPL(kvm_get_apic_base);
353
Jim Mattson58871642018-05-09 16:56:04 -0400354enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
355{
356 return kvm_apic_mode(kvm_get_apic_base(vcpu));
357}
358EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
359
Jan Kiszka58cb6282014-01-24 16:48:44 +0100360int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100361{
Jim Mattson58871642018-05-09 16:56:04 -0400362 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
363 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200364 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
365 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100366
Jim Mattson58871642018-05-09 16:56:04 -0400367 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700368 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400369 if (!msr_info->host_initiated) {
370 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
371 return 1;
372 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
373 return 1;
374 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100375
376 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800377 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100378 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100379}
380EXPORT_SYMBOL_GPL(kvm_set_apic_base);
381
Andi Kleen2605fc22014-05-02 00:44:37 +0200382asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000383{
384 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200385 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000386}
387EXPORT_SYMBOL_GPL(kvm_spurious_fault);
388
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200389#define EXCPT_BENIGN 0
390#define EXCPT_CONTRIBUTORY 1
391#define EXCPT_PF 2
392
393static int exception_class(int vector)
394{
395 switch (vector) {
396 case PF_VECTOR:
397 return EXCPT_PF;
398 case DE_VECTOR:
399 case TS_VECTOR:
400 case NP_VECTOR:
401 case SS_VECTOR:
402 case GP_VECTOR:
403 return EXCPT_CONTRIBUTORY;
404 default:
405 break;
406 }
407 return EXCPT_BENIGN;
408}
409
Nadav Amitd6e8c852014-07-24 14:51:24 +0300410#define EXCPT_FAULT 0
411#define EXCPT_TRAP 1
412#define EXCPT_ABORT 2
413#define EXCPT_INTERRUPT 3
414
415static int exception_type(int vector)
416{
417 unsigned int mask;
418
419 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
420 return EXCPT_INTERRUPT;
421
422 mask = 1 << vector;
423
424 /* #DB is trap, as instruction watchpoints are handled elsewhere */
425 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
426 return EXCPT_TRAP;
427
428 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
429 return EXCPT_ABORT;
430
431 /* Reserved exceptions will result in fault */
432 return EXCPT_FAULT;
433}
434
Jim Mattsonda998b42018-10-16 14:29:22 -0700435void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
436{
437 unsigned nr = vcpu->arch.exception.nr;
438 bool has_payload = vcpu->arch.exception.has_payload;
439 unsigned long payload = vcpu->arch.exception.payload;
440
441 if (!has_payload)
442 return;
443
444 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700445 case DB_VECTOR:
446 /*
447 * "Certain debug exceptions may clear bit 0-3. The
448 * remaining contents of the DR6 register are never
449 * cleared by the processor".
450 */
451 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
452 /*
453 * DR6.RTM is set by all #DB exceptions that don't clear it.
454 */
455 vcpu->arch.dr6 |= DR6_RTM;
456 vcpu->arch.dr6 |= payload;
457 /*
458 * Bit 16 should be set in the payload whenever the #DB
459 * exception should clear DR6.RTM. This makes the payload
460 * compatible with the pending debug exceptions under VMX.
461 * Though not currently documented in the SDM, this also
462 * makes the payload compatible with the exit qualification
463 * for #DB exceptions under VMX.
464 */
465 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800466
467 /*
468 * The #DB payload is defined as compatible with the 'pending
469 * debug exceptions' field under VMX, not DR6. While bit 12 is
470 * defined in the 'pending debug exceptions' field (enabled
471 * breakpoint), it is reserved and must be zero in DR6.
472 */
473 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700474 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700475 case PF_VECTOR:
476 vcpu->arch.cr2 = payload;
477 break;
478 }
479
480 vcpu->arch.exception.has_payload = false;
481 vcpu->arch.exception.payload = 0;
482}
483EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
484
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200485static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200486 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700487 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200488{
489 u32 prev_nr;
490 int class1, class2;
491
Avi Kivity3842d132010-07-27 12:30:24 +0300492 kvm_make_request(KVM_REQ_EVENT, vcpu);
493
Wanpeng Li664f8e22017-08-24 03:35:09 -0700494 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200495 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200496 if (has_error && !is_protmode(vcpu))
497 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700498 if (reinject) {
499 /*
500 * On vmentry, vcpu->arch.exception.pending is only
501 * true if an event injection was blocked by
502 * nested_run_pending. In that case, however,
503 * vcpu_enter_guest requests an immediate exit,
504 * and the guest shouldn't proceed far enough to
505 * need reinjection.
506 */
507 WARN_ON_ONCE(vcpu->arch.exception.pending);
508 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700509 if (WARN_ON_ONCE(has_payload)) {
510 /*
511 * A reinjected event has already
512 * delivered its payload.
513 */
514 has_payload = false;
515 payload = 0;
516 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700517 } else {
518 vcpu->arch.exception.pending = true;
519 vcpu->arch.exception.injected = false;
520 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200521 vcpu->arch.exception.has_error_code = has_error;
522 vcpu->arch.exception.nr = nr;
523 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700524 vcpu->arch.exception.has_payload = has_payload;
525 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800526 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700527 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200528 return;
529 }
530
531 /* to check exception */
532 prev_nr = vcpu->arch.exception.nr;
533 if (prev_nr == DF_VECTOR) {
534 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300535 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200536 return;
537 }
538 class1 = exception_class(prev_nr);
539 class2 = exception_class(nr);
540 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
541 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700542 /*
543 * Generate double fault per SDM Table 5-5. Set
544 * exception.pending = true so that the double fault
545 * can trigger a nested vmexit.
546 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200547 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700548 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200549 vcpu->arch.exception.has_error_code = true;
550 vcpu->arch.exception.nr = DF_VECTOR;
551 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700552 vcpu->arch.exception.has_payload = false;
553 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200554 } else
555 /* replace previous exception with a new one in a hope
556 that instruction re-execution will regenerate lost
557 exception */
558 goto queue;
559}
560
Avi Kivity298101d2007-11-25 13:41:11 +0200561void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
562{
Jim Mattson91e86d22018-10-16 14:29:21 -0700563 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200564}
565EXPORT_SYMBOL_GPL(kvm_queue_exception);
566
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200567void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
568{
Jim Mattson91e86d22018-10-16 14:29:21 -0700569 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200570}
571EXPORT_SYMBOL_GPL(kvm_requeue_exception);
572
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400573void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
574 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700575{
576 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
577}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400578EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700579
Jim Mattsonda998b42018-10-16 14:29:22 -0700580static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
581 u32 error_code, unsigned long payload)
582{
583 kvm_multiple_exception(vcpu, nr, true, error_code,
584 true, payload, false);
585}
586
Kyle Huey6affcbe2016-11-29 12:40:40 -0800587int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200588{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100589 if (err)
590 kvm_inject_gp(vcpu, 0);
591 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800592 return kvm_skip_emulated_instruction(vcpu);
593
594 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100595}
596EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200597
Avi Kivity6389ee92010-11-29 16:12:30 +0200598void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200599{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200600 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700601 vcpu->arch.exception.nested_apf =
602 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700603 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700604 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700605 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
606 } else {
607 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
608 fault->address);
609 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200610}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300611EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200612
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700613bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
614 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200615{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400616 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700617 WARN_ON_ONCE(fault->vector != PF_VECTOR);
618
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400619 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
620 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200621
Junaid Shahidee1fa202020-03-20 14:28:03 -0700622 /*
623 * Invalidate the TLB entry for the faulting address, if it exists,
624 * else the access will fault indefinitely (and to emulate hardware).
625 */
626 if ((fault->error_code & PFERR_PRESENT_MASK) &&
627 !(fault->error_code & PFERR_RSVD_MASK))
628 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
629 fault_mmu->root_hpa);
630
631 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200632 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200633}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700634EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200635
Sheng Yang3419ffc2008-05-15 09:52:48 +0800636void kvm_inject_nmi(struct kvm_vcpu *vcpu)
637{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300638 atomic_inc(&vcpu->arch.nmi_queued);
639 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800640}
641EXPORT_SYMBOL_GPL(kvm_inject_nmi);
642
Avi Kivity298101d2007-11-25 13:41:11 +0200643void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
644{
Jim Mattson91e86d22018-10-16 14:29:21 -0700645 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200646}
647EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
648
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200649void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
650{
Jim Mattson91e86d22018-10-16 14:29:21 -0700651 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200652}
653EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
654
Carsten Ottea03490e2007-10-29 16:09:35 +0100655/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300656 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
657 * a #GP and return false.
658 */
659bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200660{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700661 if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300662 return true;
663 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
664 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100665}
Avi Kivity0a79b002009-09-01 12:03:25 +0300666EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100667
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300668bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
669{
670 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
671 return true;
672
673 kvm_queue_exception(vcpu, UD_VECTOR);
674 return false;
675}
676EXPORT_SYMBOL_GPL(kvm_require_dr);
677
Carsten Ottea03490e2007-10-29 16:09:35 +0100678/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200679 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200680 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200681 * can read from guest physical or from the guest's guest physical memory.
682 */
683int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
684 gfn_t ngfn, void *data, int offset, int len,
685 u32 access)
686{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200687 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200688 gfn_t real_gfn;
689 gpa_t ngpa;
690
691 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200692 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200693 if (real_gfn == UNMAPPED_GVA)
694 return -EFAULT;
695
696 real_gfn = gpa_to_gfn(real_gfn);
697
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200698 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200699}
700EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
701
Fengguang Wu69b00492015-01-19 22:33:39 +0800702static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200703 void *data, int offset, int len, u32 access)
704{
705 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
706 data, offset, len, access);
707}
708
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700709static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
710{
711 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
712 rsvd_bits(1, 2);
713}
714
Joerg Roedelec92fe42010-09-10 17:30:51 +0200715/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700716 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100717 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200718int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100719{
720 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
721 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
722 int i;
723 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200724 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100725
Joerg Roedelff03a072010-09-10 17:30:57 +0200726 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
727 offset * sizeof(u64), sizeof(pdpte),
728 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100729 if (ret < 0) {
730 ret = 0;
731 goto out;
732 }
733 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400734 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700735 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100736 ret = 0;
737 goto out;
738 }
739 }
740 ret = 1;
741
Joerg Roedelff03a072010-09-10 17:30:57 +0200742 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700743 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
744
Carsten Ottea03490e2007-10-29 16:09:35 +0100745out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100746
747 return ret;
748}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100749EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100750
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100751bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200752{
Joerg Roedelff03a072010-09-10 17:30:57 +0200753 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200754 int offset;
755 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200756 int r;
757
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200758 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200759 return false;
760
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700761 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300762 return true;
763
Paolo Bonzinia5121772017-07-24 18:54:38 +0200764 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
765 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200766 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
767 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200768 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800769 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200770
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800771 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200772}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100773EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200774
Avi Kivity49a9b072010-06-10 17:02:14 +0300775int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100776{
Sheng Yangaad82702010-05-12 16:40:42 +0800777 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800778 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800779
Avi Kivityf9a48e62010-01-06 19:10:22 +0200780 cr0 |= X86_CR0_ET;
781
Gleb Natapovab344822010-01-21 15:28:46 +0200782#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300783 if (cr0 & 0xffffffff00000000UL)
784 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200785#endif
786
787 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100788
Gleb Natapov0f122442010-04-28 19:15:31 +0300789 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
790 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100791
Gleb Natapov0f122442010-04-28 19:15:31 +0300792 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
793 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100794
795 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
796#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200797 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100798 int cs_db, cs_l;
799
Gleb Natapov0f122442010-04-28 19:15:31 +0300800 if (!is_pae(vcpu))
801 return 1;
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700802 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300803 if (cs_l)
804 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100805 } else
806#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200807 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200808 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300809 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100810 }
811
Mao, Junjiead756a12012-07-02 01:18:48 +0000812 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
813 return 1;
814
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700815 kvm_x86_ops.set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100816
Lai Jiangshand170c412011-02-21 11:21:30 +0800817 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800818 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800819 kvm_async_pf_hash_reset(vcpu);
820 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800821
Sheng Yangaad82702010-05-12 16:40:42 +0800822 if ((cr0 ^ old_cr0) & update_bits)
823 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800824
Laszlo Ersek879ae182015-11-04 12:54:41 +0100825 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
826 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
827 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800828 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
829
Gleb Natapov0f122442010-04-28 19:15:31 +0300830 return 0;
831}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200832EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100833
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200834void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100835{
Avi Kivity49a9b072010-06-10 17:02:14 +0300836 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100837}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200838EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100839
Aaron Lewis139a12c2019-10-21 16:30:25 -0700840void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300841{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700842 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300843
Aaron Lewis139a12c2019-10-21 16:30:25 -0700844 if (vcpu->arch.xcr0 != host_xcr0)
845 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
846
847 if (vcpu->arch.xsaves_enabled &&
848 vcpu->arch.ia32_xss != host_xss)
849 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
850 }
Babu Moger37486132020-05-12 18:59:06 -0500851
852 if (static_cpu_has(X86_FEATURE_PKU) &&
853 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
854 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
855 vcpu->arch.pkru != vcpu->arch.host_pkru)
856 __write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300857}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700858EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
859
860void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
861{
Babu Moger37486132020-05-12 18:59:06 -0500862 if (static_cpu_has(X86_FEATURE_PKU) &&
863 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
864 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
865 vcpu->arch.pkru = rdpkru();
866 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
867 __write_pkru(vcpu->arch.host_pkru);
868 }
869
Aaron Lewis139a12c2019-10-21 16:30:25 -0700870 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
871
872 if (vcpu->arch.xcr0 != host_xcr0)
873 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
874
875 if (vcpu->arch.xsaves_enabled &&
876 vcpu->arch.ia32_xss != host_xss)
877 wrmsrl(MSR_IA32_XSS, host_xss);
878 }
879
880}
881EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300882
Fengguang Wu69b00492015-01-19 22:33:39 +0800883static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800884{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000885 u64 xcr0 = xcr;
886 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200887 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800888
889 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
890 if (index != XCR_XFEATURE_ENABLED_MASK)
891 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700892 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800893 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700894 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800895 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200896
897 /*
898 * Do not allow the guest to set bits that we do not support
899 * saving. However, xcr0 bit 0 is always set, even if the
900 * emulated CPU does not support XSAVE (see fx_init).
901 */
Dave Hansend91cab72015-09-02 16:31:26 -0700902 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200903 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800904 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200905
Dave Hansend91cab72015-09-02 16:31:26 -0700906 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
907 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000908 return 1;
909
Dave Hansend91cab72015-09-02 16:31:26 -0700910 if (xcr0 & XFEATURE_MASK_AVX512) {
911 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800912 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700913 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800914 return 1;
915 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800916 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000917
Dave Hansend91cab72015-09-02 16:31:26 -0700918 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000919 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800920 return 0;
921}
922
923int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
924{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700925 if (kvm_x86_ops.get_cpl(vcpu) != 0 ||
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000926 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800927 kvm_inject_gp(vcpu, 0);
928 return 1;
929 }
930 return 0;
931}
932EXPORT_SYMBOL_GPL(kvm_set_xcr);
933
Sean Christopherson345599f2019-12-10 14:44:16 -0800934#define __cr4_reserved_bits(__cpu_has, __c) \
935({ \
936 u64 __reserved_bits = CR4_RESERVED_BITS; \
937 \
938 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
939 __reserved_bits |= X86_CR4_OSXSAVE; \
940 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
941 __reserved_bits |= X86_CR4_SMEP; \
942 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
943 __reserved_bits |= X86_CR4_SMAP; \
944 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
945 __reserved_bits |= X86_CR4_FSGSBASE; \
946 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
947 __reserved_bits |= X86_CR4_PKE; \
948 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
949 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800950 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
951 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800952 __reserved_bits; \
953})
954
Wanpeng Li3ca94192019-09-18 17:50:10 +0800955static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
956{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800957 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800958 return -EINVAL;
959
Sean Christopherson345599f2019-12-10 14:44:16 -0800960 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800961 return -EINVAL;
962
963 return 0;
964}
965
Avi Kivitya83b29c2010-06-10 17:02:15 +0300966int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100967{
Avi Kivityfc78f512009-12-07 12:16:48 +0200968 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800969 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800970 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800971
Wanpeng Li3ca94192019-09-18 17:50:10 +0800972 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200973 return 1;
974
Carsten Ottea03490e2007-10-29 16:09:35 +0100975 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300976 if (!(cr4 & X86_CR4_PAE))
977 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300978 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
979 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200980 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
981 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300982 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100983
Mao, Junjiead756a12012-07-02 01:18:48 +0000984 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200985 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000986 return 1;
987
988 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
989 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
990 return 1;
991 }
992
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700993 if (kvm_x86_ops.set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300994 return 1;
995
Mao, Junjiead756a12012-07-02 01:18:48 +0000996 if (((cr4 ^ old_cr4) & pdptr_bits) ||
997 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800998 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300999
Huaitong Hanb9baba82016-03-22 16:51:21 +08001000 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +02001001 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001002
Gleb Natapov0f122442010-04-28 19:15:31 +03001003 return 0;
1004}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001005EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001006
Avi Kivity23902182010-06-10 17:02:16 +03001007int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001008{
Junaid Shahidade61e22018-06-27 14:59:15 -07001009 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001010#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001011 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1012
Junaid Shahidade61e22018-06-27 14:59:15 -07001013 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001014 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1015 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001016 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001017#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001018
Avi Kivity9f8fe502010-12-05 17:30:00 +02001019 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001020 if (!skip_tlb_flush) {
1021 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001022 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001023 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001024 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001025 }
1026
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001027 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001028 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001029 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001030 else if (is_pae_paging(vcpu) &&
1031 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001032 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001033
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001034 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001035 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001036 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001037
Gleb Natapov0f122442010-04-28 19:15:31 +03001038 return 0;
1039}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001040EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001041
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001042int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001043{
Gleb Natapov0f122442010-04-28 19:15:31 +03001044 if (cr8 & CR8_RESERVED_BITS)
1045 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001046 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001047 kvm_lapic_set_tpr(vcpu, cr8);
1048 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001049 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001050 return 0;
1051}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001052EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001053
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001054unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001055{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001056 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001057 return kvm_lapic_get_cr8(vcpu);
1058 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001059 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001060}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001061EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001062
Nadav Amitae561ed2015-04-02 03:10:37 +03001063static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1064{
1065 int i;
1066
1067 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1068 for (i = 0; i < KVM_NR_DB_REGS; i++)
1069 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1070 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1071 }
1072}
1073
Jan Kiszkac8639012012-09-21 05:42:55 +02001074static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1075{
1076 unsigned long dr7;
1077
1078 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1079 dr7 = vcpu->arch.guest_debug_dr7;
1080 else
1081 dr7 = vcpu->arch.dr7;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001082 kvm_x86_ops.set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001083 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1084 if (dr7 & DR7_BP_EN_MASK)
1085 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001086}
1087
Nadav Amit6f43ed02014-07-15 17:37:46 +03001088static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1089{
1090 u64 fixed = DR6_FIXED_1;
1091
Radim Krčmářd6321d42017-08-05 00:12:49 +02001092 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001093 fixed |= DR6_RTM;
1094 return fixed;
1095}
1096
Gleb Natapov338dbc92010-04-28 19:15:32 +03001097static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001098{
Marios Pomonisea740052019-12-11 12:47:52 -08001099 size_t size = ARRAY_SIZE(vcpu->arch.db);
1100
Gleb Natapov020df072010-04-13 10:05:23 +03001101 switch (dr) {
1102 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001103 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001104 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1105 vcpu->arch.eff_db[dr] = val;
1106 break;
1107 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001108 /* fall through */
1109 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001110 if (val & 0xffffffff00000000ULL)
1111 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001112 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001113 break;
1114 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001115 /* fall through */
1116 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001117 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001118 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001119 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001120 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001121 break;
1122 }
1123
1124 return 0;
1125}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001126
1127int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1128{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001129 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001130 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001131 return 1;
1132 }
1133 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001134}
Gleb Natapov020df072010-04-13 10:05:23 +03001135EXPORT_SYMBOL_GPL(kvm_set_dr);
1136
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001137int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001138{
Marios Pomonisea740052019-12-11 12:47:52 -08001139 size_t size = ARRAY_SIZE(vcpu->arch.db);
1140
Gleb Natapov020df072010-04-13 10:05:23 +03001141 switch (dr) {
1142 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001143 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001144 break;
1145 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001146 /* fall through */
1147 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001148 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001149 break;
1150 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001151 /* fall through */
1152 default: /* 7 */
1153 *val = vcpu->arch.dr7;
1154 break;
1155 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001156 return 0;
1157}
Gleb Natapov020df072010-04-13 10:05:23 +03001158EXPORT_SYMBOL_GPL(kvm_get_dr);
1159
Avi Kivity022cd0e2011-11-10 14:57:23 +02001160bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1161{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001162 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001163 u64 data;
1164 int err;
1165
Wei Huangc6702c92015-06-19 13:44:45 +02001166 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001167 if (err)
1168 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001169 kvm_rax_write(vcpu, (u32)data);
1170 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001171 return err;
1172}
1173EXPORT_SYMBOL_GPL(kvm_rdpmc);
1174
Carsten Otte043405e2007-10-10 17:16:19 +02001175/*
1176 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1177 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1178 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001179 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1180 * extract the supported MSRs from the related const lists.
1181 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001182 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001183 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001184 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001185 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001186
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001187static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001188 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001189 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001190#ifdef CONFIG_X86_64
1191 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1192#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001193 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001194 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001195 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001196 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1197 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1198 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1199 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1200 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1201 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001202 MSR_IA32_UMWAIT_CONTROL,
1203
Jim Mattsone2ada662019-08-21 11:20:04 -07001204 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1205 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1206 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1207 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1208 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1209 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1210 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1212 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1213 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1214 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1215 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1216 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001217 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1218 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1219 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1221 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1222 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1223 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1224 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1225 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001226};
1227
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001228static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001229static unsigned num_msrs_to_save;
1230
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001231static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001232 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1233 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1234 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1235 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001236 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001237 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1238 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001239 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001240 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001241 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001242 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001243 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001244 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001245 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1246 HV_X64_MSR_TSC_EMULATION_STATUS,
1247
1248 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001249 MSR_KVM_PV_EOI_EN,
1250
Will Auldba904632012-11-29 12:42:50 -08001251 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001252 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001253 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001254 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001255 MSR_IA32_MCG_STATUS,
1256 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001257 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001258 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001259 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001260 MSR_PLATFORM_INFO,
1261 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001262 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001263 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001264 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001265
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001266 /*
1267 * The following list leaves out MSRs whose values are determined
1268 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1269 * We always support the "true" VMX control MSRs, even if the host
1270 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001271 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001272 */
1273 MSR_IA32_VMX_BASIC,
1274 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1275 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1276 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1277 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1278 MSR_IA32_VMX_MISC,
1279 MSR_IA32_VMX_CR0_FIXED0,
1280 MSR_IA32_VMX_CR4_FIXED0,
1281 MSR_IA32_VMX_VMCS_ENUM,
1282 MSR_IA32_VMX_PROCBASED_CTLS2,
1283 MSR_IA32_VMX_EPT_VPID_CAP,
1284 MSR_IA32_VMX_VMFUNC,
1285
Borislav Petkov191c8132019-04-18 18:32:50 +02001286 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001287 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001288};
1289
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001290static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001291static unsigned num_emulated_msrs;
1292
Tom Lendacky801e4592018-02-21 13:39:51 -06001293/*
1294 * List of msr numbers which are used to expose MSR-based features that
1295 * can be used by a hypervisor to validate requested CPU features.
1296 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001297static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001298 MSR_IA32_VMX_BASIC,
1299 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1300 MSR_IA32_VMX_PINBASED_CTLS,
1301 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1302 MSR_IA32_VMX_PROCBASED_CTLS,
1303 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1304 MSR_IA32_VMX_EXIT_CTLS,
1305 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1306 MSR_IA32_VMX_ENTRY_CTLS,
1307 MSR_IA32_VMX_MISC,
1308 MSR_IA32_VMX_CR0_FIXED0,
1309 MSR_IA32_VMX_CR0_FIXED1,
1310 MSR_IA32_VMX_CR4_FIXED0,
1311 MSR_IA32_VMX_CR4_FIXED1,
1312 MSR_IA32_VMX_VMCS_ENUM,
1313 MSR_IA32_VMX_PROCBASED_CTLS2,
1314 MSR_IA32_VMX_EPT_VPID_CAP,
1315 MSR_IA32_VMX_VMFUNC,
1316
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001317 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001318 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001319 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001320};
1321
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001322static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001323static unsigned int num_msr_based_features;
1324
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001325static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001326{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001327 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001328
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001329 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1330 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001331
1332 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001333 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1334 * the nested hypervisor runs with NX huge pages. If it is not,
1335 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1336 * L1 guests, so it need not worry about its own (L2) guests.
1337 */
1338 data |= ARCH_CAP_PSCHANGE_MC_NO;
1339
1340 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001341 * If we're doing cache flushes (either "always" or "cond")
1342 * we will do one whenever the guest does a vmlaunch/vmresume.
1343 * If an outer hypervisor is doing the cache flush for us
1344 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1345 * capability to the guest too, and if EPT is disabled we're not
1346 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1347 * require a nested hypervisor to do a flush of its own.
1348 */
1349 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1350 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1351
Paolo Bonzini0c549142019-08-19 17:24:07 +02001352 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1353 data |= ARCH_CAP_RDCL_NO;
1354 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1355 data |= ARCH_CAP_SSB_NO;
1356 if (!boot_cpu_has_bug(X86_BUG_MDS))
1357 data |= ARCH_CAP_MDS_NO;
1358
Pawan Guptae1d38b62019-10-23 12:23:33 +02001359 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001360 * On TAA affected systems:
1361 * - nothing to do if TSX is disabled on the host.
1362 * - we emulate TSX_CTRL if present on the host.
1363 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001364 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001365 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001366 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001367 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1368 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001369
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001370 return data;
1371}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001372
Wanpeng Li66421c12018-02-28 14:03:30 +08001373static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1374{
1375 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001376 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001377 msr->data = kvm_get_arch_capabilities();
1378 break;
1379 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001380 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001381 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001382 default:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001383 if (kvm_x86_ops.get_msr_feature(msr))
Wanpeng Li66421c12018-02-28 14:03:30 +08001384 return 1;
1385 }
1386 return 0;
1387}
1388
Tom Lendacky801e4592018-02-21 13:39:51 -06001389static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1390{
1391 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001392 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001393
1394 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001395 r = kvm_get_msr_feature(&msr);
1396 if (r)
1397 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001398
1399 *data = msr.data;
1400
1401 return 0;
1402}
1403
Sean Christopherson11988492019-04-02 08:19:15 -07001404static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1405{
1406 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1407 return false;
1408
1409 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1410 return false;
1411
Sean Christopherson0a629562019-04-02 08:19:16 -07001412 if (efer & (EFER_LME | EFER_LMA) &&
1413 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1414 return false;
1415
1416 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1417 return false;
1418
Sean Christopherson11988492019-04-02 08:19:15 -07001419 return true;
1420
1421}
Jan Kiszka384bb782013-04-20 10:52:36 +02001422bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001423{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001424 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001425 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001426
Sean Christopherson11988492019-04-02 08:19:15 -07001427 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001428}
1429EXPORT_SYMBOL_GPL(kvm_valid_efer);
1430
Sean Christopherson11988492019-04-02 08:19:15 -07001431static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001432{
1433 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001434 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001435
Sean Christopherson11988492019-04-02 08:19:15 -07001436 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001437 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001438
Sean Christopherson11988492019-04-02 08:19:15 -07001439 if (!msr_info->host_initiated) {
1440 if (!__kvm_valid_efer(vcpu, efer))
1441 return 1;
1442
1443 if (is_paging(vcpu) &&
1444 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1445 return 1;
1446 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001447
Carsten Otte15c4a642007-10-30 18:44:17 +01001448 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001449 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001450
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001451 kvm_x86_ops.set_efer(vcpu, efer);
Sheng Yanga3d204e2010-05-12 16:40:40 +08001452
Sheng Yangaad82702010-05-12 16:40:42 +08001453 /* Update reserved bits */
1454 if ((efer ^ old_efer) & EFER_NX)
1455 kvm_mmu_reset_context(vcpu);
1456
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001457 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001458}
1459
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001460void kvm_enable_efer_bits(u64 mask)
1461{
1462 efer_reserved_bits &= ~mask;
1463}
1464EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1465
Carsten Otte15c4a642007-10-30 18:44:17 +01001466/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001467 * Write @data into the MSR specified by @index. Select MSR specific fault
1468 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001469 * Returns 0 on success, non-0 otherwise.
1470 * Assumes vcpu_load() was already called.
1471 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001472static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1473 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001474{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001475 struct msr_data msr;
1476
1477 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001478 case MSR_FS_BASE:
1479 case MSR_GS_BASE:
1480 case MSR_KERNEL_GS_BASE:
1481 case MSR_CSTAR:
1482 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001483 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001484 return 1;
1485 break;
1486 case MSR_IA32_SYSENTER_EIP:
1487 case MSR_IA32_SYSENTER_ESP:
1488 /*
1489 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1490 * non-canonical address is written on Intel but not on
1491 * AMD (which ignores the top 32-bits, because it does
1492 * not implement 64-bit SYSENTER).
1493 *
1494 * 64-bit code should hence be able to write a non-canonical
1495 * value on AMD. Making the address canonical ensures that
1496 * vmentry does not fail on Intel after writing a non-canonical
1497 * value, and that something deterministic happens if the guest
1498 * invokes 64-bit SYSENTER.
1499 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001500 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001501 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001502
1503 msr.data = data;
1504 msr.index = index;
1505 msr.host_initiated = host_initiated;
1506
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001507 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001508}
1509
1510/*
1511 * Read the MSR specified by @index into @data. Select MSR specific fault
1512 * checks are bypassed if @host_initiated is %true.
1513 * Returns 0 on success, non-0 otherwise.
1514 * Assumes vcpu_load() was already called.
1515 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001516int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1517 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001518{
1519 struct msr_data msr;
1520 int ret;
1521
1522 msr.index = index;
1523 msr.host_initiated = host_initiated;
1524
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001525 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001526 if (!ret)
1527 *data = msr.data;
1528 return ret;
1529}
1530
1531int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1532{
1533 return __kvm_get_msr(vcpu, index, data, false);
1534}
1535EXPORT_SYMBOL_GPL(kvm_get_msr);
1536
1537int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1538{
1539 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001540}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001541EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001542
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001543int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1544{
1545 u32 ecx = kvm_rcx_read(vcpu);
1546 u64 data;
1547
1548 if (kvm_get_msr(vcpu, ecx, &data)) {
1549 trace_kvm_msr_read_ex(ecx);
1550 kvm_inject_gp(vcpu, 0);
1551 return 1;
1552 }
1553
1554 trace_kvm_msr_read(ecx, data);
1555
1556 kvm_rax_write(vcpu, data & -1u);
1557 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1558 return kvm_skip_emulated_instruction(vcpu);
1559}
1560EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1561
1562int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1563{
1564 u32 ecx = kvm_rcx_read(vcpu);
1565 u64 data = kvm_read_edx_eax(vcpu);
1566
1567 if (kvm_set_msr(vcpu, ecx, data)) {
1568 trace_kvm_msr_write_ex(ecx, data);
1569 kvm_inject_gp(vcpu, 0);
1570 return 1;
1571 }
1572
1573 trace_kvm_msr_write(ecx, data);
1574 return kvm_skip_emulated_instruction(vcpu);
1575}
1576EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1577
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001578bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
1579{
1580 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
1581 need_resched() || signal_pending(current);
1582}
1583EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request);
1584
Carsten Otte313a3dc2007-10-11 19:16:52 +02001585/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001586 * The fast path for frequent and performance sensitive wrmsr emulation,
1587 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1588 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1589 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1590 * other cases which must be called after interrupts are enabled on the host.
1591 */
1592static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1593{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001594 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1595 return 1;
1596
1597 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001598 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001599 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1600 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001601
Wanpeng Lid5361672020-03-26 10:20:02 +08001602 data &= ~(1 << 12);
1603 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001604 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001605 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1606 trace_kvm_apic_write(APIC_ICR, (u32)data);
1607 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001608 }
1609
1610 return 1;
1611}
1612
Wanpeng Liae95f562020-04-28 14:23:28 +08001613static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1614{
1615 if (!kvm_can_use_hv_timer(vcpu))
1616 return 1;
1617
1618 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1619 return 0;
1620}
1621
Wanpeng Li404d5d72020-04-28 14:23:25 +08001622fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001623{
1624 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001625 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001626 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001627
1628 switch (msr) {
1629 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001630 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001631 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1632 kvm_skip_emulated_instruction(vcpu);
1633 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001634 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001635 break;
Wanpeng Liae95f562020-04-28 14:23:28 +08001636 case MSR_IA32_TSCDEADLINE:
1637 data = kvm_read_edx_eax(vcpu);
1638 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1639 kvm_skip_emulated_instruction(vcpu);
1640 ret = EXIT_FASTPATH_REENTER_GUEST;
1641 }
1642 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001643 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001644 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001645 }
1646
Wanpeng Li404d5d72020-04-28 14:23:25 +08001647 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001648 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001649
Wanpeng Li404d5d72020-04-28 14:23:25 +08001650 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001651}
1652EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1653
1654/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001655 * Adapt set_msr() to msr_io()'s calling convention
1656 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001657static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1658{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001659 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001660}
1661
Carsten Otte313a3dc2007-10-11 19:16:52 +02001662static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1663{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001664 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001665}
1666
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001667#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001668struct pvclock_clock {
1669 int vclock_mode;
1670 u64 cycle_last;
1671 u64 mask;
1672 u32 mult;
1673 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001674 u64 base_cycles;
1675 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001676};
1677
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001678struct pvclock_gtod_data {
1679 seqcount_t seq;
1680
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001681 struct pvclock_clock clock; /* extract of a clocksource struct */
1682 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001683
Paolo Bonzini917f9472020-01-22 14:32:20 +01001684 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001685 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001686};
1687
1688static struct pvclock_gtod_data pvclock_gtod_data;
1689
1690static void update_pvclock_gtod(struct timekeeper *tk)
1691{
1692 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1693
1694 write_seqcount_begin(&vdata->seq);
1695
1696 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001697 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001698 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1699 vdata->clock.mask = tk->tkr_mono.mask;
1700 vdata->clock.mult = tk->tkr_mono.mult;
1701 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001702 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1703 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001704
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001705 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001706 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1707 vdata->raw_clock.mask = tk->tkr_raw.mask;
1708 vdata->raw_clock.mult = tk->tkr_raw.mult;
1709 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001710 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1711 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001712
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001713 vdata->wall_time_sec = tk->xtime_sec;
1714
Paolo Bonzini917f9472020-01-22 14:32:20 +01001715 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001716
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001717 write_seqcount_end(&vdata->seq);
1718}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001719
1720static s64 get_kvmclock_base_ns(void)
1721{
1722 /* Count up from boot time, but with the frequency of the raw clock. */
1723 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1724}
1725#else
1726static s64 get_kvmclock_base_ns(void)
1727{
1728 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1729 return ktime_get_boottime_ns();
1730}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001731#endif
1732
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001733void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1734{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001735 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001736 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001737}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001738
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001739static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1740{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001741 int version;
1742 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001743 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001744 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001745
1746 if (!wall_clock)
1747 return;
1748
Avi Kivity9ed3c442010-05-04 15:00:37 +03001749 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1750 if (r)
1751 return;
1752
1753 if (version & 1)
1754 ++version; /* first time write, random junk */
1755
1756 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001757
Nicholas Krause1dab1342015-12-30 13:08:46 -05001758 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1759 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001760
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001761 /*
1762 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001763 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001764 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001765 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001766 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001767
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001768 wc.nsec = do_div(wall_nsec, 1000000000);
1769 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001770 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001771
1772 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1773
1774 version++;
1775 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001776}
1777
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001778static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1779{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001780 do_shl32_div32(dividend, divisor);
1781 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001782}
1783
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001784static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001785 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001786{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001787 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001788 int32_t shift = 0;
1789 uint64_t tps64;
1790 uint32_t tps32;
1791
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001792 tps64 = base_hz;
1793 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001794 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001795 tps64 >>= 1;
1796 shift--;
1797 }
1798
1799 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001800 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1801 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001802 scaled64 >>= 1;
1803 else
1804 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001805 shift++;
1806 }
1807
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001808 *pshift = shift;
1809 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001810}
1811
Marcelo Tosattid8281992012-11-27 23:29:01 -02001812#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001813static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001814#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001815
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001816static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001817static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001818
Zachary Amsdencc578282012-02-03 15:43:50 -02001819static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001820{
Zachary Amsdencc578282012-02-03 15:43:50 -02001821 u64 v = (u64)khz * (1000000 + ppm);
1822 do_div(v, 1000000);
1823 return v;
1824}
1825
Haozhong Zhang381d5852015-10-20 15:39:04 +08001826static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1827{
1828 u64 ratio;
1829
1830 /* Guest TSC same frequency as host TSC? */
1831 if (!scale) {
1832 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1833 return 0;
1834 }
1835
1836 /* TSC scaling supported? */
1837 if (!kvm_has_tsc_control) {
1838 if (user_tsc_khz > tsc_khz) {
1839 vcpu->arch.tsc_catchup = 1;
1840 vcpu->arch.tsc_always_catchup = 1;
1841 return 0;
1842 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001843 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001844 return -1;
1845 }
1846 }
1847
1848 /* TSC scaling required - calculate ratio */
1849 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1850 user_tsc_khz, tsc_khz);
1851
1852 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001853 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1854 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001855 return -1;
1856 }
1857
1858 vcpu->arch.tsc_scaling_ratio = ratio;
1859 return 0;
1860}
1861
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001862static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001863{
1864 u32 thresh_lo, thresh_hi;
1865 int use_scaling = 0;
1866
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001867 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001868 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001869 /* set tsc_scaling_ratio to a safe value */
1870 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001871 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001872 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001873
Zachary Amsdenc2855452010-09-18 14:38:15 -10001874 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001875 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001876 &vcpu->arch.virtual_tsc_shift,
1877 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001878 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001879
1880 /*
1881 * Compute the variation in TSC rate which is acceptable
1882 * within the range of tolerance and decide if the
1883 * rate being applied is within that bounds of the hardware
1884 * rate. If so, no scaling or compensation need be done.
1885 */
1886 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1887 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001888 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1889 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi);
Zachary Amsdencc578282012-02-03 15:43:50 -02001890 use_scaling = 1;
1891 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001892 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001893}
1894
1895static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1896{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001897 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001898 vcpu->arch.virtual_tsc_mult,
1899 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001900 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001901 return tsc;
1902}
1903
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001904static inline int gtod_is_based_on_tsc(int mode)
1905{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001906 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001907}
1908
Fengguang Wu69b00492015-01-19 22:33:39 +08001909static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001910{
1911#ifdef CONFIG_X86_64
1912 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001913 struct kvm_arch *ka = &vcpu->kvm->arch;
1914 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1915
1916 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1917 atomic_read(&vcpu->kvm->online_vcpus));
1918
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001919 /*
1920 * Once the masterclock is enabled, always perform request in
1921 * order to update it.
1922 *
1923 * In order to enable masterclock, the host clocksource must be TSC
1924 * and the vcpus need to have matched TSCs. When that happens,
1925 * perform request to enable masterclock.
1926 */
1927 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001928 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001929 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1930
1931 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1932 atomic_read(&vcpu->kvm->online_vcpus),
1933 ka->use_master_clock, gtod->clock.vclock_mode);
1934#endif
1935}
1936
Will Auldba904632012-11-29 12:42:50 -08001937static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1938{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001939 u64 curr_offset = vcpu->arch.l1_tsc_offset;
Will Auldba904632012-11-29 12:42:50 -08001940 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1941}
1942
Haozhong Zhang35181e82015-10-20 15:39:03 +08001943/*
1944 * Multiply tsc by a fixed point number represented by ratio.
1945 *
1946 * The most significant 64-N bits (mult) of ratio represent the
1947 * integral part of the fixed point number; the remaining N bits
1948 * (frac) represent the fractional part, ie. ratio represents a fixed
1949 * point number (mult + frac * 2^(-N)).
1950 *
1951 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1952 */
1953static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1954{
1955 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1956}
1957
1958u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1959{
1960 u64 _tsc = tsc;
1961 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1962
1963 if (ratio != kvm_default_tsc_scaling_ratio)
1964 _tsc = __scale_tsc(ratio, tsc);
1965
1966 return _tsc;
1967}
1968EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1969
Haozhong Zhang07c14192015-10-20 15:39:05 +08001970static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1971{
1972 u64 tsc;
1973
1974 tsc = kvm_scale_tsc(vcpu, rdtsc());
1975
1976 return target_tsc - tsc;
1977}
1978
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001979u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1980{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001981 return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001982}
1983EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1984
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001985static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1986{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001987 vcpu->arch.l1_tsc_offset = offset;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001988 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001989}
1990
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001991static inline bool kvm_check_tsc_unstable(void)
1992{
1993#ifdef CONFIG_X86_64
1994 /*
1995 * TSC is marked unstable when we're running on Hyper-V,
1996 * 'TSC page' clocksource is good.
1997 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001998 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001999 return false;
2000#endif
2001 return check_tsc_unstable();
2002}
2003
Will Auld8fe8ab42012-11-29 12:42:12 -08002004void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002005{
2006 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002007 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002008 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002009 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002010 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08002011 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002012 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002013
Jan Kiszka038f8c12011-02-04 10:49:11 +01002014 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002015 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002016 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002017 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002018
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002019 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002020 if (data == 0 && msr->host_initiated) {
2021 /*
2022 * detection of vcpu initialization -- need to sync
2023 * with other vCPUs. This particularly helps to keep
2024 * kvm_clock stable after CPU hotplug
2025 */
2026 synchronizing = true;
2027 } else {
2028 u64 tsc_exp = kvm->arch.last_tsc_write +
2029 nsec_to_cycles(vcpu, elapsed);
2030 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2031 /*
2032 * Special case: TSC write with a small delta (1 second)
2033 * of virtual cycle time against real time is
2034 * interpreted as an attempt to synchronize the CPU.
2035 */
2036 synchronizing = data < tsc_exp + tsc_hz &&
2037 data + tsc_hz > tsc_exp;
2038 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002039 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002040
2041 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002042 * For a reliable TSC, we can match TSC offsets, and for an unstable
2043 * TSC, we add elapsed time in this computation. We could let the
2044 * compensation code attempt to catch up if we fall behind, but
2045 * it's better to try to match offsets from the beginning.
2046 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002047 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002048 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002049 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002050 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002051 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002052 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002053 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002054 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002055 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002056 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002057 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002058 } else {
2059 /*
2060 * We split periods of matched TSC writes into generations.
2061 * For each generation, we track the original measured
2062 * nanosecond time, offset, and write, so if TSCs are in
2063 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002064 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002065 *
2066 * These values are tracked in kvm->arch.cur_xxx variables.
2067 */
2068 kvm->arch.cur_tsc_generation++;
2069 kvm->arch.cur_tsc_nsec = ns;
2070 kvm->arch.cur_tsc_write = data;
2071 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002072 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002073 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002074
2075 /*
2076 * We also track th most recent recorded KHZ, write and time to
2077 * allow the matching interval to be extended at each write.
2078 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002079 kvm->arch.last_tsc_nsec = ns;
2080 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002081 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002082
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002083 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002084
2085 /* Keep track of which generation this VCPU has synchronized to */
2086 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2087 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2088 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2089
Radim Krčmářd6321d42017-08-05 00:12:49 +02002090 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002091 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002092
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002093 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002094 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002095
2096 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002097 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002098 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002099 } else if (!already_matched) {
2100 kvm->arch.nr_vcpus_matched_tsc++;
2101 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002102
2103 kvm_track_tsc_matching(vcpu);
2104 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002105}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002106
Zachary Amsden99e3e302010-08-19 22:07:17 -10002107EXPORT_SYMBOL_GPL(kvm_write_tsc);
2108
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002109static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2110 s64 adjustment)
2111{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002112 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002113 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002114}
2115
2116static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2117{
2118 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2119 WARN_ON(adjustment < 0);
2120 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002121 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002122}
2123
Marcelo Tosattid8281992012-11-27 23:29:01 -02002124#ifdef CONFIG_X86_64
2125
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002126static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002127{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002128 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002129 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002130
2131 if (likely(ret >= last))
2132 return ret;
2133
2134 /*
2135 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002136 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002137 * very likely) and there's a data dependence, so force GCC
2138 * to generate a branch instead. I don't barrier() because
2139 * we don't actually need a barrier, and if this function
2140 * ever gets inlined it will generate worse code.
2141 */
2142 asm volatile ("");
2143 return last;
2144}
2145
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002146static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2147 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002148{
2149 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002150 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002151
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002152 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002153 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002154 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2155 tsc_timestamp);
2156 if (tsc_pg_val != U64_MAX) {
2157 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002158 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002159 v = (tsc_pg_val - clock->cycle_last) &
2160 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002161 } else {
2162 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002163 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002164 }
2165 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002166 case VDSO_CLOCKMODE_TSC:
2167 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002168 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002169 v = (*tsc_timestamp - clock->cycle_last) &
2170 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002171 break;
2172 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002173 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002174 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002175
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002176 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002177 *tsc_timestamp = v = 0;
2178
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002179 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002180}
2181
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002182static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002183{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002184 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002185 unsigned long seq;
2186 int mode;
2187 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002188
Marcelo Tosattid8281992012-11-27 23:29:01 -02002189 do {
2190 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002191 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002192 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002193 ns >>= gtod->raw_clock.shift;
2194 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002195 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002196 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002197
2198 return mode;
2199}
2200
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002201static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002202{
2203 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2204 unsigned long seq;
2205 int mode;
2206 u64 ns;
2207
2208 do {
2209 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002210 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002211 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002212 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002213 ns >>= gtod->clock.shift;
2214 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2215
2216 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2217 ts->tv_nsec = ns;
2218
2219 return mode;
2220}
2221
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002222/* returns true if host is using TSC based clocksource */
2223static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002224{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002225 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002226 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002227 return false;
2228
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002229 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002230 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002231}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002232
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002233/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002234static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002235 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002236{
2237 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002238 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002239 return false;
2240
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002241 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002242}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002243#endif
2244
2245/*
2246 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002247 * Assuming a stable TSC across physical CPUS, and a stable TSC
2248 * across virtual CPUs, the following condition is possible.
2249 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002250 * CPUs at the next numbered event.
2251 *
2252 * "timespecX" represents host monotonic time. "tscX" represents
2253 * RDTSC value.
2254 *
2255 * VCPU0 on CPU0 | VCPU1 on CPU1
2256 *
2257 * 1. read timespec0,tsc0
2258 * 2. | timespec1 = timespec0 + N
2259 * | tsc1 = tsc0 + M
2260 * 3. transition to guest | transition to guest
2261 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2262 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2263 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2264 *
2265 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2266 *
2267 * - ret0 < ret1
2268 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2269 * ...
2270 * - 0 < N - M => M < N
2271 *
2272 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2273 * always the case (the difference between two distinct xtime instances
2274 * might be smaller then the difference between corresponding TSC reads,
2275 * when updating guest vcpus pvclock areas).
2276 *
2277 * To avoid that problem, do not allow visibility of distinct
2278 * system_timestamp/tsc_timestamp values simultaneously: use a master
2279 * copy of host monotonic time values. Update that master copy
2280 * in lockstep.
2281 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002282 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002283 *
2284 */
2285
2286static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2287{
2288#ifdef CONFIG_X86_64
2289 struct kvm_arch *ka = &kvm->arch;
2290 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002291 bool host_tsc_clocksource, vcpus_matched;
2292
2293 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2294 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002295
2296 /*
2297 * If the host uses TSC clock, then passthrough TSC as stable
2298 * to the guest.
2299 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002300 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002301 &ka->master_kernel_ns,
2302 &ka->master_cycle_now);
2303
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002304 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002305 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002306 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002307
Marcelo Tosattid8281992012-11-27 23:29:01 -02002308 if (ka->use_master_clock)
2309 atomic_set(&kvm_guest_has_master_clock, 1);
2310
2311 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002312 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2313 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002314#endif
2315}
2316
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002317void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2318{
2319 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2320}
2321
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002322static void kvm_gen_update_masterclock(struct kvm *kvm)
2323{
2324#ifdef CONFIG_X86_64
2325 int i;
2326 struct kvm_vcpu *vcpu;
2327 struct kvm_arch *ka = &kvm->arch;
2328
2329 spin_lock(&ka->pvclock_gtod_sync_lock);
2330 kvm_make_mclock_inprogress_request(kvm);
2331 /* no guest entries from this point */
2332 pvclock_update_vm_gtod_copy(kvm);
2333
2334 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002335 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002336
2337 /* guest entries allowed */
2338 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002339 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002340
2341 spin_unlock(&ka->pvclock_gtod_sync_lock);
2342#endif
2343}
2344
Marcelo Tosattie891a322017-04-17 12:51:37 -03002345u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002346{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002347 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002348 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002349 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002350
Paolo Bonzini8b953442016-11-16 18:31:30 +01002351 spin_lock(&ka->pvclock_gtod_sync_lock);
2352 if (!ka->use_master_clock) {
2353 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002354 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002355 }
2356
Paolo Bonzini8b953442016-11-16 18:31:30 +01002357 hv_clock.tsc_timestamp = ka->master_cycle_now;
2358 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2359 spin_unlock(&ka->pvclock_gtod_sync_lock);
2360
Wanpeng Lie2c22062017-05-11 18:12:05 -07002361 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2362 get_cpu();
2363
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002364 if (__this_cpu_read(cpu_tsc_khz)) {
2365 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2366 &hv_clock.tsc_shift,
2367 &hv_clock.tsc_to_system_mul);
2368 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2369 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002370 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002371
2372 put_cpu();
2373
2374 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002375}
2376
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002377static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2378{
2379 struct kvm_vcpu_arch *vcpu = &v->arch;
2380 struct pvclock_vcpu_time_info guest_hv_clock;
2381
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002382 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002383 &guest_hv_clock, sizeof(guest_hv_clock))))
2384 return;
2385
2386 /* This VCPU is paused, but it's legal for a guest to read another
2387 * VCPU's kvmclock, so we really have to follow the specification where
2388 * it says that version is odd if data is being modified, and even after
2389 * it is consistent.
2390 *
2391 * Version field updates must be kept separate. This is because
2392 * kvm_write_guest_cached might use a "rep movs" instruction, and
2393 * writes within a string instruction are weakly ordered. So there
2394 * are three writes overall.
2395 *
2396 * As a small optimization, only write the version field in the first
2397 * and third write. The vcpu->pv_time cache is still valid, because the
2398 * version field is the first in the struct.
2399 */
2400 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2401
Liran Alon51c4b8b2017-11-05 16:11:30 +02002402 if (guest_hv_clock.version & 1)
2403 ++guest_hv_clock.version; /* first time write, random junk */
2404
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002405 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002406 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2407 &vcpu->hv_clock,
2408 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002409
2410 smp_wmb();
2411
2412 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2413 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2414
2415 if (vcpu->pvclock_set_guest_stopped_request) {
2416 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2417 vcpu->pvclock_set_guest_stopped_request = false;
2418 }
2419
2420 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2421
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002422 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2423 &vcpu->hv_clock,
2424 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002425
2426 smp_wmb();
2427
2428 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002429 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2430 &vcpu->hv_clock,
2431 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002432}
2433
Zachary Amsden34c238a2010-09-18 14:38:14 -10002434static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002435{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002436 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002437 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002438 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002439 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002440 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002441 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002442 bool use_master_clock;
2443
2444 kernel_ns = 0;
2445 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002446
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002447 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002448 * If the host uses TSC clock, then passthrough TSC as stable
2449 * to the guest.
2450 */
2451 spin_lock(&ka->pvclock_gtod_sync_lock);
2452 use_master_clock = ka->use_master_clock;
2453 if (use_master_clock) {
2454 host_tsc = ka->master_cycle_now;
2455 kernel_ns = ka->master_kernel_ns;
2456 }
2457 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002458
2459 /* Keep irq disabled to prevent changes to the clock */
2460 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002461 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2462 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002463 local_irq_restore(flags);
2464 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2465 return 1;
2466 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002467 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002468 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002469 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002470 }
2471
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002472 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002473
2474 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002475 * We may have to catch up the TSC to match elapsed wall clock
2476 * time for two reasons, even if kvmclock is used.
2477 * 1) CPU could have been running below the maximum TSC rate
2478 * 2) Broken TSC compensation resets the base at each VCPU
2479 * entry to avoid unknown leaps of TSC even when running
2480 * again on the same CPU. This may cause apparent elapsed
2481 * time to disappear, and the guest to stand still or run
2482 * very slowly.
2483 */
2484 if (vcpu->tsc_catchup) {
2485 u64 tsc = compute_guest_tsc(v, kernel_ns);
2486 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002487 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002488 tsc_timestamp = tsc;
2489 }
2490 }
2491
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002492 local_irq_restore(flags);
2493
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002494 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002495
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002496 if (kvm_has_tsc_control)
2497 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2498
2499 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002500 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002501 &vcpu->hv_clock.tsc_shift,
2502 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002503 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002504 }
2505
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002506 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002507 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002508 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002509
Marcelo Tosattid8281992012-11-27 23:29:01 -02002510 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002511 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002512 if (use_master_clock)
2513 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2514
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002515 vcpu->hv_clock.flags = pvclock_flags;
2516
Paolo Bonzini095cf552016-02-08 12:54:12 +01002517 if (vcpu->pv_time_enabled)
2518 kvm_setup_pvclock_page(v);
2519 if (v == kvm_get_vcpu(v->kvm, 0))
2520 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002521 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002522}
2523
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002524/*
2525 * kvmclock updates which are isolated to a given vcpu, such as
2526 * vcpu->cpu migration, should not allow system_timestamp from
2527 * the rest of the vcpus to remain static. Otherwise ntp frequency
2528 * correction applies to one vcpu's system_timestamp but not
2529 * the others.
2530 *
2531 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002532 * We need to rate-limit these requests though, as they can
2533 * considerably slow guests that have a large number of vcpus.
2534 * The time for a remote vcpu to update its kvmclock is bound
2535 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002536 */
2537
Andrew Jones7e44e442014-02-28 12:52:54 +01002538#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2539
2540static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002541{
2542 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002543 struct delayed_work *dwork = to_delayed_work(work);
2544 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2545 kvmclock_update_work);
2546 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002547 struct kvm_vcpu *vcpu;
2548
2549 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002550 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002551 kvm_vcpu_kick(vcpu);
2552 }
2553}
2554
Andrew Jones7e44e442014-02-28 12:52:54 +01002555static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2556{
2557 struct kvm *kvm = v->kvm;
2558
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002559 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002560 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2561 KVMCLOCK_UPDATE_DELAY);
2562}
2563
Andrew Jones332967a2014-02-28 12:52:55 +01002564#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2565
2566static void kvmclock_sync_fn(struct work_struct *work)
2567{
2568 struct delayed_work *dwork = to_delayed_work(work);
2569 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2570 kvmclock_sync_work);
2571 struct kvm *kvm = container_of(ka, struct kvm, arch);
2572
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002573 if (!kvmclock_periodic_sync)
2574 return;
2575
Andrew Jones332967a2014-02-28 12:52:55 +01002576 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2577 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2578 KVMCLOCK_SYNC_PERIOD);
2579}
2580
Borislav Petkov191c8132019-04-18 18:32:50 +02002581/*
2582 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2583 */
2584static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2585{
2586 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002587 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002588 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2589
2590 return false;
2591}
2592
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002593static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002594{
2595 u64 mcg_cap = vcpu->arch.mcg_cap;
2596 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002597 u32 msr = msr_info->index;
2598 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002599
2600 switch (msr) {
2601 case MSR_IA32_MCG_STATUS:
2602 vcpu->arch.mcg_status = data;
2603 break;
2604 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002605 if (!(mcg_cap & MCG_CTL_P) &&
2606 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002607 return 1;
2608 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002609 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002610 vcpu->arch.mcg_ctl = data;
2611 break;
2612 default:
2613 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002614 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002615 u32 offset = array_index_nospec(
2616 msr - MSR_IA32_MC0_CTL,
2617 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2618
Andre Przywara114be422010-03-24 17:46:42 +01002619 /* only 0 or all 1s can be written to IA32_MCi_CTL
2620 * some Linux kernels though clear bit 10 in bank 4 to
2621 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2622 * this to avoid an uncatched #GP in the guest
2623 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002624 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002625 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002626 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002627
2628 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002629 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002630 (offset & 0x3) == 1 && data != 0) {
2631 if (!can_set_mci_status(vcpu))
2632 return -1;
2633 }
2634
Huang Ying890ca9a2009-05-11 16:48:15 +08002635 vcpu->arch.mce_banks[offset] = data;
2636 break;
2637 }
2638 return 1;
2639 }
2640 return 0;
2641}
2642
Ed Swierkffde22a2009-10-15 15:21:43 -07002643static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2644{
2645 struct kvm *kvm = vcpu->kvm;
2646 int lm = is_long_mode(vcpu);
2647 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2648 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2649 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2650 : kvm->arch.xen_hvm_config.blob_size_32;
2651 u32 page_num = data & ~PAGE_MASK;
2652 u64 page_addr = data & PAGE_MASK;
2653 u8 *page;
2654 int r;
2655
2656 r = -E2BIG;
2657 if (page_num >= blob_size)
2658 goto out;
2659 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002660 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2661 if (IS_ERR(page)) {
2662 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002663 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002664 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002665 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002666 goto out_free;
2667 r = 0;
2668out_free:
2669 kfree(page);
2670out:
2671 return r;
2672}
2673
Gleb Natapov344d9582010-10-14 11:22:50 +02002674static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2675{
2676 gpa_t gpa = data & ~0x3f;
2677
Wanpeng Li52a5c152017-07-13 18:30:42 -07002678 /* Bits 3:5 are reserved, Should be zero */
2679 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002680 return 1;
2681
2682 vcpu->arch.apf.msr_val = data;
2683
2684 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2685 kvm_clear_async_pf_completion_queue(vcpu);
2686 kvm_async_pf_hash_reset(vcpu);
2687 return 0;
2688 }
2689
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002690 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002691 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002692 return 1;
2693
Gleb Natapov6adba522010-10-14 11:22:55 +02002694 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002695 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002696 kvm_async_pf_wakeup_all(vcpu);
2697 return 0;
2698}
2699
Glauber Costa12f9a482011-02-01 14:16:40 -05002700static void kvmclock_reset(struct kvm_vcpu *vcpu)
2701{
Andy Honig0b794592013-02-20 14:48:10 -08002702 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002703 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002704}
2705
Sean Christopherson77809382020-03-20 14:28:18 -07002706static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002707{
2708 ++vcpu->stat.tlb_flush;
Sean Christopherson77809382020-03-20 14:28:18 -07002709 kvm_x86_ops.tlb_flush_all(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002710}
2711
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002712static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
2713{
2714 ++vcpu->stat.tlb_flush;
2715 kvm_x86_ops.tlb_flush_guest(vcpu);
2716}
2717
Glauber Costac9aaa892011-07-11 15:28:14 -04002718static void record_steal_time(struct kvm_vcpu *vcpu)
2719{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002720 struct kvm_host_map map;
2721 struct kvm_steal_time *st;
2722
Glauber Costac9aaa892011-07-11 15:28:14 -04002723 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2724 return;
2725
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002726 /* -EAGAIN is returned in atomic context so we can just return. */
2727 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2728 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002729 return;
2730
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002731 st = map.hva +
2732 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2733
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002734 /*
2735 * Doing a TLB flush here, on the guest's behalf, can avoid
2736 * expensive IPIs.
2737 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002738 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002739 st->preempted & KVM_VCPU_FLUSH_TLB);
2740 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002741 kvm_vcpu_flush_tlb_guest(vcpu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002742
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002743 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002744
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002745 if (st->version & 1)
2746 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002747
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002748 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002749
2750 smp_wmb();
2751
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002752 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002753 vcpu->arch.st.last_steal;
2754 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002755
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002756 smp_wmb();
2757
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002758 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002759
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002760 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002761}
2762
Will Auld8fe8ab42012-11-29 12:42:12 -08002763int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002764{
Gleb Natapov57537852012-01-15 14:17:22 +02002765 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002766 u32 msr = msr_info->index;
2767 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002768
Carsten Otte15c4a642007-10-30 18:44:17 +01002769 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002770 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002771 case MSR_IA32_UCODE_WRITE:
2772 case MSR_VM_HSAVE_PA:
2773 case MSR_AMD64_PATCH_LOADER:
2774 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002775 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002776 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002777 break;
2778
Wanpeng Li518e7b92018-02-28 14:03:31 +08002779 case MSR_IA32_UCODE_REV:
2780 if (msr_info->host_initiated)
2781 vcpu->arch.microcode_version = data;
2782 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002783 case MSR_IA32_ARCH_CAPABILITIES:
2784 if (!msr_info->host_initiated)
2785 return 1;
2786 vcpu->arch.arch_capabilities = data;
2787 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002788 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002789 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002790 case MSR_K7_HWCR:
2791 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002792 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002793 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002794
2795 /* Handle McStatusWrEn */
2796 if (data == BIT_ULL(18)) {
2797 vcpu->arch.msr_hwcr = data;
2798 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002799 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2800 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002801 return 1;
2802 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002803 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002804 case MSR_FAM10H_MMIO_CONF_BASE:
2805 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002806 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2807 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002808 return 1;
2809 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002810 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002811 case MSR_IA32_DEBUGCTLMSR:
2812 if (!data) {
2813 /* We support the non-activated case already */
2814 break;
2815 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2816 /* Values other than LBR and BTF are vendor-specific,
2817 thus reserved and should throw a #GP */
2818 return 1;
2819 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002820 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2821 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002822 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002823 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002824 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002825 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002826 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002827 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2828 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002829 case MSR_IA32_TSCDEADLINE:
2830 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2831 break;
Will Auldba904632012-11-29 12:42:50 -08002832 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002833 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002834 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002835 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002836 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002837 }
2838 vcpu->arch.ia32_tsc_adjust_msr = data;
2839 }
2840 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002841 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002842 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2843 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2844 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2845 return 1;
2846 vcpu->arch.ia32_misc_enable_msr = data;
2847 kvm_update_cpuid(vcpu);
2848 } else {
2849 vcpu->arch.ia32_misc_enable_msr = data;
2850 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002851 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002852 case MSR_IA32_SMBASE:
2853 if (!msr_info->host_initiated)
2854 return 1;
2855 vcpu->arch.smbase = data;
2856 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002857 case MSR_IA32_POWER_CTL:
2858 vcpu->arch.msr_ia32_power_ctl = data;
2859 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002860 case MSR_IA32_TSC:
2861 kvm_write_tsc(vcpu, msr_info);
2862 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002863 case MSR_IA32_XSS:
2864 if (!msr_info->host_initiated &&
2865 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2866 return 1;
2867 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002868 * KVM supports exposing PT to the guest, but does not support
2869 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2870 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002871 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002872 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002873 return 1;
2874 vcpu->arch.ia32_xss = data;
2875 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002876 case MSR_SMI_COUNT:
2877 if (!msr_info->host_initiated)
2878 return 1;
2879 vcpu->arch.smi_count = data;
2880 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002881 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002882 case MSR_KVM_WALL_CLOCK:
2883 vcpu->kvm->arch.wall_clock = data;
2884 kvm_write_wall_clock(vcpu->kvm, data);
2885 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002886 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002887 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002888 struct kvm_arch *ka = &vcpu->kvm->arch;
2889
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002890 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2891 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2892
2893 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002894 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002895
2896 ka->boot_vcpu_runs_old_kvmclock = tmp;
2897 }
2898
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002899 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002900 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002901
2902 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002903 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002904 if (!(data & 1))
2905 break;
2906
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002907 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002908 &vcpu->arch.pv_time, data & ~1ULL,
2909 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002910 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002911
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002912 break;
2913 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002914 case MSR_KVM_ASYNC_PF_EN:
2915 if (kvm_pv_enable_async_pf(vcpu, data))
2916 return 1;
2917 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002918 case MSR_KVM_STEAL_TIME:
2919
2920 if (unlikely(!sched_info_on()))
2921 return 1;
2922
2923 if (data & KVM_STEAL_RESERVED_MASK)
2924 return 1;
2925
Glauber Costac9aaa892011-07-11 15:28:14 -04002926 vcpu->arch.st.msr_val = data;
2927
2928 if (!(data & KVM_MSR_ENABLED))
2929 break;
2930
Glauber Costac9aaa892011-07-11 15:28:14 -04002931 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2932
2933 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002934 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002935 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002936 return 1;
2937 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002938
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002939 case MSR_KVM_POLL_CONTROL:
2940 /* only enable bit supported */
2941 if (data & (-1ULL << 1))
2942 return 1;
2943
2944 vcpu->arch.msr_kvm_poll_control = data;
2945 break;
2946
Huang Ying890ca9a2009-05-11 16:48:15 +08002947 case MSR_IA32_MCG_CTL:
2948 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002949 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002950 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002951
Wei Huang6912ac32015-06-12 01:34:56 -04002952 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2953 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2954 pr = true; /* fall through */
2955 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2956 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002957 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002958 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002959
2960 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002961 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2962 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002963 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002964 case MSR_K7_CLK_CTL:
2965 /*
2966 * Ignore all writes to this no longer documented MSR.
2967 * Writes are only relevant for old K7 processors,
2968 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002969 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002970 * affected processor models on the command line, hence
2971 * the need to ignore the workaround.
2972 */
2973 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002974 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002975 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2976 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002977 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002978 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2979 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2980 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002981 return kvm_hv_set_msr_common(vcpu, msr, data,
2982 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002983 case MSR_IA32_BBL_CR_CTL3:
2984 /* Drop writes to this legacy MSR -- see rdmsr
2985 * counterpart for further detail.
2986 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002987 if (report_ignored_msrs)
2988 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2989 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002990 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002991 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002992 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002993 return 1;
2994 vcpu->arch.osvw.length = data;
2995 break;
2996 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002997 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002998 return 1;
2999 vcpu->arch.osvw.status = data;
3000 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003001 case MSR_PLATFORM_INFO:
3002 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003003 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3004 cpuid_fault_enabled(vcpu)))
3005 return 1;
3006 vcpu->arch.msr_platform_info = data;
3007 break;
3008 case MSR_MISC_FEATURES_ENABLES:
3009 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3010 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3011 !supports_cpuid_fault(vcpu)))
3012 return 1;
3013 vcpu->arch.msr_misc_features_enables = data;
3014 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003015 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07003016 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
3017 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02003018 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003019 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02003020 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003021 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03003022 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003023 return 1;
3024 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003025 if (report_ignored_msrs)
3026 vcpu_unimpl(vcpu,
3027 "ignored wrmsr: 0x%x data 0x%llx\n",
3028 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003029 break;
3030 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003031 }
3032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3035
Paolo Bonzini44883f02018-07-26 13:01:52 +02003036static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003037{
3038 u64 data;
3039 u64 mcg_cap = vcpu->arch.mcg_cap;
3040 unsigned bank_num = mcg_cap & 0xff;
3041
3042 switch (msr) {
3043 case MSR_IA32_P5_MC_ADDR:
3044 case MSR_IA32_P5_MC_TYPE:
3045 data = 0;
3046 break;
3047 case MSR_IA32_MCG_CAP:
3048 data = vcpu->arch.mcg_cap;
3049 break;
3050 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003051 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003052 return 1;
3053 data = vcpu->arch.mcg_ctl;
3054 break;
3055 case MSR_IA32_MCG_STATUS:
3056 data = vcpu->arch.mcg_status;
3057 break;
3058 default:
3059 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003060 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003061 u32 offset = array_index_nospec(
3062 msr - MSR_IA32_MC0_CTL,
3063 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3064
Huang Ying890ca9a2009-05-11 16:48:15 +08003065 data = vcpu->arch.mce_banks[offset];
3066 break;
3067 }
3068 return 1;
3069 }
3070 *pdata = data;
3071 return 0;
3072}
3073
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003074int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003075{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003076 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003077 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003078 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003079 case MSR_IA32_DEBUGCTLMSR:
3080 case MSR_IA32_LASTBRANCHFROMIP:
3081 case MSR_IA32_LASTBRANCHTOIP:
3082 case MSR_IA32_LASTINTFROMIP:
3083 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303084 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003085 case MSR_K8_TSEG_ADDR:
3086 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003087 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003088 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003089 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003090 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003091 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003092 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003093 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003094 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003095 /*
3096 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3097 * limit) MSRs. Just return 0, as we do not want to expose the host
3098 * data here. Do not conditionalize this on CPUID, as KVM does not do
3099 * so for existing CPU-specific MSRs.
3100 */
3101 case MSR_RAPL_POWER_UNIT:
3102 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3103 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3104 case MSR_PKG_ENERGY_STATUS: /* Total package */
3105 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003106 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003107 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003108 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003109 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3110 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3111 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3112 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003113 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003114 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3115 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003116 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003117 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003118 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003119 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003120 case MSR_IA32_ARCH_CAPABILITIES:
3121 if (!msr_info->host_initiated &&
3122 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3123 return 1;
3124 msr_info->data = vcpu->arch.arch_capabilities;
3125 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003126 case MSR_IA32_POWER_CTL:
3127 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3128 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003129 case MSR_IA32_TSC:
3130 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3131 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003132 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003133 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003134 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003135 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003136 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003137 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003138 /*
3139 * MSR_EBC_FREQUENCY_ID
3140 * Conservative value valid for even the basic CPU models.
3141 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3142 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3143 * and 266MHz for model 3, or 4. Set Core Clock
3144 * Frequency to System Bus Frequency Ratio to 1 (bits
3145 * 31:24) even though these are only valid for CPU
3146 * models > 2, however guests may end up dividing or
3147 * multiplying by zero otherwise.
3148 */
3149 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003150 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003151 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003152 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003153 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003154 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003155 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003156 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003157 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003158 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003159 break;
Will Auldba904632012-11-29 12:42:50 -08003160 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003161 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003162 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003163 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003164 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003165 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003166 case MSR_IA32_SMBASE:
3167 if (!msr_info->host_initiated)
3168 return 1;
3169 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003170 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003171 case MSR_SMI_COUNT:
3172 msr_info->data = vcpu->arch.smi_count;
3173 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003174 case MSR_IA32_PERF_STATUS:
3175 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003177 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003178 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003179 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003180 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003181 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003182 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003183 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003184 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003185 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003186 break;
3187 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003188 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003189 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003190 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003191 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003192 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003193 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003194 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003195 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003196 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003197 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003198 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003199 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003200 case MSR_KVM_POLL_CONTROL:
3201 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3202 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003203 case MSR_IA32_P5_MC_ADDR:
3204 case MSR_IA32_P5_MC_TYPE:
3205 case MSR_IA32_MCG_CAP:
3206 case MSR_IA32_MCG_CTL:
3207 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003208 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003209 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3210 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003211 case MSR_IA32_XSS:
3212 if (!msr_info->host_initiated &&
3213 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3214 return 1;
3215 msr_info->data = vcpu->arch.ia32_xss;
3216 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003217 case MSR_K7_CLK_CTL:
3218 /*
3219 * Provide expected ramp-up count for K7. All other
3220 * are set to zero, indicating minimum divisors for
3221 * every field.
3222 *
3223 * This prevents guest kernels on AMD host with CPU
3224 * type 6, model 8 and higher from exploding due to
3225 * the rdmsr failing.
3226 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003227 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003228 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003229 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003230 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3231 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003232 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003233 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3234 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3235 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003236 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003237 msr_info->index, &msr_info->data,
3238 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003239 case MSR_IA32_BBL_CR_CTL3:
3240 /* This legacy MSR exists but isn't fully documented in current
3241 * silicon. It is however accessed by winxp in very narrow
3242 * scenarios where it sets bit #19, itself documented as
3243 * a "reserved" bit. Best effort attempt to source coherent
3244 * read data here should the balance of the register be
3245 * interpreted by the guest:
3246 *
3247 * L2 cache control register 3: 64GB range, 256KB size,
3248 * enabled, latency 0x1, configured
3249 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003250 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003251 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003252 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003253 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003254 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003255 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003256 break;
3257 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003258 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003259 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003260 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003261 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003262 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003263 if (!msr_info->host_initiated &&
3264 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3265 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003266 msr_info->data = vcpu->arch.msr_platform_info;
3267 break;
3268 case MSR_MISC_FEATURES_ENABLES:
3269 msr_info->data = vcpu->arch.msr_misc_features_enables;
3270 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003271 case MSR_K7_HWCR:
3272 msr_info->data = vcpu->arch.msr_hwcr;
3273 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003274 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003275 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003276 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003277 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003278 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3279 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003280 return 1;
3281 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003282 if (report_ignored_msrs)
3283 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3284 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003285 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003286 }
3287 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003288 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003289 return 0;
3290}
3291EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3292
Carsten Otte313a3dc2007-10-11 19:16:52 +02003293/*
3294 * Read or write a bunch of msrs. All parameters are kernel addresses.
3295 *
3296 * @return number of msrs set successfully.
3297 */
3298static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3299 struct kvm_msr_entry *entries,
3300 int (*do_msr)(struct kvm_vcpu *vcpu,
3301 unsigned index, u64 *data))
3302{
Tom Lendacky801e4592018-02-21 13:39:51 -06003303 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003304
Carsten Otte313a3dc2007-10-11 19:16:52 +02003305 for (i = 0; i < msrs->nmsrs; ++i)
3306 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3307 break;
3308
Carsten Otte313a3dc2007-10-11 19:16:52 +02003309 return i;
3310}
3311
3312/*
3313 * Read or write a bunch of msrs. Parameters are user addresses.
3314 *
3315 * @return number of msrs set successfully.
3316 */
3317static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3318 int (*do_msr)(struct kvm_vcpu *vcpu,
3319 unsigned index, u64 *data),
3320 int writeback)
3321{
3322 struct kvm_msrs msrs;
3323 struct kvm_msr_entry *entries;
3324 int r, n;
3325 unsigned size;
3326
3327 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003328 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003329 goto out;
3330
3331 r = -E2BIG;
3332 if (msrs.nmsrs >= MAX_IO_MSRS)
3333 goto out;
3334
Carsten Otte313a3dc2007-10-11 19:16:52 +02003335 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003336 entries = memdup_user(user_msrs->entries, size);
3337 if (IS_ERR(entries)) {
3338 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003339 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003340 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003341
3342 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3343 if (r < 0)
3344 goto out_free;
3345
3346 r = -EFAULT;
3347 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3348 goto out_free;
3349
3350 r = n;
3351
3352out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003353 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003354out:
3355 return r;
3356}
3357
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003358static inline bool kvm_can_mwait_in_guest(void)
3359{
3360 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003361 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3362 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003363}
3364
Alexander Graf784aa3d2014-07-14 18:27:35 +02003365int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003366{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003367 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003368
3369 switch (ext) {
3370 case KVM_CAP_IRQCHIP:
3371 case KVM_CAP_HLT:
3372 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003373 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003374 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003375 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003376 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003377 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003378 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003379 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003380 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003381 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003382 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003383 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003384 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003385 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003386 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003387 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003388 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003389 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003390 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003391 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003392 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003393 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003394 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003395 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003396 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003397 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003398 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003399 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003400 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003401 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003402 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003403 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003404 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003405 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003406 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003407 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003408 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003409 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003410 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003411 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003412 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003413 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003414 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003415 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003416 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003417 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003418 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003419 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003420 case KVM_CAP_SET_GUEST_DEBUG:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003421 r = 1;
3422 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003423 case KVM_CAP_SYNC_REGS:
3424 r = KVM_SYNC_X86_VALID_FIELDS;
3425 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003426 case KVM_CAP_ADJUST_CLOCK:
3427 r = KVM_CLOCK_TSC_STABLE;
3428 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003429 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003430 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3431 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003432 if(kvm_can_mwait_in_guest())
3433 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003434 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003435 case KVM_CAP_X86_SMM:
3436 /* SMBASE is usually relocated above 1M on modern chipsets,
3437 * and SMM handlers might indeed rely on 4G segment limits,
3438 * so do not report SMM to be available if real mode is
3439 * emulated via vm86 mode. Still, do not go to great lengths
3440 * to avoid userspace's usage of the feature, because it is a
3441 * fringe case that is not enabled except via specific settings
3442 * of the module parameters.
3443 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003444 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003445 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003446 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003447 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003448 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003449 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003450 r = KVM_SOFT_MAX_VCPUS;
3451 break;
3452 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003453 r = KVM_MAX_VCPUS;
3454 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003455 case KVM_CAP_MAX_VCPU_ID:
3456 r = KVM_MAX_VCPU_ID;
3457 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003458 case KVM_CAP_PV_MMU: /* obsolete */
3459 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003460 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003461 case KVM_CAP_MCE:
3462 r = KVM_MAX_MCE_BANKS;
3463 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003464 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003465 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003466 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003467 case KVM_CAP_TSC_CONTROL:
3468 r = kvm_has_tsc_control;
3469 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003470 case KVM_CAP_X2APIC_API:
3471 r = KVM_X2APIC_API_VALID_FLAGS;
3472 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003473 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003474 r = kvm_x86_ops.nested_ops->get_state ?
3475 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003476 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003477 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003478 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003479 break;
3480 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003481 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003482 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003483 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003484 break;
3485 }
3486 return r;
3487
3488}
3489
Carsten Otte043405e2007-10-10 17:16:19 +02003490long kvm_arch_dev_ioctl(struct file *filp,
3491 unsigned int ioctl, unsigned long arg)
3492{
3493 void __user *argp = (void __user *)arg;
3494 long r;
3495
3496 switch (ioctl) {
3497 case KVM_GET_MSR_INDEX_LIST: {
3498 struct kvm_msr_list __user *user_msr_list = argp;
3499 struct kvm_msr_list msr_list;
3500 unsigned n;
3501
3502 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003503 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003504 goto out;
3505 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003506 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003507 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003508 goto out;
3509 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003510 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003511 goto out;
3512 r = -EFAULT;
3513 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3514 num_msrs_to_save * sizeof(u32)))
3515 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003516 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003517 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003518 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003519 goto out;
3520 r = 0;
3521 break;
3522 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003523 case KVM_GET_SUPPORTED_CPUID:
3524 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003525 struct kvm_cpuid2 __user *cpuid_arg = argp;
3526 struct kvm_cpuid2 cpuid;
3527
3528 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003529 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003530 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003531
3532 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3533 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003534 if (r)
3535 goto out;
3536
3537 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003538 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003539 goto out;
3540 r = 0;
3541 break;
3542 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003543 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003544 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003545 if (copy_to_user(argp, &kvm_mce_cap_supported,
3546 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003547 goto out;
3548 r = 0;
3549 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003550 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3551 struct kvm_msr_list __user *user_msr_list = argp;
3552 struct kvm_msr_list msr_list;
3553 unsigned int n;
3554
3555 r = -EFAULT;
3556 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3557 goto out;
3558 n = msr_list.nmsrs;
3559 msr_list.nmsrs = num_msr_based_features;
3560 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3561 goto out;
3562 r = -E2BIG;
3563 if (n < msr_list.nmsrs)
3564 goto out;
3565 r = -EFAULT;
3566 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3567 num_msr_based_features * sizeof(u32)))
3568 goto out;
3569 r = 0;
3570 break;
3571 }
3572 case KVM_GET_MSRS:
3573 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3574 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003575 default:
3576 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003577 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003578 }
3579out:
3580 return r;
3581}
3582
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003583static void wbinvd_ipi(void *garbage)
3584{
3585 wbinvd();
3586}
3587
3588static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3589{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003590 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003591}
3592
Carsten Otte313a3dc2007-10-11 19:16:52 +02003593void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3594{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003595 /* Address WBINVD may be executed by guest */
3596 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003597 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003598 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3599 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3600 smp_call_function_single(vcpu->cpu,
3601 wbinvd_ipi, NULL, 1);
3602 }
3603
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003604 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003605
Babu Moger37486132020-05-12 18:59:06 -05003606 /* Save host pkru register if supported */
3607 vcpu->arch.host_pkru = read_pkru();
3608
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003609 /* Apply any externally detected TSC adjustments (due to suspend) */
3610 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3611 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3612 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003613 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003614 }
3615
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003616 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003617 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003618 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003619 if (tsc_delta < 0)
3620 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003621
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003622 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003623 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003624 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003625 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003626 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003627 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003628
3629 if (kvm_lapic_hv_timer_in_use(vcpu))
3630 kvm_lapic_restart_hv_timer(vcpu);
3631
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003632 /*
3633 * On a host with synchronized TSC, there is no need to update
3634 * kvmclock on vcpu->cpu migration
3635 */
3636 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003637 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003638 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003639 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003640 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003641 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003642
Glauber Costac9aaa892011-07-11 15:28:14 -04003643 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003644}
3645
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003646static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3647{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003648 struct kvm_host_map map;
3649 struct kvm_steal_time *st;
3650
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003651 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3652 return;
3653
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003654 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003655 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003656
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003657 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3658 &vcpu->arch.st.cache, true))
3659 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003660
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003661 st = map.hva +
3662 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3663
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003664 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003665
3666 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003667}
3668
Carsten Otte313a3dc2007-10-11 19:16:52 +02003669void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3670{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003671 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003672
3673 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003674 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003675
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003676 /*
3677 * Disable page faults because we're in atomic context here.
3678 * kvm_write_guest_offset_cached() would call might_fault()
3679 * that relies on pagefault_disable() to tell if there's a
3680 * bug. NOTE: the write to guest memory may not go through if
3681 * during postcopy live migration or if there's heavy guest
3682 * paging.
3683 */
3684 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003685 /*
3686 * kvm_memslots() will be called by
3687 * kvm_write_guest_offset_cached() so take the srcu lock.
3688 */
3689 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003690 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003691 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003692 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003693 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003694 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003695 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003696 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3697 * on every vmexit, but if not, we might have a stale dr6 from the
3698 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003699 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003700 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003701}
3702
Carsten Otte313a3dc2007-10-11 19:16:52 +02003703static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3704 struct kvm_lapic_state *s)
3705{
Liran Alonfa59cc02017-12-24 18:12:53 +02003706 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003707 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003708
Radim Krčmářa92e2542016-07-12 22:09:22 +02003709 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003710}
3711
3712static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3713 struct kvm_lapic_state *s)
3714{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003715 int r;
3716
3717 r = kvm_apic_set_state(vcpu, s);
3718 if (r)
3719 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003720 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003721
3722 return 0;
3723}
3724
Matt Gingell127a4572015-11-17 17:32:05 +01003725static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3726{
3727 return (!lapic_in_kernel(vcpu) ||
3728 kvm_apic_accept_pic_intr(vcpu));
3729}
3730
Matt Gingell782d4222015-11-16 15:26:00 -08003731/*
3732 * if userspace requested an interrupt window, check that the
3733 * interrupt window is open.
3734 *
3735 * No need to exit to userspace if we already have an interrupt queued.
3736 */
3737static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3738{
3739 return kvm_arch_interrupt_allowed(vcpu) &&
3740 !kvm_cpu_has_interrupt(vcpu) &&
3741 !kvm_event_needs_reinjection(vcpu) &&
3742 kvm_cpu_accept_dm_intr(vcpu);
3743}
3744
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003745static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3746 struct kvm_interrupt *irq)
3747{
Chen Gang02cdb502013-02-27 11:33:25 +08003748 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003749 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003750
3751 if (!irqchip_in_kernel(vcpu->kvm)) {
3752 kvm_queue_interrupt(vcpu, irq->irq, false);
3753 kvm_make_request(KVM_REQ_EVENT, vcpu);
3754 return 0;
3755 }
3756
3757 /*
3758 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3759 * fail for in-kernel 8259.
3760 */
3761 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003762 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003763
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003764 if (vcpu->arch.pending_external_vector != -1)
3765 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003766
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003767 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003768 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003769 return 0;
3770}
3771
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003772static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3773{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003774 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003775
3776 return 0;
3777}
3778
Paolo Bonzinif0778252015-04-01 15:06:40 +02003779static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3780{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003781 kvm_make_request(KVM_REQ_SMI, vcpu);
3782
Paolo Bonzinif0778252015-04-01 15:06:40 +02003783 return 0;
3784}
3785
Avi Kivityb209749f2007-10-22 16:50:39 +02003786static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3787 struct kvm_tpr_access_ctl *tac)
3788{
3789 if (tac->flags)
3790 return -EINVAL;
3791 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3792 return 0;
3793}
3794
Huang Ying890ca9a2009-05-11 16:48:15 +08003795static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3796 u64 mcg_cap)
3797{
3798 int r;
3799 unsigned bank_num = mcg_cap & 0xff, bank;
3800
3801 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07003802 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003803 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003804 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003805 goto out;
3806 r = 0;
3807 vcpu->arch.mcg_cap = mcg_cap;
3808 /* Init IA32_MCG_CTL to all 1s */
3809 if (mcg_cap & MCG_CTL_P)
3810 vcpu->arch.mcg_ctl = ~(u64)0;
3811 /* Init IA32_MCi_CTL to all 1s */
3812 for (bank = 0; bank < bank_num; bank++)
3813 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003814
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003815 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003816out:
3817 return r;
3818}
3819
3820static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3821 struct kvm_x86_mce *mce)
3822{
3823 u64 mcg_cap = vcpu->arch.mcg_cap;
3824 unsigned bank_num = mcg_cap & 0xff;
3825 u64 *banks = vcpu->arch.mce_banks;
3826
3827 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3828 return -EINVAL;
3829 /*
3830 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3831 * reporting is disabled
3832 */
3833 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3834 vcpu->arch.mcg_ctl != ~(u64)0)
3835 return 0;
3836 banks += 4 * mce->bank;
3837 /*
3838 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3839 * reporting is disabled for the bank
3840 */
3841 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3842 return 0;
3843 if (mce->status & MCI_STATUS_UC) {
3844 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003845 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003846 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003847 return 0;
3848 }
3849 if (banks[1] & MCI_STATUS_VAL)
3850 mce->status |= MCI_STATUS_OVER;
3851 banks[2] = mce->addr;
3852 banks[3] = mce->misc;
3853 vcpu->arch.mcg_status = mce->mcg_status;
3854 banks[1] = mce->status;
3855 kvm_queue_exception(vcpu, MC_VECTOR);
3856 } else if (!(banks[1] & MCI_STATUS_VAL)
3857 || !(banks[1] & MCI_STATUS_UC)) {
3858 if (banks[1] & MCI_STATUS_VAL)
3859 mce->status |= MCI_STATUS_OVER;
3860 banks[2] = mce->addr;
3861 banks[3] = mce->misc;
3862 banks[1] = mce->status;
3863 } else
3864 banks[1] |= MCI_STATUS_OVER;
3865 return 0;
3866}
3867
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003868static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3869 struct kvm_vcpu_events *events)
3870{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003871 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003872
Wanpeng Li664f8e22017-08-24 03:35:09 -07003873 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003874 * In guest mode, payload delivery should be deferred,
3875 * so that the L1 hypervisor can intercept #PF before
3876 * CR2 is modified (or intercept #DB before DR6 is
3877 * modified under nVMX). Unless the per-VM capability,
3878 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3879 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3880 * opportunistically defer the exception payload, deliver it if the
3881 * capability hasn't been requested before processing a
3882 * KVM_GET_VCPU_EVENTS.
3883 */
3884 if (!vcpu->kvm->arch.exception_payload_enabled &&
3885 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3886 kvm_deliver_exception_payload(vcpu);
3887
3888 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003889 * The API doesn't provide the instruction length for software
3890 * exceptions, so don't report them. As long as the guest RIP
3891 * isn't advanced, we should expect to encounter the exception
3892 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003893 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003894 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3895 events->exception.injected = 0;
3896 events->exception.pending = 0;
3897 } else {
3898 events->exception.injected = vcpu->arch.exception.injected;
3899 events->exception.pending = vcpu->arch.exception.pending;
3900 /*
3901 * For ABI compatibility, deliberately conflate
3902 * pending and injected exceptions when
3903 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3904 */
3905 if (!vcpu->kvm->arch.exception_payload_enabled)
3906 events->exception.injected |=
3907 vcpu->arch.exception.pending;
3908 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003909 events->exception.nr = vcpu->arch.exception.nr;
3910 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3911 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003912 events->exception_has_payload = vcpu->arch.exception.has_payload;
3913 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003914
Jan Kiszka03b82a32010-02-15 10:45:41 +01003915 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003916 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003917 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003918 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003919 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003920
3921 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003922 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003923 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003924 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003925
Jan Kiszka66450a22013-03-13 12:42:34 +01003926 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003927
Paolo Bonzinif0778252015-04-01 15:06:40 +02003928 events->smi.smm = is_smm(vcpu);
3929 events->smi.pending = vcpu->arch.smi_pending;
3930 events->smi.smm_inside_nmi =
3931 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3932 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3933
Jan Kiszkadab4b912009-12-06 18:24:15 +01003934 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003935 | KVM_VCPUEVENT_VALID_SHADOW
3936 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003937 if (vcpu->kvm->arch.exception_payload_enabled)
3938 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3939
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003940 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003941}
3942
Sean Christophersonc5833c72019-04-02 08:03:10 -07003943static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003944
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003945static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3946 struct kvm_vcpu_events *events)
3947{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003948 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003949 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003950 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003951 | KVM_VCPUEVENT_VALID_SMM
3952 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003953 return -EINVAL;
3954
Jim Mattson59073aa2018-10-16 14:29:20 -07003955 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3956 if (!vcpu->kvm->arch.exception_payload_enabled)
3957 return -EINVAL;
3958 if (events->exception.pending)
3959 events->exception.injected = 0;
3960 else
3961 events->exception_has_payload = 0;
3962 } else {
3963 events->exception.pending = 0;
3964 events->exception_has_payload = 0;
3965 }
3966
3967 if ((events->exception.injected || events->exception.pending) &&
3968 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003969 return -EINVAL;
3970
David Hildenbrand28bf2882017-03-23 11:46:03 +01003971 /* INITs are latched while in SMM */
3972 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3973 (events->smi.smm || events->smi.pending) &&
3974 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3975 return -EINVAL;
3976
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003977 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003978 vcpu->arch.exception.injected = events->exception.injected;
3979 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003980 vcpu->arch.exception.nr = events->exception.nr;
3981 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3982 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003983 vcpu->arch.exception.has_payload = events->exception_has_payload;
3984 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003985
Liran Alon04140b42018-03-23 03:01:31 +03003986 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003987 vcpu->arch.interrupt.nr = events->interrupt.nr;
3988 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003989 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003990 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01003991 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003992
3993 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003994 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3995 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003996 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003997
Jan Kiszka66450a22013-03-13 12:42:34 +01003998 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003999 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004000 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004001
Paolo Bonzinif0778252015-04-01 15:06:40 +02004002 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07004003 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
4004 if (events->smi.smm)
4005 vcpu->arch.hflags |= HF_SMM_MASK;
4006 else
4007 vcpu->arch.hflags &= ~HF_SMM_MASK;
4008 kvm_smm_changed(vcpu);
4009 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004010
Paolo Bonzinif0778252015-04-01 15:06:40 +02004011 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004012
4013 if (events->smi.smm) {
4014 if (events->smi.smm_inside_nmi)
4015 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004016 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004017 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004018 }
4019
4020 if (lapic_in_kernel(vcpu)) {
4021 if (events->smi.latched_init)
4022 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4023 else
4024 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004025 }
4026 }
4027
Avi Kivity3842d132010-07-27 12:30:24 +03004028 kvm_make_request(KVM_REQ_EVENT, vcpu);
4029
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004030 return 0;
4031}
4032
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004033static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4034 struct kvm_debugregs *dbgregs)
4035{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004036 unsigned long val;
4037
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004038 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004039 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004040 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004041 dbgregs->dr7 = vcpu->arch.dr7;
4042 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004043 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004044}
4045
4046static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4047 struct kvm_debugregs *dbgregs)
4048{
4049 if (dbgregs->flags)
4050 return -EINVAL;
4051
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004052 if (dbgregs->dr6 & ~0xffffffffull)
4053 return -EINVAL;
4054 if (dbgregs->dr7 & ~0xffffffffull)
4055 return -EINVAL;
4056
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004057 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004058 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004059 vcpu->arch.dr6 = dbgregs->dr6;
4060 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004061 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004062
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004063 return 0;
4064}
4065
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004066#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4067
4068static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4069{
Marc Orrb666a4b2018-11-06 14:53:56 -08004070 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004071 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004072 u64 valid;
4073
4074 /*
4075 * Copy legacy XSAVE area, to avoid complications with CPUID
4076 * leaves 0 and 1 in the loop below.
4077 */
4078 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4079
4080 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004081 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004082 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4083
4084 /*
4085 * Copy each region from the possibly compacted offset to the
4086 * non-compacted offset.
4087 */
Dave Hansend91cab72015-09-02 16:31:26 -07004088 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004089 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004090 u64 xfeature_mask = valid & -valid;
4091 int xfeature_nr = fls64(xfeature_mask) - 1;
4092 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004093
4094 if (src) {
4095 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004096 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004097 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004098 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004099 memcpy(dest + offset, &vcpu->arch.pkru,
4100 sizeof(vcpu->arch.pkru));
4101 else
4102 memcpy(dest + offset, src, size);
4103
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004104 }
4105
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004106 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004107 }
4108}
4109
4110static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4111{
Marc Orrb666a4b2018-11-06 14:53:56 -08004112 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004113 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4114 u64 valid;
4115
4116 /*
4117 * Copy legacy XSAVE area, to avoid complications with CPUID
4118 * leaves 0 and 1 in the loop below.
4119 */
4120 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4121
4122 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004123 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004124 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004125 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004126
4127 /*
4128 * Copy each region from the non-compacted offset to the
4129 * possibly compacted offset.
4130 */
Dave Hansend91cab72015-09-02 16:31:26 -07004131 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004132 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004133 u64 xfeature_mask = valid & -valid;
4134 int xfeature_nr = fls64(xfeature_mask) - 1;
4135 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004136
4137 if (dest) {
4138 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004139 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004140 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004141 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004142 memcpy(&vcpu->arch.pkru, src + offset,
4143 sizeof(vcpu->arch.pkru));
4144 else
4145 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004146 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004147
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004148 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004149 }
4150}
4151
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004152static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4153 struct kvm_xsave *guest_xsave)
4154{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004155 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004156 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4157 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004158 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004159 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004160 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004161 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004162 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004163 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004164 }
4165}
4166
Wanpeng Lia5758132017-05-11 02:58:55 -07004167#define XSAVE_MXCSR_OFFSET 24
4168
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004169static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4170 struct kvm_xsave *guest_xsave)
4171{
4172 u64 xstate_bv =
4173 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004174 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004175
Borislav Petkovd366bf72016-04-04 22:25:02 +02004176 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004177 /*
4178 * Here we allow setting states that are not present in
4179 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4180 * with old userspace.
4181 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004182 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004183 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004184 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004185 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004186 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4187 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004188 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004189 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004190 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004191 }
4192 return 0;
4193}
4194
4195static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4196 struct kvm_xcrs *guest_xcrs)
4197{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004198 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004199 guest_xcrs->nr_xcrs = 0;
4200 return;
4201 }
4202
4203 guest_xcrs->nr_xcrs = 1;
4204 guest_xcrs->flags = 0;
4205 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4206 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4207}
4208
4209static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4210 struct kvm_xcrs *guest_xcrs)
4211{
4212 int i, r = 0;
4213
Borislav Petkovd366bf72016-04-04 22:25:02 +02004214 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004215 return -EINVAL;
4216
4217 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4218 return -EINVAL;
4219
4220 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4221 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004222 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004223 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004224 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004225 break;
4226 }
4227 if (r)
4228 r = -EINVAL;
4229 return r;
4230}
4231
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004232/*
4233 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4234 * stopped by the hypervisor. This function will be called from the host only.
4235 * EINVAL is returned when the host attempts to set the flag for a guest that
4236 * does not support pv clocks.
4237 */
4238static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4239{
Andy Honig0b794592013-02-20 14:48:10 -08004240 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004241 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004242 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004243 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4244 return 0;
4245}
4246
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004247static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4248 struct kvm_enable_cap *cap)
4249{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004250 int r;
4251 uint16_t vmcs_version;
4252 void __user *user_ptr;
4253
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004254 if (cap->flags)
4255 return -EINVAL;
4256
4257 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004258 case KVM_CAP_HYPERV_SYNIC2:
4259 if (cap->args[0])
4260 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004261 /* fall through */
4262
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004263 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004264 if (!irqchip_in_kernel(vcpu->kvm))
4265 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004266 return kvm_hv_activate_synic(vcpu, cap->cap ==
4267 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004268 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004269 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004270 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004271 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004272 if (!r) {
4273 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4274 if (copy_to_user(user_ptr, &vmcs_version,
4275 sizeof(vmcs_version)))
4276 r = -EFAULT;
4277 }
4278 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004279 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004280 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004281 return -ENOTTY;
4282
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004283 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004284
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004285 default:
4286 return -EINVAL;
4287 }
4288}
4289
Carsten Otte313a3dc2007-10-11 19:16:52 +02004290long kvm_arch_vcpu_ioctl(struct file *filp,
4291 unsigned int ioctl, unsigned long arg)
4292{
4293 struct kvm_vcpu *vcpu = filp->private_data;
4294 void __user *argp = (void __user *)arg;
4295 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004296 union {
4297 struct kvm_lapic_state *lapic;
4298 struct kvm_xsave *xsave;
4299 struct kvm_xcrs *xcrs;
4300 void *buffer;
4301 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004302
Christoffer Dall9b0624712017-12-04 21:35:36 +01004303 vcpu_load(vcpu);
4304
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004305 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004306 switch (ioctl) {
4307 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004308 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004309 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004310 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004311 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4312 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004313
Dave Hansenb772ff32008-08-11 10:01:47 -07004314 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004315 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004316 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004317 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004318 if (r)
4319 goto out;
4320 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004321 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004322 goto out;
4323 r = 0;
4324 break;
4325 }
4326 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004327 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004328 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004329 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004330 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004331 if (IS_ERR(u.lapic)) {
4332 r = PTR_ERR(u.lapic);
4333 goto out_nofree;
4334 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004335
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004336 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004337 break;
4338 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004339 case KVM_INTERRUPT: {
4340 struct kvm_interrupt irq;
4341
4342 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004343 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004344 goto out;
4345 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004346 break;
4347 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004348 case KVM_NMI: {
4349 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004350 break;
4351 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004352 case KVM_SMI: {
4353 r = kvm_vcpu_ioctl_smi(vcpu);
4354 break;
4355 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004356 case KVM_SET_CPUID: {
4357 struct kvm_cpuid __user *cpuid_arg = argp;
4358 struct kvm_cpuid cpuid;
4359
4360 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004361 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004362 goto out;
4363 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004364 break;
4365 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004366 case KVM_SET_CPUID2: {
4367 struct kvm_cpuid2 __user *cpuid_arg = argp;
4368 struct kvm_cpuid2 cpuid;
4369
4370 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004371 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004372 goto out;
4373 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004374 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004375 break;
4376 }
4377 case KVM_GET_CPUID2: {
4378 struct kvm_cpuid2 __user *cpuid_arg = argp;
4379 struct kvm_cpuid2 cpuid;
4380
4381 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004382 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004383 goto out;
4384 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004385 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004386 if (r)
4387 goto out;
4388 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004389 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004390 goto out;
4391 r = 0;
4392 break;
4393 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004394 case KVM_GET_MSRS: {
4395 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004396 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004397 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004398 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004399 }
4400 case KVM_SET_MSRS: {
4401 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004402 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004403 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004404 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004405 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004406 case KVM_TPR_ACCESS_REPORTING: {
4407 struct kvm_tpr_access_ctl tac;
4408
4409 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004410 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004411 goto out;
4412 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4413 if (r)
4414 goto out;
4415 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004416 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004417 goto out;
4418 r = 0;
4419 break;
4420 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004421 case KVM_SET_VAPIC_ADDR: {
4422 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004423 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004424
4425 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004426 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004427 goto out;
4428 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004429 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004430 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004431 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004432 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004433 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004434 break;
4435 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004436 case KVM_X86_SETUP_MCE: {
4437 u64 mcg_cap;
4438
4439 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004440 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004441 goto out;
4442 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4443 break;
4444 }
4445 case KVM_X86_SET_MCE: {
4446 struct kvm_x86_mce mce;
4447
4448 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004449 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004450 goto out;
4451 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4452 break;
4453 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004454 case KVM_GET_VCPU_EVENTS: {
4455 struct kvm_vcpu_events events;
4456
4457 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4458
4459 r = -EFAULT;
4460 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4461 break;
4462 r = 0;
4463 break;
4464 }
4465 case KVM_SET_VCPU_EVENTS: {
4466 struct kvm_vcpu_events events;
4467
4468 r = -EFAULT;
4469 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4470 break;
4471
4472 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4473 break;
4474 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004475 case KVM_GET_DEBUGREGS: {
4476 struct kvm_debugregs dbgregs;
4477
4478 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4479
4480 r = -EFAULT;
4481 if (copy_to_user(argp, &dbgregs,
4482 sizeof(struct kvm_debugregs)))
4483 break;
4484 r = 0;
4485 break;
4486 }
4487 case KVM_SET_DEBUGREGS: {
4488 struct kvm_debugregs dbgregs;
4489
4490 r = -EFAULT;
4491 if (copy_from_user(&dbgregs, argp,
4492 sizeof(struct kvm_debugregs)))
4493 break;
4494
4495 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4496 break;
4497 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004498 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004499 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004500 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004501 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004502 break;
4503
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004504 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004505
4506 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004507 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004508 break;
4509 r = 0;
4510 break;
4511 }
4512 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004513 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004514 if (IS_ERR(u.xsave)) {
4515 r = PTR_ERR(u.xsave);
4516 goto out_nofree;
4517 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004518
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004519 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004520 break;
4521 }
4522 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004523 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004524 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004525 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004526 break;
4527
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004528 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004529
4530 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004531 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004532 sizeof(struct kvm_xcrs)))
4533 break;
4534 r = 0;
4535 break;
4536 }
4537 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004538 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004539 if (IS_ERR(u.xcrs)) {
4540 r = PTR_ERR(u.xcrs);
4541 goto out_nofree;
4542 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004543
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004544 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004545 break;
4546 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004547 case KVM_SET_TSC_KHZ: {
4548 u32 user_tsc_khz;
4549
4550 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004551 user_tsc_khz = (u32)arg;
4552
4553 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4554 goto out;
4555
Zachary Amsdencc578282012-02-03 15:43:50 -02004556 if (user_tsc_khz == 0)
4557 user_tsc_khz = tsc_khz;
4558
Haozhong Zhang381d5852015-10-20 15:39:04 +08004559 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4560 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004561
Joerg Roedel92a1f122011-03-25 09:44:51 +01004562 goto out;
4563 }
4564 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004565 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004566 goto out;
4567 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004568 case KVM_KVMCLOCK_CTRL: {
4569 r = kvm_set_guest_paused(vcpu);
4570 goto out;
4571 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004572 case KVM_ENABLE_CAP: {
4573 struct kvm_enable_cap cap;
4574
4575 r = -EFAULT;
4576 if (copy_from_user(&cap, argp, sizeof(cap)))
4577 goto out;
4578 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4579 break;
4580 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004581 case KVM_GET_NESTED_STATE: {
4582 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4583 u32 user_data_size;
4584
4585 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004586 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004587 break;
4588
4589 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004590 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004591 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004592 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004593
Paolo Bonzini33b22172020-04-17 10:24:18 -04004594 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
4595 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004596 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004597 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004598
4599 if (r > user_data_size) {
4600 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004601 r = -EFAULT;
4602 else
4603 r = -E2BIG;
4604 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004605 }
Liran Alon26b471c2018-09-16 14:28:20 +03004606
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004607 r = 0;
4608 break;
4609 }
4610 case KVM_SET_NESTED_STATE: {
4611 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4612 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004613 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004614
4615 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004616 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004617 break;
4618
Liran Alon26b471c2018-09-16 14:28:20 +03004619 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004620 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004621 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004622
Liran Alon26b471c2018-09-16 14:28:20 +03004623 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004624 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004625 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004626
4627 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004628 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinidf2a69a2020-05-19 12:51:32 -04004629 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING))
Liran Alon26b471c2018-09-16 14:28:20 +03004630 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004631
4632 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004633 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4634 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004635 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004636
Sean Christophersonad5996d2019-11-22 08:58:18 -08004637 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04004638 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004639 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004640 break;
4641 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004642 case KVM_GET_SUPPORTED_HV_CPUID: {
4643 struct kvm_cpuid2 __user *cpuid_arg = argp;
4644 struct kvm_cpuid2 cpuid;
4645
4646 r = -EFAULT;
4647 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4648 goto out;
4649
4650 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4651 cpuid_arg->entries);
4652 if (r)
4653 goto out;
4654
4655 r = -EFAULT;
4656 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4657 goto out;
4658 r = 0;
4659 break;
4660 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004661 default:
4662 r = -EINVAL;
4663 }
4664out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004665 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004666out_nofree:
4667 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004668 return r;
4669}
4670
Souptick Joarder1499fa82018-04-19 00:49:58 +05304671vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004672{
4673 return VM_FAULT_SIGBUS;
4674}
4675
Carsten Otte1fe779f2007-10-29 16:08:35 +01004676static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4677{
4678 int ret;
4679
4680 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004681 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004682 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004683 return ret;
4684}
4685
Sheng Yangb927a3c2009-07-21 10:42:48 +08004686static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4687 u64 ident_addr)
4688{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004689 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004690}
4691
Carsten Otte1fe779f2007-10-29 16:08:35 +01004692static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004693 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004694{
4695 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4696 return -EINVAL;
4697
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004698 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004699
4700 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004701 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004702
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004703 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004704 return 0;
4705}
4706
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004707static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004708{
Dave Hansen39de71e2010-08-19 18:11:14 -07004709 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004710}
4711
Carsten Otte1fe779f2007-10-29 16:08:35 +01004712static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4713{
David Hildenbrand90bca052017-04-07 10:50:23 +02004714 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004715 int r;
4716
4717 r = 0;
4718 switch (chip->chip_id) {
4719 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004720 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004721 sizeof(struct kvm_pic_state));
4722 break;
4723 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004724 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004725 sizeof(struct kvm_pic_state));
4726 break;
4727 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004728 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004729 break;
4730 default:
4731 r = -EINVAL;
4732 break;
4733 }
4734 return r;
4735}
4736
4737static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4738{
David Hildenbrand90bca052017-04-07 10:50:23 +02004739 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004740 int r;
4741
4742 r = 0;
4743 switch (chip->chip_id) {
4744 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004745 spin_lock(&pic->lock);
4746 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004747 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004748 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004749 break;
4750 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004751 spin_lock(&pic->lock);
4752 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004753 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004754 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004755 break;
4756 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004757 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004758 break;
4759 default:
4760 r = -EINVAL;
4761 break;
4762 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004763 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004764 return r;
4765}
4766
Sheng Yange0f63cb2008-03-04 00:50:59 +08004767static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4768{
Radim Krčmář34f39412016-03-02 22:56:50 +01004769 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4770
4771 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4772
4773 mutex_lock(&kps->lock);
4774 memcpy(ps, &kps->channels, sizeof(*ps));
4775 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304776 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004777}
4778
4779static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4780{
Andrew Honig01856042015-11-18 14:50:23 -08004781 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004782 struct kvm_pit *pit = kvm->arch.vpit;
4783
4784 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004785 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004786 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004787 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4788 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304789 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004790}
4791
4792static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4793{
Beth Kone9f42752009-07-07 11:50:38 -04004794 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4795 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4796 sizeof(ps->channels));
4797 ps->flags = kvm->arch.vpit->pit_state.flags;
4798 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004799 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304800 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004801}
4802
4803static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4804{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304805 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004806 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004807 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004808 struct kvm_pit *pit = kvm->arch.vpit;
4809
4810 mutex_lock(&pit->pit_state.lock);
4811 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004812 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4813 if (!prev_legacy && cur_legacy)
4814 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004815 memcpy(&pit->pit_state.channels, &ps->channels,
4816 sizeof(pit->pit_state.channels));
4817 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004818 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004819 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004820 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004821 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304822 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004823}
4824
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004825static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4826 struct kvm_reinject_control *control)
4827{
Radim Krčmář71474e22016-03-02 22:56:45 +01004828 struct kvm_pit *pit = kvm->arch.vpit;
4829
Radim Krčmář71474e22016-03-02 22:56:45 +01004830 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4831 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4832 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4833 */
4834 mutex_lock(&pit->pit_state.lock);
4835 kvm_pit_set_reinject(pit, control->pit_reinject);
4836 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004837
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004838 return 0;
4839}
4840
Sean Christopherson0dff0842020-02-18 13:07:29 -08004841void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004842{
Kai Huang88178fd2015-01-28 10:54:27 +08004843 /*
4844 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4845 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004846 if (kvm_x86_ops.flush_log_dirty)
4847 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004848}
4849
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004850int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4851 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004852{
4853 if (!irqchip_in_kernel(kvm))
4854 return -ENXIO;
4855
4856 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004857 irq_event->irq, irq_event->level,
4858 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004859 return 0;
4860}
4861
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004862int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4863 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004864{
4865 int r;
4866
4867 if (cap->flags)
4868 return -EINVAL;
4869
4870 switch (cap->cap) {
4871 case KVM_CAP_DISABLE_QUIRKS:
4872 kvm->arch.disabled_quirks = cap->args[0];
4873 r = 0;
4874 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004875 case KVM_CAP_SPLIT_IRQCHIP: {
4876 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004877 r = -EINVAL;
4878 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4879 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004880 r = -EEXIST;
4881 if (irqchip_in_kernel(kvm))
4882 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004883 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004884 goto split_irqchip_unlock;
4885 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004886 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004887 goto split_irqchip_unlock;
4888 /* Pairs with irqchip_in_kernel. */
4889 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004890 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004891 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004892 r = 0;
4893split_irqchip_unlock:
4894 mutex_unlock(&kvm->lock);
4895 break;
4896 }
Radim Krčmář371313132016-07-12 22:09:27 +02004897 case KVM_CAP_X2APIC_API:
4898 r = -EINVAL;
4899 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4900 break;
4901
4902 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4903 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004904 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4905 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004906
4907 r = 0;
4908 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004909 case KVM_CAP_X86_DISABLE_EXITS:
4910 r = -EINVAL;
4911 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4912 break;
4913
4914 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4915 kvm_can_mwait_in_guest())
4916 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004917 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004918 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004919 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4920 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004921 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4922 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004923 r = 0;
4924 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004925 case KVM_CAP_MSR_PLATFORM_INFO:
4926 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4927 r = 0;
4928 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004929 case KVM_CAP_EXCEPTION_PAYLOAD:
4930 kvm->arch.exception_payload_enabled = cap->args[0];
4931 r = 0;
4932 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004933 default:
4934 r = -EINVAL;
4935 break;
4936 }
4937 return r;
4938}
4939
Carsten Otte1fe779f2007-10-29 16:08:35 +01004940long kvm_arch_vm_ioctl(struct file *filp,
4941 unsigned int ioctl, unsigned long arg)
4942{
4943 struct kvm *kvm = filp->private_data;
4944 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004945 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004946 /*
4947 * This union makes it completely explicit to gcc-3.x
4948 * that these two variables' stack usage should be
4949 * combined, not added together.
4950 */
4951 union {
4952 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004953 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004954 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004955 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004956
4957 switch (ioctl) {
4958 case KVM_SET_TSS_ADDR:
4959 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004960 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004961 case KVM_SET_IDENTITY_MAP_ADDR: {
4962 u64 ident_addr;
4963
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004964 mutex_lock(&kvm->lock);
4965 r = -EINVAL;
4966 if (kvm->created_vcpus)
4967 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004968 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004969 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004970 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004971 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004972set_identity_unlock:
4973 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004974 break;
4975 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004976 case KVM_SET_NR_MMU_PAGES:
4977 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004978 break;
4979 case KVM_GET_NR_MMU_PAGES:
4980 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4981 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004982 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004983 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004984
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004985 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004986 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004987 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004988
Avi Kivity3e515702012-03-05 14:23:29 +02004989 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004990 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004991 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004992
4993 r = kvm_pic_init(kvm);
4994 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004995 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004996
4997 r = kvm_ioapic_init(kvm);
4998 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004999 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005000 goto create_irqchip_unlock;
5001 }
5002
Avi Kivity399ec802008-11-19 13:58:46 +02005003 r = kvm_setup_default_irq_routing(kvm);
5004 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005005 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005006 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005007 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005008 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005009 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005010 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005011 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005012 create_irqchip_unlock:
5013 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005014 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005015 }
Sheng Yang78376992008-01-28 05:10:22 +08005016 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005017 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5018 goto create_pit;
5019 case KVM_CREATE_PIT2:
5020 r = -EFAULT;
5021 if (copy_from_user(&u.pit_config, argp,
5022 sizeof(struct kvm_pit_config)))
5023 goto out;
5024 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005025 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005026 r = -EEXIST;
5027 if (kvm->arch.vpit)
5028 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005029 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005030 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005031 if (kvm->arch.vpit)
5032 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005033 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005034 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005035 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005036 case KVM_GET_IRQCHIP: {
5037 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005038 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005039
Sasha Levinff5c2c02011-12-04 19:36:29 +02005040 chip = memdup_user(argp, sizeof(*chip));
5041 if (IS_ERR(chip)) {
5042 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005043 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005044 }
5045
Carsten Otte1fe779f2007-10-29 16:08:35 +01005046 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005047 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005048 goto get_irqchip_out;
5049 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5050 if (r)
5051 goto get_irqchip_out;
5052 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005053 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005054 goto get_irqchip_out;
5055 r = 0;
5056 get_irqchip_out:
5057 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005058 break;
5059 }
5060 case KVM_SET_IRQCHIP: {
5061 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005062 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005063
Sasha Levinff5c2c02011-12-04 19:36:29 +02005064 chip = memdup_user(argp, sizeof(*chip));
5065 if (IS_ERR(chip)) {
5066 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005067 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005068 }
5069
Carsten Otte1fe779f2007-10-29 16:08:35 +01005070 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005071 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005072 goto set_irqchip_out;
5073 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005074 set_irqchip_out:
5075 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005076 break;
5077 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005078 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005079 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005080 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005081 goto out;
5082 r = -ENXIO;
5083 if (!kvm->arch.vpit)
5084 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005085 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005086 if (r)
5087 goto out;
5088 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005089 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005090 goto out;
5091 r = 0;
5092 break;
5093 }
5094 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005095 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005096 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005097 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005098 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005099 r = -ENXIO;
5100 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005101 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005102 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005103set_pit_out:
5104 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005105 break;
5106 }
Beth Kone9f42752009-07-07 11:50:38 -04005107 case KVM_GET_PIT2: {
5108 r = -ENXIO;
5109 if (!kvm->arch.vpit)
5110 goto out;
5111 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5112 if (r)
5113 goto out;
5114 r = -EFAULT;
5115 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5116 goto out;
5117 r = 0;
5118 break;
5119 }
5120 case KVM_SET_PIT2: {
5121 r = -EFAULT;
5122 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5123 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005124 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005125 r = -ENXIO;
5126 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005127 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005128 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005129set_pit2_out:
5130 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005131 break;
5132 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005133 case KVM_REINJECT_CONTROL: {
5134 struct kvm_reinject_control control;
5135 r = -EFAULT;
5136 if (copy_from_user(&control, argp, sizeof(control)))
5137 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005138 r = -ENXIO;
5139 if (!kvm->arch.vpit)
5140 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005141 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005142 break;
5143 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005144 case KVM_SET_BOOT_CPU_ID:
5145 r = 0;
5146 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005147 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005148 r = -EBUSY;
5149 else
5150 kvm->arch.bsp_vcpu_id = arg;
5151 mutex_unlock(&kvm->lock);
5152 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005153 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005154 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005155 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005156 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005157 goto out;
5158 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005159 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005160 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005161 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005162 r = 0;
5163 break;
5164 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005165 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005166 struct kvm_clock_data user_ns;
5167 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005168
5169 r = -EFAULT;
5170 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5171 goto out;
5172
5173 r = -EINVAL;
5174 if (user_ns.flags)
5175 goto out;
5176
5177 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005178 /*
5179 * TODO: userspace has to take care of races with VCPU_RUN, so
5180 * kvm_gen_update_masterclock() can be cut down to locked
5181 * pvclock_update_vm_gtod_copy().
5182 */
5183 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005184 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005185 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005186 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005187 break;
5188 }
5189 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005190 struct kvm_clock_data user_ns;
5191 u64 now_ns;
5192
Marcelo Tosattie891a322017-04-17 12:51:37 -03005193 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005194 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005195 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005196 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005197
5198 r = -EFAULT;
5199 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5200 goto out;
5201 r = 0;
5202 break;
5203 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005204 case KVM_MEMORY_ENCRYPT_OP: {
5205 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005206 if (kvm_x86_ops.mem_enc_op)
5207 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005208 break;
5209 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005210 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5211 struct kvm_enc_region region;
5212
5213 r = -EFAULT;
5214 if (copy_from_user(&region, argp, sizeof(region)))
5215 goto out;
5216
5217 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005218 if (kvm_x86_ops.mem_enc_reg_region)
5219 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005220 break;
5221 }
5222 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5223 struct kvm_enc_region region;
5224
5225 r = -EFAULT;
5226 if (copy_from_user(&region, argp, sizeof(region)))
5227 goto out;
5228
5229 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005230 if (kvm_x86_ops.mem_enc_unreg_region)
5231 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005232 break;
5233 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005234 case KVM_HYPERV_EVENTFD: {
5235 struct kvm_hyperv_eventfd hvevfd;
5236
5237 r = -EFAULT;
5238 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5239 goto out;
5240 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5241 break;
5242 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005243 case KVM_SET_PMU_EVENT_FILTER:
5244 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5245 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005246 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005247 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005248 }
5249out:
5250 return r;
5251}
5252
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005253static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005254{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005255 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005256 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005257 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005258
Jim Mattsone2ada662019-08-21 11:20:04 -07005259 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005260 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005261
5262 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005263
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005264 num_msrs_to_save = 0;
5265 num_emulated_msrs = 0;
5266 num_msr_based_features = 0;
5267
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005268 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5269 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005270 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005271
5272 /*
5273 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005274 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005275 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005276 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005277 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005278 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005279 continue;
5280 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005281 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005282 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005283 continue;
5284 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03005285 case MSR_IA32_UMWAIT_CONTROL:
5286 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
5287 continue;
5288 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005289 case MSR_IA32_RTIT_CTL:
5290 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005291 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005292 continue;
5293 break;
5294 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005295 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005296 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5297 continue;
5298 break;
5299 case MSR_IA32_RTIT_OUTPUT_BASE:
5300 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005301 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005302 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5303 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5304 continue;
5305 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005306 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005307 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005308 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005309 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5310 continue;
5311 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005312 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005313 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005314 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5315 continue;
5316 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005317 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005318 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005319 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5320 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005321 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005322 default:
5323 break;
5324 }
5325
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005326 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005327 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005328
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005329 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005330 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005331 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005332
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005333 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005334 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005335
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005336 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005337 struct kvm_msr_entry msr;
5338
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005339 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005340 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005341 continue;
5342
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005343 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005344 }
Carsten Otte043405e2007-10-10 17:16:19 +02005345}
5346
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005347static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5348 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005349{
Avi Kivity70252a12010-01-19 12:51:22 +02005350 int handled = 0;
5351 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005352
Avi Kivity70252a12010-01-19 12:51:22 +02005353 do {
5354 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005355 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005356 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5357 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005358 break;
5359 handled += n;
5360 addr += n;
5361 len -= n;
5362 v += n;
5363 } while (len);
5364
5365 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005366}
5367
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005368static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005369{
Avi Kivity70252a12010-01-19 12:51:22 +02005370 int handled = 0;
5371 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005372
Avi Kivity70252a12010-01-19 12:51:22 +02005373 do {
5374 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005375 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005376 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5377 addr, n, v))
5378 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005379 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005380 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005381 handled += n;
5382 addr += n;
5383 len -= n;
5384 v += n;
5385 } while (len);
5386
5387 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005388}
5389
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005390static void kvm_set_segment(struct kvm_vcpu *vcpu,
5391 struct kvm_segment *var, int seg)
5392{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005393 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005394}
5395
5396void kvm_get_segment(struct kvm_vcpu *vcpu,
5397 struct kvm_segment *var, int seg)
5398{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005399 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005400}
5401
Paolo Bonzini54987b72014-09-02 13:23:06 +02005402gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5403 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005404{
5405 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005406
5407 BUG_ON(!mmu_is_nested(vcpu));
5408
5409 /* NPT walks are always user-walks */
5410 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005411 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005412
5413 return t_gpa;
5414}
5415
Avi Kivityab9ae312010-11-22 17:53:26 +02005416gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5417 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005418{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005419 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005420 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005421}
5422
Avi Kivityab9ae312010-11-22 17:53:26 +02005423 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5424 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005425{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005426 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005427 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005428 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005429}
5430
Avi Kivityab9ae312010-11-22 17:53:26 +02005431gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5432 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005433{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005434 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005435 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005436 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005437}
5438
5439/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005440gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5441 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005442{
Avi Kivityab9ae312010-11-22 17:53:26 +02005443 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005444}
5445
5446static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5447 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005448 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005449{
5450 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005451 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005452
5453 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005454 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005455 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005456 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005457 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005458 int ret;
5459
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005460 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005461 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005462 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5463 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005464 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005465 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005466 goto out;
5467 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005468
Izik Eidus77c20022008-12-29 01:42:19 +02005469 bytes -= toread;
5470 data += toread;
5471 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005472 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005473out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005474 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005475}
Izik Eidus77c20022008-12-29 01:42:19 +02005476
Gleb Natapov1871c602010-02-10 14:21:32 +02005477/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005478static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5479 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005480 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005481{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005482 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005483 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005484 unsigned offset;
5485 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005486
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005487 /* Inline kvm_read_guest_virt_helper for speed. */
5488 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5489 exception);
5490 if (unlikely(gpa == UNMAPPED_GVA))
5491 return X86EMUL_PROPAGATE_FAULT;
5492
5493 offset = addr & (PAGE_SIZE-1);
5494 if (WARN_ON(offset + bytes > PAGE_SIZE))
5495 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005496 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5497 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005498 if (unlikely(ret < 0))
5499 return X86EMUL_IO_NEEDED;
5500
5501 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005502}
5503
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005504int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005505 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005506 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005507{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005508 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005509
Paolo Bonzini353c0952019-01-29 18:41:16 +01005510 /*
5511 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5512 * is returned, but our callers are not ready for that and they blindly
5513 * call kvm_inject_page_fault. Ensure that they at least do not leak
5514 * uninitialized kernel stack memory into cr2 and error code.
5515 */
5516 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005517 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005518 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005519}
Nadav Har'El064aea72011-05-25 23:04:56 +03005520EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005521
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005522static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5523 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005524 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005525{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005526 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005527 u32 access = 0;
5528
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005529 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005530 access |= PFERR_USER_MASK;
5531
5532 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005533}
5534
Radim Krčmář7a036a62015-10-30 16:36:24 +01005535static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5536 unsigned long addr, void *val, unsigned int bytes)
5537{
5538 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5539 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5540
5541 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5542}
5543
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005544static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5545 struct kvm_vcpu *vcpu, u32 access,
5546 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005547{
5548 void *data = val;
5549 int r = X86EMUL_CONTINUE;
5550
5551 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005552 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005553 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005554 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005555 unsigned offset = addr & (PAGE_SIZE-1);
5556 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5557 int ret;
5558
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005559 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005560 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005561 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005562 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005563 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005564 goto out;
5565 }
5566
5567 bytes -= towrite;
5568 data += towrite;
5569 addr += towrite;
5570 }
5571out:
5572 return r;
5573}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005574
5575static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005576 unsigned int bytes, struct x86_exception *exception,
5577 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005578{
5579 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005580 u32 access = PFERR_WRITE_MASK;
5581
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005582 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005583 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005584
5585 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005586 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005587}
5588
5589int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5590 unsigned int bytes, struct x86_exception *exception)
5591{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005592 /* kvm_write_guest_virt_system can pull in tons of pages. */
5593 vcpu->arch.l1tf_flush_l1d = true;
5594
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005595 /*
5596 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5597 * is returned, but our callers are not ready for that and they blindly
5598 * call kvm_inject_page_fault. Ensure that they at least do not leak
5599 * uninitialized kernel stack memory into cr2 and error code.
5600 */
5601 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005602 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5603 PFERR_WRITE_MASK, exception);
5604}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005605EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005606
Wanpeng Li082d06e2018-04-03 16:28:48 -07005607int handle_ud(struct kvm_vcpu *vcpu)
5608{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005609 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005610 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005611 char sig[5]; /* ud2; .ascii "kvm" */
5612 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005613
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005614 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005615 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5616 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005617 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005618 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005619 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005620 }
5621
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005622 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005623}
5624EXPORT_SYMBOL_GPL(handle_ud);
5625
Tom Lendacky0f89b202016-12-14 14:59:23 -05005626static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5627 gpa_t gpa, bool write)
5628{
5629 /* For APIC access vmexit */
5630 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5631 return 1;
5632
5633 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5634 trace_vcpu_match_mmio(gva, gpa, write, true);
5635 return 1;
5636 }
5637
5638 return 0;
5639}
5640
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005641static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5642 gpa_t *gpa, struct x86_exception *exception,
5643 bool write)
5644{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005645 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03005646 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005647
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005648 /*
5649 * currently PKRU is only applied to ept enabled guest so
5650 * there is no pkey in EPT page table for L1 guest or EPT
5651 * shadow page table for L2 guest.
5652 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005653 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005654 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005655 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005656 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5657 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005658 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005659 return 1;
5660 }
5661
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005662 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5663
5664 if (*gpa == UNMAPPED_GVA)
5665 return -1;
5666
Tom Lendacky0f89b202016-12-14 14:59:23 -05005667 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005668}
5669
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005670int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005671 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005672{
5673 int ret;
5674
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005675 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005676 if (ret < 0)
5677 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005678 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005679 return 1;
5680}
5681
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005682struct read_write_emulator_ops {
5683 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5684 int bytes);
5685 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5686 void *val, int bytes);
5687 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5688 int bytes, void *val);
5689 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5690 void *val, int bytes);
5691 bool write;
5692};
5693
5694static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5695{
5696 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005697 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005698 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005699 vcpu->mmio_read_completed = 0;
5700 return 1;
5701 }
5702
5703 return 0;
5704}
5705
5706static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5707 void *val, int bytes)
5708{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005709 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005710}
5711
5712static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5713 void *val, int bytes)
5714{
5715 return emulator_write_phys(vcpu, gpa, val, bytes);
5716}
5717
5718static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5719{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005720 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005721 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5722}
5723
5724static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5725 void *val, int bytes)
5726{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005727 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005728 return X86EMUL_IO_NEEDED;
5729}
5730
5731static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5732 void *val, int bytes)
5733{
Avi Kivityf78146b2012-04-18 19:22:47 +03005734 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5735
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005736 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005737 return X86EMUL_CONTINUE;
5738}
5739
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005740static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005741 .read_write_prepare = read_prepare,
5742 .read_write_emulate = read_emulate,
5743 .read_write_mmio = vcpu_mmio_read,
5744 .read_write_exit_mmio = read_exit_mmio,
5745};
5746
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005747static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005748 .read_write_emulate = write_emulate,
5749 .read_write_mmio = write_mmio,
5750 .read_write_exit_mmio = write_exit_mmio,
5751 .write = true,
5752};
5753
Xiao Guangrong22388a32011-07-13 14:32:31 +08005754static int emulator_read_write_onepage(unsigned long addr, void *val,
5755 unsigned int bytes,
5756 struct x86_exception *exception,
5757 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005758 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005759{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005760 gpa_t gpa;
5761 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005762 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005763 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005764 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005765
5766 /*
5767 * If the exit was due to a NPF we may already have a GPA.
5768 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5769 * Note, this cannot be used on string operations since string
5770 * operation using rep will only have the initial GPA from the NPF
5771 * occurred.
5772 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005773 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5774 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5775 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005776 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5777 } else {
5778 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5779 if (ret < 0)
5780 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005781 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005782
Brijesh Singh618232e2017-08-17 18:36:57 +02005783 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005784 return X86EMUL_CONTINUE;
5785
Carsten Ottebbd9b642007-10-30 18:44:21 +01005786 /*
5787 * Is this MMIO handled locally?
5788 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005789 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005790 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005791 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005792
Avi Kivity70252a12010-01-19 12:51:22 +02005793 gpa += handled;
5794 bytes -= handled;
5795 val += handled;
5796
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005797 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5798 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5799 frag->gpa = gpa;
5800 frag->data = val;
5801 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005802 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005803}
5804
Xiubo Li52eb5a62015-03-13 17:39:45 +08005805static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5806 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005807 void *val, unsigned int bytes,
5808 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005809 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005810{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005811 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005812 gpa_t gpa;
5813 int rc;
5814
5815 if (ops->read_write_prepare &&
5816 ops->read_write_prepare(vcpu, val, bytes))
5817 return X86EMUL_CONTINUE;
5818
5819 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005820
Carsten Ottebbd9b642007-10-30 18:44:21 +01005821 /* Crossing a page boundary? */
5822 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005823 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005824
5825 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005826 rc = emulator_read_write_onepage(addr, val, now, exception,
5827 vcpu, ops);
5828
Carsten Ottebbd9b642007-10-30 18:44:21 +01005829 if (rc != X86EMUL_CONTINUE)
5830 return rc;
5831 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005832 if (ctxt->mode != X86EMUL_MODE_PROT64)
5833 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005834 val += now;
5835 bytes -= now;
5836 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005837
Avi Kivityf78146b2012-04-18 19:22:47 +03005838 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5839 vcpu, ops);
5840 if (rc != X86EMUL_CONTINUE)
5841 return rc;
5842
5843 if (!vcpu->mmio_nr_fragments)
5844 return rc;
5845
5846 gpa = vcpu->mmio_fragments[0].gpa;
5847
5848 vcpu->mmio_needed = 1;
5849 vcpu->mmio_cur_fragment = 0;
5850
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005851 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005852 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5853 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5854 vcpu->run->mmio.phys_addr = gpa;
5855
5856 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005857}
5858
5859static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5860 unsigned long addr,
5861 void *val,
5862 unsigned int bytes,
5863 struct x86_exception *exception)
5864{
5865 return emulator_read_write(ctxt, addr, val, bytes,
5866 exception, &read_emultor);
5867}
5868
Xiubo Li52eb5a62015-03-13 17:39:45 +08005869static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005870 unsigned long addr,
5871 const void *val,
5872 unsigned int bytes,
5873 struct x86_exception *exception)
5874{
5875 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5876 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005877}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005878
Avi Kivitydaea3e72010-03-15 13:59:54 +02005879#define CMPXCHG_TYPE(t, ptr, old, new) \
5880 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5881
5882#ifdef CONFIG_X86_64
5883# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5884#else
5885# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005886 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005887#endif
5888
Avi Kivity0f65dd72011-04-20 13:37:53 +03005889static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5890 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005891 const void *old,
5892 const void *new,
5893 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005894 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005895{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005896 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005897 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005898 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005899 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005900 char *kaddr;
5901 bool exchanged;
5902
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005903 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005904 if (bytes > 8 || (bytes & (bytes - 1)))
5905 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005906
Avi Kivitydaea3e72010-03-15 13:59:54 +02005907 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005908
Avi Kivitydaea3e72010-03-15 13:59:54 +02005909 if (gpa == UNMAPPED_GVA ||
5910 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5911 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005912
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005913 /*
5914 * Emulate the atomic as a straight write to avoid #AC if SLD is
5915 * enabled in the host and the access splits a cache line.
5916 */
5917 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
5918 page_line_mask = ~(cache_line_size() - 1);
5919 else
5920 page_line_mask = PAGE_MASK;
5921
5922 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005923 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005924
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005925 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005926 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005927
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005928 kaddr = map.hva + offset_in_page(gpa);
5929
Avi Kivitydaea3e72010-03-15 13:59:54 +02005930 switch (bytes) {
5931 case 1:
5932 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5933 break;
5934 case 2:
5935 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5936 break;
5937 case 4:
5938 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5939 break;
5940 case 8:
5941 exchanged = CMPXCHG64(kaddr, old, new);
5942 break;
5943 default:
5944 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005945 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005946
5947 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005948
5949 if (!exchanged)
5950 return X86EMUL_CMPXCHG_FAILED;
5951
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005952 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005953
5954 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005955
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005956emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005957 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005958
Avi Kivity0f65dd72011-04-20 13:37:53 +03005959 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005960}
5961
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005962static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5963{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005964 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005965
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005966 for (i = 0; i < vcpu->arch.pio.count; i++) {
5967 if (vcpu->arch.pio.in)
5968 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5969 vcpu->arch.pio.size, pd);
5970 else
5971 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5972 vcpu->arch.pio.port, vcpu->arch.pio.size,
5973 pd);
5974 if (r)
5975 break;
5976 pd += vcpu->arch.pio.size;
5977 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005978 return r;
5979}
5980
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005981static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5982 unsigned short port, void *val,
5983 unsigned int count, bool in)
5984{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005985 vcpu->arch.pio.port = port;
5986 vcpu->arch.pio.in = in;
5987 vcpu->arch.pio.count = count;
5988 vcpu->arch.pio.size = size;
5989
5990 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5991 vcpu->arch.pio.count = 0;
5992 return 1;
5993 }
5994
5995 vcpu->run->exit_reason = KVM_EXIT_IO;
5996 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5997 vcpu->run->io.size = size;
5998 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5999 vcpu->run->io.count = count;
6000 vcpu->run->io.port = port;
6001
6002 return 0;
6003}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006004
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006005static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6006 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006007{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006008 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006009
Gleb Natapov79729952010-03-18 15:20:24 +02006010 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006011 goto data_avail;
6012
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006013 memset(vcpu->arch.pio_data, 0, size * count);
6014
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006015 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6016 if (ret) {
6017data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006018 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006019 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006020 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006021 return 1;
6022 }
6023
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006024 return 0;
6025}
6026
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006027static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6028 int size, unsigned short port, void *val,
6029 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006030{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006031 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006032
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006033}
6034
6035static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6036 unsigned short port, const void *val,
6037 unsigned int count)
6038{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006039 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006040 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006041 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006042}
6043
Carsten Ottebbd9b642007-10-30 18:44:21 +01006044static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6045 int size, unsigned short port,
6046 const void *val, unsigned int count)
6047{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006048 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006049}
6050
6051static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6052{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006053 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006054}
6055
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006056static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006057{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006058 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006059}
6060
Jiang Biaoae6a2372016-11-07 08:54:51 +08006061static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006062{
6063 if (!need_emulate_wbinvd(vcpu))
6064 return X86EMUL_CONTINUE;
6065
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006066 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006067 int cpu = get_cpu();
6068
6069 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006070 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6071 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006072 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006073 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006074 } else
6075 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006076 return X86EMUL_CONTINUE;
6077}
Joel Schopp5cb56052015-03-02 13:43:31 -06006078
6079int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6080{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006081 kvm_emulate_wbinvd_noskip(vcpu);
6082 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006083}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006084EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6085
Joel Schopp5cb56052015-03-02 13:43:31 -06006086
6087
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006088static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6089{
Joel Schopp5cb56052015-03-02 13:43:31 -06006090 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006091}
6092
Xiubo Li52eb5a62015-03-13 17:39:45 +08006093static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6094 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006095{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006096 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006097}
6098
Xiubo Li52eb5a62015-03-13 17:39:45 +08006099static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6100 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006101{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006102
Avi Kivity717746e2011-04-20 13:37:53 +03006103 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006104}
6105
Gleb Natapov52a46612010-03-18 15:20:03 +02006106static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6107{
6108 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6109}
6110
Avi Kivity717746e2011-04-20 13:37:53 +03006111static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006112{
Avi Kivity717746e2011-04-20 13:37:53 +03006113 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006114 unsigned long value;
6115
6116 switch (cr) {
6117 case 0:
6118 value = kvm_read_cr0(vcpu);
6119 break;
6120 case 2:
6121 value = vcpu->arch.cr2;
6122 break;
6123 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006124 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006125 break;
6126 case 4:
6127 value = kvm_read_cr4(vcpu);
6128 break;
6129 case 8:
6130 value = kvm_get_cr8(vcpu);
6131 break;
6132 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006133 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006134 return 0;
6135 }
6136
6137 return value;
6138}
6139
Avi Kivity717746e2011-04-20 13:37:53 +03006140static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006141{
Avi Kivity717746e2011-04-20 13:37:53 +03006142 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006143 int res = 0;
6144
Gleb Natapov52a46612010-03-18 15:20:03 +02006145 switch (cr) {
6146 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006147 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006148 break;
6149 case 2:
6150 vcpu->arch.cr2 = val;
6151 break;
6152 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006153 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006154 break;
6155 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006156 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006157 break;
6158 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006159 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006160 break;
6161 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006162 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006163 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006164 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006165
6166 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006167}
6168
Avi Kivity717746e2011-04-20 13:37:53 +03006169static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006170{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006171 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006172}
6173
Avi Kivity4bff1e862011-04-20 13:37:53 +03006174static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006175{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006176 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006177}
6178
Avi Kivity4bff1e862011-04-20 13:37:53 +03006179static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006180{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006181 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006182}
6183
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006184static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6185{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006186 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006187}
6188
6189static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6190{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006191 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006192}
6193
Avi Kivity4bff1e862011-04-20 13:37:53 +03006194static unsigned long emulator_get_cached_segment_base(
6195 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006196{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006197 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006198}
6199
Avi Kivity1aa36612011-04-27 13:20:30 +03006200static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6201 struct desc_struct *desc, u32 *base3,
6202 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006203{
6204 struct kvm_segment var;
6205
Avi Kivity4bff1e862011-04-20 13:37:53 +03006206 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006207 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006208
Gleb Natapov378a8b02013-01-21 15:36:48 +02006209 if (var.unusable) {
6210 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006211 if (base3)
6212 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006213 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006214 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006215
6216 if (var.g)
6217 var.limit >>= 12;
6218 set_desc_limit(desc, var.limit);
6219 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006220#ifdef CONFIG_X86_64
6221 if (base3)
6222 *base3 = var.base >> 32;
6223#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006224 desc->type = var.type;
6225 desc->s = var.s;
6226 desc->dpl = var.dpl;
6227 desc->p = var.present;
6228 desc->avl = var.avl;
6229 desc->l = var.l;
6230 desc->d = var.db;
6231 desc->g = var.g;
6232
6233 return true;
6234}
6235
Avi Kivity1aa36612011-04-27 13:20:30 +03006236static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6237 struct desc_struct *desc, u32 base3,
6238 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006239{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006240 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006241 struct kvm_segment var;
6242
Avi Kivity1aa36612011-04-27 13:20:30 +03006243 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006244 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006245#ifdef CONFIG_X86_64
6246 var.base |= ((u64)base3) << 32;
6247#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006248 var.limit = get_desc_limit(desc);
6249 if (desc->g)
6250 var.limit = (var.limit << 12) | 0xfff;
6251 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006252 var.dpl = desc->dpl;
6253 var.db = desc->d;
6254 var.s = desc->s;
6255 var.l = desc->l;
6256 var.g = desc->g;
6257 var.avl = desc->avl;
6258 var.present = desc->p;
6259 var.unusable = !var.present;
6260 var.padding = 0;
6261
6262 kvm_set_segment(vcpu, &var, seg);
6263 return;
6264}
6265
Avi Kivity717746e2011-04-20 13:37:53 +03006266static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6267 u32 msr_index, u64 *pdata)
6268{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006269 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006270}
6271
6272static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6273 u32 msr_index, u64 data)
6274{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006275 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006276}
6277
Paolo Bonzini64d60672015-05-07 11:36:11 +02006278static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6279{
6280 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6281
6282 return vcpu->arch.smbase;
6283}
6284
6285static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6286{
6287 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6288
6289 vcpu->arch.smbase = smbase;
6290}
6291
Nadav Amit67f4d422014-06-02 18:34:09 +03006292static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6293 u32 pmc)
6294{
Like Xu98ff80f2019-10-27 18:52:40 +08006295 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006296}
6297
Avi Kivity222d21a2011-11-10 14:57:30 +02006298static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6299 u32 pmc, u64 *pdata)
6300{
Wei Huangc6702c92015-06-19 13:44:45 +02006301 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006302}
6303
Avi Kivity6c3287f2011-04-20 15:43:05 +03006304static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6305{
6306 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6307}
6308
Avi Kivity29535382011-04-20 13:37:53 +03006309static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006310 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006311 enum x86_intercept_stage stage)
6312{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006313 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006314 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006315}
6316
Yu Zhange911eb32017-08-24 20:27:52 +08006317static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006318 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6319 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006320{
Sean Christophersonf91af512020-03-04 17:34:37 -08006321 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006322}
6323
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006324static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6325{
6326 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6327}
6328
6329static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6330{
6331 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6332}
6333
6334static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6335{
6336 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6337}
6338
Avi Kivitydd856ef2012-08-27 23:46:17 +03006339static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6340{
6341 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6342}
6343
6344static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6345{
6346 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6347}
6348
Nadav Amit801806d2015-01-26 09:32:23 +02006349static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6350{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006351 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006352}
6353
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006354static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6355{
6356 return emul_to_vcpu(ctxt)->arch.hflags;
6357}
6358
6359static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6360{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006361 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006362}
6363
Sean Christophersoned193212019-04-02 08:03:09 -07006364static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6365 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006366{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006367 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006368}
6369
Sean Christophersonc5833c72019-04-02 08:03:10 -07006370static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6371{
6372 kvm_smm_changed(emul_to_vcpu(ctxt));
6373}
6374
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006375static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6376{
6377 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6378}
6379
Mathias Krause0225fb52012-08-30 01:30:16 +02006380static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006381 .read_gpr = emulator_read_gpr,
6382 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006383 .read_std = emulator_read_std,
6384 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006385 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006386 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006387 .read_emulated = emulator_read_emulated,
6388 .write_emulated = emulator_write_emulated,
6389 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006390 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006391 .pio_in_emulated = emulator_pio_in_emulated,
6392 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006393 .get_segment = emulator_get_segment,
6394 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006395 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006396 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006397 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006398 .set_gdt = emulator_set_gdt,
6399 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006400 .get_cr = emulator_get_cr,
6401 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006402 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006403 .get_dr = emulator_get_dr,
6404 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006405 .get_smbase = emulator_get_smbase,
6406 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006407 .set_msr = emulator_set_msr,
6408 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006409 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006410 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006411 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006412 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006413 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006414 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006415 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006416 .guest_has_long_mode = emulator_guest_has_long_mode,
6417 .guest_has_movbe = emulator_guest_has_movbe,
6418 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006419 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006420 .get_hflags = emulator_get_hflags,
6421 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006422 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006423 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006424 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006425};
6426
Gleb Natapov95cb2292010-04-28 19:15:43 +03006427static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6428{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006429 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006430 /*
6431 * an sti; sti; sequence only disable interrupts for the first
6432 * instruction. So, if the last instruction, be it emulated or
6433 * not, left the system with the INT_STI flag enabled, it
6434 * means that the last instruction is an sti. We should not
6435 * leave the flag on in this case. The same goes for mov ss
6436 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006437 if (int_shadow & mask)
6438 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006439 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006440 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006441 if (!mask)
6442 kvm_make_request(KVM_REQ_EVENT, vcpu);
6443 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006444}
6445
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006446static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006447{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006448 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006449 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07006450 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006451
6452 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006453 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6454 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006455 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006456 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006457 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006458}
6459
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006460static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6461{
6462 struct x86_emulate_ctxt *ctxt;
6463
6464 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6465 if (!ctxt) {
6466 pr_err("kvm: failed to allocate vcpu's emulator\n");
6467 return NULL;
6468 }
6469
6470 ctxt->vcpu = vcpu;
6471 ctxt->ops = &emulate_ops;
6472 vcpu->arch.emulate_ctxt = ctxt;
6473
6474 return ctxt;
6475}
6476
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006477static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6478{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006479 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006480 int cs_db, cs_l;
6481
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006482 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006483
Sean Christopherson744e6992020-02-18 15:03:09 -08006484 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006485 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006486 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6487
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006488 ctxt->eip = kvm_rip_read(vcpu);
6489 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6490 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006491 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006492 cs_db ? X86EMUL_MODE_PROT32 :
6493 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006494 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006495 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6496 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006497
Avi Kivitydd856ef2012-08-27 23:46:17 +03006498 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006499 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006500}
6501
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006502void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006503{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006504 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006505 int ret;
6506
6507 init_emulate_ctxt(vcpu);
6508
Avi Kivity9dac77f2011-06-01 15:34:25 +03006509 ctxt->op_bytes = 2;
6510 ctxt->ad_bytes = 2;
6511 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006512 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006513
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006514 if (ret != X86EMUL_CONTINUE) {
6515 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6516 } else {
6517 ctxt->eip = ctxt->_eip;
6518 kvm_rip_write(vcpu, ctxt->eip);
6519 kvm_set_rflags(vcpu, ctxt->eflags);
6520 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006521}
6522EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6523
Liran Alone2366172018-03-12 13:12:49 +02006524static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006525{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006526 ++vcpu->stat.insn_emulation_fail;
6527 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006528
Sean Christopherson42cbf062019-08-27 14:40:31 -07006529 if (emulation_type & EMULTYPE_VMWARE_GP) {
6530 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006531 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006532 }
Liran Alone2366172018-03-12 13:12:49 +02006533
Sean Christopherson738fece2019-08-27 14:40:34 -07006534 if (emulation_type & EMULTYPE_SKIP) {
6535 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6536 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6537 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006538 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006539 }
6540
Sean Christopherson22da61c2019-08-27 14:40:28 -07006541 kvm_queue_exception(vcpu, UD_VECTOR);
6542
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006543 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006544 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6545 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6546 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006547 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006548 }
Liran Alone2366172018-03-12 13:12:49 +02006549
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006550 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006551}
6552
Sean Christopherson736c2912019-12-06 15:57:14 -08006553static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006554 bool write_fault_to_shadow_pgtable,
6555 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006556{
Sean Christopherson736c2912019-12-06 15:57:14 -08006557 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006558 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006559
Sean Christopherson92daa482020-02-18 15:03:08 -08006560 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006561 return false;
6562
Sean Christopherson92daa482020-02-18 15:03:08 -08006563 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6564 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006565 return false;
6566
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006567 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006568 /*
6569 * Write permission should be allowed since only
6570 * write access need to be emulated.
6571 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006572 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006573
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006574 /*
6575 * If the mapping is invalid in guest, let cpu retry
6576 * it to generate fault.
6577 */
6578 if (gpa == UNMAPPED_GVA)
6579 return true;
6580 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006581
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006582 /*
6583 * Do not retry the unhandleable instruction if it faults on the
6584 * readonly host memory, otherwise it will goto a infinite loop:
6585 * retry instruction -> write #PF -> emulation fail -> retry
6586 * instruction -> ...
6587 */
6588 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006589
6590 /*
6591 * If the instruction failed on the error pfn, it can not be fixed,
6592 * report the error to userspace.
6593 */
6594 if (is_error_noslot_pfn(pfn))
6595 return false;
6596
6597 kvm_release_pfn_clean(pfn);
6598
6599 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006600 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006601 unsigned int indirect_shadow_pages;
6602
6603 spin_lock(&vcpu->kvm->mmu_lock);
6604 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6605 spin_unlock(&vcpu->kvm->mmu_lock);
6606
6607 if (indirect_shadow_pages)
6608 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6609
Gleb Natapova6f177e2010-07-08 12:41:12 +03006610 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006611 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006612
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006613 /*
6614 * if emulation was due to access to shadowed page table
6615 * and it failed try to unshadow page and re-enter the
6616 * guest to let CPU execute the instruction.
6617 */
6618 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006619
6620 /*
6621 * If the access faults on its page table, it can not
6622 * be fixed by unprotecting shadow page and it should
6623 * be reported to userspace.
6624 */
6625 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006626}
6627
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006628static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006629 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006630{
6631 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006632 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006633
6634 last_retry_eip = vcpu->arch.last_retry_eip;
6635 last_retry_addr = vcpu->arch.last_retry_addr;
6636
6637 /*
6638 * If the emulation is caused by #PF and it is non-page_table
6639 * writing instruction, it means the VM-EXIT is caused by shadow
6640 * page protected, we can zap the shadow page and retry this
6641 * instruction directly.
6642 *
6643 * Note: if the guest uses a non-page-table modifying instruction
6644 * on the PDE that points to the instruction, then we will unmap
6645 * the instruction and go to an infinite loop. So, we cache the
6646 * last retried eip and the last fault address, if we meet the eip
6647 * and the address again, we can break out of the potential infinite
6648 * loop.
6649 */
6650 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6651
Sean Christopherson92daa482020-02-18 15:03:08 -08006652 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006653 return false;
6654
Sean Christopherson92daa482020-02-18 15:03:08 -08006655 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6656 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006657 return false;
6658
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006659 if (x86_page_table_writing_insn(ctxt))
6660 return false;
6661
Sean Christopherson736c2912019-12-06 15:57:14 -08006662 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006663 return false;
6664
6665 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006666 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006667
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006668 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006669 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006670
Xiao Guangrong22368022013-01-13 23:44:12 +08006671 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006672
6673 return true;
6674}
6675
Gleb Natapov716d51a2012-09-03 15:24:26 +03006676static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6677static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6678
Paolo Bonzini64d60672015-05-07 11:36:11 +02006679static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006680{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006681 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006682 /* This is a good place to trace that we are exiting SMM. */
6683 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6684
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006685 /* Process a latched INIT or SMI, if any. */
6686 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006687 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006688
6689 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006690}
6691
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006692static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6693 unsigned long *db)
6694{
6695 u32 dr6 = 0;
6696 int i;
6697 u32 enable, rwlen;
6698
6699 enable = dr7;
6700 rwlen = dr7 >> 16;
6701 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6702 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6703 dr6 |= (1 << i);
6704 return dr6;
6705}
6706
Sean Christopherson120c2c42019-08-27 14:40:29 -07006707static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006708{
6709 struct kvm_run *kvm_run = vcpu->run;
6710
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006711 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6712 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
Peter Xud5d260c2020-05-05 16:49:59 -04006713 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006714 kvm_run->debug.arch.exception = DB_VECTOR;
6715 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006716 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006717 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006718 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006719 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006720}
6721
Kyle Huey6affcbe2016-11-29 12:40:40 -08006722int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6723{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006724 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006725 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006726
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006727 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006728 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006729 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006730
6731 /*
6732 * rflags is the old, "raw" value of the flags. The new value has
6733 * not been saved yet.
6734 *
6735 * This is correct even for TF set by the guest, because "the
6736 * processor will not generate this exception after the instruction
6737 * that sets the TF flag".
6738 */
6739 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006740 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006741 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006742}
6743EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6744
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006745static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6746{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006747 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6748 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006749 struct kvm_run *kvm_run = vcpu->run;
6750 unsigned long eip = kvm_get_linear_rip(vcpu);
6751 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006752 vcpu->arch.guest_debug_dr7,
6753 vcpu->arch.eff_db);
6754
6755 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006756 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006757 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006758 kvm_run->debug.arch.exception = DB_VECTOR;
6759 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006760 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006761 return true;
6762 }
6763 }
6764
Nadav Amit4161a562014-07-17 01:19:31 +03006765 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6766 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006767 unsigned long eip = kvm_get_linear_rip(vcpu);
6768 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006769 vcpu->arch.dr7,
6770 vcpu->arch.db);
6771
6772 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04006773 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006774 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006775 return true;
6776 }
6777 }
6778
6779 return false;
6780}
6781
Liran Alon04789b62018-03-12 13:12:50 +02006782static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6783{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006784 switch (ctxt->opcode_len) {
6785 case 1:
6786 switch (ctxt->b) {
6787 case 0xe4: /* IN */
6788 case 0xe5:
6789 case 0xec:
6790 case 0xed:
6791 case 0xe6: /* OUT */
6792 case 0xe7:
6793 case 0xee:
6794 case 0xef:
6795 case 0x6c: /* INS */
6796 case 0x6d:
6797 case 0x6e: /* OUTS */
6798 case 0x6f:
6799 return true;
6800 }
6801 break;
6802 case 2:
6803 switch (ctxt->b) {
6804 case 0x33: /* RDPMC */
6805 return true;
6806 }
6807 break;
Liran Alon04789b62018-03-12 13:12:50 +02006808 }
6809
6810 return false;
6811}
6812
Sean Christopherson736c2912019-12-06 15:57:14 -08006813int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6814 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006815{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006816 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006817 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006818 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006819 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006820
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006821 vcpu->arch.l1tf_flush_l1d = true;
6822
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006823 /*
6824 * Clear write_fault_to_shadow_pgtable here to ensure it is
6825 * never reused.
6826 */
6827 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006828 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006829
Sheng Yang571008d2008-01-02 14:49:22 +08006830 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006831 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006832
6833 /*
6834 * We will reenter on the same instruction since
6835 * we do not set complete_userspace_io. This does not
6836 * handle watchpoints yet, those would be handled in
6837 * the emulate_ops.
6838 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006839 if (!(emulation_type & EMULTYPE_SKIP) &&
6840 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006841 return r;
6842
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006843 ctxt->interruptibility = 0;
6844 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006845 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006846 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006847
Borislav Petkovb51e9742013-09-22 16:44:52 +02006848 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006849
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006850 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006851
Avi Kivitye46479f2010-04-11 13:05:16 +03006852 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006853 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006854 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006855 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006856 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6857 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006858 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006859 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006860 if (reexecute_instruction(vcpu, cr2_or_gpa,
6861 write_fault_to_spt,
6862 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006863 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006864 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006865 /*
6866 * #UD should result in just EMULATION_FAILED, and trap-like
6867 * exception should not be encountered during decode.
6868 */
6869 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6870 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006871 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006872 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006873 }
Liran Alone2366172018-03-12 13:12:49 +02006874 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006875 }
6876 }
6877
Sean Christopherson42cbf062019-08-27 14:40:31 -07006878 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6879 !is_vmware_backdoor_opcode(ctxt)) {
6880 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006881 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006882 }
Liran Alon04789b62018-03-12 13:12:50 +02006883
Sean Christopherson1957aa62019-08-27 14:40:39 -07006884 /*
6885 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6886 * for kvm_skip_emulated_instruction(). The caller is responsible for
6887 * updating interruptibility state and injecting single-step #DBs.
6888 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006889 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006890 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006891 if (ctxt->eflags & X86_EFLAGS_RF)
6892 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006893 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006894 }
6895
Sean Christopherson736c2912019-12-06 15:57:14 -08006896 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006897 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006898
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006899 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006900 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006901 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6902 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006903 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006904 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006905
Gleb Natapov5cd21912010-03-18 15:20:26 +02006906restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006907 if (emulation_type & EMULTYPE_PF) {
6908 /* Save the faulting GPA (cr2) in the address field */
6909 ctxt->exception.address = cr2_or_gpa;
6910
6911 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6912 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006913 ctxt->gpa_available = true;
6914 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006915 }
6916 } else {
6917 /* Sanitize the address out of an abundance of paranoia. */
6918 ctxt->exception.address = 0;
6919 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006920
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006921 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006922
Joerg Roedel775fde82011-04-04 12:39:24 +02006923 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006924 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006925
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006926 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006927 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006928 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006929 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006930
Liran Alone2366172018-03-12 13:12:49 +02006931 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006932 }
6933
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006934 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006935 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006936 if (inject_emulated_exception(vcpu))
6937 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006938 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006939 if (!vcpu->arch.pio.in) {
6940 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006941 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006942 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006943 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006944 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6945 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006946 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006947 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006948 ++vcpu->stat.mmio_exits;
6949
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006950 if (!vcpu->mmio_is_write)
6951 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006952 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006953 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006954 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006955 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006956 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006957 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006958
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006959 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006960 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006961 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006962 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006963 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006964 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6965 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006966 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006967 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006968 if (kvm_x86_ops.update_emulated_instruction)
6969 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006970 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006971 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006972
6973 /*
6974 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6975 * do nothing, and it will be requested again as soon as
6976 * the shadow expires. But we still need to check here,
6977 * because POPF has no interrupt shadow.
6978 */
6979 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6980 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006981 } else
6982 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006983
Gleb Natapove85d28f2010-07-29 15:11:52 +03006984 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006985}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006986
6987int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6988{
6989 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6990}
6991EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6992
6993int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6994 void *insn, int insn_len)
6995{
6996 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6997}
6998EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006999
Sean Christopherson8764ed52019-04-29 07:04:15 -07007000static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7001{
7002 vcpu->arch.pio.count = 0;
7003 return 1;
7004}
7005
Sean Christopherson45def772019-03-11 20:01:05 -07007006static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7007{
7008 vcpu->arch.pio.count = 0;
7009
7010 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7011 return 1;
7012
7013 return kvm_skip_emulated_instruction(vcpu);
7014}
7015
Sean Christophersondca7f122018-03-08 08:57:27 -08007016static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7017 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007018{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007019 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007020 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7021
Sean Christopherson8764ed52019-04-29 07:04:15 -07007022 if (ret)
7023 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007024
Sean Christopherson8764ed52019-04-29 07:04:15 -07007025 /*
7026 * Workaround userspace that relies on old KVM behavior of %rip being
7027 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7028 */
7029 if (port == 0x7e &&
7030 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7031 vcpu->arch.complete_userspace_io =
7032 complete_fast_pio_out_port_0x7e;
7033 kvm_skip_emulated_instruction(vcpu);
7034 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007035 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7036 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7037 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007038 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007039}
Carsten Ottede7d7892007-10-30 18:44:25 +01007040
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007041static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7042{
7043 unsigned long val;
7044
7045 /* We should only ever be called with arch.pio.count equal to 1 */
7046 BUG_ON(vcpu->arch.pio.count != 1);
7047
Sean Christopherson45def772019-03-11 20:01:05 -07007048 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7049 vcpu->arch.pio.count = 0;
7050 return 1;
7051 }
7052
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007053 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007054 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007055
7056 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007057 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007058 * the copy and tracing
7059 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007060 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007061 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007062
Sean Christopherson45def772019-03-11 20:01:05 -07007063 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007064}
7065
Sean Christophersondca7f122018-03-08 08:57:27 -08007066static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7067 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007068{
7069 unsigned long val;
7070 int ret;
7071
7072 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007073 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007074
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007075 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007076 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007077 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007078 return ret;
7079 }
7080
Sean Christopherson45def772019-03-11 20:01:05 -07007081 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007082 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7083
7084 return 0;
7085}
Sean Christophersondca7f122018-03-08 08:57:27 -08007086
7087int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7088{
Sean Christopherson45def772019-03-11 20:01:05 -07007089 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007090
Sean Christophersondca7f122018-03-08 08:57:27 -08007091 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007092 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007093 else
Sean Christopherson45def772019-03-11 20:01:05 -07007094 ret = kvm_fast_pio_out(vcpu, size, port);
7095 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007096}
7097EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007098
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007099static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007100{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007101 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007102 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007103}
7104
7105static void tsc_khz_changed(void *data)
7106{
7107 struct cpufreq_freqs *freq = data;
7108 unsigned long khz = 0;
7109
7110 if (data)
7111 khz = freq->new;
7112 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7113 khz = cpufreq_quick_get(raw_smp_processor_id());
7114 if (!khz)
7115 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007116 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007117}
7118
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007119#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007120static void kvm_hyperv_tsc_notifier(void)
7121{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007122 struct kvm *kvm;
7123 struct kvm_vcpu *vcpu;
7124 int cpu;
7125
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007126 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007127 list_for_each_entry(kvm, &vm_list, vm_list)
7128 kvm_make_mclock_inprogress_request(kvm);
7129
7130 hyperv_stop_tsc_emulation();
7131
7132 /* TSC frequency always matches when on Hyper-V */
7133 for_each_present_cpu(cpu)
7134 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7135 kvm_max_guest_tsc_khz = tsc_khz;
7136
7137 list_for_each_entry(kvm, &vm_list, vm_list) {
7138 struct kvm_arch *ka = &kvm->arch;
7139
7140 spin_lock(&ka->pvclock_gtod_sync_lock);
7141
7142 pvclock_update_vm_gtod_copy(kvm);
7143
7144 kvm_for_each_vcpu(cpu, vcpu, kvm)
7145 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7146
7147 kvm_for_each_vcpu(cpu, vcpu, kvm)
7148 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7149
7150 spin_unlock(&ka->pvclock_gtod_sync_lock);
7151 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007152 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007153}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007154#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007155
Viresh Kumardf240142019-04-29 15:03:58 +05307156static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007157{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007158 struct kvm *kvm;
7159 struct kvm_vcpu *vcpu;
7160 int i, send_ipi = 0;
7161
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007162 /*
7163 * We allow guests to temporarily run on slowing clocks,
7164 * provided we notify them after, or to run on accelerating
7165 * clocks, provided we notify them before. Thus time never
7166 * goes backwards.
7167 *
7168 * However, we have a problem. We can't atomically update
7169 * the frequency of a given CPU from this function; it is
7170 * merely a notifier, which can be called from any CPU.
7171 * Changing the TSC frequency at arbitrary points in time
7172 * requires a recomputation of local variables related to
7173 * the TSC for each VCPU. We must flag these local variables
7174 * to be updated and be sure the update takes place with the
7175 * new frequency before any guests proceed.
7176 *
7177 * Unfortunately, the combination of hotplug CPU and frequency
7178 * change creates an intractable locking scenario; the order
7179 * of when these callouts happen is undefined with respect to
7180 * CPU hotplug, and they can race with each other. As such,
7181 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7182 * undefined; you can actually have a CPU frequency change take
7183 * place in between the computation of X and the setting of the
7184 * variable. To protect against this problem, all updates of
7185 * the per_cpu tsc_khz variable are done in an interrupt
7186 * protected IPI, and all callers wishing to update the value
7187 * must wait for a synchronous IPI to complete (which is trivial
7188 * if the caller is on the CPU already). This establishes the
7189 * necessary total order on variable updates.
7190 *
7191 * Note that because a guest time update may take place
7192 * anytime after the setting of the VCPU's request bit, the
7193 * correct TSC value must be set before the request. However,
7194 * to ensure the update actually makes it to any guest which
7195 * starts running in hardware virtualization between the set
7196 * and the acquisition of the spinlock, we must also ping the
7197 * CPU after setting the request bit.
7198 *
7199 */
7200
Viresh Kumardf240142019-04-29 15:03:58 +05307201 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007202
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007203 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007204 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007205 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307206 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007207 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007208 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007209 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007210 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007211 }
7212 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007213 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007214
7215 if (freq->old < freq->new && send_ipi) {
7216 /*
7217 * We upscale the frequency. Must make the guest
7218 * doesn't see old kvmclock values while running with
7219 * the new frequency, otherwise we risk the guest sees
7220 * time go backwards.
7221 *
7222 * In case we update the frequency for another cpu
7223 * (which might be in guest context) send an interrupt
7224 * to kick the cpu out of guest context. Next time
7225 * guest context is entered kvmclock will be updated,
7226 * so the guest will not see stale values.
7227 */
Viresh Kumardf240142019-04-29 15:03:58 +05307228 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007229 }
Viresh Kumardf240142019-04-29 15:03:58 +05307230}
7231
7232static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7233 void *data)
7234{
7235 struct cpufreq_freqs *freq = data;
7236 int cpu;
7237
7238 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7239 return 0;
7240 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7241 return 0;
7242
7243 for_each_cpu(cpu, freq->policy->cpus)
7244 __kvmclock_cpufreq_notifier(freq, cpu);
7245
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007246 return 0;
7247}
7248
7249static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007250 .notifier_call = kvmclock_cpufreq_notifier
7251};
7252
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007253static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007254{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007255 tsc_khz_changed(NULL);
7256 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007257}
7258
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007259static void kvm_timer_init(void)
7260{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007261 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307262
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007263 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007264#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007265 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007266 int cpu;
7267
Avi Kivity3e26f232010-12-16 12:16:34 +02007268 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007269 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007270 if (policy) {
7271 if (policy->cpuinfo.max_freq)
7272 max_tsc_khz = policy->cpuinfo.max_freq;
7273 cpufreq_cpu_put(policy);
7274 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007275 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007276#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007277 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7278 CPUFREQ_TRANSITION_NOTIFIER);
7279 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307280
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007281 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007282 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007283}
7284
Andi Kleendd60d212017-07-25 17:20:32 -07007285DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7286EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007287
Gleb Natapovf5132b02011-11-10 14:57:22 +02007288int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007289{
Alex,Shi086c9852011-10-20 15:34:01 +08007290 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007291}
7292
7293static int kvm_is_user_mode(void)
7294{
7295 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007296
Alex,Shi086c9852011-10-20 15:34:01 +08007297 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007298 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007299
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007300 return user_mode != 0;
7301}
7302
7303static unsigned long kvm_get_guest_ip(void)
7304{
7305 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007306
Alex,Shi086c9852011-10-20 15:34:01 +08007307 if (__this_cpu_read(current_vcpu))
7308 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007309
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007310 return ip;
7311}
7312
Luwei Kang8479e042019-02-18 19:26:07 -05007313static void kvm_handle_intel_pt_intr(void)
7314{
7315 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7316
7317 kvm_make_request(KVM_REQ_PMI, vcpu);
7318 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7319 (unsigned long *)&vcpu->arch.pmu.global_status);
7320}
7321
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007322static struct perf_guest_info_callbacks kvm_guest_cbs = {
7323 .is_in_guest = kvm_is_in_guest,
7324 .is_user_mode = kvm_is_user_mode,
7325 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007326 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007327};
7328
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007329#ifdef CONFIG_X86_64
7330static void pvclock_gtod_update_fn(struct work_struct *work)
7331{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007332 struct kvm *kvm;
7333
7334 struct kvm_vcpu *vcpu;
7335 int i;
7336
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007337 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007338 list_for_each_entry(kvm, &vm_list, vm_list)
7339 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007340 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007341 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007342 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007343}
7344
7345static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7346
7347/*
7348 * Notification about pvclock gtod data update.
7349 */
7350static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7351 void *priv)
7352{
7353 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7354 struct timekeeper *tk = priv;
7355
7356 update_pvclock_gtod(tk);
7357
7358 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007359 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007360 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007361 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007362 atomic_read(&kvm_guest_has_master_clock) != 0)
7363 queue_work(system_long_wq, &pvclock_gtod_work);
7364
7365 return 0;
7366}
7367
7368static struct notifier_block pvclock_gtod_notifier = {
7369 .notifier_call = pvclock_gtod_notify,
7370};
7371#endif
7372
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007373int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007374{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007375 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007376 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007377
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007378 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007379 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007380 r = -EEXIST;
7381 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007382 }
7383
7384 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007385 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007386 r = -EOPNOTSUPP;
7387 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007388 }
7389 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007390 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007391 r = -EOPNOTSUPP;
7392 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007393 }
7394
Marc Orrb666a4b2018-11-06 14:53:56 -08007395 /*
7396 * KVM explicitly assumes that the guest has an FPU and
7397 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7398 * vCPU's FPU state as a fxregs_state struct.
7399 */
7400 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7401 printk(KERN_ERR "kvm: inadequate fpu\n");
7402 r = -EOPNOTSUPP;
7403 goto out;
7404 }
7405
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007406 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007407 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007408 __alignof__(struct fpu), SLAB_ACCOUNT,
7409 NULL);
7410 if (!x86_fpu_cache) {
7411 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7412 goto out;
7413 }
7414
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007415 x86_emulator_cache = kvm_alloc_emulator_cache();
7416 if (!x86_emulator_cache) {
7417 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7418 goto out_free_x86_fpu_cache;
7419 }
7420
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007421 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7422 if (!shared_msrs) {
7423 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007424 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007425 }
7426
Avi Kivity97db56c2008-01-13 13:23:56 +02007427 r = kvm_mmu_module_init();
7428 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007429 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007430
Sheng Yang7b523452008-04-25 21:13:50 +08007431 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007432 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007433 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007434 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007435
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007436 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7437
Sean Christophersoncfc48182020-03-02 15:56:23 -08007438 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007439 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007440 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7441 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007442
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007443 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007444 if (pi_inject_timer == -1)
7445 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007446#ifdef CONFIG_X86_64
7447 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007448
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007449 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007450 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007451#endif
7452
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007453 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007454
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007455out_free_percpu:
7456 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007457out_free_x86_emulator_cache:
7458 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007459out_free_x86_fpu_cache:
7460 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007461out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007462 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007463}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007464
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007465void kvm_arch_exit(void)
7466{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007467#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007468 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007469 clear_hv_tscchange_cb();
7470#endif
David Matlackcef84c32016-12-16 14:30:36 -08007471 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007472 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7473
Jan Kiszka888d2562009-04-17 19:24:58 +02007474 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7475 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7476 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007477 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007478#ifdef CONFIG_X86_64
7479 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7480#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007481 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007482 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007483 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007484 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007485}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007486
Joel Schopp5cb56052015-03-02 13:43:31 -06007487int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007488{
7489 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007490 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007491 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007492 return 1;
7493 } else {
7494 vcpu->run->exit_reason = KVM_EXIT_HLT;
7495 return 0;
7496 }
7497}
Joel Schopp5cb56052015-03-02 13:43:31 -06007498EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7499
7500int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7501{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007502 int ret = kvm_skip_emulated_instruction(vcpu);
7503 /*
7504 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7505 * KVM_EXIT_DEBUG here.
7506 */
7507 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007508}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007509EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7510
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007511#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007512static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7513 unsigned long clock_type)
7514{
7515 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007516 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007517 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007518 int ret;
7519
7520 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7521 return -KVM_EOPNOTSUPP;
7522
7523 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7524 return -KVM_EOPNOTSUPP;
7525
7526 clock_pairing.sec = ts.tv_sec;
7527 clock_pairing.nsec = ts.tv_nsec;
7528 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7529 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007530 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007531
7532 ret = 0;
7533 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7534 sizeof(struct kvm_clock_pairing)))
7535 ret = -KVM_EFAULT;
7536
7537 return ret;
7538}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007539#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007540
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307541/*
7542 * kvm_pv_kick_cpu_op: Kick a vcpu.
7543 *
7544 * @apicid - apicid of vcpu to be kicked.
7545 */
7546static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7547{
Raghavendra K T24d21662013-08-26 14:18:35 +05307548 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307549
Peter Xu150a84f2019-12-04 20:07:21 +01007550 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007551 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007552 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307553 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007554 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307555
Raghavendra K T24d21662013-08-26 14:18:35 +05307556 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007557 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307558}
7559
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007560bool kvm_apicv_activated(struct kvm *kvm)
7561{
7562 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7563}
7564EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7565
7566void kvm_apicv_init(struct kvm *kvm, bool enable)
7567{
7568 if (enable)
7569 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7570 &kvm->arch.apicv_inhibit_reasons);
7571 else
7572 set_bit(APICV_INHIBIT_REASON_DISABLE,
7573 &kvm->arch.apicv_inhibit_reasons);
7574}
7575EXPORT_SYMBOL_GPL(kvm_apicv_init);
7576
Wanpeng Li71506292019-06-11 20:23:49 +08007577static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7578{
7579 struct kvm_vcpu *target = NULL;
7580 struct kvm_apic_map *map;
7581
7582 rcu_read_lock();
7583 map = rcu_dereference(kvm->arch.apic_map);
7584
7585 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7586 target = map->phys_map[dest_id]->vcpu;
7587
7588 rcu_read_unlock();
7589
Wanpeng Li266e85a2019-07-24 17:43:13 +08007590 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007591 kvm_vcpu_yield_to(target);
7592}
7593
Hollis Blanchard8776e512007-10-31 17:24:24 -05007594int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7595{
7596 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007597 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007598
Radim Krčmář696ca772018-05-24 17:50:56 +02007599 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7600 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007601
Sean Christophersonde3cd112019-04-30 10:36:17 -07007602 nr = kvm_rax_read(vcpu);
7603 a0 = kvm_rbx_read(vcpu);
7604 a1 = kvm_rcx_read(vcpu);
7605 a2 = kvm_rdx_read(vcpu);
7606 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007607
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007608 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007609
Nadav Amita449c7a2014-06-18 17:19:24 +03007610 op_64_bit = is_64_bit_mode(vcpu);
7611 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007612 nr &= 0xFFFFFFFF;
7613 a0 &= 0xFFFFFFFF;
7614 a1 &= 0xFFFFFFFF;
7615 a2 &= 0xFFFFFFFF;
7616 a3 &= 0xFFFFFFFF;
7617 }
7618
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007619 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02007620 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007621 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007622 }
7623
Hollis Blanchard8776e512007-10-31 17:24:24 -05007624 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007625 case KVM_HC_VAPIC_POLL_IRQ:
7626 ret = 0;
7627 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307628 case KVM_HC_KICK_CPU:
7629 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007630 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307631 ret = 0;
7632 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007633#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007634 case KVM_HC_CLOCK_PAIRING:
7635 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7636 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007637#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007638 case KVM_HC_SEND_IPI:
7639 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7640 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007641 case KVM_HC_SCHED_YIELD:
7642 kvm_sched_yield(vcpu->kvm, a0);
7643 ret = 0;
7644 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007645 default:
7646 ret = -KVM_ENOSYS;
7647 break;
7648 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007649out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007650 if (!op_64_bit)
7651 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007652 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007653
Amit Shahf11c3a82008-02-21 01:00:30 +05307654 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007655 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007656}
7657EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7658
Jan Kiszkab6785de2012-09-20 07:43:17 +02007659static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007660{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007661 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007662 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007663 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007664
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007665 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007666
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007667 return emulator_write_emulated(ctxt, rip, instruction, 3,
7668 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007669}
7670
Avi Kivity851ba692009-08-24 11:10:17 +03007671static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007672{
Matt Gingell782d4222015-11-16 15:26:00 -08007673 return vcpu->run->request_interrupt_window &&
7674 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007675}
7676
Avi Kivity851ba692009-08-24 11:10:17 +03007677static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007678{
Avi Kivity851ba692009-08-24 11:10:17 +03007679 struct kvm_run *kvm_run = vcpu->run;
7680
Jan Kiszka91586a32009-10-05 13:07:21 +02007681 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007682 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007683 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007684 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007685 kvm_run->ready_for_interrupt_injection =
7686 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007687 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007688}
7689
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007690static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7691{
7692 int max_irr, tpr;
7693
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007694 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007695 return;
7696
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007697 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007698 return;
7699
Andrey Smetanind62caab2015-11-10 15:36:33 +03007700 if (vcpu->arch.apicv_active)
7701 return;
7702
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007703 if (!vcpu->arch.apic->vapic_addr)
7704 max_irr = kvm_lapic_find_highest_irr(vcpu);
7705 else
7706 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007707
7708 if (max_irr != -1)
7709 max_irr >>= 4;
7710
7711 tpr = kvm_lapic_get_cr8(vcpu);
7712
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007713 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007714}
7715
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007716static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007717{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007718 int r;
7719
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007720 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007721
Liran Alon1a680e32018-03-23 03:01:33 +03007722 if (vcpu->arch.exception.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007723 kvm_x86_ops.queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007724 /*
Liran Alona042c262018-03-23 03:01:32 +03007725 * Do not inject an NMI or interrupt if there is a pending
7726 * exception. Exceptions and interrupts are recognized at
7727 * instruction boundaries, i.e. the start of an instruction.
7728 * Trap-like exceptions, e.g. #DB, have higher priority than
7729 * NMIs and interrupts, i.e. traps are recognized before an
7730 * NMI/interrupt that's pending on the same instruction.
7731 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7732 * priority, but are only generated (pended) during instruction
7733 * execution, i.e. a pending fault-like exception means the
7734 * fault occurred on the *previous* instruction and must be
7735 * serviced prior to recognizing any new events in order to
7736 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007737 */
Liran Alon1a680e32018-03-23 03:01:33 +03007738 else if (!vcpu->arch.exception.pending) {
7739 if (vcpu->arch.nmi_injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007740 kvm_x86_ops.set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007741 else if (vcpu->arch.interrupt.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007742 kvm_x86_ops.set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007743 }
7744
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07007745 WARN_ON_ONCE(vcpu->arch.exception.injected &&
7746 vcpu->arch.exception.pending);
7747
Liran Alon1a680e32018-03-23 03:01:33 +03007748 /*
7749 * Call check_nested_events() even if we reinjected a previous event
7750 * in order for caller to determine if it should require immediate-exit
7751 * from L2 to L1 due to pending L1 events which require exit
7752 * from L2 to L1.
7753 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04007754 if (is_guest_mode(vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04007755 r = kvm_x86_ops.nested_ops->check_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007756 if (r != 0)
7757 return r;
7758 }
7759
7760 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007761 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007762 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7763 vcpu->arch.exception.has_error_code,
7764 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007765
Wanpeng Li664f8e22017-08-24 03:35:09 -07007766 vcpu->arch.exception.pending = false;
7767 vcpu->arch.exception.injected = true;
7768
Nadav Amitd6e8c852014-07-24 14:51:24 +03007769 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7770 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7771 X86_EFLAGS_RF);
7772
Jim Mattsonf10c7292018-10-16 14:29:23 -07007773 if (vcpu->arch.exception.nr == DB_VECTOR) {
7774 /*
7775 * This code assumes that nSVM doesn't use
7776 * check_nested_events(). If it does, the
7777 * DR6/DR7 changes should happen before L1
7778 * gets a #VMEXIT for an intercepted #DB in
7779 * L2. (Under VMX, on the other hand, the
7780 * DR6/DR7 changes should not happen in the
7781 * event of a VM-exit to L1 for an intercepted
7782 * #DB in L2.)
7783 */
7784 kvm_deliver_exception_payload(vcpu);
7785 if (vcpu->arch.dr7 & DR7_GD) {
7786 vcpu->arch.dr7 &= ~DR7_GD;
7787 kvm_update_dr7(vcpu);
7788 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007789 }
7790
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007791 kvm_x86_ops.queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007792 }
7793
7794 /* Don't consider new event if we re-injected an event */
7795 if (kvm_event_needs_reinjection(vcpu))
7796 return 0;
7797
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007798 if (vcpu->arch.smi_pending &&
7799 kvm_x86_ops.smi_allowed(vcpu, true)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007800 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007801 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007802 enter_smm(vcpu);
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007803 } else if (vcpu->arch.nmi_pending &&
7804 kvm_x86_ops.nmi_allowed(vcpu, true)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007805 --vcpu->arch.nmi_pending;
7806 vcpu->arch.nmi_injected = true;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007807 kvm_x86_ops.set_nmi(vcpu);
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007808 } else if (kvm_cpu_has_injectable_intr(vcpu) &&
7809 kvm_x86_ops.interrupt_allowed(vcpu, true)) {
7810 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
7811 kvm_x86_ops.set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007812 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007813
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007814 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007815}
7816
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007817static void process_nmi(struct kvm_vcpu *vcpu)
7818{
7819 unsigned limit = 2;
7820
7821 /*
7822 * x86 is limited to one NMI running, and one NMI pending after it.
7823 * If an NMI is already in progress, limit further NMIs to just one.
7824 * Otherwise, allow two (and we'll inject the first one immediately).
7825 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007826 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007827 limit = 1;
7828
7829 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7830 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7831 kvm_make_request(KVM_REQ_EVENT, vcpu);
7832}
7833
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007834static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007835{
7836 u32 flags = 0;
7837 flags |= seg->g << 23;
7838 flags |= seg->db << 22;
7839 flags |= seg->l << 21;
7840 flags |= seg->avl << 20;
7841 flags |= seg->present << 15;
7842 flags |= seg->dpl << 13;
7843 flags |= seg->s << 12;
7844 flags |= seg->type << 8;
7845 return flags;
7846}
7847
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007848static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007849{
7850 struct kvm_segment seg;
7851 int offset;
7852
7853 kvm_get_segment(vcpu, &seg, n);
7854 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7855
7856 if (n < 3)
7857 offset = 0x7f84 + n * 12;
7858 else
7859 offset = 0x7f2c + (n - 3) * 12;
7860
7861 put_smstate(u32, buf, offset + 8, seg.base);
7862 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007863 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007864}
7865
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007866#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007867static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007868{
7869 struct kvm_segment seg;
7870 int offset;
7871 u16 flags;
7872
7873 kvm_get_segment(vcpu, &seg, n);
7874 offset = 0x7e00 + n * 16;
7875
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007876 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007877 put_smstate(u16, buf, offset, seg.selector);
7878 put_smstate(u16, buf, offset + 2, flags);
7879 put_smstate(u32, buf, offset + 4, seg.limit);
7880 put_smstate(u64, buf, offset + 8, seg.base);
7881}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007882#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007883
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007884static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007885{
7886 struct desc_ptr dt;
7887 struct kvm_segment seg;
7888 unsigned long val;
7889 int i;
7890
7891 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7892 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7893 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7894 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7895
7896 for (i = 0; i < 8; i++)
7897 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7898
7899 kvm_get_dr(vcpu, 6, &val);
7900 put_smstate(u32, buf, 0x7fcc, (u32)val);
7901 kvm_get_dr(vcpu, 7, &val);
7902 put_smstate(u32, buf, 0x7fc8, (u32)val);
7903
7904 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7905 put_smstate(u32, buf, 0x7fc4, seg.selector);
7906 put_smstate(u32, buf, 0x7f64, seg.base);
7907 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007908 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007909
7910 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7911 put_smstate(u32, buf, 0x7fc0, seg.selector);
7912 put_smstate(u32, buf, 0x7f80, seg.base);
7913 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007914 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007915
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007916 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007917 put_smstate(u32, buf, 0x7f74, dt.address);
7918 put_smstate(u32, buf, 0x7f70, dt.size);
7919
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007920 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007921 put_smstate(u32, buf, 0x7f58, dt.address);
7922 put_smstate(u32, buf, 0x7f54, dt.size);
7923
7924 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007925 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007926
7927 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7928
7929 /* revision id */
7930 put_smstate(u32, buf, 0x7efc, 0x00020000);
7931 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7932}
7933
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007934#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007935static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007936{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007937 struct desc_ptr dt;
7938 struct kvm_segment seg;
7939 unsigned long val;
7940 int i;
7941
7942 for (i = 0; i < 16; i++)
7943 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7944
7945 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7946 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7947
7948 kvm_get_dr(vcpu, 6, &val);
7949 put_smstate(u64, buf, 0x7f68, val);
7950 kvm_get_dr(vcpu, 7, &val);
7951 put_smstate(u64, buf, 0x7f60, val);
7952
7953 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7954 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7955 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7956
7957 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7958
7959 /* revision id */
7960 put_smstate(u32, buf, 0x7efc, 0x00020064);
7961
7962 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7963
7964 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7965 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007966 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007967 put_smstate(u32, buf, 0x7e94, seg.limit);
7968 put_smstate(u64, buf, 0x7e98, seg.base);
7969
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007970 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007971 put_smstate(u32, buf, 0x7e84, dt.size);
7972 put_smstate(u64, buf, 0x7e88, dt.address);
7973
7974 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7975 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007976 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007977 put_smstate(u32, buf, 0x7e74, seg.limit);
7978 put_smstate(u64, buf, 0x7e78, seg.base);
7979
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007980 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007981 put_smstate(u32, buf, 0x7e64, dt.size);
7982 put_smstate(u64, buf, 0x7e68, dt.address);
7983
7984 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007985 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007986}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007987#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007988
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007989static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007990{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007991 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007992 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007993 char buf[512];
7994 u32 cr0;
7995
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007996 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007997 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007998#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007999 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008000 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008001 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008002#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008003 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008004
Ladi Prosek0234bf82017-10-11 16:54:40 +02008005 /*
8006 * Give pre_enter_smm() a chance to make ISA-specific changes to the
8007 * vCPU state (e.g. leave guest mode) after we've saved the state into
8008 * the SMM state-save area.
8009 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008010 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02008011
8012 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008013 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008014
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008015 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008016 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
8017 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008018 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008019
8020 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
8021 kvm_rip_write(vcpu, 0x8000);
8022
8023 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008024 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008025 vcpu->arch.cr0 = cr0;
8026
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008027 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008028
Paolo Bonzini18c36262015-08-07 12:27:54 +02008029 /* Undocumented: IDT limit is set to zero on entry to SMM. */
8030 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008031 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02008032
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008033 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
8034
8035 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8036 cs.base = vcpu->arch.smbase;
8037
8038 ds.selector = 0;
8039 ds.base = 0;
8040
8041 cs.limit = ds.limit = 0xffffffff;
8042 cs.type = ds.type = 0x3;
8043 cs.dpl = ds.dpl = 0;
8044 cs.db = ds.db = 0;
8045 cs.s = ds.s = 1;
8046 cs.l = ds.l = 0;
8047 cs.g = ds.g = 1;
8048 cs.avl = ds.avl = 0;
8049 cs.present = ds.present = 1;
8050 cs.unusable = ds.unusable = 0;
8051 cs.padding = ds.padding = 0;
8052
8053 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8054 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8055 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8056 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8057 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8058 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8059
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008060#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008061 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008062 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008063#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008064
8065 kvm_update_cpuid(vcpu);
8066 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008067}
8068
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008069static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008070{
8071 vcpu->arch.smi_pending = true;
8072 kvm_make_request(KVM_REQ_EVENT, vcpu);
8073}
8074
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008075void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8076 unsigned long *vcpu_bitmap)
8077{
8078 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008079
8080 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8081
Mao Wenandb5a95e2019-11-19 11:06:40 +08008082 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008083 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008084
8085 free_cpumask_var(cpus);
8086}
8087
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008088void kvm_make_scan_ioapic_request(struct kvm *kvm)
8089{
8090 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8091}
8092
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008093void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8094{
8095 if (!lapic_in_kernel(vcpu))
8096 return;
8097
8098 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8099 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008100 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008101}
8102EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8103
8104/*
8105 * NOTE: Do not hold any lock prior to calling this.
8106 *
8107 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8108 * locked, because it calls __x86_set_memory_region() which does
8109 * synchronize_srcu(&kvm->srcu).
8110 */
8111void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8112{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008113 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008114 unsigned long old, new, expected;
8115
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008116 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8117 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008118 return;
8119
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008120 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8121 do {
8122 expected = new = old;
8123 if (activate)
8124 __clear_bit(bit, &new);
8125 else
8126 __set_bit(bit, &new);
8127 if (new == old)
8128 break;
8129 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8130 } while (old != expected);
8131
8132 if (!!old == !!new)
8133 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008134
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008135 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008136 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8137 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008138
8139 /*
8140 * Sending request to update APICV for all other vcpus,
8141 * while update the calling vcpu immediately instead of
8142 * waiting for another #VMEXIT to handle the request.
8143 */
8144 except = kvm_get_running_vcpu();
8145 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
8146 except);
8147 if (except)
8148 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008149}
8150EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8151
Yang Zhang3d81bc72013-04-11 19:25:13 +08008152static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008153{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008154 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008155 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008156
Andrey Smetanin63086302015-11-10 15:36:32 +03008157 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008158
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008159 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008160 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008161 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008162 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008163 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008164 if (ioapic_in_kernel(vcpu->kvm))
8165 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008166 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008167
8168 if (is_guest_mode(vcpu))
8169 vcpu->arch.load_eoi_exitmap_pending = true;
8170 else
8171 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8172}
8173
8174static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8175{
8176 u64 eoi_exit_bitmap[4];
8177
8178 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8179 return;
8180
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008181 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8182 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008183 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008184}
8185
Michal Hocko93065ac2018-08-21 21:52:33 -07008186int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8187 unsigned long start, unsigned long end,
8188 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008189{
8190 unsigned long apic_address;
8191
8192 /*
8193 * The physical address of apic access page is stored in the VMCS.
8194 * Update it when it becomes invalid.
8195 */
8196 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8197 if (start <= apic_address && apic_address < end)
8198 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008199
8200 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008201}
8202
Tang Chen4256f432014-09-24 15:57:54 +08008203void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8204{
Paolo Bonzini35754c92015-07-29 12:05:37 +02008205 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008206 return;
8207
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008208 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008209 return;
8210
Sean Christophersona4148b72020-03-20 14:28:24 -07008211 kvm_x86_ops.set_apic_access_page_addr(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008212}
Tang Chen4256f432014-09-24 15:57:54 +08008213
Sean Christophersond264ee02018-08-27 15:21:12 -07008214void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8215{
8216 smp_send_reschedule(vcpu->cpu);
8217}
8218EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8219
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008220/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008221 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008222 * exiting to the userspace. Otherwise, the value will be returned to the
8223 * userspace.
8224 */
Avi Kivity851ba692009-08-24 11:10:17 +03008225static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008226{
8227 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008228 bool req_int_win =
8229 dm_request_for_irq_injection(vcpu) &&
8230 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08008231 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08008232
Jan Kiszka730dca42013-04-28 10:50:52 +02008233 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008234
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008235 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008236 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04008237 if (unlikely(!kvm_x86_ops.nested_ops->get_vmcs12_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008238 r = 0;
8239 goto out;
8240 }
8241 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008242 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008243 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008244 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008245 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008246 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8247 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008248 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8249 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008250 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8251 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008252 if (unlikely(r))
8253 goto out;
8254 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008255 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008256 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008257 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8258 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008259 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07008260 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008261
8262 /* Flushing all ASIDs flushes the current ASID... */
8263 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
8264 }
8265 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
8266 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04008267 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
8268 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008269
Avi Kivitya8eeb042010-05-10 12:34:53 +03008270 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008271 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008272 r = 0;
8273 goto out;
8274 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008275 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008276 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008277 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008278 r = 0;
8279 goto out;
8280 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008281 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8282 /* Page is swapped out. Do synthetic halt */
8283 vcpu->arch.apf.halted = true;
8284 r = 1;
8285 goto out;
8286 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008287 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8288 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008289 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8290 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008291 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8292 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008293 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008294 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008295 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008296 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008297 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8298 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8299 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008300 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008301 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8302 vcpu->run->eoi.vector =
8303 vcpu->arch.pending_ioapic_eoi;
8304 r = 0;
8305 goto out;
8306 }
8307 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008308 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8309 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008310 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8311 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008312 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8313 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008314 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8315 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8316 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8317 r = 0;
8318 goto out;
8319 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008320 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8321 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8322 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8323 r = 0;
8324 goto out;
8325 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008326 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8327 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8328 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8329 r = 0;
8330 goto out;
8331 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008332
8333 /*
8334 * KVM_REQ_HV_STIMER has to be processed after
8335 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8336 * depend on the guest clock being up-to-date
8337 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008338 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8339 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008340 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8341 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008342 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008343
Avi Kivityb463a6f2010-07-20 15:06:17 +03008344 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008345 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008346 kvm_apic_accept_events(vcpu);
8347 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8348 r = 1;
8349 goto out;
8350 }
8351
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008352 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008353 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008354 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008355 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008356 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008357 * SMIs have three cases:
8358 * 1) They can be nested, and then there is nothing to
8359 * do here because RSM will cause a vmexit anyway.
8360 * 2) There is an ISA-specific reason why SMI cannot be
8361 * injected, and the moment when this changes can be
8362 * intercepted.
8363 * 3) Or the SMI can be pending because
8364 * inject_pending_event has completed the injection
8365 * of an IRQ or NMI from the previous vmexit, and
8366 * then we request an immediate exit to inject the
8367 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008368 */
8369 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008370 if (!kvm_x86_ops.enable_smi_window(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008371 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008372 if (vcpu->arch.nmi_pending)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008373 kvm_x86_ops.enable_nmi_window(vcpu);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008374 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008375 kvm_x86_ops.enable_irq_window(vcpu);
Sean Christophersond2060bd2020-04-22 19:25:39 -07008376 if (is_guest_mode(vcpu) &&
8377 kvm_x86_ops.nested_ops->hv_timer_pending &&
8378 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8379 req_immediate_exit = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008380 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008381 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008382
Avi Kivity3842d132010-07-27 12:30:24 +03008383 if (kvm_lapic_enabled(vcpu)) {
8384 update_cr8_intercept(vcpu);
8385 kvm_lapic_sync_to_vapic(vcpu);
8386 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008387 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008388
Avi Kivityd8368af2012-05-14 18:07:56 +03008389 r = kvm_mmu_reload(vcpu);
8390 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008391 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008392 }
8393
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008394 preempt_disable();
8395
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008396 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008397
8398 /*
8399 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8400 * IPI are then delayed after guest entry, which ensures that they
8401 * result in virtual interrupt delivery.
8402 */
8403 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008404 vcpu->mode = IN_GUEST_MODE;
8405
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008406 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8407
Lan Tianyu0f127d12016-03-13 11:10:29 +08008408 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008409 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008410 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008411 *
Luwei Kang81b01662019-01-31 16:52:02 +08008412 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008413 * pairs with the memory barrier implicit in pi_test_and_set_on
8414 * (see vmx_deliver_posted_interrupt).
8415 *
8416 * 3) This also orders the write to mode from any reads to the page
8417 * tables done while the VCPU is running. Please see the comment
8418 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008419 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008420 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008421
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008422 /*
8423 * This handles the case where a posted interrupt was
8424 * notified with kvm_vcpu_kick.
8425 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008426 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008427 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008428
Wanpeng Li5a9f5442020-04-28 14:23:26 +08008429 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008430 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008431 smp_wmb();
8432 local_irq_enable();
8433 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008434 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008435 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008436 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008437 }
8438
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008439 if (req_immediate_exit) {
8440 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008441 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008442 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008443
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008444 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008445 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008446
Sean Christopherson2620fe22020-01-17 11:30:51 -08008447 fpregs_assert_state_consistent();
8448 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8449 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008450
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008451 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008452 set_debugreg(0, 7);
8453 set_debugreg(vcpu->arch.eff_db[0], 0);
8454 set_debugreg(vcpu->arch.eff_db[1], 1);
8455 set_debugreg(vcpu->arch.eff_db[2], 2);
8456 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008457 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008458 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008459 }
8460
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008461 exit_fastpath = kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008462
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008463 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008464 * Do this here before restoring debug registers on the host. And
8465 * since we do this before handling the vmexit, a DR access vmexit
8466 * can (a) read the correct value of the debug registers, (b) set
8467 * KVM_DEBUGREG_WONT_EXIT again.
8468 */
8469 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008470 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008471 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008472 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008473 kvm_update_dr7(vcpu);
8474 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008475 }
8476
8477 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008478 * If the guest has used debug registers, at least dr7
8479 * will be disabled while returning to the host.
8480 * If we don't have active breakpoints in the host, we don't
8481 * care about the messed up debug address registers. But if
8482 * we have some of them active, restore the old state.
8483 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008484 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008485 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008486
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008487 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008488
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008489 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008490 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008491
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008492 kvm_x86_ops.handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008493
Sean Christophersond7a08882019-07-10 09:07:34 -07008494 /*
8495 * Consume any pending interrupts, including the possible source of
8496 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8497 * An instruction is required after local_irq_enable() to fully unblock
8498 * interrupts on processors that implement an interrupt shadow, the
8499 * stat.exits increment will do nicely.
8500 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008501 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008502 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008503 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008504 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008505 kvm_after_interrupt(vcpu);
8506
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008507 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008508 if (lapic_in_kernel(vcpu)) {
8509 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8510 if (delta != S64_MIN) {
8511 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8512 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8513 }
8514 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008515
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008516 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008517 preempt_enable();
8518
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008519 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008520
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008521 /*
8522 * Profile KVM exit RIPs:
8523 */
8524 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008525 unsigned long rip = kvm_rip_read(vcpu);
8526 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008527 }
8528
Zachary Amsdencc578282012-02-03 15:43:50 -02008529 if (unlikely(vcpu->arch.tsc_always_catchup))
8530 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008531
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008532 if (vcpu->arch.apic_attention)
8533 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008534
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008535 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008536 return r;
8537
8538cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07008539 if (req_immediate_exit)
8540 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008541 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008542 if (unlikely(vcpu->arch.apic_attention))
8543 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008544out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008545 return r;
8546}
8547
Paolo Bonzini362c6982015-02-06 12:48:04 +01008548static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8549{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008550 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008551 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008552 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8553 kvm_vcpu_block(vcpu);
8554 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008555
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008556 if (kvm_x86_ops.post_block)
8557 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008558
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008559 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8560 return 1;
8561 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008562
Paolo Bonzini362c6982015-02-06 12:48:04 +01008563 kvm_apic_accept_events(vcpu);
8564 switch(vcpu->arch.mp_state) {
8565 case KVM_MP_STATE_HALTED:
8566 vcpu->arch.pv.pv_unhalted = false;
8567 vcpu->arch.mp_state =
8568 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008569 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008570 case KVM_MP_STATE_RUNNABLE:
8571 vcpu->arch.apf.halted = false;
8572 break;
8573 case KVM_MP_STATE_INIT_RECEIVED:
8574 break;
8575 default:
8576 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008577 }
8578 return 1;
8579}
8580
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008581static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8582{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008583 if (is_guest_mode(vcpu))
Paolo Bonzini33b22172020-04-17 10:24:18 -04008584 kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008585
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008586 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8587 !vcpu->arch.apf.halted);
8588}
8589
Paolo Bonzini362c6982015-02-06 12:48:04 +01008590static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008591{
8592 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008593 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008594
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008595 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008596 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008597
Paolo Bonzini362c6982015-02-06 12:48:04 +01008598 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008599 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008600 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008601 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008602 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008603 }
8604
Gleb Natapov09cec752009-03-23 15:11:44 +02008605 if (r <= 0)
8606 break;
8607
Radim Krčmář72875d82017-04-26 22:32:19 +02008608 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008609 if (kvm_cpu_has_pending_timer(vcpu))
8610 kvm_inject_pending_timer_irqs(vcpu);
8611
Matt Gingell782d4222015-11-16 15:26:00 -08008612 if (dm_request_for_irq_injection(vcpu) &&
8613 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008614 r = 0;
8615 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008616 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008617 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008618 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008619
8620 kvm_check_async_pf_completion(vcpu);
8621
Gleb Natapov09cec752009-03-23 15:11:44 +02008622 if (signal_pending(current)) {
8623 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008624 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008625 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008626 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008627 }
8628 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008629 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008630 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008631 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008632 }
8633 }
8634
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008635 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008636
8637 return r;
8638}
8639
Gleb Natapov716d51a2012-09-03 15:24:26 +03008640static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8641{
8642 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008643
Gleb Natapov716d51a2012-09-03 15:24:26 +03008644 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008645 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008646 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008647 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008648}
8649
8650static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8651{
8652 BUG_ON(!vcpu->arch.pio.count);
8653
8654 return complete_emulated_io(vcpu);
8655}
8656
Avi Kivityf78146b2012-04-18 19:22:47 +03008657/*
8658 * Implements the following, as a state machine:
8659 *
8660 * read:
8661 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008662 * for each mmio piece in the fragment
8663 * write gpa, len
8664 * exit
8665 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008666 * execute insn
8667 *
8668 * write:
8669 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008670 * for each mmio piece in the fragment
8671 * write gpa, len
8672 * copy data
8673 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008674 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008675static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008676{
8677 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008678 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008679 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008680
Gleb Natapov716d51a2012-09-03 15:24:26 +03008681 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008682
Gleb Natapov716d51a2012-09-03 15:24:26 +03008683 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008684 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8685 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008686 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008687 memcpy(frag->data, run->mmio.data, len);
8688
8689 if (frag->len <= 8) {
8690 /* Switch to the next fragment. */
8691 frag++;
8692 vcpu->mmio_cur_fragment++;
8693 } else {
8694 /* Go forward to the next mmio piece. */
8695 frag->data += len;
8696 frag->gpa += len;
8697 frag->len -= len;
8698 }
8699
Andrew Honiga08d3b32014-02-27 19:35:14 +01008700 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008701 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008702
8703 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008704 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008705 return 1;
8706 vcpu->mmio_read_completed = 1;
8707 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008708 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008709
Gleb Natapov716d51a2012-09-03 15:24:26 +03008710 run->exit_reason = KVM_EXIT_MMIO;
8711 run->mmio.phys_addr = frag->gpa;
8712 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008713 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8714 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008715 run->mmio.is_write = vcpu->mmio_is_write;
8716 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8717 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008718}
8719
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008720static void kvm_save_current_fpu(struct fpu *fpu)
8721{
8722 /*
8723 * If the target FPU state is not resident in the CPU registers, just
8724 * memcpy() from current, else save CPU state directly to the target.
8725 */
8726 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8727 memcpy(&fpu->state, &current->thread.fpu.state,
8728 fpu_kernel_xstate_size);
8729 else
8730 copy_fpregs_to_fpstate(fpu);
8731}
8732
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008733/* Swap (qemu) user FPU context for the guest FPU context. */
8734static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8735{
Rik van Riel5f409e22019-04-03 18:41:52 +02008736 fpregs_lock();
8737
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008738 kvm_save_current_fpu(vcpu->arch.user_fpu);
8739
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008740 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008741 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008742 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008743
8744 fpregs_mark_activate();
8745 fpregs_unlock();
8746
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008747 trace_kvm_fpu(1);
8748}
8749
8750/* When vcpu_run ends, restore user space FPU context. */
8751static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8752{
Rik van Riel5f409e22019-04-03 18:41:52 +02008753 fpregs_lock();
8754
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008755 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8756
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008757 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008758
8759 fpregs_mark_activate();
8760 fpregs_unlock();
8761
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008762 ++vcpu->stat.fpu_reload;
8763 trace_kvm_fpu(0);
8764}
8765
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008766int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008767{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008768 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008769 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008770
Christoffer Dallaccb7572017-12-04 21:35:25 +01008771 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008772 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008773 kvm_load_guest_fpu(vcpu);
8774
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008775 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008776 if (kvm_run->immediate_exit) {
8777 r = -EINTR;
8778 goto out;
8779 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008780 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008781 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008782 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008783 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008784 if (signal_pending(current)) {
8785 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008786 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008787 ++vcpu->stat.signal_exits;
8788 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008789 goto out;
8790 }
8791
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008792 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008793 r = -EINVAL;
8794 goto out;
8795 }
8796
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008797 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008798 r = sync_regs(vcpu);
8799 if (r != 0)
8800 goto out;
8801 }
8802
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008803 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008804 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008805 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8806 r = -EINVAL;
8807 goto out;
8808 }
8809 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008810
Gleb Natapov716d51a2012-09-03 15:24:26 +03008811 if (unlikely(vcpu->arch.complete_userspace_io)) {
8812 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8813 vcpu->arch.complete_userspace_io = NULL;
8814 r = cui(vcpu);
8815 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008816 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008817 } else
8818 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008819
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008820 if (kvm_run->immediate_exit)
8821 r = -EINTR;
8822 else
8823 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008824
8825out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008826 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008827 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08008828 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008829 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008830 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008831
Christoffer Dallaccb7572017-12-04 21:35:25 +01008832 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008833 return r;
8834}
8835
Ken Hofsass01643c52018-01-31 16:03:36 -08008836static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008837{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008838 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8839 /*
8840 * We are here if userspace calls get_regs() in the middle of
8841 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008842 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008843 * that usually, but some bad designed PV devices (vmware
8844 * backdoor interface) need this to work
8845 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008846 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008847 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8848 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008849 regs->rax = kvm_rax_read(vcpu);
8850 regs->rbx = kvm_rbx_read(vcpu);
8851 regs->rcx = kvm_rcx_read(vcpu);
8852 regs->rdx = kvm_rdx_read(vcpu);
8853 regs->rsi = kvm_rsi_read(vcpu);
8854 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008855 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008856 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008857#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008858 regs->r8 = kvm_r8_read(vcpu);
8859 regs->r9 = kvm_r9_read(vcpu);
8860 regs->r10 = kvm_r10_read(vcpu);
8861 regs->r11 = kvm_r11_read(vcpu);
8862 regs->r12 = kvm_r12_read(vcpu);
8863 regs->r13 = kvm_r13_read(vcpu);
8864 regs->r14 = kvm_r14_read(vcpu);
8865 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008866#endif
8867
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008868 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008869 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008870}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008871
Ken Hofsass01643c52018-01-31 16:03:36 -08008872int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8873{
8874 vcpu_load(vcpu);
8875 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008876 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008877 return 0;
8878}
8879
Ken Hofsass01643c52018-01-31 16:03:36 -08008880static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008881{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008882 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8883 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8884
Sean Christophersonde3cd112019-04-30 10:36:17 -07008885 kvm_rax_write(vcpu, regs->rax);
8886 kvm_rbx_write(vcpu, regs->rbx);
8887 kvm_rcx_write(vcpu, regs->rcx);
8888 kvm_rdx_write(vcpu, regs->rdx);
8889 kvm_rsi_write(vcpu, regs->rsi);
8890 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008891 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008892 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008893#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008894 kvm_r8_write(vcpu, regs->r8);
8895 kvm_r9_write(vcpu, regs->r9);
8896 kvm_r10_write(vcpu, regs->r10);
8897 kvm_r11_write(vcpu, regs->r11);
8898 kvm_r12_write(vcpu, regs->r12);
8899 kvm_r13_write(vcpu, regs->r13);
8900 kvm_r14_write(vcpu, regs->r14);
8901 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008902#endif
8903
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008904 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008905 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008906
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008907 vcpu->arch.exception.pending = false;
8908
Avi Kivity3842d132010-07-27 12:30:24 +03008909 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008910}
Avi Kivity3842d132010-07-27 12:30:24 +03008911
Ken Hofsass01643c52018-01-31 16:03:36 -08008912int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8913{
8914 vcpu_load(vcpu);
8915 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008916 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008917 return 0;
8918}
8919
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008920void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8921{
8922 struct kvm_segment cs;
8923
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008924 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008925 *db = cs.db;
8926 *l = cs.l;
8927}
8928EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8929
Ken Hofsass01643c52018-01-31 16:03:36 -08008930static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008931{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008932 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008933
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008934 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8935 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8936 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8937 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8938 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8939 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008940
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008941 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8942 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008943
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008944 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008945 sregs->idt.limit = dt.size;
8946 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008947 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008948 sregs->gdt.limit = dt.size;
8949 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008950
Avi Kivity4d4ec082009-12-29 18:07:30 +02008951 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008952 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008953 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008954 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008955 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008956 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008957 sregs->apic_base = kvm_get_apic_base(vcpu);
8958
Jordan Borgner0e96f312018-10-28 12:58:28 +00008959 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008960
Liran Alon04140b42018-03-23 03:01:31 +03008961 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008962 set_bit(vcpu->arch.interrupt.nr,
8963 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008964}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008965
Ken Hofsass01643c52018-01-31 16:03:36 -08008966int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8967 struct kvm_sregs *sregs)
8968{
8969 vcpu_load(vcpu);
8970 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008971 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008972 return 0;
8973}
8974
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008975int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8976 struct kvm_mp_state *mp_state)
8977{
Christoffer Dallfd232562017-12-04 21:35:30 +01008978 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008979 if (kvm_mpx_supported())
8980 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008981
Jan Kiszka66450a22013-03-13 12:42:34 +01008982 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308983 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8984 vcpu->arch.pv.pv_unhalted)
8985 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8986 else
8987 mp_state->mp_state = vcpu->arch.mp_state;
8988
Sean Christophersonf958bd22019-12-09 12:19:31 -08008989 if (kvm_mpx_supported())
8990 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008991 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008992 return 0;
8993}
8994
8995int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8996 struct kvm_mp_state *mp_state)
8997{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008998 int ret = -EINVAL;
8999
9000 vcpu_load(vcpu);
9001
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009002 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01009003 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01009004 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01009005
Liran Alon27cbe7d2019-11-11 11:16:40 +02009006 /*
9007 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
9008 * INIT state; latched init should be reported using
9009 * KVM_SET_VCPU_EVENTS, so reject it here.
9010 */
9011 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01009012 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
9013 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01009014 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01009015
Jan Kiszka66450a22013-03-13 12:42:34 +01009016 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
9017 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
9018 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
9019 } else
9020 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03009021 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01009022
9023 ret = 0;
9024out:
9025 vcpu_put(vcpu);
9026 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009027}
9028
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009029int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
9030 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009031{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009032 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009033 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009034
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009035 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02009036
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009037 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009038 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009039 if (ret) {
9040 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9041 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9042 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009043 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009044 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009045
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009046 kvm_rip_write(vcpu, ctxt->eip);
9047 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009048 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009049}
9050EXPORT_SYMBOL_GPL(kvm_task_switch);
9051
Peng Hao3140c152018-04-02 09:15:32 +08009052static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009053{
Tianyu Lan37b95952018-01-16 17:34:07 +08009054 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009055 /*
9056 * When EFER.LME and CR0.PG are set, the processor is in
9057 * 64-bit mode (though maybe in a 32-bit code segment).
9058 * CR4.PAE and EFER.LMA must be set.
9059 */
Tianyu Lan37b95952018-01-16 17:34:07 +08009060 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05009061 || !(sregs->efer & EFER_LMA))
9062 return -EINVAL;
9063 } else {
9064 /*
9065 * Not in 64-bit mode: EFER.LMA is clear and the code
9066 * segment cannot be 64-bit.
9067 */
9068 if (sregs->efer & EFER_LMA || sregs->cs.l)
9069 return -EINVAL;
9070 }
9071
Wanpeng Li3ca94192019-09-18 17:50:10 +08009072 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009073}
9074
Ken Hofsass01643c52018-01-31 16:03:36 -08009075static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009076{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009077 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009078 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009079 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009080 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009081 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009082 int ret = -EINVAL;
9083
Lan Tianyuf2981032017-12-14 03:01:52 -05009084 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009085 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009086
Jim Mattsond3802282017-08-10 10:14:13 -07009087 apic_base_msr.data = sregs->apic_base;
9088 apic_base_msr.host_initiated = true;
9089 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009090 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009091
Gleb Natapov89a27f42010-02-16 10:51:48 +02009092 dt.size = sregs->idt.limit;
9093 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009094 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009095 dt.size = sregs->gdt.limit;
9096 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009097 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009098
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009099 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009100 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009101 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009102 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009103
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009104 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009105
Avi Kivityf6801df2010-01-21 15:31:50 +02009106 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009107 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009108
Avi Kivity4d4ec082009-12-29 18:07:30 +02009109 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009110 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009111 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009112
Avi Kivityfc78f512009-12-07 12:16:48 +02009113 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009114 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9115 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009116 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009117 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009118 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009119
9120 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009121 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009122 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009123 mmu_reset_needed = 1;
9124 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009125 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009126
9127 if (mmu_reset_needed)
9128 kvm_mmu_reset_context(vcpu);
9129
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009130 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009131 pending_vec = find_first_bit(
9132 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9133 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009134 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009135 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009136 }
9137
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009138 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9139 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9140 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9141 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9142 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9143 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009144
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009145 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9146 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009147
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009148 update_cr8_intercept(vcpu);
9149
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009150 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009151 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009152 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009153 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009154 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9155
Avi Kivity3842d132010-07-27 12:30:24 +03009156 kvm_make_request(KVM_REQ_EVENT, vcpu);
9157
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009158 ret = 0;
9159out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009160 return ret;
9161}
9162
9163int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9164 struct kvm_sregs *sregs)
9165{
9166 int ret;
9167
9168 vcpu_load(vcpu);
9169 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009170 vcpu_put(vcpu);
9171 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009172}
9173
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009174int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9175 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009176{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009177 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009178 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009179
Christoffer Dall66b56562017-12-04 21:35:33 +01009180 vcpu_load(vcpu);
9181
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009182 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9183 r = -EBUSY;
9184 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009185 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009186 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9187 kvm_queue_exception(vcpu, DB_VECTOR);
9188 else
9189 kvm_queue_exception(vcpu, BP_VECTOR);
9190 }
9191
Jan Kiszka91586a32009-10-05 13:07:21 +02009192 /*
9193 * Read rflags as long as potentially injected trace flags are still
9194 * filtered out.
9195 */
9196 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009197
9198 vcpu->guest_debug = dbg->control;
9199 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9200 vcpu->guest_debug = 0;
9201
9202 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009203 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9204 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009205 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009206 } else {
9207 for (i = 0; i < KVM_NR_DB_REGS; i++)
9208 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009209 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009210 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009211
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009212 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9213 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9214 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009215
Jan Kiszka91586a32009-10-05 13:07:21 +02009216 /*
9217 * Trigger an rflags update that will inject or remove the trace
9218 * flags.
9219 */
9220 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009221
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009222 kvm_x86_ops.update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009223
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009224 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009225
Avi Kivity2122ff52010-05-13 11:25:04 +03009226out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009227 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009228 return r;
9229}
9230
9231/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009232 * Translate a guest virtual address to a guest physical address.
9233 */
9234int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9235 struct kvm_translation *tr)
9236{
9237 unsigned long vaddr = tr->linear_address;
9238 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009239 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009240
Christoffer Dall1da5b612017-12-04 21:35:32 +01009241 vcpu_load(vcpu);
9242
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009243 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009244 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009245 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009246 tr->physical_address = gpa;
9247 tr->valid = gpa != UNMAPPED_GVA;
9248 tr->writeable = 1;
9249 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009250
Christoffer Dall1da5b612017-12-04 21:35:32 +01009251 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009252 return 0;
9253}
9254
Hollis Blanchardd0752062007-10-31 17:24:25 -05009255int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9256{
Christoffer Dall13931232017-12-04 21:35:34 +01009257 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009258
Christoffer Dall13931232017-12-04 21:35:34 +01009259 vcpu_load(vcpu);
9260
Marc Orrb666a4b2018-11-06 14:53:56 -08009261 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009262 memcpy(fpu->fpr, fxsave->st_space, 128);
9263 fpu->fcw = fxsave->cwd;
9264 fpu->fsw = fxsave->swd;
9265 fpu->ftwx = fxsave->twd;
9266 fpu->last_opcode = fxsave->fop;
9267 fpu->last_ip = fxsave->rip;
9268 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009269 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009270
Christoffer Dall13931232017-12-04 21:35:34 +01009271 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009272 return 0;
9273}
9274
9275int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9276{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009277 struct fxregs_state *fxsave;
9278
9279 vcpu_load(vcpu);
9280
Marc Orrb666a4b2018-11-06 14:53:56 -08009281 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009282
Hollis Blanchardd0752062007-10-31 17:24:25 -05009283 memcpy(fxsave->st_space, fpu->fpr, 128);
9284 fxsave->cwd = fpu->fcw;
9285 fxsave->swd = fpu->fsw;
9286 fxsave->twd = fpu->ftwx;
9287 fxsave->fop = fpu->last_opcode;
9288 fxsave->rip = fpu->last_ip;
9289 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009290 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009291
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009292 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009293 return 0;
9294}
9295
Ken Hofsass01643c52018-01-31 16:03:36 -08009296static void store_regs(struct kvm_vcpu *vcpu)
9297{
9298 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9299
9300 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9301 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9302
9303 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9304 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9305
9306 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9307 kvm_vcpu_ioctl_x86_get_vcpu_events(
9308 vcpu, &vcpu->run->s.regs.events);
9309}
9310
9311static int sync_regs(struct kvm_vcpu *vcpu)
9312{
9313 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9314 return -EINVAL;
9315
9316 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9317 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9318 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9319 }
9320 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9321 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9322 return -EINVAL;
9323 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9324 }
9325 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9326 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9327 vcpu, &vcpu->run->s.regs.events))
9328 return -EINVAL;
9329 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9330 }
9331
9332 return 0;
9333}
9334
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009335static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009336{
Marc Orrb666a4b2018-11-06 14:53:56 -08009337 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009338 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009339 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009340 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009341
Dexuan Cui2acf9232010-06-10 11:27:12 +08009342 /*
9343 * Ensure guest xcr0 is valid for loading
9344 */
Dave Hansend91cab72015-09-02 16:31:26 -07009345 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009346
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009347 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009348}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009349
Sean Christopherson897cc382019-12-18 13:55:09 -08009350int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009351{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009352 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009353 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9354 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009355
Sean Christopherson897cc382019-12-18 13:55:09 -08009356 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009357}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009358
Sean Christophersone529ef62019-12-18 13:55:15 -08009359int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009360{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009361 struct page *page;
9362 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009363
Sean Christopherson95a0d012019-12-18 13:55:23 -08009364 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9365 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9366 else
9367 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9368
9369 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9370
9371 r = kvm_mmu_create(vcpu);
9372 if (r < 0)
9373 return r;
9374
9375 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009376 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9377 if (r < 0)
9378 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009379 if (kvm_apicv_activated(vcpu->kvm))
9380 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009381 } else
9382 static_key_slow_inc(&kvm_no_apic_vcpu);
9383
9384 r = -ENOMEM;
9385
9386 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9387 if (!page)
9388 goto fail_free_lapic;
9389 vcpu->arch.pio_data = page_address(page);
9390
9391 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9392 GFP_KERNEL_ACCOUNT);
9393 if (!vcpu->arch.mce_banks)
9394 goto fail_free_pio_data;
9395 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9396
9397 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9398 GFP_KERNEL_ACCOUNT))
9399 goto fail_free_mce_banks;
9400
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009401 if (!alloc_emulate_ctxt(vcpu))
9402 goto free_wbinvd_dirty_mask;
9403
Sean Christopherson95a0d012019-12-18 13:55:23 -08009404 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9405 GFP_KERNEL_ACCOUNT);
9406 if (!vcpu->arch.user_fpu) {
9407 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009408 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009409 }
9410
9411 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9412 GFP_KERNEL_ACCOUNT);
9413 if (!vcpu->arch.guest_fpu) {
9414 pr_err("kvm: failed to allocate vcpu's fpu\n");
9415 goto free_user_fpu;
9416 }
9417 fx_init(vcpu);
9418
Sean Christopherson95a0d012019-12-18 13:55:23 -08009419 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9420
9421 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9422
9423 kvm_async_pf_hash_reset(vcpu);
9424 kvm_pmu_init(vcpu);
9425
9426 vcpu->arch.pending_external_vector = -1;
9427 vcpu->arch.preempted_in_kernel = false;
9428
9429 kvm_hv_vcpu_init(vcpu);
9430
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009431 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009432 if (r)
9433 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009434
Sean Christopherson0cf91352019-03-07 15:43:02 -08009435 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009436 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009437 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009438 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009439 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009440 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009441 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009442 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009443
9444free_guest_fpu:
9445 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9446free_user_fpu:
9447 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009448free_emulate_ctxt:
9449 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009450free_wbinvd_dirty_mask:
9451 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9452fail_free_mce_banks:
9453 kfree(vcpu->arch.mce_banks);
9454fail_free_pio_data:
9455 free_page((unsigned long)vcpu->arch.pio_data);
9456fail_free_lapic:
9457 kvm_free_lapic(vcpu);
9458fail_mmu_destroy:
9459 kvm_mmu_destroy(vcpu);
9460 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009461}
9462
Dominik Dingel31928aa2014-12-04 15:47:07 +01009463void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009464{
Will Auld8fe8ab42012-11-29 12:42:12 -08009465 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009466 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009467
Roman Kagand3457c82017-07-14 17:13:20 +03009468 kvm_hv_vcpu_postcreate(vcpu);
9469
Christoffer Dallec7660c2017-12-04 21:35:23 +01009470 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009471 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009472 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009473 msr.data = 0x0;
9474 msr.index = MSR_IA32_TSC;
9475 msr.host_initiated = true;
9476 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009477 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009478
9479 /* poll control enabled by default */
9480 vcpu->arch.msr_kvm_poll_control = 1;
9481
Christoffer Dallec7660c2017-12-04 21:35:23 +01009482 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009483
Wanpeng Lib34de572020-02-28 11:18:41 +08009484 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9485 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9486 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009487}
9488
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009489void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009490{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009491 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009492 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009493
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009494 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9495
Sean Christopherson50b143e2019-12-18 13:55:07 -08009496 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009497
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009498 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009499
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009500 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009501 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9502 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9503 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009504
9505 kvm_hv_vcpu_uninit(vcpu);
9506 kvm_pmu_destroy(vcpu);
9507 kfree(vcpu->arch.mce_banks);
9508 kvm_free_lapic(vcpu);
9509 idx = srcu_read_lock(&vcpu->kvm->srcu);
9510 kvm_mmu_destroy(vcpu);
9511 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9512 free_page((unsigned long)vcpu->arch.pio_data);
9513 if (!lapic_in_kernel(vcpu))
9514 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009515}
9516
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009517void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009518{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009519 kvm_lapic_reset(vcpu, init_event);
9520
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009521 vcpu->arch.hflags = 0;
9522
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009523 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009524 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009525 atomic_set(&vcpu->arch.nmi_queued, 0);
9526 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009527 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009528 kvm_clear_interrupt_queue(vcpu);
9529 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009530
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009531 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009532 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009533 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009534 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009535 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009536
Nadav Amit11190222015-04-02 03:10:38 +03009537 vcpu->arch.cr2 = 0;
9538
Avi Kivity3842d132010-07-27 12:30:24 +03009539 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009540 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009541 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009542
Glauber Costa12f9a482011-02-01 14:16:40 -05009543 kvmclock_reset(vcpu);
9544
Gleb Natapovaf585b92010-10-14 11:22:46 +02009545 kvm_clear_async_pf_completion_queue(vcpu);
9546 kvm_async_pf_hash_reset(vcpu);
9547 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009548
Wanpeng Lia554d202017-10-11 05:10:19 -07009549 if (kvm_mpx_supported()) {
9550 void *mpx_state_buffer;
9551
9552 /*
9553 * To avoid have the INIT path from kvm_apic_has_events() that be
9554 * called with loaded FPU and does not let userspace fix the state.
9555 */
Rik van Rielf775b132017-11-14 16:54:23 -05009556 if (init_event)
9557 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009558 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009559 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009560 if (mpx_state_buffer)
9561 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009562 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009563 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009564 if (mpx_state_buffer)
9565 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009566 if (init_event)
9567 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009568 }
9569
Paolo Bonzini64d60672015-05-07 11:36:11 +02009570 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009571 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009572 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009573
Kyle Hueydb2336a2017-03-20 01:16:28 -07009574 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009575
9576 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009577 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009578
Julian Stecklina66f7b722012-12-05 15:26:19 +01009579 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9580 vcpu->arch.regs_avail = ~0;
9581 vcpu->arch.regs_dirty = ~0;
9582
Wanpeng Lia554d202017-10-11 05:10:19 -07009583 vcpu->arch.ia32_xss = 0;
9584
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009585 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009586}
9587
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009588void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009589{
9590 struct kvm_segment cs;
9591
9592 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9593 cs.selector = vector << 8;
9594 cs.base = vector << 12;
9595 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9596 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009597}
9598
Radim Krčmář13a34e02014-08-28 15:13:03 +02009599int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009600{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009601 struct kvm *kvm;
9602 struct kvm_vcpu *vcpu;
9603 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009604 int ret;
9605 u64 local_tsc;
9606 u64 max_tsc = 0;
9607 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009608
9609 kvm_shared_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009610 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009611 if (ret != 0)
9612 return ret;
9613
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009614 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009615 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009616 list_for_each_entry(kvm, &vm_list, vm_list) {
9617 kvm_for_each_vcpu(i, vcpu, kvm) {
9618 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009619 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009620 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9621 backwards_tsc = true;
9622 if (vcpu->arch.last_host_tsc > max_tsc)
9623 max_tsc = vcpu->arch.last_host_tsc;
9624 }
9625 }
9626 }
9627
9628 /*
9629 * Sometimes, even reliable TSCs go backwards. This happens on
9630 * platforms that reset TSC during suspend or hibernate actions, but
9631 * maintain synchronization. We must compensate. Fortunately, we can
9632 * detect that condition here, which happens early in CPU bringup,
9633 * before any KVM threads can be running. Unfortunately, we can't
9634 * bring the TSCs fully up to date with real time, as we aren't yet far
9635 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009636 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009637 * variables that haven't been updated yet.
9638 *
9639 * So we simply find the maximum observed TSC above, then record the
9640 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9641 * the adjustment will be applied. Note that we accumulate
9642 * adjustments, in case multiple suspend cycles happen before some VCPU
9643 * gets a chance to run again. In the event that no KVM threads get a
9644 * chance to run, we will miss the entire elapsed period, as we'll have
9645 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9646 * loose cycle time. This isn't too big a deal, since the loss will be
9647 * uniform across all VCPUs (not to mention the scenario is extremely
9648 * unlikely). It is possible that a second hibernate recovery happens
9649 * much faster than a first, causing the observed TSC here to be
9650 * smaller; this would require additional padding adjustment, which is
9651 * why we set last_host_tsc to the local tsc observed here.
9652 *
9653 * N.B. - this code below runs only on platforms with reliable TSC,
9654 * as that is the only way backwards_tsc is set above. Also note
9655 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9656 * have the same delta_cyc adjustment applied if backwards_tsc
9657 * is detected. Note further, this adjustment is only done once,
9658 * as we reset last_host_tsc on all VCPUs to stop this from being
9659 * called multiple times (one for each physical CPU bringup).
9660 *
Guo Chao4a969982012-06-28 15:17:27 +08009661 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009662 * will be compensated by the logic in vcpu_load, which sets the TSC to
9663 * catchup mode. This will catchup all VCPUs to real time, but cannot
9664 * guarantee that they stay in perfect synchronization.
9665 */
9666 if (backwards_tsc) {
9667 u64 delta_cyc = max_tsc - local_tsc;
9668 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009669 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009670 kvm_for_each_vcpu(i, vcpu, kvm) {
9671 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9672 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009673 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009674 }
9675
9676 /*
9677 * We have to disable TSC offset matching.. if you were
9678 * booting a VM while issuing an S4 host suspend....
9679 * you may have some problem. Solving this issue is
9680 * left as an exercise to the reader.
9681 */
9682 kvm->arch.last_tsc_nsec = 0;
9683 kvm->arch.last_tsc_write = 0;
9684 }
9685
9686 }
9687 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009688}
9689
Radim Krčmář13a34e02014-08-28 15:13:03 +02009690void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009691{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009692 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009693 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009694}
9695
Sean Christophersonb9904082020-03-21 13:25:55 -07009696int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009697{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009698 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009699 int r;
9700
Sean Christopherson91661982020-03-02 15:57:06 -08009701 rdmsrl_safe(MSR_EFER, &host_efer);
9702
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009703 if (boot_cpu_has(X86_FEATURE_XSAVES))
9704 rdmsrl(MSR_IA32_XSS, host_xss);
9705
Sean Christophersond008dfd2020-03-21 13:25:56 -07009706 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009707 if (r != 0)
9708 return r;
9709
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009710 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -07009711
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009712 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9713 supported_xss = 0;
9714
Paolo Bonzini139f7422020-05-05 09:40:46 -04009715#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
9716 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
9717#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -08009718
Haozhong Zhang35181e82015-10-20 15:39:03 +08009719 if (kvm_has_tsc_control) {
9720 /*
9721 * Make sure the user can only configure tsc_khz values that
9722 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009723 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009724 * be 1 on all machines.
9725 */
9726 u64 max = min(0x7fffffffULL,
9727 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9728 kvm_max_guest_tsc_khz = max;
9729
Haozhong Zhangad7218832015-10-20 15:39:02 +08009730 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009731 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009732
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009733 kvm_init_msr_list();
9734 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009735}
9736
9737void kvm_arch_hardware_unsetup(void)
9738{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009739 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009740}
9741
Sean Christophersonb9904082020-03-21 13:25:55 -07009742int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009743{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009744 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009745 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009746
9747 WARN_ON(!irqs_disabled());
9748
Paolo Bonzini139f7422020-05-05 09:40:46 -04009749 if (__cr4_reserved_bits(cpu_has, c) !=
9750 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009751 return -EIO;
9752
Sean Christophersond008dfd2020-03-21 13:25:56 -07009753 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009754}
9755
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009756bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9757{
9758 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9759}
9760EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9761
9762bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9763{
9764 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9765}
9766
Gleb Natapov54e98182012-08-05 15:58:32 +03009767struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009768EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009769
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009770void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9771{
Like Xub35e5542019-10-27 18:52:43 +08009772 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9773
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009774 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009775 if (pmu->version && unlikely(pmu->event_count)) {
9776 pmu->need_cleanup = true;
9777 kvm_make_request(KVM_REQ_PMU, vcpu);
9778 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009779 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009780}
9781
Sean Christopherson562b6b02020-01-26 16:41:13 -08009782void kvm_arch_free_vm(struct kvm *kvm)
9783{
9784 kfree(kvm->arch.hyperv.hv_pa_pg);
9785 vfree(kvm);
9786}
9787
9788
Carsten Ottee08b9632012-01-04 10:25:20 +01009789int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009790{
Carsten Ottee08b9632012-01-04 10:25:20 +01009791 if (type)
9792 return -EINVAL;
9793
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009794 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009795 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009796 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009797 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009798 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009799 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009800
Sheng Yang5550af42008-10-15 20:15:06 +08009801 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9802 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009803 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9804 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9805 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009806
Jan Kiszka038f8c12011-02-04 10:49:11 +01009807 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009808 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009809 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9810
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009811 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009812 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009813
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009814 kvm->arch.guest_can_read_msr_platform_info = true;
9815
Andrew Jones7e44e442014-02-28 12:52:54 +01009816 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009817 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009818
Roman Kagancbc02362018-02-01 16:48:31 +03009819 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009820 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009821 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009822
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009823 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009824}
9825
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009826int kvm_arch_post_init_vm(struct kvm *kvm)
9827{
9828 return kvm_mmu_post_init_vm(kvm);
9829}
9830
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009831static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9832{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009833 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009834 kvm_mmu_unload(vcpu);
9835 vcpu_put(vcpu);
9836}
9837
9838static void kvm_free_vcpus(struct kvm *kvm)
9839{
9840 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009841 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009842
9843 /*
9844 * Unpin any mmu pages first.
9845 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009846 kvm_for_each_vcpu(i, vcpu, kvm) {
9847 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009848 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009849 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009850 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009851 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009852
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009853 mutex_lock(&kvm->lock);
9854 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9855 kvm->vcpus[i] = NULL;
9856
9857 atomic_set(&kvm->online_vcpus, 0);
9858 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009859}
9860
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009861void kvm_arch_sync_events(struct kvm *kvm)
9862{
Andrew Jones332967a2014-02-28 12:52:55 +01009863 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009864 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009865 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009866}
9867
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009868int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009869{
9870 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009871 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009872 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009873 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009874
9875 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009876 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9877 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009878
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009879 slot = id_to_memslot(slots, id);
9880 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009881 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009882 return -EEXIST;
9883
9884 /*
9885 * MAP_SHARED to prevent internal slot pages from being moved
9886 * by fork()/COW.
9887 */
9888 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9889 MAP_SHARED | MAP_ANONYMOUS, 0);
9890 if (IS_ERR((void *)hva))
9891 return PTR_ERR((void *)hva);
9892 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009893 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009894 return 0;
9895
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009896 /*
9897 * Stuff a non-canonical value to catch use-after-delete. This
9898 * ends up being 0 on 32-bit KVM, but there's no better
9899 * alternative.
9900 */
9901 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009902 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009903 }
9904
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009905 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009906 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009907
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009908 m.slot = id | (i << 16);
9909 m.flags = 0;
9910 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009911 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009912 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009913 r = __kvm_set_memory_region(kvm, &m);
9914 if (r < 0)
9915 return r;
9916 }
9917
Eric Biggers103c7632018-01-31 17:30:21 -08009918 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009919 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009920
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009921 return 0;
9922}
9923EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9924
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009925void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9926{
9927 kvm_mmu_pre_destroy_vm(kvm);
9928}
9929
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009930void kvm_arch_destroy_vm(struct kvm *kvm)
9931{
Andrew Honig27469d22013-04-18 09:38:14 -07009932 if (current->mm == kvm->mm) {
9933 /*
9934 * Free memory regions allocated on behalf of userspace,
9935 * unless the the memory map has changed due to process exit
9936 * or fd copying.
9937 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009938 mutex_lock(&kvm->slots_lock);
9939 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9940 0, 0);
9941 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9942 0, 0);
9943 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9944 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009945 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009946 if (kvm_x86_ops.vm_destroy)
9947 kvm_x86_ops.vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009948 kvm_pic_destroy(kvm);
9949 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009950 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009951 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009952 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009953 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009954 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009955 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009956}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009957
Sean Christophersone96c81e2020-02-18 13:07:27 -08009958void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009959{
9960 int i;
9961
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009962 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009963 kvfree(slot->arch.rmap[i]);
9964 slot->arch.rmap[i] = NULL;
9965
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009966 if (i == 0)
9967 continue;
9968
Sean Christophersone96c81e2020-02-18 13:07:27 -08009969 kvfree(slot->arch.lpage_info[i - 1]);
9970 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009971 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009972
Sean Christophersone96c81e2020-02-18 13:07:27 -08009973 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009974}
9975
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009976static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9977 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009978{
9979 int i;
9980
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009981 /*
9982 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9983 * old arrays will be freed by __kvm_set_memory_region() if installing
9984 * the new memslot is successful.
9985 */
9986 memset(&slot->arch, 0, sizeof(slot->arch));
9987
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009988 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009989 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009990 unsigned long ugfn;
9991 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009992 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009993
9994 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9995 slot->base_gfn, level) + 1;
9996
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009997 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009998 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009999 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010000 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010001 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010002 if (i == 0)
10003 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010004
Ben Gardon254272c2019-02-11 11:02:50 -080010005 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010006 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010007 goto out_free;
10008
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010009 slot->arch.lpage_info[i - 1] = linfo;
10010
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010011 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010012 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010013 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010014 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010015 ugfn = slot->userspace_addr >> PAGE_SHIFT;
10016 /*
10017 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080010018 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010019 */
Sean Christopherson600087b2020-03-02 15:57:05 -080010020 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010021 unsigned long j;
10022
10023 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010024 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010025 }
10026 }
10027
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010028 if (kvm_page_track_create_memslot(slot, npages))
10029 goto out_free;
10030
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010031 return 0;
10032
10033out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010034 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010035 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010036 slot->arch.rmap[i] = NULL;
10037 if (i == 0)
10038 continue;
10039
Thomas Huth548ef282015-02-24 21:29:25 +010010040 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010041 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010042 }
10043 return -ENOMEM;
10044}
10045
Sean Christopherson15248252019-02-05 12:54:17 -080010046void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010047{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010048 struct kvm_vcpu *vcpu;
10049 int i;
10050
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010051 /*
10052 * memslots->generation has been incremented.
10053 * mmio generation may have reached its maximum value.
10054 */
Sean Christopherson15248252019-02-05 12:54:17 -080010055 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010056
10057 /* Force re-initialization of steal_time cache */
10058 kvm_for_each_vcpu(i, vcpu, kvm)
10059 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010060}
10061
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010062int kvm_arch_prepare_memory_region(struct kvm *kvm,
10063 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010064 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010065 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010066{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010067 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10068 return kvm_alloc_memslot_metadata(memslot,
10069 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010070 return 0;
10071}
10072
Kai Huang88178fd2015-01-28 10:54:27 +080010073static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
10074 struct kvm_memory_slot *new)
10075{
10076 /* Still write protect RO slot */
10077 if (new->flags & KVM_MEM_READONLY) {
Sean Christopherson3bae0452020-04-27 17:54:22 -070010078 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K);
Kai Huang88178fd2015-01-28 10:54:27 +080010079 return;
10080 }
10081
10082 /*
10083 * Call kvm_x86_ops dirty logging hooks when they are valid.
10084 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010085 * kvm_x86_ops.slot_disable_log_dirty is called when:
Kai Huang88178fd2015-01-28 10:54:27 +080010086 *
10087 * - KVM_MR_CREATE with dirty logging is disabled
10088 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10089 *
10090 * The reason is, in case of PML, we need to set D-bit for any slots
10091 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010092 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010093 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010094 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010095 * logging disabled for memory slots.
10096 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010097 * kvm_x86_ops.slot_enable_log_dirty is called when switching new slot
Kai Huang88178fd2015-01-28 10:54:27 +080010098 * to dirty logging mode.
10099 *
10100 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10101 *
10102 * In case of write protect:
10103 *
10104 * Write protect all pages for dirty logging.
10105 *
10106 * All the sptes including the large sptes which point to this
10107 * slot are set to readonly. We can not create any new large
10108 * spte on this slot until the end of the logging.
10109 *
10110 * See the comments in fast_page_fault().
10111 */
10112 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010113 if (kvm_x86_ops.slot_enable_log_dirty) {
10114 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010115 } else {
10116 int level =
10117 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
Sean Christopherson3bae0452020-04-27 17:54:22 -070010118 PG_LEVEL_2M : PG_LEVEL_4K;
Jay Zhou3c9bd402020-02-27 09:32:27 +080010119
10120 /*
10121 * If we're with initial-all-set, we don't need
10122 * to write protect any small page because
10123 * they're reported as dirty already. However
10124 * we still need to write-protect huge pages
10125 * so that the page split can happen lazily on
10126 * the first write to the huge page.
10127 */
10128 kvm_mmu_slot_remove_write_access(kvm, new, level);
10129 }
Kai Huang88178fd2015-01-28 10:54:27 +080010130 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010131 if (kvm_x86_ops.slot_disable_log_dirty)
10132 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010133 }
10134}
10135
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010136void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010137 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010138 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010139 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010140 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010141{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010142 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010143 kvm_mmu_change_mmu_pages(kvm,
10144 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010145
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010146 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010147 * Dirty logging tracks sptes in 4k granularity, meaning that large
10148 * sptes have to be split. If live migration is successful, the guest
10149 * in the source machine will be destroyed and large sptes will be
10150 * created in the destination. However, if the guest continues to run
10151 * in the source machine (for example if live migration fails), small
10152 * sptes will remain around and cause bad performance.
10153 *
10154 * Scan sptes if dirty logging has been stopped, dropping those
10155 * which can be collapsed into a single large-page spte. Later
10156 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010157 *
10158 * There is no need to do this in any of the following cases:
10159 * CREATE: No dirty mappings will already exist.
10160 * MOVE/DELETE: The old mappings will already have been cleaned up by
10161 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010162 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010163 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010164 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10165 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10166 kvm_mmu_zap_collapsible_sptes(kvm, new);
10167
10168 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010169 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010170 *
Kai Huang88178fd2015-01-28 10:54:27 +080010171 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10172 * been zapped so no dirty logging staff is needed for old slot. For
10173 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10174 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010175 *
10176 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010177 */
Kai Huang88178fd2015-01-28 10:54:27 +080010178 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010179 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010180
10181 /* Free the arrays associated with the old memslot. */
10182 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010183 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010184}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010185
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010186void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010187{
Sean Christopherson7390de12019-02-05 13:01:31 -080010188 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010189}
10190
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010191void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10192 struct kvm_memory_slot *slot)
10193{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010194 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010195}
10196
Liran Alone6c67d82018-09-04 10:56:52 +030010197static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10198{
10199 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010200 kvm_x86_ops.guest_apic_has_interrupt &&
10201 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010202}
10203
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010204static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10205{
10206 if (!list_empty_careful(&vcpu->async_pf.done))
10207 return true;
10208
10209 if (kvm_apic_has_events(vcpu))
10210 return true;
10211
10212 if (vcpu->arch.pv.pv_unhalted)
10213 return true;
10214
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010215 if (vcpu->arch.exception.pending)
10216 return true;
10217
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010218 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10219 (vcpu->arch.nmi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010220 kvm_x86_ops.nmi_allowed(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010221 return true;
10222
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010223 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040010224 (vcpu->arch.smi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010225 kvm_x86_ops.smi_allowed(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010226 return true;
10227
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010228 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010229 (kvm_cpu_has_interrupt(vcpu) ||
10230 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010231 return true;
10232
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010233 if (kvm_hv_has_stimer_pending(vcpu))
10234 return true;
10235
Sean Christophersond2060bd2020-04-22 19:25:39 -070010236 if (is_guest_mode(vcpu) &&
10237 kvm_x86_ops.nested_ops->hv_timer_pending &&
10238 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
10239 return true;
10240
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010241 return false;
10242}
10243
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010244int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10245{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010246 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010247}
Zhang Xiantao57361992007-12-17 14:21:40 +080010248
Wanpeng Li17e433b2019-08-05 10:03:19 +080010249bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10250{
10251 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10252 return true;
10253
10254 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10255 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10256 kvm_test_request(KVM_REQ_EVENT, vcpu))
10257 return true;
10258
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010259 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010260 return true;
10261
10262 return false;
10263}
10264
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010265bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10266{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010267 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010268}
10269
Christoffer Dallb6d33832012-03-08 16:44:24 -050010270int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010271{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010272 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010273}
Gleb Natapov78646122009-03-23 12:12:11 +020010274
10275int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10276{
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010277 return kvm_x86_ops.interrupt_allowed(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020010278}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010279
Nadav Amit82b32772014-11-02 11:54:45 +020010280unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10281{
10282 if (is_64_bit_mode(vcpu))
10283 return kvm_rip_read(vcpu);
10284 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10285 kvm_rip_read(vcpu));
10286}
10287EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10288
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010289bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10290{
Nadav Amit82b32772014-11-02 11:54:45 +020010291 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010292}
10293EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10294
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010295unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10296{
10297 unsigned long rflags;
10298
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010299 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010300 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010301 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010302 return rflags;
10303}
10304EXPORT_SYMBOL_GPL(kvm_get_rflags);
10305
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010306static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010307{
10308 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010309 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010310 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010311 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010312}
10313
10314void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10315{
10316 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010317 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010318}
10319EXPORT_SYMBOL_GPL(kvm_set_rflags);
10320
Gleb Natapov56028d02010-10-17 18:13:42 +020010321void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10322{
10323 int r;
10324
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010325 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010326 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010327 return;
10328
10329 r = kvm_mmu_reload(vcpu);
10330 if (unlikely(r))
10331 return;
10332
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010333 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010334 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010335 return;
10336
Sean Christopherson7a026742020-02-06 14:14:34 -080010337 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010338}
10339
Gleb Natapovaf585b92010-10-14 11:22:46 +020010340static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10341{
Peter Xudd03bca2020-04-16 11:58:59 -040010342 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
10343
Gleb Natapovaf585b92010-10-14 11:22:46 +020010344 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10345}
10346
10347static inline u32 kvm_async_pf_next_probe(u32 key)
10348{
Peter Xudd03bca2020-04-16 11:58:59 -040010349 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010350}
10351
10352static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10353{
10354 u32 key = kvm_async_pf_hash_fn(gfn);
10355
10356 while (vcpu->arch.apf.gfns[key] != ~0)
10357 key = kvm_async_pf_next_probe(key);
10358
10359 vcpu->arch.apf.gfns[key] = gfn;
10360}
10361
10362static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10363{
10364 int i;
10365 u32 key = kvm_async_pf_hash_fn(gfn);
10366
Peter Xudd03bca2020-04-16 11:58:59 -040010367 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010368 (vcpu->arch.apf.gfns[key] != gfn &&
10369 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010370 key = kvm_async_pf_next_probe(key);
10371
10372 return key;
10373}
10374
10375bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10376{
10377 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10378}
10379
10380static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10381{
10382 u32 i, j, k;
10383
10384 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040010385
10386 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
10387 return;
10388
Gleb Natapovaf585b92010-10-14 11:22:46 +020010389 while (true) {
10390 vcpu->arch.apf.gfns[i] = ~0;
10391 do {
10392 j = kvm_async_pf_next_probe(j);
10393 if (vcpu->arch.apf.gfns[j] == ~0)
10394 return;
10395 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10396 /*
10397 * k lies cyclically in ]i,j]
10398 * | i.k.j |
10399 * |....j i.k.| or |.k..j i...|
10400 */
10401 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10402 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10403 i = j;
10404 }
10405}
10406
Gleb Natapov7c907052010-10-14 11:22:53 +020010407static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10408{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010409
10410 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10411 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010412}
10413
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010414static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10415{
10416
10417 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10418 sizeof(u32));
10419}
10420
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010421static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10422{
10423 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10424 return false;
10425
10426 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10427 (vcpu->arch.apf.send_user_only &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010428 kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010429 return false;
10430
10431 return true;
10432}
10433
10434bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10435{
10436 if (unlikely(!lapic_in_kernel(vcpu) ||
10437 kvm_event_needs_reinjection(vcpu) ||
10438 vcpu->arch.exception.pending))
10439 return false;
10440
10441 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10442 return false;
10443
10444 /*
10445 * If interrupts are off we cannot even use an artificial
10446 * halt state.
10447 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010448 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010449}
10450
Gleb Natapovaf585b92010-10-14 11:22:46 +020010451void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10452 struct kvm_async_pf *work)
10453{
Avi Kivity6389ee92010-11-29 16:12:30 +020010454 struct x86_exception fault;
10455
Sean Christopherson736c2912019-12-06 15:57:14 -080010456 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010457 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010458
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010459 if (kvm_can_deliver_async_pf(vcpu) &&
10460 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010461 fault.vector = PF_VECTOR;
10462 fault.error_code_valid = true;
10463 fault.error_code = 0;
10464 fault.nested_page_fault = false;
10465 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010466 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010467 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010468 } else {
10469 /*
10470 * It is not possible to deliver a paravirtualized asynchronous
10471 * page fault, but putting the guest in an artificial halt state
10472 * can be beneficial nevertheless: if an interrupt arrives, we
10473 * can deliver it timely and perhaps the guest will schedule
10474 * another process. When the instruction that triggered a page
10475 * fault is retried, hopefully the page will be ready in the host.
10476 */
10477 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010478 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010479}
10480
10481void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10482 struct kvm_async_pf *work)
10483{
Avi Kivity6389ee92010-11-29 16:12:30 +020010484 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010485 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010486
chai wenf2e10662013-10-14 22:22:33 +080010487 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010488 work->arch.token = ~0; /* broadcast wakeup */
10489 else
10490 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010491 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010492
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010493 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10494 !apf_get_user(vcpu, &val)) {
10495 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10496 vcpu->arch.exception.pending &&
10497 vcpu->arch.exception.nr == PF_VECTOR &&
10498 !apf_put_user(vcpu, 0)) {
10499 vcpu->arch.exception.injected = false;
10500 vcpu->arch.exception.pending = false;
10501 vcpu->arch.exception.nr = 0;
10502 vcpu->arch.exception.has_error_code = false;
10503 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010504 vcpu->arch.exception.has_payload = false;
10505 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010506 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10507 fault.vector = PF_VECTOR;
10508 fault.error_code_valid = true;
10509 fault.error_code = 0;
10510 fault.nested_page_fault = false;
10511 fault.address = work->arch.token;
10512 fault.async_page_fault = true;
10513 kvm_inject_page_fault(vcpu, &fault);
10514 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010515 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010516 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010517 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010518}
10519
10520bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10521{
10522 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10523 return true;
10524 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010525 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010526}
10527
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010528void kvm_arch_start_assignment(struct kvm *kvm)
10529{
10530 atomic_inc(&kvm->arch.assigned_device_count);
10531}
10532EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10533
10534void kvm_arch_end_assignment(struct kvm *kvm)
10535{
10536 atomic_dec(&kvm->arch.assigned_device_count);
10537}
10538EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10539
10540bool kvm_arch_has_assigned_device(struct kvm *kvm)
10541{
10542 return atomic_read(&kvm->arch.assigned_device_count);
10543}
10544EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10545
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010546void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10547{
10548 atomic_inc(&kvm->arch.noncoherent_dma_count);
10549}
10550EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10551
10552void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10553{
10554 atomic_dec(&kvm->arch.noncoherent_dma_count);
10555}
10556EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10557
10558bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10559{
10560 return atomic_read(&kvm->arch.noncoherent_dma_count);
10561}
10562EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10563
Alex Williamson14717e22016-05-05 11:58:35 -060010564bool kvm_arch_has_irq_bypass(void)
10565{
Sean Christopherson92735b12019-08-02 15:06:17 -070010566 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010567}
10568
Feng Wu87276882015-09-18 22:29:40 +080010569int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10570 struct irq_bypass_producer *prod)
10571{
10572 struct kvm_kernel_irqfd *irqfd =
10573 container_of(cons, struct kvm_kernel_irqfd, consumer);
10574
Alex Williamson14717e22016-05-05 11:58:35 -060010575 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010576
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010577 return kvm_x86_ops.update_pi_irte(irqfd->kvm,
Alex Williamson14717e22016-05-05 11:58:35 -060010578 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010579}
10580
10581void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10582 struct irq_bypass_producer *prod)
10583{
10584 int ret;
10585 struct kvm_kernel_irqfd *irqfd =
10586 container_of(cons, struct kvm_kernel_irqfd, consumer);
10587
Feng Wu87276882015-09-18 22:29:40 +080010588 WARN_ON(irqfd->producer != prod);
10589 irqfd->producer = NULL;
10590
10591 /*
10592 * When producer of consumer is unregistered, we change back to
10593 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010594 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010595 * int this case doesn't want to receive the interrupts.
10596 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010597 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080010598 if (ret)
10599 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10600 " fails: %d\n", irqfd->consumer.token, ret);
10601}
10602
10603int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10604 uint32_t guest_irq, bool set)
10605{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010606 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080010607}
10608
Feng Wu520040142016-01-25 16:53:33 +080010609bool kvm_vector_hashing_enabled(void)
10610{
10611 return vector_hashing;
10612}
Feng Wu520040142016-01-25 16:53:33 +080010613
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010614bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10615{
10616 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10617}
10618EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10619
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010620u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10621{
10622 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10623
10624 /* The STIBP bit doesn't fault even if it's not advertised */
10625 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10626 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10627 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10628 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10629 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10630 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10631
10632 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10633 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10634 bits &= ~SPEC_CTRL_SSBD;
10635 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10636 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10637 bits &= ~SPEC_CTRL_SSBD;
10638
10639 return bits;
10640}
10641EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010642
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010643EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010644EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010645EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10646EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10647EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10648EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010649EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010650EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010651EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010652EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010653EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010654EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010655EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010656EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010657EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010658EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010659EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010660EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010661EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10662EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010663EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010664EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);