blob: 6d13e9d1bb7575243a5ba19950507fbe83946fc6 [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
Jan Kiszkac8639012012-09-21 05:42:55 +02001075static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1076{
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}
1088
Nadav Amit6f43ed02014-07-15 17:37:46 +03001089static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1090{
1091 u64 fixed = DR6_FIXED_1;
1092
Radim Krčmářd6321d42017-08-05 00:12:49 +02001093 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001094 fixed |= DR6_RTM;
1095 return fixed;
1096}
1097
Gleb Natapov338dbc92010-04-28 19:15:32 +03001098static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001099{
Marios Pomonisea740052019-12-11 12:47:52 -08001100 size_t size = ARRAY_SIZE(vcpu->arch.db);
1101
Gleb Natapov020df072010-04-13 10:05:23 +03001102 switch (dr) {
1103 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001104 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001105 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1106 vcpu->arch.eff_db[dr] = val;
1107 break;
1108 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001109 /* fall through */
1110 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001111 if (val & 0xffffffff00000000ULL)
1112 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001113 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001114 break;
1115 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001116 /* fall through */
1117 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001118 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001119 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001120 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001121 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001122 break;
1123 }
1124
1125 return 0;
1126}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001127
1128int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1129{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001130 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001131 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001132 return 1;
1133 }
1134 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001135}
Gleb Natapov020df072010-04-13 10:05:23 +03001136EXPORT_SYMBOL_GPL(kvm_set_dr);
1137
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001138int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001139{
Marios Pomonisea740052019-12-11 12:47:52 -08001140 size_t size = ARRAY_SIZE(vcpu->arch.db);
1141
Gleb Natapov020df072010-04-13 10:05:23 +03001142 switch (dr) {
1143 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001144 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001145 break;
1146 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001147 /* fall through */
1148 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001149 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001150 break;
1151 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001152 /* fall through */
1153 default: /* 7 */
1154 *val = vcpu->arch.dr7;
1155 break;
1156 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001157 return 0;
1158}
Gleb Natapov020df072010-04-13 10:05:23 +03001159EXPORT_SYMBOL_GPL(kvm_get_dr);
1160
Avi Kivity022cd0e2011-11-10 14:57:23 +02001161bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1162{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001163 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001164 u64 data;
1165 int err;
1166
Wei Huangc6702c92015-06-19 13:44:45 +02001167 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001168 if (err)
1169 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001170 kvm_rax_write(vcpu, (u32)data);
1171 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001172 return err;
1173}
1174EXPORT_SYMBOL_GPL(kvm_rdpmc);
1175
Carsten Otte043405e2007-10-10 17:16:19 +02001176/*
1177 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1178 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1179 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001180 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1181 * extract the supported MSRs from the related const lists.
1182 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001183 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001184 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001185 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001186 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001187
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001188static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001189 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001190 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001191#ifdef CONFIG_X86_64
1192 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1193#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001194 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001195 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001196 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001197 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1198 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1199 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1200 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1201 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1202 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001203 MSR_IA32_UMWAIT_CONTROL,
1204
Jim Mattsone2ada662019-08-21 11:20:04 -07001205 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1206 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1207 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1208 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1209 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1210 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1212 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1213 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1214 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1215 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1216 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1217 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001218 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1219 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1221 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1222 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1223 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1224 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1225 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1226 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001227};
1228
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001229static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001230static unsigned num_msrs_to_save;
1231
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001232static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001233 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1234 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1235 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1236 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001237 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001238 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1239 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001240 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001241 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001242 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001243 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001244 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001245 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001246 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1247 HV_X64_MSR_TSC_EMULATION_STATUS,
1248
1249 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001250 MSR_KVM_PV_EOI_EN,
1251
Will Auldba904632012-11-29 12:42:50 -08001252 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001253 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001254 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001255 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001256 MSR_IA32_MCG_STATUS,
1257 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001258 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001259 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001260 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001261 MSR_PLATFORM_INFO,
1262 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001263 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001264 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001265 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001266
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001267 /*
1268 * The following list leaves out MSRs whose values are determined
1269 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1270 * We always support the "true" VMX control MSRs, even if the host
1271 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001272 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001273 */
1274 MSR_IA32_VMX_BASIC,
1275 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1276 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1277 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1278 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1279 MSR_IA32_VMX_MISC,
1280 MSR_IA32_VMX_CR0_FIXED0,
1281 MSR_IA32_VMX_CR4_FIXED0,
1282 MSR_IA32_VMX_VMCS_ENUM,
1283 MSR_IA32_VMX_PROCBASED_CTLS2,
1284 MSR_IA32_VMX_EPT_VPID_CAP,
1285 MSR_IA32_VMX_VMFUNC,
1286
Borislav Petkov191c8132019-04-18 18:32:50 +02001287 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001288 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001289};
1290
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001291static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001292static unsigned num_emulated_msrs;
1293
Tom Lendacky801e4592018-02-21 13:39:51 -06001294/*
1295 * List of msr numbers which are used to expose MSR-based features that
1296 * can be used by a hypervisor to validate requested CPU features.
1297 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001298static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001299 MSR_IA32_VMX_BASIC,
1300 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1301 MSR_IA32_VMX_PINBASED_CTLS,
1302 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1303 MSR_IA32_VMX_PROCBASED_CTLS,
1304 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1305 MSR_IA32_VMX_EXIT_CTLS,
1306 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1307 MSR_IA32_VMX_ENTRY_CTLS,
1308 MSR_IA32_VMX_MISC,
1309 MSR_IA32_VMX_CR0_FIXED0,
1310 MSR_IA32_VMX_CR0_FIXED1,
1311 MSR_IA32_VMX_CR4_FIXED0,
1312 MSR_IA32_VMX_CR4_FIXED1,
1313 MSR_IA32_VMX_VMCS_ENUM,
1314 MSR_IA32_VMX_PROCBASED_CTLS2,
1315 MSR_IA32_VMX_EPT_VPID_CAP,
1316 MSR_IA32_VMX_VMFUNC,
1317
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001318 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001319 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001320 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001321};
1322
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001323static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001324static unsigned int num_msr_based_features;
1325
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001326static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001327{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001328 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001329
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001330 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1331 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001332
1333 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001334 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1335 * the nested hypervisor runs with NX huge pages. If it is not,
1336 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1337 * L1 guests, so it need not worry about its own (L2) guests.
1338 */
1339 data |= ARCH_CAP_PSCHANGE_MC_NO;
1340
1341 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001342 * If we're doing cache flushes (either "always" or "cond")
1343 * we will do one whenever the guest does a vmlaunch/vmresume.
1344 * If an outer hypervisor is doing the cache flush for us
1345 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1346 * capability to the guest too, and if EPT is disabled we're not
1347 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1348 * require a nested hypervisor to do a flush of its own.
1349 */
1350 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1351 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1352
Paolo Bonzini0c549142019-08-19 17:24:07 +02001353 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1354 data |= ARCH_CAP_RDCL_NO;
1355 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1356 data |= ARCH_CAP_SSB_NO;
1357 if (!boot_cpu_has_bug(X86_BUG_MDS))
1358 data |= ARCH_CAP_MDS_NO;
1359
Pawan Guptae1d38b62019-10-23 12:23:33 +02001360 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001361 * On TAA affected systems:
1362 * - nothing to do if TSX is disabled on the host.
1363 * - we emulate TSX_CTRL if present on the host.
1364 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001365 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001366 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001367 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001368 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1369 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001370
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001371 return data;
1372}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001373
Wanpeng Li66421c12018-02-28 14:03:30 +08001374static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1375{
1376 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001377 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001378 msr->data = kvm_get_arch_capabilities();
1379 break;
1380 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001381 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001382 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001383 default:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001384 if (kvm_x86_ops.get_msr_feature(msr))
Wanpeng Li66421c12018-02-28 14:03:30 +08001385 return 1;
1386 }
1387 return 0;
1388}
1389
Tom Lendacky801e4592018-02-21 13:39:51 -06001390static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1391{
1392 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001393 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001394
1395 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001396 r = kvm_get_msr_feature(&msr);
1397 if (r)
1398 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001399
1400 *data = msr.data;
1401
1402 return 0;
1403}
1404
Sean Christopherson11988492019-04-02 08:19:15 -07001405static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1406{
1407 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1408 return false;
1409
1410 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1411 return false;
1412
Sean Christopherson0a629562019-04-02 08:19:16 -07001413 if (efer & (EFER_LME | EFER_LMA) &&
1414 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1415 return false;
1416
1417 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1418 return false;
1419
Sean Christopherson11988492019-04-02 08:19:15 -07001420 return true;
1421
1422}
Jan Kiszka384bb782013-04-20 10:52:36 +02001423bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001424{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001425 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001426 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001427
Sean Christopherson11988492019-04-02 08:19:15 -07001428 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001429}
1430EXPORT_SYMBOL_GPL(kvm_valid_efer);
1431
Sean Christopherson11988492019-04-02 08:19:15 -07001432static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001433{
1434 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001435 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001436
Sean Christopherson11988492019-04-02 08:19:15 -07001437 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001438 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001439
Sean Christopherson11988492019-04-02 08:19:15 -07001440 if (!msr_info->host_initiated) {
1441 if (!__kvm_valid_efer(vcpu, efer))
1442 return 1;
1443
1444 if (is_paging(vcpu) &&
1445 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1446 return 1;
1447 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001448
Carsten Otte15c4a642007-10-30 18:44:17 +01001449 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001450 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001451
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001452 kvm_x86_ops.set_efer(vcpu, efer);
Sheng Yanga3d204e2010-05-12 16:40:40 +08001453
Sheng Yangaad82702010-05-12 16:40:42 +08001454 /* Update reserved bits */
1455 if ((efer ^ old_efer) & EFER_NX)
1456 kvm_mmu_reset_context(vcpu);
1457
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001458 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001459}
1460
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001461void kvm_enable_efer_bits(u64 mask)
1462{
1463 efer_reserved_bits &= ~mask;
1464}
1465EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1466
Carsten Otte15c4a642007-10-30 18:44:17 +01001467/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001468 * Write @data into the MSR specified by @index. Select MSR specific fault
1469 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001470 * Returns 0 on success, non-0 otherwise.
1471 * Assumes vcpu_load() was already called.
1472 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001473static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1474 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001475{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001476 struct msr_data msr;
1477
1478 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001479 case MSR_FS_BASE:
1480 case MSR_GS_BASE:
1481 case MSR_KERNEL_GS_BASE:
1482 case MSR_CSTAR:
1483 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001484 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001485 return 1;
1486 break;
1487 case MSR_IA32_SYSENTER_EIP:
1488 case MSR_IA32_SYSENTER_ESP:
1489 /*
1490 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1491 * non-canonical address is written on Intel but not on
1492 * AMD (which ignores the top 32-bits, because it does
1493 * not implement 64-bit SYSENTER).
1494 *
1495 * 64-bit code should hence be able to write a non-canonical
1496 * value on AMD. Making the address canonical ensures that
1497 * vmentry does not fail on Intel after writing a non-canonical
1498 * value, and that something deterministic happens if the guest
1499 * invokes 64-bit SYSENTER.
1500 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001501 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001502 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001503
1504 msr.data = data;
1505 msr.index = index;
1506 msr.host_initiated = host_initiated;
1507
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001508 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001509}
1510
1511/*
1512 * Read the MSR specified by @index into @data. Select MSR specific fault
1513 * checks are bypassed if @host_initiated is %true.
1514 * Returns 0 on success, non-0 otherwise.
1515 * Assumes vcpu_load() was already called.
1516 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001517int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1518 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001519{
1520 struct msr_data msr;
1521 int ret;
1522
1523 msr.index = index;
1524 msr.host_initiated = host_initiated;
1525
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001526 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001527 if (!ret)
1528 *data = msr.data;
1529 return ret;
1530}
1531
1532int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1533{
1534 return __kvm_get_msr(vcpu, index, data, false);
1535}
1536EXPORT_SYMBOL_GPL(kvm_get_msr);
1537
1538int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1539{
1540 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001541}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001542EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001543
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001544int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1545{
1546 u32 ecx = kvm_rcx_read(vcpu);
1547 u64 data;
1548
1549 if (kvm_get_msr(vcpu, ecx, &data)) {
1550 trace_kvm_msr_read_ex(ecx);
1551 kvm_inject_gp(vcpu, 0);
1552 return 1;
1553 }
1554
1555 trace_kvm_msr_read(ecx, data);
1556
1557 kvm_rax_write(vcpu, data & -1u);
1558 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1559 return kvm_skip_emulated_instruction(vcpu);
1560}
1561EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1562
1563int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1564{
1565 u32 ecx = kvm_rcx_read(vcpu);
1566 u64 data = kvm_read_edx_eax(vcpu);
1567
1568 if (kvm_set_msr(vcpu, ecx, data)) {
1569 trace_kvm_msr_write_ex(ecx, data);
1570 kvm_inject_gp(vcpu, 0);
1571 return 1;
1572 }
1573
1574 trace_kvm_msr_write(ecx, data);
1575 return kvm_skip_emulated_instruction(vcpu);
1576}
1577EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1578
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001579bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
1580{
1581 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
1582 need_resched() || signal_pending(current);
1583}
1584EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request);
1585
Carsten Otte313a3dc2007-10-11 19:16:52 +02001586/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001587 * The fast path for frequent and performance sensitive wrmsr emulation,
1588 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1589 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1590 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1591 * other cases which must be called after interrupts are enabled on the host.
1592 */
1593static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1594{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001595 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1596 return 1;
1597
1598 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001599 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001600 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1601 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001602
Wanpeng Lid5361672020-03-26 10:20:02 +08001603 data &= ~(1 << 12);
1604 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001605 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001606 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1607 trace_kvm_apic_write(APIC_ICR, (u32)data);
1608 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001609 }
1610
1611 return 1;
1612}
1613
Wanpeng Liae95f562020-04-28 14:23:28 +08001614static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1615{
1616 if (!kvm_can_use_hv_timer(vcpu))
1617 return 1;
1618
1619 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1620 return 0;
1621}
1622
Wanpeng Li404d5d72020-04-28 14:23:25 +08001623fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001624{
1625 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001626 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001627 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001628
1629 switch (msr) {
1630 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001631 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001632 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1633 kvm_skip_emulated_instruction(vcpu);
1634 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001635 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001636 break;
Wanpeng Liae95f562020-04-28 14:23:28 +08001637 case MSR_IA32_TSCDEADLINE:
1638 data = kvm_read_edx_eax(vcpu);
1639 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1640 kvm_skip_emulated_instruction(vcpu);
1641 ret = EXIT_FASTPATH_REENTER_GUEST;
1642 }
1643 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001644 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001645 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001646 }
1647
Wanpeng Li404d5d72020-04-28 14:23:25 +08001648 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001649 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001650
Wanpeng Li404d5d72020-04-28 14:23:25 +08001651 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001652}
1653EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1654
1655/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001656 * Adapt set_msr() to msr_io()'s calling convention
1657 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001658static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1659{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001660 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001661}
1662
Carsten Otte313a3dc2007-10-11 19:16:52 +02001663static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1664{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001665 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001666}
1667
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001668#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001669struct pvclock_clock {
1670 int vclock_mode;
1671 u64 cycle_last;
1672 u64 mask;
1673 u32 mult;
1674 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001675 u64 base_cycles;
1676 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001677};
1678
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001679struct pvclock_gtod_data {
1680 seqcount_t seq;
1681
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001682 struct pvclock_clock clock; /* extract of a clocksource struct */
1683 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001684
Paolo Bonzini917f9472020-01-22 14:32:20 +01001685 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001686 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001687};
1688
1689static struct pvclock_gtod_data pvclock_gtod_data;
1690
1691static void update_pvclock_gtod(struct timekeeper *tk)
1692{
1693 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1694
1695 write_seqcount_begin(&vdata->seq);
1696
1697 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001698 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001699 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1700 vdata->clock.mask = tk->tkr_mono.mask;
1701 vdata->clock.mult = tk->tkr_mono.mult;
1702 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001703 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1704 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001705
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001706 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001707 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1708 vdata->raw_clock.mask = tk->tkr_raw.mask;
1709 vdata->raw_clock.mult = tk->tkr_raw.mult;
1710 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001711 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1712 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001713
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001714 vdata->wall_time_sec = tk->xtime_sec;
1715
Paolo Bonzini917f9472020-01-22 14:32:20 +01001716 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001717
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001718 write_seqcount_end(&vdata->seq);
1719}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001720
1721static s64 get_kvmclock_base_ns(void)
1722{
1723 /* Count up from boot time, but with the frequency of the raw clock. */
1724 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1725}
1726#else
1727static s64 get_kvmclock_base_ns(void)
1728{
1729 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1730 return ktime_get_boottime_ns();
1731}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001732#endif
1733
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001734void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1735{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001736 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001737 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001738}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001739
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001740static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1741{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001742 int version;
1743 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001744 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001745 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001746
1747 if (!wall_clock)
1748 return;
1749
Avi Kivity9ed3c442010-05-04 15:00:37 +03001750 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1751 if (r)
1752 return;
1753
1754 if (version & 1)
1755 ++version; /* first time write, random junk */
1756
1757 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001758
Nicholas Krause1dab1342015-12-30 13:08:46 -05001759 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1760 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001761
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001762 /*
1763 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001764 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001765 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001766 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001767 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001768
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001769 wc.nsec = do_div(wall_nsec, 1000000000);
1770 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001771 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001772
1773 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1774
1775 version++;
1776 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001777}
1778
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001779static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1780{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001781 do_shl32_div32(dividend, divisor);
1782 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001783}
1784
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001785static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001786 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001787{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001788 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001789 int32_t shift = 0;
1790 uint64_t tps64;
1791 uint32_t tps32;
1792
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001793 tps64 = base_hz;
1794 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001795 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001796 tps64 >>= 1;
1797 shift--;
1798 }
1799
1800 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001801 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1802 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001803 scaled64 >>= 1;
1804 else
1805 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001806 shift++;
1807 }
1808
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001809 *pshift = shift;
1810 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001811}
1812
Marcelo Tosattid8281992012-11-27 23:29:01 -02001813#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001814static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001815#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001816
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001817static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001818static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001819
Zachary Amsdencc578282012-02-03 15:43:50 -02001820static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001821{
Zachary Amsdencc578282012-02-03 15:43:50 -02001822 u64 v = (u64)khz * (1000000 + ppm);
1823 do_div(v, 1000000);
1824 return v;
1825}
1826
Haozhong Zhang381d5852015-10-20 15:39:04 +08001827static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1828{
1829 u64 ratio;
1830
1831 /* Guest TSC same frequency as host TSC? */
1832 if (!scale) {
1833 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1834 return 0;
1835 }
1836
1837 /* TSC scaling supported? */
1838 if (!kvm_has_tsc_control) {
1839 if (user_tsc_khz > tsc_khz) {
1840 vcpu->arch.tsc_catchup = 1;
1841 vcpu->arch.tsc_always_catchup = 1;
1842 return 0;
1843 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001844 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001845 return -1;
1846 }
1847 }
1848
1849 /* TSC scaling required - calculate ratio */
1850 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1851 user_tsc_khz, tsc_khz);
1852
1853 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001854 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1855 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001856 return -1;
1857 }
1858
1859 vcpu->arch.tsc_scaling_ratio = ratio;
1860 return 0;
1861}
1862
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001863static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001864{
1865 u32 thresh_lo, thresh_hi;
1866 int use_scaling = 0;
1867
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001868 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001869 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001870 /* set tsc_scaling_ratio to a safe value */
1871 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001872 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001873 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001874
Zachary Amsdenc2855452010-09-18 14:38:15 -10001875 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001876 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001877 &vcpu->arch.virtual_tsc_shift,
1878 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001879 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001880
1881 /*
1882 * Compute the variation in TSC rate which is acceptable
1883 * within the range of tolerance and decide if the
1884 * rate being applied is within that bounds of the hardware
1885 * rate. If so, no scaling or compensation need be done.
1886 */
1887 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1888 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001889 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1890 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 -02001891 use_scaling = 1;
1892 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001893 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001894}
1895
1896static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1897{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001898 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001899 vcpu->arch.virtual_tsc_mult,
1900 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001901 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001902 return tsc;
1903}
1904
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001905static inline int gtod_is_based_on_tsc(int mode)
1906{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001907 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001908}
1909
Fengguang Wu69b00492015-01-19 22:33:39 +08001910static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001911{
1912#ifdef CONFIG_X86_64
1913 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001914 struct kvm_arch *ka = &vcpu->kvm->arch;
1915 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1916
1917 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1918 atomic_read(&vcpu->kvm->online_vcpus));
1919
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001920 /*
1921 * Once the masterclock is enabled, always perform request in
1922 * order to update it.
1923 *
1924 * In order to enable masterclock, the host clocksource must be TSC
1925 * and the vcpus need to have matched TSCs. When that happens,
1926 * perform request to enable masterclock.
1927 */
1928 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001929 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001930 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1931
1932 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1933 atomic_read(&vcpu->kvm->online_vcpus),
1934 ka->use_master_clock, gtod->clock.vclock_mode);
1935#endif
1936}
1937
Will Auldba904632012-11-29 12:42:50 -08001938static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1939{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001940 u64 curr_offset = vcpu->arch.l1_tsc_offset;
Will Auldba904632012-11-29 12:42:50 -08001941 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1942}
1943
Haozhong Zhang35181e82015-10-20 15:39:03 +08001944/*
1945 * Multiply tsc by a fixed point number represented by ratio.
1946 *
1947 * The most significant 64-N bits (mult) of ratio represent the
1948 * integral part of the fixed point number; the remaining N bits
1949 * (frac) represent the fractional part, ie. ratio represents a fixed
1950 * point number (mult + frac * 2^(-N)).
1951 *
1952 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1953 */
1954static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1955{
1956 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1957}
1958
1959u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1960{
1961 u64 _tsc = tsc;
1962 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1963
1964 if (ratio != kvm_default_tsc_scaling_ratio)
1965 _tsc = __scale_tsc(ratio, tsc);
1966
1967 return _tsc;
1968}
1969EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1970
Haozhong Zhang07c14192015-10-20 15:39:05 +08001971static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1972{
1973 u64 tsc;
1974
1975 tsc = kvm_scale_tsc(vcpu, rdtsc());
1976
1977 return target_tsc - tsc;
1978}
1979
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001980u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1981{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001982 return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001983}
1984EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1985
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001986static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1987{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07001988 vcpu->arch.l1_tsc_offset = offset;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001989 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001990}
1991
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001992static inline bool kvm_check_tsc_unstable(void)
1993{
1994#ifdef CONFIG_X86_64
1995 /*
1996 * TSC is marked unstable when we're running on Hyper-V,
1997 * 'TSC page' clocksource is good.
1998 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001999 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002000 return false;
2001#endif
2002 return check_tsc_unstable();
2003}
2004
Will Auld8fe8ab42012-11-29 12:42:12 -08002005void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002006{
2007 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002008 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002009 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002010 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002011 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08002012 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002013 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002014
Jan Kiszka038f8c12011-02-04 10:49:11 +01002015 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002016 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002017 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002018 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002019
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002020 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002021 if (data == 0 && msr->host_initiated) {
2022 /*
2023 * detection of vcpu initialization -- need to sync
2024 * with other vCPUs. This particularly helps to keep
2025 * kvm_clock stable after CPU hotplug
2026 */
2027 synchronizing = true;
2028 } else {
2029 u64 tsc_exp = kvm->arch.last_tsc_write +
2030 nsec_to_cycles(vcpu, elapsed);
2031 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2032 /*
2033 * Special case: TSC write with a small delta (1 second)
2034 * of virtual cycle time against real time is
2035 * interpreted as an attempt to synchronize the CPU.
2036 */
2037 synchronizing = data < tsc_exp + tsc_hz &&
2038 data + tsc_hz > tsc_exp;
2039 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002040 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002041
2042 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002043 * For a reliable TSC, we can match TSC offsets, and for an unstable
2044 * TSC, we add elapsed time in this computation. We could let the
2045 * compensation code attempt to catch up if we fall behind, but
2046 * it's better to try to match offsets from the beginning.
2047 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002048 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002049 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002050 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002051 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002052 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002053 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002054 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002055 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002056 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002057 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002058 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002059 } else {
2060 /*
2061 * We split periods of matched TSC writes into generations.
2062 * For each generation, we track the original measured
2063 * nanosecond time, offset, and write, so if TSCs are in
2064 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002065 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002066 *
2067 * These values are tracked in kvm->arch.cur_xxx variables.
2068 */
2069 kvm->arch.cur_tsc_generation++;
2070 kvm->arch.cur_tsc_nsec = ns;
2071 kvm->arch.cur_tsc_write = data;
2072 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002073 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002074 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002075
2076 /*
2077 * We also track th most recent recorded KHZ, write and time to
2078 * allow the matching interval to be extended at each write.
2079 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002080 kvm->arch.last_tsc_nsec = ns;
2081 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002082 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002083
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002084 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002085
2086 /* Keep track of which generation this VCPU has synchronized to */
2087 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2088 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2089 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2090
Radim Krčmářd6321d42017-08-05 00:12:49 +02002091 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002092 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002093
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002094 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002095 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002096
2097 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002098 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002099 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002100 } else if (!already_matched) {
2101 kvm->arch.nr_vcpus_matched_tsc++;
2102 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002103
2104 kvm_track_tsc_matching(vcpu);
2105 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002106}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002107
Zachary Amsden99e3e302010-08-19 22:07:17 -10002108EXPORT_SYMBOL_GPL(kvm_write_tsc);
2109
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002110static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2111 s64 adjustment)
2112{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002113 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002114 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002115}
2116
2117static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2118{
2119 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2120 WARN_ON(adjustment < 0);
2121 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002122 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002123}
2124
Marcelo Tosattid8281992012-11-27 23:29:01 -02002125#ifdef CONFIG_X86_64
2126
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002127static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002128{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002129 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002130 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002131
2132 if (likely(ret >= last))
2133 return ret;
2134
2135 /*
2136 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002137 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002138 * very likely) and there's a data dependence, so force GCC
2139 * to generate a branch instead. I don't barrier() because
2140 * we don't actually need a barrier, and if this function
2141 * ever gets inlined it will generate worse code.
2142 */
2143 asm volatile ("");
2144 return last;
2145}
2146
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002147static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2148 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002149{
2150 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002151 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002152
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002153 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002154 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002155 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2156 tsc_timestamp);
2157 if (tsc_pg_val != U64_MAX) {
2158 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002159 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002160 v = (tsc_pg_val - clock->cycle_last) &
2161 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002162 } else {
2163 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002164 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002165 }
2166 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002167 case VDSO_CLOCKMODE_TSC:
2168 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002169 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002170 v = (*tsc_timestamp - clock->cycle_last) &
2171 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002172 break;
2173 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002174 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002175 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002176
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002177 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002178 *tsc_timestamp = v = 0;
2179
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002180 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002181}
2182
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002183static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002184{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002185 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002186 unsigned long seq;
2187 int mode;
2188 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002189
Marcelo Tosattid8281992012-11-27 23:29:01 -02002190 do {
2191 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002192 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002193 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002194 ns >>= gtod->raw_clock.shift;
2195 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002196 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002197 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002198
2199 return mode;
2200}
2201
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002202static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002203{
2204 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2205 unsigned long seq;
2206 int mode;
2207 u64 ns;
2208
2209 do {
2210 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002211 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002212 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002213 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002214 ns >>= gtod->clock.shift;
2215 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2216
2217 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2218 ts->tv_nsec = ns;
2219
2220 return mode;
2221}
2222
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002223/* returns true if host is using TSC based clocksource */
2224static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002225{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002226 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002227 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002228 return false;
2229
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002230 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002231 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002232}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002233
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002234/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002235static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002236 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002237{
2238 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002239 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002240 return false;
2241
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002242 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002243}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002244#endif
2245
2246/*
2247 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002248 * Assuming a stable TSC across physical CPUS, and a stable TSC
2249 * across virtual CPUs, the following condition is possible.
2250 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002251 * CPUs at the next numbered event.
2252 *
2253 * "timespecX" represents host monotonic time. "tscX" represents
2254 * RDTSC value.
2255 *
2256 * VCPU0 on CPU0 | VCPU1 on CPU1
2257 *
2258 * 1. read timespec0,tsc0
2259 * 2. | timespec1 = timespec0 + N
2260 * | tsc1 = tsc0 + M
2261 * 3. transition to guest | transition to guest
2262 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2263 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2264 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2265 *
2266 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2267 *
2268 * - ret0 < ret1
2269 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2270 * ...
2271 * - 0 < N - M => M < N
2272 *
2273 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2274 * always the case (the difference between two distinct xtime instances
2275 * might be smaller then the difference between corresponding TSC reads,
2276 * when updating guest vcpus pvclock areas).
2277 *
2278 * To avoid that problem, do not allow visibility of distinct
2279 * system_timestamp/tsc_timestamp values simultaneously: use a master
2280 * copy of host monotonic time values. Update that master copy
2281 * in lockstep.
2282 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002283 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002284 *
2285 */
2286
2287static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2288{
2289#ifdef CONFIG_X86_64
2290 struct kvm_arch *ka = &kvm->arch;
2291 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002292 bool host_tsc_clocksource, vcpus_matched;
2293
2294 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2295 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002296
2297 /*
2298 * If the host uses TSC clock, then passthrough TSC as stable
2299 * to the guest.
2300 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002301 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002302 &ka->master_kernel_ns,
2303 &ka->master_cycle_now);
2304
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002305 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002306 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002307 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002308
Marcelo Tosattid8281992012-11-27 23:29:01 -02002309 if (ka->use_master_clock)
2310 atomic_set(&kvm_guest_has_master_clock, 1);
2311
2312 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002313 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2314 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002315#endif
2316}
2317
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002318void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2319{
2320 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2321}
2322
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002323static void kvm_gen_update_masterclock(struct kvm *kvm)
2324{
2325#ifdef CONFIG_X86_64
2326 int i;
2327 struct kvm_vcpu *vcpu;
2328 struct kvm_arch *ka = &kvm->arch;
2329
2330 spin_lock(&ka->pvclock_gtod_sync_lock);
2331 kvm_make_mclock_inprogress_request(kvm);
2332 /* no guest entries from this point */
2333 pvclock_update_vm_gtod_copy(kvm);
2334
2335 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002336 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002337
2338 /* guest entries allowed */
2339 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002340 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002341
2342 spin_unlock(&ka->pvclock_gtod_sync_lock);
2343#endif
2344}
2345
Marcelo Tosattie891a322017-04-17 12:51:37 -03002346u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002347{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002348 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002349 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002350 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002351
Paolo Bonzini8b953442016-11-16 18:31:30 +01002352 spin_lock(&ka->pvclock_gtod_sync_lock);
2353 if (!ka->use_master_clock) {
2354 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002355 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002356 }
2357
Paolo Bonzini8b953442016-11-16 18:31:30 +01002358 hv_clock.tsc_timestamp = ka->master_cycle_now;
2359 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2360 spin_unlock(&ka->pvclock_gtod_sync_lock);
2361
Wanpeng Lie2c22062017-05-11 18:12:05 -07002362 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2363 get_cpu();
2364
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002365 if (__this_cpu_read(cpu_tsc_khz)) {
2366 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2367 &hv_clock.tsc_shift,
2368 &hv_clock.tsc_to_system_mul);
2369 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2370 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002371 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002372
2373 put_cpu();
2374
2375 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002376}
2377
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002378static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2379{
2380 struct kvm_vcpu_arch *vcpu = &v->arch;
2381 struct pvclock_vcpu_time_info guest_hv_clock;
2382
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002383 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002384 &guest_hv_clock, sizeof(guest_hv_clock))))
2385 return;
2386
2387 /* This VCPU is paused, but it's legal for a guest to read another
2388 * VCPU's kvmclock, so we really have to follow the specification where
2389 * it says that version is odd if data is being modified, and even after
2390 * it is consistent.
2391 *
2392 * Version field updates must be kept separate. This is because
2393 * kvm_write_guest_cached might use a "rep movs" instruction, and
2394 * writes within a string instruction are weakly ordered. So there
2395 * are three writes overall.
2396 *
2397 * As a small optimization, only write the version field in the first
2398 * and third write. The vcpu->pv_time cache is still valid, because the
2399 * version field is the first in the struct.
2400 */
2401 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2402
Liran Alon51c4b8b2017-11-05 16:11:30 +02002403 if (guest_hv_clock.version & 1)
2404 ++guest_hv_clock.version; /* first time write, random junk */
2405
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002406 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002407 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2408 &vcpu->hv_clock,
2409 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002410
2411 smp_wmb();
2412
2413 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2414 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2415
2416 if (vcpu->pvclock_set_guest_stopped_request) {
2417 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2418 vcpu->pvclock_set_guest_stopped_request = false;
2419 }
2420
2421 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2422
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002423 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2424 &vcpu->hv_clock,
2425 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002426
2427 smp_wmb();
2428
2429 vcpu->hv_clock.version++;
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.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002433}
2434
Zachary Amsden34c238a2010-09-18 14:38:14 -10002435static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002436{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002437 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002438 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002439 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002440 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002441 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002442 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002443 bool use_master_clock;
2444
2445 kernel_ns = 0;
2446 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002447
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002448 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002449 * If the host uses TSC clock, then passthrough TSC as stable
2450 * to the guest.
2451 */
2452 spin_lock(&ka->pvclock_gtod_sync_lock);
2453 use_master_clock = ka->use_master_clock;
2454 if (use_master_clock) {
2455 host_tsc = ka->master_cycle_now;
2456 kernel_ns = ka->master_kernel_ns;
2457 }
2458 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002459
2460 /* Keep irq disabled to prevent changes to the clock */
2461 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002462 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2463 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002464 local_irq_restore(flags);
2465 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2466 return 1;
2467 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002468 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002469 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002470 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002471 }
2472
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002473 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002474
2475 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002476 * We may have to catch up the TSC to match elapsed wall clock
2477 * time for two reasons, even if kvmclock is used.
2478 * 1) CPU could have been running below the maximum TSC rate
2479 * 2) Broken TSC compensation resets the base at each VCPU
2480 * entry to avoid unknown leaps of TSC even when running
2481 * again on the same CPU. This may cause apparent elapsed
2482 * time to disappear, and the guest to stand still or run
2483 * very slowly.
2484 */
2485 if (vcpu->tsc_catchup) {
2486 u64 tsc = compute_guest_tsc(v, kernel_ns);
2487 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002488 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002489 tsc_timestamp = tsc;
2490 }
2491 }
2492
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002493 local_irq_restore(flags);
2494
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002495 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002496
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002497 if (kvm_has_tsc_control)
2498 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2499
2500 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002501 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002502 &vcpu->hv_clock.tsc_shift,
2503 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002504 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002505 }
2506
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002507 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002508 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002509 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002510
Marcelo Tosattid8281992012-11-27 23:29:01 -02002511 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002512 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002513 if (use_master_clock)
2514 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2515
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002516 vcpu->hv_clock.flags = pvclock_flags;
2517
Paolo Bonzini095cf552016-02-08 12:54:12 +01002518 if (vcpu->pv_time_enabled)
2519 kvm_setup_pvclock_page(v);
2520 if (v == kvm_get_vcpu(v->kvm, 0))
2521 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002522 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002523}
2524
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002525/*
2526 * kvmclock updates which are isolated to a given vcpu, such as
2527 * vcpu->cpu migration, should not allow system_timestamp from
2528 * the rest of the vcpus to remain static. Otherwise ntp frequency
2529 * correction applies to one vcpu's system_timestamp but not
2530 * the others.
2531 *
2532 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002533 * We need to rate-limit these requests though, as they can
2534 * considerably slow guests that have a large number of vcpus.
2535 * The time for a remote vcpu to update its kvmclock is bound
2536 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002537 */
2538
Andrew Jones7e44e442014-02-28 12:52:54 +01002539#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2540
2541static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002542{
2543 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002544 struct delayed_work *dwork = to_delayed_work(work);
2545 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2546 kvmclock_update_work);
2547 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002548 struct kvm_vcpu *vcpu;
2549
2550 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002551 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002552 kvm_vcpu_kick(vcpu);
2553 }
2554}
2555
Andrew Jones7e44e442014-02-28 12:52:54 +01002556static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2557{
2558 struct kvm *kvm = v->kvm;
2559
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002560 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002561 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2562 KVMCLOCK_UPDATE_DELAY);
2563}
2564
Andrew Jones332967a2014-02-28 12:52:55 +01002565#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2566
2567static void kvmclock_sync_fn(struct work_struct *work)
2568{
2569 struct delayed_work *dwork = to_delayed_work(work);
2570 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2571 kvmclock_sync_work);
2572 struct kvm *kvm = container_of(ka, struct kvm, arch);
2573
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002574 if (!kvmclock_periodic_sync)
2575 return;
2576
Andrew Jones332967a2014-02-28 12:52:55 +01002577 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2578 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2579 KVMCLOCK_SYNC_PERIOD);
2580}
2581
Borislav Petkov191c8132019-04-18 18:32:50 +02002582/*
2583 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2584 */
2585static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2586{
2587 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002588 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002589 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2590
2591 return false;
2592}
2593
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002594static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002595{
2596 u64 mcg_cap = vcpu->arch.mcg_cap;
2597 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002598 u32 msr = msr_info->index;
2599 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002600
2601 switch (msr) {
2602 case MSR_IA32_MCG_STATUS:
2603 vcpu->arch.mcg_status = data;
2604 break;
2605 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002606 if (!(mcg_cap & MCG_CTL_P) &&
2607 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002608 return 1;
2609 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002610 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002611 vcpu->arch.mcg_ctl = data;
2612 break;
2613 default:
2614 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002615 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002616 u32 offset = array_index_nospec(
2617 msr - MSR_IA32_MC0_CTL,
2618 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2619
Andre Przywara114be422010-03-24 17:46:42 +01002620 /* only 0 or all 1s can be written to IA32_MCi_CTL
2621 * some Linux kernels though clear bit 10 in bank 4 to
2622 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2623 * this to avoid an uncatched #GP in the guest
2624 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002625 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002626 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002627 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002628
2629 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002630 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002631 (offset & 0x3) == 1 && data != 0) {
2632 if (!can_set_mci_status(vcpu))
2633 return -1;
2634 }
2635
Huang Ying890ca9a2009-05-11 16:48:15 +08002636 vcpu->arch.mce_banks[offset] = data;
2637 break;
2638 }
2639 return 1;
2640 }
2641 return 0;
2642}
2643
Ed Swierkffde22a2009-10-15 15:21:43 -07002644static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2645{
2646 struct kvm *kvm = vcpu->kvm;
2647 int lm = is_long_mode(vcpu);
2648 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2649 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2650 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2651 : kvm->arch.xen_hvm_config.blob_size_32;
2652 u32 page_num = data & ~PAGE_MASK;
2653 u64 page_addr = data & PAGE_MASK;
2654 u8 *page;
2655 int r;
2656
2657 r = -E2BIG;
2658 if (page_num >= blob_size)
2659 goto out;
2660 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002661 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2662 if (IS_ERR(page)) {
2663 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002664 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002665 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002666 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002667 goto out_free;
2668 r = 0;
2669out_free:
2670 kfree(page);
2671out:
2672 return r;
2673}
2674
Gleb Natapov344d9582010-10-14 11:22:50 +02002675static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2676{
2677 gpa_t gpa = data & ~0x3f;
2678
Wanpeng Li52a5c152017-07-13 18:30:42 -07002679 /* Bits 3:5 are reserved, Should be zero */
2680 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002681 return 1;
2682
2683 vcpu->arch.apf.msr_val = data;
2684
2685 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2686 kvm_clear_async_pf_completion_queue(vcpu);
2687 kvm_async_pf_hash_reset(vcpu);
2688 return 0;
2689 }
2690
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002691 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002692 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002693 return 1;
2694
Gleb Natapov6adba522010-10-14 11:22:55 +02002695 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002696 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002697 kvm_async_pf_wakeup_all(vcpu);
2698 return 0;
2699}
2700
Glauber Costa12f9a482011-02-01 14:16:40 -05002701static void kvmclock_reset(struct kvm_vcpu *vcpu)
2702{
Andy Honig0b794592013-02-20 14:48:10 -08002703 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002704 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002705}
2706
Sean Christopherson77809382020-03-20 14:28:18 -07002707static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002708{
2709 ++vcpu->stat.tlb_flush;
Sean Christopherson77809382020-03-20 14:28:18 -07002710 kvm_x86_ops.tlb_flush_all(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002711}
2712
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002713static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
2714{
2715 ++vcpu->stat.tlb_flush;
2716 kvm_x86_ops.tlb_flush_guest(vcpu);
2717}
2718
Glauber Costac9aaa892011-07-11 15:28:14 -04002719static void record_steal_time(struct kvm_vcpu *vcpu)
2720{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002721 struct kvm_host_map map;
2722 struct kvm_steal_time *st;
2723
Glauber Costac9aaa892011-07-11 15:28:14 -04002724 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2725 return;
2726
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002727 /* -EAGAIN is returned in atomic context so we can just return. */
2728 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2729 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002730 return;
2731
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002732 st = map.hva +
2733 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2734
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002735 /*
2736 * Doing a TLB flush here, on the guest's behalf, can avoid
2737 * expensive IPIs.
2738 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002739 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002740 st->preempted & KVM_VCPU_FLUSH_TLB);
2741 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002742 kvm_vcpu_flush_tlb_guest(vcpu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002743
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002744 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002745
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002746 if (st->version & 1)
2747 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002748
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002749 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002750
2751 smp_wmb();
2752
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002753 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002754 vcpu->arch.st.last_steal;
2755 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002756
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002757 smp_wmb();
2758
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002759 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002760
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002761 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002762}
2763
Will Auld8fe8ab42012-11-29 12:42:12 -08002764int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002765{
Gleb Natapov57537852012-01-15 14:17:22 +02002766 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002767 u32 msr = msr_info->index;
2768 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002769
Carsten Otte15c4a642007-10-30 18:44:17 +01002770 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002771 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002772 case MSR_IA32_UCODE_WRITE:
2773 case MSR_VM_HSAVE_PA:
2774 case MSR_AMD64_PATCH_LOADER:
2775 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002776 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002777 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002778 break;
2779
Wanpeng Li518e7b92018-02-28 14:03:31 +08002780 case MSR_IA32_UCODE_REV:
2781 if (msr_info->host_initiated)
2782 vcpu->arch.microcode_version = data;
2783 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002784 case MSR_IA32_ARCH_CAPABILITIES:
2785 if (!msr_info->host_initiated)
2786 return 1;
2787 vcpu->arch.arch_capabilities = data;
2788 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002789 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002790 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002791 case MSR_K7_HWCR:
2792 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002793 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002794 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002795
2796 /* Handle McStatusWrEn */
2797 if (data == BIT_ULL(18)) {
2798 vcpu->arch.msr_hwcr = data;
2799 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002800 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2801 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002802 return 1;
2803 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002804 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002805 case MSR_FAM10H_MMIO_CONF_BASE:
2806 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002807 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2808 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002809 return 1;
2810 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002811 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002812 case MSR_IA32_DEBUGCTLMSR:
2813 if (!data) {
2814 /* We support the non-activated case already */
2815 break;
2816 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2817 /* Values other than LBR and BTF are vendor-specific,
2818 thus reserved and should throw a #GP */
2819 return 1;
2820 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002821 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2822 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002823 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002824 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002825 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002826 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002827 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002828 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2829 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002830 case MSR_IA32_TSCDEADLINE:
2831 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2832 break;
Will Auldba904632012-11-29 12:42:50 -08002833 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002834 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002835 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002836 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002837 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002838 }
2839 vcpu->arch.ia32_tsc_adjust_msr = data;
2840 }
2841 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002842 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002843 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2844 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2845 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2846 return 1;
2847 vcpu->arch.ia32_misc_enable_msr = data;
2848 kvm_update_cpuid(vcpu);
2849 } else {
2850 vcpu->arch.ia32_misc_enable_msr = data;
2851 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002852 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002853 case MSR_IA32_SMBASE:
2854 if (!msr_info->host_initiated)
2855 return 1;
2856 vcpu->arch.smbase = data;
2857 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002858 case MSR_IA32_POWER_CTL:
2859 vcpu->arch.msr_ia32_power_ctl = data;
2860 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002861 case MSR_IA32_TSC:
2862 kvm_write_tsc(vcpu, msr_info);
2863 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002864 case MSR_IA32_XSS:
2865 if (!msr_info->host_initiated &&
2866 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2867 return 1;
2868 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002869 * KVM supports exposing PT to the guest, but does not support
2870 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2871 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002872 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002873 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002874 return 1;
2875 vcpu->arch.ia32_xss = data;
2876 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002877 case MSR_SMI_COUNT:
2878 if (!msr_info->host_initiated)
2879 return 1;
2880 vcpu->arch.smi_count = data;
2881 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002882 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002883 case MSR_KVM_WALL_CLOCK:
2884 vcpu->kvm->arch.wall_clock = data;
2885 kvm_write_wall_clock(vcpu->kvm, data);
2886 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002887 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002888 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002889 struct kvm_arch *ka = &vcpu->kvm->arch;
2890
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002891 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2892 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2893
2894 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002895 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002896
2897 ka->boot_vcpu_runs_old_kvmclock = tmp;
2898 }
2899
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002900 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002901 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002902
2903 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002904 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002905 if (!(data & 1))
2906 break;
2907
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002908 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002909 &vcpu->arch.pv_time, data & ~1ULL,
2910 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002911 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002912
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002913 break;
2914 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002915 case MSR_KVM_ASYNC_PF_EN:
2916 if (kvm_pv_enable_async_pf(vcpu, data))
2917 return 1;
2918 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002919 case MSR_KVM_STEAL_TIME:
2920
2921 if (unlikely(!sched_info_on()))
2922 return 1;
2923
2924 if (data & KVM_STEAL_RESERVED_MASK)
2925 return 1;
2926
Glauber Costac9aaa892011-07-11 15:28:14 -04002927 vcpu->arch.st.msr_val = data;
2928
2929 if (!(data & KVM_MSR_ENABLED))
2930 break;
2931
Glauber Costac9aaa892011-07-11 15:28:14 -04002932 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2933
2934 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002935 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002936 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002937 return 1;
2938 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002939
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002940 case MSR_KVM_POLL_CONTROL:
2941 /* only enable bit supported */
2942 if (data & (-1ULL << 1))
2943 return 1;
2944
2945 vcpu->arch.msr_kvm_poll_control = data;
2946 break;
2947
Huang Ying890ca9a2009-05-11 16:48:15 +08002948 case MSR_IA32_MCG_CTL:
2949 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002950 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002951 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002952
Wei Huang6912ac32015-06-12 01:34:56 -04002953 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2954 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2955 pr = true; /* fall through */
2956 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2957 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002958 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002959 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002960
2961 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002962 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2963 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002964 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002965 case MSR_K7_CLK_CTL:
2966 /*
2967 * Ignore all writes to this no longer documented MSR.
2968 * Writes are only relevant for old K7 processors,
2969 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002970 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002971 * affected processor models on the command line, hence
2972 * the need to ignore the workaround.
2973 */
2974 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002975 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002976 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2977 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002978 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002979 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2980 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2981 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002982 return kvm_hv_set_msr_common(vcpu, msr, data,
2983 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002984 case MSR_IA32_BBL_CR_CTL3:
2985 /* Drop writes to this legacy MSR -- see rdmsr
2986 * counterpart for further detail.
2987 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002988 if (report_ignored_msrs)
2989 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2990 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002991 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002992 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002993 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002994 return 1;
2995 vcpu->arch.osvw.length = data;
2996 break;
2997 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002998 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002999 return 1;
3000 vcpu->arch.osvw.status = data;
3001 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003002 case MSR_PLATFORM_INFO:
3003 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003004 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3005 cpuid_fault_enabled(vcpu)))
3006 return 1;
3007 vcpu->arch.msr_platform_info = data;
3008 break;
3009 case MSR_MISC_FEATURES_ENABLES:
3010 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3011 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3012 !supports_cpuid_fault(vcpu)))
3013 return 1;
3014 vcpu->arch.msr_misc_features_enables = data;
3015 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003016 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07003017 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
3018 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02003019 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003020 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02003021 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003022 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03003023 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003024 return 1;
3025 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003026 if (report_ignored_msrs)
3027 vcpu_unimpl(vcpu,
3028 "ignored wrmsr: 0x%x data 0x%llx\n",
3029 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003030 break;
3031 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003032 }
3033 return 0;
3034}
3035EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3036
Paolo Bonzini44883f02018-07-26 13:01:52 +02003037static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003038{
3039 u64 data;
3040 u64 mcg_cap = vcpu->arch.mcg_cap;
3041 unsigned bank_num = mcg_cap & 0xff;
3042
3043 switch (msr) {
3044 case MSR_IA32_P5_MC_ADDR:
3045 case MSR_IA32_P5_MC_TYPE:
3046 data = 0;
3047 break;
3048 case MSR_IA32_MCG_CAP:
3049 data = vcpu->arch.mcg_cap;
3050 break;
3051 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003052 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003053 return 1;
3054 data = vcpu->arch.mcg_ctl;
3055 break;
3056 case MSR_IA32_MCG_STATUS:
3057 data = vcpu->arch.mcg_status;
3058 break;
3059 default:
3060 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003061 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003062 u32 offset = array_index_nospec(
3063 msr - MSR_IA32_MC0_CTL,
3064 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3065
Huang Ying890ca9a2009-05-11 16:48:15 +08003066 data = vcpu->arch.mce_banks[offset];
3067 break;
3068 }
3069 return 1;
3070 }
3071 *pdata = data;
3072 return 0;
3073}
3074
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003075int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003076{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003077 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003078 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003079 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003080 case MSR_IA32_DEBUGCTLMSR:
3081 case MSR_IA32_LASTBRANCHFROMIP:
3082 case MSR_IA32_LASTBRANCHTOIP:
3083 case MSR_IA32_LASTINTFROMIP:
3084 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303085 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003086 case MSR_K8_TSEG_ADDR:
3087 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003088 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003089 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003090 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003091 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003092 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003093 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003094 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003095 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003096 /*
3097 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3098 * limit) MSRs. Just return 0, as we do not want to expose the host
3099 * data here. Do not conditionalize this on CPUID, as KVM does not do
3100 * so for existing CPU-specific MSRs.
3101 */
3102 case MSR_RAPL_POWER_UNIT:
3103 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3104 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3105 case MSR_PKG_ENERGY_STATUS: /* Total package */
3106 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003107 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003108 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003109 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003110 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3111 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3112 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3113 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003114 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003115 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3116 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003117 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003118 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003119 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003120 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003121 case MSR_IA32_ARCH_CAPABILITIES:
3122 if (!msr_info->host_initiated &&
3123 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3124 return 1;
3125 msr_info->data = vcpu->arch.arch_capabilities;
3126 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003127 case MSR_IA32_POWER_CTL:
3128 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3129 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003130 case MSR_IA32_TSC:
3131 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3132 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003133 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003134 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003135 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003136 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003137 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003138 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003139 /*
3140 * MSR_EBC_FREQUENCY_ID
3141 * Conservative value valid for even the basic CPU models.
3142 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3143 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3144 * and 266MHz for model 3, or 4. Set Core Clock
3145 * Frequency to System Bus Frequency Ratio to 1 (bits
3146 * 31:24) even though these are only valid for CPU
3147 * models > 2, however guests may end up dividing or
3148 * multiplying by zero otherwise.
3149 */
3150 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003151 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003152 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003153 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003154 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003155 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003156 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003157 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003158 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003159 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003160 break;
Will Auldba904632012-11-29 12:42:50 -08003161 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003162 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003163 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003164 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003165 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003166 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003167 case MSR_IA32_SMBASE:
3168 if (!msr_info->host_initiated)
3169 return 1;
3170 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003171 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003172 case MSR_SMI_COUNT:
3173 msr_info->data = vcpu->arch.smi_count;
3174 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003175 case MSR_IA32_PERF_STATUS:
3176 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003177 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003178 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003179 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003180 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003181 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003182 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003183 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003184 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003185 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003186 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003187 break;
3188 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003189 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003190 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003191 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003192 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003193 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003194 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003195 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003196 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003197 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003198 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003199 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003200 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003201 case MSR_KVM_POLL_CONTROL:
3202 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3203 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003204 case MSR_IA32_P5_MC_ADDR:
3205 case MSR_IA32_P5_MC_TYPE:
3206 case MSR_IA32_MCG_CAP:
3207 case MSR_IA32_MCG_CTL:
3208 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003209 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003210 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3211 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003212 case MSR_IA32_XSS:
3213 if (!msr_info->host_initiated &&
3214 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3215 return 1;
3216 msr_info->data = vcpu->arch.ia32_xss;
3217 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003218 case MSR_K7_CLK_CTL:
3219 /*
3220 * Provide expected ramp-up count for K7. All other
3221 * are set to zero, indicating minimum divisors for
3222 * every field.
3223 *
3224 * This prevents guest kernels on AMD host with CPU
3225 * type 6, model 8 and higher from exploding due to
3226 * the rdmsr failing.
3227 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003228 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003229 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003230 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003231 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3232 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003233 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003234 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3235 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3236 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003237 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003238 msr_info->index, &msr_info->data,
3239 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003240 case MSR_IA32_BBL_CR_CTL3:
3241 /* This legacy MSR exists but isn't fully documented in current
3242 * silicon. It is however accessed by winxp in very narrow
3243 * scenarios where it sets bit #19, itself documented as
3244 * a "reserved" bit. Best effort attempt to source coherent
3245 * read data here should the balance of the register be
3246 * interpreted by the guest:
3247 *
3248 * L2 cache control register 3: 64GB range, 256KB size,
3249 * enabled, latency 0x1, configured
3250 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003251 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003252 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003253 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003254 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003255 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003256 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003257 break;
3258 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003259 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003260 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003261 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003262 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003263 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003264 if (!msr_info->host_initiated &&
3265 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3266 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003267 msr_info->data = vcpu->arch.msr_platform_info;
3268 break;
3269 case MSR_MISC_FEATURES_ENABLES:
3270 msr_info->data = vcpu->arch.msr_misc_features_enables;
3271 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003272 case MSR_K7_HWCR:
3273 msr_info->data = vcpu->arch.msr_hwcr;
3274 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003275 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003276 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003277 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003278 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003279 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3280 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003281 return 1;
3282 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003283 if (report_ignored_msrs)
3284 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3285 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003286 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003287 }
3288 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003289 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003290 return 0;
3291}
3292EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3293
Carsten Otte313a3dc2007-10-11 19:16:52 +02003294/*
3295 * Read or write a bunch of msrs. All parameters are kernel addresses.
3296 *
3297 * @return number of msrs set successfully.
3298 */
3299static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3300 struct kvm_msr_entry *entries,
3301 int (*do_msr)(struct kvm_vcpu *vcpu,
3302 unsigned index, u64 *data))
3303{
Tom Lendacky801e4592018-02-21 13:39:51 -06003304 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003305
Carsten Otte313a3dc2007-10-11 19:16:52 +02003306 for (i = 0; i < msrs->nmsrs; ++i)
3307 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3308 break;
3309
Carsten Otte313a3dc2007-10-11 19:16:52 +02003310 return i;
3311}
3312
3313/*
3314 * Read or write a bunch of msrs. Parameters are user addresses.
3315 *
3316 * @return number of msrs set successfully.
3317 */
3318static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3319 int (*do_msr)(struct kvm_vcpu *vcpu,
3320 unsigned index, u64 *data),
3321 int writeback)
3322{
3323 struct kvm_msrs msrs;
3324 struct kvm_msr_entry *entries;
3325 int r, n;
3326 unsigned size;
3327
3328 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003329 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003330 goto out;
3331
3332 r = -E2BIG;
3333 if (msrs.nmsrs >= MAX_IO_MSRS)
3334 goto out;
3335
Carsten Otte313a3dc2007-10-11 19:16:52 +02003336 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003337 entries = memdup_user(user_msrs->entries, size);
3338 if (IS_ERR(entries)) {
3339 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003340 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003341 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003342
3343 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3344 if (r < 0)
3345 goto out_free;
3346
3347 r = -EFAULT;
3348 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3349 goto out_free;
3350
3351 r = n;
3352
3353out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003354 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003355out:
3356 return r;
3357}
3358
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003359static inline bool kvm_can_mwait_in_guest(void)
3360{
3361 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003362 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3363 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003364}
3365
Alexander Graf784aa3d2014-07-14 18:27:35 +02003366int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003367{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003368 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003369
3370 switch (ext) {
3371 case KVM_CAP_IRQCHIP:
3372 case KVM_CAP_HLT:
3373 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003374 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003375 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003376 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003377 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003378 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003379 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003380 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003381 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003382 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003383 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003384 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003385 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003386 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003387 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003388 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003389 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003390 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003391 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003392 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003393 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003394 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003395 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003396 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003397 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003398 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003399 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003400 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003401 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003402 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003403 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003404 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003405 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003406 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003407 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003408 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003409 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003410 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003411 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003412 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003413 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003414 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003415 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003416 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003417 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003418 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003419 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003420 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003421 case KVM_CAP_SET_GUEST_DEBUG:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003422 r = 1;
3423 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003424 case KVM_CAP_SYNC_REGS:
3425 r = KVM_SYNC_X86_VALID_FIELDS;
3426 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003427 case KVM_CAP_ADJUST_CLOCK:
3428 r = KVM_CLOCK_TSC_STABLE;
3429 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003430 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003431 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3432 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003433 if(kvm_can_mwait_in_guest())
3434 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003435 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003436 case KVM_CAP_X86_SMM:
3437 /* SMBASE is usually relocated above 1M on modern chipsets,
3438 * and SMM handlers might indeed rely on 4G segment limits,
3439 * so do not report SMM to be available if real mode is
3440 * emulated via vm86 mode. Still, do not go to great lengths
3441 * to avoid userspace's usage of the feature, because it is a
3442 * fringe case that is not enabled except via specific settings
3443 * of the module parameters.
3444 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003445 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003446 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003447 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003448 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003449 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003450 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003451 r = KVM_SOFT_MAX_VCPUS;
3452 break;
3453 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003454 r = KVM_MAX_VCPUS;
3455 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003456 case KVM_CAP_MAX_VCPU_ID:
3457 r = KVM_MAX_VCPU_ID;
3458 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003459 case KVM_CAP_PV_MMU: /* obsolete */
3460 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003461 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003462 case KVM_CAP_MCE:
3463 r = KVM_MAX_MCE_BANKS;
3464 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003465 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003466 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003467 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003468 case KVM_CAP_TSC_CONTROL:
3469 r = kvm_has_tsc_control;
3470 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003471 case KVM_CAP_X2APIC_API:
3472 r = KVM_X2APIC_API_VALID_FLAGS;
3473 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003474 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003475 r = kvm_x86_ops.nested_ops->get_state ?
3476 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003477 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003478 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003479 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003480 break;
3481 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003482 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003483 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003484 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003485 break;
3486 }
3487 return r;
3488
3489}
3490
Carsten Otte043405e2007-10-10 17:16:19 +02003491long kvm_arch_dev_ioctl(struct file *filp,
3492 unsigned int ioctl, unsigned long arg)
3493{
3494 void __user *argp = (void __user *)arg;
3495 long r;
3496
3497 switch (ioctl) {
3498 case KVM_GET_MSR_INDEX_LIST: {
3499 struct kvm_msr_list __user *user_msr_list = argp;
3500 struct kvm_msr_list msr_list;
3501 unsigned n;
3502
3503 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003504 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003505 goto out;
3506 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003507 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003508 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003509 goto out;
3510 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003511 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003512 goto out;
3513 r = -EFAULT;
3514 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3515 num_msrs_to_save * sizeof(u32)))
3516 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003517 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003518 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003519 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003520 goto out;
3521 r = 0;
3522 break;
3523 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003524 case KVM_GET_SUPPORTED_CPUID:
3525 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003526 struct kvm_cpuid2 __user *cpuid_arg = argp;
3527 struct kvm_cpuid2 cpuid;
3528
3529 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003530 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003531 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003532
3533 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3534 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003535 if (r)
3536 goto out;
3537
3538 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003539 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003540 goto out;
3541 r = 0;
3542 break;
3543 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003544 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003545 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003546 if (copy_to_user(argp, &kvm_mce_cap_supported,
3547 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003548 goto out;
3549 r = 0;
3550 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003551 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3552 struct kvm_msr_list __user *user_msr_list = argp;
3553 struct kvm_msr_list msr_list;
3554 unsigned int n;
3555
3556 r = -EFAULT;
3557 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3558 goto out;
3559 n = msr_list.nmsrs;
3560 msr_list.nmsrs = num_msr_based_features;
3561 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3562 goto out;
3563 r = -E2BIG;
3564 if (n < msr_list.nmsrs)
3565 goto out;
3566 r = -EFAULT;
3567 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3568 num_msr_based_features * sizeof(u32)))
3569 goto out;
3570 r = 0;
3571 break;
3572 }
3573 case KVM_GET_MSRS:
3574 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3575 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003576 default:
3577 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003578 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003579 }
3580out:
3581 return r;
3582}
3583
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003584static void wbinvd_ipi(void *garbage)
3585{
3586 wbinvd();
3587}
3588
3589static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3590{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003591 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003592}
3593
Carsten Otte313a3dc2007-10-11 19:16:52 +02003594void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3595{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003596 /* Address WBINVD may be executed by guest */
3597 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003598 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003599 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3600 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3601 smp_call_function_single(vcpu->cpu,
3602 wbinvd_ipi, NULL, 1);
3603 }
3604
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003605 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003606
Babu Moger37486132020-05-12 18:59:06 -05003607 /* Save host pkru register if supported */
3608 vcpu->arch.host_pkru = read_pkru();
3609
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003610 /* Apply any externally detected TSC adjustments (due to suspend) */
3611 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3612 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3613 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003614 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003615 }
3616
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003617 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003618 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003619 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003620 if (tsc_delta < 0)
3621 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003622
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003623 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003624 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003625 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003626 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003627 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003628 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003629
3630 if (kvm_lapic_hv_timer_in_use(vcpu))
3631 kvm_lapic_restart_hv_timer(vcpu);
3632
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003633 /*
3634 * On a host with synchronized TSC, there is no need to update
3635 * kvmclock on vcpu->cpu migration
3636 */
3637 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003638 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003639 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003640 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003641 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003642 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003643
Glauber Costac9aaa892011-07-11 15:28:14 -04003644 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003645}
3646
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003647static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3648{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003649 struct kvm_host_map map;
3650 struct kvm_steal_time *st;
3651
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003652 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3653 return;
3654
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003655 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003656 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003657
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003658 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3659 &vcpu->arch.st.cache, true))
3660 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003661
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003662 st = map.hva +
3663 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3664
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003665 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003666
3667 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003668}
3669
Carsten Otte313a3dc2007-10-11 19:16:52 +02003670void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3671{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003672 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003673
3674 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003675 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003676
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003677 /*
3678 * Disable page faults because we're in atomic context here.
3679 * kvm_write_guest_offset_cached() would call might_fault()
3680 * that relies on pagefault_disable() to tell if there's a
3681 * bug. NOTE: the write to guest memory may not go through if
3682 * during postcopy live migration or if there's heavy guest
3683 * paging.
3684 */
3685 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003686 /*
3687 * kvm_memslots() will be called by
3688 * kvm_write_guest_offset_cached() so take the srcu lock.
3689 */
3690 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003691 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003692 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003693 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003694 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003695 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003696 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003697 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3698 * on every vmexit, but if not, we might have a stale dr6 from the
3699 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003700 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003701 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003702}
3703
Carsten Otte313a3dc2007-10-11 19:16:52 +02003704static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3705 struct kvm_lapic_state *s)
3706{
Liran Alonfa59cc02017-12-24 18:12:53 +02003707 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003708 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003709
Radim Krčmářa92e2542016-07-12 22:09:22 +02003710 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003711}
3712
3713static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3714 struct kvm_lapic_state *s)
3715{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003716 int r;
3717
3718 r = kvm_apic_set_state(vcpu, s);
3719 if (r)
3720 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003721 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003722
3723 return 0;
3724}
3725
Matt Gingell127a4572015-11-17 17:32:05 +01003726static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3727{
3728 return (!lapic_in_kernel(vcpu) ||
3729 kvm_apic_accept_pic_intr(vcpu));
3730}
3731
Matt Gingell782d4222015-11-16 15:26:00 -08003732/*
3733 * if userspace requested an interrupt window, check that the
3734 * interrupt window is open.
3735 *
3736 * No need to exit to userspace if we already have an interrupt queued.
3737 */
3738static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3739{
3740 return kvm_arch_interrupt_allowed(vcpu) &&
3741 !kvm_cpu_has_interrupt(vcpu) &&
3742 !kvm_event_needs_reinjection(vcpu) &&
3743 kvm_cpu_accept_dm_intr(vcpu);
3744}
3745
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003746static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3747 struct kvm_interrupt *irq)
3748{
Chen Gang02cdb502013-02-27 11:33:25 +08003749 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003750 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003751
3752 if (!irqchip_in_kernel(vcpu->kvm)) {
3753 kvm_queue_interrupt(vcpu, irq->irq, false);
3754 kvm_make_request(KVM_REQ_EVENT, vcpu);
3755 return 0;
3756 }
3757
3758 /*
3759 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3760 * fail for in-kernel 8259.
3761 */
3762 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003763 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003764
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003765 if (vcpu->arch.pending_external_vector != -1)
3766 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003767
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003768 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003769 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003770 return 0;
3771}
3772
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003773static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3774{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003775 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003776
3777 return 0;
3778}
3779
Paolo Bonzinif0778252015-04-01 15:06:40 +02003780static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3781{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003782 kvm_make_request(KVM_REQ_SMI, vcpu);
3783
Paolo Bonzinif0778252015-04-01 15:06:40 +02003784 return 0;
3785}
3786
Avi Kivityb209749f2007-10-22 16:50:39 +02003787static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3788 struct kvm_tpr_access_ctl *tac)
3789{
3790 if (tac->flags)
3791 return -EINVAL;
3792 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3793 return 0;
3794}
3795
Huang Ying890ca9a2009-05-11 16:48:15 +08003796static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3797 u64 mcg_cap)
3798{
3799 int r;
3800 unsigned bank_num = mcg_cap & 0xff, bank;
3801
3802 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07003803 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003804 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003805 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003806 goto out;
3807 r = 0;
3808 vcpu->arch.mcg_cap = mcg_cap;
3809 /* Init IA32_MCG_CTL to all 1s */
3810 if (mcg_cap & MCG_CTL_P)
3811 vcpu->arch.mcg_ctl = ~(u64)0;
3812 /* Init IA32_MCi_CTL to all 1s */
3813 for (bank = 0; bank < bank_num; bank++)
3814 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003815
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003816 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003817out:
3818 return r;
3819}
3820
3821static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3822 struct kvm_x86_mce *mce)
3823{
3824 u64 mcg_cap = vcpu->arch.mcg_cap;
3825 unsigned bank_num = mcg_cap & 0xff;
3826 u64 *banks = vcpu->arch.mce_banks;
3827
3828 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3829 return -EINVAL;
3830 /*
3831 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3832 * reporting is disabled
3833 */
3834 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3835 vcpu->arch.mcg_ctl != ~(u64)0)
3836 return 0;
3837 banks += 4 * mce->bank;
3838 /*
3839 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3840 * reporting is disabled for the bank
3841 */
3842 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3843 return 0;
3844 if (mce->status & MCI_STATUS_UC) {
3845 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003846 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003847 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003848 return 0;
3849 }
3850 if (banks[1] & MCI_STATUS_VAL)
3851 mce->status |= MCI_STATUS_OVER;
3852 banks[2] = mce->addr;
3853 banks[3] = mce->misc;
3854 vcpu->arch.mcg_status = mce->mcg_status;
3855 banks[1] = mce->status;
3856 kvm_queue_exception(vcpu, MC_VECTOR);
3857 } else if (!(banks[1] & MCI_STATUS_VAL)
3858 || !(banks[1] & MCI_STATUS_UC)) {
3859 if (banks[1] & MCI_STATUS_VAL)
3860 mce->status |= MCI_STATUS_OVER;
3861 banks[2] = mce->addr;
3862 banks[3] = mce->misc;
3863 banks[1] = mce->status;
3864 } else
3865 banks[1] |= MCI_STATUS_OVER;
3866 return 0;
3867}
3868
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003869static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3870 struct kvm_vcpu_events *events)
3871{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003872 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003873
Wanpeng Li664f8e22017-08-24 03:35:09 -07003874 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003875 * In guest mode, payload delivery should be deferred,
3876 * so that the L1 hypervisor can intercept #PF before
3877 * CR2 is modified (or intercept #DB before DR6 is
3878 * modified under nVMX). Unless the per-VM capability,
3879 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3880 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3881 * opportunistically defer the exception payload, deliver it if the
3882 * capability hasn't been requested before processing a
3883 * KVM_GET_VCPU_EVENTS.
3884 */
3885 if (!vcpu->kvm->arch.exception_payload_enabled &&
3886 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3887 kvm_deliver_exception_payload(vcpu);
3888
3889 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003890 * The API doesn't provide the instruction length for software
3891 * exceptions, so don't report them. As long as the guest RIP
3892 * isn't advanced, we should expect to encounter the exception
3893 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003894 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003895 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3896 events->exception.injected = 0;
3897 events->exception.pending = 0;
3898 } else {
3899 events->exception.injected = vcpu->arch.exception.injected;
3900 events->exception.pending = vcpu->arch.exception.pending;
3901 /*
3902 * For ABI compatibility, deliberately conflate
3903 * pending and injected exceptions when
3904 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3905 */
3906 if (!vcpu->kvm->arch.exception_payload_enabled)
3907 events->exception.injected |=
3908 vcpu->arch.exception.pending;
3909 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003910 events->exception.nr = vcpu->arch.exception.nr;
3911 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3912 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003913 events->exception_has_payload = vcpu->arch.exception.has_payload;
3914 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003915
Jan Kiszka03b82a32010-02-15 10:45:41 +01003916 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003917 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003918 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003919 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003920 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003921
3922 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003923 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003924 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003925 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003926
Jan Kiszka66450a22013-03-13 12:42:34 +01003927 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003928
Paolo Bonzinif0778252015-04-01 15:06:40 +02003929 events->smi.smm = is_smm(vcpu);
3930 events->smi.pending = vcpu->arch.smi_pending;
3931 events->smi.smm_inside_nmi =
3932 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3933 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3934
Jan Kiszkadab4b912009-12-06 18:24:15 +01003935 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003936 | KVM_VCPUEVENT_VALID_SHADOW
3937 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003938 if (vcpu->kvm->arch.exception_payload_enabled)
3939 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3940
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003941 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003942}
3943
Sean Christophersonc5833c72019-04-02 08:03:10 -07003944static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003945
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003946static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3947 struct kvm_vcpu_events *events)
3948{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003949 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003950 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003951 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003952 | KVM_VCPUEVENT_VALID_SMM
3953 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003954 return -EINVAL;
3955
Jim Mattson59073aa2018-10-16 14:29:20 -07003956 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3957 if (!vcpu->kvm->arch.exception_payload_enabled)
3958 return -EINVAL;
3959 if (events->exception.pending)
3960 events->exception.injected = 0;
3961 else
3962 events->exception_has_payload = 0;
3963 } else {
3964 events->exception.pending = 0;
3965 events->exception_has_payload = 0;
3966 }
3967
3968 if ((events->exception.injected || events->exception.pending) &&
3969 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003970 return -EINVAL;
3971
David Hildenbrand28bf2882017-03-23 11:46:03 +01003972 /* INITs are latched while in SMM */
3973 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3974 (events->smi.smm || events->smi.pending) &&
3975 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3976 return -EINVAL;
3977
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003978 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003979 vcpu->arch.exception.injected = events->exception.injected;
3980 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003981 vcpu->arch.exception.nr = events->exception.nr;
3982 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3983 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003984 vcpu->arch.exception.has_payload = events->exception_has_payload;
3985 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003986
Liran Alon04140b42018-03-23 03:01:31 +03003987 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003988 vcpu->arch.interrupt.nr = events->interrupt.nr;
3989 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003990 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003991 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01003992 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003993
3994 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003995 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3996 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003997 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003998
Jan Kiszka66450a22013-03-13 12:42:34 +01003999 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004000 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004001 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004002
Paolo Bonzinif0778252015-04-01 15:06:40 +02004003 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07004004 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
4005 if (events->smi.smm)
4006 vcpu->arch.hflags |= HF_SMM_MASK;
4007 else
4008 vcpu->arch.hflags &= ~HF_SMM_MASK;
4009 kvm_smm_changed(vcpu);
4010 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004011
Paolo Bonzinif0778252015-04-01 15:06:40 +02004012 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004013
4014 if (events->smi.smm) {
4015 if (events->smi.smm_inside_nmi)
4016 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004017 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004018 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004019 }
4020
4021 if (lapic_in_kernel(vcpu)) {
4022 if (events->smi.latched_init)
4023 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4024 else
4025 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004026 }
4027 }
4028
Avi Kivity3842d132010-07-27 12:30:24 +03004029 kvm_make_request(KVM_REQ_EVENT, vcpu);
4030
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004031 return 0;
4032}
4033
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004034static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4035 struct kvm_debugregs *dbgregs)
4036{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004037 unsigned long val;
4038
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004039 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004040 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004041 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004042 dbgregs->dr7 = vcpu->arch.dr7;
4043 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004044 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004045}
4046
4047static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4048 struct kvm_debugregs *dbgregs)
4049{
4050 if (dbgregs->flags)
4051 return -EINVAL;
4052
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004053 if (dbgregs->dr6 & ~0xffffffffull)
4054 return -EINVAL;
4055 if (dbgregs->dr7 & ~0xffffffffull)
4056 return -EINVAL;
4057
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004058 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004059 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004060 vcpu->arch.dr6 = dbgregs->dr6;
4061 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004062 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004063
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004064 return 0;
4065}
4066
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004067#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4068
4069static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4070{
Marc Orrb666a4b2018-11-06 14:53:56 -08004071 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004072 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004073 u64 valid;
4074
4075 /*
4076 * Copy legacy XSAVE area, to avoid complications with CPUID
4077 * leaves 0 and 1 in the loop below.
4078 */
4079 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4080
4081 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004082 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004083 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4084
4085 /*
4086 * Copy each region from the possibly compacted offset to the
4087 * non-compacted offset.
4088 */
Dave Hansend91cab72015-09-02 16:31:26 -07004089 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004090 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004091 u64 xfeature_mask = valid & -valid;
4092 int xfeature_nr = fls64(xfeature_mask) - 1;
4093 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004094
4095 if (src) {
4096 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004097 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004098 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004099 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004100 memcpy(dest + offset, &vcpu->arch.pkru,
4101 sizeof(vcpu->arch.pkru));
4102 else
4103 memcpy(dest + offset, src, size);
4104
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004105 }
4106
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004107 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004108 }
4109}
4110
4111static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4112{
Marc Orrb666a4b2018-11-06 14:53:56 -08004113 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004114 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4115 u64 valid;
4116
4117 /*
4118 * Copy legacy XSAVE area, to avoid complications with CPUID
4119 * leaves 0 and 1 in the loop below.
4120 */
4121 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4122
4123 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004124 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004125 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004126 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004127
4128 /*
4129 * Copy each region from the non-compacted offset to the
4130 * possibly compacted offset.
4131 */
Dave Hansend91cab72015-09-02 16:31:26 -07004132 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004133 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004134 u64 xfeature_mask = valid & -valid;
4135 int xfeature_nr = fls64(xfeature_mask) - 1;
4136 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004137
4138 if (dest) {
4139 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004140 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004141 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004142 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004143 memcpy(&vcpu->arch.pkru, src + offset,
4144 sizeof(vcpu->arch.pkru));
4145 else
4146 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004147 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004148
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004149 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004150 }
4151}
4152
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004153static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4154 struct kvm_xsave *guest_xsave)
4155{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004156 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004157 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4158 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004159 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004160 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004161 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004162 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004163 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004164 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004165 }
4166}
4167
Wanpeng Lia5758132017-05-11 02:58:55 -07004168#define XSAVE_MXCSR_OFFSET 24
4169
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004170static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4171 struct kvm_xsave *guest_xsave)
4172{
4173 u64 xstate_bv =
4174 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004175 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004176
Borislav Petkovd366bf72016-04-04 22:25:02 +02004177 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004178 /*
4179 * Here we allow setting states that are not present in
4180 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4181 * with old userspace.
4182 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004183 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004184 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004185 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004186 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004187 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4188 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004189 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004190 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004191 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004192 }
4193 return 0;
4194}
4195
4196static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4197 struct kvm_xcrs *guest_xcrs)
4198{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004199 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004200 guest_xcrs->nr_xcrs = 0;
4201 return;
4202 }
4203
4204 guest_xcrs->nr_xcrs = 1;
4205 guest_xcrs->flags = 0;
4206 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4207 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4208}
4209
4210static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4211 struct kvm_xcrs *guest_xcrs)
4212{
4213 int i, r = 0;
4214
Borislav Petkovd366bf72016-04-04 22:25:02 +02004215 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004216 return -EINVAL;
4217
4218 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4219 return -EINVAL;
4220
4221 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4222 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004223 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004224 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004225 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004226 break;
4227 }
4228 if (r)
4229 r = -EINVAL;
4230 return r;
4231}
4232
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004233/*
4234 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4235 * stopped by the hypervisor. This function will be called from the host only.
4236 * EINVAL is returned when the host attempts to set the flag for a guest that
4237 * does not support pv clocks.
4238 */
4239static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4240{
Andy Honig0b794592013-02-20 14:48:10 -08004241 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004242 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004243 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004244 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4245 return 0;
4246}
4247
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004248static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4249 struct kvm_enable_cap *cap)
4250{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004251 int r;
4252 uint16_t vmcs_version;
4253 void __user *user_ptr;
4254
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004255 if (cap->flags)
4256 return -EINVAL;
4257
4258 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004259 case KVM_CAP_HYPERV_SYNIC2:
4260 if (cap->args[0])
4261 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004262 /* fall through */
4263
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004264 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004265 if (!irqchip_in_kernel(vcpu->kvm))
4266 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004267 return kvm_hv_activate_synic(vcpu, cap->cap ==
4268 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004269 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004270 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004271 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004272 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004273 if (!r) {
4274 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4275 if (copy_to_user(user_ptr, &vmcs_version,
4276 sizeof(vmcs_version)))
4277 r = -EFAULT;
4278 }
4279 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004280 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004281 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004282 return -ENOTTY;
4283
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004284 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004285
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004286 default:
4287 return -EINVAL;
4288 }
4289}
4290
Carsten Otte313a3dc2007-10-11 19:16:52 +02004291long kvm_arch_vcpu_ioctl(struct file *filp,
4292 unsigned int ioctl, unsigned long arg)
4293{
4294 struct kvm_vcpu *vcpu = filp->private_data;
4295 void __user *argp = (void __user *)arg;
4296 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004297 union {
4298 struct kvm_lapic_state *lapic;
4299 struct kvm_xsave *xsave;
4300 struct kvm_xcrs *xcrs;
4301 void *buffer;
4302 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004303
Christoffer Dall9b0624712017-12-04 21:35:36 +01004304 vcpu_load(vcpu);
4305
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004306 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004307 switch (ioctl) {
4308 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004309 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004310 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004311 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004312 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4313 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004314
Dave Hansenb772ff32008-08-11 10:01:47 -07004315 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004316 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004317 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004318 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004319 if (r)
4320 goto out;
4321 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004322 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004323 goto out;
4324 r = 0;
4325 break;
4326 }
4327 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004328 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004329 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004330 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004331 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004332 if (IS_ERR(u.lapic)) {
4333 r = PTR_ERR(u.lapic);
4334 goto out_nofree;
4335 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004336
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004337 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004338 break;
4339 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004340 case KVM_INTERRUPT: {
4341 struct kvm_interrupt irq;
4342
4343 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004344 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004345 goto out;
4346 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004347 break;
4348 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004349 case KVM_NMI: {
4350 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004351 break;
4352 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004353 case KVM_SMI: {
4354 r = kvm_vcpu_ioctl_smi(vcpu);
4355 break;
4356 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004357 case KVM_SET_CPUID: {
4358 struct kvm_cpuid __user *cpuid_arg = argp;
4359 struct kvm_cpuid cpuid;
4360
4361 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004362 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004363 goto out;
4364 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004365 break;
4366 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004367 case KVM_SET_CPUID2: {
4368 struct kvm_cpuid2 __user *cpuid_arg = argp;
4369 struct kvm_cpuid2 cpuid;
4370
4371 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004372 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004373 goto out;
4374 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004375 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004376 break;
4377 }
4378 case KVM_GET_CPUID2: {
4379 struct kvm_cpuid2 __user *cpuid_arg = argp;
4380 struct kvm_cpuid2 cpuid;
4381
4382 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004383 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004384 goto out;
4385 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004386 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004387 if (r)
4388 goto out;
4389 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004390 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004391 goto out;
4392 r = 0;
4393 break;
4394 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004395 case KVM_GET_MSRS: {
4396 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004397 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004398 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004399 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004400 }
4401 case KVM_SET_MSRS: {
4402 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004403 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004404 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004405 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004406 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004407 case KVM_TPR_ACCESS_REPORTING: {
4408 struct kvm_tpr_access_ctl tac;
4409
4410 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004411 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004412 goto out;
4413 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4414 if (r)
4415 goto out;
4416 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004417 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004418 goto out;
4419 r = 0;
4420 break;
4421 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004422 case KVM_SET_VAPIC_ADDR: {
4423 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004424 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004425
4426 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004427 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004428 goto out;
4429 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004430 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004431 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004432 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004433 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004434 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004435 break;
4436 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004437 case KVM_X86_SETUP_MCE: {
4438 u64 mcg_cap;
4439
4440 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004441 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004442 goto out;
4443 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4444 break;
4445 }
4446 case KVM_X86_SET_MCE: {
4447 struct kvm_x86_mce mce;
4448
4449 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004450 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004451 goto out;
4452 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4453 break;
4454 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004455 case KVM_GET_VCPU_EVENTS: {
4456 struct kvm_vcpu_events events;
4457
4458 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4459
4460 r = -EFAULT;
4461 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4462 break;
4463 r = 0;
4464 break;
4465 }
4466 case KVM_SET_VCPU_EVENTS: {
4467 struct kvm_vcpu_events events;
4468
4469 r = -EFAULT;
4470 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4471 break;
4472
4473 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4474 break;
4475 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004476 case KVM_GET_DEBUGREGS: {
4477 struct kvm_debugregs dbgregs;
4478
4479 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4480
4481 r = -EFAULT;
4482 if (copy_to_user(argp, &dbgregs,
4483 sizeof(struct kvm_debugregs)))
4484 break;
4485 r = 0;
4486 break;
4487 }
4488 case KVM_SET_DEBUGREGS: {
4489 struct kvm_debugregs dbgregs;
4490
4491 r = -EFAULT;
4492 if (copy_from_user(&dbgregs, argp,
4493 sizeof(struct kvm_debugregs)))
4494 break;
4495
4496 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4497 break;
4498 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004499 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004500 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004501 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004502 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004503 break;
4504
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004505 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004506
4507 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004508 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004509 break;
4510 r = 0;
4511 break;
4512 }
4513 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004514 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004515 if (IS_ERR(u.xsave)) {
4516 r = PTR_ERR(u.xsave);
4517 goto out_nofree;
4518 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004519
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004520 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004521 break;
4522 }
4523 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004524 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004525 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004526 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004527 break;
4528
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004529 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004530
4531 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004532 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004533 sizeof(struct kvm_xcrs)))
4534 break;
4535 r = 0;
4536 break;
4537 }
4538 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004539 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004540 if (IS_ERR(u.xcrs)) {
4541 r = PTR_ERR(u.xcrs);
4542 goto out_nofree;
4543 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004544
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004545 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004546 break;
4547 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004548 case KVM_SET_TSC_KHZ: {
4549 u32 user_tsc_khz;
4550
4551 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004552 user_tsc_khz = (u32)arg;
4553
4554 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4555 goto out;
4556
Zachary Amsdencc578282012-02-03 15:43:50 -02004557 if (user_tsc_khz == 0)
4558 user_tsc_khz = tsc_khz;
4559
Haozhong Zhang381d5852015-10-20 15:39:04 +08004560 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4561 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004562
Joerg Roedel92a1f122011-03-25 09:44:51 +01004563 goto out;
4564 }
4565 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004566 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004567 goto out;
4568 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004569 case KVM_KVMCLOCK_CTRL: {
4570 r = kvm_set_guest_paused(vcpu);
4571 goto out;
4572 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004573 case KVM_ENABLE_CAP: {
4574 struct kvm_enable_cap cap;
4575
4576 r = -EFAULT;
4577 if (copy_from_user(&cap, argp, sizeof(cap)))
4578 goto out;
4579 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4580 break;
4581 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582 case KVM_GET_NESTED_STATE: {
4583 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4584 u32 user_data_size;
4585
4586 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004587 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004588 break;
4589
4590 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004591 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004592 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004593 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004594
Paolo Bonzini33b22172020-04-17 10:24:18 -04004595 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
4596 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004597 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004598 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004599
4600 if (r > user_data_size) {
4601 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004602 r = -EFAULT;
4603 else
4604 r = -E2BIG;
4605 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004606 }
Liran Alon26b471c2018-09-16 14:28:20 +03004607
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004608 r = 0;
4609 break;
4610 }
4611 case KVM_SET_NESTED_STATE: {
4612 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4613 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004614 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004615
4616 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004617 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004618 break;
4619
Liran Alon26b471c2018-09-16 14:28:20 +03004620 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004621 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004622 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004623
Liran Alon26b471c2018-09-16 14:28:20 +03004624 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004625 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004626 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004627
4628 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004629 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinidf2a69a2020-05-19 12:51:32 -04004630 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING))
Liran Alon26b471c2018-09-16 14:28:20 +03004631 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004632
4633 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004634 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4635 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004636 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004637
Sean Christophersonad5996d2019-11-22 08:58:18 -08004638 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04004639 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004640 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004641 break;
4642 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004643 case KVM_GET_SUPPORTED_HV_CPUID: {
4644 struct kvm_cpuid2 __user *cpuid_arg = argp;
4645 struct kvm_cpuid2 cpuid;
4646
4647 r = -EFAULT;
4648 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4649 goto out;
4650
4651 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4652 cpuid_arg->entries);
4653 if (r)
4654 goto out;
4655
4656 r = -EFAULT;
4657 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4658 goto out;
4659 r = 0;
4660 break;
4661 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004662 default:
4663 r = -EINVAL;
4664 }
4665out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004666 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004667out_nofree:
4668 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004669 return r;
4670}
4671
Souptick Joarder1499fa82018-04-19 00:49:58 +05304672vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004673{
4674 return VM_FAULT_SIGBUS;
4675}
4676
Carsten Otte1fe779f2007-10-29 16:08:35 +01004677static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4678{
4679 int ret;
4680
4681 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004682 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004683 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004684 return ret;
4685}
4686
Sheng Yangb927a3c2009-07-21 10:42:48 +08004687static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4688 u64 ident_addr)
4689{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004690 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004691}
4692
Carsten Otte1fe779f2007-10-29 16:08:35 +01004693static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004694 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004695{
4696 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4697 return -EINVAL;
4698
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004699 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004700
4701 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004702 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004703
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004704 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004705 return 0;
4706}
4707
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004708static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004709{
Dave Hansen39de71e2010-08-19 18:11:14 -07004710 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004711}
4712
Carsten Otte1fe779f2007-10-29 16:08:35 +01004713static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4714{
David Hildenbrand90bca052017-04-07 10:50:23 +02004715 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004716 int r;
4717
4718 r = 0;
4719 switch (chip->chip_id) {
4720 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004721 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004722 sizeof(struct kvm_pic_state));
4723 break;
4724 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004725 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004726 sizeof(struct kvm_pic_state));
4727 break;
4728 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004729 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004730 break;
4731 default:
4732 r = -EINVAL;
4733 break;
4734 }
4735 return r;
4736}
4737
4738static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4739{
David Hildenbrand90bca052017-04-07 10:50:23 +02004740 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004741 int r;
4742
4743 r = 0;
4744 switch (chip->chip_id) {
4745 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004746 spin_lock(&pic->lock);
4747 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004748 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004749 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004750 break;
4751 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004752 spin_lock(&pic->lock);
4753 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004754 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004755 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004756 break;
4757 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004758 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004759 break;
4760 default:
4761 r = -EINVAL;
4762 break;
4763 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004764 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004765 return r;
4766}
4767
Sheng Yange0f63cb2008-03-04 00:50:59 +08004768static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4769{
Radim Krčmář34f39412016-03-02 22:56:50 +01004770 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4771
4772 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4773
4774 mutex_lock(&kps->lock);
4775 memcpy(ps, &kps->channels, sizeof(*ps));
4776 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304777 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004778}
4779
4780static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4781{
Andrew Honig01856042015-11-18 14:50:23 -08004782 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004783 struct kvm_pit *pit = kvm->arch.vpit;
4784
4785 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004786 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004787 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004788 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4789 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304790 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004791}
4792
4793static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4794{
Beth Kone9f42752009-07-07 11:50:38 -04004795 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4796 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4797 sizeof(ps->channels));
4798 ps->flags = kvm->arch.vpit->pit_state.flags;
4799 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004800 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304801 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004802}
4803
4804static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4805{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304806 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004807 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004808 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004809 struct kvm_pit *pit = kvm->arch.vpit;
4810
4811 mutex_lock(&pit->pit_state.lock);
4812 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004813 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4814 if (!prev_legacy && cur_legacy)
4815 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004816 memcpy(&pit->pit_state.channels, &ps->channels,
4817 sizeof(pit->pit_state.channels));
4818 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004819 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004820 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004821 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004822 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304823 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004824}
4825
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004826static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4827 struct kvm_reinject_control *control)
4828{
Radim Krčmář71474e22016-03-02 22:56:45 +01004829 struct kvm_pit *pit = kvm->arch.vpit;
4830
Radim Krčmář71474e22016-03-02 22:56:45 +01004831 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4832 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4833 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4834 */
4835 mutex_lock(&pit->pit_state.lock);
4836 kvm_pit_set_reinject(pit, control->pit_reinject);
4837 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004838
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004839 return 0;
4840}
4841
Sean Christopherson0dff0842020-02-18 13:07:29 -08004842void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004843{
Kai Huang88178fd2015-01-28 10:54:27 +08004844 /*
4845 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4846 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004847 if (kvm_x86_ops.flush_log_dirty)
4848 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004849}
4850
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004851int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4852 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004853{
4854 if (!irqchip_in_kernel(kvm))
4855 return -ENXIO;
4856
4857 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004858 irq_event->irq, irq_event->level,
4859 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004860 return 0;
4861}
4862
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004863int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4864 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004865{
4866 int r;
4867
4868 if (cap->flags)
4869 return -EINVAL;
4870
4871 switch (cap->cap) {
4872 case KVM_CAP_DISABLE_QUIRKS:
4873 kvm->arch.disabled_quirks = cap->args[0];
4874 r = 0;
4875 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004876 case KVM_CAP_SPLIT_IRQCHIP: {
4877 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004878 r = -EINVAL;
4879 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4880 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004881 r = -EEXIST;
4882 if (irqchip_in_kernel(kvm))
4883 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004884 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004885 goto split_irqchip_unlock;
4886 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004887 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004888 goto split_irqchip_unlock;
4889 /* Pairs with irqchip_in_kernel. */
4890 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004891 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004892 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004893 r = 0;
4894split_irqchip_unlock:
4895 mutex_unlock(&kvm->lock);
4896 break;
4897 }
Radim Krčmář371313132016-07-12 22:09:27 +02004898 case KVM_CAP_X2APIC_API:
4899 r = -EINVAL;
4900 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4901 break;
4902
4903 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4904 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004905 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4906 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004907
4908 r = 0;
4909 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004910 case KVM_CAP_X86_DISABLE_EXITS:
4911 r = -EINVAL;
4912 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4913 break;
4914
4915 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4916 kvm_can_mwait_in_guest())
4917 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004918 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004919 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004920 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4921 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004922 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4923 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004924 r = 0;
4925 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004926 case KVM_CAP_MSR_PLATFORM_INFO:
4927 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4928 r = 0;
4929 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004930 case KVM_CAP_EXCEPTION_PAYLOAD:
4931 kvm->arch.exception_payload_enabled = cap->args[0];
4932 r = 0;
4933 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004934 default:
4935 r = -EINVAL;
4936 break;
4937 }
4938 return r;
4939}
4940
Carsten Otte1fe779f2007-10-29 16:08:35 +01004941long kvm_arch_vm_ioctl(struct file *filp,
4942 unsigned int ioctl, unsigned long arg)
4943{
4944 struct kvm *kvm = filp->private_data;
4945 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004946 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004947 /*
4948 * This union makes it completely explicit to gcc-3.x
4949 * that these two variables' stack usage should be
4950 * combined, not added together.
4951 */
4952 union {
4953 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004954 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004955 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004956 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004957
4958 switch (ioctl) {
4959 case KVM_SET_TSS_ADDR:
4960 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004961 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004962 case KVM_SET_IDENTITY_MAP_ADDR: {
4963 u64 ident_addr;
4964
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004965 mutex_lock(&kvm->lock);
4966 r = -EINVAL;
4967 if (kvm->created_vcpus)
4968 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004969 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004970 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004971 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004972 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004973set_identity_unlock:
4974 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004975 break;
4976 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004977 case KVM_SET_NR_MMU_PAGES:
4978 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004979 break;
4980 case KVM_GET_NR_MMU_PAGES:
4981 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4982 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004983 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004984 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004985
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004986 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004987 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004988 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004989
Avi Kivity3e515702012-03-05 14:23:29 +02004990 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004991 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004992 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004993
4994 r = kvm_pic_init(kvm);
4995 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004996 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004997
4998 r = kvm_ioapic_init(kvm);
4999 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005000 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005001 goto create_irqchip_unlock;
5002 }
5003
Avi Kivity399ec802008-11-19 13:58:46 +02005004 r = kvm_setup_default_irq_routing(kvm);
5005 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005006 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005007 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005008 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005009 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005010 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005011 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005012 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005013 create_irqchip_unlock:
5014 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005015 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005016 }
Sheng Yang78376992008-01-28 05:10:22 +08005017 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005018 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5019 goto create_pit;
5020 case KVM_CREATE_PIT2:
5021 r = -EFAULT;
5022 if (copy_from_user(&u.pit_config, argp,
5023 sizeof(struct kvm_pit_config)))
5024 goto out;
5025 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005026 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005027 r = -EEXIST;
5028 if (kvm->arch.vpit)
5029 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005030 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005031 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005032 if (kvm->arch.vpit)
5033 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005034 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005035 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005036 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005037 case KVM_GET_IRQCHIP: {
5038 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005039 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005040
Sasha Levinff5c2c02011-12-04 19:36:29 +02005041 chip = memdup_user(argp, sizeof(*chip));
5042 if (IS_ERR(chip)) {
5043 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005044 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005045 }
5046
Carsten Otte1fe779f2007-10-29 16:08:35 +01005047 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005048 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005049 goto get_irqchip_out;
5050 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5051 if (r)
5052 goto get_irqchip_out;
5053 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005054 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005055 goto get_irqchip_out;
5056 r = 0;
5057 get_irqchip_out:
5058 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005059 break;
5060 }
5061 case KVM_SET_IRQCHIP: {
5062 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005063 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005064
Sasha Levinff5c2c02011-12-04 19:36:29 +02005065 chip = memdup_user(argp, sizeof(*chip));
5066 if (IS_ERR(chip)) {
5067 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005068 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005069 }
5070
Carsten Otte1fe779f2007-10-29 16:08:35 +01005071 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005072 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005073 goto set_irqchip_out;
5074 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005075 set_irqchip_out:
5076 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005077 break;
5078 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005079 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005080 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005081 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005082 goto out;
5083 r = -ENXIO;
5084 if (!kvm->arch.vpit)
5085 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005086 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005087 if (r)
5088 goto out;
5089 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005090 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005091 goto out;
5092 r = 0;
5093 break;
5094 }
5095 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005096 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005097 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005098 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005099 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005100 r = -ENXIO;
5101 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005102 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005103 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005104set_pit_out:
5105 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005106 break;
5107 }
Beth Kone9f42752009-07-07 11:50:38 -04005108 case KVM_GET_PIT2: {
5109 r = -ENXIO;
5110 if (!kvm->arch.vpit)
5111 goto out;
5112 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5113 if (r)
5114 goto out;
5115 r = -EFAULT;
5116 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5117 goto out;
5118 r = 0;
5119 break;
5120 }
5121 case KVM_SET_PIT2: {
5122 r = -EFAULT;
5123 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5124 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005125 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005126 r = -ENXIO;
5127 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005128 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005129 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005130set_pit2_out:
5131 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005132 break;
5133 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005134 case KVM_REINJECT_CONTROL: {
5135 struct kvm_reinject_control control;
5136 r = -EFAULT;
5137 if (copy_from_user(&control, argp, sizeof(control)))
5138 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005139 r = -ENXIO;
5140 if (!kvm->arch.vpit)
5141 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005142 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005143 break;
5144 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005145 case KVM_SET_BOOT_CPU_ID:
5146 r = 0;
5147 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005148 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005149 r = -EBUSY;
5150 else
5151 kvm->arch.bsp_vcpu_id = arg;
5152 mutex_unlock(&kvm->lock);
5153 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005154 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005155 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005156 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005157 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005158 goto out;
5159 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005160 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005161 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005162 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005163 r = 0;
5164 break;
5165 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005166 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005167 struct kvm_clock_data user_ns;
5168 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005169
5170 r = -EFAULT;
5171 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5172 goto out;
5173
5174 r = -EINVAL;
5175 if (user_ns.flags)
5176 goto out;
5177
5178 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005179 /*
5180 * TODO: userspace has to take care of races with VCPU_RUN, so
5181 * kvm_gen_update_masterclock() can be cut down to locked
5182 * pvclock_update_vm_gtod_copy().
5183 */
5184 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005185 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005186 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005187 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005188 break;
5189 }
5190 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005191 struct kvm_clock_data user_ns;
5192 u64 now_ns;
5193
Marcelo Tosattie891a322017-04-17 12:51:37 -03005194 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005195 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005196 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005197 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005198
5199 r = -EFAULT;
5200 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5201 goto out;
5202 r = 0;
5203 break;
5204 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005205 case KVM_MEMORY_ENCRYPT_OP: {
5206 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005207 if (kvm_x86_ops.mem_enc_op)
5208 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005209 break;
5210 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005211 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5212 struct kvm_enc_region region;
5213
5214 r = -EFAULT;
5215 if (copy_from_user(&region, argp, sizeof(region)))
5216 goto out;
5217
5218 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005219 if (kvm_x86_ops.mem_enc_reg_region)
5220 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005221 break;
5222 }
5223 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5224 struct kvm_enc_region region;
5225
5226 r = -EFAULT;
5227 if (copy_from_user(&region, argp, sizeof(region)))
5228 goto out;
5229
5230 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005231 if (kvm_x86_ops.mem_enc_unreg_region)
5232 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005233 break;
5234 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005235 case KVM_HYPERV_EVENTFD: {
5236 struct kvm_hyperv_eventfd hvevfd;
5237
5238 r = -EFAULT;
5239 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5240 goto out;
5241 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5242 break;
5243 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005244 case KVM_SET_PMU_EVENT_FILTER:
5245 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5246 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005247 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005248 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005249 }
5250out:
5251 return r;
5252}
5253
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005254static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005255{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005256 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005257 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005258 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005259
Jim Mattsone2ada662019-08-21 11:20:04 -07005260 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005261 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005262
5263 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005264
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005265 num_msrs_to_save = 0;
5266 num_emulated_msrs = 0;
5267 num_msr_based_features = 0;
5268
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005269 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5270 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005271 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005272
5273 /*
5274 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005275 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005276 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005277 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005278 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005279 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005280 continue;
5281 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005282 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005283 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005284 continue;
5285 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03005286 case MSR_IA32_UMWAIT_CONTROL:
5287 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
5288 continue;
5289 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005290 case MSR_IA32_RTIT_CTL:
5291 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005292 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005293 continue;
5294 break;
5295 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005296 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005297 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5298 continue;
5299 break;
5300 case MSR_IA32_RTIT_OUTPUT_BASE:
5301 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005302 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005303 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5304 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5305 continue;
5306 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005307 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005308 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005309 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005310 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5311 continue;
5312 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005313 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005314 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005315 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5316 continue;
5317 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005318 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005319 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005320 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5321 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005322 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005323 default:
5324 break;
5325 }
5326
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005327 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005328 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005329
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005330 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005331 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005332 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005333
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005334 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005335 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005336
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005337 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005338 struct kvm_msr_entry msr;
5339
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005340 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005341 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005342 continue;
5343
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005344 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005345 }
Carsten Otte043405e2007-10-10 17:16:19 +02005346}
5347
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005348static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5349 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005350{
Avi Kivity70252a12010-01-19 12:51:22 +02005351 int handled = 0;
5352 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005353
Avi Kivity70252a12010-01-19 12:51:22 +02005354 do {
5355 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005356 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005357 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5358 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005359 break;
5360 handled += n;
5361 addr += n;
5362 len -= n;
5363 v += n;
5364 } while (len);
5365
5366 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005367}
5368
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005369static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005370{
Avi Kivity70252a12010-01-19 12:51:22 +02005371 int handled = 0;
5372 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005373
Avi Kivity70252a12010-01-19 12:51:22 +02005374 do {
5375 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005376 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005377 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5378 addr, n, v))
5379 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005380 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005381 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005382 handled += n;
5383 addr += n;
5384 len -= n;
5385 v += n;
5386 } while (len);
5387
5388 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005389}
5390
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005391static void kvm_set_segment(struct kvm_vcpu *vcpu,
5392 struct kvm_segment *var, int seg)
5393{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005394 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005395}
5396
5397void kvm_get_segment(struct kvm_vcpu *vcpu,
5398 struct kvm_segment *var, int seg)
5399{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005400 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005401}
5402
Paolo Bonzini54987b72014-09-02 13:23:06 +02005403gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5404 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005405{
5406 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005407
5408 BUG_ON(!mmu_is_nested(vcpu));
5409
5410 /* NPT walks are always user-walks */
5411 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005412 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005413
5414 return t_gpa;
5415}
5416
Avi Kivityab9ae312010-11-22 17:53:26 +02005417gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5418 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005419{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005420 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005421 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005422}
5423
Avi Kivityab9ae312010-11-22 17:53:26 +02005424 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5425 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005426{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005427 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005428 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005429 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005430}
5431
Avi Kivityab9ae312010-11-22 17:53:26 +02005432gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5433 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005434{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005435 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005436 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005437 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005438}
5439
5440/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005441gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5442 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005443{
Avi Kivityab9ae312010-11-22 17:53:26 +02005444 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005445}
5446
5447static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5448 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005449 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005450{
5451 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005452 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005453
5454 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005455 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005456 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005457 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005458 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005459 int ret;
5460
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005461 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005462 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005463 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5464 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005465 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005466 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005467 goto out;
5468 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005469
Izik Eidus77c20022008-12-29 01:42:19 +02005470 bytes -= toread;
5471 data += toread;
5472 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005473 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005474out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005475 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005476}
Izik Eidus77c20022008-12-29 01:42:19 +02005477
Gleb Natapov1871c602010-02-10 14:21:32 +02005478/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005479static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5480 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005481 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005482{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005483 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005484 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005485 unsigned offset;
5486 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005487
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005488 /* Inline kvm_read_guest_virt_helper for speed. */
5489 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5490 exception);
5491 if (unlikely(gpa == UNMAPPED_GVA))
5492 return X86EMUL_PROPAGATE_FAULT;
5493
5494 offset = addr & (PAGE_SIZE-1);
5495 if (WARN_ON(offset + bytes > PAGE_SIZE))
5496 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005497 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5498 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005499 if (unlikely(ret < 0))
5500 return X86EMUL_IO_NEEDED;
5501
5502 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005503}
5504
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005505int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005506 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005507 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005508{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005509 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005510
Paolo Bonzini353c0952019-01-29 18:41:16 +01005511 /*
5512 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5513 * is returned, but our callers are not ready for that and they blindly
5514 * call kvm_inject_page_fault. Ensure that they at least do not leak
5515 * uninitialized kernel stack memory into cr2 and error code.
5516 */
5517 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005518 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005519 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005520}
Nadav Har'El064aea72011-05-25 23:04:56 +03005521EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005522
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005523static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5524 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005525 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005526{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005527 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005528 u32 access = 0;
5529
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005530 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005531 access |= PFERR_USER_MASK;
5532
5533 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005534}
5535
Radim Krčmář7a036a62015-10-30 16:36:24 +01005536static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5537 unsigned long addr, void *val, unsigned int bytes)
5538{
5539 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5540 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5541
5542 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5543}
5544
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005545static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5546 struct kvm_vcpu *vcpu, u32 access,
5547 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005548{
5549 void *data = val;
5550 int r = X86EMUL_CONTINUE;
5551
5552 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005553 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005554 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005555 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005556 unsigned offset = addr & (PAGE_SIZE-1);
5557 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5558 int ret;
5559
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005560 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005561 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005562 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005563 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005564 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005565 goto out;
5566 }
5567
5568 bytes -= towrite;
5569 data += towrite;
5570 addr += towrite;
5571 }
5572out:
5573 return r;
5574}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005575
5576static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005577 unsigned int bytes, struct x86_exception *exception,
5578 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005579{
5580 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005581 u32 access = PFERR_WRITE_MASK;
5582
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005583 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005584 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005585
5586 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005587 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005588}
5589
5590int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5591 unsigned int bytes, struct x86_exception *exception)
5592{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005593 /* kvm_write_guest_virt_system can pull in tons of pages. */
5594 vcpu->arch.l1tf_flush_l1d = true;
5595
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005596 /*
5597 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5598 * is returned, but our callers are not ready for that and they blindly
5599 * call kvm_inject_page_fault. Ensure that they at least do not leak
5600 * uninitialized kernel stack memory into cr2 and error code.
5601 */
5602 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005603 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5604 PFERR_WRITE_MASK, exception);
5605}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005606EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005607
Wanpeng Li082d06e2018-04-03 16:28:48 -07005608int handle_ud(struct kvm_vcpu *vcpu)
5609{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005610 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005611 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005612 char sig[5]; /* ud2; .ascii "kvm" */
5613 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005614
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005615 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005616 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5617 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005618 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005619 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005620 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005621 }
5622
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005623 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005624}
5625EXPORT_SYMBOL_GPL(handle_ud);
5626
Tom Lendacky0f89b202016-12-14 14:59:23 -05005627static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5628 gpa_t gpa, bool write)
5629{
5630 /* For APIC access vmexit */
5631 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5632 return 1;
5633
5634 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5635 trace_vcpu_match_mmio(gva, gpa, write, true);
5636 return 1;
5637 }
5638
5639 return 0;
5640}
5641
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005642static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5643 gpa_t *gpa, struct x86_exception *exception,
5644 bool write)
5645{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005646 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03005647 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005648
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005649 /*
5650 * currently PKRU is only applied to ept enabled guest so
5651 * there is no pkey in EPT page table for L1 guest or EPT
5652 * shadow page table for L2 guest.
5653 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005654 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005655 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005656 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005657 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5658 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005659 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005660 return 1;
5661 }
5662
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005663 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5664
5665 if (*gpa == UNMAPPED_GVA)
5666 return -1;
5667
Tom Lendacky0f89b202016-12-14 14:59:23 -05005668 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005669}
5670
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005671int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005672 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005673{
5674 int ret;
5675
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005676 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005677 if (ret < 0)
5678 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005679 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005680 return 1;
5681}
5682
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005683struct read_write_emulator_ops {
5684 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5685 int bytes);
5686 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5687 void *val, int bytes);
5688 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5689 int bytes, void *val);
5690 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5691 void *val, int bytes);
5692 bool write;
5693};
5694
5695static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5696{
5697 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005698 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005699 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005700 vcpu->mmio_read_completed = 0;
5701 return 1;
5702 }
5703
5704 return 0;
5705}
5706
5707static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5708 void *val, int bytes)
5709{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005710 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005711}
5712
5713static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5714 void *val, int bytes)
5715{
5716 return emulator_write_phys(vcpu, gpa, val, bytes);
5717}
5718
5719static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5720{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005721 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005722 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5723}
5724
5725static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5726 void *val, int bytes)
5727{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005728 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005729 return X86EMUL_IO_NEEDED;
5730}
5731
5732static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5733 void *val, int bytes)
5734{
Avi Kivityf78146b2012-04-18 19:22:47 +03005735 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5736
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005737 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005738 return X86EMUL_CONTINUE;
5739}
5740
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005741static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005742 .read_write_prepare = read_prepare,
5743 .read_write_emulate = read_emulate,
5744 .read_write_mmio = vcpu_mmio_read,
5745 .read_write_exit_mmio = read_exit_mmio,
5746};
5747
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005748static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005749 .read_write_emulate = write_emulate,
5750 .read_write_mmio = write_mmio,
5751 .read_write_exit_mmio = write_exit_mmio,
5752 .write = true,
5753};
5754
Xiao Guangrong22388a32011-07-13 14:32:31 +08005755static int emulator_read_write_onepage(unsigned long addr, void *val,
5756 unsigned int bytes,
5757 struct x86_exception *exception,
5758 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005759 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005760{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005761 gpa_t gpa;
5762 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005763 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005764 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005765 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005766
5767 /*
5768 * If the exit was due to a NPF we may already have a GPA.
5769 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5770 * Note, this cannot be used on string operations since string
5771 * operation using rep will only have the initial GPA from the NPF
5772 * occurred.
5773 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005774 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5775 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5776 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005777 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5778 } else {
5779 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5780 if (ret < 0)
5781 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005782 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005783
Brijesh Singh618232e2017-08-17 18:36:57 +02005784 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005785 return X86EMUL_CONTINUE;
5786
Carsten Ottebbd9b642007-10-30 18:44:21 +01005787 /*
5788 * Is this MMIO handled locally?
5789 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005790 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005791 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005792 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005793
Avi Kivity70252a12010-01-19 12:51:22 +02005794 gpa += handled;
5795 bytes -= handled;
5796 val += handled;
5797
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005798 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5799 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5800 frag->gpa = gpa;
5801 frag->data = val;
5802 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005803 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005804}
5805
Xiubo Li52eb5a62015-03-13 17:39:45 +08005806static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5807 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005808 void *val, unsigned int bytes,
5809 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005810 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005811{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005812 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005813 gpa_t gpa;
5814 int rc;
5815
5816 if (ops->read_write_prepare &&
5817 ops->read_write_prepare(vcpu, val, bytes))
5818 return X86EMUL_CONTINUE;
5819
5820 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005821
Carsten Ottebbd9b642007-10-30 18:44:21 +01005822 /* Crossing a page boundary? */
5823 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005824 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005825
5826 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005827 rc = emulator_read_write_onepage(addr, val, now, exception,
5828 vcpu, ops);
5829
Carsten Ottebbd9b642007-10-30 18:44:21 +01005830 if (rc != X86EMUL_CONTINUE)
5831 return rc;
5832 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005833 if (ctxt->mode != X86EMUL_MODE_PROT64)
5834 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005835 val += now;
5836 bytes -= now;
5837 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005838
Avi Kivityf78146b2012-04-18 19:22:47 +03005839 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5840 vcpu, ops);
5841 if (rc != X86EMUL_CONTINUE)
5842 return rc;
5843
5844 if (!vcpu->mmio_nr_fragments)
5845 return rc;
5846
5847 gpa = vcpu->mmio_fragments[0].gpa;
5848
5849 vcpu->mmio_needed = 1;
5850 vcpu->mmio_cur_fragment = 0;
5851
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005852 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005853 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5854 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5855 vcpu->run->mmio.phys_addr = gpa;
5856
5857 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005858}
5859
5860static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5861 unsigned long addr,
5862 void *val,
5863 unsigned int bytes,
5864 struct x86_exception *exception)
5865{
5866 return emulator_read_write(ctxt, addr, val, bytes,
5867 exception, &read_emultor);
5868}
5869
Xiubo Li52eb5a62015-03-13 17:39:45 +08005870static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005871 unsigned long addr,
5872 const void *val,
5873 unsigned int bytes,
5874 struct x86_exception *exception)
5875{
5876 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5877 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005878}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005879
Avi Kivitydaea3e72010-03-15 13:59:54 +02005880#define CMPXCHG_TYPE(t, ptr, old, new) \
5881 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5882
5883#ifdef CONFIG_X86_64
5884# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5885#else
5886# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005887 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005888#endif
5889
Avi Kivity0f65dd72011-04-20 13:37:53 +03005890static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5891 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005892 const void *old,
5893 const void *new,
5894 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005895 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005896{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005897 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005898 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005899 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005900 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005901 char *kaddr;
5902 bool exchanged;
5903
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005904 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005905 if (bytes > 8 || (bytes & (bytes - 1)))
5906 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005907
Avi Kivitydaea3e72010-03-15 13:59:54 +02005908 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005909
Avi Kivitydaea3e72010-03-15 13:59:54 +02005910 if (gpa == UNMAPPED_GVA ||
5911 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5912 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005913
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005914 /*
5915 * Emulate the atomic as a straight write to avoid #AC if SLD is
5916 * enabled in the host and the access splits a cache line.
5917 */
5918 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
5919 page_line_mask = ~(cache_line_size() - 1);
5920 else
5921 page_line_mask = PAGE_MASK;
5922
5923 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005924 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005925
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005926 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005927 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005928
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005929 kaddr = map.hva + offset_in_page(gpa);
5930
Avi Kivitydaea3e72010-03-15 13:59:54 +02005931 switch (bytes) {
5932 case 1:
5933 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5934 break;
5935 case 2:
5936 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5937 break;
5938 case 4:
5939 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5940 break;
5941 case 8:
5942 exchanged = CMPXCHG64(kaddr, old, new);
5943 break;
5944 default:
5945 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005946 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005947
5948 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005949
5950 if (!exchanged)
5951 return X86EMUL_CMPXCHG_FAILED;
5952
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005953 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005954
5955 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005956
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005957emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005958 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005959
Avi Kivity0f65dd72011-04-20 13:37:53 +03005960 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005961}
5962
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005963static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5964{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005965 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005966
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005967 for (i = 0; i < vcpu->arch.pio.count; i++) {
5968 if (vcpu->arch.pio.in)
5969 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5970 vcpu->arch.pio.size, pd);
5971 else
5972 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5973 vcpu->arch.pio.port, vcpu->arch.pio.size,
5974 pd);
5975 if (r)
5976 break;
5977 pd += vcpu->arch.pio.size;
5978 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005979 return r;
5980}
5981
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005982static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5983 unsigned short port, void *val,
5984 unsigned int count, bool in)
5985{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005986 vcpu->arch.pio.port = port;
5987 vcpu->arch.pio.in = in;
5988 vcpu->arch.pio.count = count;
5989 vcpu->arch.pio.size = size;
5990
5991 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5992 vcpu->arch.pio.count = 0;
5993 return 1;
5994 }
5995
5996 vcpu->run->exit_reason = KVM_EXIT_IO;
5997 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5998 vcpu->run->io.size = size;
5999 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6000 vcpu->run->io.count = count;
6001 vcpu->run->io.port = port;
6002
6003 return 0;
6004}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006005
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006006static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6007 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006008{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006009 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006010
Gleb Natapov79729952010-03-18 15:20:24 +02006011 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006012 goto data_avail;
6013
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006014 memset(vcpu->arch.pio_data, 0, size * count);
6015
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006016 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6017 if (ret) {
6018data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006019 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006020 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006021 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006022 return 1;
6023 }
6024
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006025 return 0;
6026}
6027
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006028static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6029 int size, unsigned short port, void *val,
6030 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006031{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006032 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006033
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006034}
6035
6036static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6037 unsigned short port, const void *val,
6038 unsigned int count)
6039{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006040 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006041 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006042 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006043}
6044
Carsten Ottebbd9b642007-10-30 18:44:21 +01006045static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6046 int size, unsigned short port,
6047 const void *val, unsigned int count)
6048{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006049 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006050}
6051
6052static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6053{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006054 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006055}
6056
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006057static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006058{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006059 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006060}
6061
Jiang Biaoae6a2372016-11-07 08:54:51 +08006062static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006063{
6064 if (!need_emulate_wbinvd(vcpu))
6065 return X86EMUL_CONTINUE;
6066
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006067 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006068 int cpu = get_cpu();
6069
6070 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006071 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6072 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006073 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006074 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006075 } else
6076 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006077 return X86EMUL_CONTINUE;
6078}
Joel Schopp5cb56052015-03-02 13:43:31 -06006079
6080int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6081{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006082 kvm_emulate_wbinvd_noskip(vcpu);
6083 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006084}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006085EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6086
Joel Schopp5cb56052015-03-02 13:43:31 -06006087
6088
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006089static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6090{
Joel Schopp5cb56052015-03-02 13:43:31 -06006091 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006092}
6093
Xiubo Li52eb5a62015-03-13 17:39:45 +08006094static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6095 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006096{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006097 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006098}
6099
Xiubo Li52eb5a62015-03-13 17:39:45 +08006100static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6101 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006102{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006103
Avi Kivity717746e2011-04-20 13:37:53 +03006104 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006105}
6106
Gleb Natapov52a46612010-03-18 15:20:03 +02006107static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6108{
6109 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6110}
6111
Avi Kivity717746e2011-04-20 13:37:53 +03006112static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006113{
Avi Kivity717746e2011-04-20 13:37:53 +03006114 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006115 unsigned long value;
6116
6117 switch (cr) {
6118 case 0:
6119 value = kvm_read_cr0(vcpu);
6120 break;
6121 case 2:
6122 value = vcpu->arch.cr2;
6123 break;
6124 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006125 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006126 break;
6127 case 4:
6128 value = kvm_read_cr4(vcpu);
6129 break;
6130 case 8:
6131 value = kvm_get_cr8(vcpu);
6132 break;
6133 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006134 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006135 return 0;
6136 }
6137
6138 return value;
6139}
6140
Avi Kivity717746e2011-04-20 13:37:53 +03006141static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006142{
Avi Kivity717746e2011-04-20 13:37:53 +03006143 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006144 int res = 0;
6145
Gleb Natapov52a46612010-03-18 15:20:03 +02006146 switch (cr) {
6147 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006148 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006149 break;
6150 case 2:
6151 vcpu->arch.cr2 = val;
6152 break;
6153 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006154 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006155 break;
6156 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006157 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006158 break;
6159 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006160 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006161 break;
6162 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006163 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006164 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006165 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006166
6167 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006168}
6169
Avi Kivity717746e2011-04-20 13:37:53 +03006170static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006171{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006172 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006173}
6174
Avi Kivity4bff1e862011-04-20 13:37:53 +03006175static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006176{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006177 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006178}
6179
Avi Kivity4bff1e862011-04-20 13:37:53 +03006180static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006181{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006182 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006183}
6184
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006185static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6186{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006187 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006188}
6189
6190static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6191{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006192 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006193}
6194
Avi Kivity4bff1e862011-04-20 13:37:53 +03006195static unsigned long emulator_get_cached_segment_base(
6196 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006197{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006198 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006199}
6200
Avi Kivity1aa36612011-04-27 13:20:30 +03006201static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6202 struct desc_struct *desc, u32 *base3,
6203 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006204{
6205 struct kvm_segment var;
6206
Avi Kivity4bff1e862011-04-20 13:37:53 +03006207 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006208 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006209
Gleb Natapov378a8b02013-01-21 15:36:48 +02006210 if (var.unusable) {
6211 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006212 if (base3)
6213 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006214 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006215 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006216
6217 if (var.g)
6218 var.limit >>= 12;
6219 set_desc_limit(desc, var.limit);
6220 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006221#ifdef CONFIG_X86_64
6222 if (base3)
6223 *base3 = var.base >> 32;
6224#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006225 desc->type = var.type;
6226 desc->s = var.s;
6227 desc->dpl = var.dpl;
6228 desc->p = var.present;
6229 desc->avl = var.avl;
6230 desc->l = var.l;
6231 desc->d = var.db;
6232 desc->g = var.g;
6233
6234 return true;
6235}
6236
Avi Kivity1aa36612011-04-27 13:20:30 +03006237static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6238 struct desc_struct *desc, u32 base3,
6239 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006240{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006241 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006242 struct kvm_segment var;
6243
Avi Kivity1aa36612011-04-27 13:20:30 +03006244 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006245 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006246#ifdef CONFIG_X86_64
6247 var.base |= ((u64)base3) << 32;
6248#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006249 var.limit = get_desc_limit(desc);
6250 if (desc->g)
6251 var.limit = (var.limit << 12) | 0xfff;
6252 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006253 var.dpl = desc->dpl;
6254 var.db = desc->d;
6255 var.s = desc->s;
6256 var.l = desc->l;
6257 var.g = desc->g;
6258 var.avl = desc->avl;
6259 var.present = desc->p;
6260 var.unusable = !var.present;
6261 var.padding = 0;
6262
6263 kvm_set_segment(vcpu, &var, seg);
6264 return;
6265}
6266
Avi Kivity717746e2011-04-20 13:37:53 +03006267static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6268 u32 msr_index, u64 *pdata)
6269{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006270 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006271}
6272
6273static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6274 u32 msr_index, u64 data)
6275{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006276 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006277}
6278
Paolo Bonzini64d60672015-05-07 11:36:11 +02006279static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6280{
6281 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6282
6283 return vcpu->arch.smbase;
6284}
6285
6286static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6287{
6288 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6289
6290 vcpu->arch.smbase = smbase;
6291}
6292
Nadav Amit67f4d422014-06-02 18:34:09 +03006293static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6294 u32 pmc)
6295{
Like Xu98ff80f2019-10-27 18:52:40 +08006296 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006297}
6298
Avi Kivity222d21a2011-11-10 14:57:30 +02006299static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6300 u32 pmc, u64 *pdata)
6301{
Wei Huangc6702c92015-06-19 13:44:45 +02006302 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006303}
6304
Avi Kivity6c3287f2011-04-20 15:43:05 +03006305static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6306{
6307 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6308}
6309
Avi Kivity29535382011-04-20 13:37:53 +03006310static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006311 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006312 enum x86_intercept_stage stage)
6313{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006314 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006315 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006316}
6317
Yu Zhange911eb32017-08-24 20:27:52 +08006318static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006319 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6320 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006321{
Sean Christophersonf91af512020-03-04 17:34:37 -08006322 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006323}
6324
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006325static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6326{
6327 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6328}
6329
6330static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6331{
6332 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6333}
6334
6335static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6336{
6337 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6338}
6339
Avi Kivitydd856ef2012-08-27 23:46:17 +03006340static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6341{
6342 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6343}
6344
6345static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6346{
6347 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6348}
6349
Nadav Amit801806d2015-01-26 09:32:23 +02006350static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6351{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006352 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006353}
6354
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006355static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6356{
6357 return emul_to_vcpu(ctxt)->arch.hflags;
6358}
6359
6360static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6361{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006362 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006363}
6364
Sean Christophersoned193212019-04-02 08:03:09 -07006365static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6366 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006367{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006368 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006369}
6370
Sean Christophersonc5833c72019-04-02 08:03:10 -07006371static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6372{
6373 kvm_smm_changed(emul_to_vcpu(ctxt));
6374}
6375
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006376static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6377{
6378 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6379}
6380
Mathias Krause0225fb52012-08-30 01:30:16 +02006381static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006382 .read_gpr = emulator_read_gpr,
6383 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006384 .read_std = emulator_read_std,
6385 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006386 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006387 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006388 .read_emulated = emulator_read_emulated,
6389 .write_emulated = emulator_write_emulated,
6390 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006391 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006392 .pio_in_emulated = emulator_pio_in_emulated,
6393 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006394 .get_segment = emulator_get_segment,
6395 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006396 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006397 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006398 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006399 .set_gdt = emulator_set_gdt,
6400 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006401 .get_cr = emulator_get_cr,
6402 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006403 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006404 .get_dr = emulator_get_dr,
6405 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006406 .get_smbase = emulator_get_smbase,
6407 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006408 .set_msr = emulator_set_msr,
6409 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006410 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006411 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006412 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006413 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006414 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006415 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006416 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006417 .guest_has_long_mode = emulator_guest_has_long_mode,
6418 .guest_has_movbe = emulator_guest_has_movbe,
6419 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006420 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006421 .get_hflags = emulator_get_hflags,
6422 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006423 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006424 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006425 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006426};
6427
Gleb Natapov95cb2292010-04-28 19:15:43 +03006428static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6429{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006430 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006431 /*
6432 * an sti; sti; sequence only disable interrupts for the first
6433 * instruction. So, if the last instruction, be it emulated or
6434 * not, left the system with the INT_STI flag enabled, it
6435 * means that the last instruction is an sti. We should not
6436 * leave the flag on in this case. The same goes for mov ss
6437 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006438 if (int_shadow & mask)
6439 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006440 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006441 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006442 if (!mask)
6443 kvm_make_request(KVM_REQ_EVENT, vcpu);
6444 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006445}
6446
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006447static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006448{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006449 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006450 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07006451 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006452
6453 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006454 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6455 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006456 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006457 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006458 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006459}
6460
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006461static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6462{
6463 struct x86_emulate_ctxt *ctxt;
6464
6465 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6466 if (!ctxt) {
6467 pr_err("kvm: failed to allocate vcpu's emulator\n");
6468 return NULL;
6469 }
6470
6471 ctxt->vcpu = vcpu;
6472 ctxt->ops = &emulate_ops;
6473 vcpu->arch.emulate_ctxt = ctxt;
6474
6475 return ctxt;
6476}
6477
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006478static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6479{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006480 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006481 int cs_db, cs_l;
6482
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006483 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006484
Sean Christopherson744e6992020-02-18 15:03:09 -08006485 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006486 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006487 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6488
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006489 ctxt->eip = kvm_rip_read(vcpu);
6490 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6491 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006492 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006493 cs_db ? X86EMUL_MODE_PROT32 :
6494 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006495 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006496 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6497 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006498
Avi Kivitydd856ef2012-08-27 23:46:17 +03006499 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006500 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006501}
6502
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006503void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006504{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006505 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006506 int ret;
6507
6508 init_emulate_ctxt(vcpu);
6509
Avi Kivity9dac77f2011-06-01 15:34:25 +03006510 ctxt->op_bytes = 2;
6511 ctxt->ad_bytes = 2;
6512 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006513 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006514
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006515 if (ret != X86EMUL_CONTINUE) {
6516 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6517 } else {
6518 ctxt->eip = ctxt->_eip;
6519 kvm_rip_write(vcpu, ctxt->eip);
6520 kvm_set_rflags(vcpu, ctxt->eflags);
6521 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006522}
6523EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6524
Liran Alone2366172018-03-12 13:12:49 +02006525static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006526{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006527 ++vcpu->stat.insn_emulation_fail;
6528 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006529
Sean Christopherson42cbf062019-08-27 14:40:31 -07006530 if (emulation_type & EMULTYPE_VMWARE_GP) {
6531 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006532 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006533 }
Liran Alone2366172018-03-12 13:12:49 +02006534
Sean Christopherson738fece2019-08-27 14:40:34 -07006535 if (emulation_type & EMULTYPE_SKIP) {
6536 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6537 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6538 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006539 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006540 }
6541
Sean Christopherson22da61c2019-08-27 14:40:28 -07006542 kvm_queue_exception(vcpu, UD_VECTOR);
6543
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006544 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006545 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6546 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6547 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006548 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006549 }
Liran Alone2366172018-03-12 13:12:49 +02006550
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006551 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006552}
6553
Sean Christopherson736c2912019-12-06 15:57:14 -08006554static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006555 bool write_fault_to_shadow_pgtable,
6556 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006557{
Sean Christopherson736c2912019-12-06 15:57:14 -08006558 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006559 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006560
Sean Christopherson92daa482020-02-18 15:03:08 -08006561 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006562 return false;
6563
Sean Christopherson92daa482020-02-18 15:03:08 -08006564 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6565 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006566 return false;
6567
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006568 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006569 /*
6570 * Write permission should be allowed since only
6571 * write access need to be emulated.
6572 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006573 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006574
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006575 /*
6576 * If the mapping is invalid in guest, let cpu retry
6577 * it to generate fault.
6578 */
6579 if (gpa == UNMAPPED_GVA)
6580 return true;
6581 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006582
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006583 /*
6584 * Do not retry the unhandleable instruction if it faults on the
6585 * readonly host memory, otherwise it will goto a infinite loop:
6586 * retry instruction -> write #PF -> emulation fail -> retry
6587 * instruction -> ...
6588 */
6589 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006590
6591 /*
6592 * If the instruction failed on the error pfn, it can not be fixed,
6593 * report the error to userspace.
6594 */
6595 if (is_error_noslot_pfn(pfn))
6596 return false;
6597
6598 kvm_release_pfn_clean(pfn);
6599
6600 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006601 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006602 unsigned int indirect_shadow_pages;
6603
6604 spin_lock(&vcpu->kvm->mmu_lock);
6605 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6606 spin_unlock(&vcpu->kvm->mmu_lock);
6607
6608 if (indirect_shadow_pages)
6609 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6610
Gleb Natapova6f177e2010-07-08 12:41:12 +03006611 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006612 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006613
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006614 /*
6615 * if emulation was due to access to shadowed page table
6616 * and it failed try to unshadow page and re-enter the
6617 * guest to let CPU execute the instruction.
6618 */
6619 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006620
6621 /*
6622 * If the access faults on its page table, it can not
6623 * be fixed by unprotecting shadow page and it should
6624 * be reported to userspace.
6625 */
6626 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006627}
6628
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006629static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006630 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006631{
6632 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006633 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006634
6635 last_retry_eip = vcpu->arch.last_retry_eip;
6636 last_retry_addr = vcpu->arch.last_retry_addr;
6637
6638 /*
6639 * If the emulation is caused by #PF and it is non-page_table
6640 * writing instruction, it means the VM-EXIT is caused by shadow
6641 * page protected, we can zap the shadow page and retry this
6642 * instruction directly.
6643 *
6644 * Note: if the guest uses a non-page-table modifying instruction
6645 * on the PDE that points to the instruction, then we will unmap
6646 * the instruction and go to an infinite loop. So, we cache the
6647 * last retried eip and the last fault address, if we meet the eip
6648 * and the address again, we can break out of the potential infinite
6649 * loop.
6650 */
6651 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6652
Sean Christopherson92daa482020-02-18 15:03:08 -08006653 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006654 return false;
6655
Sean Christopherson92daa482020-02-18 15:03:08 -08006656 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6657 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006658 return false;
6659
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006660 if (x86_page_table_writing_insn(ctxt))
6661 return false;
6662
Sean Christopherson736c2912019-12-06 15:57:14 -08006663 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006664 return false;
6665
6666 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006667 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006668
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006669 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006670 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006671
Xiao Guangrong22368022013-01-13 23:44:12 +08006672 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006673
6674 return true;
6675}
6676
Gleb Natapov716d51a2012-09-03 15:24:26 +03006677static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6678static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6679
Paolo Bonzini64d60672015-05-07 11:36:11 +02006680static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006681{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006682 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006683 /* This is a good place to trace that we are exiting SMM. */
6684 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6685
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006686 /* Process a latched INIT or SMI, if any. */
6687 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006688 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006689
6690 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006691}
6692
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006693static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6694 unsigned long *db)
6695{
6696 u32 dr6 = 0;
6697 int i;
6698 u32 enable, rwlen;
6699
6700 enable = dr7;
6701 rwlen = dr7 >> 16;
6702 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6703 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6704 dr6 |= (1 << i);
6705 return dr6;
6706}
6707
Sean Christopherson120c2c42019-08-27 14:40:29 -07006708static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006709{
6710 struct kvm_run *kvm_run = vcpu->run;
6711
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006712 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6713 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
Peter Xud5d260c2020-05-05 16:49:59 -04006714 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006715 kvm_run->debug.arch.exception = DB_VECTOR;
6716 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006717 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006718 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006719 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006720 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006721}
6722
Kyle Huey6affcbe2016-11-29 12:40:40 -08006723int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6724{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006725 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006726 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006727
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006728 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006729 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006730 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006731
6732 /*
6733 * rflags is the old, "raw" value of the flags. The new value has
6734 * not been saved yet.
6735 *
6736 * This is correct even for TF set by the guest, because "the
6737 * processor will not generate this exception after the instruction
6738 * that sets the TF flag".
6739 */
6740 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006741 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006742 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006743}
6744EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6745
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006746static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6747{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006748 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6749 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006750 struct kvm_run *kvm_run = vcpu->run;
6751 unsigned long eip = kvm_get_linear_rip(vcpu);
6752 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006753 vcpu->arch.guest_debug_dr7,
6754 vcpu->arch.eff_db);
6755
6756 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006757 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006758 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006759 kvm_run->debug.arch.exception = DB_VECTOR;
6760 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006761 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006762 return true;
6763 }
6764 }
6765
Nadav Amit4161a562014-07-17 01:19:31 +03006766 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6767 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006768 unsigned long eip = kvm_get_linear_rip(vcpu);
6769 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006770 vcpu->arch.dr7,
6771 vcpu->arch.db);
6772
6773 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04006774 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006775 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006776 return true;
6777 }
6778 }
6779
6780 return false;
6781}
6782
Liran Alon04789b62018-03-12 13:12:50 +02006783static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6784{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006785 switch (ctxt->opcode_len) {
6786 case 1:
6787 switch (ctxt->b) {
6788 case 0xe4: /* IN */
6789 case 0xe5:
6790 case 0xec:
6791 case 0xed:
6792 case 0xe6: /* OUT */
6793 case 0xe7:
6794 case 0xee:
6795 case 0xef:
6796 case 0x6c: /* INS */
6797 case 0x6d:
6798 case 0x6e: /* OUTS */
6799 case 0x6f:
6800 return true;
6801 }
6802 break;
6803 case 2:
6804 switch (ctxt->b) {
6805 case 0x33: /* RDPMC */
6806 return true;
6807 }
6808 break;
Liran Alon04789b62018-03-12 13:12:50 +02006809 }
6810
6811 return false;
6812}
6813
Sean Christopherson736c2912019-12-06 15:57:14 -08006814int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6815 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006816{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006817 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006818 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006819 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006820 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006821
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006822 vcpu->arch.l1tf_flush_l1d = true;
6823
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006824 /*
6825 * Clear write_fault_to_shadow_pgtable here to ensure it is
6826 * never reused.
6827 */
6828 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006829 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006830
Sheng Yang571008d2008-01-02 14:49:22 +08006831 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006832 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006833
6834 /*
6835 * We will reenter on the same instruction since
6836 * we do not set complete_userspace_io. This does not
6837 * handle watchpoints yet, those would be handled in
6838 * the emulate_ops.
6839 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006840 if (!(emulation_type & EMULTYPE_SKIP) &&
6841 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006842 return r;
6843
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006844 ctxt->interruptibility = 0;
6845 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006846 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006847 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006848
Borislav Petkovb51e9742013-09-22 16:44:52 +02006849 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006850
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006851 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006852
Avi Kivitye46479f2010-04-11 13:05:16 +03006853 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006854 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006855 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006856 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006857 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6858 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006859 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006860 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006861 if (reexecute_instruction(vcpu, cr2_or_gpa,
6862 write_fault_to_spt,
6863 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006864 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006865 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006866 /*
6867 * #UD should result in just EMULATION_FAILED, and trap-like
6868 * exception should not be encountered during decode.
6869 */
6870 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6871 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006872 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006873 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006874 }
Liran Alone2366172018-03-12 13:12:49 +02006875 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006876 }
6877 }
6878
Sean Christopherson42cbf062019-08-27 14:40:31 -07006879 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6880 !is_vmware_backdoor_opcode(ctxt)) {
6881 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006882 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006883 }
Liran Alon04789b62018-03-12 13:12:50 +02006884
Sean Christopherson1957aa62019-08-27 14:40:39 -07006885 /*
6886 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6887 * for kvm_skip_emulated_instruction(). The caller is responsible for
6888 * updating interruptibility state and injecting single-step #DBs.
6889 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006890 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006891 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006892 if (ctxt->eflags & X86_EFLAGS_RF)
6893 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006894 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006895 }
6896
Sean Christopherson736c2912019-12-06 15:57:14 -08006897 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006898 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006899
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006900 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006901 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006902 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6903 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006904 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006905 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006906
Gleb Natapov5cd21912010-03-18 15:20:26 +02006907restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006908 if (emulation_type & EMULTYPE_PF) {
6909 /* Save the faulting GPA (cr2) in the address field */
6910 ctxt->exception.address = cr2_or_gpa;
6911
6912 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6913 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006914 ctxt->gpa_available = true;
6915 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006916 }
6917 } else {
6918 /* Sanitize the address out of an abundance of paranoia. */
6919 ctxt->exception.address = 0;
6920 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006921
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006922 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006923
Joerg Roedel775fde82011-04-04 12:39:24 +02006924 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006925 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006926
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006927 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006928 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006929 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006930 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006931
Liran Alone2366172018-03-12 13:12:49 +02006932 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006933 }
6934
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006935 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006936 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006937 if (inject_emulated_exception(vcpu))
6938 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006939 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006940 if (!vcpu->arch.pio.in) {
6941 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006942 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006943 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006944 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006945 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6946 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006947 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006948 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006949 ++vcpu->stat.mmio_exits;
6950
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006951 if (!vcpu->mmio_is_write)
6952 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006953 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006954 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006955 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006956 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006957 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006958 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006959
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006960 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006961 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006962 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006963 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006964 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006965 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6966 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006967 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006968 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006969 if (kvm_x86_ops.update_emulated_instruction)
6970 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006971 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006972 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006973
6974 /*
6975 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6976 * do nothing, and it will be requested again as soon as
6977 * the shadow expires. But we still need to check here,
6978 * because POPF has no interrupt shadow.
6979 */
6980 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6981 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006982 } else
6983 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006984
Gleb Natapove85d28f2010-07-29 15:11:52 +03006985 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006986}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006987
6988int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6989{
6990 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6991}
6992EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6993
6994int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6995 void *insn, int insn_len)
6996{
6997 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6998}
6999EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007000
Sean Christopherson8764ed52019-04-29 07:04:15 -07007001static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7002{
7003 vcpu->arch.pio.count = 0;
7004 return 1;
7005}
7006
Sean Christopherson45def772019-03-11 20:01:05 -07007007static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7008{
7009 vcpu->arch.pio.count = 0;
7010
7011 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7012 return 1;
7013
7014 return kvm_skip_emulated_instruction(vcpu);
7015}
7016
Sean Christophersondca7f122018-03-08 08:57:27 -08007017static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7018 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007019{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007020 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007021 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7022
Sean Christopherson8764ed52019-04-29 07:04:15 -07007023 if (ret)
7024 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007025
Sean Christopherson8764ed52019-04-29 07:04:15 -07007026 /*
7027 * Workaround userspace that relies on old KVM behavior of %rip being
7028 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7029 */
7030 if (port == 0x7e &&
7031 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7032 vcpu->arch.complete_userspace_io =
7033 complete_fast_pio_out_port_0x7e;
7034 kvm_skip_emulated_instruction(vcpu);
7035 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007036 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7037 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7038 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007039 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007040}
Carsten Ottede7d7892007-10-30 18:44:25 +01007041
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007042static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7043{
7044 unsigned long val;
7045
7046 /* We should only ever be called with arch.pio.count equal to 1 */
7047 BUG_ON(vcpu->arch.pio.count != 1);
7048
Sean Christopherson45def772019-03-11 20:01:05 -07007049 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7050 vcpu->arch.pio.count = 0;
7051 return 1;
7052 }
7053
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007054 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007055 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007056
7057 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007058 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007059 * the copy and tracing
7060 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007061 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007062 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007063
Sean Christopherson45def772019-03-11 20:01:05 -07007064 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007065}
7066
Sean Christophersondca7f122018-03-08 08:57:27 -08007067static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7068 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007069{
7070 unsigned long val;
7071 int ret;
7072
7073 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007074 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007075
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007076 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007077 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007078 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007079 return ret;
7080 }
7081
Sean Christopherson45def772019-03-11 20:01:05 -07007082 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007083 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7084
7085 return 0;
7086}
Sean Christophersondca7f122018-03-08 08:57:27 -08007087
7088int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7089{
Sean Christopherson45def772019-03-11 20:01:05 -07007090 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007091
Sean Christophersondca7f122018-03-08 08:57:27 -08007092 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007093 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007094 else
Sean Christopherson45def772019-03-11 20:01:05 -07007095 ret = kvm_fast_pio_out(vcpu, size, port);
7096 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007097}
7098EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007099
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007100static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007101{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007102 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007103 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007104}
7105
7106static void tsc_khz_changed(void *data)
7107{
7108 struct cpufreq_freqs *freq = data;
7109 unsigned long khz = 0;
7110
7111 if (data)
7112 khz = freq->new;
7113 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7114 khz = cpufreq_quick_get(raw_smp_processor_id());
7115 if (!khz)
7116 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007117 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007118}
7119
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007120#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007121static void kvm_hyperv_tsc_notifier(void)
7122{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007123 struct kvm *kvm;
7124 struct kvm_vcpu *vcpu;
7125 int cpu;
7126
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007127 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007128 list_for_each_entry(kvm, &vm_list, vm_list)
7129 kvm_make_mclock_inprogress_request(kvm);
7130
7131 hyperv_stop_tsc_emulation();
7132
7133 /* TSC frequency always matches when on Hyper-V */
7134 for_each_present_cpu(cpu)
7135 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7136 kvm_max_guest_tsc_khz = tsc_khz;
7137
7138 list_for_each_entry(kvm, &vm_list, vm_list) {
7139 struct kvm_arch *ka = &kvm->arch;
7140
7141 spin_lock(&ka->pvclock_gtod_sync_lock);
7142
7143 pvclock_update_vm_gtod_copy(kvm);
7144
7145 kvm_for_each_vcpu(cpu, vcpu, kvm)
7146 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7147
7148 kvm_for_each_vcpu(cpu, vcpu, kvm)
7149 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7150
7151 spin_unlock(&ka->pvclock_gtod_sync_lock);
7152 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007153 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007154}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007155#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007156
Viresh Kumardf240142019-04-29 15:03:58 +05307157static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007158{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007159 struct kvm *kvm;
7160 struct kvm_vcpu *vcpu;
7161 int i, send_ipi = 0;
7162
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007163 /*
7164 * We allow guests to temporarily run on slowing clocks,
7165 * provided we notify them after, or to run on accelerating
7166 * clocks, provided we notify them before. Thus time never
7167 * goes backwards.
7168 *
7169 * However, we have a problem. We can't atomically update
7170 * the frequency of a given CPU from this function; it is
7171 * merely a notifier, which can be called from any CPU.
7172 * Changing the TSC frequency at arbitrary points in time
7173 * requires a recomputation of local variables related to
7174 * the TSC for each VCPU. We must flag these local variables
7175 * to be updated and be sure the update takes place with the
7176 * new frequency before any guests proceed.
7177 *
7178 * Unfortunately, the combination of hotplug CPU and frequency
7179 * change creates an intractable locking scenario; the order
7180 * of when these callouts happen is undefined with respect to
7181 * CPU hotplug, and they can race with each other. As such,
7182 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7183 * undefined; you can actually have a CPU frequency change take
7184 * place in between the computation of X and the setting of the
7185 * variable. To protect against this problem, all updates of
7186 * the per_cpu tsc_khz variable are done in an interrupt
7187 * protected IPI, and all callers wishing to update the value
7188 * must wait for a synchronous IPI to complete (which is trivial
7189 * if the caller is on the CPU already). This establishes the
7190 * necessary total order on variable updates.
7191 *
7192 * Note that because a guest time update may take place
7193 * anytime after the setting of the VCPU's request bit, the
7194 * correct TSC value must be set before the request. However,
7195 * to ensure the update actually makes it to any guest which
7196 * starts running in hardware virtualization between the set
7197 * and the acquisition of the spinlock, we must also ping the
7198 * CPU after setting the request bit.
7199 *
7200 */
7201
Viresh Kumardf240142019-04-29 15:03:58 +05307202 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007203
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007204 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007205 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007206 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307207 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007208 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007209 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007210 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007211 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007212 }
7213 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007214 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007215
7216 if (freq->old < freq->new && send_ipi) {
7217 /*
7218 * We upscale the frequency. Must make the guest
7219 * doesn't see old kvmclock values while running with
7220 * the new frequency, otherwise we risk the guest sees
7221 * time go backwards.
7222 *
7223 * In case we update the frequency for another cpu
7224 * (which might be in guest context) send an interrupt
7225 * to kick the cpu out of guest context. Next time
7226 * guest context is entered kvmclock will be updated,
7227 * so the guest will not see stale values.
7228 */
Viresh Kumardf240142019-04-29 15:03:58 +05307229 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007230 }
Viresh Kumardf240142019-04-29 15:03:58 +05307231}
7232
7233static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7234 void *data)
7235{
7236 struct cpufreq_freqs *freq = data;
7237 int cpu;
7238
7239 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7240 return 0;
7241 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7242 return 0;
7243
7244 for_each_cpu(cpu, freq->policy->cpus)
7245 __kvmclock_cpufreq_notifier(freq, cpu);
7246
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007247 return 0;
7248}
7249
7250static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007251 .notifier_call = kvmclock_cpufreq_notifier
7252};
7253
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007254static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007255{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007256 tsc_khz_changed(NULL);
7257 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007258}
7259
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007260static void kvm_timer_init(void)
7261{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007262 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307263
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007264 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007265#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007266 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007267 int cpu;
7268
Avi Kivity3e26f232010-12-16 12:16:34 +02007269 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007270 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007271 if (policy) {
7272 if (policy->cpuinfo.max_freq)
7273 max_tsc_khz = policy->cpuinfo.max_freq;
7274 cpufreq_cpu_put(policy);
7275 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007276 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007277#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007278 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7279 CPUFREQ_TRANSITION_NOTIFIER);
7280 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307281
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007282 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007283 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007284}
7285
Andi Kleendd60d212017-07-25 17:20:32 -07007286DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7287EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007288
Gleb Natapovf5132b02011-11-10 14:57:22 +02007289int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007290{
Alex,Shi086c9852011-10-20 15:34:01 +08007291 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007292}
7293
7294static int kvm_is_user_mode(void)
7295{
7296 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007297
Alex,Shi086c9852011-10-20 15:34:01 +08007298 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007299 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007300
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007301 return user_mode != 0;
7302}
7303
7304static unsigned long kvm_get_guest_ip(void)
7305{
7306 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007307
Alex,Shi086c9852011-10-20 15:34:01 +08007308 if (__this_cpu_read(current_vcpu))
7309 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007310
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007311 return ip;
7312}
7313
Luwei Kang8479e042019-02-18 19:26:07 -05007314static void kvm_handle_intel_pt_intr(void)
7315{
7316 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7317
7318 kvm_make_request(KVM_REQ_PMI, vcpu);
7319 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7320 (unsigned long *)&vcpu->arch.pmu.global_status);
7321}
7322
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007323static struct perf_guest_info_callbacks kvm_guest_cbs = {
7324 .is_in_guest = kvm_is_in_guest,
7325 .is_user_mode = kvm_is_user_mode,
7326 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007327 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007328};
7329
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007330#ifdef CONFIG_X86_64
7331static void pvclock_gtod_update_fn(struct work_struct *work)
7332{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007333 struct kvm *kvm;
7334
7335 struct kvm_vcpu *vcpu;
7336 int i;
7337
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007338 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007339 list_for_each_entry(kvm, &vm_list, vm_list)
7340 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007341 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007342 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007343 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007344}
7345
7346static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7347
7348/*
7349 * Notification about pvclock gtod data update.
7350 */
7351static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7352 void *priv)
7353{
7354 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7355 struct timekeeper *tk = priv;
7356
7357 update_pvclock_gtod(tk);
7358
7359 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007360 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007361 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007362 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007363 atomic_read(&kvm_guest_has_master_clock) != 0)
7364 queue_work(system_long_wq, &pvclock_gtod_work);
7365
7366 return 0;
7367}
7368
7369static struct notifier_block pvclock_gtod_notifier = {
7370 .notifier_call = pvclock_gtod_notify,
7371};
7372#endif
7373
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007374int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007375{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007376 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007377 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007378
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007379 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007380 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007381 r = -EEXIST;
7382 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007383 }
7384
7385 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007386 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007387 r = -EOPNOTSUPP;
7388 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007389 }
7390 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007391 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007392 r = -EOPNOTSUPP;
7393 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007394 }
7395
Marc Orrb666a4b2018-11-06 14:53:56 -08007396 /*
7397 * KVM explicitly assumes that the guest has an FPU and
7398 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7399 * vCPU's FPU state as a fxregs_state struct.
7400 */
7401 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7402 printk(KERN_ERR "kvm: inadequate fpu\n");
7403 r = -EOPNOTSUPP;
7404 goto out;
7405 }
7406
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007407 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007408 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007409 __alignof__(struct fpu), SLAB_ACCOUNT,
7410 NULL);
7411 if (!x86_fpu_cache) {
7412 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7413 goto out;
7414 }
7415
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007416 x86_emulator_cache = kvm_alloc_emulator_cache();
7417 if (!x86_emulator_cache) {
7418 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7419 goto out_free_x86_fpu_cache;
7420 }
7421
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007422 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7423 if (!shared_msrs) {
7424 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007425 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007426 }
7427
Avi Kivity97db56c2008-01-13 13:23:56 +02007428 r = kvm_mmu_module_init();
7429 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007430 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007431
Sheng Yang7b523452008-04-25 21:13:50 +08007432 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007433 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007434 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007435 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007436
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007437 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7438
Sean Christophersoncfc48182020-03-02 15:56:23 -08007439 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007440 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007441 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7442 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007443
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007444 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007445 if (pi_inject_timer == -1)
7446 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007447#ifdef CONFIG_X86_64
7448 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007449
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007450 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007451 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007452#endif
7453
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007454 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007455
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007456out_free_percpu:
7457 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007458out_free_x86_emulator_cache:
7459 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007460out_free_x86_fpu_cache:
7461 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007462out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007463 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007464}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007465
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007466void kvm_arch_exit(void)
7467{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007468#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007469 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007470 clear_hv_tscchange_cb();
7471#endif
David Matlackcef84c32016-12-16 14:30:36 -08007472 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007473 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7474
Jan Kiszka888d2562009-04-17 19:24:58 +02007475 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7476 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7477 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007478 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007479#ifdef CONFIG_X86_64
7480 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7481#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007482 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007483 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007484 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007485 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007486}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007487
Joel Schopp5cb56052015-03-02 13:43:31 -06007488int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007489{
7490 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007491 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007492 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007493 return 1;
7494 } else {
7495 vcpu->run->exit_reason = KVM_EXIT_HLT;
7496 return 0;
7497 }
7498}
Joel Schopp5cb56052015-03-02 13:43:31 -06007499EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7500
7501int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7502{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007503 int ret = kvm_skip_emulated_instruction(vcpu);
7504 /*
7505 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7506 * KVM_EXIT_DEBUG here.
7507 */
7508 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007509}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007510EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7511
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007512#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007513static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7514 unsigned long clock_type)
7515{
7516 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007517 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007518 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007519 int ret;
7520
7521 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7522 return -KVM_EOPNOTSUPP;
7523
7524 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7525 return -KVM_EOPNOTSUPP;
7526
7527 clock_pairing.sec = ts.tv_sec;
7528 clock_pairing.nsec = ts.tv_nsec;
7529 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7530 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007531 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007532
7533 ret = 0;
7534 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7535 sizeof(struct kvm_clock_pairing)))
7536 ret = -KVM_EFAULT;
7537
7538 return ret;
7539}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007540#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007541
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307542/*
7543 * kvm_pv_kick_cpu_op: Kick a vcpu.
7544 *
7545 * @apicid - apicid of vcpu to be kicked.
7546 */
7547static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7548{
Raghavendra K T24d21662013-08-26 14:18:35 +05307549 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307550
Peter Xu150a84f2019-12-04 20:07:21 +01007551 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007552 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007553 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307554 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007555 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307556
Raghavendra K T24d21662013-08-26 14:18:35 +05307557 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007558 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307559}
7560
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007561bool kvm_apicv_activated(struct kvm *kvm)
7562{
7563 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7564}
7565EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7566
7567void kvm_apicv_init(struct kvm *kvm, bool enable)
7568{
7569 if (enable)
7570 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7571 &kvm->arch.apicv_inhibit_reasons);
7572 else
7573 set_bit(APICV_INHIBIT_REASON_DISABLE,
7574 &kvm->arch.apicv_inhibit_reasons);
7575}
7576EXPORT_SYMBOL_GPL(kvm_apicv_init);
7577
Wanpeng Li71506292019-06-11 20:23:49 +08007578static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7579{
7580 struct kvm_vcpu *target = NULL;
7581 struct kvm_apic_map *map;
7582
7583 rcu_read_lock();
7584 map = rcu_dereference(kvm->arch.apic_map);
7585
7586 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7587 target = map->phys_map[dest_id]->vcpu;
7588
7589 rcu_read_unlock();
7590
Wanpeng Li266e85a2019-07-24 17:43:13 +08007591 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007592 kvm_vcpu_yield_to(target);
7593}
7594
Hollis Blanchard8776e512007-10-31 17:24:24 -05007595int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7596{
7597 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007598 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007599
Radim Krčmář696ca772018-05-24 17:50:56 +02007600 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7601 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007602
Sean Christophersonde3cd112019-04-30 10:36:17 -07007603 nr = kvm_rax_read(vcpu);
7604 a0 = kvm_rbx_read(vcpu);
7605 a1 = kvm_rcx_read(vcpu);
7606 a2 = kvm_rdx_read(vcpu);
7607 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007608
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007609 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007610
Nadav Amita449c7a2014-06-18 17:19:24 +03007611 op_64_bit = is_64_bit_mode(vcpu);
7612 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007613 nr &= 0xFFFFFFFF;
7614 a0 &= 0xFFFFFFFF;
7615 a1 &= 0xFFFFFFFF;
7616 a2 &= 0xFFFFFFFF;
7617 a3 &= 0xFFFFFFFF;
7618 }
7619
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007620 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02007621 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007622 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007623 }
7624
Hollis Blanchard8776e512007-10-31 17:24:24 -05007625 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007626 case KVM_HC_VAPIC_POLL_IRQ:
7627 ret = 0;
7628 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307629 case KVM_HC_KICK_CPU:
7630 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007631 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307632 ret = 0;
7633 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007634#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007635 case KVM_HC_CLOCK_PAIRING:
7636 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7637 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007638#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007639 case KVM_HC_SEND_IPI:
7640 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7641 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007642 case KVM_HC_SCHED_YIELD:
7643 kvm_sched_yield(vcpu->kvm, a0);
7644 ret = 0;
7645 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007646 default:
7647 ret = -KVM_ENOSYS;
7648 break;
7649 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007650out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007651 if (!op_64_bit)
7652 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007653 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007654
Amit Shahf11c3a82008-02-21 01:00:30 +05307655 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007656 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007657}
7658EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7659
Jan Kiszkab6785de2012-09-20 07:43:17 +02007660static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007661{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007662 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007663 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007664 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007665
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007666 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007667
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007668 return emulator_write_emulated(ctxt, rip, instruction, 3,
7669 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007670}
7671
Avi Kivity851ba692009-08-24 11:10:17 +03007672static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007673{
Matt Gingell782d4222015-11-16 15:26:00 -08007674 return vcpu->run->request_interrupt_window &&
7675 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007676}
7677
Avi Kivity851ba692009-08-24 11:10:17 +03007678static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007679{
Avi Kivity851ba692009-08-24 11:10:17 +03007680 struct kvm_run *kvm_run = vcpu->run;
7681
Jan Kiszka91586a32009-10-05 13:07:21 +02007682 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007683 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007684 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007685 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007686 kvm_run->ready_for_interrupt_injection =
7687 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007688 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007689}
7690
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007691static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7692{
7693 int max_irr, tpr;
7694
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007695 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007696 return;
7697
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007698 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007699 return;
7700
Andrey Smetanind62caab2015-11-10 15:36:33 +03007701 if (vcpu->arch.apicv_active)
7702 return;
7703
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007704 if (!vcpu->arch.apic->vapic_addr)
7705 max_irr = kvm_lapic_find_highest_irr(vcpu);
7706 else
7707 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007708
7709 if (max_irr != -1)
7710 max_irr >>= 4;
7711
7712 tpr = kvm_lapic_get_cr8(vcpu);
7713
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007714 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007715}
7716
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007717static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007718{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007719 int r;
7720
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007721 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007722
Liran Alon1a680e32018-03-23 03:01:33 +03007723 if (vcpu->arch.exception.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007724 kvm_x86_ops.queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007725 /*
Liran Alona042c262018-03-23 03:01:32 +03007726 * Do not inject an NMI or interrupt if there is a pending
7727 * exception. Exceptions and interrupts are recognized at
7728 * instruction boundaries, i.e. the start of an instruction.
7729 * Trap-like exceptions, e.g. #DB, have higher priority than
7730 * NMIs and interrupts, i.e. traps are recognized before an
7731 * NMI/interrupt that's pending on the same instruction.
7732 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7733 * priority, but are only generated (pended) during instruction
7734 * execution, i.e. a pending fault-like exception means the
7735 * fault occurred on the *previous* instruction and must be
7736 * serviced prior to recognizing any new events in order to
7737 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007738 */
Liran Alon1a680e32018-03-23 03:01:33 +03007739 else if (!vcpu->arch.exception.pending) {
7740 if (vcpu->arch.nmi_injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007741 kvm_x86_ops.set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007742 else if (vcpu->arch.interrupt.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007743 kvm_x86_ops.set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007744 }
7745
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07007746 WARN_ON_ONCE(vcpu->arch.exception.injected &&
7747 vcpu->arch.exception.pending);
7748
Liran Alon1a680e32018-03-23 03:01:33 +03007749 /*
7750 * Call check_nested_events() even if we reinjected a previous event
7751 * in order for caller to determine if it should require immediate-exit
7752 * from L2 to L1 due to pending L1 events which require exit
7753 * from L2 to L1.
7754 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04007755 if (is_guest_mode(vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04007756 r = kvm_x86_ops.nested_ops->check_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007757 if (r != 0)
7758 return r;
7759 }
7760
7761 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007762 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007763 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7764 vcpu->arch.exception.has_error_code,
7765 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007766
Wanpeng Li664f8e22017-08-24 03:35:09 -07007767 vcpu->arch.exception.pending = false;
7768 vcpu->arch.exception.injected = true;
7769
Nadav Amitd6e8c852014-07-24 14:51:24 +03007770 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7771 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7772 X86_EFLAGS_RF);
7773
Jim Mattsonf10c7292018-10-16 14:29:23 -07007774 if (vcpu->arch.exception.nr == DB_VECTOR) {
7775 /*
7776 * This code assumes that nSVM doesn't use
7777 * check_nested_events(). If it does, the
7778 * DR6/DR7 changes should happen before L1
7779 * gets a #VMEXIT for an intercepted #DB in
7780 * L2. (Under VMX, on the other hand, the
7781 * DR6/DR7 changes should not happen in the
7782 * event of a VM-exit to L1 for an intercepted
7783 * #DB in L2.)
7784 */
7785 kvm_deliver_exception_payload(vcpu);
7786 if (vcpu->arch.dr7 & DR7_GD) {
7787 vcpu->arch.dr7 &= ~DR7_GD;
7788 kvm_update_dr7(vcpu);
7789 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007790 }
7791
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007792 kvm_x86_ops.queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007793 }
7794
7795 /* Don't consider new event if we re-injected an event */
7796 if (kvm_event_needs_reinjection(vcpu))
7797 return 0;
7798
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007799 if (vcpu->arch.smi_pending &&
7800 kvm_x86_ops.smi_allowed(vcpu, true)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007801 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007802 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007803 enter_smm(vcpu);
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007804 } else if (vcpu->arch.nmi_pending &&
7805 kvm_x86_ops.nmi_allowed(vcpu, true)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007806 --vcpu->arch.nmi_pending;
7807 vcpu->arch.nmi_injected = true;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007808 kvm_x86_ops.set_nmi(vcpu);
Paolo Bonzinic300ab92020-04-23 14:08:58 -04007809 } else if (kvm_cpu_has_injectable_intr(vcpu) &&
7810 kvm_x86_ops.interrupt_allowed(vcpu, true)) {
7811 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
7812 kvm_x86_ops.set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007813 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007814
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007815 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007816}
7817
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007818static void process_nmi(struct kvm_vcpu *vcpu)
7819{
7820 unsigned limit = 2;
7821
7822 /*
7823 * x86 is limited to one NMI running, and one NMI pending after it.
7824 * If an NMI is already in progress, limit further NMIs to just one.
7825 * Otherwise, allow two (and we'll inject the first one immediately).
7826 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007827 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007828 limit = 1;
7829
7830 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7831 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7832 kvm_make_request(KVM_REQ_EVENT, vcpu);
7833}
7834
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007835static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007836{
7837 u32 flags = 0;
7838 flags |= seg->g << 23;
7839 flags |= seg->db << 22;
7840 flags |= seg->l << 21;
7841 flags |= seg->avl << 20;
7842 flags |= seg->present << 15;
7843 flags |= seg->dpl << 13;
7844 flags |= seg->s << 12;
7845 flags |= seg->type << 8;
7846 return flags;
7847}
7848
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007849static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007850{
7851 struct kvm_segment seg;
7852 int offset;
7853
7854 kvm_get_segment(vcpu, &seg, n);
7855 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7856
7857 if (n < 3)
7858 offset = 0x7f84 + n * 12;
7859 else
7860 offset = 0x7f2c + (n - 3) * 12;
7861
7862 put_smstate(u32, buf, offset + 8, seg.base);
7863 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007864 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007865}
7866
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007867#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007868static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007869{
7870 struct kvm_segment seg;
7871 int offset;
7872 u16 flags;
7873
7874 kvm_get_segment(vcpu, &seg, n);
7875 offset = 0x7e00 + n * 16;
7876
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007877 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007878 put_smstate(u16, buf, offset, seg.selector);
7879 put_smstate(u16, buf, offset + 2, flags);
7880 put_smstate(u32, buf, offset + 4, seg.limit);
7881 put_smstate(u64, buf, offset + 8, seg.base);
7882}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007883#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007884
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007885static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007886{
7887 struct desc_ptr dt;
7888 struct kvm_segment seg;
7889 unsigned long val;
7890 int i;
7891
7892 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7893 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7894 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7895 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7896
7897 for (i = 0; i < 8; i++)
7898 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7899
7900 kvm_get_dr(vcpu, 6, &val);
7901 put_smstate(u32, buf, 0x7fcc, (u32)val);
7902 kvm_get_dr(vcpu, 7, &val);
7903 put_smstate(u32, buf, 0x7fc8, (u32)val);
7904
7905 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7906 put_smstate(u32, buf, 0x7fc4, seg.selector);
7907 put_smstate(u32, buf, 0x7f64, seg.base);
7908 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007909 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007910
7911 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7912 put_smstate(u32, buf, 0x7fc0, seg.selector);
7913 put_smstate(u32, buf, 0x7f80, seg.base);
7914 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007915 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007916
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007917 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007918 put_smstate(u32, buf, 0x7f74, dt.address);
7919 put_smstate(u32, buf, 0x7f70, dt.size);
7920
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007921 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007922 put_smstate(u32, buf, 0x7f58, dt.address);
7923 put_smstate(u32, buf, 0x7f54, dt.size);
7924
7925 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007926 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007927
7928 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7929
7930 /* revision id */
7931 put_smstate(u32, buf, 0x7efc, 0x00020000);
7932 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7933}
7934
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007935#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007936static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007937{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007938 struct desc_ptr dt;
7939 struct kvm_segment seg;
7940 unsigned long val;
7941 int i;
7942
7943 for (i = 0; i < 16; i++)
7944 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7945
7946 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7947 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7948
7949 kvm_get_dr(vcpu, 6, &val);
7950 put_smstate(u64, buf, 0x7f68, val);
7951 kvm_get_dr(vcpu, 7, &val);
7952 put_smstate(u64, buf, 0x7f60, val);
7953
7954 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7955 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7956 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7957
7958 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7959
7960 /* revision id */
7961 put_smstate(u32, buf, 0x7efc, 0x00020064);
7962
7963 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7964
7965 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7966 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007967 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007968 put_smstate(u32, buf, 0x7e94, seg.limit);
7969 put_smstate(u64, buf, 0x7e98, seg.base);
7970
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007971 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007972 put_smstate(u32, buf, 0x7e84, dt.size);
7973 put_smstate(u64, buf, 0x7e88, dt.address);
7974
7975 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7976 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007977 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007978 put_smstate(u32, buf, 0x7e74, seg.limit);
7979 put_smstate(u64, buf, 0x7e78, seg.base);
7980
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007981 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007982 put_smstate(u32, buf, 0x7e64, dt.size);
7983 put_smstate(u64, buf, 0x7e68, dt.address);
7984
7985 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007986 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007987}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007988#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007989
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007990static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007991{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007992 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007993 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007994 char buf[512];
7995 u32 cr0;
7996
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007997 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007998 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007999#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008000 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008001 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008002 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008003#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008004 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008005
Ladi Prosek0234bf82017-10-11 16:54:40 +02008006 /*
8007 * Give pre_enter_smm() a chance to make ISA-specific changes to the
8008 * vCPU state (e.g. leave guest mode) after we've saved the state into
8009 * the SMM state-save area.
8010 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008011 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02008012
8013 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008014 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008015
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008016 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008017 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
8018 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008019 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008020
8021 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
8022 kvm_rip_write(vcpu, 0x8000);
8023
8024 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008025 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008026 vcpu->arch.cr0 = cr0;
8027
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008028 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008029
Paolo Bonzini18c36262015-08-07 12:27:54 +02008030 /* Undocumented: IDT limit is set to zero on entry to SMM. */
8031 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008032 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02008033
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008034 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
8035
8036 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8037 cs.base = vcpu->arch.smbase;
8038
8039 ds.selector = 0;
8040 ds.base = 0;
8041
8042 cs.limit = ds.limit = 0xffffffff;
8043 cs.type = ds.type = 0x3;
8044 cs.dpl = ds.dpl = 0;
8045 cs.db = ds.db = 0;
8046 cs.s = ds.s = 1;
8047 cs.l = ds.l = 0;
8048 cs.g = ds.g = 1;
8049 cs.avl = ds.avl = 0;
8050 cs.present = ds.present = 1;
8051 cs.unusable = ds.unusable = 0;
8052 cs.padding = ds.padding = 0;
8053
8054 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8055 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8056 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8057 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8058 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8059 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8060
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008061#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008062 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008063 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008064#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008065
8066 kvm_update_cpuid(vcpu);
8067 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008068}
8069
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008070static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008071{
8072 vcpu->arch.smi_pending = true;
8073 kvm_make_request(KVM_REQ_EVENT, vcpu);
8074}
8075
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008076void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8077 unsigned long *vcpu_bitmap)
8078{
8079 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008080
8081 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8082
Mao Wenandb5a95e2019-11-19 11:06:40 +08008083 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008084 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008085
8086 free_cpumask_var(cpus);
8087}
8088
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008089void kvm_make_scan_ioapic_request(struct kvm *kvm)
8090{
8091 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8092}
8093
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008094void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8095{
8096 if (!lapic_in_kernel(vcpu))
8097 return;
8098
8099 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8100 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008101 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008102}
8103EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8104
8105/*
8106 * NOTE: Do not hold any lock prior to calling this.
8107 *
8108 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8109 * locked, because it calls __x86_set_memory_region() which does
8110 * synchronize_srcu(&kvm->srcu).
8111 */
8112void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8113{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008114 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008115 unsigned long old, new, expected;
8116
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008117 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8118 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008119 return;
8120
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008121 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8122 do {
8123 expected = new = old;
8124 if (activate)
8125 __clear_bit(bit, &new);
8126 else
8127 __set_bit(bit, &new);
8128 if (new == old)
8129 break;
8130 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8131 } while (old != expected);
8132
8133 if (!!old == !!new)
8134 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008135
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008136 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008137 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8138 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008139
8140 /*
8141 * Sending request to update APICV for all other vcpus,
8142 * while update the calling vcpu immediately instead of
8143 * waiting for another #VMEXIT to handle the request.
8144 */
8145 except = kvm_get_running_vcpu();
8146 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
8147 except);
8148 if (except)
8149 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008150}
8151EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8152
Yang Zhang3d81bc72013-04-11 19:25:13 +08008153static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008154{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008155 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008156 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008157
Andrey Smetanin63086302015-11-10 15:36:32 +03008158 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008159
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008160 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008161 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008162 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008163 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008164 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008165 if (ioapic_in_kernel(vcpu->kvm))
8166 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008167 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008168
8169 if (is_guest_mode(vcpu))
8170 vcpu->arch.load_eoi_exitmap_pending = true;
8171 else
8172 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8173}
8174
8175static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8176{
8177 u64 eoi_exit_bitmap[4];
8178
8179 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8180 return;
8181
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008182 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8183 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008184 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008185}
8186
Michal Hocko93065ac2018-08-21 21:52:33 -07008187int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8188 unsigned long start, unsigned long end,
8189 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008190{
8191 unsigned long apic_address;
8192
8193 /*
8194 * The physical address of apic access page is stored in the VMCS.
8195 * Update it when it becomes invalid.
8196 */
8197 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8198 if (start <= apic_address && apic_address < end)
8199 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008200
8201 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008202}
8203
Tang Chen4256f432014-09-24 15:57:54 +08008204void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8205{
Paolo Bonzini35754c92015-07-29 12:05:37 +02008206 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008207 return;
8208
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008209 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008210 return;
8211
Sean Christophersona4148b72020-03-20 14:28:24 -07008212 kvm_x86_ops.set_apic_access_page_addr(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008213}
Tang Chen4256f432014-09-24 15:57:54 +08008214
Sean Christophersond264ee02018-08-27 15:21:12 -07008215void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8216{
8217 smp_send_reschedule(vcpu->cpu);
8218}
8219EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8220
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008221/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008222 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008223 * exiting to the userspace. Otherwise, the value will be returned to the
8224 * userspace.
8225 */
Avi Kivity851ba692009-08-24 11:10:17 +03008226static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008227{
8228 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008229 bool req_int_win =
8230 dm_request_for_irq_injection(vcpu) &&
8231 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08008232 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08008233
Jan Kiszka730dca42013-04-28 10:50:52 +02008234 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008235
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008236 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008237 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04008238 if (unlikely(!kvm_x86_ops.nested_ops->get_vmcs12_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008239 r = 0;
8240 goto out;
8241 }
8242 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008243 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008244 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008245 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008246 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008247 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8248 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008249 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8250 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008251 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8252 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008253 if (unlikely(r))
8254 goto out;
8255 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008256 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008257 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008258 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8259 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008260 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07008261 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008262
8263 /* Flushing all ASIDs flushes the current ASID... */
8264 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
8265 }
8266 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
8267 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04008268 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
8269 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008270
Avi Kivitya8eeb042010-05-10 12:34:53 +03008271 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008272 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008273 r = 0;
8274 goto out;
8275 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008276 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008277 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008278 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008279 r = 0;
8280 goto out;
8281 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008282 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8283 /* Page is swapped out. Do synthetic halt */
8284 vcpu->arch.apf.halted = true;
8285 r = 1;
8286 goto out;
8287 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008288 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8289 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008290 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8291 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008292 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8293 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008294 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008295 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008296 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008297 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008298 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8299 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8300 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008301 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008302 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8303 vcpu->run->eoi.vector =
8304 vcpu->arch.pending_ioapic_eoi;
8305 r = 0;
8306 goto out;
8307 }
8308 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008309 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8310 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008311 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8312 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008313 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8314 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008315 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8316 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8317 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8318 r = 0;
8319 goto out;
8320 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008321 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8322 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8323 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8324 r = 0;
8325 goto out;
8326 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008327 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8328 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8329 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8330 r = 0;
8331 goto out;
8332 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008333
8334 /*
8335 * KVM_REQ_HV_STIMER has to be processed after
8336 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8337 * depend on the guest clock being up-to-date
8338 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008339 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8340 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008341 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8342 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008343 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008344
Avi Kivityb463a6f2010-07-20 15:06:17 +03008345 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008346 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008347 kvm_apic_accept_events(vcpu);
8348 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8349 r = 1;
8350 goto out;
8351 }
8352
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008353 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008354 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008355 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008356 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008357 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008358 * SMIs have three cases:
8359 * 1) They can be nested, and then there is nothing to
8360 * do here because RSM will cause a vmexit anyway.
8361 * 2) There is an ISA-specific reason why SMI cannot be
8362 * injected, and the moment when this changes can be
8363 * intercepted.
8364 * 3) Or the SMI can be pending because
8365 * inject_pending_event has completed the injection
8366 * of an IRQ or NMI from the previous vmexit, and
8367 * then we request an immediate exit to inject the
8368 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008369 */
8370 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008371 if (!kvm_x86_ops.enable_smi_window(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008372 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008373 if (vcpu->arch.nmi_pending)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008374 kvm_x86_ops.enable_nmi_window(vcpu);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008375 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008376 kvm_x86_ops.enable_irq_window(vcpu);
Sean Christophersond2060bd2020-04-22 19:25:39 -07008377 if (is_guest_mode(vcpu) &&
8378 kvm_x86_ops.nested_ops->hv_timer_pending &&
8379 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8380 req_immediate_exit = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008381 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008382 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008383
Avi Kivity3842d132010-07-27 12:30:24 +03008384 if (kvm_lapic_enabled(vcpu)) {
8385 update_cr8_intercept(vcpu);
8386 kvm_lapic_sync_to_vapic(vcpu);
8387 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008388 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008389
Avi Kivityd8368af2012-05-14 18:07:56 +03008390 r = kvm_mmu_reload(vcpu);
8391 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008392 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008393 }
8394
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008395 preempt_disable();
8396
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008397 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008398
8399 /*
8400 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8401 * IPI are then delayed after guest entry, which ensures that they
8402 * result in virtual interrupt delivery.
8403 */
8404 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008405 vcpu->mode = IN_GUEST_MODE;
8406
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008407 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8408
Lan Tianyu0f127d12016-03-13 11:10:29 +08008409 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008410 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008411 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008412 *
Luwei Kang81b01662019-01-31 16:52:02 +08008413 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008414 * pairs with the memory barrier implicit in pi_test_and_set_on
8415 * (see vmx_deliver_posted_interrupt).
8416 *
8417 * 3) This also orders the write to mode from any reads to the page
8418 * tables done while the VCPU is running. Please see the comment
8419 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008420 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008421 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008422
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008423 /*
8424 * This handles the case where a posted interrupt was
8425 * notified with kvm_vcpu_kick.
8426 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008427 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008428 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008429
Wanpeng Li5a9f5442020-04-28 14:23:26 +08008430 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008431 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008432 smp_wmb();
8433 local_irq_enable();
8434 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008435 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008436 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008437 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008438 }
8439
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008440 if (req_immediate_exit) {
8441 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008442 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008443 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008444
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008445 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008446 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008447
Sean Christopherson2620fe22020-01-17 11:30:51 -08008448 fpregs_assert_state_consistent();
8449 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8450 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008451
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008452 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008453 set_debugreg(0, 7);
8454 set_debugreg(vcpu->arch.eff_db[0], 0);
8455 set_debugreg(vcpu->arch.eff_db[1], 1);
8456 set_debugreg(vcpu->arch.eff_db[2], 2);
8457 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008458 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008459 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008460 }
8461
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008462 exit_fastpath = kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008463
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008464 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008465 * Do this here before restoring debug registers on the host. And
8466 * since we do this before handling the vmexit, a DR access vmexit
8467 * can (a) read the correct value of the debug registers, (b) set
8468 * KVM_DEBUGREG_WONT_EXIT again.
8469 */
8470 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008471 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008472 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008473 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008474 kvm_update_dr7(vcpu);
8475 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008476 }
8477
8478 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008479 * If the guest has used debug registers, at least dr7
8480 * will be disabled while returning to the host.
8481 * If we don't have active breakpoints in the host, we don't
8482 * care about the messed up debug address registers. But if
8483 * we have some of them active, restore the old state.
8484 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008485 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008486 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008487
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008488 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008489
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008490 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008491 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008492
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008493 kvm_x86_ops.handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008494
Sean Christophersond7a08882019-07-10 09:07:34 -07008495 /*
8496 * Consume any pending interrupts, including the possible source of
8497 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8498 * An instruction is required after local_irq_enable() to fully unblock
8499 * interrupts on processors that implement an interrupt shadow, the
8500 * stat.exits increment will do nicely.
8501 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008502 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008503 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008504 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008505 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008506 kvm_after_interrupt(vcpu);
8507
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008508 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008509 if (lapic_in_kernel(vcpu)) {
8510 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8511 if (delta != S64_MIN) {
8512 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8513 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8514 }
8515 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008516
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008517 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008518 preempt_enable();
8519
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008520 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008521
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008522 /*
8523 * Profile KVM exit RIPs:
8524 */
8525 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008526 unsigned long rip = kvm_rip_read(vcpu);
8527 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008528 }
8529
Zachary Amsdencc578282012-02-03 15:43:50 -02008530 if (unlikely(vcpu->arch.tsc_always_catchup))
8531 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008532
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008533 if (vcpu->arch.apic_attention)
8534 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008535
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008536 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008537 return r;
8538
8539cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07008540 if (req_immediate_exit)
8541 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008542 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008543 if (unlikely(vcpu->arch.apic_attention))
8544 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008545out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008546 return r;
8547}
8548
Paolo Bonzini362c6982015-02-06 12:48:04 +01008549static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8550{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008551 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008552 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008553 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8554 kvm_vcpu_block(vcpu);
8555 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008556
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008557 if (kvm_x86_ops.post_block)
8558 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008559
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008560 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8561 return 1;
8562 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008563
Paolo Bonzini362c6982015-02-06 12:48:04 +01008564 kvm_apic_accept_events(vcpu);
8565 switch(vcpu->arch.mp_state) {
8566 case KVM_MP_STATE_HALTED:
8567 vcpu->arch.pv.pv_unhalted = false;
8568 vcpu->arch.mp_state =
8569 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008570 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008571 case KVM_MP_STATE_RUNNABLE:
8572 vcpu->arch.apf.halted = false;
8573 break;
8574 case KVM_MP_STATE_INIT_RECEIVED:
8575 break;
8576 default:
8577 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008578 }
8579 return 1;
8580}
8581
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008582static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8583{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008584 if (is_guest_mode(vcpu))
Paolo Bonzini33b22172020-04-17 10:24:18 -04008585 kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008586
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008587 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8588 !vcpu->arch.apf.halted);
8589}
8590
Paolo Bonzini362c6982015-02-06 12:48:04 +01008591static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008592{
8593 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008594 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008595
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008596 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008597 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008598
Paolo Bonzini362c6982015-02-06 12:48:04 +01008599 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008600 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008601 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008602 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008603 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008604 }
8605
Gleb Natapov09cec752009-03-23 15:11:44 +02008606 if (r <= 0)
8607 break;
8608
Radim Krčmář72875d82017-04-26 22:32:19 +02008609 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008610 if (kvm_cpu_has_pending_timer(vcpu))
8611 kvm_inject_pending_timer_irqs(vcpu);
8612
Matt Gingell782d4222015-11-16 15:26:00 -08008613 if (dm_request_for_irq_injection(vcpu) &&
8614 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008615 r = 0;
8616 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008617 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008618 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008619 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008620
8621 kvm_check_async_pf_completion(vcpu);
8622
Gleb Natapov09cec752009-03-23 15:11:44 +02008623 if (signal_pending(current)) {
8624 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008625 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008626 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008627 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008628 }
8629 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008630 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008631 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008632 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008633 }
8634 }
8635
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008636 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008637
8638 return r;
8639}
8640
Gleb Natapov716d51a2012-09-03 15:24:26 +03008641static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8642{
8643 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008644
Gleb Natapov716d51a2012-09-03 15:24:26 +03008645 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008646 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008647 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008648 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008649}
8650
8651static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8652{
8653 BUG_ON(!vcpu->arch.pio.count);
8654
8655 return complete_emulated_io(vcpu);
8656}
8657
Avi Kivityf78146b2012-04-18 19:22:47 +03008658/*
8659 * Implements the following, as a state machine:
8660 *
8661 * read:
8662 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008663 * for each mmio piece in the fragment
8664 * write gpa, len
8665 * exit
8666 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008667 * execute insn
8668 *
8669 * write:
8670 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008671 * for each mmio piece in the fragment
8672 * write gpa, len
8673 * copy data
8674 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008675 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008676static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008677{
8678 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008679 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008680 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008681
Gleb Natapov716d51a2012-09-03 15:24:26 +03008682 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008683
Gleb Natapov716d51a2012-09-03 15:24:26 +03008684 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008685 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8686 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008687 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008688 memcpy(frag->data, run->mmio.data, len);
8689
8690 if (frag->len <= 8) {
8691 /* Switch to the next fragment. */
8692 frag++;
8693 vcpu->mmio_cur_fragment++;
8694 } else {
8695 /* Go forward to the next mmio piece. */
8696 frag->data += len;
8697 frag->gpa += len;
8698 frag->len -= len;
8699 }
8700
Andrew Honiga08d3b32014-02-27 19:35:14 +01008701 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008702 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008703
8704 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008705 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008706 return 1;
8707 vcpu->mmio_read_completed = 1;
8708 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008709 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008710
Gleb Natapov716d51a2012-09-03 15:24:26 +03008711 run->exit_reason = KVM_EXIT_MMIO;
8712 run->mmio.phys_addr = frag->gpa;
8713 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008714 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8715 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008716 run->mmio.is_write = vcpu->mmio_is_write;
8717 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8718 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008719}
8720
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008721static void kvm_save_current_fpu(struct fpu *fpu)
8722{
8723 /*
8724 * If the target FPU state is not resident in the CPU registers, just
8725 * memcpy() from current, else save CPU state directly to the target.
8726 */
8727 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8728 memcpy(&fpu->state, &current->thread.fpu.state,
8729 fpu_kernel_xstate_size);
8730 else
8731 copy_fpregs_to_fpstate(fpu);
8732}
8733
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008734/* Swap (qemu) user FPU context for the guest FPU context. */
8735static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8736{
Rik van Riel5f409e22019-04-03 18:41:52 +02008737 fpregs_lock();
8738
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008739 kvm_save_current_fpu(vcpu->arch.user_fpu);
8740
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008741 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008742 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008743 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008744
8745 fpregs_mark_activate();
8746 fpregs_unlock();
8747
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008748 trace_kvm_fpu(1);
8749}
8750
8751/* When vcpu_run ends, restore user space FPU context. */
8752static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8753{
Rik van Riel5f409e22019-04-03 18:41:52 +02008754 fpregs_lock();
8755
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008756 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8757
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008758 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008759
8760 fpregs_mark_activate();
8761 fpregs_unlock();
8762
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008763 ++vcpu->stat.fpu_reload;
8764 trace_kvm_fpu(0);
8765}
8766
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008767int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008768{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008769 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008770 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008771
Christoffer Dallaccb7572017-12-04 21:35:25 +01008772 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008773 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008774 kvm_load_guest_fpu(vcpu);
8775
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008776 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008777 if (kvm_run->immediate_exit) {
8778 r = -EINTR;
8779 goto out;
8780 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008781 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008782 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008783 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008784 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008785 if (signal_pending(current)) {
8786 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008787 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008788 ++vcpu->stat.signal_exits;
8789 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008790 goto out;
8791 }
8792
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008793 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008794 r = -EINVAL;
8795 goto out;
8796 }
8797
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008798 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008799 r = sync_regs(vcpu);
8800 if (r != 0)
8801 goto out;
8802 }
8803
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008804 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008805 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008806 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8807 r = -EINVAL;
8808 goto out;
8809 }
8810 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008811
Gleb Natapov716d51a2012-09-03 15:24:26 +03008812 if (unlikely(vcpu->arch.complete_userspace_io)) {
8813 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8814 vcpu->arch.complete_userspace_io = NULL;
8815 r = cui(vcpu);
8816 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008817 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008818 } else
8819 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008820
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008821 if (kvm_run->immediate_exit)
8822 r = -EINTR;
8823 else
8824 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008825
8826out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008827 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008828 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08008829 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008830 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008831 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008832
Christoffer Dallaccb7572017-12-04 21:35:25 +01008833 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008834 return r;
8835}
8836
Ken Hofsass01643c52018-01-31 16:03:36 -08008837static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008838{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008839 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8840 /*
8841 * We are here if userspace calls get_regs() in the middle of
8842 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008843 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008844 * that usually, but some bad designed PV devices (vmware
8845 * backdoor interface) need this to work
8846 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008847 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008848 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8849 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008850 regs->rax = kvm_rax_read(vcpu);
8851 regs->rbx = kvm_rbx_read(vcpu);
8852 regs->rcx = kvm_rcx_read(vcpu);
8853 regs->rdx = kvm_rdx_read(vcpu);
8854 regs->rsi = kvm_rsi_read(vcpu);
8855 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008856 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008857 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008858#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008859 regs->r8 = kvm_r8_read(vcpu);
8860 regs->r9 = kvm_r9_read(vcpu);
8861 regs->r10 = kvm_r10_read(vcpu);
8862 regs->r11 = kvm_r11_read(vcpu);
8863 regs->r12 = kvm_r12_read(vcpu);
8864 regs->r13 = kvm_r13_read(vcpu);
8865 regs->r14 = kvm_r14_read(vcpu);
8866 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008867#endif
8868
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008869 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008870 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008871}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008872
Ken Hofsass01643c52018-01-31 16:03:36 -08008873int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8874{
8875 vcpu_load(vcpu);
8876 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008877 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008878 return 0;
8879}
8880
Ken Hofsass01643c52018-01-31 16:03:36 -08008881static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008882{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008883 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8884 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8885
Sean Christophersonde3cd112019-04-30 10:36:17 -07008886 kvm_rax_write(vcpu, regs->rax);
8887 kvm_rbx_write(vcpu, regs->rbx);
8888 kvm_rcx_write(vcpu, regs->rcx);
8889 kvm_rdx_write(vcpu, regs->rdx);
8890 kvm_rsi_write(vcpu, regs->rsi);
8891 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008892 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008893 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008894#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008895 kvm_r8_write(vcpu, regs->r8);
8896 kvm_r9_write(vcpu, regs->r9);
8897 kvm_r10_write(vcpu, regs->r10);
8898 kvm_r11_write(vcpu, regs->r11);
8899 kvm_r12_write(vcpu, regs->r12);
8900 kvm_r13_write(vcpu, regs->r13);
8901 kvm_r14_write(vcpu, regs->r14);
8902 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008903#endif
8904
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008905 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008906 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008907
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008908 vcpu->arch.exception.pending = false;
8909
Avi Kivity3842d132010-07-27 12:30:24 +03008910 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008911}
Avi Kivity3842d132010-07-27 12:30:24 +03008912
Ken Hofsass01643c52018-01-31 16:03:36 -08008913int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8914{
8915 vcpu_load(vcpu);
8916 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008917 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008918 return 0;
8919}
8920
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008921void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8922{
8923 struct kvm_segment cs;
8924
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008925 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008926 *db = cs.db;
8927 *l = cs.l;
8928}
8929EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8930
Ken Hofsass01643c52018-01-31 16:03:36 -08008931static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008932{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008933 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008934
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008935 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8936 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8937 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8938 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8939 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8940 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008941
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008942 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8943 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008944
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008945 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008946 sregs->idt.limit = dt.size;
8947 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008948 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008949 sregs->gdt.limit = dt.size;
8950 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008951
Avi Kivity4d4ec082009-12-29 18:07:30 +02008952 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008953 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008954 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008955 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008956 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008957 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008958 sregs->apic_base = kvm_get_apic_base(vcpu);
8959
Jordan Borgner0e96f312018-10-28 12:58:28 +00008960 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008961
Liran Alon04140b42018-03-23 03:01:31 +03008962 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008963 set_bit(vcpu->arch.interrupt.nr,
8964 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008965}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008966
Ken Hofsass01643c52018-01-31 16:03:36 -08008967int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8968 struct kvm_sregs *sregs)
8969{
8970 vcpu_load(vcpu);
8971 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008972 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008973 return 0;
8974}
8975
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008976int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8977 struct kvm_mp_state *mp_state)
8978{
Christoffer Dallfd232562017-12-04 21:35:30 +01008979 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008980 if (kvm_mpx_supported())
8981 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008982
Jan Kiszka66450a22013-03-13 12:42:34 +01008983 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308984 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8985 vcpu->arch.pv.pv_unhalted)
8986 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8987 else
8988 mp_state->mp_state = vcpu->arch.mp_state;
8989
Sean Christophersonf958bd22019-12-09 12:19:31 -08008990 if (kvm_mpx_supported())
8991 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008992 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008993 return 0;
8994}
8995
8996int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8997 struct kvm_mp_state *mp_state)
8998{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008999 int ret = -EINVAL;
9000
9001 vcpu_load(vcpu);
9002
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009003 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01009004 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01009005 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01009006
Liran Alon27cbe7d2019-11-11 11:16:40 +02009007 /*
9008 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
9009 * INIT state; latched init should be reported using
9010 * KVM_SET_VCPU_EVENTS, so reject it here.
9011 */
9012 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01009013 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
9014 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01009015 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01009016
Jan Kiszka66450a22013-03-13 12:42:34 +01009017 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
9018 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
9019 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
9020 } else
9021 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03009022 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01009023
9024 ret = 0;
9025out:
9026 vcpu_put(vcpu);
9027 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009028}
9029
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009030int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
9031 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009032{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009033 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009034 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009035
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009036 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02009037
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009038 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009039 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009040 if (ret) {
9041 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9042 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9043 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009044 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009045 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009046
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009047 kvm_rip_write(vcpu, ctxt->eip);
9048 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009049 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009050}
9051EXPORT_SYMBOL_GPL(kvm_task_switch);
9052
Peng Hao3140c152018-04-02 09:15:32 +08009053static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009054{
Tianyu Lan37b95952018-01-16 17:34:07 +08009055 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009056 /*
9057 * When EFER.LME and CR0.PG are set, the processor is in
9058 * 64-bit mode (though maybe in a 32-bit code segment).
9059 * CR4.PAE and EFER.LMA must be set.
9060 */
Tianyu Lan37b95952018-01-16 17:34:07 +08009061 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05009062 || !(sregs->efer & EFER_LMA))
9063 return -EINVAL;
9064 } else {
9065 /*
9066 * Not in 64-bit mode: EFER.LMA is clear and the code
9067 * segment cannot be 64-bit.
9068 */
9069 if (sregs->efer & EFER_LMA || sregs->cs.l)
9070 return -EINVAL;
9071 }
9072
Wanpeng Li3ca94192019-09-18 17:50:10 +08009073 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009074}
9075
Ken Hofsass01643c52018-01-31 16:03:36 -08009076static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009077{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009078 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009079 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009080 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009081 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009082 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009083 int ret = -EINVAL;
9084
Lan Tianyuf2981032017-12-14 03:01:52 -05009085 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009086 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009087
Jim Mattsond3802282017-08-10 10:14:13 -07009088 apic_base_msr.data = sregs->apic_base;
9089 apic_base_msr.host_initiated = true;
9090 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009091 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009092
Gleb Natapov89a27f42010-02-16 10:51:48 +02009093 dt.size = sregs->idt.limit;
9094 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009095 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009096 dt.size = sregs->gdt.limit;
9097 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009098 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009099
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009100 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009101 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009102 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009103 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009104
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009105 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009106
Avi Kivityf6801df2010-01-21 15:31:50 +02009107 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009108 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009109
Avi Kivity4d4ec082009-12-29 18:07:30 +02009110 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009111 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009112 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009113
Avi Kivityfc78f512009-12-07 12:16:48 +02009114 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009115 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9116 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009117 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009118 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009119 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009120
9121 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009122 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009123 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009124 mmu_reset_needed = 1;
9125 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009126 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009127
9128 if (mmu_reset_needed)
9129 kvm_mmu_reset_context(vcpu);
9130
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009131 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009132 pending_vec = find_first_bit(
9133 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9134 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009135 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009136 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009137 }
9138
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009139 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9140 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9141 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9142 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9143 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9144 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009145
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009146 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9147 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009148
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009149 update_cr8_intercept(vcpu);
9150
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009151 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009152 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009153 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009154 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009155 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9156
Avi Kivity3842d132010-07-27 12:30:24 +03009157 kvm_make_request(KVM_REQ_EVENT, vcpu);
9158
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009159 ret = 0;
9160out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009161 return ret;
9162}
9163
9164int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9165 struct kvm_sregs *sregs)
9166{
9167 int ret;
9168
9169 vcpu_load(vcpu);
9170 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009171 vcpu_put(vcpu);
9172 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009173}
9174
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009175int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9176 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009177{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009178 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009179 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009180
Christoffer Dall66b56562017-12-04 21:35:33 +01009181 vcpu_load(vcpu);
9182
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009183 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9184 r = -EBUSY;
9185 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009186 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009187 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9188 kvm_queue_exception(vcpu, DB_VECTOR);
9189 else
9190 kvm_queue_exception(vcpu, BP_VECTOR);
9191 }
9192
Jan Kiszka91586a32009-10-05 13:07:21 +02009193 /*
9194 * Read rflags as long as potentially injected trace flags are still
9195 * filtered out.
9196 */
9197 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009198
9199 vcpu->guest_debug = dbg->control;
9200 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9201 vcpu->guest_debug = 0;
9202
9203 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009204 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9205 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009206 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009207 } else {
9208 for (i = 0; i < KVM_NR_DB_REGS; i++)
9209 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009210 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009211 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009212
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009213 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9214 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9215 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009216
Jan Kiszka91586a32009-10-05 13:07:21 +02009217 /*
9218 * Trigger an rflags update that will inject or remove the trace
9219 * flags.
9220 */
9221 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009222
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009223 kvm_x86_ops.update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009224
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009225 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009226
Avi Kivity2122ff52010-05-13 11:25:04 +03009227out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009228 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009229 return r;
9230}
9231
9232/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009233 * Translate a guest virtual address to a guest physical address.
9234 */
9235int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9236 struct kvm_translation *tr)
9237{
9238 unsigned long vaddr = tr->linear_address;
9239 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009240 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009241
Christoffer Dall1da5b612017-12-04 21:35:32 +01009242 vcpu_load(vcpu);
9243
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009244 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009245 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009246 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009247 tr->physical_address = gpa;
9248 tr->valid = gpa != UNMAPPED_GVA;
9249 tr->writeable = 1;
9250 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009251
Christoffer Dall1da5b612017-12-04 21:35:32 +01009252 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009253 return 0;
9254}
9255
Hollis Blanchardd0752062007-10-31 17:24:25 -05009256int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9257{
Christoffer Dall13931232017-12-04 21:35:34 +01009258 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009259
Christoffer Dall13931232017-12-04 21:35:34 +01009260 vcpu_load(vcpu);
9261
Marc Orrb666a4b2018-11-06 14:53:56 -08009262 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009263 memcpy(fpu->fpr, fxsave->st_space, 128);
9264 fpu->fcw = fxsave->cwd;
9265 fpu->fsw = fxsave->swd;
9266 fpu->ftwx = fxsave->twd;
9267 fpu->last_opcode = fxsave->fop;
9268 fpu->last_ip = fxsave->rip;
9269 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009270 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009271
Christoffer Dall13931232017-12-04 21:35:34 +01009272 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009273 return 0;
9274}
9275
9276int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9277{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009278 struct fxregs_state *fxsave;
9279
9280 vcpu_load(vcpu);
9281
Marc Orrb666a4b2018-11-06 14:53:56 -08009282 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009283
Hollis Blanchardd0752062007-10-31 17:24:25 -05009284 memcpy(fxsave->st_space, fpu->fpr, 128);
9285 fxsave->cwd = fpu->fcw;
9286 fxsave->swd = fpu->fsw;
9287 fxsave->twd = fpu->ftwx;
9288 fxsave->fop = fpu->last_opcode;
9289 fxsave->rip = fpu->last_ip;
9290 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009291 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009292
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009293 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009294 return 0;
9295}
9296
Ken Hofsass01643c52018-01-31 16:03:36 -08009297static void store_regs(struct kvm_vcpu *vcpu)
9298{
9299 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9300
9301 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9302 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9303
9304 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9305 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9306
9307 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9308 kvm_vcpu_ioctl_x86_get_vcpu_events(
9309 vcpu, &vcpu->run->s.regs.events);
9310}
9311
9312static int sync_regs(struct kvm_vcpu *vcpu)
9313{
9314 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9315 return -EINVAL;
9316
9317 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9318 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9319 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9320 }
9321 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9322 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9323 return -EINVAL;
9324 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9325 }
9326 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9327 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9328 vcpu, &vcpu->run->s.regs.events))
9329 return -EINVAL;
9330 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9331 }
9332
9333 return 0;
9334}
9335
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009336static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009337{
Marc Orrb666a4b2018-11-06 14:53:56 -08009338 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009339 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009340 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009341 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009342
Dexuan Cui2acf9232010-06-10 11:27:12 +08009343 /*
9344 * Ensure guest xcr0 is valid for loading
9345 */
Dave Hansend91cab72015-09-02 16:31:26 -07009346 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009347
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009348 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009349}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009350
Sean Christopherson897cc382019-12-18 13:55:09 -08009351int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009352{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009353 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009354 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9355 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009356
Sean Christopherson897cc382019-12-18 13:55:09 -08009357 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009358}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009359
Sean Christophersone529ef62019-12-18 13:55:15 -08009360int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009361{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009362 struct page *page;
9363 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009364
Sean Christopherson95a0d012019-12-18 13:55:23 -08009365 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9366 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9367 else
9368 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9369
9370 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9371
9372 r = kvm_mmu_create(vcpu);
9373 if (r < 0)
9374 return r;
9375
9376 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009377 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9378 if (r < 0)
9379 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009380 if (kvm_apicv_activated(vcpu->kvm))
9381 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009382 } else
9383 static_key_slow_inc(&kvm_no_apic_vcpu);
9384
9385 r = -ENOMEM;
9386
9387 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9388 if (!page)
9389 goto fail_free_lapic;
9390 vcpu->arch.pio_data = page_address(page);
9391
9392 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9393 GFP_KERNEL_ACCOUNT);
9394 if (!vcpu->arch.mce_banks)
9395 goto fail_free_pio_data;
9396 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9397
9398 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9399 GFP_KERNEL_ACCOUNT))
9400 goto fail_free_mce_banks;
9401
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009402 if (!alloc_emulate_ctxt(vcpu))
9403 goto free_wbinvd_dirty_mask;
9404
Sean Christopherson95a0d012019-12-18 13:55:23 -08009405 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9406 GFP_KERNEL_ACCOUNT);
9407 if (!vcpu->arch.user_fpu) {
9408 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009409 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009410 }
9411
9412 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9413 GFP_KERNEL_ACCOUNT);
9414 if (!vcpu->arch.guest_fpu) {
9415 pr_err("kvm: failed to allocate vcpu's fpu\n");
9416 goto free_user_fpu;
9417 }
9418 fx_init(vcpu);
9419
Sean Christopherson95a0d012019-12-18 13:55:23 -08009420 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9421
9422 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9423
9424 kvm_async_pf_hash_reset(vcpu);
9425 kvm_pmu_init(vcpu);
9426
9427 vcpu->arch.pending_external_vector = -1;
9428 vcpu->arch.preempted_in_kernel = false;
9429
9430 kvm_hv_vcpu_init(vcpu);
9431
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009432 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009433 if (r)
9434 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009435
Sean Christopherson0cf91352019-03-07 15:43:02 -08009436 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009437 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009438 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009439 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009440 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009441 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009442 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009443 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009444
9445free_guest_fpu:
9446 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9447free_user_fpu:
9448 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009449free_emulate_ctxt:
9450 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009451free_wbinvd_dirty_mask:
9452 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9453fail_free_mce_banks:
9454 kfree(vcpu->arch.mce_banks);
9455fail_free_pio_data:
9456 free_page((unsigned long)vcpu->arch.pio_data);
9457fail_free_lapic:
9458 kvm_free_lapic(vcpu);
9459fail_mmu_destroy:
9460 kvm_mmu_destroy(vcpu);
9461 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009462}
9463
Dominik Dingel31928aa2014-12-04 15:47:07 +01009464void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009465{
Will Auld8fe8ab42012-11-29 12:42:12 -08009466 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009467 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009468
Roman Kagand3457c82017-07-14 17:13:20 +03009469 kvm_hv_vcpu_postcreate(vcpu);
9470
Christoffer Dallec7660c2017-12-04 21:35:23 +01009471 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009472 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009473 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009474 msr.data = 0x0;
9475 msr.index = MSR_IA32_TSC;
9476 msr.host_initiated = true;
9477 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009478 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009479
9480 /* poll control enabled by default */
9481 vcpu->arch.msr_kvm_poll_control = 1;
9482
Christoffer Dallec7660c2017-12-04 21:35:23 +01009483 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009484
Wanpeng Lib34de572020-02-28 11:18:41 +08009485 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9486 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9487 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009488}
9489
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009490void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009491{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009492 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009493 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009494
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009495 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9496
Sean Christopherson50b143e2019-12-18 13:55:07 -08009497 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009498
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009499 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009500
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009501 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009502 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9503 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9504 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009505
9506 kvm_hv_vcpu_uninit(vcpu);
9507 kvm_pmu_destroy(vcpu);
9508 kfree(vcpu->arch.mce_banks);
9509 kvm_free_lapic(vcpu);
9510 idx = srcu_read_lock(&vcpu->kvm->srcu);
9511 kvm_mmu_destroy(vcpu);
9512 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9513 free_page((unsigned long)vcpu->arch.pio_data);
9514 if (!lapic_in_kernel(vcpu))
9515 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009516}
9517
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009518void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009519{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009520 kvm_lapic_reset(vcpu, init_event);
9521
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009522 vcpu->arch.hflags = 0;
9523
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009524 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009525 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009526 atomic_set(&vcpu->arch.nmi_queued, 0);
9527 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009528 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009529 kvm_clear_interrupt_queue(vcpu);
9530 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009531
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009532 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009533 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009534 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009535 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009536 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009537
Nadav Amit11190222015-04-02 03:10:38 +03009538 vcpu->arch.cr2 = 0;
9539
Avi Kivity3842d132010-07-27 12:30:24 +03009540 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009541 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009542 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009543
Glauber Costa12f9a482011-02-01 14:16:40 -05009544 kvmclock_reset(vcpu);
9545
Gleb Natapovaf585b92010-10-14 11:22:46 +02009546 kvm_clear_async_pf_completion_queue(vcpu);
9547 kvm_async_pf_hash_reset(vcpu);
9548 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009549
Wanpeng Lia554d202017-10-11 05:10:19 -07009550 if (kvm_mpx_supported()) {
9551 void *mpx_state_buffer;
9552
9553 /*
9554 * To avoid have the INIT path from kvm_apic_has_events() that be
9555 * called with loaded FPU and does not let userspace fix the state.
9556 */
Rik van Rielf775b132017-11-14 16:54:23 -05009557 if (init_event)
9558 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009559 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009560 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009561 if (mpx_state_buffer)
9562 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009563 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009564 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009565 if (mpx_state_buffer)
9566 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009567 if (init_event)
9568 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009569 }
9570
Paolo Bonzini64d60672015-05-07 11:36:11 +02009571 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009572 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009573 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009574
Kyle Hueydb2336a2017-03-20 01:16:28 -07009575 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009576
9577 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009578 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009579
Julian Stecklina66f7b722012-12-05 15:26:19 +01009580 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9581 vcpu->arch.regs_avail = ~0;
9582 vcpu->arch.regs_dirty = ~0;
9583
Wanpeng Lia554d202017-10-11 05:10:19 -07009584 vcpu->arch.ia32_xss = 0;
9585
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009586 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009587}
9588
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009589void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009590{
9591 struct kvm_segment cs;
9592
9593 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9594 cs.selector = vector << 8;
9595 cs.base = vector << 12;
9596 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9597 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009598}
9599
Radim Krčmář13a34e02014-08-28 15:13:03 +02009600int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009601{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009602 struct kvm *kvm;
9603 struct kvm_vcpu *vcpu;
9604 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009605 int ret;
9606 u64 local_tsc;
9607 u64 max_tsc = 0;
9608 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009609
9610 kvm_shared_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009611 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009612 if (ret != 0)
9613 return ret;
9614
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009615 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009616 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009617 list_for_each_entry(kvm, &vm_list, vm_list) {
9618 kvm_for_each_vcpu(i, vcpu, kvm) {
9619 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009620 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009621 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9622 backwards_tsc = true;
9623 if (vcpu->arch.last_host_tsc > max_tsc)
9624 max_tsc = vcpu->arch.last_host_tsc;
9625 }
9626 }
9627 }
9628
9629 /*
9630 * Sometimes, even reliable TSCs go backwards. This happens on
9631 * platforms that reset TSC during suspend or hibernate actions, but
9632 * maintain synchronization. We must compensate. Fortunately, we can
9633 * detect that condition here, which happens early in CPU bringup,
9634 * before any KVM threads can be running. Unfortunately, we can't
9635 * bring the TSCs fully up to date with real time, as we aren't yet far
9636 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009637 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009638 * variables that haven't been updated yet.
9639 *
9640 * So we simply find the maximum observed TSC above, then record the
9641 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9642 * the adjustment will be applied. Note that we accumulate
9643 * adjustments, in case multiple suspend cycles happen before some VCPU
9644 * gets a chance to run again. In the event that no KVM threads get a
9645 * chance to run, we will miss the entire elapsed period, as we'll have
9646 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9647 * loose cycle time. This isn't too big a deal, since the loss will be
9648 * uniform across all VCPUs (not to mention the scenario is extremely
9649 * unlikely). It is possible that a second hibernate recovery happens
9650 * much faster than a first, causing the observed TSC here to be
9651 * smaller; this would require additional padding adjustment, which is
9652 * why we set last_host_tsc to the local tsc observed here.
9653 *
9654 * N.B. - this code below runs only on platforms with reliable TSC,
9655 * as that is the only way backwards_tsc is set above. Also note
9656 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9657 * have the same delta_cyc adjustment applied if backwards_tsc
9658 * is detected. Note further, this adjustment is only done once,
9659 * as we reset last_host_tsc on all VCPUs to stop this from being
9660 * called multiple times (one for each physical CPU bringup).
9661 *
Guo Chao4a969982012-06-28 15:17:27 +08009662 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009663 * will be compensated by the logic in vcpu_load, which sets the TSC to
9664 * catchup mode. This will catchup all VCPUs to real time, but cannot
9665 * guarantee that they stay in perfect synchronization.
9666 */
9667 if (backwards_tsc) {
9668 u64 delta_cyc = max_tsc - local_tsc;
9669 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009670 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009671 kvm_for_each_vcpu(i, vcpu, kvm) {
9672 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9673 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009674 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009675 }
9676
9677 /*
9678 * We have to disable TSC offset matching.. if you were
9679 * booting a VM while issuing an S4 host suspend....
9680 * you may have some problem. Solving this issue is
9681 * left as an exercise to the reader.
9682 */
9683 kvm->arch.last_tsc_nsec = 0;
9684 kvm->arch.last_tsc_write = 0;
9685 }
9686
9687 }
9688 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009689}
9690
Radim Krčmář13a34e02014-08-28 15:13:03 +02009691void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009692{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009693 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009694 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009695}
9696
Sean Christophersonb9904082020-03-21 13:25:55 -07009697int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009698{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009699 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009700 int r;
9701
Sean Christopherson91661982020-03-02 15:57:06 -08009702 rdmsrl_safe(MSR_EFER, &host_efer);
9703
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009704 if (boot_cpu_has(X86_FEATURE_XSAVES))
9705 rdmsrl(MSR_IA32_XSS, host_xss);
9706
Sean Christophersond008dfd2020-03-21 13:25:56 -07009707 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009708 if (r != 0)
9709 return r;
9710
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009711 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -07009712
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009713 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9714 supported_xss = 0;
9715
Paolo Bonzini139f7422020-05-05 09:40:46 -04009716#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
9717 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
9718#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -08009719
Haozhong Zhang35181e82015-10-20 15:39:03 +08009720 if (kvm_has_tsc_control) {
9721 /*
9722 * Make sure the user can only configure tsc_khz values that
9723 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009724 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009725 * be 1 on all machines.
9726 */
9727 u64 max = min(0x7fffffffULL,
9728 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9729 kvm_max_guest_tsc_khz = max;
9730
Haozhong Zhangad7218832015-10-20 15:39:02 +08009731 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009732 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009733
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009734 kvm_init_msr_list();
9735 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009736}
9737
9738void kvm_arch_hardware_unsetup(void)
9739{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009740 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009741}
9742
Sean Christophersonb9904082020-03-21 13:25:55 -07009743int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009744{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009745 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009746 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009747
9748 WARN_ON(!irqs_disabled());
9749
Paolo Bonzini139f7422020-05-05 09:40:46 -04009750 if (__cr4_reserved_bits(cpu_has, c) !=
9751 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009752 return -EIO;
9753
Sean Christophersond008dfd2020-03-21 13:25:56 -07009754 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009755}
9756
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009757bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9758{
9759 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9760}
9761EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9762
9763bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9764{
9765 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9766}
9767
Gleb Natapov54e98182012-08-05 15:58:32 +03009768struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009769EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009770
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009771void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9772{
Like Xub35e5542019-10-27 18:52:43 +08009773 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9774
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009775 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009776 if (pmu->version && unlikely(pmu->event_count)) {
9777 pmu->need_cleanup = true;
9778 kvm_make_request(KVM_REQ_PMU, vcpu);
9779 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009780 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009781}
9782
Sean Christopherson562b6b02020-01-26 16:41:13 -08009783void kvm_arch_free_vm(struct kvm *kvm)
9784{
9785 kfree(kvm->arch.hyperv.hv_pa_pg);
9786 vfree(kvm);
9787}
9788
9789
Carsten Ottee08b9632012-01-04 10:25:20 +01009790int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009791{
Carsten Ottee08b9632012-01-04 10:25:20 +01009792 if (type)
9793 return -EINVAL;
9794
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009795 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009796 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009797 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009798 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009799 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009800 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009801
Sheng Yang5550af42008-10-15 20:15:06 +08009802 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9803 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009804 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9805 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9806 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009807
Jan Kiszka038f8c12011-02-04 10:49:11 +01009808 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009809 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009810 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9811
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009812 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009813 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009814
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009815 kvm->arch.guest_can_read_msr_platform_info = true;
9816
Andrew Jones7e44e442014-02-28 12:52:54 +01009817 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009818 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009819
Roman Kagancbc02362018-02-01 16:48:31 +03009820 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009821 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009822 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009823
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009824 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009825}
9826
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009827int kvm_arch_post_init_vm(struct kvm *kvm)
9828{
9829 return kvm_mmu_post_init_vm(kvm);
9830}
9831
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009832static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9833{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009834 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009835 kvm_mmu_unload(vcpu);
9836 vcpu_put(vcpu);
9837}
9838
9839static void kvm_free_vcpus(struct kvm *kvm)
9840{
9841 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009842 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009843
9844 /*
9845 * Unpin any mmu pages first.
9846 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009847 kvm_for_each_vcpu(i, vcpu, kvm) {
9848 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009849 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009850 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009851 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009852 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009853
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009854 mutex_lock(&kvm->lock);
9855 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9856 kvm->vcpus[i] = NULL;
9857
9858 atomic_set(&kvm->online_vcpus, 0);
9859 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009860}
9861
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009862void kvm_arch_sync_events(struct kvm *kvm)
9863{
Andrew Jones332967a2014-02-28 12:52:55 +01009864 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009865 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009866 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009867}
9868
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009869int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009870{
9871 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009872 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009873 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009874 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009875
9876 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009877 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9878 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009879
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009880 slot = id_to_memslot(slots, id);
9881 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009882 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009883 return -EEXIST;
9884
9885 /*
9886 * MAP_SHARED to prevent internal slot pages from being moved
9887 * by fork()/COW.
9888 */
9889 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9890 MAP_SHARED | MAP_ANONYMOUS, 0);
9891 if (IS_ERR((void *)hva))
9892 return PTR_ERR((void *)hva);
9893 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009894 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009895 return 0;
9896
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009897 /*
9898 * Stuff a non-canonical value to catch use-after-delete. This
9899 * ends up being 0 on 32-bit KVM, but there's no better
9900 * alternative.
9901 */
9902 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009903 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009904 }
9905
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009906 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009907 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009908
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009909 m.slot = id | (i << 16);
9910 m.flags = 0;
9911 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009912 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009913 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009914 r = __kvm_set_memory_region(kvm, &m);
9915 if (r < 0)
9916 return r;
9917 }
9918
Eric Biggers103c7632018-01-31 17:30:21 -08009919 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009920 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009921
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009922 return 0;
9923}
9924EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9925
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009926void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9927{
9928 kvm_mmu_pre_destroy_vm(kvm);
9929}
9930
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009931void kvm_arch_destroy_vm(struct kvm *kvm)
9932{
Andrew Honig27469d22013-04-18 09:38:14 -07009933 if (current->mm == kvm->mm) {
9934 /*
9935 * Free memory regions allocated on behalf of userspace,
9936 * unless the the memory map has changed due to process exit
9937 * or fd copying.
9938 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009939 mutex_lock(&kvm->slots_lock);
9940 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9941 0, 0);
9942 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9943 0, 0);
9944 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9945 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009946 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009947 if (kvm_x86_ops.vm_destroy)
9948 kvm_x86_ops.vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009949 kvm_pic_destroy(kvm);
9950 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009951 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009952 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009953 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009954 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009955 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009956 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009957}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009958
Sean Christophersone96c81e2020-02-18 13:07:27 -08009959void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009960{
9961 int i;
9962
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009963 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009964 kvfree(slot->arch.rmap[i]);
9965 slot->arch.rmap[i] = NULL;
9966
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009967 if (i == 0)
9968 continue;
9969
Sean Christophersone96c81e2020-02-18 13:07:27 -08009970 kvfree(slot->arch.lpage_info[i - 1]);
9971 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009972 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009973
Sean Christophersone96c81e2020-02-18 13:07:27 -08009974 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009975}
9976
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009977static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9978 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009979{
9980 int i;
9981
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009982 /*
9983 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9984 * old arrays will be freed by __kvm_set_memory_region() if installing
9985 * the new memslot is successful.
9986 */
9987 memset(&slot->arch, 0, sizeof(slot->arch));
9988
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009989 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009990 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009991 unsigned long ugfn;
9992 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009993 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009994
9995 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9996 slot->base_gfn, level) + 1;
9997
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009998 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009999 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -080010000 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010001 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010002 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010003 if (i == 0)
10004 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010005
Ben Gardon254272c2019-02-11 11:02:50 -080010006 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010007 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010008 goto out_free;
10009
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010010 slot->arch.lpage_info[i - 1] = linfo;
10011
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010012 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010013 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010014 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010015 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010016 ugfn = slot->userspace_addr >> PAGE_SHIFT;
10017 /*
10018 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080010019 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010020 */
Sean Christopherson600087b2020-03-02 15:57:05 -080010021 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010022 unsigned long j;
10023
10024 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010025 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010026 }
10027 }
10028
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010029 if (kvm_page_track_create_memslot(slot, npages))
10030 goto out_free;
10031
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010032 return 0;
10033
10034out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010035 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010036 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010037 slot->arch.rmap[i] = NULL;
10038 if (i == 0)
10039 continue;
10040
Thomas Huth548ef282015-02-24 21:29:25 +010010041 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010042 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010043 }
10044 return -ENOMEM;
10045}
10046
Sean Christopherson15248252019-02-05 12:54:17 -080010047void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010048{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010049 struct kvm_vcpu *vcpu;
10050 int i;
10051
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010052 /*
10053 * memslots->generation has been incremented.
10054 * mmio generation may have reached its maximum value.
10055 */
Sean Christopherson15248252019-02-05 12:54:17 -080010056 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010057
10058 /* Force re-initialization of steal_time cache */
10059 kvm_for_each_vcpu(i, vcpu, kvm)
10060 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010061}
10062
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010063int kvm_arch_prepare_memory_region(struct kvm *kvm,
10064 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010065 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010066 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010067{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010068 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10069 return kvm_alloc_memslot_metadata(memslot,
10070 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010071 return 0;
10072}
10073
Kai Huang88178fd2015-01-28 10:54:27 +080010074static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
10075 struct kvm_memory_slot *new)
10076{
10077 /* Still write protect RO slot */
10078 if (new->flags & KVM_MEM_READONLY) {
Sean Christopherson3bae0452020-04-27 17:54:22 -070010079 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K);
Kai Huang88178fd2015-01-28 10:54:27 +080010080 return;
10081 }
10082
10083 /*
10084 * Call kvm_x86_ops dirty logging hooks when they are valid.
10085 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010086 * kvm_x86_ops.slot_disable_log_dirty is called when:
Kai Huang88178fd2015-01-28 10:54:27 +080010087 *
10088 * - KVM_MR_CREATE with dirty logging is disabled
10089 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10090 *
10091 * The reason is, in case of PML, we need to set D-bit for any slots
10092 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010093 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010094 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010095 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010096 * logging disabled for memory slots.
10097 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010098 * kvm_x86_ops.slot_enable_log_dirty is called when switching new slot
Kai Huang88178fd2015-01-28 10:54:27 +080010099 * to dirty logging mode.
10100 *
10101 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10102 *
10103 * In case of write protect:
10104 *
10105 * Write protect all pages for dirty logging.
10106 *
10107 * All the sptes including the large sptes which point to this
10108 * slot are set to readonly. We can not create any new large
10109 * spte on this slot until the end of the logging.
10110 *
10111 * See the comments in fast_page_fault().
10112 */
10113 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010114 if (kvm_x86_ops.slot_enable_log_dirty) {
10115 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010116 } else {
10117 int level =
10118 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
Sean Christopherson3bae0452020-04-27 17:54:22 -070010119 PG_LEVEL_2M : PG_LEVEL_4K;
Jay Zhou3c9bd402020-02-27 09:32:27 +080010120
10121 /*
10122 * If we're with initial-all-set, we don't need
10123 * to write protect any small page because
10124 * they're reported as dirty already. However
10125 * we still need to write-protect huge pages
10126 * so that the page split can happen lazily on
10127 * the first write to the huge page.
10128 */
10129 kvm_mmu_slot_remove_write_access(kvm, new, level);
10130 }
Kai Huang88178fd2015-01-28 10:54:27 +080010131 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010132 if (kvm_x86_ops.slot_disable_log_dirty)
10133 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010134 }
10135}
10136
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010137void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010138 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010139 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010140 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010141 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010142{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010143 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010144 kvm_mmu_change_mmu_pages(kvm,
10145 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010146
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010147 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010148 * Dirty logging tracks sptes in 4k granularity, meaning that large
10149 * sptes have to be split. If live migration is successful, the guest
10150 * in the source machine will be destroyed and large sptes will be
10151 * created in the destination. However, if the guest continues to run
10152 * in the source machine (for example if live migration fails), small
10153 * sptes will remain around and cause bad performance.
10154 *
10155 * Scan sptes if dirty logging has been stopped, dropping those
10156 * which can be collapsed into a single large-page spte. Later
10157 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010158 *
10159 * There is no need to do this in any of the following cases:
10160 * CREATE: No dirty mappings will already exist.
10161 * MOVE/DELETE: The old mappings will already have been cleaned up by
10162 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010163 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010164 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010165 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10166 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10167 kvm_mmu_zap_collapsible_sptes(kvm, new);
10168
10169 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010170 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010171 *
Kai Huang88178fd2015-01-28 10:54:27 +080010172 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10173 * been zapped so no dirty logging staff is needed for old slot. For
10174 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10175 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010176 *
10177 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010178 */
Kai Huang88178fd2015-01-28 10:54:27 +080010179 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010180 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010181
10182 /* Free the arrays associated with the old memslot. */
10183 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010184 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010185}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010186
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010187void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010188{
Sean Christopherson7390de12019-02-05 13:01:31 -080010189 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010190}
10191
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010192void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10193 struct kvm_memory_slot *slot)
10194{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010195 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010196}
10197
Liran Alone6c67d82018-09-04 10:56:52 +030010198static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10199{
10200 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010201 kvm_x86_ops.guest_apic_has_interrupt &&
10202 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010203}
10204
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010205static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10206{
10207 if (!list_empty_careful(&vcpu->async_pf.done))
10208 return true;
10209
10210 if (kvm_apic_has_events(vcpu))
10211 return true;
10212
10213 if (vcpu->arch.pv.pv_unhalted)
10214 return true;
10215
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010216 if (vcpu->arch.exception.pending)
10217 return true;
10218
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010219 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10220 (vcpu->arch.nmi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010221 kvm_x86_ops.nmi_allowed(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010222 return true;
10223
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010224 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040010225 (vcpu->arch.smi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010226 kvm_x86_ops.smi_allowed(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010227 return true;
10228
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010229 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010230 (kvm_cpu_has_interrupt(vcpu) ||
10231 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010232 return true;
10233
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010234 if (kvm_hv_has_stimer_pending(vcpu))
10235 return true;
10236
Sean Christophersond2060bd2020-04-22 19:25:39 -070010237 if (is_guest_mode(vcpu) &&
10238 kvm_x86_ops.nested_ops->hv_timer_pending &&
10239 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
10240 return true;
10241
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010242 return false;
10243}
10244
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010245int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10246{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010247 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010248}
Zhang Xiantao57361992007-12-17 14:21:40 +080010249
Wanpeng Li17e433b2019-08-05 10:03:19 +080010250bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10251{
10252 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10253 return true;
10254
10255 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10256 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10257 kvm_test_request(KVM_REQ_EVENT, vcpu))
10258 return true;
10259
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010260 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010261 return true;
10262
10263 return false;
10264}
10265
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010266bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10267{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010268 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010269}
10270
Christoffer Dallb6d33832012-03-08 16:44:24 -050010271int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010272{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010273 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010274}
Gleb Natapov78646122009-03-23 12:12:11 +020010275
10276int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10277{
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010278 return kvm_x86_ops.interrupt_allowed(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020010279}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010280
Nadav Amit82b32772014-11-02 11:54:45 +020010281unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10282{
10283 if (is_64_bit_mode(vcpu))
10284 return kvm_rip_read(vcpu);
10285 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10286 kvm_rip_read(vcpu));
10287}
10288EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10289
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010290bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10291{
Nadav Amit82b32772014-11-02 11:54:45 +020010292 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010293}
10294EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10295
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010296unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10297{
10298 unsigned long rflags;
10299
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010300 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010301 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010302 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010303 return rflags;
10304}
10305EXPORT_SYMBOL_GPL(kvm_get_rflags);
10306
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010307static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010308{
10309 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010310 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010311 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010312 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010313}
10314
10315void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10316{
10317 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010318 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010319}
10320EXPORT_SYMBOL_GPL(kvm_set_rflags);
10321
Gleb Natapov56028d02010-10-17 18:13:42 +020010322void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10323{
10324 int r;
10325
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010326 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010327 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010328 return;
10329
10330 r = kvm_mmu_reload(vcpu);
10331 if (unlikely(r))
10332 return;
10333
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010334 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010335 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010336 return;
10337
Sean Christopherson7a026742020-02-06 14:14:34 -080010338 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010339}
10340
Gleb Natapovaf585b92010-10-14 11:22:46 +020010341static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10342{
Peter Xudd03bca2020-04-16 11:58:59 -040010343 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
10344
Gleb Natapovaf585b92010-10-14 11:22:46 +020010345 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10346}
10347
10348static inline u32 kvm_async_pf_next_probe(u32 key)
10349{
Peter Xudd03bca2020-04-16 11:58:59 -040010350 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010351}
10352
10353static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10354{
10355 u32 key = kvm_async_pf_hash_fn(gfn);
10356
10357 while (vcpu->arch.apf.gfns[key] != ~0)
10358 key = kvm_async_pf_next_probe(key);
10359
10360 vcpu->arch.apf.gfns[key] = gfn;
10361}
10362
10363static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10364{
10365 int i;
10366 u32 key = kvm_async_pf_hash_fn(gfn);
10367
Peter Xudd03bca2020-04-16 11:58:59 -040010368 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010369 (vcpu->arch.apf.gfns[key] != gfn &&
10370 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010371 key = kvm_async_pf_next_probe(key);
10372
10373 return key;
10374}
10375
10376bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10377{
10378 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10379}
10380
10381static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10382{
10383 u32 i, j, k;
10384
10385 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040010386
10387 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
10388 return;
10389
Gleb Natapovaf585b92010-10-14 11:22:46 +020010390 while (true) {
10391 vcpu->arch.apf.gfns[i] = ~0;
10392 do {
10393 j = kvm_async_pf_next_probe(j);
10394 if (vcpu->arch.apf.gfns[j] == ~0)
10395 return;
10396 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10397 /*
10398 * k lies cyclically in ]i,j]
10399 * | i.k.j |
10400 * |....j i.k.| or |.k..j i...|
10401 */
10402 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10403 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10404 i = j;
10405 }
10406}
10407
Gleb Natapov7c907052010-10-14 11:22:53 +020010408static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10409{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010410
10411 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10412 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010413}
10414
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010415static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10416{
10417
10418 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10419 sizeof(u32));
10420}
10421
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010422static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10423{
10424 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10425 return false;
10426
10427 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10428 (vcpu->arch.apf.send_user_only &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010429 kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010430 return false;
10431
10432 return true;
10433}
10434
10435bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10436{
10437 if (unlikely(!lapic_in_kernel(vcpu) ||
10438 kvm_event_needs_reinjection(vcpu) ||
10439 vcpu->arch.exception.pending))
10440 return false;
10441
10442 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10443 return false;
10444
10445 /*
10446 * If interrupts are off we cannot even use an artificial
10447 * halt state.
10448 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010449 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010450}
10451
Gleb Natapovaf585b92010-10-14 11:22:46 +020010452void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10453 struct kvm_async_pf *work)
10454{
Avi Kivity6389ee92010-11-29 16:12:30 +020010455 struct x86_exception fault;
10456
Sean Christopherson736c2912019-12-06 15:57:14 -080010457 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010458 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010459
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010460 if (kvm_can_deliver_async_pf(vcpu) &&
10461 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010462 fault.vector = PF_VECTOR;
10463 fault.error_code_valid = true;
10464 fault.error_code = 0;
10465 fault.nested_page_fault = false;
10466 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010467 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010468 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010469 } else {
10470 /*
10471 * It is not possible to deliver a paravirtualized asynchronous
10472 * page fault, but putting the guest in an artificial halt state
10473 * can be beneficial nevertheless: if an interrupt arrives, we
10474 * can deliver it timely and perhaps the guest will schedule
10475 * another process. When the instruction that triggered a page
10476 * fault is retried, hopefully the page will be ready in the host.
10477 */
10478 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010479 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010480}
10481
10482void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10483 struct kvm_async_pf *work)
10484{
Avi Kivity6389ee92010-11-29 16:12:30 +020010485 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010486 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010487
chai wenf2e10662013-10-14 22:22:33 +080010488 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010489 work->arch.token = ~0; /* broadcast wakeup */
10490 else
10491 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010492 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010493
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010494 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10495 !apf_get_user(vcpu, &val)) {
10496 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10497 vcpu->arch.exception.pending &&
10498 vcpu->arch.exception.nr == PF_VECTOR &&
10499 !apf_put_user(vcpu, 0)) {
10500 vcpu->arch.exception.injected = false;
10501 vcpu->arch.exception.pending = false;
10502 vcpu->arch.exception.nr = 0;
10503 vcpu->arch.exception.has_error_code = false;
10504 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010505 vcpu->arch.exception.has_payload = false;
10506 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010507 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10508 fault.vector = PF_VECTOR;
10509 fault.error_code_valid = true;
10510 fault.error_code = 0;
10511 fault.nested_page_fault = false;
10512 fault.address = work->arch.token;
10513 fault.async_page_fault = true;
10514 kvm_inject_page_fault(vcpu, &fault);
10515 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010516 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010517 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010518 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010519}
10520
10521bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10522{
10523 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10524 return true;
10525 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010526 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010527}
10528
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010529void kvm_arch_start_assignment(struct kvm *kvm)
10530{
10531 atomic_inc(&kvm->arch.assigned_device_count);
10532}
10533EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10534
10535void kvm_arch_end_assignment(struct kvm *kvm)
10536{
10537 atomic_dec(&kvm->arch.assigned_device_count);
10538}
10539EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10540
10541bool kvm_arch_has_assigned_device(struct kvm *kvm)
10542{
10543 return atomic_read(&kvm->arch.assigned_device_count);
10544}
10545EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10546
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010547void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10548{
10549 atomic_inc(&kvm->arch.noncoherent_dma_count);
10550}
10551EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10552
10553void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10554{
10555 atomic_dec(&kvm->arch.noncoherent_dma_count);
10556}
10557EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10558
10559bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10560{
10561 return atomic_read(&kvm->arch.noncoherent_dma_count);
10562}
10563EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10564
Alex Williamson14717e22016-05-05 11:58:35 -060010565bool kvm_arch_has_irq_bypass(void)
10566{
Sean Christopherson92735b12019-08-02 15:06:17 -070010567 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010568}
10569
Feng Wu87276882015-09-18 22:29:40 +080010570int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10571 struct irq_bypass_producer *prod)
10572{
10573 struct kvm_kernel_irqfd *irqfd =
10574 container_of(cons, struct kvm_kernel_irqfd, consumer);
10575
Alex Williamson14717e22016-05-05 11:58:35 -060010576 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010577
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010578 return kvm_x86_ops.update_pi_irte(irqfd->kvm,
Alex Williamson14717e22016-05-05 11:58:35 -060010579 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010580}
10581
10582void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10583 struct irq_bypass_producer *prod)
10584{
10585 int ret;
10586 struct kvm_kernel_irqfd *irqfd =
10587 container_of(cons, struct kvm_kernel_irqfd, consumer);
10588
Feng Wu87276882015-09-18 22:29:40 +080010589 WARN_ON(irqfd->producer != prod);
10590 irqfd->producer = NULL;
10591
10592 /*
10593 * When producer of consumer is unregistered, we change back to
10594 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010595 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010596 * int this case doesn't want to receive the interrupts.
10597 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010598 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080010599 if (ret)
10600 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10601 " fails: %d\n", irqfd->consumer.token, ret);
10602}
10603
10604int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10605 uint32_t guest_irq, bool set)
10606{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010607 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080010608}
10609
Feng Wu520040142016-01-25 16:53:33 +080010610bool kvm_vector_hashing_enabled(void)
10611{
10612 return vector_hashing;
10613}
Feng Wu520040142016-01-25 16:53:33 +080010614
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010615bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10616{
10617 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10618}
10619EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10620
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010621u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10622{
10623 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10624
10625 /* The STIBP bit doesn't fault even if it's not advertised */
10626 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10627 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10628 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10629 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10630 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10631 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10632
10633 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10634 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10635 bits &= ~SPEC_CTRL_SSBD;
10636 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10637 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10638 bits &= ~SPEC_CTRL_SSBD;
10639
10640 return bits;
10641}
10642EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010643
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010644EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010645EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010646EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10647EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10648EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10649EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010650EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010651EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010652EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010653EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010654EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010655EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010656EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010657EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010658EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010659EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010660EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010661EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010662EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10663EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010664EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010665EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);