blob: 24de847af52ebc96a7f08b6466722270563d5c0f [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"
彭浩(Richard)88197e62020-05-21 05:57:49 +000021#include "ioapic.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080022#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080023#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020024#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030025#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -080026#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020028#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020029#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030030#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060031#include "lapic.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020032
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020033#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030034#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020035#include <linux/kvm.h>
36#include <linux/fs.h>
37#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040038#include <linux/export.h>
39#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080040#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050041#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010042#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010044#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030045#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020046#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080048#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080049#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020050#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020051#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020052#include <linux/timekeeper_internal.h>
53#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080054#include <linux/kvm_irqfd.h>
55#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010056#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080057#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050058#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010059
Avi Kivityaec51dc2009-07-01 16:01:02 +030060#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080061
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020062#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080063#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020064#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080065#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020066#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020067#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100068#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030069#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080070#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010071#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010072#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080073#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090074#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000075#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020076
Dave Hansend1898b72016-06-01 10:42:20 -070077#define CREATE_TRACE_POINTS
78#include "trace.h"
79
Carsten Otte313a3dc2007-10-11 19:16:52 +020080#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080081#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080082u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
83EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080084
Avi Kivity0f65dd72011-04-20 13:37:53 +030085#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080086 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030087
Joerg Roedel50a37eb2008-01-31 14:57:38 +010088/* EFER defaults:
89 * - enable syscall per default because its emulated by KVM
90 * - enable LME and LMA per default on 64 bit KVM
91 */
92#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080093static
94u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010095#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080096static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010097#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020098
Sean Christophersonb11306b2019-12-10 14:44:13 -080099static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
100
Radim Krčmářc5192652016-07-12 22:09:28 +0200101#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
102 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200103
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300104static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300105static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200106static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100107static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800108static void store_regs(struct kvm_vcpu *vcpu);
109static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200110
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700111struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300112EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800113
Paolo Bonzini893590c2015-11-06 11:46:24 +0100114static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030115module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200116
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200117static bool __read_mostly report_ignored_msrs = true;
118module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
119
Wanpeng Li4c276252018-05-05 04:02:32 -0700120unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200121module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
122
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300123static bool __read_mostly kvmclock_periodic_sync = true;
124module_param(kvmclock_periodic_sync, bool, S_IRUGO);
125
Paolo Bonzini893590c2015-11-06 11:46:24 +0100126bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100127EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100129EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800130u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
131EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
132u64 __read_mostly kvm_max_tsc_scaling_ratio;
133EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700134u64 __read_mostly kvm_default_tsc_scaling_ratio;
135EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100136
Zachary Amsdencc578282012-02-03 15:43:50 -0200137/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100138static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200139module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
140
Sean Christophersonc3941d92019-04-17 10:15:33 -0700141/*
142 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
143 * adaptive tuning starting from default advancment of 1000ns. '0' disables
144 * advancement entirely. Any other value is used as-is and disables adaptive
145 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
146 */
147static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800148module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500149
Feng Wu520040142016-01-25 16:53:33 +0800150static bool __read_mostly vector_hashing = true;
151module_param(vector_hashing, bool, S_IRUGO);
152
Liran Alonc4ae60e2018-03-12 13:12:47 +0200153bool __read_mostly enable_vmware_backdoor = false;
154module_param(enable_vmware_backdoor, bool, S_IRUGO);
155EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
156
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700157static bool __read_mostly force_emulation_prefix = false;
158module_param(force_emulation_prefix, bool, S_IRUGO);
159
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800160int __read_mostly pi_inject_timer = -1;
161module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
162
Avi Kivity18863bd2009-09-07 11:12:18 +0300163#define KVM_NR_SHARED_MSRS 16
164
165struct kvm_shared_msrs_global {
166 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800167 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300168};
169
170struct kvm_shared_msrs {
171 struct user_return_notifier urn;
172 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800173 struct kvm_shared_msr_values {
174 u64 host;
175 u64 curr;
176 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300177};
178
179static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200180static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300181
Sean Christophersoncfc48182020-03-02 15:56:23 -0800182#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
183 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
184 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
185 | XFEATURE_MASK_PKRU)
186
Sean Christopherson91661982020-03-02 15:57:06 -0800187u64 __read_mostly host_efer;
188EXPORT_SYMBOL_GPL(host_efer);
189
Aaron Lewis139a12c2019-10-21 16:30:25 -0700190static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100191u64 __read_mostly supported_xss;
192EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700193
Hollis Blanchard417bc302007-10-31 17:24:23 -0500194struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200195 VCPU_STAT("pf_fixed", pf_fixed),
196 VCPU_STAT("pf_guest", pf_guest),
197 VCPU_STAT("tlb_flush", tlb_flush),
198 VCPU_STAT("invlpg", invlpg),
199 VCPU_STAT("exits", exits),
200 VCPU_STAT("io_exits", io_exits),
201 VCPU_STAT("mmio_exits", mmio_exits),
202 VCPU_STAT("signal_exits", signal_exits),
203 VCPU_STAT("irq_window", irq_window_exits),
204 VCPU_STAT("nmi_window", nmi_window_exits),
205 VCPU_STAT("halt_exits", halt_exits),
206 VCPU_STAT("halt_successful_poll", halt_successful_poll),
207 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
208 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
209 VCPU_STAT("halt_wakeup", halt_wakeup),
210 VCPU_STAT("hypercalls", hypercalls),
211 VCPU_STAT("request_irq", request_irq_exits),
212 VCPU_STAT("irq_exits", irq_exits),
213 VCPU_STAT("host_state_reload", host_state_reload),
214 VCPU_STAT("fpu_reload", fpu_reload),
215 VCPU_STAT("insn_emulation", insn_emulation),
216 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
217 VCPU_STAT("irq_injections", irq_injections),
218 VCPU_STAT("nmi_injections", nmi_injections),
219 VCPU_STAT("req_event", req_event),
220 VCPU_STAT("l1d_flush", l1d_flush),
David Matlackcb953122020-05-08 11:22:40 -0700221 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
222 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200223 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
224 VM_STAT("mmu_pte_write", mmu_pte_write),
225 VM_STAT("mmu_pte_updated", mmu_pte_updated),
226 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
227 VM_STAT("mmu_flooded", mmu_flooded),
228 VM_STAT("mmu_recycled", mmu_recycled),
229 VM_STAT("mmu_cache_miss", mmu_cache_miss),
230 VM_STAT("mmu_unsync", mmu_unsync),
231 VM_STAT("remote_tlb_flush", remote_tlb_flush),
232 VM_STAT("largepages", lpages, .mode = 0444),
233 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
234 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500235 { NULL }
236};
237
Dexuan Cui2acf9232010-06-10 11:27:12 +0800238u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800239u64 __read_mostly supported_xcr0;
240EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800241
Marc Orrb666a4b2018-11-06 14:53:56 -0800242struct kmem_cache *x86_fpu_cache;
243EXPORT_SYMBOL_GPL(x86_fpu_cache);
244
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800245static struct kmem_cache *x86_emulator_cache;
246
247static struct kmem_cache *kvm_alloc_emulator_cache(void)
248{
Sean Christopherson06add252020-02-18 15:29:50 -0800249 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
250 unsigned int size = sizeof(struct x86_emulate_ctxt);
251
252 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800253 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800254 SLAB_ACCOUNT, useroffset,
255 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800256}
257
Jan Kiszkab6785de2012-09-20 07:43:17 +0200258static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300259
Gleb Natapovaf585b92010-10-14 11:22:46 +0200260static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
261{
262 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400263 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200264 vcpu->arch.apf.gfns[i] = ~0;
265}
266
Avi Kivity18863bd2009-09-07 11:12:18 +0300267static void kvm_on_user_return(struct user_return_notifier *urn)
268{
269 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300270 struct kvm_shared_msrs *locals
271 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800272 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700273 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300274
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700275 /*
276 * Disabling irqs at this point since the following code could be
277 * interrupted and executed through kvm_arch_hardware_disable()
278 */
279 local_irq_save(flags);
280 if (locals->registered) {
281 locals->registered = false;
282 user_return_notifier_unregister(urn);
283 }
284 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300285 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800286 values = &locals->values[slot];
287 if (values->host != values->curr) {
288 wrmsrl(shared_msrs_global.msrs[slot], values->host);
289 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300290 }
291 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300292}
293
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800294void kvm_define_shared_msr(unsigned slot, u32 msr)
295{
Nadav Amit0123be42014-07-24 15:06:56 +0300296 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200297 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300298 if (slot >= shared_msrs_global.nr)
299 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300300}
301EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
302
303static void kvm_shared_msr_cpu_online(void)
304{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800305 unsigned int cpu = smp_processor_id();
306 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
307 u64 value;
308 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300309
Sean Christopherson05c19c22019-11-22 12:04:50 -0800310 for (i = 0; i < shared_msrs_global.nr; ++i) {
311 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
312 smsr->values[i].host = value;
313 smsr->values[i].curr = value;
314 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300315}
316
Andy Honig8b3c3102014-08-27 11:16:44 -0700317int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300318{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200319 unsigned int cpu = smp_processor_id();
320 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700321 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300322
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500323 value = (value & mask) | (smsr->values[slot].host & ~mask);
324 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700325 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700326 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
327 if (err)
328 return 1;
329
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500330 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300331 if (!smsr->registered) {
332 smsr->urn.on_user_return = kvm_on_user_return;
333 user_return_notifier_register(&smsr->urn);
334 smsr->registered = true;
335 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700336 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300337}
338EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
339
Radim Krčmář13a34e02014-08-28 15:13:03 +0200340static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200341{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200342 unsigned int cpu = smp_processor_id();
343 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200344
345 if (smsr->registered)
346 kvm_on_user_return(&smsr->urn);
347}
348
Carsten Otte6866b832007-10-29 16:09:10 +0100349u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
350{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300351 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100352}
353EXPORT_SYMBOL_GPL(kvm_get_apic_base);
354
Jim Mattson58871642018-05-09 16:56:04 -0400355enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
356{
357 return kvm_apic_mode(kvm_get_apic_base(vcpu));
358}
359EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
360
Jan Kiszka58cb6282014-01-24 16:48:44 +0100361int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100362{
Jim Mattson58871642018-05-09 16:56:04 -0400363 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
364 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200365 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
366 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100367
Jim Mattson58871642018-05-09 16:56:04 -0400368 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700369 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400370 if (!msr_info->host_initiated) {
371 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
372 return 1;
373 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
374 return 1;
375 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100376
377 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800378 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100379 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100380}
381EXPORT_SYMBOL_GPL(kvm_set_apic_base);
382
Andi Kleen2605fc22014-05-02 00:44:37 +0200383asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000384{
385 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200386 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000387}
388EXPORT_SYMBOL_GPL(kvm_spurious_fault);
389
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200390#define EXCPT_BENIGN 0
391#define EXCPT_CONTRIBUTORY 1
392#define EXCPT_PF 2
393
394static int exception_class(int vector)
395{
396 switch (vector) {
397 case PF_VECTOR:
398 return EXCPT_PF;
399 case DE_VECTOR:
400 case TS_VECTOR:
401 case NP_VECTOR:
402 case SS_VECTOR:
403 case GP_VECTOR:
404 return EXCPT_CONTRIBUTORY;
405 default:
406 break;
407 }
408 return EXCPT_BENIGN;
409}
410
Nadav Amitd6e8c852014-07-24 14:51:24 +0300411#define EXCPT_FAULT 0
412#define EXCPT_TRAP 1
413#define EXCPT_ABORT 2
414#define EXCPT_INTERRUPT 3
415
416static int exception_type(int vector)
417{
418 unsigned int mask;
419
420 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
421 return EXCPT_INTERRUPT;
422
423 mask = 1 << vector;
424
425 /* #DB is trap, as instruction watchpoints are handled elsewhere */
426 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
427 return EXCPT_TRAP;
428
429 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
430 return EXCPT_ABORT;
431
432 /* Reserved exceptions will result in fault */
433 return EXCPT_FAULT;
434}
435
Jim Mattsonda998b42018-10-16 14:29:22 -0700436void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
437{
438 unsigned nr = vcpu->arch.exception.nr;
439 bool has_payload = vcpu->arch.exception.has_payload;
440 unsigned long payload = vcpu->arch.exception.payload;
441
442 if (!has_payload)
443 return;
444
445 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700446 case DB_VECTOR:
447 /*
448 * "Certain debug exceptions may clear bit 0-3. The
449 * remaining contents of the DR6 register are never
450 * cleared by the processor".
451 */
452 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
453 /*
454 * DR6.RTM is set by all #DB exceptions that don't clear it.
455 */
456 vcpu->arch.dr6 |= DR6_RTM;
457 vcpu->arch.dr6 |= payload;
458 /*
459 * Bit 16 should be set in the payload whenever the #DB
460 * exception should clear DR6.RTM. This makes the payload
461 * compatible with the pending debug exceptions under VMX.
462 * Though not currently documented in the SDM, this also
463 * makes the payload compatible with the exit qualification
464 * for #DB exceptions under VMX.
465 */
466 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800467
468 /*
469 * The #DB payload is defined as compatible with the 'pending
470 * debug exceptions' field under VMX, not DR6. While bit 12 is
471 * defined in the 'pending debug exceptions' field (enabled
472 * breakpoint), it is reserved and must be zero in DR6.
473 */
474 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700475 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700476 case PF_VECTOR:
477 vcpu->arch.cr2 = payload;
478 break;
479 }
480
481 vcpu->arch.exception.has_payload = false;
482 vcpu->arch.exception.payload = 0;
483}
484EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
485
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200486static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200487 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700488 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200489{
490 u32 prev_nr;
491 int class1, class2;
492
Avi Kivity3842d132010-07-27 12:30:24 +0300493 kvm_make_request(KVM_REQ_EVENT, vcpu);
494
Wanpeng Li664f8e22017-08-24 03:35:09 -0700495 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200496 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200497 if (has_error && !is_protmode(vcpu))
498 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700499 if (reinject) {
500 /*
501 * On vmentry, vcpu->arch.exception.pending is only
502 * true if an event injection was blocked by
503 * nested_run_pending. In that case, however,
504 * vcpu_enter_guest requests an immediate exit,
505 * and the guest shouldn't proceed far enough to
506 * need reinjection.
507 */
508 WARN_ON_ONCE(vcpu->arch.exception.pending);
509 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700510 if (WARN_ON_ONCE(has_payload)) {
511 /*
512 * A reinjected event has already
513 * delivered its payload.
514 */
515 has_payload = false;
516 payload = 0;
517 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700518 } else {
519 vcpu->arch.exception.pending = true;
520 vcpu->arch.exception.injected = false;
521 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200522 vcpu->arch.exception.has_error_code = has_error;
523 vcpu->arch.exception.nr = nr;
524 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700525 vcpu->arch.exception.has_payload = has_payload;
526 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800527 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700528 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200529 return;
530 }
531
532 /* to check exception */
533 prev_nr = vcpu->arch.exception.nr;
534 if (prev_nr == DF_VECTOR) {
535 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300536 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200537 return;
538 }
539 class1 = exception_class(prev_nr);
540 class2 = exception_class(nr);
541 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
542 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700543 /*
544 * Generate double fault per SDM Table 5-5. Set
545 * exception.pending = true so that the double fault
546 * can trigger a nested vmexit.
547 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200548 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700549 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200550 vcpu->arch.exception.has_error_code = true;
551 vcpu->arch.exception.nr = DF_VECTOR;
552 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700553 vcpu->arch.exception.has_payload = false;
554 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200555 } else
556 /* replace previous exception with a new one in a hope
557 that instruction re-execution will regenerate lost
558 exception */
559 goto queue;
560}
561
Avi Kivity298101d2007-11-25 13:41:11 +0200562void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
563{
Jim Mattson91e86d22018-10-16 14:29:21 -0700564 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200565}
566EXPORT_SYMBOL_GPL(kvm_queue_exception);
567
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200568void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
569{
Jim Mattson91e86d22018-10-16 14:29:21 -0700570 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200571}
572EXPORT_SYMBOL_GPL(kvm_requeue_exception);
573
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400574void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
575 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700576{
577 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
578}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400579EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700580
Jim Mattsonda998b42018-10-16 14:29:22 -0700581static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
582 u32 error_code, unsigned long payload)
583{
584 kvm_multiple_exception(vcpu, nr, true, error_code,
585 true, payload, false);
586}
587
Kyle Huey6affcbe2016-11-29 12:40:40 -0800588int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200589{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100590 if (err)
591 kvm_inject_gp(vcpu, 0);
592 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800593 return kvm_skip_emulated_instruction(vcpu);
594
595 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100596}
597EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200598
Avi Kivity6389ee92010-11-29 16:12:30 +0200599void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200600{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200601 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700602 vcpu->arch.exception.nested_apf =
603 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700604 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700605 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700606 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
607 } else {
608 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
609 fault->address);
610 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200611}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300612EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200613
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700614bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
615 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200616{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400617 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700618 WARN_ON_ONCE(fault->vector != PF_VECTOR);
619
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400620 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
621 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200622
Junaid Shahidee1fa202020-03-20 14:28:03 -0700623 /*
624 * Invalidate the TLB entry for the faulting address, if it exists,
625 * else the access will fault indefinitely (and to emulate hardware).
626 */
627 if ((fault->error_code & PFERR_PRESENT_MASK) &&
628 !(fault->error_code & PFERR_RSVD_MASK))
629 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
630 fault_mmu->root_hpa);
631
632 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200633 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200634}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700635EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200636
Sheng Yang3419ffc2008-05-15 09:52:48 +0800637void kvm_inject_nmi(struct kvm_vcpu *vcpu)
638{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300639 atomic_inc(&vcpu->arch.nmi_queued);
640 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800641}
642EXPORT_SYMBOL_GPL(kvm_inject_nmi);
643
Avi Kivity298101d2007-11-25 13:41:11 +0200644void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
645{
Jim Mattson91e86d22018-10-16 14:29:21 -0700646 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200647}
648EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
649
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200650void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
651{
Jim Mattson91e86d22018-10-16 14:29:21 -0700652 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200653}
654EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
655
Carsten Ottea03490e2007-10-29 16:09:35 +0100656/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300657 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
658 * a #GP and return false.
659 */
660bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200661{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700662 if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300663 return true;
664 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
665 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100666}
Avi Kivity0a79b002009-09-01 12:03:25 +0300667EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100668
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300669bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
670{
671 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
672 return true;
673
674 kvm_queue_exception(vcpu, UD_VECTOR);
675 return false;
676}
677EXPORT_SYMBOL_GPL(kvm_require_dr);
678
Carsten Ottea03490e2007-10-29 16:09:35 +0100679/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200680 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200681 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200682 * can read from guest physical or from the guest's guest physical memory.
683 */
684int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
685 gfn_t ngfn, void *data, int offset, int len,
686 u32 access)
687{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200688 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200689 gfn_t real_gfn;
690 gpa_t ngpa;
691
692 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200693 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200694 if (real_gfn == UNMAPPED_GVA)
695 return -EFAULT;
696
697 real_gfn = gpa_to_gfn(real_gfn);
698
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200699 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200700}
701EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
702
Fengguang Wu69b00492015-01-19 22:33:39 +0800703static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200704 void *data, int offset, int len, u32 access)
705{
706 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
707 data, offset, len, access);
708}
709
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700710static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
711{
712 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
713 rsvd_bits(1, 2);
714}
715
Joerg Roedelec92fe42010-09-10 17:30:51 +0200716/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700717 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100718 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200719int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100720{
721 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
722 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
723 int i;
724 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200725 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100726
Joerg Roedelff03a072010-09-10 17:30:57 +0200727 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
728 offset * sizeof(u64), sizeof(pdpte),
729 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100730 if (ret < 0) {
731 ret = 0;
732 goto out;
733 }
734 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400735 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700736 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100737 ret = 0;
738 goto out;
739 }
740 }
741 ret = 1;
742
Joerg Roedelff03a072010-09-10 17:30:57 +0200743 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700744 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
745
Carsten Ottea03490e2007-10-29 16:09:35 +0100746out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100747
748 return ret;
749}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100750EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100751
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100752bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200753{
Joerg Roedelff03a072010-09-10 17:30:57 +0200754 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200755 int offset;
756 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200757 int r;
758
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200759 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200760 return false;
761
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700762 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300763 return true;
764
Paolo Bonzinia5121772017-07-24 18:54:38 +0200765 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
766 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200767 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
768 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200769 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800770 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200771
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800772 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200773}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100774EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200775
Avi Kivity49a9b072010-06-10 17:02:14 +0300776int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100777{
Sheng Yangaad82702010-05-12 16:40:42 +0800778 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800779 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800780
Avi Kivityf9a48e62010-01-06 19:10:22 +0200781 cr0 |= X86_CR0_ET;
782
Gleb Natapovab344822010-01-21 15:28:46 +0200783#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300784 if (cr0 & 0xffffffff00000000UL)
785 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200786#endif
787
788 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100789
Gleb Natapov0f122442010-04-28 19:15:31 +0300790 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
791 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100792
Gleb Natapov0f122442010-04-28 19:15:31 +0300793 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
794 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100795
796 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
797#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200798 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100799 int cs_db, cs_l;
800
Gleb Natapov0f122442010-04-28 19:15:31 +0300801 if (!is_pae(vcpu))
802 return 1;
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700803 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300804 if (cs_l)
805 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100806 } else
807#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200808 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200809 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300810 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100811 }
812
Mao, Junjiead756a12012-07-02 01:18:48 +0000813 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
814 return 1;
815
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700816 kvm_x86_ops.set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100817
Lai Jiangshand170c412011-02-21 11:21:30 +0800818 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800819 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800820 kvm_async_pf_hash_reset(vcpu);
821 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800822
Sheng Yangaad82702010-05-12 16:40:42 +0800823 if ((cr0 ^ old_cr0) & update_bits)
824 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800825
Laszlo Ersek879ae182015-11-04 12:54:41 +0100826 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
827 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
828 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800829 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
830
Gleb Natapov0f122442010-04-28 19:15:31 +0300831 return 0;
832}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200833EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100834
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200835void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100836{
Avi Kivity49a9b072010-06-10 17:02:14 +0300837 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100838}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200839EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100840
Aaron Lewis139a12c2019-10-21 16:30:25 -0700841void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300842{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700843 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300844
Aaron Lewis139a12c2019-10-21 16:30:25 -0700845 if (vcpu->arch.xcr0 != host_xcr0)
846 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
847
848 if (vcpu->arch.xsaves_enabled &&
849 vcpu->arch.ia32_xss != host_xss)
850 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
851 }
Babu Moger37486132020-05-12 18:59:06 -0500852
853 if (static_cpu_has(X86_FEATURE_PKU) &&
854 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
855 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
856 vcpu->arch.pkru != vcpu->arch.host_pkru)
857 __write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300858}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700859EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
860
861void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
862{
Babu Moger37486132020-05-12 18:59:06 -0500863 if (static_cpu_has(X86_FEATURE_PKU) &&
864 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
865 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
866 vcpu->arch.pkru = rdpkru();
867 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
868 __write_pkru(vcpu->arch.host_pkru);
869 }
870
Aaron Lewis139a12c2019-10-21 16:30:25 -0700871 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
872
873 if (vcpu->arch.xcr0 != host_xcr0)
874 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
875
876 if (vcpu->arch.xsaves_enabled &&
877 vcpu->arch.ia32_xss != host_xss)
878 wrmsrl(MSR_IA32_XSS, host_xss);
879 }
880
881}
882EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300883
Fengguang Wu69b00492015-01-19 22:33:39 +0800884static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800885{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000886 u64 xcr0 = xcr;
887 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200888 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800889
890 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
891 if (index != XCR_XFEATURE_ENABLED_MASK)
892 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700893 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800894 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700895 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800896 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200897
898 /*
899 * Do not allow the guest to set bits that we do not support
900 * saving. However, xcr0 bit 0 is always set, even if the
901 * emulated CPU does not support XSAVE (see fx_init).
902 */
Dave Hansend91cab72015-09-02 16:31:26 -0700903 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200904 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800905 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200906
Dave Hansend91cab72015-09-02 16:31:26 -0700907 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
908 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000909 return 1;
910
Dave Hansend91cab72015-09-02 16:31:26 -0700911 if (xcr0 & XFEATURE_MASK_AVX512) {
912 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800913 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700914 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800915 return 1;
916 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800917 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000918
Dave Hansend91cab72015-09-02 16:31:26 -0700919 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000920 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800921 return 0;
922}
923
924int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
925{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700926 if (kvm_x86_ops.get_cpl(vcpu) != 0 ||
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000927 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800928 kvm_inject_gp(vcpu, 0);
929 return 1;
930 }
931 return 0;
932}
933EXPORT_SYMBOL_GPL(kvm_set_xcr);
934
Sean Christopherson345599f2019-12-10 14:44:16 -0800935#define __cr4_reserved_bits(__cpu_has, __c) \
936({ \
937 u64 __reserved_bits = CR4_RESERVED_BITS; \
938 \
939 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
940 __reserved_bits |= X86_CR4_OSXSAVE; \
941 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
942 __reserved_bits |= X86_CR4_SMEP; \
943 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
944 __reserved_bits |= X86_CR4_SMAP; \
945 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
946 __reserved_bits |= X86_CR4_FSGSBASE; \
947 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
948 __reserved_bits |= X86_CR4_PKE; \
949 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
950 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800951 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
952 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800953 __reserved_bits; \
954})
955
Wanpeng Li3ca94192019-09-18 17:50:10 +0800956static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
957{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800958 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800959 return -EINVAL;
960
Sean Christopherson345599f2019-12-10 14:44:16 -0800961 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800962 return -EINVAL;
963
964 return 0;
965}
966
Avi Kivitya83b29c2010-06-10 17:02:15 +0300967int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100968{
Avi Kivityfc78f512009-12-07 12:16:48 +0200969 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800970 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800971 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800972
Wanpeng Li3ca94192019-09-18 17:50:10 +0800973 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200974 return 1;
975
Carsten Ottea03490e2007-10-29 16:09:35 +0100976 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300977 if (!(cr4 & X86_CR4_PAE))
978 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300979 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
980 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200981 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
982 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300983 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100984
Mao, Junjiead756a12012-07-02 01:18:48 +0000985 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200986 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000987 return 1;
988
989 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
990 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
991 return 1;
992 }
993
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700994 if (kvm_x86_ops.set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300995 return 1;
996
Mao, Junjiead756a12012-07-02 01:18:48 +0000997 if (((cr4 ^ old_cr4) & pdptr_bits) ||
998 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800999 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +03001000
Huaitong Hanb9baba82016-03-22 16:51:21 +08001001 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +02001002 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001003
Gleb Natapov0f122442010-04-28 19:15:31 +03001004 return 0;
1005}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001006EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001007
Avi Kivity23902182010-06-10 17:02:16 +03001008int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001009{
Junaid Shahidade61e22018-06-27 14:59:15 -07001010 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001011#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001012 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1013
Junaid Shahidade61e22018-06-27 14:59:15 -07001014 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001015 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1016 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001017 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001018#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001019
Avi Kivity9f8fe502010-12-05 17:30:00 +02001020 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001021 if (!skip_tlb_flush) {
1022 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001023 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001024 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001025 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001026 }
1027
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001028 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001029 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001030 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001031 else if (is_pae_paging(vcpu) &&
1032 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001033 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001034
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001035 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001036 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001037 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001038
Gleb Natapov0f122442010-04-28 19:15:31 +03001039 return 0;
1040}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001041EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001042
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001043int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001044{
Gleb Natapov0f122442010-04-28 19:15:31 +03001045 if (cr8 & CR8_RESERVED_BITS)
1046 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001047 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001048 kvm_lapic_set_tpr(vcpu, cr8);
1049 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001050 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001051 return 0;
1052}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001053EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001054
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001055unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001056{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001057 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001058 return kvm_lapic_get_cr8(vcpu);
1059 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001060 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001061}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001062EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001063
Nadav Amitae561ed2015-04-02 03:10:37 +03001064static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1065{
1066 int i;
1067
1068 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1069 for (i = 0; i < KVM_NR_DB_REGS; i++)
1070 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1071 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1072 }
1073}
1074
Paolo Bonzini7c866632020-05-16 08:42:28 -04001075void kvm_update_dr7(struct kvm_vcpu *vcpu)
Jan Kiszkac8639012012-09-21 05:42:55 +02001076{
1077 unsigned long dr7;
1078
1079 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1080 dr7 = vcpu->arch.guest_debug_dr7;
1081 else
1082 dr7 = vcpu->arch.dr7;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001083 kvm_x86_ops.set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001084 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1085 if (dr7 & DR7_BP_EN_MASK)
1086 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001087}
Paolo Bonzini7c866632020-05-16 08:42:28 -04001088EXPORT_SYMBOL_GPL(kvm_update_dr7);
Jan Kiszkac8639012012-09-21 05:42:55 +02001089
Nadav Amit6f43ed02014-07-15 17:37:46 +03001090static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1091{
1092 u64 fixed = DR6_FIXED_1;
1093
Radim Krčmářd6321d42017-08-05 00:12:49 +02001094 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001095 fixed |= DR6_RTM;
1096 return fixed;
1097}
1098
Gleb Natapov338dbc92010-04-28 19:15:32 +03001099static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001100{
Marios Pomonisea740052019-12-11 12:47:52 -08001101 size_t size = ARRAY_SIZE(vcpu->arch.db);
1102
Gleb Natapov020df072010-04-13 10:05:23 +03001103 switch (dr) {
1104 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001105 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001106 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1107 vcpu->arch.eff_db[dr] = val;
1108 break;
1109 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001110 /* fall through */
1111 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001112 if (val & 0xffffffff00000000ULL)
1113 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001114 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001115 break;
1116 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001117 /* fall through */
1118 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001119 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001120 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001121 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001122 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001123 break;
1124 }
1125
1126 return 0;
1127}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001128
1129int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1130{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001131 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001132 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001133 return 1;
1134 }
1135 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001136}
Gleb Natapov020df072010-04-13 10:05:23 +03001137EXPORT_SYMBOL_GPL(kvm_set_dr);
1138
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001139int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001140{
Marios Pomonisea740052019-12-11 12:47:52 -08001141 size_t size = ARRAY_SIZE(vcpu->arch.db);
1142
Gleb Natapov020df072010-04-13 10:05:23 +03001143 switch (dr) {
1144 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001145 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001146 break;
1147 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001148 /* fall through */
1149 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001150 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001151 break;
1152 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001153 /* fall through */
1154 default: /* 7 */
1155 *val = vcpu->arch.dr7;
1156 break;
1157 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001158 return 0;
1159}
Gleb Natapov020df072010-04-13 10:05:23 +03001160EXPORT_SYMBOL_GPL(kvm_get_dr);
1161
Avi Kivity022cd0e2011-11-10 14:57:23 +02001162bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1163{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001164 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001165 u64 data;
1166 int err;
1167
Wei Huangc6702c92015-06-19 13:44:45 +02001168 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001169 if (err)
1170 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001171 kvm_rax_write(vcpu, (u32)data);
1172 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001173 return err;
1174}
1175EXPORT_SYMBOL_GPL(kvm_rdpmc);
1176
Carsten Otte043405e2007-10-10 17:16:19 +02001177/*
1178 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1179 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1180 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001181 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1182 * extract the supported MSRs from the related const lists.
1183 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001184 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001185 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001186 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001187 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001188
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001189static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001190 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001191 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001192#ifdef CONFIG_X86_64
1193 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1194#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001195 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001196 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001197 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001198 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1199 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1200 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1201 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1202 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1203 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001204 MSR_IA32_UMWAIT_CONTROL,
1205
Jim Mattsone2ada662019-08-21 11:20:04 -07001206 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1207 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1208 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1209 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1210 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1212 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1213 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1214 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1215 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1216 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1217 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1218 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001219 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1221 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1222 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1223 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1224 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1225 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1226 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1227 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001228};
1229
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001230static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001231static unsigned num_msrs_to_save;
1232
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001233static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001234 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1235 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1236 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1237 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001238 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001239 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1240 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001241 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001242 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001243 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001244 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001245 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001246 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001247 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1248 HV_X64_MSR_TSC_EMULATION_STATUS,
Jon Doronf97f5a52020-05-29 16:45:40 +03001249 HV_X64_MSR_SYNDBG_OPTIONS,
1250 HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS,
1251 HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER,
1252 HV_X64_MSR_SYNDBG_PENDING_BUFFER,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001253
1254 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02001255 MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001256
Will Auldba904632012-11-29 12:42:50 -08001257 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001258 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001259 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001260 MSR_IA32_PERF_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001261 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001262 MSR_IA32_MCG_STATUS,
1263 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001264 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001265 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001266 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001267 MSR_PLATFORM_INFO,
1268 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001269 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001270 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001271 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001272
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001273 /*
1274 * The following list leaves out MSRs whose values are determined
1275 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1276 * We always support the "true" VMX control MSRs, even if the host
1277 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001278 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001279 */
1280 MSR_IA32_VMX_BASIC,
1281 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1282 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1283 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1284 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1285 MSR_IA32_VMX_MISC,
1286 MSR_IA32_VMX_CR0_FIXED0,
1287 MSR_IA32_VMX_CR4_FIXED0,
1288 MSR_IA32_VMX_VMCS_ENUM,
1289 MSR_IA32_VMX_PROCBASED_CTLS2,
1290 MSR_IA32_VMX_EPT_VPID_CAP,
1291 MSR_IA32_VMX_VMFUNC,
1292
Borislav Petkov191c8132019-04-18 18:32:50 +02001293 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001294 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001295};
1296
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001297static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001298static unsigned num_emulated_msrs;
1299
Tom Lendacky801e4592018-02-21 13:39:51 -06001300/*
1301 * List of msr numbers which are used to expose MSR-based features that
1302 * can be used by a hypervisor to validate requested CPU features.
1303 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001304static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001305 MSR_IA32_VMX_BASIC,
1306 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1307 MSR_IA32_VMX_PINBASED_CTLS,
1308 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1309 MSR_IA32_VMX_PROCBASED_CTLS,
1310 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1311 MSR_IA32_VMX_EXIT_CTLS,
1312 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1313 MSR_IA32_VMX_ENTRY_CTLS,
1314 MSR_IA32_VMX_MISC,
1315 MSR_IA32_VMX_CR0_FIXED0,
1316 MSR_IA32_VMX_CR0_FIXED1,
1317 MSR_IA32_VMX_CR4_FIXED0,
1318 MSR_IA32_VMX_CR4_FIXED1,
1319 MSR_IA32_VMX_VMCS_ENUM,
1320 MSR_IA32_VMX_PROCBASED_CTLS2,
1321 MSR_IA32_VMX_EPT_VPID_CAP,
1322 MSR_IA32_VMX_VMFUNC,
1323
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001324 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001325 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001326 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001327 MSR_IA32_PERF_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001328};
1329
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001330static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001331static unsigned int num_msr_based_features;
1332
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001333static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001334{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001335 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001336
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001337 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1338 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001339
1340 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001341 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1342 * the nested hypervisor runs with NX huge pages. If it is not,
1343 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1344 * L1 guests, so it need not worry about its own (L2) guests.
1345 */
1346 data |= ARCH_CAP_PSCHANGE_MC_NO;
1347
1348 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001349 * If we're doing cache flushes (either "always" or "cond")
1350 * we will do one whenever the guest does a vmlaunch/vmresume.
1351 * If an outer hypervisor is doing the cache flush for us
1352 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1353 * capability to the guest too, and if EPT is disabled we're not
1354 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1355 * require a nested hypervisor to do a flush of its own.
1356 */
1357 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1358 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1359
Paolo Bonzini0c549142019-08-19 17:24:07 +02001360 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1361 data |= ARCH_CAP_RDCL_NO;
1362 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1363 data |= ARCH_CAP_SSB_NO;
1364 if (!boot_cpu_has_bug(X86_BUG_MDS))
1365 data |= ARCH_CAP_MDS_NO;
1366
Pawan Guptae1d38b62019-10-23 12:23:33 +02001367 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001368 * On TAA affected systems:
1369 * - nothing to do if TSX is disabled on the host.
1370 * - we emulate TSX_CTRL if present on the host.
1371 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001372 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001373 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001374 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001375 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1376 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001377
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001378 return data;
1379}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001380
Wanpeng Li66421c12018-02-28 14:03:30 +08001381static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1382{
1383 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001384 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001385 msr->data = kvm_get_arch_capabilities();
1386 break;
1387 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001388 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001389 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001390 default:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001391 if (kvm_x86_ops.get_msr_feature(msr))
Wanpeng Li66421c12018-02-28 14:03:30 +08001392 return 1;
1393 }
1394 return 0;
1395}
1396
Tom Lendacky801e4592018-02-21 13:39:51 -06001397static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1398{
1399 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001400 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001401
1402 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001403 r = kvm_get_msr_feature(&msr);
1404 if (r)
1405 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001406
1407 *data = msr.data;
1408
1409 return 0;
1410}
1411
Sean Christopherson11988492019-04-02 08:19:15 -07001412static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1413{
1414 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1415 return false;
1416
1417 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1418 return false;
1419
Sean Christopherson0a629562019-04-02 08:19:16 -07001420 if (efer & (EFER_LME | EFER_LMA) &&
1421 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1422 return false;
1423
1424 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1425 return false;
1426
Sean Christopherson11988492019-04-02 08:19:15 -07001427 return true;
1428
1429}
Jan Kiszka384bb782013-04-20 10:52:36 +02001430bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001431{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001432 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001433 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001434
Sean Christopherson11988492019-04-02 08:19:15 -07001435 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001436}
1437EXPORT_SYMBOL_GPL(kvm_valid_efer);
1438
Sean Christopherson11988492019-04-02 08:19:15 -07001439static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001440{
1441 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001442 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001443
Sean Christopherson11988492019-04-02 08:19:15 -07001444 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001445 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001446
Sean Christopherson11988492019-04-02 08:19:15 -07001447 if (!msr_info->host_initiated) {
1448 if (!__kvm_valid_efer(vcpu, efer))
1449 return 1;
1450
1451 if (is_paging(vcpu) &&
1452 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1453 return 1;
1454 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001455
Carsten Otte15c4a642007-10-30 18:44:17 +01001456 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001457 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001458
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001459 kvm_x86_ops.set_efer(vcpu, efer);
Sheng Yanga3d204e2010-05-12 16:40:40 +08001460
Sheng Yangaad82702010-05-12 16:40:42 +08001461 /* Update reserved bits */
1462 if ((efer ^ old_efer) & EFER_NX)
1463 kvm_mmu_reset_context(vcpu);
1464
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001465 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001466}
1467
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001468void kvm_enable_efer_bits(u64 mask)
1469{
1470 efer_reserved_bits &= ~mask;
1471}
1472EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1473
Carsten Otte15c4a642007-10-30 18:44:17 +01001474/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001475 * Write @data into the MSR specified by @index. Select MSR specific fault
1476 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001477 * Returns 0 on success, non-0 otherwise.
1478 * Assumes vcpu_load() was already called.
1479 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001480static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1481 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001482{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001483 struct msr_data msr;
1484
1485 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001486 case MSR_FS_BASE:
1487 case MSR_GS_BASE:
1488 case MSR_KERNEL_GS_BASE:
1489 case MSR_CSTAR:
1490 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001491 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001492 return 1;
1493 break;
1494 case MSR_IA32_SYSENTER_EIP:
1495 case MSR_IA32_SYSENTER_ESP:
1496 /*
1497 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1498 * non-canonical address is written on Intel but not on
1499 * AMD (which ignores the top 32-bits, because it does
1500 * not implement 64-bit SYSENTER).
1501 *
1502 * 64-bit code should hence be able to write a non-canonical
1503 * value on AMD. Making the address canonical ensures that
1504 * vmentry does not fail on Intel after writing a non-canonical
1505 * value, and that something deterministic happens if the guest
1506 * invokes 64-bit SYSENTER.
1507 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001508 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001509 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001510
1511 msr.data = data;
1512 msr.index = index;
1513 msr.host_initiated = host_initiated;
1514
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001515 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001516}
1517
1518/*
1519 * Read the MSR specified by @index into @data. Select MSR specific fault
1520 * checks are bypassed if @host_initiated is %true.
1521 * Returns 0 on success, non-0 otherwise.
1522 * Assumes vcpu_load() was already called.
1523 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001524int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1525 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001526{
1527 struct msr_data msr;
1528 int ret;
1529
1530 msr.index = index;
1531 msr.host_initiated = host_initiated;
1532
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001533 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001534 if (!ret)
1535 *data = msr.data;
1536 return ret;
1537}
1538
1539int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1540{
1541 return __kvm_get_msr(vcpu, index, data, false);
1542}
1543EXPORT_SYMBOL_GPL(kvm_get_msr);
1544
1545int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1546{
1547 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001548}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001549EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001550
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001551int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1552{
1553 u32 ecx = kvm_rcx_read(vcpu);
1554 u64 data;
1555
1556 if (kvm_get_msr(vcpu, ecx, &data)) {
1557 trace_kvm_msr_read_ex(ecx);
1558 kvm_inject_gp(vcpu, 0);
1559 return 1;
1560 }
1561
1562 trace_kvm_msr_read(ecx, data);
1563
1564 kvm_rax_write(vcpu, data & -1u);
1565 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1566 return kvm_skip_emulated_instruction(vcpu);
1567}
1568EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1569
1570int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1571{
1572 u32 ecx = kvm_rcx_read(vcpu);
1573 u64 data = kvm_read_edx_eax(vcpu);
1574
1575 if (kvm_set_msr(vcpu, ecx, data)) {
1576 trace_kvm_msr_write_ex(ecx, data);
1577 kvm_inject_gp(vcpu, 0);
1578 return 1;
1579 }
1580
1581 trace_kvm_msr_write(ecx, data);
1582 return kvm_skip_emulated_instruction(vcpu);
1583}
1584EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1585
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001586bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
1587{
1588 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
1589 need_resched() || signal_pending(current);
1590}
1591EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request);
1592
Carsten Otte313a3dc2007-10-11 19:16:52 +02001593/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001594 * The fast path for frequent and performance sensitive wrmsr emulation,
1595 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1596 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1597 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1598 * other cases which must be called after interrupts are enabled on the host.
1599 */
1600static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1601{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001602 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1603 return 1;
1604
1605 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001606 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001607 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1608 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001609
Wanpeng Lid5361672020-03-26 10:20:02 +08001610 data &= ~(1 << 12);
1611 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001612 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001613 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1614 trace_kvm_apic_write(APIC_ICR, (u32)data);
1615 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001616 }
1617
1618 return 1;
1619}
1620
Wanpeng Liae95f562020-04-28 14:23:28 +08001621static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1622{
1623 if (!kvm_can_use_hv_timer(vcpu))
1624 return 1;
1625
1626 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1627 return 0;
1628}
1629
Wanpeng Li404d5d72020-04-28 14:23:25 +08001630fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001631{
1632 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001633 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001634 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001635
1636 switch (msr) {
1637 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001638 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001639 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1640 kvm_skip_emulated_instruction(vcpu);
1641 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001642 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001643 break;
Wanpeng Liae95f562020-04-28 14:23:28 +08001644 case MSR_IA32_TSCDEADLINE:
1645 data = kvm_read_edx_eax(vcpu);
1646 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1647 kvm_skip_emulated_instruction(vcpu);
1648 ret = EXIT_FASTPATH_REENTER_GUEST;
1649 }
1650 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001651 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001652 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001653 }
1654
Wanpeng Li404d5d72020-04-28 14:23:25 +08001655 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001656 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001657
Wanpeng Li404d5d72020-04-28 14:23:25 +08001658 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001659}
1660EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1661
1662/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001663 * Adapt set_msr() to msr_io()'s calling convention
1664 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001665static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1666{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001667 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001668}
1669
Carsten Otte313a3dc2007-10-11 19:16:52 +02001670static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1671{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001672 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001673}
1674
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001675#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001676struct pvclock_clock {
1677 int vclock_mode;
1678 u64 cycle_last;
1679 u64 mask;
1680 u32 mult;
1681 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001682 u64 base_cycles;
1683 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001684};
1685
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001686struct pvclock_gtod_data {
1687 seqcount_t seq;
1688
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001689 struct pvclock_clock clock; /* extract of a clocksource struct */
1690 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001691
Paolo Bonzini917f9472020-01-22 14:32:20 +01001692 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001693 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001694};
1695
1696static struct pvclock_gtod_data pvclock_gtod_data;
1697
1698static void update_pvclock_gtod(struct timekeeper *tk)
1699{
1700 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1701
1702 write_seqcount_begin(&vdata->seq);
1703
1704 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001705 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001706 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1707 vdata->clock.mask = tk->tkr_mono.mask;
1708 vdata->clock.mult = tk->tkr_mono.mult;
1709 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001710 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1711 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001712
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001713 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001714 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1715 vdata->raw_clock.mask = tk->tkr_raw.mask;
1716 vdata->raw_clock.mult = tk->tkr_raw.mult;
1717 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001718 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1719 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001720
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001721 vdata->wall_time_sec = tk->xtime_sec;
1722
Paolo Bonzini917f9472020-01-22 14:32:20 +01001723 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001724
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001725 write_seqcount_end(&vdata->seq);
1726}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001727
1728static s64 get_kvmclock_base_ns(void)
1729{
1730 /* Count up from boot time, but with the frequency of the raw clock. */
1731 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1732}
1733#else
1734static s64 get_kvmclock_base_ns(void)
1735{
1736 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1737 return ktime_get_boottime_ns();
1738}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001739#endif
1740
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001741void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1742{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001743 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001744 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001745}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001746
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001747static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1748{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001749 int version;
1750 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001751 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001752 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001753
1754 if (!wall_clock)
1755 return;
1756
Avi Kivity9ed3c442010-05-04 15:00:37 +03001757 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1758 if (r)
1759 return;
1760
1761 if (version & 1)
1762 ++version; /* first time write, random junk */
1763
1764 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001765
Nicholas Krause1dab1342015-12-30 13:08:46 -05001766 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1767 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001768
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001769 /*
1770 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001771 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001772 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001773 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001774 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001775
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001776 wc.nsec = do_div(wall_nsec, 1000000000);
1777 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001778 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001779
1780 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1781
1782 version++;
1783 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001784}
1785
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001786static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1787{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001788 do_shl32_div32(dividend, divisor);
1789 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001790}
1791
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001792static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001793 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001794{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001795 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001796 int32_t shift = 0;
1797 uint64_t tps64;
1798 uint32_t tps32;
1799
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001800 tps64 = base_hz;
1801 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001802 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001803 tps64 >>= 1;
1804 shift--;
1805 }
1806
1807 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001808 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1809 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001810 scaled64 >>= 1;
1811 else
1812 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001813 shift++;
1814 }
1815
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001816 *pshift = shift;
1817 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001818}
1819
Marcelo Tosattid8281992012-11-27 23:29:01 -02001820#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001821static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001822#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001823
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001824static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001825static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001826
Zachary Amsdencc578282012-02-03 15:43:50 -02001827static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001828{
Zachary Amsdencc578282012-02-03 15:43:50 -02001829 u64 v = (u64)khz * (1000000 + ppm);
1830 do_div(v, 1000000);
1831 return v;
1832}
1833
Haozhong Zhang381d5852015-10-20 15:39:04 +08001834static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1835{
1836 u64 ratio;
1837
1838 /* Guest TSC same frequency as host TSC? */
1839 if (!scale) {
1840 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1841 return 0;
1842 }
1843
1844 /* TSC scaling supported? */
1845 if (!kvm_has_tsc_control) {
1846 if (user_tsc_khz > tsc_khz) {
1847 vcpu->arch.tsc_catchup = 1;
1848 vcpu->arch.tsc_always_catchup = 1;
1849 return 0;
1850 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001851 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001852 return -1;
1853 }
1854 }
1855
1856 /* TSC scaling required - calculate ratio */
1857 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1858 user_tsc_khz, tsc_khz);
1859
1860 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001861 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1862 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001863 return -1;
1864 }
1865
1866 vcpu->arch.tsc_scaling_ratio = ratio;
1867 return 0;
1868}
1869
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001870static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001871{
1872 u32 thresh_lo, thresh_hi;
1873 int use_scaling = 0;
1874
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001875 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001876 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001877 /* set tsc_scaling_ratio to a safe value */
1878 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001879 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001880 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001881
Zachary Amsdenc2855452010-09-18 14:38:15 -10001882 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001883 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001884 &vcpu->arch.virtual_tsc_shift,
1885 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001886 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001887
1888 /*
1889 * Compute the variation in TSC rate which is acceptable
1890 * within the range of tolerance and decide if the
1891 * rate being applied is within that bounds of the hardware
1892 * rate. If so, no scaling or compensation need be done.
1893 */
1894 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1895 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001896 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1897 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 -02001898 use_scaling = 1;
1899 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001900 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001901}
1902
1903static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1904{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001905 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001906 vcpu->arch.virtual_tsc_mult,
1907 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001908 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001909 return tsc;
1910}
1911
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001912static inline int gtod_is_based_on_tsc(int mode)
1913{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001914 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001915}
1916
Fengguang Wu69b00492015-01-19 22:33:39 +08001917static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001918{
1919#ifdef CONFIG_X86_64
1920 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001921 struct kvm_arch *ka = &vcpu->kvm->arch;
1922 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1923
1924 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1925 atomic_read(&vcpu->kvm->online_vcpus));
1926
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001927 /*
1928 * Once the masterclock is enabled, always perform request in
1929 * order to update it.
1930 *
1931 * In order to enable masterclock, the host clocksource must be TSC
1932 * and the vcpus need to have matched TSCs. When that happens,
1933 * perform request to enable masterclock.
1934 */
1935 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001936 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001937 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1938
1939 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1940 atomic_read(&vcpu->kvm->online_vcpus),
1941 ka->use_master_clock, gtod->clock.vclock_mode);
1942#endif
1943}
1944
Will Auldba904632012-11-29 12:42:50 -08001945static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1946{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001947 u64 curr_offset = vcpu->arch.l1_tsc_offset;
Will Auldba904632012-11-29 12:42:50 -08001948 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1949}
1950
Haozhong Zhang35181e82015-10-20 15:39:03 +08001951/*
1952 * Multiply tsc by a fixed point number represented by ratio.
1953 *
1954 * The most significant 64-N bits (mult) of ratio represent the
1955 * integral part of the fixed point number; the remaining N bits
1956 * (frac) represent the fractional part, ie. ratio represents a fixed
1957 * point number (mult + frac * 2^(-N)).
1958 *
1959 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1960 */
1961static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1962{
1963 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1964}
1965
1966u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1967{
1968 u64 _tsc = tsc;
1969 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1970
1971 if (ratio != kvm_default_tsc_scaling_ratio)
1972 _tsc = __scale_tsc(ratio, tsc);
1973
1974 return _tsc;
1975}
1976EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1977
Haozhong Zhang07c14192015-10-20 15:39:05 +08001978static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1979{
1980 u64 tsc;
1981
1982 tsc = kvm_scale_tsc(vcpu, rdtsc());
1983
1984 return target_tsc - tsc;
1985}
1986
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001987u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1988{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001989 return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001990}
1991EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1992
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001993static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1994{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001995 vcpu->arch.l1_tsc_offset = offset;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001996 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001997}
1998
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001999static inline bool kvm_check_tsc_unstable(void)
2000{
2001#ifdef CONFIG_X86_64
2002 /*
2003 * TSC is marked unstable when we're running on Hyper-V,
2004 * 'TSC page' clocksource is good.
2005 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002006 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002007 return false;
2008#endif
2009 return check_tsc_unstable();
2010}
2011
Will Auld8fe8ab42012-11-29 12:42:12 -08002012void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002013{
2014 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002015 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002016 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002017 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002018 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08002019 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002020 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002021
Jan Kiszka038f8c12011-02-04 10:49:11 +01002022 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002023 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002024 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002025 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002026
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002027 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002028 if (data == 0 && msr->host_initiated) {
2029 /*
2030 * detection of vcpu initialization -- need to sync
2031 * with other vCPUs. This particularly helps to keep
2032 * kvm_clock stable after CPU hotplug
2033 */
2034 synchronizing = true;
2035 } else {
2036 u64 tsc_exp = kvm->arch.last_tsc_write +
2037 nsec_to_cycles(vcpu, elapsed);
2038 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2039 /*
2040 * Special case: TSC write with a small delta (1 second)
2041 * of virtual cycle time against real time is
2042 * interpreted as an attempt to synchronize the CPU.
2043 */
2044 synchronizing = data < tsc_exp + tsc_hz &&
2045 data + tsc_hz > tsc_exp;
2046 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002047 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002048
2049 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002050 * For a reliable TSC, we can match TSC offsets, and for an unstable
2051 * TSC, we add elapsed time in this computation. We could let the
2052 * compensation code attempt to catch up if we fall behind, but
2053 * it's better to try to match offsets from the beginning.
2054 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002055 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002056 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002057 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002058 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002059 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002060 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002061 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002062 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002063 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002064 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002065 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002066 } else {
2067 /*
2068 * We split periods of matched TSC writes into generations.
2069 * For each generation, we track the original measured
2070 * nanosecond time, offset, and write, so if TSCs are in
2071 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002072 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002073 *
2074 * These values are tracked in kvm->arch.cur_xxx variables.
2075 */
2076 kvm->arch.cur_tsc_generation++;
2077 kvm->arch.cur_tsc_nsec = ns;
2078 kvm->arch.cur_tsc_write = data;
2079 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002080 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002081 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002082
2083 /*
2084 * We also track th most recent recorded KHZ, write and time to
2085 * allow the matching interval to be extended at each write.
2086 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002087 kvm->arch.last_tsc_nsec = ns;
2088 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002089 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002090
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002091 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002092
2093 /* Keep track of which generation this VCPU has synchronized to */
2094 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2095 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2096 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2097
Radim Krčmářd6321d42017-08-05 00:12:49 +02002098 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002099 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002100
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002101 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002102 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002103
2104 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002105 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002106 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002107 } else if (!already_matched) {
2108 kvm->arch.nr_vcpus_matched_tsc++;
2109 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002110
2111 kvm_track_tsc_matching(vcpu);
2112 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002113}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002114
Zachary Amsden99e3e302010-08-19 22:07:17 -10002115EXPORT_SYMBOL_GPL(kvm_write_tsc);
2116
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002117static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2118 s64 adjustment)
2119{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002120 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002121 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002122}
2123
2124static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2125{
2126 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2127 WARN_ON(adjustment < 0);
2128 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002129 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002130}
2131
Marcelo Tosattid8281992012-11-27 23:29:01 -02002132#ifdef CONFIG_X86_64
2133
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002134static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002135{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002136 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002137 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002138
2139 if (likely(ret >= last))
2140 return ret;
2141
2142 /*
2143 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002144 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002145 * very likely) and there's a data dependence, so force GCC
2146 * to generate a branch instead. I don't barrier() because
2147 * we don't actually need a barrier, and if this function
2148 * ever gets inlined it will generate worse code.
2149 */
2150 asm volatile ("");
2151 return last;
2152}
2153
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002154static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2155 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002156{
2157 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002158 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002159
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002160 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002161 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002162 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2163 tsc_timestamp);
2164 if (tsc_pg_val != U64_MAX) {
2165 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002166 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002167 v = (tsc_pg_val - clock->cycle_last) &
2168 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002169 } else {
2170 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002171 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002172 }
2173 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002174 case VDSO_CLOCKMODE_TSC:
2175 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002176 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002177 v = (*tsc_timestamp - clock->cycle_last) &
2178 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002179 break;
2180 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002181 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002182 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002183
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002184 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002185 *tsc_timestamp = v = 0;
2186
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002187 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002188}
2189
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002190static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002191{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002192 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002193 unsigned long seq;
2194 int mode;
2195 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002196
Marcelo Tosattid8281992012-11-27 23:29:01 -02002197 do {
2198 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002199 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002200 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002201 ns >>= gtod->raw_clock.shift;
2202 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002203 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002204 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002205
2206 return mode;
2207}
2208
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002209static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002210{
2211 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2212 unsigned long seq;
2213 int mode;
2214 u64 ns;
2215
2216 do {
2217 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002218 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002219 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002220 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002221 ns >>= gtod->clock.shift;
2222 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2223
2224 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2225 ts->tv_nsec = ns;
2226
2227 return mode;
2228}
2229
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002230/* returns true if host is using TSC based clocksource */
2231static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002232{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002233 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002234 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002235 return false;
2236
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002237 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002238 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002239}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002240
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002241/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002242static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002243 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002244{
2245 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002246 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002247 return false;
2248
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002249 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002250}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002251#endif
2252
2253/*
2254 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002255 * Assuming a stable TSC across physical CPUS, and a stable TSC
2256 * across virtual CPUs, the following condition is possible.
2257 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002258 * CPUs at the next numbered event.
2259 *
2260 * "timespecX" represents host monotonic time. "tscX" represents
2261 * RDTSC value.
2262 *
2263 * VCPU0 on CPU0 | VCPU1 on CPU1
2264 *
2265 * 1. read timespec0,tsc0
2266 * 2. | timespec1 = timespec0 + N
2267 * | tsc1 = tsc0 + M
2268 * 3. transition to guest | transition to guest
2269 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2270 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2271 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2272 *
2273 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2274 *
2275 * - ret0 < ret1
2276 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2277 * ...
2278 * - 0 < N - M => M < N
2279 *
2280 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2281 * always the case (the difference between two distinct xtime instances
2282 * might be smaller then the difference between corresponding TSC reads,
2283 * when updating guest vcpus pvclock areas).
2284 *
2285 * To avoid that problem, do not allow visibility of distinct
2286 * system_timestamp/tsc_timestamp values simultaneously: use a master
2287 * copy of host monotonic time values. Update that master copy
2288 * in lockstep.
2289 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002290 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002291 *
2292 */
2293
2294static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2295{
2296#ifdef CONFIG_X86_64
2297 struct kvm_arch *ka = &kvm->arch;
2298 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002299 bool host_tsc_clocksource, vcpus_matched;
2300
2301 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2302 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002303
2304 /*
2305 * If the host uses TSC clock, then passthrough TSC as stable
2306 * to the guest.
2307 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002308 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002309 &ka->master_kernel_ns,
2310 &ka->master_cycle_now);
2311
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002312 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002313 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002314 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002315
Marcelo Tosattid8281992012-11-27 23:29:01 -02002316 if (ka->use_master_clock)
2317 atomic_set(&kvm_guest_has_master_clock, 1);
2318
2319 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002320 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2321 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002322#endif
2323}
2324
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002325void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2326{
2327 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2328}
2329
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002330static void kvm_gen_update_masterclock(struct kvm *kvm)
2331{
2332#ifdef CONFIG_X86_64
2333 int i;
2334 struct kvm_vcpu *vcpu;
2335 struct kvm_arch *ka = &kvm->arch;
2336
2337 spin_lock(&ka->pvclock_gtod_sync_lock);
2338 kvm_make_mclock_inprogress_request(kvm);
2339 /* no guest entries from this point */
2340 pvclock_update_vm_gtod_copy(kvm);
2341
2342 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002343 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002344
2345 /* guest entries allowed */
2346 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002347 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002348
2349 spin_unlock(&ka->pvclock_gtod_sync_lock);
2350#endif
2351}
2352
Marcelo Tosattie891a322017-04-17 12:51:37 -03002353u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002354{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002355 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002356 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002357 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002358
Paolo Bonzini8b953442016-11-16 18:31:30 +01002359 spin_lock(&ka->pvclock_gtod_sync_lock);
2360 if (!ka->use_master_clock) {
2361 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002362 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002363 }
2364
Paolo Bonzini8b953442016-11-16 18:31:30 +01002365 hv_clock.tsc_timestamp = ka->master_cycle_now;
2366 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2367 spin_unlock(&ka->pvclock_gtod_sync_lock);
2368
Wanpeng Lie2c22062017-05-11 18:12:05 -07002369 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2370 get_cpu();
2371
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002372 if (__this_cpu_read(cpu_tsc_khz)) {
2373 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2374 &hv_clock.tsc_shift,
2375 &hv_clock.tsc_to_system_mul);
2376 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2377 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002378 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002379
2380 put_cpu();
2381
2382 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002383}
2384
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002385static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2386{
2387 struct kvm_vcpu_arch *vcpu = &v->arch;
2388 struct pvclock_vcpu_time_info guest_hv_clock;
2389
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002390 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002391 &guest_hv_clock, sizeof(guest_hv_clock))))
2392 return;
2393
2394 /* This VCPU is paused, but it's legal for a guest to read another
2395 * VCPU's kvmclock, so we really have to follow the specification where
2396 * it says that version is odd if data is being modified, and even after
2397 * it is consistent.
2398 *
2399 * Version field updates must be kept separate. This is because
2400 * kvm_write_guest_cached might use a "rep movs" instruction, and
2401 * writes within a string instruction are weakly ordered. So there
2402 * are three writes overall.
2403 *
2404 * As a small optimization, only write the version field in the first
2405 * and third write. The vcpu->pv_time cache is still valid, because the
2406 * version field is the first in the struct.
2407 */
2408 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2409
Liran Alon51c4b8b2017-11-05 16:11:30 +02002410 if (guest_hv_clock.version & 1)
2411 ++guest_hv_clock.version; /* first time write, random junk */
2412
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002413 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002414 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2415 &vcpu->hv_clock,
2416 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002417
2418 smp_wmb();
2419
2420 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2421 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2422
2423 if (vcpu->pvclock_set_guest_stopped_request) {
2424 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2425 vcpu->pvclock_set_guest_stopped_request = false;
2426 }
2427
2428 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2429
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002430 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2431 &vcpu->hv_clock,
2432 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002433
2434 smp_wmb();
2435
2436 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002437 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2438 &vcpu->hv_clock,
2439 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002440}
2441
Zachary Amsden34c238a2010-09-18 14:38:14 -10002442static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002443{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002444 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002445 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002446 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002447 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002448 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002449 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002450 bool use_master_clock;
2451
2452 kernel_ns = 0;
2453 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002454
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002455 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002456 * If the host uses TSC clock, then passthrough TSC as stable
2457 * to the guest.
2458 */
2459 spin_lock(&ka->pvclock_gtod_sync_lock);
2460 use_master_clock = ka->use_master_clock;
2461 if (use_master_clock) {
2462 host_tsc = ka->master_cycle_now;
2463 kernel_ns = ka->master_kernel_ns;
2464 }
2465 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002466
2467 /* Keep irq disabled to prevent changes to the clock */
2468 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002469 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2470 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002471 local_irq_restore(flags);
2472 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2473 return 1;
2474 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002475 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002476 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002477 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002478 }
2479
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002480 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002481
2482 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002483 * We may have to catch up the TSC to match elapsed wall clock
2484 * time for two reasons, even if kvmclock is used.
2485 * 1) CPU could have been running below the maximum TSC rate
2486 * 2) Broken TSC compensation resets the base at each VCPU
2487 * entry to avoid unknown leaps of TSC even when running
2488 * again on the same CPU. This may cause apparent elapsed
2489 * time to disappear, and the guest to stand still or run
2490 * very slowly.
2491 */
2492 if (vcpu->tsc_catchup) {
2493 u64 tsc = compute_guest_tsc(v, kernel_ns);
2494 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002495 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002496 tsc_timestamp = tsc;
2497 }
2498 }
2499
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002500 local_irq_restore(flags);
2501
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002502 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002503
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002504 if (kvm_has_tsc_control)
2505 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2506
2507 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002508 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002509 &vcpu->hv_clock.tsc_shift,
2510 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002511 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002512 }
2513
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002514 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002515 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002516 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002517
Marcelo Tosattid8281992012-11-27 23:29:01 -02002518 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002519 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002520 if (use_master_clock)
2521 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2522
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002523 vcpu->hv_clock.flags = pvclock_flags;
2524
Paolo Bonzini095cf552016-02-08 12:54:12 +01002525 if (vcpu->pv_time_enabled)
2526 kvm_setup_pvclock_page(v);
2527 if (v == kvm_get_vcpu(v->kvm, 0))
2528 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002529 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002530}
2531
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002532/*
2533 * kvmclock updates which are isolated to a given vcpu, such as
2534 * vcpu->cpu migration, should not allow system_timestamp from
2535 * the rest of the vcpus to remain static. Otherwise ntp frequency
2536 * correction applies to one vcpu's system_timestamp but not
2537 * the others.
2538 *
2539 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002540 * We need to rate-limit these requests though, as they can
2541 * considerably slow guests that have a large number of vcpus.
2542 * The time for a remote vcpu to update its kvmclock is bound
2543 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002544 */
2545
Andrew Jones7e44e442014-02-28 12:52:54 +01002546#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2547
2548static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002549{
2550 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002551 struct delayed_work *dwork = to_delayed_work(work);
2552 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2553 kvmclock_update_work);
2554 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002555 struct kvm_vcpu *vcpu;
2556
2557 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002558 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002559 kvm_vcpu_kick(vcpu);
2560 }
2561}
2562
Andrew Jones7e44e442014-02-28 12:52:54 +01002563static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2564{
2565 struct kvm *kvm = v->kvm;
2566
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002567 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002568 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2569 KVMCLOCK_UPDATE_DELAY);
2570}
2571
Andrew Jones332967a2014-02-28 12:52:55 +01002572#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2573
2574static void kvmclock_sync_fn(struct work_struct *work)
2575{
2576 struct delayed_work *dwork = to_delayed_work(work);
2577 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2578 kvmclock_sync_work);
2579 struct kvm *kvm = container_of(ka, struct kvm, arch);
2580
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002581 if (!kvmclock_periodic_sync)
2582 return;
2583
Andrew Jones332967a2014-02-28 12:52:55 +01002584 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2585 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2586 KVMCLOCK_SYNC_PERIOD);
2587}
2588
Borislav Petkov191c8132019-04-18 18:32:50 +02002589/*
2590 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2591 */
2592static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2593{
2594 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002595 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002596 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2597
2598 return false;
2599}
2600
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002601static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002602{
2603 u64 mcg_cap = vcpu->arch.mcg_cap;
2604 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002605 u32 msr = msr_info->index;
2606 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002607
2608 switch (msr) {
2609 case MSR_IA32_MCG_STATUS:
2610 vcpu->arch.mcg_status = data;
2611 break;
2612 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002613 if (!(mcg_cap & MCG_CTL_P) &&
2614 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002615 return 1;
2616 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002617 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002618 vcpu->arch.mcg_ctl = data;
2619 break;
2620 default:
2621 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002622 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002623 u32 offset = array_index_nospec(
2624 msr - MSR_IA32_MC0_CTL,
2625 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2626
Andre Przywara114be422010-03-24 17:46:42 +01002627 /* only 0 or all 1s can be written to IA32_MCi_CTL
2628 * some Linux kernels though clear bit 10 in bank 4 to
2629 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2630 * this to avoid an uncatched #GP in the guest
2631 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002632 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002633 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002634 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002635
2636 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002637 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002638 (offset & 0x3) == 1 && data != 0) {
2639 if (!can_set_mci_status(vcpu))
2640 return -1;
2641 }
2642
Huang Ying890ca9a2009-05-11 16:48:15 +08002643 vcpu->arch.mce_banks[offset] = data;
2644 break;
2645 }
2646 return 1;
2647 }
2648 return 0;
2649}
2650
Ed Swierkffde22a2009-10-15 15:21:43 -07002651static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2652{
2653 struct kvm *kvm = vcpu->kvm;
2654 int lm = is_long_mode(vcpu);
2655 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2656 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2657 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2658 : kvm->arch.xen_hvm_config.blob_size_32;
2659 u32 page_num = data & ~PAGE_MASK;
2660 u64 page_addr = data & PAGE_MASK;
2661 u8 *page;
2662 int r;
2663
2664 r = -E2BIG;
2665 if (page_num >= blob_size)
2666 goto out;
2667 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002668 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2669 if (IS_ERR(page)) {
2670 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002671 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002672 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002673 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002674 goto out_free;
2675 r = 0;
2676out_free:
2677 kfree(page);
2678out:
2679 return r;
2680}
2681
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002682static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
2683{
2684 u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
2685
2686 return (vcpu->arch.apf.msr_en_val & mask) == mask;
2687}
2688
Gleb Natapov344d9582010-10-14 11:22:50 +02002689static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2690{
2691 gpa_t gpa = data & ~0x3f;
2692
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002693 /* Bits 4:5 are reserved, Should be zero */
2694 if (data & 0x30)
Gleb Natapov344d9582010-10-14 11:22:50 +02002695 return 1;
2696
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002697 vcpu->arch.apf.msr_en_val = data;
Gleb Natapov344d9582010-10-14 11:22:50 +02002698
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002699 if (!kvm_pv_async_pf_enabled(vcpu)) {
Gleb Natapov344d9582010-10-14 11:22:50 +02002700 kvm_clear_async_pf_completion_queue(vcpu);
2701 kvm_async_pf_hash_reset(vcpu);
2702 return 0;
2703 }
2704
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002705 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02002706 sizeof(u64)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002707 return 1;
2708
Gleb Natapov6adba522010-10-14 11:22:55 +02002709 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002710 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002711
Gleb Natapov344d9582010-10-14 11:22:50 +02002712 kvm_async_pf_wakeup_all(vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002713
2714 return 0;
2715}
2716
2717static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data)
2718{
2719 /* Bits 8-63 are reserved */
2720 if (data >> 8)
2721 return 1;
2722
2723 if (!lapic_in_kernel(vcpu))
2724 return 1;
2725
2726 vcpu->arch.apf.msr_int_val = data;
2727
2728 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK;
2729
Gleb Natapov344d9582010-10-14 11:22:50 +02002730 return 0;
2731}
2732
Glauber Costa12f9a482011-02-01 14:16:40 -05002733static void kvmclock_reset(struct kvm_vcpu *vcpu)
2734{
Andy Honig0b794592013-02-20 14:48:10 -08002735 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002736 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002737}
2738
Sean Christopherson77809382020-03-20 14:28:18 -07002739static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002740{
2741 ++vcpu->stat.tlb_flush;
Sean Christopherson77809382020-03-20 14:28:18 -07002742 kvm_x86_ops.tlb_flush_all(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002743}
2744
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002745static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
2746{
2747 ++vcpu->stat.tlb_flush;
2748 kvm_x86_ops.tlb_flush_guest(vcpu);
2749}
2750
Glauber Costac9aaa892011-07-11 15:28:14 -04002751static void record_steal_time(struct kvm_vcpu *vcpu)
2752{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002753 struct kvm_host_map map;
2754 struct kvm_steal_time *st;
2755
Glauber Costac9aaa892011-07-11 15:28:14 -04002756 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2757 return;
2758
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002759 /* -EAGAIN is returned in atomic context so we can just return. */
2760 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2761 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002762 return;
2763
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002764 st = map.hva +
2765 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2766
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002767 /*
2768 * Doing a TLB flush here, on the guest's behalf, can avoid
2769 * expensive IPIs.
2770 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002771 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002772 st->preempted & KVM_VCPU_FLUSH_TLB);
2773 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002774 kvm_vcpu_flush_tlb_guest(vcpu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002775
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002776 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002777
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002778 if (st->version & 1)
2779 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002780
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002781 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002782
2783 smp_wmb();
2784
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002785 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002786 vcpu->arch.st.last_steal;
2787 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002788
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002789 smp_wmb();
2790
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002791 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002792
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002793 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002794}
2795
Will Auld8fe8ab42012-11-29 12:42:12 -08002796int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002797{
Gleb Natapov57537852012-01-15 14:17:22 +02002798 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002799 u32 msr = msr_info->index;
2800 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002801
Carsten Otte15c4a642007-10-30 18:44:17 +01002802 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002803 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002804 case MSR_IA32_UCODE_WRITE:
2805 case MSR_VM_HSAVE_PA:
2806 case MSR_AMD64_PATCH_LOADER:
2807 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002808 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002809 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002810 break;
2811
Wanpeng Li518e7b92018-02-28 14:03:31 +08002812 case MSR_IA32_UCODE_REV:
2813 if (msr_info->host_initiated)
2814 vcpu->arch.microcode_version = data;
2815 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002816 case MSR_IA32_ARCH_CAPABILITIES:
2817 if (!msr_info->host_initiated)
2818 return 1;
2819 vcpu->arch.arch_capabilities = data;
2820 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002821 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002822 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002823 case MSR_K7_HWCR:
2824 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002825 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002826 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002827
2828 /* Handle McStatusWrEn */
2829 if (data == BIT_ULL(18)) {
2830 vcpu->arch.msr_hwcr = data;
2831 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002832 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2833 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002834 return 1;
2835 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002836 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002837 case MSR_FAM10H_MMIO_CONF_BASE:
2838 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002839 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2840 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002841 return 1;
2842 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002843 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002844 case MSR_IA32_DEBUGCTLMSR:
2845 if (!data) {
2846 /* We support the non-activated case already */
2847 break;
2848 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2849 /* Values other than LBR and BTF are vendor-specific,
2850 thus reserved and should throw a #GP */
2851 return 1;
2852 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002853 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2854 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002855 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002856 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002857 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002858 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002859 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002860 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2861 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002862 case MSR_IA32_TSCDEADLINE:
2863 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2864 break;
Will Auldba904632012-11-29 12:42:50 -08002865 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002866 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002867 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002868 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002869 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002870 }
2871 vcpu->arch.ia32_tsc_adjust_msr = data;
2872 }
2873 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002874 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002875 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2876 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2877 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2878 return 1;
2879 vcpu->arch.ia32_misc_enable_msr = data;
2880 kvm_update_cpuid(vcpu);
2881 } else {
2882 vcpu->arch.ia32_misc_enable_msr = data;
2883 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002884 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002885 case MSR_IA32_SMBASE:
2886 if (!msr_info->host_initiated)
2887 return 1;
2888 vcpu->arch.smbase = data;
2889 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002890 case MSR_IA32_POWER_CTL:
2891 vcpu->arch.msr_ia32_power_ctl = data;
2892 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002893 case MSR_IA32_TSC:
2894 kvm_write_tsc(vcpu, msr_info);
2895 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002896 case MSR_IA32_XSS:
2897 if (!msr_info->host_initiated &&
2898 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2899 return 1;
2900 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002901 * KVM supports exposing PT to the guest, but does not support
2902 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2903 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002904 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002905 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002906 return 1;
2907 vcpu->arch.ia32_xss = data;
2908 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002909 case MSR_SMI_COUNT:
2910 if (!msr_info->host_initiated)
2911 return 1;
2912 vcpu->arch.smi_count = data;
2913 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002914 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002915 case MSR_KVM_WALL_CLOCK:
2916 vcpu->kvm->arch.wall_clock = data;
2917 kvm_write_wall_clock(vcpu->kvm, data);
2918 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002919 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002920 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002921 struct kvm_arch *ka = &vcpu->kvm->arch;
2922
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002923 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2924 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2925
2926 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002927 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002928
2929 ka->boot_vcpu_runs_old_kvmclock = tmp;
2930 }
2931
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002932 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002933 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002934
2935 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002936 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002937 if (!(data & 1))
2938 break;
2939
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002940 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002941 &vcpu->arch.pv_time, data & ~1ULL,
2942 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002943 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002944
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002945 break;
2946 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002947 case MSR_KVM_ASYNC_PF_EN:
2948 if (kvm_pv_enable_async_pf(vcpu, data))
2949 return 1;
2950 break;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002951 case MSR_KVM_ASYNC_PF_INT:
2952 if (kvm_pv_enable_async_pf_int(vcpu, data))
2953 return 1;
2954 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02002955 case MSR_KVM_ASYNC_PF_ACK:
2956 if (data & 0x1) {
2957 vcpu->arch.apf.pageready_pending = false;
2958 kvm_check_async_pf_completion(vcpu);
2959 }
2960 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002961 case MSR_KVM_STEAL_TIME:
2962
2963 if (unlikely(!sched_info_on()))
2964 return 1;
2965
2966 if (data & KVM_STEAL_RESERVED_MASK)
2967 return 1;
2968
Glauber Costac9aaa892011-07-11 15:28:14 -04002969 vcpu->arch.st.msr_val = data;
2970
2971 if (!(data & KVM_MSR_ENABLED))
2972 break;
2973
Glauber Costac9aaa892011-07-11 15:28:14 -04002974 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2975
2976 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002977 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002978 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002979 return 1;
2980 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002981
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002982 case MSR_KVM_POLL_CONTROL:
2983 /* only enable bit supported */
2984 if (data & (-1ULL << 1))
2985 return 1;
2986
2987 vcpu->arch.msr_kvm_poll_control = data;
2988 break;
2989
Huang Ying890ca9a2009-05-11 16:48:15 +08002990 case MSR_IA32_MCG_CTL:
2991 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002992 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002993 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002994
Wei Huang6912ac32015-06-12 01:34:56 -04002995 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2996 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2997 pr = true; /* fall through */
2998 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2999 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003000 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003001 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02003002
3003 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03003004 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
3005 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02003006 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003007 case MSR_K7_CLK_CTL:
3008 /*
3009 * Ignore all writes to this no longer documented MSR.
3010 * Writes are only relevant for old K7 processors,
3011 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08003012 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003013 * affected processor models on the command line, hence
3014 * the need to ignore the workaround.
3015 */
3016 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003017 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003018 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3019 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003020 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3021 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003022 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003023 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3024 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3025 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003026 return kvm_hv_set_msr_common(vcpu, msr, data,
3027 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003028 case MSR_IA32_BBL_CR_CTL3:
3029 /* Drop writes to this legacy MSR -- see rdmsr
3030 * counterpart for further detail.
3031 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003032 if (report_ignored_msrs)
3033 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
3034 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05003035 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003036 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003037 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003038 return 1;
3039 vcpu->arch.osvw.length = data;
3040 break;
3041 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003042 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003043 return 1;
3044 vcpu->arch.osvw.status = data;
3045 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003046 case MSR_PLATFORM_INFO:
3047 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003048 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3049 cpuid_fault_enabled(vcpu)))
3050 return 1;
3051 vcpu->arch.msr_platform_info = data;
3052 break;
3053 case MSR_MISC_FEATURES_ENABLES:
3054 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3055 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3056 !supports_cpuid_fault(vcpu)))
3057 return 1;
3058 vcpu->arch.msr_misc_features_enables = data;
3059 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003060 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07003061 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
3062 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02003063 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003064 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02003065 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003066 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03003067 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003068 return 1;
3069 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003070 if (report_ignored_msrs)
3071 vcpu_unimpl(vcpu,
3072 "ignored wrmsr: 0x%x data 0x%llx\n",
3073 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003074 break;
3075 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003076 }
3077 return 0;
3078}
3079EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3080
Paolo Bonzini44883f02018-07-26 13:01:52 +02003081static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003082{
3083 u64 data;
3084 u64 mcg_cap = vcpu->arch.mcg_cap;
3085 unsigned bank_num = mcg_cap & 0xff;
3086
3087 switch (msr) {
3088 case MSR_IA32_P5_MC_ADDR:
3089 case MSR_IA32_P5_MC_TYPE:
3090 data = 0;
3091 break;
3092 case MSR_IA32_MCG_CAP:
3093 data = vcpu->arch.mcg_cap;
3094 break;
3095 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003096 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003097 return 1;
3098 data = vcpu->arch.mcg_ctl;
3099 break;
3100 case MSR_IA32_MCG_STATUS:
3101 data = vcpu->arch.mcg_status;
3102 break;
3103 default:
3104 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003105 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003106 u32 offset = array_index_nospec(
3107 msr - MSR_IA32_MC0_CTL,
3108 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3109
Huang Ying890ca9a2009-05-11 16:48:15 +08003110 data = vcpu->arch.mce_banks[offset];
3111 break;
3112 }
3113 return 1;
3114 }
3115 *pdata = data;
3116 return 0;
3117}
3118
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003119int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003120{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003121 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003122 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003123 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003124 case MSR_IA32_DEBUGCTLMSR:
3125 case MSR_IA32_LASTBRANCHFROMIP:
3126 case MSR_IA32_LASTBRANCHTOIP:
3127 case MSR_IA32_LASTINTFROMIP:
3128 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303129 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003130 case MSR_K8_TSEG_ADDR:
3131 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003132 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003133 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003134 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003135 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003136 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003137 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003138 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003139 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003140 /*
3141 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3142 * limit) MSRs. Just return 0, as we do not want to expose the host
3143 * data here. Do not conditionalize this on CPUID, as KVM does not do
3144 * so for existing CPU-specific MSRs.
3145 */
3146 case MSR_RAPL_POWER_UNIT:
3147 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3148 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3149 case MSR_PKG_ENERGY_STATUS: /* Total package */
3150 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003151 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003152 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003153 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003154 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3155 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3156 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3157 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003158 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003159 return kvm_pmu_get_msr(vcpu, msr_info);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003160 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003161 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003162 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003163 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003164 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003165 case MSR_IA32_ARCH_CAPABILITIES:
3166 if (!msr_info->host_initiated &&
3167 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3168 return 1;
3169 msr_info->data = vcpu->arch.arch_capabilities;
3170 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003171 case MSR_IA32_POWER_CTL:
3172 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3173 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003174 case MSR_IA32_TSC:
3175 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3176 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003177 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003178 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003179 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003180 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003181 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003182 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003183 /*
3184 * MSR_EBC_FREQUENCY_ID
3185 * Conservative value valid for even the basic CPU models.
3186 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3187 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3188 * and 266MHz for model 3, or 4. Set Core Clock
3189 * Frequency to System Bus Frequency Ratio to 1 (bits
3190 * 31:24) even though these are only valid for CPU
3191 * models > 2, however guests may end up dividing or
3192 * multiplying by zero otherwise.
3193 */
3194 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003195 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003196 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003197 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003198 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003199 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003200 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003201 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003202 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003203 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003204 break;
Will Auldba904632012-11-29 12:42:50 -08003205 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003206 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003207 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003208 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003209 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003210 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003211 case MSR_IA32_SMBASE:
3212 if (!msr_info->host_initiated)
3213 return 1;
3214 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003215 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003216 case MSR_SMI_COUNT:
3217 msr_info->data = vcpu->arch.smi_count;
3218 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003219 case MSR_IA32_PERF_STATUS:
3220 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003221 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003222 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003223 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003224 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003225 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003226 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003227 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003228 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003229 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003230 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003231 break;
3232 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003233 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003234 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003235 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003236 case MSR_KVM_ASYNC_PF_EN:
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003237 msr_info->data = vcpu->arch.apf.msr_en_val;
3238 break;
3239 case MSR_KVM_ASYNC_PF_INT:
3240 msr_info->data = vcpu->arch.apf.msr_int_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003241 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003242 case MSR_KVM_ASYNC_PF_ACK:
3243 msr_info->data = 0;
3244 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003245 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003246 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003247 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003248 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003249 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003250 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003251 case MSR_KVM_POLL_CONTROL:
3252 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3253 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003254 case MSR_IA32_P5_MC_ADDR:
3255 case MSR_IA32_P5_MC_TYPE:
3256 case MSR_IA32_MCG_CAP:
3257 case MSR_IA32_MCG_CTL:
3258 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003259 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003260 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3261 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003262 case MSR_IA32_XSS:
3263 if (!msr_info->host_initiated &&
3264 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3265 return 1;
3266 msr_info->data = vcpu->arch.ia32_xss;
3267 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003268 case MSR_K7_CLK_CTL:
3269 /*
3270 * Provide expected ramp-up count for K7. All other
3271 * are set to zero, indicating minimum divisors for
3272 * every field.
3273 *
3274 * This prevents guest kernels on AMD host with CPU
3275 * type 6, model 8 and higher from exploding due to
3276 * the rdmsr failing.
3277 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003278 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003279 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003280 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003281 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3282 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003283 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3284 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003285 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003286 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3287 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3288 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003289 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003290 msr_info->index, &msr_info->data,
3291 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003292 case MSR_IA32_BBL_CR_CTL3:
3293 /* This legacy MSR exists but isn't fully documented in current
3294 * silicon. It is however accessed by winxp in very narrow
3295 * scenarios where it sets bit #19, itself documented as
3296 * a "reserved" bit. Best effort attempt to source coherent
3297 * read data here should the balance of the register be
3298 * interpreted by the guest:
3299 *
3300 * L2 cache control register 3: 64GB range, 256KB size,
3301 * enabled, latency 0x1, configured
3302 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003303 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003304 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003305 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003306 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003307 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003308 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003309 break;
3310 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003311 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003312 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003313 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003314 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003315 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003316 if (!msr_info->host_initiated &&
3317 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3318 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003319 msr_info->data = vcpu->arch.msr_platform_info;
3320 break;
3321 case MSR_MISC_FEATURES_ENABLES:
3322 msr_info->data = vcpu->arch.msr_misc_features_enables;
3323 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003324 case MSR_K7_HWCR:
3325 msr_info->data = vcpu->arch.msr_hwcr;
3326 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003327 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003328 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003329 return kvm_pmu_get_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02003330 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003331 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3332 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003333 return 1;
3334 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003335 if (report_ignored_msrs)
3336 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3337 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003338 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003339 }
3340 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003341 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003342 return 0;
3343}
3344EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3345
Carsten Otte313a3dc2007-10-11 19:16:52 +02003346/*
3347 * Read or write a bunch of msrs. All parameters are kernel addresses.
3348 *
3349 * @return number of msrs set successfully.
3350 */
3351static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3352 struct kvm_msr_entry *entries,
3353 int (*do_msr)(struct kvm_vcpu *vcpu,
3354 unsigned index, u64 *data))
3355{
Tom Lendacky801e4592018-02-21 13:39:51 -06003356 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003357
Carsten Otte313a3dc2007-10-11 19:16:52 +02003358 for (i = 0; i < msrs->nmsrs; ++i)
3359 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3360 break;
3361
Carsten Otte313a3dc2007-10-11 19:16:52 +02003362 return i;
3363}
3364
3365/*
3366 * Read or write a bunch of msrs. Parameters are user addresses.
3367 *
3368 * @return number of msrs set successfully.
3369 */
3370static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3371 int (*do_msr)(struct kvm_vcpu *vcpu,
3372 unsigned index, u64 *data),
3373 int writeback)
3374{
3375 struct kvm_msrs msrs;
3376 struct kvm_msr_entry *entries;
3377 int r, n;
3378 unsigned size;
3379
3380 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003381 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003382 goto out;
3383
3384 r = -E2BIG;
3385 if (msrs.nmsrs >= MAX_IO_MSRS)
3386 goto out;
3387
Carsten Otte313a3dc2007-10-11 19:16:52 +02003388 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003389 entries = memdup_user(user_msrs->entries, size);
3390 if (IS_ERR(entries)) {
3391 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003392 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003393 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003394
3395 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3396 if (r < 0)
3397 goto out_free;
3398
3399 r = -EFAULT;
3400 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3401 goto out_free;
3402
3403 r = n;
3404
3405out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003406 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003407out:
3408 return r;
3409}
3410
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003411static inline bool kvm_can_mwait_in_guest(void)
3412{
3413 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003414 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3415 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003416}
3417
Alexander Graf784aa3d2014-07-14 18:27:35 +02003418int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003419{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003420 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003421
3422 switch (ext) {
3423 case KVM_CAP_IRQCHIP:
3424 case KVM_CAP_HLT:
3425 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003426 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003427 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003428 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003429 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003430 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003431 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003432 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003433 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003434 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003435 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003436 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003437 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003438 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003439 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003440 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003441 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003442 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003443 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003444 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003445 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003446 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003447 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003448 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003449 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003450 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003451 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003452 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003453 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003454 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003455 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003456 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003457 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003458 case KVM_CAP_ASYNC_PF:
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02003459 case KVM_CAP_ASYNC_PF_INT:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003460 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003461 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003462 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003463 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003464 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003465 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003466 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003467 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003468 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003469 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003470 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003471 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003472 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003473 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003474 case KVM_CAP_SET_GUEST_DEBUG:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003475 r = 1;
3476 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003477 case KVM_CAP_SYNC_REGS:
3478 r = KVM_SYNC_X86_VALID_FIELDS;
3479 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003480 case KVM_CAP_ADJUST_CLOCK:
3481 r = KVM_CLOCK_TSC_STABLE;
3482 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003483 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003484 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3485 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003486 if(kvm_can_mwait_in_guest())
3487 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003488 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003489 case KVM_CAP_X86_SMM:
3490 /* SMBASE is usually relocated above 1M on modern chipsets,
3491 * and SMM handlers might indeed rely on 4G segment limits,
3492 * so do not report SMM to be available if real mode is
3493 * emulated via vm86 mode. Still, do not go to great lengths
3494 * to avoid userspace's usage of the feature, because it is a
3495 * fringe case that is not enabled except via specific settings
3496 * of the module parameters.
3497 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003498 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003499 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003500 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003501 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003502 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003503 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003504 r = KVM_SOFT_MAX_VCPUS;
3505 break;
3506 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003507 r = KVM_MAX_VCPUS;
3508 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003509 case KVM_CAP_MAX_VCPU_ID:
3510 r = KVM_MAX_VCPU_ID;
3511 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003512 case KVM_CAP_PV_MMU: /* obsolete */
3513 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003514 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003515 case KVM_CAP_MCE:
3516 r = KVM_MAX_MCE_BANKS;
3517 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003518 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003519 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003520 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003521 case KVM_CAP_TSC_CONTROL:
3522 r = kvm_has_tsc_control;
3523 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003524 case KVM_CAP_X2APIC_API:
3525 r = KVM_X2APIC_API_VALID_FLAGS;
3526 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003527 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003528 r = kvm_x86_ops.nested_ops->get_state ?
3529 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003530 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003531 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003532 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003533 break;
3534 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003535 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003536 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003537 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003538 break;
3539 }
3540 return r;
3541
3542}
3543
Carsten Otte043405e2007-10-10 17:16:19 +02003544long kvm_arch_dev_ioctl(struct file *filp,
3545 unsigned int ioctl, unsigned long arg)
3546{
3547 void __user *argp = (void __user *)arg;
3548 long r;
3549
3550 switch (ioctl) {
3551 case KVM_GET_MSR_INDEX_LIST: {
3552 struct kvm_msr_list __user *user_msr_list = argp;
3553 struct kvm_msr_list msr_list;
3554 unsigned n;
3555
3556 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003557 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003558 goto out;
3559 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003560 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003561 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003562 goto out;
3563 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003564 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003565 goto out;
3566 r = -EFAULT;
3567 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3568 num_msrs_to_save * sizeof(u32)))
3569 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003570 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003571 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003572 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003573 goto out;
3574 r = 0;
3575 break;
3576 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003577 case KVM_GET_SUPPORTED_CPUID:
3578 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003579 struct kvm_cpuid2 __user *cpuid_arg = argp;
3580 struct kvm_cpuid2 cpuid;
3581
3582 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003583 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003584 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003585
3586 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3587 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003588 if (r)
3589 goto out;
3590
3591 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003592 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003593 goto out;
3594 r = 0;
3595 break;
3596 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003597 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003598 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003599 if (copy_to_user(argp, &kvm_mce_cap_supported,
3600 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003601 goto out;
3602 r = 0;
3603 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003604 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3605 struct kvm_msr_list __user *user_msr_list = argp;
3606 struct kvm_msr_list msr_list;
3607 unsigned int n;
3608
3609 r = -EFAULT;
3610 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3611 goto out;
3612 n = msr_list.nmsrs;
3613 msr_list.nmsrs = num_msr_based_features;
3614 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3615 goto out;
3616 r = -E2BIG;
3617 if (n < msr_list.nmsrs)
3618 goto out;
3619 r = -EFAULT;
3620 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3621 num_msr_based_features * sizeof(u32)))
3622 goto out;
3623 r = 0;
3624 break;
3625 }
3626 case KVM_GET_MSRS:
3627 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3628 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003629 default:
3630 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003631 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003632 }
3633out:
3634 return r;
3635}
3636
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003637static void wbinvd_ipi(void *garbage)
3638{
3639 wbinvd();
3640}
3641
3642static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3643{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003644 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003645}
3646
Carsten Otte313a3dc2007-10-11 19:16:52 +02003647void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3648{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003649 /* Address WBINVD may be executed by guest */
3650 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003651 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003652 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3653 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3654 smp_call_function_single(vcpu->cpu,
3655 wbinvd_ipi, NULL, 1);
3656 }
3657
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003658 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003659
Babu Moger37486132020-05-12 18:59:06 -05003660 /* Save host pkru register if supported */
3661 vcpu->arch.host_pkru = read_pkru();
3662
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003663 /* Apply any externally detected TSC adjustments (due to suspend) */
3664 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3665 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3666 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003667 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003668 }
3669
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003670 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003671 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003672 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003673 if (tsc_delta < 0)
3674 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003675
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003676 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003677 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003678 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003679 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003680 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003681 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003682
3683 if (kvm_lapic_hv_timer_in_use(vcpu))
3684 kvm_lapic_restart_hv_timer(vcpu);
3685
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003686 /*
3687 * On a host with synchronized TSC, there is no need to update
3688 * kvmclock on vcpu->cpu migration
3689 */
3690 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003691 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003692 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003693 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003694 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003695 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003696
Glauber Costac9aaa892011-07-11 15:28:14 -04003697 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003698}
3699
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003700static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3701{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003702 struct kvm_host_map map;
3703 struct kvm_steal_time *st;
3704
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003705 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3706 return;
3707
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003708 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003709 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003710
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003711 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3712 &vcpu->arch.st.cache, true))
3713 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003714
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003715 st = map.hva +
3716 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3717
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003718 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003719
3720 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003721}
3722
Carsten Otte313a3dc2007-10-11 19:16:52 +02003723void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3724{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003725 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003726
3727 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003728 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003729
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003730 /*
3731 * Disable page faults because we're in atomic context here.
3732 * kvm_write_guest_offset_cached() would call might_fault()
3733 * that relies on pagefault_disable() to tell if there's a
3734 * bug. NOTE: the write to guest memory may not go through if
3735 * during postcopy live migration or if there's heavy guest
3736 * paging.
3737 */
3738 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003739 /*
3740 * kvm_memslots() will be called by
3741 * kvm_write_guest_offset_cached() so take the srcu lock.
3742 */
3743 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003744 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003745 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003746 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003747 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003748 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003749 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003750 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3751 * on every vmexit, but if not, we might have a stale dr6 from the
3752 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003753 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003754 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003755}
3756
Carsten Otte313a3dc2007-10-11 19:16:52 +02003757static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3758 struct kvm_lapic_state *s)
3759{
Liran Alonfa59cc02017-12-24 18:12:53 +02003760 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003761 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003762
Radim Krčmářa92e2542016-07-12 22:09:22 +02003763 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003764}
3765
3766static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3767 struct kvm_lapic_state *s)
3768{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003769 int r;
3770
3771 r = kvm_apic_set_state(vcpu, s);
3772 if (r)
3773 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003774 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003775
3776 return 0;
3777}
3778
Matt Gingell127a4572015-11-17 17:32:05 +01003779static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3780{
3781 return (!lapic_in_kernel(vcpu) ||
3782 kvm_apic_accept_pic_intr(vcpu));
3783}
3784
Matt Gingell782d4222015-11-16 15:26:00 -08003785/*
3786 * if userspace requested an interrupt window, check that the
3787 * interrupt window is open.
3788 *
3789 * No need to exit to userspace if we already have an interrupt queued.
3790 */
3791static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3792{
3793 return kvm_arch_interrupt_allowed(vcpu) &&
3794 !kvm_cpu_has_interrupt(vcpu) &&
3795 !kvm_event_needs_reinjection(vcpu) &&
3796 kvm_cpu_accept_dm_intr(vcpu);
3797}
3798
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003799static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3800 struct kvm_interrupt *irq)
3801{
Chen Gang02cdb502013-02-27 11:33:25 +08003802 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003803 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003804
3805 if (!irqchip_in_kernel(vcpu->kvm)) {
3806 kvm_queue_interrupt(vcpu, irq->irq, false);
3807 kvm_make_request(KVM_REQ_EVENT, vcpu);
3808 return 0;
3809 }
3810
3811 /*
3812 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3813 * fail for in-kernel 8259.
3814 */
3815 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003816 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003817
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003818 if (vcpu->arch.pending_external_vector != -1)
3819 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003820
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003821 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003822 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003823 return 0;
3824}
3825
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003826static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3827{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003828 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003829
3830 return 0;
3831}
3832
Paolo Bonzinif0778252015-04-01 15:06:40 +02003833static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3834{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003835 kvm_make_request(KVM_REQ_SMI, vcpu);
3836
Paolo Bonzinif0778252015-04-01 15:06:40 +02003837 return 0;
3838}
3839
Avi Kivityb209749f2007-10-22 16:50:39 +02003840static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3841 struct kvm_tpr_access_ctl *tac)
3842{
3843 if (tac->flags)
3844 return -EINVAL;
3845 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3846 return 0;
3847}
3848
Huang Ying890ca9a2009-05-11 16:48:15 +08003849static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3850 u64 mcg_cap)
3851{
3852 int r;
3853 unsigned bank_num = mcg_cap & 0xff, bank;
3854
3855 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07003856 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003857 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003858 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003859 goto out;
3860 r = 0;
3861 vcpu->arch.mcg_cap = mcg_cap;
3862 /* Init IA32_MCG_CTL to all 1s */
3863 if (mcg_cap & MCG_CTL_P)
3864 vcpu->arch.mcg_ctl = ~(u64)0;
3865 /* Init IA32_MCi_CTL to all 1s */
3866 for (bank = 0; bank < bank_num; bank++)
3867 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003868
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003869 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003870out:
3871 return r;
3872}
3873
3874static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3875 struct kvm_x86_mce *mce)
3876{
3877 u64 mcg_cap = vcpu->arch.mcg_cap;
3878 unsigned bank_num = mcg_cap & 0xff;
3879 u64 *banks = vcpu->arch.mce_banks;
3880
3881 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3882 return -EINVAL;
3883 /*
3884 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3885 * reporting is disabled
3886 */
3887 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3888 vcpu->arch.mcg_ctl != ~(u64)0)
3889 return 0;
3890 banks += 4 * mce->bank;
3891 /*
3892 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3893 * reporting is disabled for the bank
3894 */
3895 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3896 return 0;
3897 if (mce->status & MCI_STATUS_UC) {
3898 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003899 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003900 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003901 return 0;
3902 }
3903 if (banks[1] & MCI_STATUS_VAL)
3904 mce->status |= MCI_STATUS_OVER;
3905 banks[2] = mce->addr;
3906 banks[3] = mce->misc;
3907 vcpu->arch.mcg_status = mce->mcg_status;
3908 banks[1] = mce->status;
3909 kvm_queue_exception(vcpu, MC_VECTOR);
3910 } else if (!(banks[1] & MCI_STATUS_VAL)
3911 || !(banks[1] & MCI_STATUS_UC)) {
3912 if (banks[1] & MCI_STATUS_VAL)
3913 mce->status |= MCI_STATUS_OVER;
3914 banks[2] = mce->addr;
3915 banks[3] = mce->misc;
3916 banks[1] = mce->status;
3917 } else
3918 banks[1] |= MCI_STATUS_OVER;
3919 return 0;
3920}
3921
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003922static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3923 struct kvm_vcpu_events *events)
3924{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003925 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003926
Wanpeng Li664f8e22017-08-24 03:35:09 -07003927 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003928 * In guest mode, payload delivery should be deferred,
3929 * so that the L1 hypervisor can intercept #PF before
3930 * CR2 is modified (or intercept #DB before DR6 is
3931 * modified under nVMX). Unless the per-VM capability,
3932 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3933 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3934 * opportunistically defer the exception payload, deliver it if the
3935 * capability hasn't been requested before processing a
3936 * KVM_GET_VCPU_EVENTS.
3937 */
3938 if (!vcpu->kvm->arch.exception_payload_enabled &&
3939 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3940 kvm_deliver_exception_payload(vcpu);
3941
3942 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003943 * The API doesn't provide the instruction length for software
3944 * exceptions, so don't report them. As long as the guest RIP
3945 * isn't advanced, we should expect to encounter the exception
3946 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003947 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003948 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3949 events->exception.injected = 0;
3950 events->exception.pending = 0;
3951 } else {
3952 events->exception.injected = vcpu->arch.exception.injected;
3953 events->exception.pending = vcpu->arch.exception.pending;
3954 /*
3955 * For ABI compatibility, deliberately conflate
3956 * pending and injected exceptions when
3957 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3958 */
3959 if (!vcpu->kvm->arch.exception_payload_enabled)
3960 events->exception.injected |=
3961 vcpu->arch.exception.pending;
3962 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003963 events->exception.nr = vcpu->arch.exception.nr;
3964 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3965 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003966 events->exception_has_payload = vcpu->arch.exception.has_payload;
3967 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003968
Jan Kiszka03b82a32010-02-15 10:45:41 +01003969 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003970 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003971 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003972 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003973 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003974
3975 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003976 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003977 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003978 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003979
Jan Kiszka66450a22013-03-13 12:42:34 +01003980 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003981
Paolo Bonzinif0778252015-04-01 15:06:40 +02003982 events->smi.smm = is_smm(vcpu);
3983 events->smi.pending = vcpu->arch.smi_pending;
3984 events->smi.smm_inside_nmi =
3985 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3986 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3987
Jan Kiszkadab4b912009-12-06 18:24:15 +01003988 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003989 | KVM_VCPUEVENT_VALID_SHADOW
3990 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003991 if (vcpu->kvm->arch.exception_payload_enabled)
3992 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3993
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003994 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003995}
3996
Sean Christophersonc5833c72019-04-02 08:03:10 -07003997static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003998
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003999static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
4000 struct kvm_vcpu_events *events)
4001{
Jan Kiszkadab4b912009-12-06 18:24:15 +01004002 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01004003 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02004004 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07004005 | KVM_VCPUEVENT_VALID_SMM
4006 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004007 return -EINVAL;
4008
Jim Mattson59073aa2018-10-16 14:29:20 -07004009 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
4010 if (!vcpu->kvm->arch.exception_payload_enabled)
4011 return -EINVAL;
4012 if (events->exception.pending)
4013 events->exception.injected = 0;
4014 else
4015 events->exception_has_payload = 0;
4016 } else {
4017 events->exception.pending = 0;
4018 events->exception_has_payload = 0;
4019 }
4020
4021 if ((events->exception.injected || events->exception.pending) &&
4022 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02004023 return -EINVAL;
4024
David Hildenbrand28bf2882017-03-23 11:46:03 +01004025 /* INITs are latched while in SMM */
4026 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
4027 (events->smi.smm || events->smi.pending) &&
4028 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4029 return -EINVAL;
4030
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004031 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004032 vcpu->arch.exception.injected = events->exception.injected;
4033 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004034 vcpu->arch.exception.nr = events->exception.nr;
4035 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
4036 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004037 vcpu->arch.exception.has_payload = events->exception_has_payload;
4038 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004039
Liran Alon04140b42018-03-23 03:01:31 +03004040 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004041 vcpu->arch.interrupt.nr = events->interrupt.nr;
4042 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01004043 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004044 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01004045 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004046
4047 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01004048 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
4049 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004050 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004051
Jan Kiszka66450a22013-03-13 12:42:34 +01004052 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004053 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004054 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004055
Paolo Bonzinif0778252015-04-01 15:06:40 +02004056 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07004057 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
4058 if (events->smi.smm)
4059 vcpu->arch.hflags |= HF_SMM_MASK;
4060 else
4061 vcpu->arch.hflags &= ~HF_SMM_MASK;
4062 kvm_smm_changed(vcpu);
4063 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004064
Paolo Bonzinif0778252015-04-01 15:06:40 +02004065 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004066
4067 if (events->smi.smm) {
4068 if (events->smi.smm_inside_nmi)
4069 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004070 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004071 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004072 }
4073
4074 if (lapic_in_kernel(vcpu)) {
4075 if (events->smi.latched_init)
4076 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4077 else
4078 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004079 }
4080 }
4081
Avi Kivity3842d132010-07-27 12:30:24 +03004082 kvm_make_request(KVM_REQ_EVENT, vcpu);
4083
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004084 return 0;
4085}
4086
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004087static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4088 struct kvm_debugregs *dbgregs)
4089{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004090 unsigned long val;
4091
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004092 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004093 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004094 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004095 dbgregs->dr7 = vcpu->arch.dr7;
4096 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004097 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004098}
4099
4100static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4101 struct kvm_debugregs *dbgregs)
4102{
4103 if (dbgregs->flags)
4104 return -EINVAL;
4105
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004106 if (dbgregs->dr6 & ~0xffffffffull)
4107 return -EINVAL;
4108 if (dbgregs->dr7 & ~0xffffffffull)
4109 return -EINVAL;
4110
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004111 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004112 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004113 vcpu->arch.dr6 = dbgregs->dr6;
4114 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004115 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004116
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004117 return 0;
4118}
4119
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004120#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4121
4122static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4123{
Marc Orrb666a4b2018-11-06 14:53:56 -08004124 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004125 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004126 u64 valid;
4127
4128 /*
4129 * Copy legacy XSAVE area, to avoid complications with CPUID
4130 * leaves 0 and 1 in the loop below.
4131 */
4132 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4133
4134 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004135 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004136 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4137
4138 /*
4139 * Copy each region from the possibly compacted offset to the
4140 * non-compacted offset.
4141 */
Dave Hansend91cab72015-09-02 16:31:26 -07004142 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004143 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004144 u64 xfeature_mask = valid & -valid;
4145 int xfeature_nr = fls64(xfeature_mask) - 1;
4146 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004147
4148 if (src) {
4149 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004150 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004151 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004152 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004153 memcpy(dest + offset, &vcpu->arch.pkru,
4154 sizeof(vcpu->arch.pkru));
4155 else
4156 memcpy(dest + offset, src, size);
4157
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004158 }
4159
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004160 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004161 }
4162}
4163
4164static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4165{
Marc Orrb666a4b2018-11-06 14:53:56 -08004166 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004167 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4168 u64 valid;
4169
4170 /*
4171 * Copy legacy XSAVE area, to avoid complications with CPUID
4172 * leaves 0 and 1 in the loop below.
4173 */
4174 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4175
4176 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004177 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004178 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004179 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004180
4181 /*
4182 * Copy each region from the non-compacted offset to the
4183 * possibly compacted offset.
4184 */
Dave Hansend91cab72015-09-02 16:31:26 -07004185 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004186 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004187 u64 xfeature_mask = valid & -valid;
4188 int xfeature_nr = fls64(xfeature_mask) - 1;
4189 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004190
4191 if (dest) {
4192 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004193 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004194 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004195 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004196 memcpy(&vcpu->arch.pkru, src + offset,
4197 sizeof(vcpu->arch.pkru));
4198 else
4199 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004200 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004201
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004202 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004203 }
4204}
4205
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004206static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4207 struct kvm_xsave *guest_xsave)
4208{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004209 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004210 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4211 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004212 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004213 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004214 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004215 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004216 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004217 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004218 }
4219}
4220
Wanpeng Lia5758132017-05-11 02:58:55 -07004221#define XSAVE_MXCSR_OFFSET 24
4222
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004223static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4224 struct kvm_xsave *guest_xsave)
4225{
4226 u64 xstate_bv =
4227 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004228 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004229
Borislav Petkovd366bf72016-04-04 22:25:02 +02004230 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004231 /*
4232 * Here we allow setting states that are not present in
4233 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4234 * with old userspace.
4235 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004236 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004237 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004238 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004239 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004240 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4241 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004242 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004243 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004244 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004245 }
4246 return 0;
4247}
4248
4249static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4250 struct kvm_xcrs *guest_xcrs)
4251{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004252 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004253 guest_xcrs->nr_xcrs = 0;
4254 return;
4255 }
4256
4257 guest_xcrs->nr_xcrs = 1;
4258 guest_xcrs->flags = 0;
4259 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4260 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4261}
4262
4263static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4264 struct kvm_xcrs *guest_xcrs)
4265{
4266 int i, r = 0;
4267
Borislav Petkovd366bf72016-04-04 22:25:02 +02004268 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004269 return -EINVAL;
4270
4271 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4272 return -EINVAL;
4273
4274 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4275 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004276 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004277 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004278 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004279 break;
4280 }
4281 if (r)
4282 r = -EINVAL;
4283 return r;
4284}
4285
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004286/*
4287 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4288 * stopped by the hypervisor. This function will be called from the host only.
4289 * EINVAL is returned when the host attempts to set the flag for a guest that
4290 * does not support pv clocks.
4291 */
4292static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4293{
Andy Honig0b794592013-02-20 14:48:10 -08004294 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004295 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004296 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004297 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4298 return 0;
4299}
4300
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004301static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4302 struct kvm_enable_cap *cap)
4303{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004304 int r;
4305 uint16_t vmcs_version;
4306 void __user *user_ptr;
4307
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004308 if (cap->flags)
4309 return -EINVAL;
4310
4311 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004312 case KVM_CAP_HYPERV_SYNIC2:
4313 if (cap->args[0])
4314 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004315 /* fall through */
4316
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004317 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004318 if (!irqchip_in_kernel(vcpu->kvm))
4319 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004320 return kvm_hv_activate_synic(vcpu, cap->cap ==
4321 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004322 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004323 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004324 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004325 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004326 if (!r) {
4327 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4328 if (copy_to_user(user_ptr, &vmcs_version,
4329 sizeof(vmcs_version)))
4330 r = -EFAULT;
4331 }
4332 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004333 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004334 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004335 return -ENOTTY;
4336
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004337 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004338
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004339 default:
4340 return -EINVAL;
4341 }
4342}
4343
Carsten Otte313a3dc2007-10-11 19:16:52 +02004344long kvm_arch_vcpu_ioctl(struct file *filp,
4345 unsigned int ioctl, unsigned long arg)
4346{
4347 struct kvm_vcpu *vcpu = filp->private_data;
4348 void __user *argp = (void __user *)arg;
4349 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004350 union {
4351 struct kvm_lapic_state *lapic;
4352 struct kvm_xsave *xsave;
4353 struct kvm_xcrs *xcrs;
4354 void *buffer;
4355 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004356
Christoffer Dall9b0624712017-12-04 21:35:36 +01004357 vcpu_load(vcpu);
4358
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004359 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004360 switch (ioctl) {
4361 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004362 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004363 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004364 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004365 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4366 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004367
Dave Hansenb772ff32008-08-11 10:01:47 -07004368 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004369 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004370 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004371 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004372 if (r)
4373 goto out;
4374 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004375 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004376 goto out;
4377 r = 0;
4378 break;
4379 }
4380 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004381 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004382 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004383 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004384 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004385 if (IS_ERR(u.lapic)) {
4386 r = PTR_ERR(u.lapic);
4387 goto out_nofree;
4388 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004389
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004390 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004391 break;
4392 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004393 case KVM_INTERRUPT: {
4394 struct kvm_interrupt irq;
4395
4396 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004397 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004398 goto out;
4399 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004400 break;
4401 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004402 case KVM_NMI: {
4403 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004404 break;
4405 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004406 case KVM_SMI: {
4407 r = kvm_vcpu_ioctl_smi(vcpu);
4408 break;
4409 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004410 case KVM_SET_CPUID: {
4411 struct kvm_cpuid __user *cpuid_arg = argp;
4412 struct kvm_cpuid cpuid;
4413
4414 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004415 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004416 goto out;
4417 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004418 break;
4419 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004420 case KVM_SET_CPUID2: {
4421 struct kvm_cpuid2 __user *cpuid_arg = argp;
4422 struct kvm_cpuid2 cpuid;
4423
4424 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004425 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004426 goto out;
4427 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004428 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004429 break;
4430 }
4431 case KVM_GET_CPUID2: {
4432 struct kvm_cpuid2 __user *cpuid_arg = argp;
4433 struct kvm_cpuid2 cpuid;
4434
4435 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004436 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004437 goto out;
4438 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004439 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004440 if (r)
4441 goto out;
4442 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004443 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004444 goto out;
4445 r = 0;
4446 break;
4447 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004448 case KVM_GET_MSRS: {
4449 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004450 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004451 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004452 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004453 }
4454 case KVM_SET_MSRS: {
4455 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004456 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004457 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004458 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004459 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004460 case KVM_TPR_ACCESS_REPORTING: {
4461 struct kvm_tpr_access_ctl tac;
4462
4463 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004464 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004465 goto out;
4466 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4467 if (r)
4468 goto out;
4469 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004470 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004471 goto out;
4472 r = 0;
4473 break;
4474 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004475 case KVM_SET_VAPIC_ADDR: {
4476 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004477 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004478
4479 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004480 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004481 goto out;
4482 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004483 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004484 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004485 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004486 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004487 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004488 break;
4489 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004490 case KVM_X86_SETUP_MCE: {
4491 u64 mcg_cap;
4492
4493 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004494 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004495 goto out;
4496 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4497 break;
4498 }
4499 case KVM_X86_SET_MCE: {
4500 struct kvm_x86_mce mce;
4501
4502 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004503 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004504 goto out;
4505 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4506 break;
4507 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004508 case KVM_GET_VCPU_EVENTS: {
4509 struct kvm_vcpu_events events;
4510
4511 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4512
4513 r = -EFAULT;
4514 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4515 break;
4516 r = 0;
4517 break;
4518 }
4519 case KVM_SET_VCPU_EVENTS: {
4520 struct kvm_vcpu_events events;
4521
4522 r = -EFAULT;
4523 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4524 break;
4525
4526 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4527 break;
4528 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004529 case KVM_GET_DEBUGREGS: {
4530 struct kvm_debugregs dbgregs;
4531
4532 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4533
4534 r = -EFAULT;
4535 if (copy_to_user(argp, &dbgregs,
4536 sizeof(struct kvm_debugregs)))
4537 break;
4538 r = 0;
4539 break;
4540 }
4541 case KVM_SET_DEBUGREGS: {
4542 struct kvm_debugregs dbgregs;
4543
4544 r = -EFAULT;
4545 if (copy_from_user(&dbgregs, argp,
4546 sizeof(struct kvm_debugregs)))
4547 break;
4548
4549 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4550 break;
4551 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004552 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004553 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004554 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004555 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004556 break;
4557
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004558 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004559
4560 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004561 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004562 break;
4563 r = 0;
4564 break;
4565 }
4566 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004567 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004568 if (IS_ERR(u.xsave)) {
4569 r = PTR_ERR(u.xsave);
4570 goto out_nofree;
4571 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004572
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004573 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004574 break;
4575 }
4576 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004577 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004578 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004579 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004580 break;
4581
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004582 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004583
4584 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004585 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004586 sizeof(struct kvm_xcrs)))
4587 break;
4588 r = 0;
4589 break;
4590 }
4591 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004592 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004593 if (IS_ERR(u.xcrs)) {
4594 r = PTR_ERR(u.xcrs);
4595 goto out_nofree;
4596 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004597
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004598 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004599 break;
4600 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004601 case KVM_SET_TSC_KHZ: {
4602 u32 user_tsc_khz;
4603
4604 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004605 user_tsc_khz = (u32)arg;
4606
4607 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4608 goto out;
4609
Zachary Amsdencc578282012-02-03 15:43:50 -02004610 if (user_tsc_khz == 0)
4611 user_tsc_khz = tsc_khz;
4612
Haozhong Zhang381d5852015-10-20 15:39:04 +08004613 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4614 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004615
Joerg Roedel92a1f122011-03-25 09:44:51 +01004616 goto out;
4617 }
4618 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004619 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004620 goto out;
4621 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004622 case KVM_KVMCLOCK_CTRL: {
4623 r = kvm_set_guest_paused(vcpu);
4624 goto out;
4625 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004626 case KVM_ENABLE_CAP: {
4627 struct kvm_enable_cap cap;
4628
4629 r = -EFAULT;
4630 if (copy_from_user(&cap, argp, sizeof(cap)))
4631 goto out;
4632 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4633 break;
4634 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004635 case KVM_GET_NESTED_STATE: {
4636 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4637 u32 user_data_size;
4638
4639 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004640 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004641 break;
4642
4643 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004644 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004645 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004646 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004647
Paolo Bonzini33b22172020-04-17 10:24:18 -04004648 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
4649 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004650 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004651 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004652
4653 if (r > user_data_size) {
4654 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004655 r = -EFAULT;
4656 else
4657 r = -E2BIG;
4658 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004659 }
Liran Alon26b471c2018-09-16 14:28:20 +03004660
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004661 r = 0;
4662 break;
4663 }
4664 case KVM_SET_NESTED_STATE: {
4665 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4666 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004667 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004668
4669 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004670 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004671 break;
4672
Liran Alon26b471c2018-09-16 14:28:20 +03004673 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004674 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004675 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004676
Liran Alon26b471c2018-09-16 14:28:20 +03004677 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004678 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004679 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004680
4681 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004682 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04004683 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
4684 | KVM_STATE_NESTED_GIF_SET))
Liran Alon26b471c2018-09-16 14:28:20 +03004685 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004686
4687 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004688 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4689 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004690 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004691
Sean Christophersonad5996d2019-11-22 08:58:18 -08004692 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04004693 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004694 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004695 break;
4696 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004697 case KVM_GET_SUPPORTED_HV_CPUID: {
4698 struct kvm_cpuid2 __user *cpuid_arg = argp;
4699 struct kvm_cpuid2 cpuid;
4700
4701 r = -EFAULT;
4702 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4703 goto out;
4704
4705 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4706 cpuid_arg->entries);
4707 if (r)
4708 goto out;
4709
4710 r = -EFAULT;
4711 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4712 goto out;
4713 r = 0;
4714 break;
4715 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004716 default:
4717 r = -EINVAL;
4718 }
4719out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004720 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004721out_nofree:
4722 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004723 return r;
4724}
4725
Souptick Joarder1499fa82018-04-19 00:49:58 +05304726vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004727{
4728 return VM_FAULT_SIGBUS;
4729}
4730
Carsten Otte1fe779f2007-10-29 16:08:35 +01004731static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4732{
4733 int ret;
4734
4735 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004736 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004737 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004738 return ret;
4739}
4740
Sheng Yangb927a3c2009-07-21 10:42:48 +08004741static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4742 u64 ident_addr)
4743{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004744 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004745}
4746
Carsten Otte1fe779f2007-10-29 16:08:35 +01004747static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004748 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004749{
4750 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4751 return -EINVAL;
4752
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004753 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004754
4755 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004756 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004757
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004758 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004759 return 0;
4760}
4761
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004762static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004763{
Dave Hansen39de71e2010-08-19 18:11:14 -07004764 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004765}
4766
Carsten Otte1fe779f2007-10-29 16:08:35 +01004767static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4768{
David Hildenbrand90bca052017-04-07 10:50:23 +02004769 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004770 int r;
4771
4772 r = 0;
4773 switch (chip->chip_id) {
4774 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004775 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004776 sizeof(struct kvm_pic_state));
4777 break;
4778 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004779 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004780 sizeof(struct kvm_pic_state));
4781 break;
4782 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004783 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004784 break;
4785 default:
4786 r = -EINVAL;
4787 break;
4788 }
4789 return r;
4790}
4791
4792static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4793{
David Hildenbrand90bca052017-04-07 10:50:23 +02004794 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004795 int r;
4796
4797 r = 0;
4798 switch (chip->chip_id) {
4799 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004800 spin_lock(&pic->lock);
4801 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004802 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004803 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004804 break;
4805 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004806 spin_lock(&pic->lock);
4807 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004808 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004809 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004810 break;
4811 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004812 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004813 break;
4814 default:
4815 r = -EINVAL;
4816 break;
4817 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004818 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004819 return r;
4820}
4821
Sheng Yange0f63cb2008-03-04 00:50:59 +08004822static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4823{
Radim Krčmář34f39412016-03-02 22:56:50 +01004824 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4825
4826 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4827
4828 mutex_lock(&kps->lock);
4829 memcpy(ps, &kps->channels, sizeof(*ps));
4830 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304831 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004832}
4833
4834static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4835{
Andrew Honig01856042015-11-18 14:50:23 -08004836 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004837 struct kvm_pit *pit = kvm->arch.vpit;
4838
4839 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004840 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004841 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004842 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4843 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304844 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004845}
4846
4847static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4848{
Beth Kone9f42752009-07-07 11:50:38 -04004849 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4850 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4851 sizeof(ps->channels));
4852 ps->flags = kvm->arch.vpit->pit_state.flags;
4853 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004854 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304855 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004856}
4857
4858static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4859{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304860 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004861 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004862 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004863 struct kvm_pit *pit = kvm->arch.vpit;
4864
4865 mutex_lock(&pit->pit_state.lock);
4866 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004867 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4868 if (!prev_legacy && cur_legacy)
4869 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004870 memcpy(&pit->pit_state.channels, &ps->channels,
4871 sizeof(pit->pit_state.channels));
4872 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004873 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004874 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004875 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004876 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304877 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004878}
4879
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004880static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4881 struct kvm_reinject_control *control)
4882{
Radim Krčmář71474e22016-03-02 22:56:45 +01004883 struct kvm_pit *pit = kvm->arch.vpit;
4884
Radim Krčmář71474e22016-03-02 22:56:45 +01004885 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4886 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4887 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4888 */
4889 mutex_lock(&pit->pit_state.lock);
4890 kvm_pit_set_reinject(pit, control->pit_reinject);
4891 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004892
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004893 return 0;
4894}
4895
Sean Christopherson0dff0842020-02-18 13:07:29 -08004896void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004897{
Kai Huang88178fd2015-01-28 10:54:27 +08004898 /*
4899 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4900 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004901 if (kvm_x86_ops.flush_log_dirty)
4902 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004903}
4904
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004905int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4906 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004907{
4908 if (!irqchip_in_kernel(kvm))
4909 return -ENXIO;
4910
4911 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004912 irq_event->irq, irq_event->level,
4913 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004914 return 0;
4915}
4916
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004917int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4918 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004919{
4920 int r;
4921
4922 if (cap->flags)
4923 return -EINVAL;
4924
4925 switch (cap->cap) {
4926 case KVM_CAP_DISABLE_QUIRKS:
4927 kvm->arch.disabled_quirks = cap->args[0];
4928 r = 0;
4929 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004930 case KVM_CAP_SPLIT_IRQCHIP: {
4931 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004932 r = -EINVAL;
4933 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4934 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004935 r = -EEXIST;
4936 if (irqchip_in_kernel(kvm))
4937 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004938 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004939 goto split_irqchip_unlock;
4940 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004941 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004942 goto split_irqchip_unlock;
4943 /* Pairs with irqchip_in_kernel. */
4944 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004945 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004946 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004947 r = 0;
4948split_irqchip_unlock:
4949 mutex_unlock(&kvm->lock);
4950 break;
4951 }
Radim Krčmář371313132016-07-12 22:09:27 +02004952 case KVM_CAP_X2APIC_API:
4953 r = -EINVAL;
4954 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4955 break;
4956
4957 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4958 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004959 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4960 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004961
4962 r = 0;
4963 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004964 case KVM_CAP_X86_DISABLE_EXITS:
4965 r = -EINVAL;
4966 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4967 break;
4968
4969 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4970 kvm_can_mwait_in_guest())
4971 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004972 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004973 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004974 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4975 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004976 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4977 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004978 r = 0;
4979 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004980 case KVM_CAP_MSR_PLATFORM_INFO:
4981 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4982 r = 0;
4983 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004984 case KVM_CAP_EXCEPTION_PAYLOAD:
4985 kvm->arch.exception_payload_enabled = cap->args[0];
4986 r = 0;
4987 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004988 default:
4989 r = -EINVAL;
4990 break;
4991 }
4992 return r;
4993}
4994
Carsten Otte1fe779f2007-10-29 16:08:35 +01004995long kvm_arch_vm_ioctl(struct file *filp,
4996 unsigned int ioctl, unsigned long arg)
4997{
4998 struct kvm *kvm = filp->private_data;
4999 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03005000 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07005001 /*
5002 * This union makes it completely explicit to gcc-3.x
5003 * that these two variables' stack usage should be
5004 * combined, not added together.
5005 */
5006 union {
5007 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04005008 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005009 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07005010 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005011
5012 switch (ioctl) {
5013 case KVM_SET_TSS_ADDR:
5014 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005015 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005016 case KVM_SET_IDENTITY_MAP_ADDR: {
5017 u64 ident_addr;
5018
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005019 mutex_lock(&kvm->lock);
5020 r = -EINVAL;
5021 if (kvm->created_vcpus)
5022 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005023 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005024 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005025 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005026 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005027set_identity_unlock:
5028 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005029 break;
5030 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01005031 case KVM_SET_NR_MMU_PAGES:
5032 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005033 break;
5034 case KVM_GET_NR_MMU_PAGES:
5035 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
5036 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005037 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005038 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01005039
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005040 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01005041 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005042 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005043
Avi Kivity3e515702012-03-05 14:23:29 +02005044 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005045 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02005046 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005047
5048 r = kvm_pic_init(kvm);
5049 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005050 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005051
5052 r = kvm_ioapic_init(kvm);
5053 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005054 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005055 goto create_irqchip_unlock;
5056 }
5057
Avi Kivity399ec802008-11-19 13:58:46 +02005058 r = kvm_setup_default_irq_routing(kvm);
5059 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005060 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005061 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005062 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005063 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005064 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005065 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005066 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005067 create_irqchip_unlock:
5068 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005069 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005070 }
Sheng Yang78376992008-01-28 05:10:22 +08005071 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005072 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5073 goto create_pit;
5074 case KVM_CREATE_PIT2:
5075 r = -EFAULT;
5076 if (copy_from_user(&u.pit_config, argp,
5077 sizeof(struct kvm_pit_config)))
5078 goto out;
5079 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005080 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005081 r = -EEXIST;
5082 if (kvm->arch.vpit)
5083 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005084 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005085 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005086 if (kvm->arch.vpit)
5087 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005088 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005089 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005090 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005091 case KVM_GET_IRQCHIP: {
5092 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005093 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005094
Sasha Levinff5c2c02011-12-04 19:36:29 +02005095 chip = memdup_user(argp, sizeof(*chip));
5096 if (IS_ERR(chip)) {
5097 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005098 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005099 }
5100
Carsten Otte1fe779f2007-10-29 16:08:35 +01005101 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005102 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005103 goto get_irqchip_out;
5104 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5105 if (r)
5106 goto get_irqchip_out;
5107 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005108 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005109 goto get_irqchip_out;
5110 r = 0;
5111 get_irqchip_out:
5112 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005113 break;
5114 }
5115 case KVM_SET_IRQCHIP: {
5116 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005117 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005118
Sasha Levinff5c2c02011-12-04 19:36:29 +02005119 chip = memdup_user(argp, sizeof(*chip));
5120 if (IS_ERR(chip)) {
5121 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005122 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005123 }
5124
Carsten Otte1fe779f2007-10-29 16:08:35 +01005125 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005126 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005127 goto set_irqchip_out;
5128 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005129 set_irqchip_out:
5130 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005131 break;
5132 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005133 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005134 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005135 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005136 goto out;
5137 r = -ENXIO;
5138 if (!kvm->arch.vpit)
5139 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005140 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005141 if (r)
5142 goto out;
5143 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005144 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005145 goto out;
5146 r = 0;
5147 break;
5148 }
5149 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005150 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005151 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005152 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005153 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005154 r = -ENXIO;
5155 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005156 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005157 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005158set_pit_out:
5159 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005160 break;
5161 }
Beth Kone9f42752009-07-07 11:50:38 -04005162 case KVM_GET_PIT2: {
5163 r = -ENXIO;
5164 if (!kvm->arch.vpit)
5165 goto out;
5166 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5167 if (r)
5168 goto out;
5169 r = -EFAULT;
5170 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5171 goto out;
5172 r = 0;
5173 break;
5174 }
5175 case KVM_SET_PIT2: {
5176 r = -EFAULT;
5177 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5178 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005179 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005180 r = -ENXIO;
5181 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005182 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005183 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005184set_pit2_out:
5185 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005186 break;
5187 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005188 case KVM_REINJECT_CONTROL: {
5189 struct kvm_reinject_control control;
5190 r = -EFAULT;
5191 if (copy_from_user(&control, argp, sizeof(control)))
5192 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005193 r = -ENXIO;
5194 if (!kvm->arch.vpit)
5195 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005196 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005197 break;
5198 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005199 case KVM_SET_BOOT_CPU_ID:
5200 r = 0;
5201 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005202 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005203 r = -EBUSY;
5204 else
5205 kvm->arch.bsp_vcpu_id = arg;
5206 mutex_unlock(&kvm->lock);
5207 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005208 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005209 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005210 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005211 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005212 goto out;
5213 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005214 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005215 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005216 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005217 r = 0;
5218 break;
5219 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005220 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005221 struct kvm_clock_data user_ns;
5222 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005223
5224 r = -EFAULT;
5225 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5226 goto out;
5227
5228 r = -EINVAL;
5229 if (user_ns.flags)
5230 goto out;
5231
5232 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005233 /*
5234 * TODO: userspace has to take care of races with VCPU_RUN, so
5235 * kvm_gen_update_masterclock() can be cut down to locked
5236 * pvclock_update_vm_gtod_copy().
5237 */
5238 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005239 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005240 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005241 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005242 break;
5243 }
5244 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005245 struct kvm_clock_data user_ns;
5246 u64 now_ns;
5247
Marcelo Tosattie891a322017-04-17 12:51:37 -03005248 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005249 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005250 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005251 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005252
5253 r = -EFAULT;
5254 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5255 goto out;
5256 r = 0;
5257 break;
5258 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005259 case KVM_MEMORY_ENCRYPT_OP: {
5260 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005261 if (kvm_x86_ops.mem_enc_op)
5262 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005263 break;
5264 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005265 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5266 struct kvm_enc_region region;
5267
5268 r = -EFAULT;
5269 if (copy_from_user(&region, argp, sizeof(region)))
5270 goto out;
5271
5272 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005273 if (kvm_x86_ops.mem_enc_reg_region)
5274 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005275 break;
5276 }
5277 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5278 struct kvm_enc_region region;
5279
5280 r = -EFAULT;
5281 if (copy_from_user(&region, argp, sizeof(region)))
5282 goto out;
5283
5284 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005285 if (kvm_x86_ops.mem_enc_unreg_region)
5286 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005287 break;
5288 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005289 case KVM_HYPERV_EVENTFD: {
5290 struct kvm_hyperv_eventfd hvevfd;
5291
5292 r = -EFAULT;
5293 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5294 goto out;
5295 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5296 break;
5297 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005298 case KVM_SET_PMU_EVENT_FILTER:
5299 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5300 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005301 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005302 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005303 }
5304out:
5305 return r;
5306}
5307
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005308static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005309{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005310 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005311 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005312 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005313
Jim Mattsone2ada662019-08-21 11:20:04 -07005314 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005315 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005316
5317 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005318
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005319 num_msrs_to_save = 0;
5320 num_emulated_msrs = 0;
5321 num_msr_based_features = 0;
5322
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005323 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5324 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005325 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005326
5327 /*
5328 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005329 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005330 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005331 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005332 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005333 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005334 continue;
5335 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005336 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005337 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005338 continue;
5339 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03005340 case MSR_IA32_UMWAIT_CONTROL:
5341 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
5342 continue;
5343 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005344 case MSR_IA32_RTIT_CTL:
5345 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005346 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005347 continue;
5348 break;
5349 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005350 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005351 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5352 continue;
5353 break;
5354 case MSR_IA32_RTIT_OUTPUT_BASE:
5355 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005356 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005357 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5358 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5359 continue;
5360 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005361 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005362 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005363 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005364 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5365 continue;
5366 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005367 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005368 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005369 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5370 continue;
5371 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005372 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005373 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005374 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5375 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005376 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005377 default:
5378 break;
5379 }
5380
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005381 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005382 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005383
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005384 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005385 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005386 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005387
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005388 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005389 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005390
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005391 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005392 struct kvm_msr_entry msr;
5393
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005394 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005395 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005396 continue;
5397
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005398 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005399 }
Carsten Otte043405e2007-10-10 17:16:19 +02005400}
5401
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005402static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5403 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005404{
Avi Kivity70252a12010-01-19 12:51:22 +02005405 int handled = 0;
5406 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005407
Avi Kivity70252a12010-01-19 12:51:22 +02005408 do {
5409 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005410 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005411 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5412 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005413 break;
5414 handled += n;
5415 addr += n;
5416 len -= n;
5417 v += n;
5418 } while (len);
5419
5420 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005421}
5422
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005423static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005424{
Avi Kivity70252a12010-01-19 12:51:22 +02005425 int handled = 0;
5426 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005427
Avi Kivity70252a12010-01-19 12:51:22 +02005428 do {
5429 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005430 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005431 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5432 addr, n, v))
5433 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005434 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005435 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005436 handled += n;
5437 addr += n;
5438 len -= n;
5439 v += n;
5440 } while (len);
5441
5442 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005443}
5444
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005445static void kvm_set_segment(struct kvm_vcpu *vcpu,
5446 struct kvm_segment *var, int seg)
5447{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005448 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005449}
5450
5451void kvm_get_segment(struct kvm_vcpu *vcpu,
5452 struct kvm_segment *var, int seg)
5453{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005454 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005455}
5456
Paolo Bonzini54987b72014-09-02 13:23:06 +02005457gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5458 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005459{
5460 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005461
5462 BUG_ON(!mmu_is_nested(vcpu));
5463
5464 /* NPT walks are always user-walks */
5465 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005466 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005467
5468 return t_gpa;
5469}
5470
Avi Kivityab9ae312010-11-22 17:53:26 +02005471gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5472 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005473{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005474 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005475 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005476}
5477
Avi Kivityab9ae312010-11-22 17:53:26 +02005478 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5479 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005480{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005481 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005482 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005483 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005484}
5485
Avi Kivityab9ae312010-11-22 17:53:26 +02005486gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5487 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005488{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005489 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005490 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005491 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005492}
5493
5494/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005495gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5496 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005497{
Avi Kivityab9ae312010-11-22 17:53:26 +02005498 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005499}
5500
5501static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5502 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005503 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005504{
5505 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005506 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005507
5508 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005509 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005510 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005511 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005512 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005513 int ret;
5514
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005515 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005516 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005517 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5518 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005519 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005520 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005521 goto out;
5522 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005523
Izik Eidus77c20022008-12-29 01:42:19 +02005524 bytes -= toread;
5525 data += toread;
5526 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005527 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005528out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005529 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005530}
Izik Eidus77c20022008-12-29 01:42:19 +02005531
Gleb Natapov1871c602010-02-10 14:21:32 +02005532/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005533static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5534 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005535 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005536{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005537 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005538 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005539 unsigned offset;
5540 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005541
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005542 /* Inline kvm_read_guest_virt_helper for speed. */
5543 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5544 exception);
5545 if (unlikely(gpa == UNMAPPED_GVA))
5546 return X86EMUL_PROPAGATE_FAULT;
5547
5548 offset = addr & (PAGE_SIZE-1);
5549 if (WARN_ON(offset + bytes > PAGE_SIZE))
5550 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005551 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5552 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005553 if (unlikely(ret < 0))
5554 return X86EMUL_IO_NEEDED;
5555
5556 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005557}
5558
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005559int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005560 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005561 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005562{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005563 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005564
Paolo Bonzini353c0952019-01-29 18:41:16 +01005565 /*
5566 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5567 * is returned, but our callers are not ready for that and they blindly
5568 * call kvm_inject_page_fault. Ensure that they at least do not leak
5569 * uninitialized kernel stack memory into cr2 and error code.
5570 */
5571 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005572 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005573 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005574}
Nadav Har'El064aea72011-05-25 23:04:56 +03005575EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005576
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005577static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5578 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005579 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005580{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005581 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005582 u32 access = 0;
5583
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005584 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005585 access |= PFERR_USER_MASK;
5586
5587 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005588}
5589
Radim Krčmář7a036a62015-10-30 16:36:24 +01005590static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5591 unsigned long addr, void *val, unsigned int bytes)
5592{
5593 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5594 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5595
5596 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5597}
5598
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005599static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5600 struct kvm_vcpu *vcpu, u32 access,
5601 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005602{
5603 void *data = val;
5604 int r = X86EMUL_CONTINUE;
5605
5606 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005607 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005608 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005609 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005610 unsigned offset = addr & (PAGE_SIZE-1);
5611 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5612 int ret;
5613
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005614 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005615 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005616 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005617 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005618 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005619 goto out;
5620 }
5621
5622 bytes -= towrite;
5623 data += towrite;
5624 addr += towrite;
5625 }
5626out:
5627 return r;
5628}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005629
5630static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005631 unsigned int bytes, struct x86_exception *exception,
5632 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005633{
5634 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005635 u32 access = PFERR_WRITE_MASK;
5636
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005637 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005638 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005639
5640 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005641 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005642}
5643
5644int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5645 unsigned int bytes, struct x86_exception *exception)
5646{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005647 /* kvm_write_guest_virt_system can pull in tons of pages. */
5648 vcpu->arch.l1tf_flush_l1d = true;
5649
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005650 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5651 PFERR_WRITE_MASK, exception);
5652}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005653EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005654
Wanpeng Li082d06e2018-04-03 16:28:48 -07005655int handle_ud(struct kvm_vcpu *vcpu)
5656{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005657 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005658 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005659 char sig[5]; /* ud2; .ascii "kvm" */
5660 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005661
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005662 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005663 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5664 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005665 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005666 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005667 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005668 }
5669
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005670 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005671}
5672EXPORT_SYMBOL_GPL(handle_ud);
5673
Tom Lendacky0f89b202016-12-14 14:59:23 -05005674static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5675 gpa_t gpa, bool write)
5676{
5677 /* For APIC access vmexit */
5678 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5679 return 1;
5680
5681 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5682 trace_vcpu_match_mmio(gva, gpa, write, true);
5683 return 1;
5684 }
5685
5686 return 0;
5687}
5688
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005689static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5690 gpa_t *gpa, struct x86_exception *exception,
5691 bool write)
5692{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005693 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03005694 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005695
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005696 /*
5697 * currently PKRU is only applied to ept enabled guest so
5698 * there is no pkey in EPT page table for L1 guest or EPT
5699 * shadow page table for L2 guest.
5700 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005701 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005702 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005703 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005704 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5705 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005706 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005707 return 1;
5708 }
5709
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005710 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5711
5712 if (*gpa == UNMAPPED_GVA)
5713 return -1;
5714
Tom Lendacky0f89b202016-12-14 14:59:23 -05005715 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005716}
5717
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005718int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005719 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005720{
5721 int ret;
5722
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005723 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005724 if (ret < 0)
5725 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005726 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005727 return 1;
5728}
5729
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005730struct read_write_emulator_ops {
5731 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5732 int bytes);
5733 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5734 void *val, int bytes);
5735 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5736 int bytes, void *val);
5737 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5738 void *val, int bytes);
5739 bool write;
5740};
5741
5742static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5743{
5744 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005745 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005746 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005747 vcpu->mmio_read_completed = 0;
5748 return 1;
5749 }
5750
5751 return 0;
5752}
5753
5754static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5755 void *val, int bytes)
5756{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005757 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005758}
5759
5760static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5761 void *val, int bytes)
5762{
5763 return emulator_write_phys(vcpu, gpa, val, bytes);
5764}
5765
5766static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5767{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005768 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005769 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5770}
5771
5772static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5773 void *val, int bytes)
5774{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005775 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005776 return X86EMUL_IO_NEEDED;
5777}
5778
5779static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5780 void *val, int bytes)
5781{
Avi Kivityf78146b2012-04-18 19:22:47 +03005782 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5783
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005784 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005785 return X86EMUL_CONTINUE;
5786}
5787
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005788static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005789 .read_write_prepare = read_prepare,
5790 .read_write_emulate = read_emulate,
5791 .read_write_mmio = vcpu_mmio_read,
5792 .read_write_exit_mmio = read_exit_mmio,
5793};
5794
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005795static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005796 .read_write_emulate = write_emulate,
5797 .read_write_mmio = write_mmio,
5798 .read_write_exit_mmio = write_exit_mmio,
5799 .write = true,
5800};
5801
Xiao Guangrong22388a32011-07-13 14:32:31 +08005802static int emulator_read_write_onepage(unsigned long addr, void *val,
5803 unsigned int bytes,
5804 struct x86_exception *exception,
5805 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005806 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005807{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005808 gpa_t gpa;
5809 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005810 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005811 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005812 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005813
5814 /*
5815 * If the exit was due to a NPF we may already have a GPA.
5816 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5817 * Note, this cannot be used on string operations since string
5818 * operation using rep will only have the initial GPA from the NPF
5819 * occurred.
5820 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005821 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5822 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5823 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005824 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5825 } else {
5826 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5827 if (ret < 0)
5828 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005829 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005830
Brijesh Singh618232e2017-08-17 18:36:57 +02005831 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005832 return X86EMUL_CONTINUE;
5833
Carsten Ottebbd9b642007-10-30 18:44:21 +01005834 /*
5835 * Is this MMIO handled locally?
5836 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005837 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005838 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005839 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005840
Avi Kivity70252a12010-01-19 12:51:22 +02005841 gpa += handled;
5842 bytes -= handled;
5843 val += handled;
5844
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005845 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5846 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5847 frag->gpa = gpa;
5848 frag->data = val;
5849 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005850 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005851}
5852
Xiubo Li52eb5a62015-03-13 17:39:45 +08005853static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5854 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005855 void *val, unsigned int bytes,
5856 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005857 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005858{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005859 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005860 gpa_t gpa;
5861 int rc;
5862
5863 if (ops->read_write_prepare &&
5864 ops->read_write_prepare(vcpu, val, bytes))
5865 return X86EMUL_CONTINUE;
5866
5867 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005868
Carsten Ottebbd9b642007-10-30 18:44:21 +01005869 /* Crossing a page boundary? */
5870 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005871 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005872
5873 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005874 rc = emulator_read_write_onepage(addr, val, now, exception,
5875 vcpu, ops);
5876
Carsten Ottebbd9b642007-10-30 18:44:21 +01005877 if (rc != X86EMUL_CONTINUE)
5878 return rc;
5879 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005880 if (ctxt->mode != X86EMUL_MODE_PROT64)
5881 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005882 val += now;
5883 bytes -= now;
5884 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005885
Avi Kivityf78146b2012-04-18 19:22:47 +03005886 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5887 vcpu, ops);
5888 if (rc != X86EMUL_CONTINUE)
5889 return rc;
5890
5891 if (!vcpu->mmio_nr_fragments)
5892 return rc;
5893
5894 gpa = vcpu->mmio_fragments[0].gpa;
5895
5896 vcpu->mmio_needed = 1;
5897 vcpu->mmio_cur_fragment = 0;
5898
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005899 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005900 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5901 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5902 vcpu->run->mmio.phys_addr = gpa;
5903
5904 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005905}
5906
5907static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5908 unsigned long addr,
5909 void *val,
5910 unsigned int bytes,
5911 struct x86_exception *exception)
5912{
5913 return emulator_read_write(ctxt, addr, val, bytes,
5914 exception, &read_emultor);
5915}
5916
Xiubo Li52eb5a62015-03-13 17:39:45 +08005917static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005918 unsigned long addr,
5919 const void *val,
5920 unsigned int bytes,
5921 struct x86_exception *exception)
5922{
5923 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5924 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005925}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005926
Avi Kivitydaea3e72010-03-15 13:59:54 +02005927#define CMPXCHG_TYPE(t, ptr, old, new) \
5928 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5929
5930#ifdef CONFIG_X86_64
5931# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5932#else
5933# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005934 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005935#endif
5936
Avi Kivity0f65dd72011-04-20 13:37:53 +03005937static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5938 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005939 const void *old,
5940 const void *new,
5941 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005942 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005943{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005944 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005945 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005946 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005947 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005948 char *kaddr;
5949 bool exchanged;
5950
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005951 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005952 if (bytes > 8 || (bytes & (bytes - 1)))
5953 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005954
Avi Kivitydaea3e72010-03-15 13:59:54 +02005955 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005956
Avi Kivitydaea3e72010-03-15 13:59:54 +02005957 if (gpa == UNMAPPED_GVA ||
5958 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5959 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005960
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005961 /*
5962 * Emulate the atomic as a straight write to avoid #AC if SLD is
5963 * enabled in the host and the access splits a cache line.
5964 */
5965 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
5966 page_line_mask = ~(cache_line_size() - 1);
5967 else
5968 page_line_mask = PAGE_MASK;
5969
5970 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005971 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005972
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005973 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005974 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005975
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005976 kaddr = map.hva + offset_in_page(gpa);
5977
Avi Kivitydaea3e72010-03-15 13:59:54 +02005978 switch (bytes) {
5979 case 1:
5980 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5981 break;
5982 case 2:
5983 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5984 break;
5985 case 4:
5986 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5987 break;
5988 case 8:
5989 exchanged = CMPXCHG64(kaddr, old, new);
5990 break;
5991 default:
5992 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005993 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005994
5995 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005996
5997 if (!exchanged)
5998 return X86EMUL_CMPXCHG_FAILED;
5999
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006000 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03006001
6002 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02006003
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006004emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02006005 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006006
Avi Kivity0f65dd72011-04-20 13:37:53 +03006007 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006008}
6009
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006010static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
6011{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006012 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006013
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006014 for (i = 0; i < vcpu->arch.pio.count; i++) {
6015 if (vcpu->arch.pio.in)
6016 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
6017 vcpu->arch.pio.size, pd);
6018 else
6019 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
6020 vcpu->arch.pio.port, vcpu->arch.pio.size,
6021 pd);
6022 if (r)
6023 break;
6024 pd += vcpu->arch.pio.size;
6025 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006026 return r;
6027}
6028
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006029static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6030 unsigned short port, void *val,
6031 unsigned int count, bool in)
6032{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006033 vcpu->arch.pio.port = port;
6034 vcpu->arch.pio.in = in;
6035 vcpu->arch.pio.count = count;
6036 vcpu->arch.pio.size = size;
6037
6038 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6039 vcpu->arch.pio.count = 0;
6040 return 1;
6041 }
6042
6043 vcpu->run->exit_reason = KVM_EXIT_IO;
6044 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
6045 vcpu->run->io.size = size;
6046 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6047 vcpu->run->io.count = count;
6048 vcpu->run->io.port = port;
6049
6050 return 0;
6051}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006052
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006053static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6054 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006055{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006056 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006057
Gleb Natapov79729952010-03-18 15:20:24 +02006058 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006059 goto data_avail;
6060
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006061 memset(vcpu->arch.pio_data, 0, size * count);
6062
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006063 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6064 if (ret) {
6065data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006066 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006067 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006068 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006069 return 1;
6070 }
6071
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006072 return 0;
6073}
6074
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006075static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6076 int size, unsigned short port, void *val,
6077 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006078{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006079 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006080
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006081}
6082
6083static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6084 unsigned short port, const void *val,
6085 unsigned int count)
6086{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006087 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006088 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006089 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006090}
6091
Carsten Ottebbd9b642007-10-30 18:44:21 +01006092static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6093 int size, unsigned short port,
6094 const void *val, unsigned int count)
6095{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006096 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006097}
6098
6099static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6100{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006101 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006102}
6103
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006104static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006105{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006106 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006107}
6108
Jiang Biaoae6a2372016-11-07 08:54:51 +08006109static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006110{
6111 if (!need_emulate_wbinvd(vcpu))
6112 return X86EMUL_CONTINUE;
6113
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006114 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006115 int cpu = get_cpu();
6116
6117 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006118 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6119 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006120 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006121 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006122 } else
6123 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006124 return X86EMUL_CONTINUE;
6125}
Joel Schopp5cb56052015-03-02 13:43:31 -06006126
6127int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6128{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006129 kvm_emulate_wbinvd_noskip(vcpu);
6130 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006131}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006132EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6133
Joel Schopp5cb56052015-03-02 13:43:31 -06006134
6135
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006136static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6137{
Joel Schopp5cb56052015-03-02 13:43:31 -06006138 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006139}
6140
Xiubo Li52eb5a62015-03-13 17:39:45 +08006141static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6142 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006143{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006144 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006145}
6146
Xiubo Li52eb5a62015-03-13 17:39:45 +08006147static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6148 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006149{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006150
Avi Kivity717746e2011-04-20 13:37:53 +03006151 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006152}
6153
Gleb Natapov52a46612010-03-18 15:20:03 +02006154static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6155{
6156 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6157}
6158
Avi Kivity717746e2011-04-20 13:37:53 +03006159static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006160{
Avi Kivity717746e2011-04-20 13:37:53 +03006161 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006162 unsigned long value;
6163
6164 switch (cr) {
6165 case 0:
6166 value = kvm_read_cr0(vcpu);
6167 break;
6168 case 2:
6169 value = vcpu->arch.cr2;
6170 break;
6171 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006172 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006173 break;
6174 case 4:
6175 value = kvm_read_cr4(vcpu);
6176 break;
6177 case 8:
6178 value = kvm_get_cr8(vcpu);
6179 break;
6180 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006181 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006182 return 0;
6183 }
6184
6185 return value;
6186}
6187
Avi Kivity717746e2011-04-20 13:37:53 +03006188static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006189{
Avi Kivity717746e2011-04-20 13:37:53 +03006190 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006191 int res = 0;
6192
Gleb Natapov52a46612010-03-18 15:20:03 +02006193 switch (cr) {
6194 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006195 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006196 break;
6197 case 2:
6198 vcpu->arch.cr2 = val;
6199 break;
6200 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006201 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006202 break;
6203 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006204 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006205 break;
6206 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006207 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006208 break;
6209 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006210 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006211 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006212 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006213
6214 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006215}
6216
Avi Kivity717746e2011-04-20 13:37:53 +03006217static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006218{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006219 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006220}
6221
Avi Kivity4bff1e862011-04-20 13:37:53 +03006222static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006223{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006224 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006225}
6226
Avi Kivity4bff1e862011-04-20 13:37:53 +03006227static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006228{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006229 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006230}
6231
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006232static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6233{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006234 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006235}
6236
6237static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6238{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006239 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006240}
6241
Avi Kivity4bff1e862011-04-20 13:37:53 +03006242static unsigned long emulator_get_cached_segment_base(
6243 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006244{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006245 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006246}
6247
Avi Kivity1aa36612011-04-27 13:20:30 +03006248static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6249 struct desc_struct *desc, u32 *base3,
6250 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006251{
6252 struct kvm_segment var;
6253
Avi Kivity4bff1e862011-04-20 13:37:53 +03006254 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006255 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006256
Gleb Natapov378a8b02013-01-21 15:36:48 +02006257 if (var.unusable) {
6258 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006259 if (base3)
6260 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006261 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006262 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006263
6264 if (var.g)
6265 var.limit >>= 12;
6266 set_desc_limit(desc, var.limit);
6267 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006268#ifdef CONFIG_X86_64
6269 if (base3)
6270 *base3 = var.base >> 32;
6271#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006272 desc->type = var.type;
6273 desc->s = var.s;
6274 desc->dpl = var.dpl;
6275 desc->p = var.present;
6276 desc->avl = var.avl;
6277 desc->l = var.l;
6278 desc->d = var.db;
6279 desc->g = var.g;
6280
6281 return true;
6282}
6283
Avi Kivity1aa36612011-04-27 13:20:30 +03006284static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6285 struct desc_struct *desc, u32 base3,
6286 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006287{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006288 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006289 struct kvm_segment var;
6290
Avi Kivity1aa36612011-04-27 13:20:30 +03006291 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006292 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006293#ifdef CONFIG_X86_64
6294 var.base |= ((u64)base3) << 32;
6295#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006296 var.limit = get_desc_limit(desc);
6297 if (desc->g)
6298 var.limit = (var.limit << 12) | 0xfff;
6299 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006300 var.dpl = desc->dpl;
6301 var.db = desc->d;
6302 var.s = desc->s;
6303 var.l = desc->l;
6304 var.g = desc->g;
6305 var.avl = desc->avl;
6306 var.present = desc->p;
6307 var.unusable = !var.present;
6308 var.padding = 0;
6309
6310 kvm_set_segment(vcpu, &var, seg);
6311 return;
6312}
6313
Avi Kivity717746e2011-04-20 13:37:53 +03006314static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6315 u32 msr_index, u64 *pdata)
6316{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006317 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006318}
6319
6320static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6321 u32 msr_index, u64 data)
6322{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006323 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006324}
6325
Paolo Bonzini64d60672015-05-07 11:36:11 +02006326static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6327{
6328 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6329
6330 return vcpu->arch.smbase;
6331}
6332
6333static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6334{
6335 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6336
6337 vcpu->arch.smbase = smbase;
6338}
6339
Nadav Amit67f4d422014-06-02 18:34:09 +03006340static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6341 u32 pmc)
6342{
Like Xu98ff80f2019-10-27 18:52:40 +08006343 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006344}
6345
Avi Kivity222d21a2011-11-10 14:57:30 +02006346static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6347 u32 pmc, u64 *pdata)
6348{
Wei Huangc6702c92015-06-19 13:44:45 +02006349 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006350}
6351
Avi Kivity6c3287f2011-04-20 15:43:05 +03006352static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6353{
6354 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6355}
6356
Avi Kivity29535382011-04-20 13:37:53 +03006357static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006358 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006359 enum x86_intercept_stage stage)
6360{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006361 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006362 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006363}
6364
Yu Zhange911eb32017-08-24 20:27:52 +08006365static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006366 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6367 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006368{
Sean Christophersonf91af512020-03-04 17:34:37 -08006369 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006370}
6371
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006372static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6373{
6374 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6375}
6376
6377static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6378{
6379 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6380}
6381
6382static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6383{
6384 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6385}
6386
Avi Kivitydd856ef2012-08-27 23:46:17 +03006387static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6388{
6389 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6390}
6391
6392static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6393{
6394 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6395}
6396
Nadav Amit801806d2015-01-26 09:32:23 +02006397static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6398{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006399 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006400}
6401
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006402static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6403{
6404 return emul_to_vcpu(ctxt)->arch.hflags;
6405}
6406
6407static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6408{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006409 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006410}
6411
Sean Christophersoned193212019-04-02 08:03:09 -07006412static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6413 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006414{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006415 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006416}
6417
Sean Christophersonc5833c72019-04-02 08:03:10 -07006418static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6419{
6420 kvm_smm_changed(emul_to_vcpu(ctxt));
6421}
6422
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006423static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6424{
6425 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6426}
6427
Mathias Krause0225fb52012-08-30 01:30:16 +02006428static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006429 .read_gpr = emulator_read_gpr,
6430 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006431 .read_std = emulator_read_std,
6432 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006433 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006434 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006435 .read_emulated = emulator_read_emulated,
6436 .write_emulated = emulator_write_emulated,
6437 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006438 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006439 .pio_in_emulated = emulator_pio_in_emulated,
6440 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006441 .get_segment = emulator_get_segment,
6442 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006443 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006444 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006445 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006446 .set_gdt = emulator_set_gdt,
6447 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006448 .get_cr = emulator_get_cr,
6449 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006450 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006451 .get_dr = emulator_get_dr,
6452 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006453 .get_smbase = emulator_get_smbase,
6454 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006455 .set_msr = emulator_set_msr,
6456 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006457 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006458 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006459 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006460 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006461 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006462 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006463 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006464 .guest_has_long_mode = emulator_guest_has_long_mode,
6465 .guest_has_movbe = emulator_guest_has_movbe,
6466 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006467 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006468 .get_hflags = emulator_get_hflags,
6469 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006470 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006471 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006472 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006473};
6474
Gleb Natapov95cb2292010-04-28 19:15:43 +03006475static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6476{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006477 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006478 /*
6479 * an sti; sti; sequence only disable interrupts for the first
6480 * instruction. So, if the last instruction, be it emulated or
6481 * not, left the system with the INT_STI flag enabled, it
6482 * means that the last instruction is an sti. We should not
6483 * leave the flag on in this case. The same goes for mov ss
6484 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006485 if (int_shadow & mask)
6486 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006487 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006488 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006489 if (!mask)
6490 kvm_make_request(KVM_REQ_EVENT, vcpu);
6491 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006492}
6493
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006494static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006495{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006496 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006497 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07006498 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006499
6500 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006501 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6502 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006503 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006504 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006505 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006506}
6507
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006508static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6509{
6510 struct x86_emulate_ctxt *ctxt;
6511
6512 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6513 if (!ctxt) {
6514 pr_err("kvm: failed to allocate vcpu's emulator\n");
6515 return NULL;
6516 }
6517
6518 ctxt->vcpu = vcpu;
6519 ctxt->ops = &emulate_ops;
6520 vcpu->arch.emulate_ctxt = ctxt;
6521
6522 return ctxt;
6523}
6524
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006525static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6526{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006527 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006528 int cs_db, cs_l;
6529
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006530 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006531
Sean Christopherson744e6992020-02-18 15:03:09 -08006532 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006533 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006534 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6535
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006536 ctxt->eip = kvm_rip_read(vcpu);
6537 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6538 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006539 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006540 cs_db ? X86EMUL_MODE_PROT32 :
6541 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006542 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006543 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6544 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006545
Avi Kivitydd856ef2012-08-27 23:46:17 +03006546 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006547 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006548}
6549
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006550void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006551{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006552 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006553 int ret;
6554
6555 init_emulate_ctxt(vcpu);
6556
Avi Kivity9dac77f2011-06-01 15:34:25 +03006557 ctxt->op_bytes = 2;
6558 ctxt->ad_bytes = 2;
6559 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006560 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006561
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006562 if (ret != X86EMUL_CONTINUE) {
6563 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6564 } else {
6565 ctxt->eip = ctxt->_eip;
6566 kvm_rip_write(vcpu, ctxt->eip);
6567 kvm_set_rflags(vcpu, ctxt->eflags);
6568 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006569}
6570EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6571
Liran Alone2366172018-03-12 13:12:49 +02006572static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006573{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006574 ++vcpu->stat.insn_emulation_fail;
6575 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006576
Sean Christopherson42cbf062019-08-27 14:40:31 -07006577 if (emulation_type & EMULTYPE_VMWARE_GP) {
6578 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006579 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006580 }
Liran Alone2366172018-03-12 13:12:49 +02006581
Sean Christopherson738fece2019-08-27 14:40:34 -07006582 if (emulation_type & EMULTYPE_SKIP) {
6583 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6584 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6585 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006586 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006587 }
6588
Sean Christopherson22da61c2019-08-27 14:40:28 -07006589 kvm_queue_exception(vcpu, UD_VECTOR);
6590
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006591 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006592 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6593 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6594 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006595 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006596 }
Liran Alone2366172018-03-12 13:12:49 +02006597
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006598 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006599}
6600
Sean Christopherson736c2912019-12-06 15:57:14 -08006601static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006602 bool write_fault_to_shadow_pgtable,
6603 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006604{
Sean Christopherson736c2912019-12-06 15:57:14 -08006605 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006606 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006607
Sean Christopherson92daa482020-02-18 15:03:08 -08006608 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006609 return false;
6610
Sean Christopherson92daa482020-02-18 15:03:08 -08006611 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6612 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006613 return false;
6614
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006615 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006616 /*
6617 * Write permission should be allowed since only
6618 * write access need to be emulated.
6619 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006620 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006621
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006622 /*
6623 * If the mapping is invalid in guest, let cpu retry
6624 * it to generate fault.
6625 */
6626 if (gpa == UNMAPPED_GVA)
6627 return true;
6628 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006629
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006630 /*
6631 * Do not retry the unhandleable instruction if it faults on the
6632 * readonly host memory, otherwise it will goto a infinite loop:
6633 * retry instruction -> write #PF -> emulation fail -> retry
6634 * instruction -> ...
6635 */
6636 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006637
6638 /*
6639 * If the instruction failed on the error pfn, it can not be fixed,
6640 * report the error to userspace.
6641 */
6642 if (is_error_noslot_pfn(pfn))
6643 return false;
6644
6645 kvm_release_pfn_clean(pfn);
6646
6647 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006648 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006649 unsigned int indirect_shadow_pages;
6650
6651 spin_lock(&vcpu->kvm->mmu_lock);
6652 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6653 spin_unlock(&vcpu->kvm->mmu_lock);
6654
6655 if (indirect_shadow_pages)
6656 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6657
Gleb Natapova6f177e2010-07-08 12:41:12 +03006658 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006659 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006660
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006661 /*
6662 * if emulation was due to access to shadowed page table
6663 * and it failed try to unshadow page and re-enter the
6664 * guest to let CPU execute the instruction.
6665 */
6666 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006667
6668 /*
6669 * If the access faults on its page table, it can not
6670 * be fixed by unprotecting shadow page and it should
6671 * be reported to userspace.
6672 */
6673 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006674}
6675
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006676static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006677 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006678{
6679 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006680 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006681
6682 last_retry_eip = vcpu->arch.last_retry_eip;
6683 last_retry_addr = vcpu->arch.last_retry_addr;
6684
6685 /*
6686 * If the emulation is caused by #PF and it is non-page_table
6687 * writing instruction, it means the VM-EXIT is caused by shadow
6688 * page protected, we can zap the shadow page and retry this
6689 * instruction directly.
6690 *
6691 * Note: if the guest uses a non-page-table modifying instruction
6692 * on the PDE that points to the instruction, then we will unmap
6693 * the instruction and go to an infinite loop. So, we cache the
6694 * last retried eip and the last fault address, if we meet the eip
6695 * and the address again, we can break out of the potential infinite
6696 * loop.
6697 */
6698 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6699
Sean Christopherson92daa482020-02-18 15:03:08 -08006700 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006701 return false;
6702
Sean Christopherson92daa482020-02-18 15:03:08 -08006703 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6704 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006705 return false;
6706
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006707 if (x86_page_table_writing_insn(ctxt))
6708 return false;
6709
Sean Christopherson736c2912019-12-06 15:57:14 -08006710 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006711 return false;
6712
6713 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006714 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006715
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006716 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006717 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006718
Xiao Guangrong22368022013-01-13 23:44:12 +08006719 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006720
6721 return true;
6722}
6723
Gleb Natapov716d51a2012-09-03 15:24:26 +03006724static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6725static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6726
Paolo Bonzini64d60672015-05-07 11:36:11 +02006727static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006728{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006729 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006730 /* This is a good place to trace that we are exiting SMM. */
6731 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6732
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006733 /* Process a latched INIT or SMI, if any. */
6734 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006735 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006736
6737 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006738}
6739
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006740static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6741 unsigned long *db)
6742{
6743 u32 dr6 = 0;
6744 int i;
6745 u32 enable, rwlen;
6746
6747 enable = dr7;
6748 rwlen = dr7 >> 16;
6749 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6750 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6751 dr6 |= (1 << i);
6752 return dr6;
6753}
6754
Sean Christopherson120c2c42019-08-27 14:40:29 -07006755static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006756{
6757 struct kvm_run *kvm_run = vcpu->run;
6758
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006759 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6760 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
Peter Xud5d260c2020-05-05 16:49:59 -04006761 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006762 kvm_run->debug.arch.exception = DB_VECTOR;
6763 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006764 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006765 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006766 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006767 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006768}
6769
Kyle Huey6affcbe2016-11-29 12:40:40 -08006770int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6771{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006772 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006773 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006774
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006775 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006776 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006777 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006778
6779 /*
6780 * rflags is the old, "raw" value of the flags. The new value has
6781 * not been saved yet.
6782 *
6783 * This is correct even for TF set by the guest, because "the
6784 * processor will not generate this exception after the instruction
6785 * that sets the TF flag".
6786 */
6787 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006788 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006789 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006790}
6791EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6792
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006793static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6794{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006795 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6796 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006797 struct kvm_run *kvm_run = vcpu->run;
6798 unsigned long eip = kvm_get_linear_rip(vcpu);
6799 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006800 vcpu->arch.guest_debug_dr7,
6801 vcpu->arch.eff_db);
6802
6803 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006804 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006805 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006806 kvm_run->debug.arch.exception = DB_VECTOR;
6807 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006808 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006809 return true;
6810 }
6811 }
6812
Nadav Amit4161a562014-07-17 01:19:31 +03006813 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6814 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006815 unsigned long eip = kvm_get_linear_rip(vcpu);
6816 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006817 vcpu->arch.dr7,
6818 vcpu->arch.db);
6819
6820 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04006821 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006822 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006823 return true;
6824 }
6825 }
6826
6827 return false;
6828}
6829
Liran Alon04789b62018-03-12 13:12:50 +02006830static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6831{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006832 switch (ctxt->opcode_len) {
6833 case 1:
6834 switch (ctxt->b) {
6835 case 0xe4: /* IN */
6836 case 0xe5:
6837 case 0xec:
6838 case 0xed:
6839 case 0xe6: /* OUT */
6840 case 0xe7:
6841 case 0xee:
6842 case 0xef:
6843 case 0x6c: /* INS */
6844 case 0x6d:
6845 case 0x6e: /* OUTS */
6846 case 0x6f:
6847 return true;
6848 }
6849 break;
6850 case 2:
6851 switch (ctxt->b) {
6852 case 0x33: /* RDPMC */
6853 return true;
6854 }
6855 break;
Liran Alon04789b62018-03-12 13:12:50 +02006856 }
6857
6858 return false;
6859}
6860
Sean Christopherson736c2912019-12-06 15:57:14 -08006861int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6862 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006863{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006864 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006865 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006866 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006867 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006868
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006869 vcpu->arch.l1tf_flush_l1d = true;
6870
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006871 /*
6872 * Clear write_fault_to_shadow_pgtable here to ensure it is
6873 * never reused.
6874 */
6875 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006876 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006877
Sheng Yang571008d2008-01-02 14:49:22 +08006878 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006879 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006880
6881 /*
6882 * We will reenter on the same instruction since
6883 * we do not set complete_userspace_io. This does not
6884 * handle watchpoints yet, those would be handled in
6885 * the emulate_ops.
6886 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006887 if (!(emulation_type & EMULTYPE_SKIP) &&
6888 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006889 return r;
6890
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006891 ctxt->interruptibility = 0;
6892 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006893 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006894 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006895
Borislav Petkovb51e9742013-09-22 16:44:52 +02006896 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006897
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006898 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006899
Avi Kivitye46479f2010-04-11 13:05:16 +03006900 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006901 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006902 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006903 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006904 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6905 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006906 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006907 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006908 if (reexecute_instruction(vcpu, cr2_or_gpa,
6909 write_fault_to_spt,
6910 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006911 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006912 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006913 /*
6914 * #UD should result in just EMULATION_FAILED, and trap-like
6915 * exception should not be encountered during decode.
6916 */
6917 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6918 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006919 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006920 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006921 }
Liran Alone2366172018-03-12 13:12:49 +02006922 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006923 }
6924 }
6925
Sean Christopherson42cbf062019-08-27 14:40:31 -07006926 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6927 !is_vmware_backdoor_opcode(ctxt)) {
6928 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006929 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006930 }
Liran Alon04789b62018-03-12 13:12:50 +02006931
Sean Christopherson1957aa62019-08-27 14:40:39 -07006932 /*
6933 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6934 * for kvm_skip_emulated_instruction(). The caller is responsible for
6935 * updating interruptibility state and injecting single-step #DBs.
6936 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006937 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006938 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006939 if (ctxt->eflags & X86_EFLAGS_RF)
6940 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006941 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006942 }
6943
Sean Christopherson736c2912019-12-06 15:57:14 -08006944 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006945 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006946
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006947 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006948 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006949 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6950 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006951 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006952 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006953
Gleb Natapov5cd21912010-03-18 15:20:26 +02006954restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006955 if (emulation_type & EMULTYPE_PF) {
6956 /* Save the faulting GPA (cr2) in the address field */
6957 ctxt->exception.address = cr2_or_gpa;
6958
6959 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6960 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006961 ctxt->gpa_available = true;
6962 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006963 }
6964 } else {
6965 /* Sanitize the address out of an abundance of paranoia. */
6966 ctxt->exception.address = 0;
6967 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006968
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006969 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006970
Joerg Roedel775fde82011-04-04 12:39:24 +02006971 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006972 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006973
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006974 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006975 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006976 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006977 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006978
Liran Alone2366172018-03-12 13:12:49 +02006979 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006980 }
6981
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006982 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006983 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006984 if (inject_emulated_exception(vcpu))
6985 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006986 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006987 if (!vcpu->arch.pio.in) {
6988 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006989 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006990 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006991 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006992 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6993 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006994 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006995 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006996 ++vcpu->stat.mmio_exits;
6997
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006998 if (!vcpu->mmio_is_write)
6999 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007000 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007001 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007002 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03007003 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007004 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007005 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03007006
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007007 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007008 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007009 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007010 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02007011 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007012 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
7013 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007014 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07007015 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007016 if (kvm_x86_ops.update_emulated_instruction)
7017 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02007018 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007019 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007020
7021 /*
7022 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
7023 * do nothing, and it will be requested again as soon as
7024 * the shadow expires. But we still need to check here,
7025 * because POPF has no interrupt shadow.
7026 */
7027 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
7028 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007029 } else
7030 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03007031
Gleb Natapove85d28f2010-07-29 15:11:52 +03007032 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007033}
Sean Christophersonc60658d2018-08-23 13:56:53 -07007034
7035int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
7036{
7037 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
7038}
7039EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
7040
7041int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
7042 void *insn, int insn_len)
7043{
7044 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
7045}
7046EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007047
Sean Christopherson8764ed52019-04-29 07:04:15 -07007048static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7049{
7050 vcpu->arch.pio.count = 0;
7051 return 1;
7052}
7053
Sean Christopherson45def772019-03-11 20:01:05 -07007054static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7055{
7056 vcpu->arch.pio.count = 0;
7057
7058 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7059 return 1;
7060
7061 return kvm_skip_emulated_instruction(vcpu);
7062}
7063
Sean Christophersondca7f122018-03-08 08:57:27 -08007064static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7065 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007066{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007067 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007068 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7069
Sean Christopherson8764ed52019-04-29 07:04:15 -07007070 if (ret)
7071 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007072
Sean Christopherson8764ed52019-04-29 07:04:15 -07007073 /*
7074 * Workaround userspace that relies on old KVM behavior of %rip being
7075 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7076 */
7077 if (port == 0x7e &&
7078 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7079 vcpu->arch.complete_userspace_io =
7080 complete_fast_pio_out_port_0x7e;
7081 kvm_skip_emulated_instruction(vcpu);
7082 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007083 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7084 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7085 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007086 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007087}
Carsten Ottede7d7892007-10-30 18:44:25 +01007088
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007089static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7090{
7091 unsigned long val;
7092
7093 /* We should only ever be called with arch.pio.count equal to 1 */
7094 BUG_ON(vcpu->arch.pio.count != 1);
7095
Sean Christopherson45def772019-03-11 20:01:05 -07007096 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7097 vcpu->arch.pio.count = 0;
7098 return 1;
7099 }
7100
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007101 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007102 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007103
7104 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007105 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007106 * the copy and tracing
7107 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007108 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007109 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007110
Sean Christopherson45def772019-03-11 20:01:05 -07007111 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007112}
7113
Sean Christophersondca7f122018-03-08 08:57:27 -08007114static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7115 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007116{
7117 unsigned long val;
7118 int ret;
7119
7120 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007121 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007122
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007123 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007124 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007125 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007126 return ret;
7127 }
7128
Sean Christopherson45def772019-03-11 20:01:05 -07007129 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007130 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7131
7132 return 0;
7133}
Sean Christophersondca7f122018-03-08 08:57:27 -08007134
7135int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7136{
Sean Christopherson45def772019-03-11 20:01:05 -07007137 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007138
Sean Christophersondca7f122018-03-08 08:57:27 -08007139 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007140 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007141 else
Sean Christopherson45def772019-03-11 20:01:05 -07007142 ret = kvm_fast_pio_out(vcpu, size, port);
7143 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007144}
7145EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007146
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007147static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007148{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007149 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007150 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007151}
7152
7153static void tsc_khz_changed(void *data)
7154{
7155 struct cpufreq_freqs *freq = data;
7156 unsigned long khz = 0;
7157
7158 if (data)
7159 khz = freq->new;
7160 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7161 khz = cpufreq_quick_get(raw_smp_processor_id());
7162 if (!khz)
7163 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007164 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007165}
7166
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007167#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007168static void kvm_hyperv_tsc_notifier(void)
7169{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007170 struct kvm *kvm;
7171 struct kvm_vcpu *vcpu;
7172 int cpu;
7173
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007174 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007175 list_for_each_entry(kvm, &vm_list, vm_list)
7176 kvm_make_mclock_inprogress_request(kvm);
7177
7178 hyperv_stop_tsc_emulation();
7179
7180 /* TSC frequency always matches when on Hyper-V */
7181 for_each_present_cpu(cpu)
7182 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7183 kvm_max_guest_tsc_khz = tsc_khz;
7184
7185 list_for_each_entry(kvm, &vm_list, vm_list) {
7186 struct kvm_arch *ka = &kvm->arch;
7187
7188 spin_lock(&ka->pvclock_gtod_sync_lock);
7189
7190 pvclock_update_vm_gtod_copy(kvm);
7191
7192 kvm_for_each_vcpu(cpu, vcpu, kvm)
7193 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7194
7195 kvm_for_each_vcpu(cpu, vcpu, kvm)
7196 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7197
7198 spin_unlock(&ka->pvclock_gtod_sync_lock);
7199 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007200 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007201}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007202#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007203
Viresh Kumardf240142019-04-29 15:03:58 +05307204static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007205{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007206 struct kvm *kvm;
7207 struct kvm_vcpu *vcpu;
7208 int i, send_ipi = 0;
7209
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007210 /*
7211 * We allow guests to temporarily run on slowing clocks,
7212 * provided we notify them after, or to run on accelerating
7213 * clocks, provided we notify them before. Thus time never
7214 * goes backwards.
7215 *
7216 * However, we have a problem. We can't atomically update
7217 * the frequency of a given CPU from this function; it is
7218 * merely a notifier, which can be called from any CPU.
7219 * Changing the TSC frequency at arbitrary points in time
7220 * requires a recomputation of local variables related to
7221 * the TSC for each VCPU. We must flag these local variables
7222 * to be updated and be sure the update takes place with the
7223 * new frequency before any guests proceed.
7224 *
7225 * Unfortunately, the combination of hotplug CPU and frequency
7226 * change creates an intractable locking scenario; the order
7227 * of when these callouts happen is undefined with respect to
7228 * CPU hotplug, and they can race with each other. As such,
7229 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7230 * undefined; you can actually have a CPU frequency change take
7231 * place in between the computation of X and the setting of the
7232 * variable. To protect against this problem, all updates of
7233 * the per_cpu tsc_khz variable are done in an interrupt
7234 * protected IPI, and all callers wishing to update the value
7235 * must wait for a synchronous IPI to complete (which is trivial
7236 * if the caller is on the CPU already). This establishes the
7237 * necessary total order on variable updates.
7238 *
7239 * Note that because a guest time update may take place
7240 * anytime after the setting of the VCPU's request bit, the
7241 * correct TSC value must be set before the request. However,
7242 * to ensure the update actually makes it to any guest which
7243 * starts running in hardware virtualization between the set
7244 * and the acquisition of the spinlock, we must also ping the
7245 * CPU after setting the request bit.
7246 *
7247 */
7248
Viresh Kumardf240142019-04-29 15:03:58 +05307249 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007250
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007251 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007252 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007253 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307254 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007255 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007256 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007257 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007258 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007259 }
7260 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007261 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007262
7263 if (freq->old < freq->new && send_ipi) {
7264 /*
7265 * We upscale the frequency. Must make the guest
7266 * doesn't see old kvmclock values while running with
7267 * the new frequency, otherwise we risk the guest sees
7268 * time go backwards.
7269 *
7270 * In case we update the frequency for another cpu
7271 * (which might be in guest context) send an interrupt
7272 * to kick the cpu out of guest context. Next time
7273 * guest context is entered kvmclock will be updated,
7274 * so the guest will not see stale values.
7275 */
Viresh Kumardf240142019-04-29 15:03:58 +05307276 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007277 }
Viresh Kumardf240142019-04-29 15:03:58 +05307278}
7279
7280static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7281 void *data)
7282{
7283 struct cpufreq_freqs *freq = data;
7284 int cpu;
7285
7286 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7287 return 0;
7288 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7289 return 0;
7290
7291 for_each_cpu(cpu, freq->policy->cpus)
7292 __kvmclock_cpufreq_notifier(freq, cpu);
7293
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007294 return 0;
7295}
7296
7297static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007298 .notifier_call = kvmclock_cpufreq_notifier
7299};
7300
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007301static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007302{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007303 tsc_khz_changed(NULL);
7304 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007305}
7306
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007307static void kvm_timer_init(void)
7308{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007309 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307310
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007311 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007312#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007313 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007314 int cpu;
7315
Avi Kivity3e26f232010-12-16 12:16:34 +02007316 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007317 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007318 if (policy) {
7319 if (policy->cpuinfo.max_freq)
7320 max_tsc_khz = policy->cpuinfo.max_freq;
7321 cpufreq_cpu_put(policy);
7322 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007323 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007324#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007325 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7326 CPUFREQ_TRANSITION_NOTIFIER);
7327 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307328
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007329 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007330 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007331}
7332
Andi Kleendd60d212017-07-25 17:20:32 -07007333DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7334EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007335
Gleb Natapovf5132b02011-11-10 14:57:22 +02007336int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007337{
Alex,Shi086c9852011-10-20 15:34:01 +08007338 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007339}
7340
7341static int kvm_is_user_mode(void)
7342{
7343 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007344
Alex,Shi086c9852011-10-20 15:34:01 +08007345 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007346 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007347
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007348 return user_mode != 0;
7349}
7350
7351static unsigned long kvm_get_guest_ip(void)
7352{
7353 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007354
Alex,Shi086c9852011-10-20 15:34:01 +08007355 if (__this_cpu_read(current_vcpu))
7356 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007357
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007358 return ip;
7359}
7360
Luwei Kang8479e042019-02-18 19:26:07 -05007361static void kvm_handle_intel_pt_intr(void)
7362{
7363 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7364
7365 kvm_make_request(KVM_REQ_PMI, vcpu);
7366 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7367 (unsigned long *)&vcpu->arch.pmu.global_status);
7368}
7369
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007370static struct perf_guest_info_callbacks kvm_guest_cbs = {
7371 .is_in_guest = kvm_is_in_guest,
7372 .is_user_mode = kvm_is_user_mode,
7373 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007374 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007375};
7376
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007377#ifdef CONFIG_X86_64
7378static void pvclock_gtod_update_fn(struct work_struct *work)
7379{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007380 struct kvm *kvm;
7381
7382 struct kvm_vcpu *vcpu;
7383 int i;
7384
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007385 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007386 list_for_each_entry(kvm, &vm_list, vm_list)
7387 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007388 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007389 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007390 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007391}
7392
7393static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7394
7395/*
7396 * Notification about pvclock gtod data update.
7397 */
7398static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7399 void *priv)
7400{
7401 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7402 struct timekeeper *tk = priv;
7403
7404 update_pvclock_gtod(tk);
7405
7406 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007407 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007408 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007409 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007410 atomic_read(&kvm_guest_has_master_clock) != 0)
7411 queue_work(system_long_wq, &pvclock_gtod_work);
7412
7413 return 0;
7414}
7415
7416static struct notifier_block pvclock_gtod_notifier = {
7417 .notifier_call = pvclock_gtod_notify,
7418};
7419#endif
7420
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007421int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007422{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007423 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007424 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007425
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007426 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007427 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007428 r = -EEXIST;
7429 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007430 }
7431
7432 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007433 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007434 r = -EOPNOTSUPP;
7435 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007436 }
7437 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007438 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007439 r = -EOPNOTSUPP;
7440 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007441 }
7442
Marc Orrb666a4b2018-11-06 14:53:56 -08007443 /*
7444 * KVM explicitly assumes that the guest has an FPU and
7445 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7446 * vCPU's FPU state as a fxregs_state struct.
7447 */
7448 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7449 printk(KERN_ERR "kvm: inadequate fpu\n");
7450 r = -EOPNOTSUPP;
7451 goto out;
7452 }
7453
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007454 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007455 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007456 __alignof__(struct fpu), SLAB_ACCOUNT,
7457 NULL);
7458 if (!x86_fpu_cache) {
7459 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7460 goto out;
7461 }
7462
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007463 x86_emulator_cache = kvm_alloc_emulator_cache();
7464 if (!x86_emulator_cache) {
7465 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7466 goto out_free_x86_fpu_cache;
7467 }
7468
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007469 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7470 if (!shared_msrs) {
7471 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007472 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007473 }
7474
Avi Kivity97db56c2008-01-13 13:23:56 +02007475 r = kvm_mmu_module_init();
7476 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007477 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007478
Sheng Yang7b523452008-04-25 21:13:50 +08007479 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007480 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007481 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007482 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007483
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007484 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7485
Sean Christophersoncfc48182020-03-02 15:56:23 -08007486 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007487 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007488 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7489 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007490
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007491 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007492 if (pi_inject_timer == -1)
7493 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007494#ifdef CONFIG_X86_64
7495 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007496
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007497 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007498 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007499#endif
7500
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007501 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007502
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007503out_free_percpu:
7504 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007505out_free_x86_emulator_cache:
7506 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007507out_free_x86_fpu_cache:
7508 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007509out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007510 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007511}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007512
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007513void kvm_arch_exit(void)
7514{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007515#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007516 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007517 clear_hv_tscchange_cb();
7518#endif
David Matlackcef84c32016-12-16 14:30:36 -08007519 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007520 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7521
Jan Kiszka888d2562009-04-17 19:24:58 +02007522 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7523 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7524 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007525 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007526#ifdef CONFIG_X86_64
7527 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7528#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007529 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007530 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007531 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007532 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007533}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007534
Joel Schopp5cb56052015-03-02 13:43:31 -06007535int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007536{
7537 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007538 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007539 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007540 return 1;
7541 } else {
7542 vcpu->run->exit_reason = KVM_EXIT_HLT;
7543 return 0;
7544 }
7545}
Joel Schopp5cb56052015-03-02 13:43:31 -06007546EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7547
7548int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7549{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007550 int ret = kvm_skip_emulated_instruction(vcpu);
7551 /*
7552 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7553 * KVM_EXIT_DEBUG here.
7554 */
7555 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007556}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007557EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7558
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007559#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007560static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7561 unsigned long clock_type)
7562{
7563 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007564 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007565 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007566 int ret;
7567
7568 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7569 return -KVM_EOPNOTSUPP;
7570
7571 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7572 return -KVM_EOPNOTSUPP;
7573
7574 clock_pairing.sec = ts.tv_sec;
7575 clock_pairing.nsec = ts.tv_nsec;
7576 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7577 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007578 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007579
7580 ret = 0;
7581 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7582 sizeof(struct kvm_clock_pairing)))
7583 ret = -KVM_EFAULT;
7584
7585 return ret;
7586}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007587#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007588
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307589/*
7590 * kvm_pv_kick_cpu_op: Kick a vcpu.
7591 *
7592 * @apicid - apicid of vcpu to be kicked.
7593 */
7594static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7595{
Raghavendra K T24d21662013-08-26 14:18:35 +05307596 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307597
Peter Xu150a84f2019-12-04 20:07:21 +01007598 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007599 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007600 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307601 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007602 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307603
Raghavendra K T24d21662013-08-26 14:18:35 +05307604 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007605 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307606}
7607
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007608bool kvm_apicv_activated(struct kvm *kvm)
7609{
7610 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7611}
7612EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7613
7614void kvm_apicv_init(struct kvm *kvm, bool enable)
7615{
7616 if (enable)
7617 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7618 &kvm->arch.apicv_inhibit_reasons);
7619 else
7620 set_bit(APICV_INHIBIT_REASON_DISABLE,
7621 &kvm->arch.apicv_inhibit_reasons);
7622}
7623EXPORT_SYMBOL_GPL(kvm_apicv_init);
7624
Wanpeng Li71506292019-06-11 20:23:49 +08007625static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7626{
7627 struct kvm_vcpu *target = NULL;
7628 struct kvm_apic_map *map;
7629
7630 rcu_read_lock();
7631 map = rcu_dereference(kvm->arch.apic_map);
7632
7633 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7634 target = map->phys_map[dest_id]->vcpu;
7635
7636 rcu_read_unlock();
7637
Wanpeng Li266e85a2019-07-24 17:43:13 +08007638 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007639 kvm_vcpu_yield_to(target);
7640}
7641
Hollis Blanchard8776e512007-10-31 17:24:24 -05007642int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7643{
7644 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007645 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007646
Radim Krčmář696ca772018-05-24 17:50:56 +02007647 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7648 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007649
Sean Christophersonde3cd112019-04-30 10:36:17 -07007650 nr = kvm_rax_read(vcpu);
7651 a0 = kvm_rbx_read(vcpu);
7652 a1 = kvm_rcx_read(vcpu);
7653 a2 = kvm_rdx_read(vcpu);
7654 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007655
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007656 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007657
Nadav Amita449c7a2014-06-18 17:19:24 +03007658 op_64_bit = is_64_bit_mode(vcpu);
7659 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007660 nr &= 0xFFFFFFFF;
7661 a0 &= 0xFFFFFFFF;
7662 a1 &= 0xFFFFFFFF;
7663 a2 &= 0xFFFFFFFF;
7664 a3 &= 0xFFFFFFFF;
7665 }
7666
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007667 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02007668 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007669 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007670 }
7671
Hollis Blanchard8776e512007-10-31 17:24:24 -05007672 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007673 case KVM_HC_VAPIC_POLL_IRQ:
7674 ret = 0;
7675 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307676 case KVM_HC_KICK_CPU:
7677 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007678 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307679 ret = 0;
7680 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007681#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007682 case KVM_HC_CLOCK_PAIRING:
7683 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7684 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007685#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007686 case KVM_HC_SEND_IPI:
7687 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7688 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007689 case KVM_HC_SCHED_YIELD:
7690 kvm_sched_yield(vcpu->kvm, a0);
7691 ret = 0;
7692 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007693 default:
7694 ret = -KVM_ENOSYS;
7695 break;
7696 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007697out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007698 if (!op_64_bit)
7699 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007700 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007701
Amit Shahf11c3a82008-02-21 01:00:30 +05307702 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007703 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007704}
7705EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7706
Jan Kiszkab6785de2012-09-20 07:43:17 +02007707static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007708{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007709 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007710 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007711 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007712
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007713 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007714
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007715 return emulator_write_emulated(ctxt, rip, instruction, 3,
7716 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007717}
7718
Avi Kivity851ba692009-08-24 11:10:17 +03007719static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007720{
Matt Gingell782d4222015-11-16 15:26:00 -08007721 return vcpu->run->request_interrupt_window &&
7722 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007723}
7724
Avi Kivity851ba692009-08-24 11:10:17 +03007725static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007726{
Avi Kivity851ba692009-08-24 11:10:17 +03007727 struct kvm_run *kvm_run = vcpu->run;
7728
Jan Kiszka91586a32009-10-05 13:07:21 +02007729 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007730 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007731 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007732 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007733 kvm_run->ready_for_interrupt_injection =
7734 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007735 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007736}
7737
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007738static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7739{
7740 int max_irr, tpr;
7741
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007742 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007743 return;
7744
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007745 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007746 return;
7747
Andrey Smetanind62caab2015-11-10 15:36:33 +03007748 if (vcpu->arch.apicv_active)
7749 return;
7750
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007751 if (!vcpu->arch.apic->vapic_addr)
7752 max_irr = kvm_lapic_find_highest_irr(vcpu);
7753 else
7754 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007755
7756 if (max_irr != -1)
7757 max_irr >>= 4;
7758
7759 tpr = kvm_lapic_get_cr8(vcpu);
7760
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007761 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007762}
7763
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007764static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007765{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007766 int r;
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007767 bool can_inject = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007768
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007769 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007770
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007771 if (vcpu->arch.exception.injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007772 kvm_x86_ops.queue_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007773 can_inject = false;
7774 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07007775 /*
Liran Alona042c262018-03-23 03:01:32 +03007776 * Do not inject an NMI or interrupt if there is a pending
7777 * exception. Exceptions and interrupts are recognized at
7778 * instruction boundaries, i.e. the start of an instruction.
7779 * Trap-like exceptions, e.g. #DB, have higher priority than
7780 * NMIs and interrupts, i.e. traps are recognized before an
7781 * NMI/interrupt that's pending on the same instruction.
7782 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7783 * priority, but are only generated (pended) during instruction
7784 * execution, i.e. a pending fault-like exception means the
7785 * fault occurred on the *previous* instruction and must be
7786 * serviced prior to recognizing any new events in order to
7787 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007788 */
Liran Alon1a680e32018-03-23 03:01:33 +03007789 else if (!vcpu->arch.exception.pending) {
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007790 if (vcpu->arch.nmi_injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007791 kvm_x86_ops.set_nmi(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007792 can_inject = false;
7793 } else if (vcpu->arch.interrupt.injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007794 kvm_x86_ops.set_irq(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007795 can_inject = false;
7796 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07007797 }
7798
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07007799 WARN_ON_ONCE(vcpu->arch.exception.injected &&
7800 vcpu->arch.exception.pending);
7801
Liran Alon1a680e32018-03-23 03:01:33 +03007802 /*
7803 * Call check_nested_events() even if we reinjected a previous event
7804 * in order for caller to determine if it should require immediate-exit
7805 * from L2 to L1 due to pending L1 events which require exit
7806 * from L2 to L1.
7807 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04007808 if (is_guest_mode(vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04007809 r = kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007810 if (r < 0)
7811 goto busy;
Wanpeng Li664f8e22017-08-24 03:35:09 -07007812 }
7813
7814 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007815 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007816 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7817 vcpu->arch.exception.has_error_code,
7818 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007819
Wanpeng Li664f8e22017-08-24 03:35:09 -07007820 vcpu->arch.exception.pending = false;
7821 vcpu->arch.exception.injected = true;
7822
Nadav Amitd6e8c852014-07-24 14:51:24 +03007823 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7824 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7825 X86_EFLAGS_RF);
7826
Jim Mattsonf10c7292018-10-16 14:29:23 -07007827 if (vcpu->arch.exception.nr == DB_VECTOR) {
Jim Mattsonf10c7292018-10-16 14:29:23 -07007828 kvm_deliver_exception_payload(vcpu);
7829 if (vcpu->arch.dr7 & DR7_GD) {
7830 vcpu->arch.dr7 &= ~DR7_GD;
7831 kvm_update_dr7(vcpu);
7832 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007833 }
7834
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007835 kvm_x86_ops.queue_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04007836 can_inject = false;
Liran Alon1a680e32018-03-23 03:01:33 +03007837 }
7838
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007839 /*
7840 * Finally, inject interrupt events. If an event cannot be injected
7841 * due to architectural conditions (e.g. IF=0) a window-open exit
7842 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending
7843 * and can architecturally be injected, but we cannot do it right now:
7844 * an interrupt could have arrived just now and we have to inject it
7845 * as a vmexit, or there could already an event in the queue, which is
7846 * indicated by can_inject. In that case we request an immediate exit
7847 * in order to make progress and get back here for another iteration.
7848 * The kvm_x86_ops hooks communicate this by returning -EBUSY.
7849 */
7850 if (vcpu->arch.smi_pending) {
7851 r = can_inject ? kvm_x86_ops.smi_allowed(vcpu, true) : -EBUSY;
7852 if (r < 0)
7853 goto busy;
7854 if (r) {
7855 vcpu->arch.smi_pending = false;
7856 ++vcpu->arch.smi_count;
7857 enter_smm(vcpu);
7858 can_inject = false;
7859 } else
7860 kvm_x86_ops.enable_smi_window(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007861 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007862
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007863 if (vcpu->arch.nmi_pending) {
7864 r = can_inject ? kvm_x86_ops.nmi_allowed(vcpu, true) : -EBUSY;
7865 if (r < 0)
7866 goto busy;
7867 if (r) {
7868 --vcpu->arch.nmi_pending;
7869 vcpu->arch.nmi_injected = true;
7870 kvm_x86_ops.set_nmi(vcpu);
7871 can_inject = false;
7872 WARN_ON(kvm_x86_ops.nmi_allowed(vcpu, true) < 0);
7873 }
7874 if (vcpu->arch.nmi_pending)
7875 kvm_x86_ops.enable_nmi_window(vcpu);
7876 }
7877
7878 if (kvm_cpu_has_injectable_intr(vcpu)) {
7879 r = can_inject ? kvm_x86_ops.interrupt_allowed(vcpu, true) : -EBUSY;
7880 if (r < 0)
7881 goto busy;
7882 if (r) {
7883 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
7884 kvm_x86_ops.set_irq(vcpu);
7885 WARN_ON(kvm_x86_ops.interrupt_allowed(vcpu, true) < 0);
7886 }
7887 if (kvm_cpu_has_injectable_intr(vcpu))
7888 kvm_x86_ops.enable_irq_window(vcpu);
7889 }
7890
7891 if (is_guest_mode(vcpu) &&
7892 kvm_x86_ops.nested_ops->hv_timer_pending &&
7893 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
7894 *req_immediate_exit = true;
7895
7896 WARN_ON(vcpu->arch.exception.pending);
7897 return;
7898
7899busy:
7900 *req_immediate_exit = true;
7901 return;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007902}
7903
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007904static void process_nmi(struct kvm_vcpu *vcpu)
7905{
7906 unsigned limit = 2;
7907
7908 /*
7909 * x86 is limited to one NMI running, and one NMI pending after it.
7910 * If an NMI is already in progress, limit further NMIs to just one.
7911 * Otherwise, allow two (and we'll inject the first one immediately).
7912 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007913 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007914 limit = 1;
7915
7916 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7917 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7918 kvm_make_request(KVM_REQ_EVENT, vcpu);
7919}
7920
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007921static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007922{
7923 u32 flags = 0;
7924 flags |= seg->g << 23;
7925 flags |= seg->db << 22;
7926 flags |= seg->l << 21;
7927 flags |= seg->avl << 20;
7928 flags |= seg->present << 15;
7929 flags |= seg->dpl << 13;
7930 flags |= seg->s << 12;
7931 flags |= seg->type << 8;
7932 return flags;
7933}
7934
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007935static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007936{
7937 struct kvm_segment seg;
7938 int offset;
7939
7940 kvm_get_segment(vcpu, &seg, n);
7941 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7942
7943 if (n < 3)
7944 offset = 0x7f84 + n * 12;
7945 else
7946 offset = 0x7f2c + (n - 3) * 12;
7947
7948 put_smstate(u32, buf, offset + 8, seg.base);
7949 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007950 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007951}
7952
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007953#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007954static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007955{
7956 struct kvm_segment seg;
7957 int offset;
7958 u16 flags;
7959
7960 kvm_get_segment(vcpu, &seg, n);
7961 offset = 0x7e00 + n * 16;
7962
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007963 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007964 put_smstate(u16, buf, offset, seg.selector);
7965 put_smstate(u16, buf, offset + 2, flags);
7966 put_smstate(u32, buf, offset + 4, seg.limit);
7967 put_smstate(u64, buf, offset + 8, seg.base);
7968}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007969#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007970
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007971static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007972{
7973 struct desc_ptr dt;
7974 struct kvm_segment seg;
7975 unsigned long val;
7976 int i;
7977
7978 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7979 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7980 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7981 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7982
7983 for (i = 0; i < 8; i++)
7984 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7985
7986 kvm_get_dr(vcpu, 6, &val);
7987 put_smstate(u32, buf, 0x7fcc, (u32)val);
7988 kvm_get_dr(vcpu, 7, &val);
7989 put_smstate(u32, buf, 0x7fc8, (u32)val);
7990
7991 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7992 put_smstate(u32, buf, 0x7fc4, seg.selector);
7993 put_smstate(u32, buf, 0x7f64, seg.base);
7994 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007995 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007996
7997 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7998 put_smstate(u32, buf, 0x7fc0, seg.selector);
7999 put_smstate(u32, buf, 0x7f80, seg.base);
8000 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008001 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008002
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008003 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008004 put_smstate(u32, buf, 0x7f74, dt.address);
8005 put_smstate(u32, buf, 0x7f70, dt.size);
8006
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008007 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008008 put_smstate(u32, buf, 0x7f58, dt.address);
8009 put_smstate(u32, buf, 0x7f54, dt.size);
8010
8011 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008012 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008013
8014 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
8015
8016 /* revision id */
8017 put_smstate(u32, buf, 0x7efc, 0x00020000);
8018 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
8019}
8020
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008021#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008022static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008023{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008024 struct desc_ptr dt;
8025 struct kvm_segment seg;
8026 unsigned long val;
8027 int i;
8028
8029 for (i = 0; i < 16; i++)
8030 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
8031
8032 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
8033 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
8034
8035 kvm_get_dr(vcpu, 6, &val);
8036 put_smstate(u64, buf, 0x7f68, val);
8037 kvm_get_dr(vcpu, 7, &val);
8038 put_smstate(u64, buf, 0x7f60, val);
8039
8040 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
8041 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
8042 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
8043
8044 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
8045
8046 /* revision id */
8047 put_smstate(u32, buf, 0x7efc, 0x00020064);
8048
8049 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
8050
8051 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8052 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008053 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008054 put_smstate(u32, buf, 0x7e94, seg.limit);
8055 put_smstate(u64, buf, 0x7e98, seg.base);
8056
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008057 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008058 put_smstate(u32, buf, 0x7e84, dt.size);
8059 put_smstate(u64, buf, 0x7e88, dt.address);
8060
8061 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8062 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008063 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008064 put_smstate(u32, buf, 0x7e74, seg.limit);
8065 put_smstate(u64, buf, 0x7e78, seg.base);
8066
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008067 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008068 put_smstate(u32, buf, 0x7e64, dt.size);
8069 put_smstate(u64, buf, 0x7e68, dt.address);
8070
8071 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008072 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008073}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008074#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008075
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008076static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02008077{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008078 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02008079 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008080 char buf[512];
8081 u32 cr0;
8082
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008083 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008084 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008085#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008086 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008087 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008088 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008089#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008090 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008091
Ladi Prosek0234bf82017-10-11 16:54:40 +02008092 /*
8093 * Give pre_enter_smm() a chance to make ISA-specific changes to the
8094 * vCPU state (e.g. leave guest mode) after we've saved the state into
8095 * the SMM state-save area.
8096 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008097 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02008098
8099 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008100 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008101
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008102 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008103 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
8104 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008105 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008106
8107 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
8108 kvm_rip_write(vcpu, 0x8000);
8109
8110 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008111 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008112 vcpu->arch.cr0 = cr0;
8113
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008114 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008115
Paolo Bonzini18c36262015-08-07 12:27:54 +02008116 /* Undocumented: IDT limit is set to zero on entry to SMM. */
8117 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008118 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02008119
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008120 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
8121
8122 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8123 cs.base = vcpu->arch.smbase;
8124
8125 ds.selector = 0;
8126 ds.base = 0;
8127
8128 cs.limit = ds.limit = 0xffffffff;
8129 cs.type = ds.type = 0x3;
8130 cs.dpl = ds.dpl = 0;
8131 cs.db = ds.db = 0;
8132 cs.s = ds.s = 1;
8133 cs.l = ds.l = 0;
8134 cs.g = ds.g = 1;
8135 cs.avl = ds.avl = 0;
8136 cs.present = ds.present = 1;
8137 cs.unusable = ds.unusable = 0;
8138 cs.padding = ds.padding = 0;
8139
8140 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8141 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8142 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8143 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8144 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8145 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8146
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008147#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008148 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008149 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008150#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008151
8152 kvm_update_cpuid(vcpu);
8153 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008154}
8155
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008156static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008157{
8158 vcpu->arch.smi_pending = true;
8159 kvm_make_request(KVM_REQ_EVENT, vcpu);
8160}
8161
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008162void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8163 unsigned long *vcpu_bitmap)
8164{
8165 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008166
8167 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8168
Mao Wenandb5a95e2019-11-19 11:06:40 +08008169 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008170 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008171
8172 free_cpumask_var(cpus);
8173}
8174
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008175void kvm_make_scan_ioapic_request(struct kvm *kvm)
8176{
8177 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8178}
8179
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008180void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8181{
8182 if (!lapic_in_kernel(vcpu))
8183 return;
8184
8185 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8186 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008187 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008188}
8189EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8190
8191/*
8192 * NOTE: Do not hold any lock prior to calling this.
8193 *
8194 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8195 * locked, because it calls __x86_set_memory_region() which does
8196 * synchronize_srcu(&kvm->srcu).
8197 */
8198void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8199{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008200 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008201 unsigned long old, new, expected;
8202
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008203 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8204 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008205 return;
8206
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008207 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8208 do {
8209 expected = new = old;
8210 if (activate)
8211 __clear_bit(bit, &new);
8212 else
8213 __set_bit(bit, &new);
8214 if (new == old)
8215 break;
8216 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8217 } while (old != expected);
8218
8219 if (!!old == !!new)
8220 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008221
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008222 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008223 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8224 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008225
8226 /*
8227 * Sending request to update APICV for all other vcpus,
8228 * while update the calling vcpu immediately instead of
8229 * waiting for another #VMEXIT to handle the request.
8230 */
8231 except = kvm_get_running_vcpu();
8232 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
8233 except);
8234 if (except)
8235 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008236}
8237EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8238
Yang Zhang3d81bc72013-04-11 19:25:13 +08008239static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008240{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008241 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008242 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008243
Andrey Smetanin63086302015-11-10 15:36:32 +03008244 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008245
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008246 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008247 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008248 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008249 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008250 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008251 if (ioapic_in_kernel(vcpu->kvm))
8252 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008253 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008254
8255 if (is_guest_mode(vcpu))
8256 vcpu->arch.load_eoi_exitmap_pending = true;
8257 else
8258 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8259}
8260
8261static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8262{
8263 u64 eoi_exit_bitmap[4];
8264
8265 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8266 return;
8267
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008268 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8269 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008270 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008271}
8272
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +09008273void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8274 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008275{
8276 unsigned long apic_address;
8277
8278 /*
8279 * The physical address of apic access page is stored in the VMCS.
8280 * Update it when it becomes invalid.
8281 */
8282 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8283 if (start <= apic_address && apic_address < end)
8284 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
8285}
8286
Tang Chen4256f432014-09-24 15:57:54 +08008287void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8288{
Paolo Bonzini35754c92015-07-29 12:05:37 +02008289 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008290 return;
8291
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008292 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008293 return;
8294
Sean Christophersona4148b72020-03-20 14:28:24 -07008295 kvm_x86_ops.set_apic_access_page_addr(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008296}
Tang Chen4256f432014-09-24 15:57:54 +08008297
Sean Christophersond264ee02018-08-27 15:21:12 -07008298void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8299{
8300 smp_send_reschedule(vcpu->cpu);
8301}
8302EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8303
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008304/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008305 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008306 * exiting to the userspace. Otherwise, the value will be returned to the
8307 * userspace.
8308 */
Avi Kivity851ba692009-08-24 11:10:17 +03008309static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008310{
8311 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008312 bool req_int_win =
8313 dm_request_for_irq_injection(vcpu) &&
8314 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08008315 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08008316
Jan Kiszka730dca42013-04-28 10:50:52 +02008317 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008318
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008319 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008320 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04008321 if (unlikely(!kvm_x86_ops.nested_ops->get_vmcs12_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008322 r = 0;
8323 goto out;
8324 }
8325 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008326 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008327 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008328 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008329 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008330 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8331 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008332 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8333 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008334 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8335 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008336 if (unlikely(r))
8337 goto out;
8338 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008339 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008340 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008341 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8342 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008343 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07008344 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008345
8346 /* Flushing all ASIDs flushes the current ASID... */
8347 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
8348 }
8349 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
8350 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04008351 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
8352 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008353
Avi Kivitya8eeb042010-05-10 12:34:53 +03008354 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008355 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008356 r = 0;
8357 goto out;
8358 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008359 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008360 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008361 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008362 r = 0;
8363 goto out;
8364 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008365 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8366 /* Page is swapped out. Do synthetic halt */
8367 vcpu->arch.apf.halted = true;
8368 r = 1;
8369 goto out;
8370 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008371 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8372 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008373 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8374 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008375 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8376 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008377 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008378 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008379 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008380 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008381 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8382 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8383 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008384 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008385 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8386 vcpu->run->eoi.vector =
8387 vcpu->arch.pending_ioapic_eoi;
8388 r = 0;
8389 goto out;
8390 }
8391 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008392 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8393 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008394 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8395 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008396 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8397 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008398 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8399 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8400 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8401 r = 0;
8402 goto out;
8403 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008404 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8405 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8406 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8407 r = 0;
8408 goto out;
8409 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008410 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8411 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8412 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8413 r = 0;
8414 goto out;
8415 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008416
8417 /*
8418 * KVM_REQ_HV_STIMER has to be processed after
8419 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8420 * depend on the guest clock being up-to-date
8421 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008422 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8423 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008424 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8425 kvm_vcpu_update_apicv(vcpu);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02008426 if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
8427 kvm_check_async_pf_completion(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008428 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008429
Avi Kivityb463a6f2010-07-20 15:06:17 +03008430 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008431 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008432 kvm_apic_accept_events(vcpu);
8433 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8434 r = 1;
8435 goto out;
8436 }
8437
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008438 inject_pending_event(vcpu, &req_immediate_exit);
8439 if (req_int_win)
8440 kvm_x86_ops.enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008441
Avi Kivity3842d132010-07-27 12:30:24 +03008442 if (kvm_lapic_enabled(vcpu)) {
8443 update_cr8_intercept(vcpu);
8444 kvm_lapic_sync_to_vapic(vcpu);
8445 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008446 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008447
Avi Kivityd8368af2012-05-14 18:07:56 +03008448 r = kvm_mmu_reload(vcpu);
8449 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008450 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008451 }
8452
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008453 preempt_disable();
8454
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008455 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008456
8457 /*
8458 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8459 * IPI are then delayed after guest entry, which ensures that they
8460 * result in virtual interrupt delivery.
8461 */
8462 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008463 vcpu->mode = IN_GUEST_MODE;
8464
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008465 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8466
Lan Tianyu0f127d12016-03-13 11:10:29 +08008467 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008468 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008469 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008470 *
Luwei Kang81b01662019-01-31 16:52:02 +08008471 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008472 * pairs with the memory barrier implicit in pi_test_and_set_on
8473 * (see vmx_deliver_posted_interrupt).
8474 *
8475 * 3) This also orders the write to mode from any reads to the page
8476 * tables done while the VCPU is running. Please see the comment
8477 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008478 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008479 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008480
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008481 /*
8482 * This handles the case where a posted interrupt was
8483 * notified with kvm_vcpu_kick.
8484 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008485 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008486 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008487
Wanpeng Li5a9f5442020-04-28 14:23:26 +08008488 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008489 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008490 smp_wmb();
8491 local_irq_enable();
8492 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008493 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008494 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008495 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008496 }
8497
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008498 if (req_immediate_exit) {
8499 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008500 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008501 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008502
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008503 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008504 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008505
Sean Christopherson2620fe22020-01-17 11:30:51 -08008506 fpregs_assert_state_consistent();
8507 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8508 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008509
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008510 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008511 set_debugreg(0, 7);
8512 set_debugreg(vcpu->arch.eff_db[0], 0);
8513 set_debugreg(vcpu->arch.eff_db[1], 1);
8514 set_debugreg(vcpu->arch.eff_db[2], 2);
8515 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008516 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008517 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008518 }
8519
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008520 exit_fastpath = kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008521
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008522 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008523 * Do this here before restoring debug registers on the host. And
8524 * since we do this before handling the vmexit, a DR access vmexit
8525 * can (a) read the correct value of the debug registers, (b) set
8526 * KVM_DEBUGREG_WONT_EXIT again.
8527 */
8528 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008529 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008530 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008531 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008532 kvm_update_dr7(vcpu);
8533 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008534 }
8535
8536 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008537 * If the guest has used debug registers, at least dr7
8538 * will be disabled while returning to the host.
8539 * If we don't have active breakpoints in the host, we don't
8540 * care about the messed up debug address registers. But if
8541 * we have some of them active, restore the old state.
8542 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008543 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008544 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008545
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008546 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008547
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008548 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008549 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008550
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008551 kvm_x86_ops.handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008552
Sean Christophersond7a08882019-07-10 09:07:34 -07008553 /*
8554 * Consume any pending interrupts, including the possible source of
8555 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8556 * An instruction is required after local_irq_enable() to fully unblock
8557 * interrupts on processors that implement an interrupt shadow, the
8558 * stat.exits increment will do nicely.
8559 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008560 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008561 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008562 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008563 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008564 kvm_after_interrupt(vcpu);
8565
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008566 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008567 if (lapic_in_kernel(vcpu)) {
8568 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8569 if (delta != S64_MIN) {
8570 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8571 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8572 }
8573 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008574
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008575 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008576 preempt_enable();
8577
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008578 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008579
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008580 /*
8581 * Profile KVM exit RIPs:
8582 */
8583 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008584 unsigned long rip = kvm_rip_read(vcpu);
8585 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008586 }
8587
Zachary Amsdencc578282012-02-03 15:43:50 -02008588 if (unlikely(vcpu->arch.tsc_always_catchup))
8589 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008590
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008591 if (vcpu->arch.apic_attention)
8592 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008593
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008594 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008595 return r;
8596
8597cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07008598 if (req_immediate_exit)
8599 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008600 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008601 if (unlikely(vcpu->arch.apic_attention))
8602 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008603out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008604 return r;
8605}
8606
Paolo Bonzini362c6982015-02-06 12:48:04 +01008607static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8608{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008609 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008610 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008611 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8612 kvm_vcpu_block(vcpu);
8613 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008614
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008615 if (kvm_x86_ops.post_block)
8616 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008617
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008618 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8619 return 1;
8620 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008621
Paolo Bonzini362c6982015-02-06 12:48:04 +01008622 kvm_apic_accept_events(vcpu);
8623 switch(vcpu->arch.mp_state) {
8624 case KVM_MP_STATE_HALTED:
8625 vcpu->arch.pv.pv_unhalted = false;
8626 vcpu->arch.mp_state =
8627 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008628 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008629 case KVM_MP_STATE_RUNNABLE:
8630 vcpu->arch.apf.halted = false;
8631 break;
8632 case KVM_MP_STATE_INIT_RECEIVED:
8633 break;
8634 default:
8635 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008636 }
8637 return 1;
8638}
8639
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008640static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8641{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008642 if (is_guest_mode(vcpu))
Paolo Bonzini33b22172020-04-17 10:24:18 -04008643 kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008644
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008645 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8646 !vcpu->arch.apf.halted);
8647}
8648
Paolo Bonzini362c6982015-02-06 12:48:04 +01008649static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008650{
8651 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008652 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008653
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008654 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008655 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008656
Paolo Bonzini362c6982015-02-06 12:48:04 +01008657 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008658 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008659 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008660 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008661 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008662 }
8663
Gleb Natapov09cec752009-03-23 15:11:44 +02008664 if (r <= 0)
8665 break;
8666
Radim Krčmář72875d82017-04-26 22:32:19 +02008667 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008668 if (kvm_cpu_has_pending_timer(vcpu))
8669 kvm_inject_pending_timer_irqs(vcpu);
8670
Matt Gingell782d4222015-11-16 15:26:00 -08008671 if (dm_request_for_irq_injection(vcpu) &&
8672 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008673 r = 0;
8674 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008675 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008676 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008677 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008678
Gleb Natapov09cec752009-03-23 15:11:44 +02008679 if (signal_pending(current)) {
8680 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008681 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008682 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008683 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008684 }
8685 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008686 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008687 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008688 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008689 }
8690 }
8691
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008692 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008693
8694 return r;
8695}
8696
Gleb Natapov716d51a2012-09-03 15:24:26 +03008697static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8698{
8699 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008700
Gleb Natapov716d51a2012-09-03 15:24:26 +03008701 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008702 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008703 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008704 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008705}
8706
8707static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8708{
8709 BUG_ON(!vcpu->arch.pio.count);
8710
8711 return complete_emulated_io(vcpu);
8712}
8713
Avi Kivityf78146b2012-04-18 19:22:47 +03008714/*
8715 * Implements the following, as a state machine:
8716 *
8717 * read:
8718 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008719 * for each mmio piece in the fragment
8720 * write gpa, len
8721 * exit
8722 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008723 * execute insn
8724 *
8725 * write:
8726 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008727 * for each mmio piece in the fragment
8728 * write gpa, len
8729 * copy data
8730 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008731 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008732static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008733{
8734 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008735 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008736 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008737
Gleb Natapov716d51a2012-09-03 15:24:26 +03008738 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008739
Gleb Natapov716d51a2012-09-03 15:24:26 +03008740 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008741 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8742 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008743 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008744 memcpy(frag->data, run->mmio.data, len);
8745
8746 if (frag->len <= 8) {
8747 /* Switch to the next fragment. */
8748 frag++;
8749 vcpu->mmio_cur_fragment++;
8750 } else {
8751 /* Go forward to the next mmio piece. */
8752 frag->data += len;
8753 frag->gpa += len;
8754 frag->len -= len;
8755 }
8756
Andrew Honiga08d3b32014-02-27 19:35:14 +01008757 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008758 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008759
8760 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008761 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008762 return 1;
8763 vcpu->mmio_read_completed = 1;
8764 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008765 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008766
Gleb Natapov716d51a2012-09-03 15:24:26 +03008767 run->exit_reason = KVM_EXIT_MMIO;
8768 run->mmio.phys_addr = frag->gpa;
8769 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008770 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8771 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008772 run->mmio.is_write = vcpu->mmio_is_write;
8773 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8774 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008775}
8776
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008777static void kvm_save_current_fpu(struct fpu *fpu)
8778{
8779 /*
8780 * If the target FPU state is not resident in the CPU registers, just
8781 * memcpy() from current, else save CPU state directly to the target.
8782 */
8783 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8784 memcpy(&fpu->state, &current->thread.fpu.state,
8785 fpu_kernel_xstate_size);
8786 else
8787 copy_fpregs_to_fpstate(fpu);
8788}
8789
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008790/* Swap (qemu) user FPU context for the guest FPU context. */
8791static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8792{
Rik van Riel5f409e22019-04-03 18:41:52 +02008793 fpregs_lock();
8794
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008795 kvm_save_current_fpu(vcpu->arch.user_fpu);
8796
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008797 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008798 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008799 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008800
8801 fpregs_mark_activate();
8802 fpregs_unlock();
8803
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008804 trace_kvm_fpu(1);
8805}
8806
8807/* When vcpu_run ends, restore user space FPU context. */
8808static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8809{
Rik van Riel5f409e22019-04-03 18:41:52 +02008810 fpregs_lock();
8811
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008812 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8813
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008814 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008815
8816 fpregs_mark_activate();
8817 fpregs_unlock();
8818
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008819 ++vcpu->stat.fpu_reload;
8820 trace_kvm_fpu(0);
8821}
8822
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008823int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008824{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008825 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008826 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008827
Christoffer Dallaccb7572017-12-04 21:35:25 +01008828 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008829 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008830 kvm_load_guest_fpu(vcpu);
8831
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008832 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008833 if (kvm_run->immediate_exit) {
8834 r = -EINTR;
8835 goto out;
8836 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008837 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008838 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008839 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008840 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008841 if (signal_pending(current)) {
8842 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008843 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008844 ++vcpu->stat.signal_exits;
8845 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008846 goto out;
8847 }
8848
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008849 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008850 r = -EINVAL;
8851 goto out;
8852 }
8853
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008854 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008855 r = sync_regs(vcpu);
8856 if (r != 0)
8857 goto out;
8858 }
8859
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008860 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008861 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008862 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8863 r = -EINVAL;
8864 goto out;
8865 }
8866 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008867
Gleb Natapov716d51a2012-09-03 15:24:26 +03008868 if (unlikely(vcpu->arch.complete_userspace_io)) {
8869 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8870 vcpu->arch.complete_userspace_io = NULL;
8871 r = cui(vcpu);
8872 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008873 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008874 } else
8875 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008876
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008877 if (kvm_run->immediate_exit)
8878 r = -EINTR;
8879 else
8880 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008881
8882out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008883 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008884 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08008885 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008886 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008887 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008888
Christoffer Dallaccb7572017-12-04 21:35:25 +01008889 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008890 return r;
8891}
8892
Ken Hofsass01643c52018-01-31 16:03:36 -08008893static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008894{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008895 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8896 /*
8897 * We are here if userspace calls get_regs() in the middle of
8898 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008899 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008900 * that usually, but some bad designed PV devices (vmware
8901 * backdoor interface) need this to work
8902 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008903 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008904 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8905 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008906 regs->rax = kvm_rax_read(vcpu);
8907 regs->rbx = kvm_rbx_read(vcpu);
8908 regs->rcx = kvm_rcx_read(vcpu);
8909 regs->rdx = kvm_rdx_read(vcpu);
8910 regs->rsi = kvm_rsi_read(vcpu);
8911 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008912 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008913 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008914#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008915 regs->r8 = kvm_r8_read(vcpu);
8916 regs->r9 = kvm_r9_read(vcpu);
8917 regs->r10 = kvm_r10_read(vcpu);
8918 regs->r11 = kvm_r11_read(vcpu);
8919 regs->r12 = kvm_r12_read(vcpu);
8920 regs->r13 = kvm_r13_read(vcpu);
8921 regs->r14 = kvm_r14_read(vcpu);
8922 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008923#endif
8924
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008925 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008926 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008927}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008928
Ken Hofsass01643c52018-01-31 16:03:36 -08008929int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8930{
8931 vcpu_load(vcpu);
8932 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008933 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008934 return 0;
8935}
8936
Ken Hofsass01643c52018-01-31 16:03:36 -08008937static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008938{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008939 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8940 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8941
Sean Christophersonde3cd112019-04-30 10:36:17 -07008942 kvm_rax_write(vcpu, regs->rax);
8943 kvm_rbx_write(vcpu, regs->rbx);
8944 kvm_rcx_write(vcpu, regs->rcx);
8945 kvm_rdx_write(vcpu, regs->rdx);
8946 kvm_rsi_write(vcpu, regs->rsi);
8947 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008948 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008949 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008950#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008951 kvm_r8_write(vcpu, regs->r8);
8952 kvm_r9_write(vcpu, regs->r9);
8953 kvm_r10_write(vcpu, regs->r10);
8954 kvm_r11_write(vcpu, regs->r11);
8955 kvm_r12_write(vcpu, regs->r12);
8956 kvm_r13_write(vcpu, regs->r13);
8957 kvm_r14_write(vcpu, regs->r14);
8958 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008959#endif
8960
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008961 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008962 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008963
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008964 vcpu->arch.exception.pending = false;
8965
Avi Kivity3842d132010-07-27 12:30:24 +03008966 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008967}
Avi Kivity3842d132010-07-27 12:30:24 +03008968
Ken Hofsass01643c52018-01-31 16:03:36 -08008969int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8970{
8971 vcpu_load(vcpu);
8972 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008973 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008974 return 0;
8975}
8976
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008977void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8978{
8979 struct kvm_segment cs;
8980
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008981 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008982 *db = cs.db;
8983 *l = cs.l;
8984}
8985EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8986
Ken Hofsass01643c52018-01-31 16:03:36 -08008987static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008988{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008989 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008990
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008991 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8992 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8993 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8994 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8995 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8996 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008997
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008998 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8999 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009000
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009001 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009002 sregs->idt.limit = dt.size;
9003 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009004 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009005 sregs->gdt.limit = dt.size;
9006 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009007
Avi Kivity4d4ec082009-12-29 18:07:30 +02009008 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009009 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009010 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02009011 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009012 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02009013 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009014 sregs->apic_base = kvm_get_apic_base(vcpu);
9015
Jordan Borgner0e96f312018-10-28 12:58:28 +00009016 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009017
Liran Alon04140b42018-03-23 03:01:31 +03009018 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03009019 set_bit(vcpu->arch.interrupt.nr,
9020 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08009021}
Gleb Natapov16d7a192009-04-21 17:45:10 +03009022
Ken Hofsass01643c52018-01-31 16:03:36 -08009023int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
9024 struct kvm_sregs *sregs)
9025{
9026 vcpu_load(vcpu);
9027 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01009028 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009029 return 0;
9030}
9031
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009032int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
9033 struct kvm_mp_state *mp_state)
9034{
Christoffer Dallfd232562017-12-04 21:35:30 +01009035 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08009036 if (kvm_mpx_supported())
9037 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009038
Jan Kiszka66450a22013-03-13 12:42:34 +01009039 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05309040 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
9041 vcpu->arch.pv.pv_unhalted)
9042 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
9043 else
9044 mp_state->mp_state = vcpu->arch.mp_state;
9045
Sean Christophersonf958bd22019-12-09 12:19:31 -08009046 if (kvm_mpx_supported())
9047 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009048 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009049 return 0;
9050}
9051
9052int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
9053 struct kvm_mp_state *mp_state)
9054{
Christoffer Dalle83dff52017-12-04 21:35:31 +01009055 int ret = -EINVAL;
9056
9057 vcpu_load(vcpu);
9058
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009059 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01009060 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01009061 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01009062
Liran Alon27cbe7d2019-11-11 11:16:40 +02009063 /*
9064 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
9065 * INIT state; latched init should be reported using
9066 * KVM_SET_VCPU_EVENTS, so reject it here.
9067 */
9068 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01009069 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
9070 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01009071 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01009072
Jan Kiszka66450a22013-03-13 12:42:34 +01009073 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
9074 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
9075 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
9076 } else
9077 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03009078 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01009079
9080 ret = 0;
9081out:
9082 vcpu_put(vcpu);
9083 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009084}
9085
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009086int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
9087 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009088{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009089 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009090 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009091
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009092 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02009093
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009094 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009095 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009096 if (ret) {
9097 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9098 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9099 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009100 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009101 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009102
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009103 kvm_rip_write(vcpu, ctxt->eip);
9104 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009105 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009106}
9107EXPORT_SYMBOL_GPL(kvm_task_switch);
9108
Peng Hao3140c152018-04-02 09:15:32 +08009109static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009110{
Tianyu Lan37b95952018-01-16 17:34:07 +08009111 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009112 /*
9113 * When EFER.LME and CR0.PG are set, the processor is in
9114 * 64-bit mode (though maybe in a 32-bit code segment).
9115 * CR4.PAE and EFER.LMA must be set.
9116 */
Tianyu Lan37b95952018-01-16 17:34:07 +08009117 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05009118 || !(sregs->efer & EFER_LMA))
9119 return -EINVAL;
9120 } else {
9121 /*
9122 * Not in 64-bit mode: EFER.LMA is clear and the code
9123 * segment cannot be 64-bit.
9124 */
9125 if (sregs->efer & EFER_LMA || sregs->cs.l)
9126 return -EINVAL;
9127 }
9128
Wanpeng Li3ca94192019-09-18 17:50:10 +08009129 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009130}
9131
Ken Hofsass01643c52018-01-31 16:03:36 -08009132static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009133{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009134 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009135 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009136 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009137 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009138 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009139 int ret = -EINVAL;
9140
Lan Tianyuf2981032017-12-14 03:01:52 -05009141 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009142 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009143
Jim Mattsond3802282017-08-10 10:14:13 -07009144 apic_base_msr.data = sregs->apic_base;
9145 apic_base_msr.host_initiated = true;
9146 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009147 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009148
Gleb Natapov89a27f42010-02-16 10:51:48 +02009149 dt.size = sregs->idt.limit;
9150 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009151 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009152 dt.size = sregs->gdt.limit;
9153 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009154 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009155
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009156 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009157 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009158 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009159 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009160
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009161 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009162
Avi Kivityf6801df2010-01-21 15:31:50 +02009163 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009164 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009165
Avi Kivity4d4ec082009-12-29 18:07:30 +02009166 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009167 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009168 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009169
Avi Kivityfc78f512009-12-07 12:16:48 +02009170 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009171 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9172 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009173 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009174 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009175 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009176
9177 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009178 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009179 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009180 mmu_reset_needed = 1;
9181 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009182 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009183
9184 if (mmu_reset_needed)
9185 kvm_mmu_reset_context(vcpu);
9186
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009187 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009188 pending_vec = find_first_bit(
9189 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9190 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009191 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009192 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009193 }
9194
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009195 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9196 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9197 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9198 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9199 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9200 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009201
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009202 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9203 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009204
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009205 update_cr8_intercept(vcpu);
9206
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009207 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009208 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009209 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009210 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009211 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9212
Avi Kivity3842d132010-07-27 12:30:24 +03009213 kvm_make_request(KVM_REQ_EVENT, vcpu);
9214
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009215 ret = 0;
9216out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009217 return ret;
9218}
9219
9220int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9221 struct kvm_sregs *sregs)
9222{
9223 int ret;
9224
9225 vcpu_load(vcpu);
9226 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009227 vcpu_put(vcpu);
9228 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009229}
9230
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009231int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9232 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009233{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009234 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009235 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009236
Christoffer Dall66b56562017-12-04 21:35:33 +01009237 vcpu_load(vcpu);
9238
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009239 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9240 r = -EBUSY;
9241 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009242 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009243 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9244 kvm_queue_exception(vcpu, DB_VECTOR);
9245 else
9246 kvm_queue_exception(vcpu, BP_VECTOR);
9247 }
9248
Jan Kiszka91586a32009-10-05 13:07:21 +02009249 /*
9250 * Read rflags as long as potentially injected trace flags are still
9251 * filtered out.
9252 */
9253 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009254
9255 vcpu->guest_debug = dbg->control;
9256 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9257 vcpu->guest_debug = 0;
9258
9259 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009260 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9261 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009262 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009263 } else {
9264 for (i = 0; i < KVM_NR_DB_REGS; i++)
9265 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009266 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009267 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009268
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009269 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9270 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9271 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009272
Jan Kiszka91586a32009-10-05 13:07:21 +02009273 /*
9274 * Trigger an rflags update that will inject or remove the trace
9275 * flags.
9276 */
9277 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009278
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009279 kvm_x86_ops.update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009280
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009281 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009282
Avi Kivity2122ff52010-05-13 11:25:04 +03009283out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009284 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009285 return r;
9286}
9287
9288/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009289 * Translate a guest virtual address to a guest physical address.
9290 */
9291int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9292 struct kvm_translation *tr)
9293{
9294 unsigned long vaddr = tr->linear_address;
9295 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009296 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009297
Christoffer Dall1da5b612017-12-04 21:35:32 +01009298 vcpu_load(vcpu);
9299
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009300 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009301 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009302 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009303 tr->physical_address = gpa;
9304 tr->valid = gpa != UNMAPPED_GVA;
9305 tr->writeable = 1;
9306 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009307
Christoffer Dall1da5b612017-12-04 21:35:32 +01009308 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009309 return 0;
9310}
9311
Hollis Blanchardd0752062007-10-31 17:24:25 -05009312int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9313{
Christoffer Dall13931232017-12-04 21:35:34 +01009314 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009315
Christoffer Dall13931232017-12-04 21:35:34 +01009316 vcpu_load(vcpu);
9317
Marc Orrb666a4b2018-11-06 14:53:56 -08009318 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009319 memcpy(fpu->fpr, fxsave->st_space, 128);
9320 fpu->fcw = fxsave->cwd;
9321 fpu->fsw = fxsave->swd;
9322 fpu->ftwx = fxsave->twd;
9323 fpu->last_opcode = fxsave->fop;
9324 fpu->last_ip = fxsave->rip;
9325 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009326 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009327
Christoffer Dall13931232017-12-04 21:35:34 +01009328 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009329 return 0;
9330}
9331
9332int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9333{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009334 struct fxregs_state *fxsave;
9335
9336 vcpu_load(vcpu);
9337
Marc Orrb666a4b2018-11-06 14:53:56 -08009338 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009339
Hollis Blanchardd0752062007-10-31 17:24:25 -05009340 memcpy(fxsave->st_space, fpu->fpr, 128);
9341 fxsave->cwd = fpu->fcw;
9342 fxsave->swd = fpu->fsw;
9343 fxsave->twd = fpu->ftwx;
9344 fxsave->fop = fpu->last_opcode;
9345 fxsave->rip = fpu->last_ip;
9346 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009347 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009348
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009349 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009350 return 0;
9351}
9352
Ken Hofsass01643c52018-01-31 16:03:36 -08009353static void store_regs(struct kvm_vcpu *vcpu)
9354{
9355 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9356
9357 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9358 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9359
9360 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9361 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9362
9363 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9364 kvm_vcpu_ioctl_x86_get_vcpu_events(
9365 vcpu, &vcpu->run->s.regs.events);
9366}
9367
9368static int sync_regs(struct kvm_vcpu *vcpu)
9369{
9370 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9371 return -EINVAL;
9372
9373 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9374 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9375 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9376 }
9377 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9378 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9379 return -EINVAL;
9380 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9381 }
9382 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9383 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9384 vcpu, &vcpu->run->s.regs.events))
9385 return -EINVAL;
9386 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9387 }
9388
9389 return 0;
9390}
9391
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009392static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009393{
Marc Orrb666a4b2018-11-06 14:53:56 -08009394 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009395 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009396 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009397 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009398
Dexuan Cui2acf9232010-06-10 11:27:12 +08009399 /*
9400 * Ensure guest xcr0 is valid for loading
9401 */
Dave Hansend91cab72015-09-02 16:31:26 -07009402 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009403
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009404 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009405}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009406
Sean Christopherson897cc382019-12-18 13:55:09 -08009407int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009408{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009409 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009410 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9411 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009412
Sean Christopherson897cc382019-12-18 13:55:09 -08009413 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009414}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009415
Sean Christophersone529ef62019-12-18 13:55:15 -08009416int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009417{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009418 struct page *page;
9419 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009420
Sean Christopherson95a0d012019-12-18 13:55:23 -08009421 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9422 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9423 else
9424 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9425
9426 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9427
9428 r = kvm_mmu_create(vcpu);
9429 if (r < 0)
9430 return r;
9431
9432 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009433 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9434 if (r < 0)
9435 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009436 if (kvm_apicv_activated(vcpu->kvm))
9437 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009438 } else
9439 static_key_slow_inc(&kvm_no_apic_vcpu);
9440
9441 r = -ENOMEM;
9442
9443 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9444 if (!page)
9445 goto fail_free_lapic;
9446 vcpu->arch.pio_data = page_address(page);
9447
9448 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9449 GFP_KERNEL_ACCOUNT);
9450 if (!vcpu->arch.mce_banks)
9451 goto fail_free_pio_data;
9452 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9453
9454 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9455 GFP_KERNEL_ACCOUNT))
9456 goto fail_free_mce_banks;
9457
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009458 if (!alloc_emulate_ctxt(vcpu))
9459 goto free_wbinvd_dirty_mask;
9460
Sean Christopherson95a0d012019-12-18 13:55:23 -08009461 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9462 GFP_KERNEL_ACCOUNT);
9463 if (!vcpu->arch.user_fpu) {
9464 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009465 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009466 }
9467
9468 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9469 GFP_KERNEL_ACCOUNT);
9470 if (!vcpu->arch.guest_fpu) {
9471 pr_err("kvm: failed to allocate vcpu's fpu\n");
9472 goto free_user_fpu;
9473 }
9474 fx_init(vcpu);
9475
Sean Christopherson95a0d012019-12-18 13:55:23 -08009476 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christopherson7d2e8742020-05-27 01:54:00 -07009477 vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009478
9479 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9480
9481 kvm_async_pf_hash_reset(vcpu);
9482 kvm_pmu_init(vcpu);
9483
9484 vcpu->arch.pending_external_vector = -1;
9485 vcpu->arch.preempted_in_kernel = false;
9486
9487 kvm_hv_vcpu_init(vcpu);
9488
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009489 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009490 if (r)
9491 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009492
Sean Christopherson0cf91352019-03-07 15:43:02 -08009493 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009494 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009495 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009496 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009497 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009498 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009499 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009500 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009501
9502free_guest_fpu:
9503 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9504free_user_fpu:
9505 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009506free_emulate_ctxt:
9507 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009508free_wbinvd_dirty_mask:
9509 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9510fail_free_mce_banks:
9511 kfree(vcpu->arch.mce_banks);
9512fail_free_pio_data:
9513 free_page((unsigned long)vcpu->arch.pio_data);
9514fail_free_lapic:
9515 kvm_free_lapic(vcpu);
9516fail_mmu_destroy:
9517 kvm_mmu_destroy(vcpu);
9518 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009519}
9520
Dominik Dingel31928aa2014-12-04 15:47:07 +01009521void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009522{
Will Auld8fe8ab42012-11-29 12:42:12 -08009523 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009524 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009525
Roman Kagand3457c82017-07-14 17:13:20 +03009526 kvm_hv_vcpu_postcreate(vcpu);
9527
Christoffer Dallec7660c2017-12-04 21:35:23 +01009528 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009529 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009530 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009531 msr.data = 0x0;
9532 msr.index = MSR_IA32_TSC;
9533 msr.host_initiated = true;
9534 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009535 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009536
9537 /* poll control enabled by default */
9538 vcpu->arch.msr_kvm_poll_control = 1;
9539
Christoffer Dallec7660c2017-12-04 21:35:23 +01009540 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009541
Wanpeng Lib34de572020-02-28 11:18:41 +08009542 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9543 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9544 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009545}
9546
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009547void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009548{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009549 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009550 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009551
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009552 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9553
Sean Christopherson50b143e2019-12-18 13:55:07 -08009554 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009555
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009556 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009557
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009558 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009559 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9560 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9561 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009562
9563 kvm_hv_vcpu_uninit(vcpu);
9564 kvm_pmu_destroy(vcpu);
9565 kfree(vcpu->arch.mce_banks);
9566 kvm_free_lapic(vcpu);
9567 idx = srcu_read_lock(&vcpu->kvm->srcu);
9568 kvm_mmu_destroy(vcpu);
9569 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9570 free_page((unsigned long)vcpu->arch.pio_data);
9571 if (!lapic_in_kernel(vcpu))
9572 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009573}
9574
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009575void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009576{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009577 kvm_lapic_reset(vcpu, init_event);
9578
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009579 vcpu->arch.hflags = 0;
9580
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009581 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009582 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009583 atomic_set(&vcpu->arch.nmi_queued, 0);
9584 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009585 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009586 kvm_clear_interrupt_queue(vcpu);
9587 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009588
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009589 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009590 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009591 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009592 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009593 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009594
Nadav Amit11190222015-04-02 03:10:38 +03009595 vcpu->arch.cr2 = 0;
9596
Avi Kivity3842d132010-07-27 12:30:24 +03009597 kvm_make_request(KVM_REQ_EVENT, vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02009598 vcpu->arch.apf.msr_en_val = 0;
9599 vcpu->arch.apf.msr_int_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009600 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009601
Glauber Costa12f9a482011-02-01 14:16:40 -05009602 kvmclock_reset(vcpu);
9603
Gleb Natapovaf585b92010-10-14 11:22:46 +02009604 kvm_clear_async_pf_completion_queue(vcpu);
9605 kvm_async_pf_hash_reset(vcpu);
9606 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009607
Wanpeng Lia554d202017-10-11 05:10:19 -07009608 if (kvm_mpx_supported()) {
9609 void *mpx_state_buffer;
9610
9611 /*
9612 * To avoid have the INIT path from kvm_apic_has_events() that be
9613 * called with loaded FPU and does not let userspace fix the state.
9614 */
Rik van Rielf775b132017-11-14 16:54:23 -05009615 if (init_event)
9616 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009617 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009618 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009619 if (mpx_state_buffer)
9620 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009621 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009622 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009623 if (mpx_state_buffer)
9624 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009625 if (init_event)
9626 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009627 }
9628
Paolo Bonzini64d60672015-05-07 11:36:11 +02009629 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009630 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009631 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009632
Kyle Hueydb2336a2017-03-20 01:16:28 -07009633 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009634
9635 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009636 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009637
Julian Stecklina66f7b722012-12-05 15:26:19 +01009638 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9639 vcpu->arch.regs_avail = ~0;
9640 vcpu->arch.regs_dirty = ~0;
9641
Wanpeng Lia554d202017-10-11 05:10:19 -07009642 vcpu->arch.ia32_xss = 0;
9643
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009644 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009645}
9646
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009647void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009648{
9649 struct kvm_segment cs;
9650
9651 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9652 cs.selector = vector << 8;
9653 cs.base = vector << 12;
9654 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9655 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009656}
9657
Radim Krčmář13a34e02014-08-28 15:13:03 +02009658int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009659{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009660 struct kvm *kvm;
9661 struct kvm_vcpu *vcpu;
9662 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009663 int ret;
9664 u64 local_tsc;
9665 u64 max_tsc = 0;
9666 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009667
9668 kvm_shared_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009669 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009670 if (ret != 0)
9671 return ret;
9672
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009673 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009674 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009675 list_for_each_entry(kvm, &vm_list, vm_list) {
9676 kvm_for_each_vcpu(i, vcpu, kvm) {
9677 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009678 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009679 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9680 backwards_tsc = true;
9681 if (vcpu->arch.last_host_tsc > max_tsc)
9682 max_tsc = vcpu->arch.last_host_tsc;
9683 }
9684 }
9685 }
9686
9687 /*
9688 * Sometimes, even reliable TSCs go backwards. This happens on
9689 * platforms that reset TSC during suspend or hibernate actions, but
9690 * maintain synchronization. We must compensate. Fortunately, we can
9691 * detect that condition here, which happens early in CPU bringup,
9692 * before any KVM threads can be running. Unfortunately, we can't
9693 * bring the TSCs fully up to date with real time, as we aren't yet far
9694 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009695 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009696 * variables that haven't been updated yet.
9697 *
9698 * So we simply find the maximum observed TSC above, then record the
9699 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9700 * the adjustment will be applied. Note that we accumulate
9701 * adjustments, in case multiple suspend cycles happen before some VCPU
9702 * gets a chance to run again. In the event that no KVM threads get a
9703 * chance to run, we will miss the entire elapsed period, as we'll have
9704 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9705 * loose cycle time. This isn't too big a deal, since the loss will be
9706 * uniform across all VCPUs (not to mention the scenario is extremely
9707 * unlikely). It is possible that a second hibernate recovery happens
9708 * much faster than a first, causing the observed TSC here to be
9709 * smaller; this would require additional padding adjustment, which is
9710 * why we set last_host_tsc to the local tsc observed here.
9711 *
9712 * N.B. - this code below runs only on platforms with reliable TSC,
9713 * as that is the only way backwards_tsc is set above. Also note
9714 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9715 * have the same delta_cyc adjustment applied if backwards_tsc
9716 * is detected. Note further, this adjustment is only done once,
9717 * as we reset last_host_tsc on all VCPUs to stop this from being
9718 * called multiple times (one for each physical CPU bringup).
9719 *
Guo Chao4a969982012-06-28 15:17:27 +08009720 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009721 * will be compensated by the logic in vcpu_load, which sets the TSC to
9722 * catchup mode. This will catchup all VCPUs to real time, but cannot
9723 * guarantee that they stay in perfect synchronization.
9724 */
9725 if (backwards_tsc) {
9726 u64 delta_cyc = max_tsc - local_tsc;
9727 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009728 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009729 kvm_for_each_vcpu(i, vcpu, kvm) {
9730 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9731 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009732 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009733 }
9734
9735 /*
9736 * We have to disable TSC offset matching.. if you were
9737 * booting a VM while issuing an S4 host suspend....
9738 * you may have some problem. Solving this issue is
9739 * left as an exercise to the reader.
9740 */
9741 kvm->arch.last_tsc_nsec = 0;
9742 kvm->arch.last_tsc_write = 0;
9743 }
9744
9745 }
9746 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009747}
9748
Radim Krčmář13a34e02014-08-28 15:13:03 +02009749void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009750{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009751 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009752 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009753}
9754
Sean Christophersonb9904082020-03-21 13:25:55 -07009755int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009756{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009757 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009758 int r;
9759
Sean Christopherson91661982020-03-02 15:57:06 -08009760 rdmsrl_safe(MSR_EFER, &host_efer);
9761
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009762 if (boot_cpu_has(X86_FEATURE_XSAVES))
9763 rdmsrl(MSR_IA32_XSS, host_xss);
9764
Sean Christophersond008dfd2020-03-21 13:25:56 -07009765 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009766 if (r != 0)
9767 return r;
9768
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009769 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -07009770
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009771 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9772 supported_xss = 0;
9773
Paolo Bonzini139f7422020-05-05 09:40:46 -04009774#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
9775 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
9776#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -08009777
Haozhong Zhang35181e82015-10-20 15:39:03 +08009778 if (kvm_has_tsc_control) {
9779 /*
9780 * Make sure the user can only configure tsc_khz values that
9781 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009782 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009783 * be 1 on all machines.
9784 */
9785 u64 max = min(0x7fffffffULL,
9786 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9787 kvm_max_guest_tsc_khz = max;
9788
Haozhong Zhangad7218832015-10-20 15:39:02 +08009789 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009790 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009791
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009792 kvm_init_msr_list();
9793 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009794}
9795
9796void kvm_arch_hardware_unsetup(void)
9797{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009798 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009799}
9800
Sean Christophersonb9904082020-03-21 13:25:55 -07009801int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009802{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009803 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009804 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009805
9806 WARN_ON(!irqs_disabled());
9807
Paolo Bonzini139f7422020-05-05 09:40:46 -04009808 if (__cr4_reserved_bits(cpu_has, c) !=
9809 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009810 return -EIO;
9811
Sean Christophersond008dfd2020-03-21 13:25:56 -07009812 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009813}
9814
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009815bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9816{
9817 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9818}
9819EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9820
9821bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9822{
9823 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9824}
9825
Gleb Natapov54e98182012-08-05 15:58:32 +03009826struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009827EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009828
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009829void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9830{
Like Xub35e5542019-10-27 18:52:43 +08009831 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9832
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009833 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009834 if (pmu->version && unlikely(pmu->event_count)) {
9835 pmu->need_cleanup = true;
9836 kvm_make_request(KVM_REQ_PMU, vcpu);
9837 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009838 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009839}
9840
Sean Christopherson562b6b02020-01-26 16:41:13 -08009841void kvm_arch_free_vm(struct kvm *kvm)
9842{
9843 kfree(kvm->arch.hyperv.hv_pa_pg);
9844 vfree(kvm);
9845}
9846
9847
Carsten Ottee08b9632012-01-04 10:25:20 +01009848int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009849{
Carsten Ottee08b9632012-01-04 10:25:20 +01009850 if (type)
9851 return -EINVAL;
9852
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009853 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009854 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009855 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009856 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009857 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009858 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009859
Sheng Yang5550af42008-10-15 20:15:06 +08009860 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9861 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009862 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9863 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9864 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009865
Jan Kiszka038f8c12011-02-04 10:49:11 +01009866 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009867 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009868 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9869
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009870 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009871 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009872
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009873 kvm->arch.guest_can_read_msr_platform_info = true;
9874
Andrew Jones7e44e442014-02-28 12:52:54 +01009875 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009876 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009877
Roman Kagancbc02362018-02-01 16:48:31 +03009878 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009879 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009880 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009881
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009882 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009883}
9884
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009885int kvm_arch_post_init_vm(struct kvm *kvm)
9886{
9887 return kvm_mmu_post_init_vm(kvm);
9888}
9889
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009890static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9891{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009892 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009893 kvm_mmu_unload(vcpu);
9894 vcpu_put(vcpu);
9895}
9896
9897static void kvm_free_vcpus(struct kvm *kvm)
9898{
9899 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009900 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009901
9902 /*
9903 * Unpin any mmu pages first.
9904 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009905 kvm_for_each_vcpu(i, vcpu, kvm) {
9906 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009907 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009908 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009909 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009910 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009911
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009912 mutex_lock(&kvm->lock);
9913 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9914 kvm->vcpus[i] = NULL;
9915
9916 atomic_set(&kvm->online_vcpus, 0);
9917 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009918}
9919
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009920void kvm_arch_sync_events(struct kvm *kvm)
9921{
Andrew Jones332967a2014-02-28 12:52:55 +01009922 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009923 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009924 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009925}
9926
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009927int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009928{
9929 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009930 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009931 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009932 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009933
9934 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009935 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9936 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009937
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009938 slot = id_to_memslot(slots, id);
9939 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009940 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009941 return -EEXIST;
9942
9943 /*
9944 * MAP_SHARED to prevent internal slot pages from being moved
9945 * by fork()/COW.
9946 */
9947 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9948 MAP_SHARED | MAP_ANONYMOUS, 0);
9949 if (IS_ERR((void *)hva))
9950 return PTR_ERR((void *)hva);
9951 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009952 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009953 return 0;
9954
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009955 /*
9956 * Stuff a non-canonical value to catch use-after-delete. This
9957 * ends up being 0 on 32-bit KVM, but there's no better
9958 * alternative.
9959 */
9960 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009961 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009962 }
9963
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009964 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009965 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009966
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009967 m.slot = id | (i << 16);
9968 m.flags = 0;
9969 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009970 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009971 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009972 r = __kvm_set_memory_region(kvm, &m);
9973 if (r < 0)
9974 return r;
9975 }
9976
Eric Biggers103c7632018-01-31 17:30:21 -08009977 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009978 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009979
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009980 return 0;
9981}
9982EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9983
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009984void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9985{
9986 kvm_mmu_pre_destroy_vm(kvm);
9987}
9988
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009989void kvm_arch_destroy_vm(struct kvm *kvm)
9990{
Andrew Honig27469d22013-04-18 09:38:14 -07009991 if (current->mm == kvm->mm) {
9992 /*
9993 * Free memory regions allocated on behalf of userspace,
9994 * unless the the memory map has changed due to process exit
9995 * or fd copying.
9996 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009997 mutex_lock(&kvm->slots_lock);
9998 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9999 0, 0);
10000 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
10001 0, 0);
10002 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
10003 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -070010004 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010005 if (kvm_x86_ops.vm_destroy)
10006 kvm_x86_ops.vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +080010007 kvm_pic_destroy(kvm);
10008 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010009 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +020010010 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -070010011 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010012 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +020010013 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030010014 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010015}
Zhang Xiantao0de10342007-11-20 16:25:04 +080010016
Sean Christophersone96c81e2020-02-18 13:07:27 -080010017void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010018{
10019 int i;
10020
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010021 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080010022 kvfree(slot->arch.rmap[i]);
10023 slot->arch.rmap[i] = NULL;
10024
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010025 if (i == 0)
10026 continue;
10027
Sean Christophersone96c81e2020-02-18 13:07:27 -080010028 kvfree(slot->arch.lpage_info[i - 1]);
10029 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010030 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010031
Sean Christophersone96c81e2020-02-18 13:07:27 -080010032 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010033}
10034
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010035static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
10036 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010037{
10038 int i;
10039
Sean Christophersonedd4fa32020-02-18 13:07:15 -080010040 /*
10041 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
10042 * old arrays will be freed by __kvm_set_memory_region() if installing
10043 * the new memslot is successful.
10044 */
10045 memset(&slot->arch, 0, sizeof(slot->arch));
10046
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010047 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010048 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010049 unsigned long ugfn;
10050 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010051 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010052
10053 lpages = gfn_to_index(slot->base_gfn + npages - 1,
10054 slot->base_gfn, level) + 1;
10055
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010056 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -070010057 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -080010058 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010059 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010060 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010061 if (i == 0)
10062 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010063
Ben Gardon254272c2019-02-11 11:02:50 -080010064 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010065 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010066 goto out_free;
10067
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010068 slot->arch.lpage_info[i - 1] = linfo;
10069
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010070 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010071 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010072 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010073 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010074 ugfn = slot->userspace_addr >> PAGE_SHIFT;
10075 /*
10076 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080010077 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010078 */
Sean Christopherson600087b2020-03-02 15:57:05 -080010079 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010080 unsigned long j;
10081
10082 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010083 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010084 }
10085 }
10086
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010087 if (kvm_page_track_create_memslot(slot, npages))
10088 goto out_free;
10089
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010090 return 0;
10091
10092out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010093 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010094 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010095 slot->arch.rmap[i] = NULL;
10096 if (i == 0)
10097 continue;
10098
Thomas Huth548ef282015-02-24 21:29:25 +010010099 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010100 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010101 }
10102 return -ENOMEM;
10103}
10104
Sean Christopherson15248252019-02-05 12:54:17 -080010105void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010106{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010107 struct kvm_vcpu *vcpu;
10108 int i;
10109
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010110 /*
10111 * memslots->generation has been incremented.
10112 * mmio generation may have reached its maximum value.
10113 */
Sean Christopherson15248252019-02-05 12:54:17 -080010114 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010115
10116 /* Force re-initialization of steal_time cache */
10117 kvm_for_each_vcpu(i, vcpu, kvm)
10118 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010119}
10120
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010121int kvm_arch_prepare_memory_region(struct kvm *kvm,
10122 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010123 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010124 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010125{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010126 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10127 return kvm_alloc_memslot_metadata(memslot,
10128 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010129 return 0;
10130}
10131
Kai Huang88178fd2015-01-28 10:54:27 +080010132static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
Anthony Yznaga37416792020-06-02 13:07:30 -070010133 struct kvm_memory_slot *old,
10134 struct kvm_memory_slot *new,
10135 enum kvm_mr_change change)
Kai Huang88178fd2015-01-28 10:54:27 +080010136{
Anthony Yznaga37416792020-06-02 13:07:30 -070010137 /*
10138 * Nothing to do for RO slots or CREATE/MOVE/DELETE of a slot.
10139 * See comments below.
10140 */
10141 if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY))
Kai Huang88178fd2015-01-28 10:54:27 +080010142 return;
Kai Huang88178fd2015-01-28 10:54:27 +080010143
10144 /*
Anthony Yznaga37416792020-06-02 13:07:30 -070010145 * Dirty logging tracks sptes in 4k granularity, meaning that large
10146 * sptes have to be split. If live migration is successful, the guest
10147 * in the source machine will be destroyed and large sptes will be
10148 * created in the destination. However, if the guest continues to run
10149 * in the source machine (for example if live migration fails), small
10150 * sptes will remain around and cause bad performance.
Kai Huang88178fd2015-01-28 10:54:27 +080010151 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010152 * Scan sptes if dirty logging has been stopped, dropping those
10153 * which can be collapsed into a single large-page spte. Later
10154 * page faults will create the large-page sptes.
Kai Huang88178fd2015-01-28 10:54:27 +080010155 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010156 * There is no need to do this in any of the following cases:
10157 * CREATE: No dirty mappings will already exist.
10158 * MOVE/DELETE: The old mappings will already have been cleaned up by
10159 * kvm_arch_flush_shadow_memslot()
10160 */
10161 if ((old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10162 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10163 kvm_mmu_zap_collapsible_sptes(kvm, new);
10164
10165 /*
10166 * Enable or disable dirty logging for the slot.
Kai Huang88178fd2015-01-28 10:54:27 +080010167 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010168 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of the old
10169 * slot have been zapped so no dirty logging updates are needed for
10170 * the old slot.
10171 * For KVM_MR_CREATE and KVM_MR_MOVE, once the new slot is visible
10172 * any mappings that might be created in it will consume the
10173 * properties of the new slot and do not need to be updated here.
Kai Huang88178fd2015-01-28 10:54:27 +080010174 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010175 * When PML is enabled, the kvm_x86_ops dirty logging hooks are
10176 * called to enable/disable dirty logging.
Kai Huang88178fd2015-01-28 10:54:27 +080010177 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010178 * When disabling dirty logging with PML enabled, the D-bit is set
10179 * for sptes in the slot in order to prevent unnecessary GPA
10180 * logging in the PML buffer (and potential PML buffer full VMEXIT).
10181 * This guarantees leaving PML enabled for the guest's lifetime
10182 * won't have any additional overhead from PML when the guest is
10183 * running with dirty logging disabled.
Kai Huang88178fd2015-01-28 10:54:27 +080010184 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010185 * When enabling dirty logging, large sptes are write-protected
10186 * so they can be split on first write. New large sptes cannot
10187 * be created for this slot until the end of the logging.
Kai Huang88178fd2015-01-28 10:54:27 +080010188 * See the comments in fast_page_fault().
Anthony Yznaga37416792020-06-02 13:07:30 -070010189 * For small sptes, nothing is done if the dirty log is in the
10190 * initial-all-set state. Otherwise, depending on whether pml
10191 * is enabled the D-bit or the W-bit will be cleared.
Kai Huang88178fd2015-01-28 10:54:27 +080010192 */
10193 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010194 if (kvm_x86_ops.slot_enable_log_dirty) {
10195 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010196 } else {
10197 int level =
10198 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
Sean Christopherson3bae0452020-04-27 17:54:22 -070010199 PG_LEVEL_2M : PG_LEVEL_4K;
Jay Zhou3c9bd402020-02-27 09:32:27 +080010200
10201 /*
10202 * If we're with initial-all-set, we don't need
10203 * to write protect any small page because
10204 * they're reported as dirty already. However
10205 * we still need to write-protect huge pages
10206 * so that the page split can happen lazily on
10207 * the first write to the huge page.
10208 */
10209 kvm_mmu_slot_remove_write_access(kvm, new, level);
10210 }
Kai Huang88178fd2015-01-28 10:54:27 +080010211 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010212 if (kvm_x86_ops.slot_disable_log_dirty)
10213 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010214 }
10215}
10216
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010217void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010218 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010219 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010220 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010221 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010222{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010223 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010224 kvm_mmu_change_mmu_pages(kvm,
10225 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010226
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010227 /*
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010228 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010229 */
Anthony Yznaga37416792020-06-02 13:07:30 -070010230 kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change);
Sean Christopherson21198842020-02-18 13:07:25 -080010231
10232 /* Free the arrays associated with the old memslot. */
10233 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010234 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010235}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010236
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010237void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010238{
Sean Christopherson7390de12019-02-05 13:01:31 -080010239 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010240}
10241
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010242void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10243 struct kvm_memory_slot *slot)
10244{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010245 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010246}
10247
Liran Alone6c67d82018-09-04 10:56:52 +030010248static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10249{
10250 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010251 kvm_x86_ops.guest_apic_has_interrupt &&
10252 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010253}
10254
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010255static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10256{
10257 if (!list_empty_careful(&vcpu->async_pf.done))
10258 return true;
10259
10260 if (kvm_apic_has_events(vcpu))
10261 return true;
10262
10263 if (vcpu->arch.pv.pv_unhalted)
10264 return true;
10265
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010266 if (vcpu->arch.exception.pending)
10267 return true;
10268
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010269 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10270 (vcpu->arch.nmi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010271 kvm_x86_ops.nmi_allowed(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010272 return true;
10273
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010274 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040010275 (vcpu->arch.smi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010276 kvm_x86_ops.smi_allowed(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010277 return true;
10278
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010279 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010280 (kvm_cpu_has_interrupt(vcpu) ||
10281 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010282 return true;
10283
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010284 if (kvm_hv_has_stimer_pending(vcpu))
10285 return true;
10286
Sean Christophersond2060bd2020-04-22 19:25:39 -070010287 if (is_guest_mode(vcpu) &&
10288 kvm_x86_ops.nested_ops->hv_timer_pending &&
10289 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
10290 return true;
10291
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010292 return false;
10293}
10294
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010295int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10296{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010297 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010298}
Zhang Xiantao57361992007-12-17 14:21:40 +080010299
Wanpeng Li17e433b2019-08-05 10:03:19 +080010300bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10301{
10302 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10303 return true;
10304
10305 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10306 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10307 kvm_test_request(KVM_REQ_EVENT, vcpu))
10308 return true;
10309
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010310 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010311 return true;
10312
10313 return false;
10314}
10315
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010316bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10317{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010318 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010319}
10320
Christoffer Dallb6d33832012-03-08 16:44:24 -050010321int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010322{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010323 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010324}
Gleb Natapov78646122009-03-23 12:12:11 +020010325
10326int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10327{
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010328 return kvm_x86_ops.interrupt_allowed(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020010329}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010330
Nadav Amit82b32772014-11-02 11:54:45 +020010331unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10332{
10333 if (is_64_bit_mode(vcpu))
10334 return kvm_rip_read(vcpu);
10335 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10336 kvm_rip_read(vcpu));
10337}
10338EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10339
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010340bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10341{
Nadav Amit82b32772014-11-02 11:54:45 +020010342 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010343}
10344EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10345
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010346unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10347{
10348 unsigned long rflags;
10349
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010350 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010351 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010352 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010353 return rflags;
10354}
10355EXPORT_SYMBOL_GPL(kvm_get_rflags);
10356
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010357static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010358{
10359 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010360 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010361 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010362 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010363}
10364
10365void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10366{
10367 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010368 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010369}
10370EXPORT_SYMBOL_GPL(kvm_set_rflags);
10371
Gleb Natapov56028d02010-10-17 18:13:42 +020010372void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10373{
10374 int r;
10375
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010376 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010377 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010378 return;
10379
10380 r = kvm_mmu_reload(vcpu);
10381 if (unlikely(r))
10382 return;
10383
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010384 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010385 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010386 return;
10387
Sean Christopherson7a026742020-02-06 14:14:34 -080010388 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010389}
10390
Gleb Natapovaf585b92010-10-14 11:22:46 +020010391static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10392{
Peter Xudd03bca2020-04-16 11:58:59 -040010393 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
10394
Gleb Natapovaf585b92010-10-14 11:22:46 +020010395 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10396}
10397
10398static inline u32 kvm_async_pf_next_probe(u32 key)
10399{
Peter Xudd03bca2020-04-16 11:58:59 -040010400 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010401}
10402
10403static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10404{
10405 u32 key = kvm_async_pf_hash_fn(gfn);
10406
10407 while (vcpu->arch.apf.gfns[key] != ~0)
10408 key = kvm_async_pf_next_probe(key);
10409
10410 vcpu->arch.apf.gfns[key] = gfn;
10411}
10412
10413static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10414{
10415 int i;
10416 u32 key = kvm_async_pf_hash_fn(gfn);
10417
Peter Xudd03bca2020-04-16 11:58:59 -040010418 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010419 (vcpu->arch.apf.gfns[key] != gfn &&
10420 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010421 key = kvm_async_pf_next_probe(key);
10422
10423 return key;
10424}
10425
10426bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10427{
10428 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10429}
10430
10431static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10432{
10433 u32 i, j, k;
10434
10435 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040010436
10437 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
10438 return;
10439
Gleb Natapovaf585b92010-10-14 11:22:46 +020010440 while (true) {
10441 vcpu->arch.apf.gfns[i] = ~0;
10442 do {
10443 j = kvm_async_pf_next_probe(j);
10444 if (vcpu->arch.apf.gfns[j] == ~0)
10445 return;
10446 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10447 /*
10448 * k lies cyclically in ]i,j]
10449 * | i.k.j |
10450 * |....j i.k.| or |.k..j i...|
10451 */
10452 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10453 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10454 i = j;
10455 }
10456}
10457
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010458static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020010459{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010460 u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT;
10461
10462 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason,
10463 sizeof(reason));
10464}
10465
10466static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token)
10467{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010468 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010469
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010470 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
10471 &token, offset, sizeof(token));
10472}
10473
10474static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu)
10475{
10476 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
10477 u32 val;
10478
10479 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
10480 &val, offset, sizeof(val)))
10481 return false;
10482
10483 return !val;
Gleb Natapov7c907052010-10-14 11:22:53 +020010484}
10485
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010486static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10487{
10488 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10489 return false;
10490
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010491 if (!kvm_pv_async_pf_enabled(vcpu) ||
10492 (vcpu->arch.apf.send_user_only && kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010493 return false;
10494
10495 return true;
10496}
10497
10498bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10499{
10500 if (unlikely(!lapic_in_kernel(vcpu) ||
10501 kvm_event_needs_reinjection(vcpu) ||
10502 vcpu->arch.exception.pending))
10503 return false;
10504
10505 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10506 return false;
10507
10508 /*
10509 * If interrupts are off we cannot even use an artificial
10510 * halt state.
10511 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010512 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010513}
10514
Gleb Natapovaf585b92010-10-14 11:22:46 +020010515void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10516 struct kvm_async_pf *work)
10517{
Avi Kivity6389ee92010-11-29 16:12:30 +020010518 struct x86_exception fault;
10519
Sean Christopherson736c2912019-12-06 15:57:14 -080010520 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010521 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010522
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010523 if (kvm_can_deliver_async_pf(vcpu) &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010524 !apf_put_user_notpresent(vcpu)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010525 fault.vector = PF_VECTOR;
10526 fault.error_code_valid = true;
10527 fault.error_code = 0;
10528 fault.nested_page_fault = false;
10529 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010530 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010531 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010532 } else {
10533 /*
10534 * It is not possible to deliver a paravirtualized asynchronous
10535 * page fault, but putting the guest in an artificial halt state
10536 * can be beneficial nevertheless: if an interrupt arrives, we
10537 * can deliver it timely and perhaps the guest will schedule
10538 * another process. When the instruction that triggered a page
10539 * fault is retried, hopefully the page will be ready in the host.
10540 */
10541 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010542 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010543}
10544
10545void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10546 struct kvm_async_pf *work)
10547{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010548 struct kvm_lapic_irq irq = {
10549 .delivery_mode = APIC_DM_FIXED,
10550 .vector = vcpu->arch.apf.vec
10551 };
Avi Kivity6389ee92010-11-29 16:12:30 +020010552
chai wenf2e10662013-10-14 22:22:33 +080010553 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010554 work->arch.token = ~0; /* broadcast wakeup */
10555 else
10556 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010557 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010558
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010559 if (kvm_pv_async_pf_enabled(vcpu) &&
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020010560 !apf_put_user_ready(vcpu, work->arch.token)) {
10561 vcpu->arch.apf.pageready_pending = true;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010562 kvm_apic_set_irq(vcpu, &irq, NULL);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020010563 }
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010564
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010565 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010566 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010567}
10568
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020010569void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
10570{
10571 kvm_make_request(KVM_REQ_APF_READY, vcpu);
10572 if (!vcpu->arch.apf.pageready_pending)
10573 kvm_vcpu_kick(vcpu);
10574}
10575
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +020010576bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020010577{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010578 if (!kvm_pv_async_pf_enabled(vcpu))
Gleb Natapov7c907052010-10-14 11:22:53 +020010579 return true;
10580 else
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010581 return apf_pageready_slot_free(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010582}
10583
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010584void kvm_arch_start_assignment(struct kvm *kvm)
10585{
10586 atomic_inc(&kvm->arch.assigned_device_count);
10587}
10588EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10589
10590void kvm_arch_end_assignment(struct kvm *kvm)
10591{
10592 atomic_dec(&kvm->arch.assigned_device_count);
10593}
10594EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10595
10596bool kvm_arch_has_assigned_device(struct kvm *kvm)
10597{
10598 return atomic_read(&kvm->arch.assigned_device_count);
10599}
10600EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10601
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010602void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10603{
10604 atomic_inc(&kvm->arch.noncoherent_dma_count);
10605}
10606EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10607
10608void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10609{
10610 atomic_dec(&kvm->arch.noncoherent_dma_count);
10611}
10612EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10613
10614bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10615{
10616 return atomic_read(&kvm->arch.noncoherent_dma_count);
10617}
10618EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10619
Alex Williamson14717e22016-05-05 11:58:35 -060010620bool kvm_arch_has_irq_bypass(void)
10621{
Sean Christopherson92735b12019-08-02 15:06:17 -070010622 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010623}
10624
Feng Wu87276882015-09-18 22:29:40 +080010625int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10626 struct irq_bypass_producer *prod)
10627{
10628 struct kvm_kernel_irqfd *irqfd =
10629 container_of(cons, struct kvm_kernel_irqfd, consumer);
10630
Alex Williamson14717e22016-05-05 11:58:35 -060010631 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010632
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010633 return kvm_x86_ops.update_pi_irte(irqfd->kvm,
Alex Williamson14717e22016-05-05 11:58:35 -060010634 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010635}
10636
10637void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10638 struct irq_bypass_producer *prod)
10639{
10640 int ret;
10641 struct kvm_kernel_irqfd *irqfd =
10642 container_of(cons, struct kvm_kernel_irqfd, consumer);
10643
Feng Wu87276882015-09-18 22:29:40 +080010644 WARN_ON(irqfd->producer != prod);
10645 irqfd->producer = NULL;
10646
10647 /*
10648 * When producer of consumer is unregistered, we change back to
10649 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010650 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010651 * int this case doesn't want to receive the interrupts.
10652 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010653 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080010654 if (ret)
10655 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10656 " fails: %d\n", irqfd->consumer.token, ret);
10657}
10658
10659int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10660 uint32_t guest_irq, bool set)
10661{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010662 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080010663}
10664
Feng Wu520040142016-01-25 16:53:33 +080010665bool kvm_vector_hashing_enabled(void)
10666{
10667 return vector_hashing;
10668}
Feng Wu520040142016-01-25 16:53:33 +080010669
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010670bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10671{
10672 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10673}
10674EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10675
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010676u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10677{
10678 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10679
10680 /* The STIBP bit doesn't fault even if it's not advertised */
10681 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10682 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10683 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10684 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10685 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10686 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10687
10688 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10689 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10690 bits &= ~SPEC_CTRL_SSBD;
10691 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10692 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10693 bits &= ~SPEC_CTRL_SSBD;
10694
10695 return bits;
10696}
10697EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010698
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010699EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010700EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010701EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10702EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10703EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10704EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010705EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010706EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010707EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010708EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010709EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010710EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010711EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010712EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010713EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010714EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010715EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010716EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010717EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10718EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010719EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010720EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);