blob: 93bbbce67a03eb34b315f746acca05c2da8f9e23 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Carsten Otte043405e2007-10-10 17:16:19 +02002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * derived from drivers/kvm/kvm_main.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03008 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020011 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030015 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020020#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080022#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020023#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030025#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020026#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020027#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030028#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020029
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020030#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030031#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020032#include <linux/kvm.h>
33#include <linux/fs.h>
34#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040035#include <linux/export.h>
36#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080037#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050038#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010039#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030040#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010041#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030042#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020043#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080045#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080046#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020047#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020048#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020049#include <linux/timekeeper_internal.h>
50#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080051#include <linux/kvm_irqfd.h>
52#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010053#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080054#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050055#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010056
Avi Kivityaec51dc2009-07-01 16:01:02 +030057#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080058
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020059#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080060#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020061#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080062#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020063#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020064#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100065#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030066#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080067#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010068#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010069#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080070#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090071#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000072#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020073
Dave Hansend1898b72016-06-01 10:42:20 -070074#define CREATE_TRACE_POINTS
75#include "trace.h"
76
Carsten Otte313a3dc2007-10-11 19:16:52 +020077#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080078#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080079u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
80EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080081
Avi Kivity0f65dd72011-04-20 13:37:53 +030082#define emul_to_vcpu(ctxt) \
83 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
84
Joerg Roedel50a37eb2008-01-31 14:57:38 +010085/* EFER defaults:
86 * - enable syscall per default because its emulated by KVM
87 * - enable LME and LMA per default on 64 bit KVM
88 */
89#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080090static
91u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010092#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080093static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020095
Paolo Bonzini833b45d2019-09-30 18:48:44 +020096#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
97#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
Hollis Blanchard417bc302007-10-31 17:24:23 -050098
Radim Krčmářc5192652016-07-12 22:09:28 +020099#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
100 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200101
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300102static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300103static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200104static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100105static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800106static void store_regs(struct kvm_vcpu *vcpu);
107static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200108
Paolo Bonzini893590c2015-11-06 11:46:24 +0100109struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300110EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800111
Paolo Bonzini893590c2015-11-06 11:46:24 +0100112static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030113module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200114
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200115static bool __read_mostly report_ignored_msrs = true;
116module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
117
Wanpeng Li4c276252018-05-05 04:02:32 -0700118unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200119module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
120
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300121static bool __read_mostly kvmclock_periodic_sync = true;
122module_param(kvmclock_periodic_sync, bool, S_IRUGO);
123
Paolo Bonzini893590c2015-11-06 11:46:24 +0100124bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100125EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100126u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100127EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800128u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
129EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
130u64 __read_mostly kvm_max_tsc_scaling_ratio;
131EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700132u64 __read_mostly kvm_default_tsc_scaling_ratio;
133EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100134
Zachary Amsdencc578282012-02-03 15:43:50 -0200135/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100136static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200137module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
138
Sean Christophersonc3941d92019-04-17 10:15:33 -0700139/*
140 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
141 * adaptive tuning starting from default advancment of 1000ns. '0' disables
142 * advancement entirely. Any other value is used as-is and disables adaptive
143 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
144 */
145static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800146module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500147
Feng Wu520040142016-01-25 16:53:33 +0800148static bool __read_mostly vector_hashing = true;
149module_param(vector_hashing, bool, S_IRUGO);
150
Liran Alonc4ae60e2018-03-12 13:12:47 +0200151bool __read_mostly enable_vmware_backdoor = false;
152module_param(enable_vmware_backdoor, bool, S_IRUGO);
153EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
154
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700155static bool __read_mostly force_emulation_prefix = false;
156module_param(force_emulation_prefix, bool, S_IRUGO);
157
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800158int __read_mostly pi_inject_timer = -1;
159module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
160
Avi Kivity18863bd2009-09-07 11:12:18 +0300161#define KVM_NR_SHARED_MSRS 16
162
163struct kvm_shared_msrs_global {
164 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800165 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300166};
167
168struct kvm_shared_msrs {
169 struct user_return_notifier urn;
170 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800171 struct kvm_shared_msr_values {
172 u64 host;
173 u64 curr;
174 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300175};
176
177static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200178static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300179
Aaron Lewis139a12c2019-10-21 16:30:25 -0700180static u64 __read_mostly host_xss;
181
Hollis Blanchard417bc302007-10-31 17:24:23 -0500182struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200183 { "pf_fixed", VCPU_STAT(pf_fixed) },
184 { "pf_guest", VCPU_STAT(pf_guest) },
185 { "tlb_flush", VCPU_STAT(tlb_flush) },
186 { "invlpg", VCPU_STAT(invlpg) },
187 { "exits", VCPU_STAT(exits) },
188 { "io_exits", VCPU_STAT(io_exits) },
189 { "mmio_exits", VCPU_STAT(mmio_exits) },
190 { "signal_exits", VCPU_STAT(signal_exits) },
191 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800192 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200193 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100194 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200195 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200196 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200197 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530198 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200199 { "request_irq", VCPU_STAT(request_irq_exits) },
200 { "irq_exits", VCPU_STAT(irq_exits) },
201 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200202 { "fpu_reload", VCPU_STAT(fpu_reload) },
203 { "insn_emulation", VCPU_STAT(insn_emulation) },
204 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300205 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200206 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100207 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200208 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200209 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
210 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
211 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
212 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
213 { "mmu_flooded", VM_STAT(mmu_flooded) },
214 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200215 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300216 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200217 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200218 { "largepages", VM_STAT(lpages, .mode = 0444) },
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100219 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
David Matlackf3414bc2016-12-20 15:25:57 -0800220 { "max_mmu_page_hash_collisions",
221 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500222 { NULL }
223};
224
Dexuan Cui2acf9232010-06-10 11:27:12 +0800225u64 __read_mostly host_xcr0;
226
Marc Orrb666a4b2018-11-06 14:53:56 -0800227struct kmem_cache *x86_fpu_cache;
228EXPORT_SYMBOL_GPL(x86_fpu_cache);
229
Jan Kiszkab6785de2012-09-20 07:43:17 +0200230static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300231
Gleb Natapovaf585b92010-10-14 11:22:46 +0200232static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
233{
234 int i;
235 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
236 vcpu->arch.apf.gfns[i] = ~0;
237}
238
Avi Kivity18863bd2009-09-07 11:12:18 +0300239static void kvm_on_user_return(struct user_return_notifier *urn)
240{
241 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300242 struct kvm_shared_msrs *locals
243 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800244 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700245 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300246
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700247 /*
248 * Disabling irqs at this point since the following code could be
249 * interrupted and executed through kvm_arch_hardware_disable()
250 */
251 local_irq_save(flags);
252 if (locals->registered) {
253 locals->registered = false;
254 user_return_notifier_unregister(urn);
255 }
256 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300257 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800258 values = &locals->values[slot];
259 if (values->host != values->curr) {
260 wrmsrl(shared_msrs_global.msrs[slot], values->host);
261 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300262 }
263 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300264}
265
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800266void kvm_define_shared_msr(unsigned slot, u32 msr)
267{
Nadav Amit0123be42014-07-24 15:06:56 +0300268 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200269 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300270 if (slot >= shared_msrs_global.nr)
271 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300272}
273EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
274
275static void kvm_shared_msr_cpu_online(void)
276{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800277 unsigned int cpu = smp_processor_id();
278 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
279 u64 value;
280 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300281
Sean Christopherson05c19c22019-11-22 12:04:50 -0800282 for (i = 0; i < shared_msrs_global.nr; ++i) {
283 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
284 smsr->values[i].host = value;
285 smsr->values[i].curr = value;
286 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300287}
288
Andy Honig8b3c3102014-08-27 11:16:44 -0700289int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300290{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200291 unsigned int cpu = smp_processor_id();
292 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700293 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300294
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500295 value = (value & mask) | (smsr->values[slot].host & ~mask);
296 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700297 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700298 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
299 if (err)
300 return 1;
301
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500302 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300303 if (!smsr->registered) {
304 smsr->urn.on_user_return = kvm_on_user_return;
305 user_return_notifier_register(&smsr->urn);
306 smsr->registered = true;
307 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700308 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300309}
310EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
311
Radim Krčmář13a34e02014-08-28 15:13:03 +0200312static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200313{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200314 unsigned int cpu = smp_processor_id();
315 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200316
317 if (smsr->registered)
318 kvm_on_user_return(&smsr->urn);
319}
320
Carsten Otte6866b832007-10-29 16:09:10 +0100321u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
322{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300323 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100324}
325EXPORT_SYMBOL_GPL(kvm_get_apic_base);
326
Jim Mattson58871642018-05-09 16:56:04 -0400327enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
328{
329 return kvm_apic_mode(kvm_get_apic_base(vcpu));
330}
331EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
332
Jan Kiszka58cb6282014-01-24 16:48:44 +0100333int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100334{
Jim Mattson58871642018-05-09 16:56:04 -0400335 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
336 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200337 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
338 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100339
Jim Mattson58871642018-05-09 16:56:04 -0400340 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700341 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400342 if (!msr_info->host_initiated) {
343 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
344 return 1;
345 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
346 return 1;
347 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100348
349 kvm_lapic_set_base(vcpu, msr_info->data);
350 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100351}
352EXPORT_SYMBOL_GPL(kvm_set_apic_base);
353
Andi Kleen2605fc22014-05-02 00:44:37 +0200354asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000355{
356 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200357 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000358}
359EXPORT_SYMBOL_GPL(kvm_spurious_fault);
360
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200361#define EXCPT_BENIGN 0
362#define EXCPT_CONTRIBUTORY 1
363#define EXCPT_PF 2
364
365static int exception_class(int vector)
366{
367 switch (vector) {
368 case PF_VECTOR:
369 return EXCPT_PF;
370 case DE_VECTOR:
371 case TS_VECTOR:
372 case NP_VECTOR:
373 case SS_VECTOR:
374 case GP_VECTOR:
375 return EXCPT_CONTRIBUTORY;
376 default:
377 break;
378 }
379 return EXCPT_BENIGN;
380}
381
Nadav Amitd6e8c852014-07-24 14:51:24 +0300382#define EXCPT_FAULT 0
383#define EXCPT_TRAP 1
384#define EXCPT_ABORT 2
385#define EXCPT_INTERRUPT 3
386
387static int exception_type(int vector)
388{
389 unsigned int mask;
390
391 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
392 return EXCPT_INTERRUPT;
393
394 mask = 1 << vector;
395
396 /* #DB is trap, as instruction watchpoints are handled elsewhere */
397 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
398 return EXCPT_TRAP;
399
400 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
401 return EXCPT_ABORT;
402
403 /* Reserved exceptions will result in fault */
404 return EXCPT_FAULT;
405}
406
Jim Mattsonda998b42018-10-16 14:29:22 -0700407void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
408{
409 unsigned nr = vcpu->arch.exception.nr;
410 bool has_payload = vcpu->arch.exception.has_payload;
411 unsigned long payload = vcpu->arch.exception.payload;
412
413 if (!has_payload)
414 return;
415
416 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700417 case DB_VECTOR:
418 /*
419 * "Certain debug exceptions may clear bit 0-3. The
420 * remaining contents of the DR6 register are never
421 * cleared by the processor".
422 */
423 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
424 /*
425 * DR6.RTM is set by all #DB exceptions that don't clear it.
426 */
427 vcpu->arch.dr6 |= DR6_RTM;
428 vcpu->arch.dr6 |= payload;
429 /*
430 * Bit 16 should be set in the payload whenever the #DB
431 * exception should clear DR6.RTM. This makes the payload
432 * compatible with the pending debug exceptions under VMX.
433 * Though not currently documented in the SDM, this also
434 * makes the payload compatible with the exit qualification
435 * for #DB exceptions under VMX.
436 */
437 vcpu->arch.dr6 ^= payload & DR6_RTM;
438 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700439 case PF_VECTOR:
440 vcpu->arch.cr2 = payload;
441 break;
442 }
443
444 vcpu->arch.exception.has_payload = false;
445 vcpu->arch.exception.payload = 0;
446}
447EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
448
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200449static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200450 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700451 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200452{
453 u32 prev_nr;
454 int class1, class2;
455
Avi Kivity3842d132010-07-27 12:30:24 +0300456 kvm_make_request(KVM_REQ_EVENT, vcpu);
457
Wanpeng Li664f8e22017-08-24 03:35:09 -0700458 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200459 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200460 if (has_error && !is_protmode(vcpu))
461 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700462 if (reinject) {
463 /*
464 * On vmentry, vcpu->arch.exception.pending is only
465 * true if an event injection was blocked by
466 * nested_run_pending. In that case, however,
467 * vcpu_enter_guest requests an immediate exit,
468 * and the guest shouldn't proceed far enough to
469 * need reinjection.
470 */
471 WARN_ON_ONCE(vcpu->arch.exception.pending);
472 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700473 if (WARN_ON_ONCE(has_payload)) {
474 /*
475 * A reinjected event has already
476 * delivered its payload.
477 */
478 has_payload = false;
479 payload = 0;
480 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700481 } else {
482 vcpu->arch.exception.pending = true;
483 vcpu->arch.exception.injected = false;
484 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200485 vcpu->arch.exception.has_error_code = has_error;
486 vcpu->arch.exception.nr = nr;
487 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700488 vcpu->arch.exception.has_payload = has_payload;
489 vcpu->arch.exception.payload = payload;
Jim Mattsonda998b42018-10-16 14:29:22 -0700490 /*
491 * In guest mode, payload delivery should be deferred,
492 * so that the L1 hypervisor can intercept #PF before
Jim Mattsonf10c7292018-10-16 14:29:23 -0700493 * CR2 is modified (or intercept #DB before DR6 is
494 * modified under nVMX). However, for ABI
495 * compatibility with KVM_GET_VCPU_EVENTS and
496 * KVM_SET_VCPU_EVENTS, we can't delay payload
497 * delivery unless userspace has enabled this
498 * functionality via the per-VM capability,
499 * KVM_CAP_EXCEPTION_PAYLOAD.
Jim Mattsonda998b42018-10-16 14:29:22 -0700500 */
501 if (!vcpu->kvm->arch.exception_payload_enabled ||
502 !is_guest_mode(vcpu))
503 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200504 return;
505 }
506
507 /* to check exception */
508 prev_nr = vcpu->arch.exception.nr;
509 if (prev_nr == DF_VECTOR) {
510 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300511 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200512 return;
513 }
514 class1 = exception_class(prev_nr);
515 class2 = exception_class(nr);
516 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
517 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700518 /*
519 * Generate double fault per SDM Table 5-5. Set
520 * exception.pending = true so that the double fault
521 * can trigger a nested vmexit.
522 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200523 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700524 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200525 vcpu->arch.exception.has_error_code = true;
526 vcpu->arch.exception.nr = DF_VECTOR;
527 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700528 vcpu->arch.exception.has_payload = false;
529 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200530 } else
531 /* replace previous exception with a new one in a hope
532 that instruction re-execution will regenerate lost
533 exception */
534 goto queue;
535}
536
Avi Kivity298101d2007-11-25 13:41:11 +0200537void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
538{
Jim Mattson91e86d22018-10-16 14:29:21 -0700539 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200540}
541EXPORT_SYMBOL_GPL(kvm_queue_exception);
542
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200543void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
544{
Jim Mattson91e86d22018-10-16 14:29:21 -0700545 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200546}
547EXPORT_SYMBOL_GPL(kvm_requeue_exception);
548
Jim Mattsonf10c7292018-10-16 14:29:23 -0700549static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
550 unsigned long payload)
551{
552 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
553}
554
Jim Mattsonda998b42018-10-16 14:29:22 -0700555static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
556 u32 error_code, unsigned long payload)
557{
558 kvm_multiple_exception(vcpu, nr, true, error_code,
559 true, payload, false);
560}
561
Kyle Huey6affcbe2016-11-29 12:40:40 -0800562int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200563{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100564 if (err)
565 kvm_inject_gp(vcpu, 0);
566 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800567 return kvm_skip_emulated_instruction(vcpu);
568
569 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100570}
571EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200572
Avi Kivity6389ee92010-11-29 16:12:30 +0200573void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200574{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200575 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700576 vcpu->arch.exception.nested_apf =
577 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700578 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700579 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700580 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
581 } else {
582 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
583 fault->address);
584 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200585}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300586EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200587
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200588static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200589{
Avi Kivity6389ee92010-11-29 16:12:30 +0200590 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
591 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200592 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200593 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200594
595 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200596}
597
Sheng Yang3419ffc2008-05-15 09:52:48 +0800598void kvm_inject_nmi(struct kvm_vcpu *vcpu)
599{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300600 atomic_inc(&vcpu->arch.nmi_queued);
601 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800602}
603EXPORT_SYMBOL_GPL(kvm_inject_nmi);
604
Avi Kivity298101d2007-11-25 13:41:11 +0200605void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
606{
Jim Mattson91e86d22018-10-16 14:29:21 -0700607 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200608}
609EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
610
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200611void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
612{
Jim Mattson91e86d22018-10-16 14:29:21 -0700613 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200614}
615EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
616
Carsten Ottea03490e2007-10-29 16:09:35 +0100617/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300618 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
619 * a #GP and return false.
620 */
621bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200622{
Avi Kivity0a79b002009-09-01 12:03:25 +0300623 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
624 return true;
625 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
626 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100627}
Avi Kivity0a79b002009-09-01 12:03:25 +0300628EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100629
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300630bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
631{
632 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
633 return true;
634
635 kvm_queue_exception(vcpu, UD_VECTOR);
636 return false;
637}
638EXPORT_SYMBOL_GPL(kvm_require_dr);
639
Carsten Ottea03490e2007-10-29 16:09:35 +0100640/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200641 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200642 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200643 * can read from guest physical or from the guest's guest physical memory.
644 */
645int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
646 gfn_t ngfn, void *data, int offset, int len,
647 u32 access)
648{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200649 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200650 gfn_t real_gfn;
651 gpa_t ngpa;
652
653 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200654 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200655 if (real_gfn == UNMAPPED_GVA)
656 return -EFAULT;
657
658 real_gfn = gpa_to_gfn(real_gfn);
659
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200660 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200661}
662EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
663
Fengguang Wu69b00492015-01-19 22:33:39 +0800664static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200665 void *data, int offset, int len, u32 access)
666{
667 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
668 data, offset, len, access);
669}
670
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700671static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
672{
673 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
674 rsvd_bits(1, 2);
675}
676
Joerg Roedelec92fe42010-09-10 17:30:51 +0200677/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700678 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100679 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200680int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100681{
682 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
683 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
684 int i;
685 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200686 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100687
Joerg Roedelff03a072010-09-10 17:30:57 +0200688 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
689 offset * sizeof(u64), sizeof(pdpte),
690 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100691 if (ret < 0) {
692 ret = 0;
693 goto out;
694 }
695 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400696 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700697 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100698 ret = 0;
699 goto out;
700 }
701 }
702 ret = 1;
703
Joerg Roedelff03a072010-09-10 17:30:57 +0200704 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700705 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
706
Carsten Ottea03490e2007-10-29 16:09:35 +0100707out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100708
709 return ret;
710}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100711EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100712
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100713bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200714{
Joerg Roedelff03a072010-09-10 17:30:57 +0200715 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200716 int offset;
717 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200718 int r;
719
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200720 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200721 return false;
722
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700723 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300724 return true;
725
Paolo Bonzinia5121772017-07-24 18:54:38 +0200726 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
727 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200728 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
729 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200730 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800731 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200732
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800733 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200734}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100735EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200736
Avi Kivity49a9b072010-06-10 17:02:14 +0300737int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100738{
Sheng Yangaad82702010-05-12 16:40:42 +0800739 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800740 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800741
Avi Kivityf9a48e62010-01-06 19:10:22 +0200742 cr0 |= X86_CR0_ET;
743
Gleb Natapovab344822010-01-21 15:28:46 +0200744#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300745 if (cr0 & 0xffffffff00000000UL)
746 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200747#endif
748
749 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100750
Gleb Natapov0f122442010-04-28 19:15:31 +0300751 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
752 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100753
Gleb Natapov0f122442010-04-28 19:15:31 +0300754 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
755 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100756
757 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
758#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200759 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100760 int cs_db, cs_l;
761
Gleb Natapov0f122442010-04-28 19:15:31 +0300762 if (!is_pae(vcpu))
763 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100764 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300765 if (cs_l)
766 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100767 } else
768#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200769 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200770 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300771 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100772 }
773
Mao, Junjiead756a12012-07-02 01:18:48 +0000774 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
775 return 1;
776
Carsten Ottea03490e2007-10-29 16:09:35 +0100777 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100778
Lai Jiangshand170c412011-02-21 11:21:30 +0800779 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800780 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800781 kvm_async_pf_hash_reset(vcpu);
782 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800783
Sheng Yangaad82702010-05-12 16:40:42 +0800784 if ((cr0 ^ old_cr0) & update_bits)
785 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800786
Laszlo Ersek879ae182015-11-04 12:54:41 +0100787 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
788 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
789 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800790 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
791
Gleb Natapov0f122442010-04-28 19:15:31 +0300792 return 0;
793}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200794EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100795
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200796void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100797{
Avi Kivity49a9b072010-06-10 17:02:14 +0300798 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100799}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200800EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100801
Aaron Lewis139a12c2019-10-21 16:30:25 -0700802void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300803{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700804 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300805
Aaron Lewis139a12c2019-10-21 16:30:25 -0700806 if (vcpu->arch.xcr0 != host_xcr0)
807 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
808
809 if (vcpu->arch.xsaves_enabled &&
810 vcpu->arch.ia32_xss != host_xss)
811 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
812 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300813}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700814EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
815
816void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
817{
818 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
819
820 if (vcpu->arch.xcr0 != host_xcr0)
821 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
822
823 if (vcpu->arch.xsaves_enabled &&
824 vcpu->arch.ia32_xss != host_xss)
825 wrmsrl(MSR_IA32_XSS, host_xss);
826 }
827
828}
829EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300830
Fengguang Wu69b00492015-01-19 22:33:39 +0800831static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800832{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000833 u64 xcr0 = xcr;
834 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200835 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800836
837 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
838 if (index != XCR_XFEATURE_ENABLED_MASK)
839 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700840 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800841 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700842 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800843 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200844
845 /*
846 * Do not allow the guest to set bits that we do not support
847 * saving. However, xcr0 bit 0 is always set, even if the
848 * emulated CPU does not support XSAVE (see fx_init).
849 */
Dave Hansend91cab72015-09-02 16:31:26 -0700850 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200851 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800852 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200853
Dave Hansend91cab72015-09-02 16:31:26 -0700854 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
855 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000856 return 1;
857
Dave Hansend91cab72015-09-02 16:31:26 -0700858 if (xcr0 & XFEATURE_MASK_AVX512) {
859 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800860 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700861 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800862 return 1;
863 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800864 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000865
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000867 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800868 return 0;
869}
870
871int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
872{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000873 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
874 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800875 kvm_inject_gp(vcpu, 0);
876 return 1;
877 }
878 return 0;
879}
880EXPORT_SYMBOL_GPL(kvm_set_xcr);
881
Wanpeng Li3ca94192019-09-18 17:50:10 +0800882static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
883{
884 if (cr4 & CR4_RESERVED_BITS)
885 return -EINVAL;
886
887 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
888 return -EINVAL;
889
890 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
891 return -EINVAL;
892
893 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
894 return -EINVAL;
895
896 if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
897 return -EINVAL;
898
899 if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
900 return -EINVAL;
901
902 if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
903 return -EINVAL;
904
905 if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
906 return -EINVAL;
907
908 return 0;
909}
910
Avi Kivitya83b29c2010-06-10 17:02:15 +0300911int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100912{
Avi Kivityfc78f512009-12-07 12:16:48 +0200913 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800914 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800915 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800916
Wanpeng Li3ca94192019-09-18 17:50:10 +0800917 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200918 return 1;
919
Carsten Ottea03490e2007-10-29 16:09:35 +0100920 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300921 if (!(cr4 & X86_CR4_PAE))
922 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300923 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
924 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200925 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
926 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300927 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100928
Mao, Junjiead756a12012-07-02 01:18:48 +0000929 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200930 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000931 return 1;
932
933 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
934 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
935 return 1;
936 }
937
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300938 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300939 return 1;
940
Mao, Junjiead756a12012-07-02 01:18:48 +0000941 if (((cr4 ^ old_cr4) & pdptr_bits) ||
942 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800943 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300944
Huaitong Hanb9baba82016-03-22 16:51:21 +0800945 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200946 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800947
Gleb Natapov0f122442010-04-28 19:15:31 +0300948 return 0;
949}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200950EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100951
Avi Kivity23902182010-06-10 17:02:16 +0300952int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100953{
Junaid Shahidade61e22018-06-27 14:59:15 -0700954 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100955#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700956 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
957
Junaid Shahidade61e22018-06-27 14:59:15 -0700958 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700959 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
960 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -0700961 }
Paolo Bonziniac146232014-11-10 13:53:25 +0100962#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200963
Avi Kivity9f8fe502010-12-05 17:30:00 +0200964 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -0700965 if (!skip_tlb_flush) {
966 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -0700967 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -0700968 }
Gleb Natapov0f122442010-04-28 19:15:31 +0300969 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200970 }
971
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800972 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -0700973 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800974 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200975 else if (is_pae_paging(vcpu) &&
976 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300977 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100978
Junaid Shahidade61e22018-06-27 14:59:15 -0700979 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +0300980 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700981 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -0700982
Gleb Natapov0f122442010-04-28 19:15:31 +0300983 return 0;
984}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200985EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100986
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100987int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100988{
Gleb Natapov0f122442010-04-28 19:15:31 +0300989 if (cr8 & CR8_RESERVED_BITS)
990 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200991 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100992 kvm_lapic_set_tpr(vcpu, cr8);
993 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800994 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300995 return 0;
996}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200997EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100998
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200999unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001000{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001001 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001002 return kvm_lapic_get_cr8(vcpu);
1003 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001004 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001005}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001006EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001007
Nadav Amitae561ed2015-04-02 03:10:37 +03001008static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1009{
1010 int i;
1011
1012 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1013 for (i = 0; i < KVM_NR_DB_REGS; i++)
1014 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1015 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1016 }
1017}
1018
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001019static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1020{
1021 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1022 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1023}
1024
Jan Kiszkac8639012012-09-21 05:42:55 +02001025static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1026{
1027 unsigned long dr7;
1028
1029 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1030 dr7 = vcpu->arch.guest_debug_dr7;
1031 else
1032 dr7 = vcpu->arch.dr7;
1033 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001034 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1035 if (dr7 & DR7_BP_EN_MASK)
1036 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001037}
1038
Nadav Amit6f43ed02014-07-15 17:37:46 +03001039static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1040{
1041 u64 fixed = DR6_FIXED_1;
1042
Radim Krčmářd6321d42017-08-05 00:12:49 +02001043 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001044 fixed |= DR6_RTM;
1045 return fixed;
1046}
1047
Gleb Natapov338dbc92010-04-28 19:15:32 +03001048static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001049{
1050 switch (dr) {
1051 case 0 ... 3:
1052 vcpu->arch.db[dr] = val;
1053 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1054 vcpu->arch.eff_db[dr] = val;
1055 break;
1056 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001057 /* fall through */
1058 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001059 if (val & 0xffffffff00000000ULL)
1060 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001061 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001062 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001063 break;
1064 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001065 /* fall through */
1066 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +03001067 if (val & 0xffffffff00000000ULL)
1068 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001069 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001070 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001071 break;
1072 }
1073
1074 return 0;
1075}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001076
1077int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1078{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001079 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001080 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001081 return 1;
1082 }
1083 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001084}
Gleb Natapov020df072010-04-13 10:05:23 +03001085EXPORT_SYMBOL_GPL(kvm_set_dr);
1086
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001087int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001088{
1089 switch (dr) {
1090 case 0 ... 3:
1091 *val = vcpu->arch.db[dr];
1092 break;
1093 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001094 /* fall through */
1095 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001096 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1097 *val = vcpu->arch.dr6;
1098 else
1099 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001100 break;
1101 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001102 /* fall through */
1103 default: /* 7 */
1104 *val = vcpu->arch.dr7;
1105 break;
1106 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001107 return 0;
1108}
Gleb Natapov020df072010-04-13 10:05:23 +03001109EXPORT_SYMBOL_GPL(kvm_get_dr);
1110
Avi Kivity022cd0e2011-11-10 14:57:23 +02001111bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1112{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001113 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001114 u64 data;
1115 int err;
1116
Wei Huangc6702c92015-06-19 13:44:45 +02001117 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001118 if (err)
1119 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001120 kvm_rax_write(vcpu, (u32)data);
1121 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001122 return err;
1123}
1124EXPORT_SYMBOL_GPL(kvm_rdpmc);
1125
Carsten Otte043405e2007-10-10 17:16:19 +02001126/*
1127 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1128 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1129 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001130 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1131 * extract the supported MSRs from the related const lists.
1132 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001133 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001134 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001135 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001136 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001137
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001138static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001139 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001140 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001141#ifdef CONFIG_X86_64
1142 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1143#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001144 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01001145 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001146 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001147 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1148 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1149 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1150 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1151 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1152 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001153 MSR_IA32_UMWAIT_CONTROL,
1154
Jim Mattsone2ada662019-08-21 11:20:04 -07001155 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1156 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1157 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1158 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1159 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1160 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1161 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1162 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1163 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1164 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1165 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1166 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1167 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001168 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1169 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1170 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1171 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1172 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1173 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1174 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1175 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1176 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001177};
1178
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001179static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001180static unsigned num_msrs_to_save;
1181
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001182static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001183 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1184 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1185 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1186 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001187 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001188 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1189 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001190 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001191 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001192 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001193 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001194 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001195 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001196 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1197 HV_X64_MSR_TSC_EMULATION_STATUS,
1198
1199 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001200 MSR_KVM_PV_EOI_EN,
1201
Will Auldba904632012-11-29 12:42:50 -08001202 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001203 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001204 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001205 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001206 MSR_IA32_MCG_STATUS,
1207 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001208 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001209 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001210 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001211 MSR_PLATFORM_INFO,
1212 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001213 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001214 MSR_IA32_POWER_CTL,
Borislav Petkov191c8132019-04-18 18:32:50 +02001215
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001216 /*
1217 * The following list leaves out MSRs whose values are determined
1218 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1219 * We always support the "true" VMX control MSRs, even if the host
1220 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001221 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001222 */
1223 MSR_IA32_VMX_BASIC,
1224 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1225 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1226 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1227 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1228 MSR_IA32_VMX_MISC,
1229 MSR_IA32_VMX_CR0_FIXED0,
1230 MSR_IA32_VMX_CR4_FIXED0,
1231 MSR_IA32_VMX_VMCS_ENUM,
1232 MSR_IA32_VMX_PROCBASED_CTLS2,
1233 MSR_IA32_VMX_EPT_VPID_CAP,
1234 MSR_IA32_VMX_VMFUNC,
1235
Borislav Petkov191c8132019-04-18 18:32:50 +02001236 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001237 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001238};
1239
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001240static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001241static unsigned num_emulated_msrs;
1242
Tom Lendacky801e4592018-02-21 13:39:51 -06001243/*
1244 * List of msr numbers which are used to expose MSR-based features that
1245 * can be used by a hypervisor to validate requested CPU features.
1246 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001247static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001248 MSR_IA32_VMX_BASIC,
1249 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1250 MSR_IA32_VMX_PINBASED_CTLS,
1251 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1252 MSR_IA32_VMX_PROCBASED_CTLS,
1253 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1254 MSR_IA32_VMX_EXIT_CTLS,
1255 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1256 MSR_IA32_VMX_ENTRY_CTLS,
1257 MSR_IA32_VMX_MISC,
1258 MSR_IA32_VMX_CR0_FIXED0,
1259 MSR_IA32_VMX_CR0_FIXED1,
1260 MSR_IA32_VMX_CR4_FIXED0,
1261 MSR_IA32_VMX_CR4_FIXED1,
1262 MSR_IA32_VMX_VMCS_ENUM,
1263 MSR_IA32_VMX_PROCBASED_CTLS2,
1264 MSR_IA32_VMX_EPT_VPID_CAP,
1265 MSR_IA32_VMX_VMFUNC,
1266
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001267 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001268 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001269 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001270};
1271
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001272static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001273static unsigned int num_msr_based_features;
1274
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001275static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001276{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001277 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001278
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001279 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1280 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001281
1282 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001283 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1284 * the nested hypervisor runs with NX huge pages. If it is not,
1285 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1286 * L1 guests, so it need not worry about its own (L2) guests.
1287 */
1288 data |= ARCH_CAP_PSCHANGE_MC_NO;
1289
1290 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001291 * If we're doing cache flushes (either "always" or "cond")
1292 * we will do one whenever the guest does a vmlaunch/vmresume.
1293 * If an outer hypervisor is doing the cache flush for us
1294 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1295 * capability to the guest too, and if EPT is disabled we're not
1296 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1297 * require a nested hypervisor to do a flush of its own.
1298 */
1299 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1300 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1301
Paolo Bonzini0c549142019-08-19 17:24:07 +02001302 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1303 data |= ARCH_CAP_RDCL_NO;
1304 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1305 data |= ARCH_CAP_SSB_NO;
1306 if (!boot_cpu_has_bug(X86_BUG_MDS))
1307 data |= ARCH_CAP_MDS_NO;
1308
Pawan Guptae1d38b62019-10-23 12:23:33 +02001309 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001310 * On TAA affected systems:
1311 * - nothing to do if TSX is disabled on the host.
1312 * - we emulate TSX_CTRL if present on the host.
1313 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001314 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001315 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001316 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001317 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1318 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001319
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001320 return data;
1321}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001322
Wanpeng Li66421c12018-02-28 14:03:30 +08001323static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1324{
1325 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001326 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001327 msr->data = kvm_get_arch_capabilities();
1328 break;
1329 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001330 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001331 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001332 default:
1333 if (kvm_x86_ops->get_msr_feature(msr))
1334 return 1;
1335 }
1336 return 0;
1337}
1338
Tom Lendacky801e4592018-02-21 13:39:51 -06001339static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1340{
1341 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001342 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001343
1344 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001345 r = kvm_get_msr_feature(&msr);
1346 if (r)
1347 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001348
1349 *data = msr.data;
1350
1351 return 0;
1352}
1353
Sean Christopherson11988492019-04-02 08:19:15 -07001354static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1355{
1356 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1357 return false;
1358
1359 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1360 return false;
1361
Sean Christopherson0a629562019-04-02 08:19:16 -07001362 if (efer & (EFER_LME | EFER_LMA) &&
1363 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1364 return false;
1365
1366 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1367 return false;
1368
Sean Christopherson11988492019-04-02 08:19:15 -07001369 return true;
1370
1371}
Jan Kiszka384bb782013-04-20 10:52:36 +02001372bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001373{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001374 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001375 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001376
Sean Christopherson11988492019-04-02 08:19:15 -07001377 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001378}
1379EXPORT_SYMBOL_GPL(kvm_valid_efer);
1380
Sean Christopherson11988492019-04-02 08:19:15 -07001381static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001382{
1383 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001384 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001385
Sean Christopherson11988492019-04-02 08:19:15 -07001386 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001387 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001388
Sean Christopherson11988492019-04-02 08:19:15 -07001389 if (!msr_info->host_initiated) {
1390 if (!__kvm_valid_efer(vcpu, efer))
1391 return 1;
1392
1393 if (is_paging(vcpu) &&
1394 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1395 return 1;
1396 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001397
Carsten Otte15c4a642007-10-30 18:44:17 +01001398 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001399 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001400
Sheng Yanga3d204e2010-05-12 16:40:40 +08001401 kvm_x86_ops->set_efer(vcpu, efer);
1402
Sheng Yangaad82702010-05-12 16:40:42 +08001403 /* Update reserved bits */
1404 if ((efer ^ old_efer) & EFER_NX)
1405 kvm_mmu_reset_context(vcpu);
1406
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001407 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001408}
1409
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001410void kvm_enable_efer_bits(u64 mask)
1411{
1412 efer_reserved_bits &= ~mask;
1413}
1414EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1415
Carsten Otte15c4a642007-10-30 18:44:17 +01001416/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001417 * Write @data into the MSR specified by @index. Select MSR specific fault
1418 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001419 * Returns 0 on success, non-0 otherwise.
1420 * Assumes vcpu_load() was already called.
1421 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001422static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1423 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001424{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001425 struct msr_data msr;
1426
1427 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001428 case MSR_FS_BASE:
1429 case MSR_GS_BASE:
1430 case MSR_KERNEL_GS_BASE:
1431 case MSR_CSTAR:
1432 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001433 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001434 return 1;
1435 break;
1436 case MSR_IA32_SYSENTER_EIP:
1437 case MSR_IA32_SYSENTER_ESP:
1438 /*
1439 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1440 * non-canonical address is written on Intel but not on
1441 * AMD (which ignores the top 32-bits, because it does
1442 * not implement 64-bit SYSENTER).
1443 *
1444 * 64-bit code should hence be able to write a non-canonical
1445 * value on AMD. Making the address canonical ensures that
1446 * vmentry does not fail on Intel after writing a non-canonical
1447 * value, and that something deterministic happens if the guest
1448 * invokes 64-bit SYSENTER.
1449 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001450 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001451 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001452
1453 msr.data = data;
1454 msr.index = index;
1455 msr.host_initiated = host_initiated;
1456
1457 return kvm_x86_ops->set_msr(vcpu, &msr);
1458}
1459
1460/*
1461 * Read the MSR specified by @index into @data. Select MSR specific fault
1462 * checks are bypassed if @host_initiated is %true.
1463 * Returns 0 on success, non-0 otherwise.
1464 * Assumes vcpu_load() was already called.
1465 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001466int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1467 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001468{
1469 struct msr_data msr;
1470 int ret;
1471
1472 msr.index = index;
1473 msr.host_initiated = host_initiated;
1474
1475 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1476 if (!ret)
1477 *data = msr.data;
1478 return ret;
1479}
1480
1481int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1482{
1483 return __kvm_get_msr(vcpu, index, data, false);
1484}
1485EXPORT_SYMBOL_GPL(kvm_get_msr);
1486
1487int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1488{
1489 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001490}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001491EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001492
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001493int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1494{
1495 u32 ecx = kvm_rcx_read(vcpu);
1496 u64 data;
1497
1498 if (kvm_get_msr(vcpu, ecx, &data)) {
1499 trace_kvm_msr_read_ex(ecx);
1500 kvm_inject_gp(vcpu, 0);
1501 return 1;
1502 }
1503
1504 trace_kvm_msr_read(ecx, data);
1505
1506 kvm_rax_write(vcpu, data & -1u);
1507 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1508 return kvm_skip_emulated_instruction(vcpu);
1509}
1510EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1511
1512int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1513{
1514 u32 ecx = kvm_rcx_read(vcpu);
1515 u64 data = kvm_read_edx_eax(vcpu);
1516
1517 if (kvm_set_msr(vcpu, ecx, data)) {
1518 trace_kvm_msr_write_ex(ecx, data);
1519 kvm_inject_gp(vcpu, 0);
1520 return 1;
1521 }
1522
1523 trace_kvm_msr_write(ecx, data);
1524 return kvm_skip_emulated_instruction(vcpu);
1525}
1526EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1527
Carsten Otte313a3dc2007-10-11 19:16:52 +02001528/*
1529 * Adapt set_msr() to msr_io()'s calling convention
1530 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001531static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1532{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001533 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001534}
1535
Carsten Otte313a3dc2007-10-11 19:16:52 +02001536static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1537{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001538 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001539}
1540
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001541#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001542struct pvclock_clock {
1543 int vclock_mode;
1544 u64 cycle_last;
1545 u64 mask;
1546 u32 mult;
1547 u32 shift;
1548};
1549
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001550struct pvclock_gtod_data {
1551 seqcount_t seq;
1552
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001553 struct pvclock_clock clock; /* extract of a clocksource struct */
1554 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001555
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001556 u64 boot_ns_raw;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001557 u64 boot_ns;
1558 u64 nsec_base;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001559 u64 wall_time_sec;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001560 u64 monotonic_raw_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001561};
1562
1563static struct pvclock_gtod_data pvclock_gtod_data;
1564
1565static void update_pvclock_gtod(struct timekeeper *tk)
1566{
1567 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001568 u64 boot_ns, boot_ns_raw;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001569
Peter Zijlstra876e7882015-03-19 10:09:06 +01001570 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001571 boot_ns_raw = ktime_to_ns(ktime_add(tk->tkr_raw.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001572
1573 write_seqcount_begin(&vdata->seq);
1574
1575 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001576 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1577 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1578 vdata->clock.mask = tk->tkr_mono.mask;
1579 vdata->clock.mult = tk->tkr_mono.mult;
1580 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001581
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001582 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1583 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1584 vdata->raw_clock.mask = tk->tkr_raw.mask;
1585 vdata->raw_clock.mult = tk->tkr_raw.mult;
1586 vdata->raw_clock.shift = tk->tkr_raw.shift;
1587
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001588 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001589 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001590
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001591 vdata->wall_time_sec = tk->xtime_sec;
1592
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001593 vdata->boot_ns_raw = boot_ns_raw;
1594 vdata->monotonic_raw_nsec = tk->tkr_raw.xtime_nsec;
1595
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001596 write_seqcount_end(&vdata->seq);
1597}
1598#endif
1599
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001600void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1601{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001602 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001603 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001604}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001605
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001606static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1607{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001608 int version;
1609 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001610 struct pvclock_wall_clock wc;
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001611 struct timespec64 boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001612
1613 if (!wall_clock)
1614 return;
1615
Avi Kivity9ed3c442010-05-04 15:00:37 +03001616 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1617 if (r)
1618 return;
1619
1620 if (version & 1)
1621 ++version; /* first time write, random junk */
1622
1623 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001624
Nicholas Krause1dab1342015-12-30 13:08:46 -05001625 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1626 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001627
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001628 /*
1629 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001630 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001631 * wall clock specified here. guest system time equals host
1632 * system time for us, thus we must fill in host boot time here.
1633 */
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001634 getboottime64(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001635
Bruce Rogers4b648662012-07-20 10:44:24 -06001636 if (kvm->arch.kvmclock_offset) {
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001637 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1638 boot = timespec64_sub(boot, ts);
Bruce Rogers4b648662012-07-20 10:44:24 -06001639 }
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001640 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001641 wc.nsec = boot.tv_nsec;
1642 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001643
1644 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1645
1646 version++;
1647 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001648}
1649
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001650static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1651{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001652 do_shl32_div32(dividend, divisor);
1653 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001654}
1655
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001656static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001657 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001658{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001659 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001660 int32_t shift = 0;
1661 uint64_t tps64;
1662 uint32_t tps32;
1663
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001664 tps64 = base_hz;
1665 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001666 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001667 tps64 >>= 1;
1668 shift--;
1669 }
1670
1671 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001672 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1673 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001674 scaled64 >>= 1;
1675 else
1676 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001677 shift++;
1678 }
1679
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001680 *pshift = shift;
1681 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001682}
1683
Marcelo Tosattid8281992012-11-27 23:29:01 -02001684#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001685static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001686#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001687
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001688static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001689static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001690
Zachary Amsdencc578282012-02-03 15:43:50 -02001691static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001692{
Zachary Amsdencc578282012-02-03 15:43:50 -02001693 u64 v = (u64)khz * (1000000 + ppm);
1694 do_div(v, 1000000);
1695 return v;
1696}
1697
Haozhong Zhang381d5852015-10-20 15:39:04 +08001698static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1699{
1700 u64 ratio;
1701
1702 /* Guest TSC same frequency as host TSC? */
1703 if (!scale) {
1704 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1705 return 0;
1706 }
1707
1708 /* TSC scaling supported? */
1709 if (!kvm_has_tsc_control) {
1710 if (user_tsc_khz > tsc_khz) {
1711 vcpu->arch.tsc_catchup = 1;
1712 vcpu->arch.tsc_always_catchup = 1;
1713 return 0;
1714 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001715 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001716 return -1;
1717 }
1718 }
1719
1720 /* TSC scaling required - calculate ratio */
1721 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1722 user_tsc_khz, tsc_khz);
1723
1724 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001725 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1726 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001727 return -1;
1728 }
1729
1730 vcpu->arch.tsc_scaling_ratio = ratio;
1731 return 0;
1732}
1733
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001734static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001735{
1736 u32 thresh_lo, thresh_hi;
1737 int use_scaling = 0;
1738
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001739 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001740 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001741 /* set tsc_scaling_ratio to a safe value */
1742 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001743 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001744 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001745
Zachary Amsdenc2855452010-09-18 14:38:15 -10001746 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001747 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001748 &vcpu->arch.virtual_tsc_shift,
1749 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001750 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001751
1752 /*
1753 * Compute the variation in TSC rate which is acceptable
1754 * within the range of tolerance and decide if the
1755 * rate being applied is within that bounds of the hardware
1756 * rate. If so, no scaling or compensation need be done.
1757 */
1758 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1759 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001760 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1761 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 -02001762 use_scaling = 1;
1763 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001764 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001765}
1766
1767static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1768{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001769 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001770 vcpu->arch.virtual_tsc_mult,
1771 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001772 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001773 return tsc;
1774}
1775
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001776static inline int gtod_is_based_on_tsc(int mode)
1777{
1778 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1779}
1780
Fengguang Wu69b00492015-01-19 22:33:39 +08001781static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001782{
1783#ifdef CONFIG_X86_64
1784 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001785 struct kvm_arch *ka = &vcpu->kvm->arch;
1786 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1787
1788 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1789 atomic_read(&vcpu->kvm->online_vcpus));
1790
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001791 /*
1792 * Once the masterclock is enabled, always perform request in
1793 * order to update it.
1794 *
1795 * In order to enable masterclock, the host clocksource must be TSC
1796 * and the vcpus need to have matched TSCs. When that happens,
1797 * perform request to enable masterclock.
1798 */
1799 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001800 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001801 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1802
1803 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1804 atomic_read(&vcpu->kvm->online_vcpus),
1805 ka->use_master_clock, gtod->clock.vclock_mode);
1806#endif
1807}
1808
Will Auldba904632012-11-29 12:42:50 -08001809static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1810{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001811 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001812 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1813}
1814
Haozhong Zhang35181e82015-10-20 15:39:03 +08001815/*
1816 * Multiply tsc by a fixed point number represented by ratio.
1817 *
1818 * The most significant 64-N bits (mult) of ratio represent the
1819 * integral part of the fixed point number; the remaining N bits
1820 * (frac) represent the fractional part, ie. ratio represents a fixed
1821 * point number (mult + frac * 2^(-N)).
1822 *
1823 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1824 */
1825static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1826{
1827 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1828}
1829
1830u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1831{
1832 u64 _tsc = tsc;
1833 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1834
1835 if (ratio != kvm_default_tsc_scaling_ratio)
1836 _tsc = __scale_tsc(ratio, tsc);
1837
1838 return _tsc;
1839}
1840EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1841
Haozhong Zhang07c14192015-10-20 15:39:05 +08001842static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1843{
1844 u64 tsc;
1845
1846 tsc = kvm_scale_tsc(vcpu, rdtsc());
1847
1848 return target_tsc - tsc;
1849}
1850
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001851u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1852{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001853 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1854
1855 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001856}
1857EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1858
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001859static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1860{
Leonid Shatz326e7422018-11-06 12:14:25 +02001861 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001862}
1863
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001864static inline bool kvm_check_tsc_unstable(void)
1865{
1866#ifdef CONFIG_X86_64
1867 /*
1868 * TSC is marked unstable when we're running on Hyper-V,
1869 * 'TSC page' clocksource is good.
1870 */
1871 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1872 return false;
1873#endif
1874 return check_tsc_unstable();
1875}
1876
Will Auld8fe8ab42012-11-29 12:42:12 -08001877void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001878{
1879 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001880 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001881 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001882 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001883 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001884 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001885 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001886
Jan Kiszka038f8c12011-02-04 10:49:11 +01001887 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001888 offset = kvm_compute_tsc_offset(vcpu, data);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02001889 ns = ktime_get_boottime_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001890 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001891
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001892 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001893 if (data == 0 && msr->host_initiated) {
1894 /*
1895 * detection of vcpu initialization -- need to sync
1896 * with other vCPUs. This particularly helps to keep
1897 * kvm_clock stable after CPU hotplug
1898 */
1899 synchronizing = true;
1900 } else {
1901 u64 tsc_exp = kvm->arch.last_tsc_write +
1902 nsec_to_cycles(vcpu, elapsed);
1903 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1904 /*
1905 * Special case: TSC write with a small delta (1 second)
1906 * of virtual cycle time against real time is
1907 * interpreted as an attempt to synchronize the CPU.
1908 */
1909 synchronizing = data < tsc_exp + tsc_hz &&
1910 data + tsc_hz > tsc_exp;
1911 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001912 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001913
1914 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001915 * For a reliable TSC, we can match TSC offsets, and for an unstable
1916 * TSC, we add elapsed time in this computation. We could let the
1917 * compensation code attempt to catch up if we fall behind, but
1918 * it's better to try to match offsets from the beginning.
1919 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001920 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001921 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001922 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001923 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001924 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001925 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001926 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001927 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001928 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001929 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001930 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001931 } else {
1932 /*
1933 * We split periods of matched TSC writes into generations.
1934 * For each generation, we track the original measured
1935 * nanosecond time, offset, and write, so if TSCs are in
1936 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001937 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001938 *
1939 * These values are tracked in kvm->arch.cur_xxx variables.
1940 */
1941 kvm->arch.cur_tsc_generation++;
1942 kvm->arch.cur_tsc_nsec = ns;
1943 kvm->arch.cur_tsc_write = data;
1944 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001945 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001946 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001947
1948 /*
1949 * We also track th most recent recorded KHZ, write and time to
1950 * allow the matching interval to be extended at each write.
1951 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001952 kvm->arch.last_tsc_nsec = ns;
1953 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001954 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001955
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001956 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001957
1958 /* Keep track of which generation this VCPU has synchronized to */
1959 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1960 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1961 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1962
Radim Krčmářd6321d42017-08-05 00:12:49 +02001963 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08001964 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001965
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001966 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001967 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001968
1969 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001970 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001971 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001972 } else if (!already_matched) {
1973 kvm->arch.nr_vcpus_matched_tsc++;
1974 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001975
1976 kvm_track_tsc_matching(vcpu);
1977 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001978}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001979
Zachary Amsden99e3e302010-08-19 22:07:17 -10001980EXPORT_SYMBOL_GPL(kvm_write_tsc);
1981
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001982static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1983 s64 adjustment)
1984{
Leonid Shatz326e7422018-11-06 12:14:25 +02001985 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1986 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001987}
1988
1989static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1990{
1991 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1992 WARN_ON(adjustment < 0);
1993 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01001994 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001995}
1996
Marcelo Tosattid8281992012-11-27 23:29:01 -02001997#ifdef CONFIG_X86_64
1998
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001999static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002000{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002001 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002002 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002003
2004 if (likely(ret >= last))
2005 return ret;
2006
2007 /*
2008 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002009 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002010 * very likely) and there's a data dependence, so force GCC
2011 * to generate a branch instead. I don't barrier() because
2012 * we don't actually need a barrier, and if this function
2013 * ever gets inlined it will generate worse code.
2014 */
2015 asm volatile ("");
2016 return last;
2017}
2018
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002019static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2020 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002021{
2022 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002023 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002024
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002025 switch (clock->vclock_mode) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002026 case VCLOCK_HVCLOCK:
2027 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2028 tsc_timestamp);
2029 if (tsc_pg_val != U64_MAX) {
2030 /* TSC page valid */
2031 *mode = VCLOCK_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002032 v = (tsc_pg_val - clock->cycle_last) &
2033 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002034 } else {
2035 /* TSC page invalid */
2036 *mode = VCLOCK_NONE;
2037 }
2038 break;
2039 case VCLOCK_TSC:
2040 *mode = VCLOCK_TSC;
2041 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002042 v = (*tsc_timestamp - clock->cycle_last) &
2043 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002044 break;
2045 default:
2046 *mode = VCLOCK_NONE;
2047 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002048
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002049 if (*mode == VCLOCK_NONE)
2050 *tsc_timestamp = v = 0;
2051
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002052 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002053}
2054
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002055static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002056{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002057 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002058 unsigned long seq;
2059 int mode;
2060 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002061
Marcelo Tosattid8281992012-11-27 23:29:01 -02002062 do {
2063 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002064 ns = gtod->monotonic_raw_nsec;
2065 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002066 ns >>= gtod->clock.shift;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002067 ns += gtod->boot_ns_raw;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002068 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002069 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002070
2071 return mode;
2072}
2073
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002074static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002075{
2076 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2077 unsigned long seq;
2078 int mode;
2079 u64 ns;
2080
2081 do {
2082 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002083 ts->tv_sec = gtod->wall_time_sec;
2084 ns = gtod->nsec_base;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002085 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002086 ns >>= gtod->clock.shift;
2087 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2088
2089 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2090 ts->tv_nsec = ns;
2091
2092 return mode;
2093}
2094
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002095/* returns true if host is using TSC based clocksource */
2096static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002097{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002098 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002099 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002100 return false;
2101
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002102 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002103 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002104}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002105
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002106/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002107static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002108 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002109{
2110 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002111 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002112 return false;
2113
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002114 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002115}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002116#endif
2117
2118/*
2119 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002120 * Assuming a stable TSC across physical CPUS, and a stable TSC
2121 * across virtual CPUs, the following condition is possible.
2122 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002123 * CPUs at the next numbered event.
2124 *
2125 * "timespecX" represents host monotonic time. "tscX" represents
2126 * RDTSC value.
2127 *
2128 * VCPU0 on CPU0 | VCPU1 on CPU1
2129 *
2130 * 1. read timespec0,tsc0
2131 * 2. | timespec1 = timespec0 + N
2132 * | tsc1 = tsc0 + M
2133 * 3. transition to guest | transition to guest
2134 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2135 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2136 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2137 *
2138 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2139 *
2140 * - ret0 < ret1
2141 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2142 * ...
2143 * - 0 < N - M => M < N
2144 *
2145 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2146 * always the case (the difference between two distinct xtime instances
2147 * might be smaller then the difference between corresponding TSC reads,
2148 * when updating guest vcpus pvclock areas).
2149 *
2150 * To avoid that problem, do not allow visibility of distinct
2151 * system_timestamp/tsc_timestamp values simultaneously: use a master
2152 * copy of host monotonic time values. Update that master copy
2153 * in lockstep.
2154 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002155 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002156 *
2157 */
2158
2159static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2160{
2161#ifdef CONFIG_X86_64
2162 struct kvm_arch *ka = &kvm->arch;
2163 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002164 bool host_tsc_clocksource, vcpus_matched;
2165
2166 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2167 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002168
2169 /*
2170 * If the host uses TSC clock, then passthrough TSC as stable
2171 * to the guest.
2172 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002173 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002174 &ka->master_kernel_ns,
2175 &ka->master_cycle_now);
2176
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002177 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002178 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002179 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002180
Marcelo Tosattid8281992012-11-27 23:29:01 -02002181 if (ka->use_master_clock)
2182 atomic_set(&kvm_guest_has_master_clock, 1);
2183
2184 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002185 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2186 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002187#endif
2188}
2189
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002190void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2191{
2192 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2193}
2194
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002195static void kvm_gen_update_masterclock(struct kvm *kvm)
2196{
2197#ifdef CONFIG_X86_64
2198 int i;
2199 struct kvm_vcpu *vcpu;
2200 struct kvm_arch *ka = &kvm->arch;
2201
2202 spin_lock(&ka->pvclock_gtod_sync_lock);
2203 kvm_make_mclock_inprogress_request(kvm);
2204 /* no guest entries from this point */
2205 pvclock_update_vm_gtod_copy(kvm);
2206
2207 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002208 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002209
2210 /* guest entries allowed */
2211 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002212 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002213
2214 spin_unlock(&ka->pvclock_gtod_sync_lock);
2215#endif
2216}
2217
Marcelo Tosattie891a322017-04-17 12:51:37 -03002218u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002219{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002220 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002221 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002222 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002223
Paolo Bonzini8b953442016-11-16 18:31:30 +01002224 spin_lock(&ka->pvclock_gtod_sync_lock);
2225 if (!ka->use_master_clock) {
2226 spin_unlock(&ka->pvclock_gtod_sync_lock);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002227 return ktime_get_boottime_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002228 }
2229
Paolo Bonzini8b953442016-11-16 18:31:30 +01002230 hv_clock.tsc_timestamp = ka->master_cycle_now;
2231 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2232 spin_unlock(&ka->pvclock_gtod_sync_lock);
2233
Wanpeng Lie2c22062017-05-11 18:12:05 -07002234 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2235 get_cpu();
2236
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002237 if (__this_cpu_read(cpu_tsc_khz)) {
2238 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2239 &hv_clock.tsc_shift,
2240 &hv_clock.tsc_to_system_mul);
2241 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2242 } else
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002243 ret = ktime_get_boottime_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002244
2245 put_cpu();
2246
2247 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002248}
2249
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002250static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2251{
2252 struct kvm_vcpu_arch *vcpu = &v->arch;
2253 struct pvclock_vcpu_time_info guest_hv_clock;
2254
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002255 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002256 &guest_hv_clock, sizeof(guest_hv_clock))))
2257 return;
2258
2259 /* This VCPU is paused, but it's legal for a guest to read another
2260 * VCPU's kvmclock, so we really have to follow the specification where
2261 * it says that version is odd if data is being modified, and even after
2262 * it is consistent.
2263 *
2264 * Version field updates must be kept separate. This is because
2265 * kvm_write_guest_cached might use a "rep movs" instruction, and
2266 * writes within a string instruction are weakly ordered. So there
2267 * are three writes overall.
2268 *
2269 * As a small optimization, only write the version field in the first
2270 * and third write. The vcpu->pv_time cache is still valid, because the
2271 * version field is the first in the struct.
2272 */
2273 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2274
Liran Alon51c4b8b2017-11-05 16:11:30 +02002275 if (guest_hv_clock.version & 1)
2276 ++guest_hv_clock.version; /* first time write, random junk */
2277
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002278 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002279 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2280 &vcpu->hv_clock,
2281 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002282
2283 smp_wmb();
2284
2285 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2286 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2287
2288 if (vcpu->pvclock_set_guest_stopped_request) {
2289 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2290 vcpu->pvclock_set_guest_stopped_request = false;
2291 }
2292
2293 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2294
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002295 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2296 &vcpu->hv_clock,
2297 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002298
2299 smp_wmb();
2300
2301 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002302 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2303 &vcpu->hv_clock,
2304 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002305}
2306
Zachary Amsden34c238a2010-09-18 14:38:14 -10002307static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002308{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002309 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002310 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002311 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002312 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002313 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002314 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002315 bool use_master_clock;
2316
2317 kernel_ns = 0;
2318 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002319
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002320 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002321 * If the host uses TSC clock, then passthrough TSC as stable
2322 * to the guest.
2323 */
2324 spin_lock(&ka->pvclock_gtod_sync_lock);
2325 use_master_clock = ka->use_master_clock;
2326 if (use_master_clock) {
2327 host_tsc = ka->master_cycle_now;
2328 kernel_ns = ka->master_kernel_ns;
2329 }
2330 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002331
2332 /* Keep irq disabled to prevent changes to the clock */
2333 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002334 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2335 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002336 local_irq_restore(flags);
2337 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2338 return 1;
2339 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002340 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002341 host_tsc = rdtsc();
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002342 kernel_ns = ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002343 }
2344
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002345 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002346
2347 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002348 * We may have to catch up the TSC to match elapsed wall clock
2349 * time for two reasons, even if kvmclock is used.
2350 * 1) CPU could have been running below the maximum TSC rate
2351 * 2) Broken TSC compensation resets the base at each VCPU
2352 * entry to avoid unknown leaps of TSC even when running
2353 * again on the same CPU. This may cause apparent elapsed
2354 * time to disappear, and the guest to stand still or run
2355 * very slowly.
2356 */
2357 if (vcpu->tsc_catchup) {
2358 u64 tsc = compute_guest_tsc(v, kernel_ns);
2359 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002360 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002361 tsc_timestamp = tsc;
2362 }
2363 }
2364
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002365 local_irq_restore(flags);
2366
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002367 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002368
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002369 if (kvm_has_tsc_control)
2370 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2371
2372 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002373 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002374 &vcpu->hv_clock.tsc_shift,
2375 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002376 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002377 }
2378
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002379 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002380 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002381 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002382
Marcelo Tosattid8281992012-11-27 23:29:01 -02002383 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002384 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002385 if (use_master_clock)
2386 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2387
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002388 vcpu->hv_clock.flags = pvclock_flags;
2389
Paolo Bonzini095cf552016-02-08 12:54:12 +01002390 if (vcpu->pv_time_enabled)
2391 kvm_setup_pvclock_page(v);
2392 if (v == kvm_get_vcpu(v->kvm, 0))
2393 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002394 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002395}
2396
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002397/*
2398 * kvmclock updates which are isolated to a given vcpu, such as
2399 * vcpu->cpu migration, should not allow system_timestamp from
2400 * the rest of the vcpus to remain static. Otherwise ntp frequency
2401 * correction applies to one vcpu's system_timestamp but not
2402 * the others.
2403 *
2404 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002405 * We need to rate-limit these requests though, as they can
2406 * considerably slow guests that have a large number of vcpus.
2407 * The time for a remote vcpu to update its kvmclock is bound
2408 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002409 */
2410
Andrew Jones7e44e442014-02-28 12:52:54 +01002411#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2412
2413static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002414{
2415 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002416 struct delayed_work *dwork = to_delayed_work(work);
2417 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2418 kvmclock_update_work);
2419 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002420 struct kvm_vcpu *vcpu;
2421
2422 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002423 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002424 kvm_vcpu_kick(vcpu);
2425 }
2426}
2427
Andrew Jones7e44e442014-02-28 12:52:54 +01002428static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2429{
2430 struct kvm *kvm = v->kvm;
2431
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002432 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002433 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2434 KVMCLOCK_UPDATE_DELAY);
2435}
2436
Andrew Jones332967a2014-02-28 12:52:55 +01002437#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2438
2439static void kvmclock_sync_fn(struct work_struct *work)
2440{
2441 struct delayed_work *dwork = to_delayed_work(work);
2442 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2443 kvmclock_sync_work);
2444 struct kvm *kvm = container_of(ka, struct kvm, arch);
2445
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002446 if (!kvmclock_periodic_sync)
2447 return;
2448
Andrew Jones332967a2014-02-28 12:52:55 +01002449 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2450 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2451 KVMCLOCK_SYNC_PERIOD);
2452}
2453
Borislav Petkov191c8132019-04-18 18:32:50 +02002454/*
2455 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2456 */
2457static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2458{
2459 /* McStatusWrEn enabled? */
2460 if (guest_cpuid_is_amd(vcpu))
2461 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2462
2463 return false;
2464}
2465
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002466static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002467{
2468 u64 mcg_cap = vcpu->arch.mcg_cap;
2469 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002470 u32 msr = msr_info->index;
2471 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002472
2473 switch (msr) {
2474 case MSR_IA32_MCG_STATUS:
2475 vcpu->arch.mcg_status = data;
2476 break;
2477 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002478 if (!(mcg_cap & MCG_CTL_P) &&
2479 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002480 return 1;
2481 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002482 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002483 vcpu->arch.mcg_ctl = data;
2484 break;
2485 default:
2486 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002487 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002488 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01002489 /* only 0 or all 1s can be written to IA32_MCi_CTL
2490 * some Linux kernels though clear bit 10 in bank 4 to
2491 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2492 * this to avoid an uncatched #GP in the guest
2493 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002494 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002495 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002496 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002497
2498 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002499 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002500 (offset & 0x3) == 1 && data != 0) {
2501 if (!can_set_mci_status(vcpu))
2502 return -1;
2503 }
2504
Huang Ying890ca9a2009-05-11 16:48:15 +08002505 vcpu->arch.mce_banks[offset] = data;
2506 break;
2507 }
2508 return 1;
2509 }
2510 return 0;
2511}
2512
Ed Swierkffde22a2009-10-15 15:21:43 -07002513static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2514{
2515 struct kvm *kvm = vcpu->kvm;
2516 int lm = is_long_mode(vcpu);
2517 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2518 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2519 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2520 : kvm->arch.xen_hvm_config.blob_size_32;
2521 u32 page_num = data & ~PAGE_MASK;
2522 u64 page_addr = data & PAGE_MASK;
2523 u8 *page;
2524 int r;
2525
2526 r = -E2BIG;
2527 if (page_num >= blob_size)
2528 goto out;
2529 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002530 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2531 if (IS_ERR(page)) {
2532 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002533 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002534 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002535 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002536 goto out_free;
2537 r = 0;
2538out_free:
2539 kfree(page);
2540out:
2541 return r;
2542}
2543
Gleb Natapov344d9582010-10-14 11:22:50 +02002544static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2545{
2546 gpa_t gpa = data & ~0x3f;
2547
Wanpeng Li52a5c152017-07-13 18:30:42 -07002548 /* Bits 3:5 are reserved, Should be zero */
2549 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002550 return 1;
2551
2552 vcpu->arch.apf.msr_val = data;
2553
2554 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2555 kvm_clear_async_pf_completion_queue(vcpu);
2556 kvm_async_pf_hash_reset(vcpu);
2557 return 0;
2558 }
2559
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002560 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002561 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002562 return 1;
2563
Gleb Natapov6adba522010-10-14 11:22:55 +02002564 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002565 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002566 kvm_async_pf_wakeup_all(vcpu);
2567 return 0;
2568}
2569
Glauber Costa12f9a482011-02-01 14:16:40 -05002570static void kvmclock_reset(struct kvm_vcpu *vcpu)
2571{
Andy Honig0b794592013-02-20 14:48:10 -08002572 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002573 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002574}
2575
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002576static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2577{
2578 ++vcpu->stat.tlb_flush;
2579 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2580}
2581
Glauber Costac9aaa892011-07-11 15:28:14 -04002582static void record_steal_time(struct kvm_vcpu *vcpu)
2583{
2584 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2585 return;
2586
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002587 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002588 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2589 return;
2590
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002591 /*
2592 * Doing a TLB flush here, on the guest's behalf, can avoid
2593 * expensive IPIs.
2594 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002595 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
2596 vcpu->arch.st.steal.preempted & KVM_VCPU_FLUSH_TLB);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002597 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
2598 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002599
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002600 if (vcpu->arch.st.steal.version & 1)
2601 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2602
2603 vcpu->arch.st.steal.version += 1;
2604
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002605 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002606 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2607
2608 smp_wmb();
2609
Liang Chenc54cdf12016-03-16 19:33:16 +08002610 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2611 vcpu->arch.st.last_steal;
2612 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002613
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002614 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002615 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2616
2617 smp_wmb();
2618
2619 vcpu->arch.st.steal.version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002620
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002621 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002622 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2623}
2624
Will Auld8fe8ab42012-11-29 12:42:12 -08002625int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002626{
Gleb Natapov57537852012-01-15 14:17:22 +02002627 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002628 u32 msr = msr_info->index;
2629 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002630
Carsten Otte15c4a642007-10-30 18:44:17 +01002631 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002632 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002633 case MSR_IA32_UCODE_WRITE:
2634 case MSR_VM_HSAVE_PA:
2635 case MSR_AMD64_PATCH_LOADER:
2636 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002637 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002638 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002639 break;
2640
Wanpeng Li518e7b92018-02-28 14:03:31 +08002641 case MSR_IA32_UCODE_REV:
2642 if (msr_info->host_initiated)
2643 vcpu->arch.microcode_version = data;
2644 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002645 case MSR_IA32_ARCH_CAPABILITIES:
2646 if (!msr_info->host_initiated)
2647 return 1;
2648 vcpu->arch.arch_capabilities = data;
2649 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002650 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002651 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002652 case MSR_K7_HWCR:
2653 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002654 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002655 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002656
2657 /* Handle McStatusWrEn */
2658 if (data == BIT_ULL(18)) {
2659 vcpu->arch.msr_hwcr = data;
2660 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002661 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2662 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002663 return 1;
2664 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002665 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002666 case MSR_FAM10H_MMIO_CONF_BASE:
2667 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002668 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2669 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002670 return 1;
2671 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002672 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002673 case MSR_IA32_DEBUGCTLMSR:
2674 if (!data) {
2675 /* We support the non-activated case already */
2676 break;
2677 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2678 /* Values other than LBR and BTF are vendor-specific,
2679 thus reserved and should throw a #GP */
2680 return 1;
2681 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002682 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2683 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002684 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002685 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002686 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002687 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002688 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002689 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2690 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002691 case MSR_IA32_TSCDEADLINE:
2692 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2693 break;
Will Auldba904632012-11-29 12:42:50 -08002694 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002695 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002696 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002697 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002698 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002699 }
2700 vcpu->arch.ia32_tsc_adjust_msr = data;
2701 }
2702 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002703 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002704 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2705 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2706 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2707 return 1;
2708 vcpu->arch.ia32_misc_enable_msr = data;
2709 kvm_update_cpuid(vcpu);
2710 } else {
2711 vcpu->arch.ia32_misc_enable_msr = data;
2712 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002713 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002714 case MSR_IA32_SMBASE:
2715 if (!msr_info->host_initiated)
2716 return 1;
2717 vcpu->arch.smbase = data;
2718 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002719 case MSR_IA32_POWER_CTL:
2720 vcpu->arch.msr_ia32_power_ctl = data;
2721 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002722 case MSR_IA32_TSC:
2723 kvm_write_tsc(vcpu, msr_info);
2724 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002725 case MSR_IA32_XSS:
2726 if (!msr_info->host_initiated &&
2727 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2728 return 1;
2729 /*
2730 * We do support PT if kvm_x86_ops->pt_supported(), but we do
2731 * not support IA32_XSS[bit 8]. Guests will have to use
2732 * RDMSR/WRMSR rather than XSAVES/XRSTORS to save/restore PT
2733 * MSRs.
2734 */
2735 if (data != 0)
2736 return 1;
2737 vcpu->arch.ia32_xss = data;
2738 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002739 case MSR_SMI_COUNT:
2740 if (!msr_info->host_initiated)
2741 return 1;
2742 vcpu->arch.smi_count = data;
2743 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002744 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002745 case MSR_KVM_WALL_CLOCK:
2746 vcpu->kvm->arch.wall_clock = data;
2747 kvm_write_wall_clock(vcpu->kvm, data);
2748 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002749 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002750 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002751 struct kvm_arch *ka = &vcpu->kvm->arch;
2752
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002753 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2754 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2755
2756 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002757 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002758
2759 ka->boot_vcpu_runs_old_kvmclock = tmp;
2760 }
2761
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002762 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002763 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002764
2765 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002766 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002767 if (!(data & 1))
2768 break;
2769
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002770 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002771 &vcpu->arch.pv_time, data & ~1ULL,
2772 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002773 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002774
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002775 break;
2776 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002777 case MSR_KVM_ASYNC_PF_EN:
2778 if (kvm_pv_enable_async_pf(vcpu, data))
2779 return 1;
2780 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002781 case MSR_KVM_STEAL_TIME:
2782
2783 if (unlikely(!sched_info_on()))
2784 return 1;
2785
2786 if (data & KVM_STEAL_RESERVED_MASK)
2787 return 1;
2788
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002789 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002790 data & KVM_STEAL_VALID_BITS,
2791 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002792 return 1;
2793
2794 vcpu->arch.st.msr_val = data;
2795
2796 if (!(data & KVM_MSR_ENABLED))
2797 break;
2798
Glauber Costac9aaa892011-07-11 15:28:14 -04002799 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2800
2801 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002802 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002803 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002804 return 1;
2805 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002806
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002807 case MSR_KVM_POLL_CONTROL:
2808 /* only enable bit supported */
2809 if (data & (-1ULL << 1))
2810 return 1;
2811
2812 vcpu->arch.msr_kvm_poll_control = data;
2813 break;
2814
Huang Ying890ca9a2009-05-11 16:48:15 +08002815 case MSR_IA32_MCG_CTL:
2816 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002817 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002818 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002819
Wei Huang6912ac32015-06-12 01:34:56 -04002820 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2821 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2822 pr = true; /* fall through */
2823 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2824 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002825 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002826 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002827
2828 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002829 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2830 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002831 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002832 case MSR_K7_CLK_CTL:
2833 /*
2834 * Ignore all writes to this no longer documented MSR.
2835 * Writes are only relevant for old K7 processors,
2836 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002837 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002838 * affected processor models on the command line, hence
2839 * the need to ignore the workaround.
2840 */
2841 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002842 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002843 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2844 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002845 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002846 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2847 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2848 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002849 return kvm_hv_set_msr_common(vcpu, msr, data,
2850 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002851 case MSR_IA32_BBL_CR_CTL3:
2852 /* Drop writes to this legacy MSR -- see rdmsr
2853 * counterpart for further detail.
2854 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002855 if (report_ignored_msrs)
2856 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2857 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002858 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002859 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002860 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002861 return 1;
2862 vcpu->arch.osvw.length = data;
2863 break;
2864 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002865 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002866 return 1;
2867 vcpu->arch.osvw.status = data;
2868 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002869 case MSR_PLATFORM_INFO:
2870 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002871 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2872 cpuid_fault_enabled(vcpu)))
2873 return 1;
2874 vcpu->arch.msr_platform_info = data;
2875 break;
2876 case MSR_MISC_FEATURES_ENABLES:
2877 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2878 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2879 !supports_cpuid_fault(vcpu)))
2880 return 1;
2881 vcpu->arch.msr_misc_features_enables = data;
2882 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002883 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002884 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2885 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002886 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002887 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002888 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002889 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002890 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002891 return 1;
2892 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002893 if (report_ignored_msrs)
2894 vcpu_unimpl(vcpu,
2895 "ignored wrmsr: 0x%x data 0x%llx\n",
2896 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002897 break;
2898 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002899 }
2900 return 0;
2901}
2902EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2903
Paolo Bonzini44883f02018-07-26 13:01:52 +02002904static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002905{
2906 u64 data;
2907 u64 mcg_cap = vcpu->arch.mcg_cap;
2908 unsigned bank_num = mcg_cap & 0xff;
2909
2910 switch (msr) {
2911 case MSR_IA32_P5_MC_ADDR:
2912 case MSR_IA32_P5_MC_TYPE:
2913 data = 0;
2914 break;
2915 case MSR_IA32_MCG_CAP:
2916 data = vcpu->arch.mcg_cap;
2917 break;
2918 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002919 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002920 return 1;
2921 data = vcpu->arch.mcg_ctl;
2922 break;
2923 case MSR_IA32_MCG_STATUS:
2924 data = vcpu->arch.mcg_status;
2925 break;
2926 default:
2927 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002928 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002929 u32 offset = msr - MSR_IA32_MC0_CTL;
2930 data = vcpu->arch.mce_banks[offset];
2931 break;
2932 }
2933 return 1;
2934 }
2935 *pdata = data;
2936 return 0;
2937}
2938
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002939int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002940{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002941 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002942 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002943 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002944 case MSR_IA32_DEBUGCTLMSR:
2945 case MSR_IA32_LASTBRANCHFROMIP:
2946 case MSR_IA32_LASTBRANCHTOIP:
2947 case MSR_IA32_LASTINTFROMIP:
2948 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302949 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002950 case MSR_K8_TSEG_ADDR:
2951 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002952 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002953 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002954 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002955 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002956 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03002957 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02002958 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002959 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002960 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002961 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06002962 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04002963 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2964 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2965 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2966 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002967 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002968 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2969 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002970 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002971 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08002972 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002973 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002974 case MSR_IA32_ARCH_CAPABILITIES:
2975 if (!msr_info->host_initiated &&
2976 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2977 return 1;
2978 msr_info->data = vcpu->arch.arch_capabilities;
2979 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002980 case MSR_IA32_POWER_CTL:
2981 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
2982 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002983 case MSR_IA32_TSC:
2984 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2985 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002986 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002987 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002988 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002989 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002990 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002991 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002992 /*
2993 * MSR_EBC_FREQUENCY_ID
2994 * Conservative value valid for even the basic CPU models.
2995 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2996 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2997 * and 266MHz for model 3, or 4. Set Core Clock
2998 * Frequency to System Bus Frequency Ratio to 1 (bits
2999 * 31:24) even though these are only valid for CPU
3000 * models > 2, however guests may end up dividing or
3001 * multiplying by zero otherwise.
3002 */
3003 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003004 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003005 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003006 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003007 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003008 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003009 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003010 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003011 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003012 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003013 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003014 break;
Will Auldba904632012-11-29 12:42:50 -08003015 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003016 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003017 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003018 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003019 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003020 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003021 case MSR_IA32_SMBASE:
3022 if (!msr_info->host_initiated)
3023 return 1;
3024 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003025 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003026 case MSR_SMI_COUNT:
3027 msr_info->data = vcpu->arch.smi_count;
3028 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003029 case MSR_IA32_PERF_STATUS:
3030 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003031 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003032 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003033 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003034 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003035 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003036 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003037 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003038 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003039 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003040 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003041 break;
3042 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003043 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003044 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003045 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003046 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003047 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003048 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003049 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003050 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003051 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003052 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003053 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003054 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003055 case MSR_KVM_POLL_CONTROL:
3056 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3057 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003058 case MSR_IA32_P5_MC_ADDR:
3059 case MSR_IA32_P5_MC_TYPE:
3060 case MSR_IA32_MCG_CAP:
3061 case MSR_IA32_MCG_CTL:
3062 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003063 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003064 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3065 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003066 case MSR_IA32_XSS:
3067 if (!msr_info->host_initiated &&
3068 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3069 return 1;
3070 msr_info->data = vcpu->arch.ia32_xss;
3071 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003072 case MSR_K7_CLK_CTL:
3073 /*
3074 * Provide expected ramp-up count for K7. All other
3075 * are set to zero, indicating minimum divisors for
3076 * every field.
3077 *
3078 * This prevents guest kernels on AMD host with CPU
3079 * type 6, model 8 and higher from exploding due to
3080 * the rdmsr failing.
3081 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003082 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003083 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003084 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003085 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3086 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003087 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003088 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3089 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3090 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003091 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003092 msr_info->index, &msr_info->data,
3093 msr_info->host_initiated);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003094 break;
john cooper91c9c3e2011-01-21 00:21:00 -05003095 case MSR_IA32_BBL_CR_CTL3:
3096 /* This legacy MSR exists but isn't fully documented in current
3097 * silicon. It is however accessed by winxp in very narrow
3098 * scenarios where it sets bit #19, itself documented as
3099 * a "reserved" bit. Best effort attempt to source coherent
3100 * read data here should the balance of the register be
3101 * interpreted by the guest:
3102 *
3103 * L2 cache control register 3: 64GB range, 256KB size,
3104 * enabled, latency 0x1, configured
3105 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003106 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003107 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003108 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003109 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003110 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003111 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003112 break;
3113 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003114 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003115 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003116 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003117 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003118 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003119 if (!msr_info->host_initiated &&
3120 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3121 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003122 msr_info->data = vcpu->arch.msr_platform_info;
3123 break;
3124 case MSR_MISC_FEATURES_ENABLES:
3125 msr_info->data = vcpu->arch.msr_misc_features_enables;
3126 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003127 case MSR_K7_HWCR:
3128 msr_info->data = vcpu->arch.msr_hwcr;
3129 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003130 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003131 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003132 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003133 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003134 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3135 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003136 return 1;
3137 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003138 if (report_ignored_msrs)
3139 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3140 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003141 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003142 }
3143 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003144 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003145 return 0;
3146}
3147EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3148
Carsten Otte313a3dc2007-10-11 19:16:52 +02003149/*
3150 * Read or write a bunch of msrs. All parameters are kernel addresses.
3151 *
3152 * @return number of msrs set successfully.
3153 */
3154static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3155 struct kvm_msr_entry *entries,
3156 int (*do_msr)(struct kvm_vcpu *vcpu,
3157 unsigned index, u64 *data))
3158{
Tom Lendacky801e4592018-02-21 13:39:51 -06003159 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003160
Carsten Otte313a3dc2007-10-11 19:16:52 +02003161 for (i = 0; i < msrs->nmsrs; ++i)
3162 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3163 break;
3164
Carsten Otte313a3dc2007-10-11 19:16:52 +02003165 return i;
3166}
3167
3168/*
3169 * Read or write a bunch of msrs. Parameters are user addresses.
3170 *
3171 * @return number of msrs set successfully.
3172 */
3173static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3174 int (*do_msr)(struct kvm_vcpu *vcpu,
3175 unsigned index, u64 *data),
3176 int writeback)
3177{
3178 struct kvm_msrs msrs;
3179 struct kvm_msr_entry *entries;
3180 int r, n;
3181 unsigned size;
3182
3183 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003184 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003185 goto out;
3186
3187 r = -E2BIG;
3188 if (msrs.nmsrs >= MAX_IO_MSRS)
3189 goto out;
3190
Carsten Otte313a3dc2007-10-11 19:16:52 +02003191 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003192 entries = memdup_user(user_msrs->entries, size);
3193 if (IS_ERR(entries)) {
3194 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003195 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003196 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003197
3198 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3199 if (r < 0)
3200 goto out_free;
3201
3202 r = -EFAULT;
3203 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3204 goto out_free;
3205
3206 r = n;
3207
3208out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003209 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003210out:
3211 return r;
3212}
3213
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003214static inline bool kvm_can_mwait_in_guest(void)
3215{
3216 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003217 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3218 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003219}
3220
Alexander Graf784aa3d2014-07-14 18:27:35 +02003221int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003222{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003223 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003224
3225 switch (ext) {
3226 case KVM_CAP_IRQCHIP:
3227 case KVM_CAP_HLT:
3228 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003229 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003230 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003231 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003232 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003233 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003234 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003235 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003236 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003237 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003238 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003239 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003240 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003241 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003242 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003243 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003244 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003245 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003246 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003247 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003248 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003249 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003250 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003251 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003252 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003253 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003254 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003255 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003256 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003257 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003258 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003259 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003260 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003261 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003262 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003263 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003264 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003265 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003266 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003267 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003268 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003269 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003270 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003271 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003272 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003273 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003274 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003275 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003276 r = 1;
3277 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003278 case KVM_CAP_SYNC_REGS:
3279 r = KVM_SYNC_X86_VALID_FIELDS;
3280 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003281 case KVM_CAP_ADJUST_CLOCK:
3282 r = KVM_CLOCK_TSC_STABLE;
3283 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003284 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003285 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3286 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003287 if(kvm_can_mwait_in_guest())
3288 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003289 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003290 case KVM_CAP_X86_SMM:
3291 /* SMBASE is usually relocated above 1M on modern chipsets,
3292 * and SMM handlers might indeed rely on 4G segment limits,
3293 * so do not report SMM to be available if real mode is
3294 * emulated via vm86 mode. Still, do not go to great lengths
3295 * to avoid userspace's usage of the feature, because it is a
3296 * fringe case that is not enabled except via specific settings
3297 * of the module parameters.
3298 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003299 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003300 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003301 case KVM_CAP_VAPIC:
3302 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3303 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003304 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003305 r = KVM_SOFT_MAX_VCPUS;
3306 break;
3307 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003308 r = KVM_MAX_VCPUS;
3309 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003310 case KVM_CAP_MAX_VCPU_ID:
3311 r = KVM_MAX_VCPU_ID;
3312 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003313 case KVM_CAP_PV_MMU: /* obsolete */
3314 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003315 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003316 case KVM_CAP_MCE:
3317 r = KVM_MAX_MCE_BANKS;
3318 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003319 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003320 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003321 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003322 case KVM_CAP_TSC_CONTROL:
3323 r = kvm_has_tsc_control;
3324 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003325 case KVM_CAP_X2APIC_API:
3326 r = KVM_X2APIC_API_VALID_FLAGS;
3327 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003328 case KVM_CAP_NESTED_STATE:
3329 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303330 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003331 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003332 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003333 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3334 break;
3335 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3336 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003337 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003338 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003339 break;
3340 }
3341 return r;
3342
3343}
3344
Carsten Otte043405e2007-10-10 17:16:19 +02003345long kvm_arch_dev_ioctl(struct file *filp,
3346 unsigned int ioctl, unsigned long arg)
3347{
3348 void __user *argp = (void __user *)arg;
3349 long r;
3350
3351 switch (ioctl) {
3352 case KVM_GET_MSR_INDEX_LIST: {
3353 struct kvm_msr_list __user *user_msr_list = argp;
3354 struct kvm_msr_list msr_list;
3355 unsigned n;
3356
3357 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003358 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003359 goto out;
3360 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003361 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003362 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003363 goto out;
3364 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003365 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003366 goto out;
3367 r = -EFAULT;
3368 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3369 num_msrs_to_save * sizeof(u32)))
3370 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003371 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003372 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003373 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003374 goto out;
3375 r = 0;
3376 break;
3377 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003378 case KVM_GET_SUPPORTED_CPUID:
3379 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003380 struct kvm_cpuid2 __user *cpuid_arg = argp;
3381 struct kvm_cpuid2 cpuid;
3382
3383 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003384 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003385 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003386
3387 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3388 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003389 if (r)
3390 goto out;
3391
3392 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003393 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003394 goto out;
3395 r = 0;
3396 break;
3397 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003398 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003399 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003400 if (copy_to_user(argp, &kvm_mce_cap_supported,
3401 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003402 goto out;
3403 r = 0;
3404 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003405 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3406 struct kvm_msr_list __user *user_msr_list = argp;
3407 struct kvm_msr_list msr_list;
3408 unsigned int n;
3409
3410 r = -EFAULT;
3411 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3412 goto out;
3413 n = msr_list.nmsrs;
3414 msr_list.nmsrs = num_msr_based_features;
3415 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3416 goto out;
3417 r = -E2BIG;
3418 if (n < msr_list.nmsrs)
3419 goto out;
3420 r = -EFAULT;
3421 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3422 num_msr_based_features * sizeof(u32)))
3423 goto out;
3424 r = 0;
3425 break;
3426 }
3427 case KVM_GET_MSRS:
3428 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3429 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003430 }
Carsten Otte043405e2007-10-10 17:16:19 +02003431 default:
3432 r = -EINVAL;
3433 }
3434out:
3435 return r;
3436}
3437
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003438static void wbinvd_ipi(void *garbage)
3439{
3440 wbinvd();
3441}
3442
3443static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3444{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003445 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003446}
3447
Carsten Otte313a3dc2007-10-11 19:16:52 +02003448void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3449{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003450 /* Address WBINVD may be executed by guest */
3451 if (need_emulate_wbinvd(vcpu)) {
3452 if (kvm_x86_ops->has_wbinvd_exit())
3453 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3454 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3455 smp_call_function_single(vcpu->cpu,
3456 wbinvd_ipi, NULL, 1);
3457 }
3458
Carsten Otte313a3dc2007-10-11 19:16:52 +02003459 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003460
Wanpeng Lie7517322019-07-22 12:26:20 +08003461 fpregs_assert_state_consistent();
3462 if (test_thread_flag(TIF_NEED_FPU_LOAD))
3463 switch_fpu_return();
3464
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003465 /* Apply any externally detected TSC adjustments (due to suspend) */
3466 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3467 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3468 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003469 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003470 }
3471
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003472 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003473 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003474 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003475 if (tsc_delta < 0)
3476 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003477
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003478 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003479 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003480 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003481 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003482 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003483 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003484
3485 if (kvm_lapic_hv_timer_in_use(vcpu))
3486 kvm_lapic_restart_hv_timer(vcpu);
3487
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003488 /*
3489 * On a host with synchronized TSC, there is no need to update
3490 * kvmclock on vcpu->cpu migration
3491 */
3492 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003493 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003494 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003495 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003496 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003497 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003498
Glauber Costac9aaa892011-07-11 15:28:14 -04003499 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003500}
3501
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003502static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3503{
3504 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3505 return;
3506
Wanpeng Lifa55eed2017-12-12 17:33:01 -08003507 vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003508
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003509 kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003510 &vcpu->arch.st.steal.preempted,
3511 offsetof(struct kvm_steal_time, preempted),
3512 sizeof(vcpu->arch.st.steal.preempted));
3513}
3514
Carsten Otte313a3dc2007-10-11 19:16:52 +02003515void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3516{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003517 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003518
3519 if (vcpu->preempted)
3520 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3521
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003522 /*
3523 * Disable page faults because we're in atomic context here.
3524 * kvm_write_guest_offset_cached() would call might_fault()
3525 * that relies on pagefault_disable() to tell if there's a
3526 * bug. NOTE: the write to guest memory may not go through if
3527 * during postcopy live migration or if there's heavy guest
3528 * paging.
3529 */
3530 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003531 /*
3532 * kvm_memslots() will be called by
3533 * kvm_write_guest_offset_cached() so take the srcu lock.
3534 */
3535 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003536 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003537 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003538 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003539 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003540 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003541 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003542 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3543 * on every vmexit, but if not, we might have a stale dr6 from the
3544 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003545 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003546 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003547}
3548
Carsten Otte313a3dc2007-10-11 19:16:52 +02003549static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3550 struct kvm_lapic_state *s)
3551{
Liran Alonfa59cc02017-12-24 18:12:53 +02003552 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003553 kvm_x86_ops->sync_pir_to_irr(vcpu);
3554
Radim Krčmářa92e2542016-07-12 22:09:22 +02003555 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003556}
3557
3558static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3559 struct kvm_lapic_state *s)
3560{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003561 int r;
3562
3563 r = kvm_apic_set_state(vcpu, s);
3564 if (r)
3565 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003566 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003567
3568 return 0;
3569}
3570
Matt Gingell127a4572015-11-17 17:32:05 +01003571static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3572{
3573 return (!lapic_in_kernel(vcpu) ||
3574 kvm_apic_accept_pic_intr(vcpu));
3575}
3576
Matt Gingell782d4222015-11-16 15:26:00 -08003577/*
3578 * if userspace requested an interrupt window, check that the
3579 * interrupt window is open.
3580 *
3581 * No need to exit to userspace if we already have an interrupt queued.
3582 */
3583static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3584{
3585 return kvm_arch_interrupt_allowed(vcpu) &&
3586 !kvm_cpu_has_interrupt(vcpu) &&
3587 !kvm_event_needs_reinjection(vcpu) &&
3588 kvm_cpu_accept_dm_intr(vcpu);
3589}
3590
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003591static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3592 struct kvm_interrupt *irq)
3593{
Chen Gang02cdb502013-02-27 11:33:25 +08003594 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003595 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003596
3597 if (!irqchip_in_kernel(vcpu->kvm)) {
3598 kvm_queue_interrupt(vcpu, irq->irq, false);
3599 kvm_make_request(KVM_REQ_EVENT, vcpu);
3600 return 0;
3601 }
3602
3603 /*
3604 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3605 * fail for in-kernel 8259.
3606 */
3607 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003608 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003609
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003610 if (vcpu->arch.pending_external_vector != -1)
3611 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003612
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003613 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003614 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003615 return 0;
3616}
3617
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003618static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3619{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003620 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003621
3622 return 0;
3623}
3624
Paolo Bonzinif0778252015-04-01 15:06:40 +02003625static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3626{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003627 kvm_make_request(KVM_REQ_SMI, vcpu);
3628
Paolo Bonzinif0778252015-04-01 15:06:40 +02003629 return 0;
3630}
3631
Avi Kivityb209749f2007-10-22 16:50:39 +02003632static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3633 struct kvm_tpr_access_ctl *tac)
3634{
3635 if (tac->flags)
3636 return -EINVAL;
3637 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3638 return 0;
3639}
3640
Huang Ying890ca9a2009-05-11 16:48:15 +08003641static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3642 u64 mcg_cap)
3643{
3644 int r;
3645 unsigned bank_num = mcg_cap & 0xff, bank;
3646
3647 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003648 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003649 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003650 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003651 goto out;
3652 r = 0;
3653 vcpu->arch.mcg_cap = mcg_cap;
3654 /* Init IA32_MCG_CTL to all 1s */
3655 if (mcg_cap & MCG_CTL_P)
3656 vcpu->arch.mcg_ctl = ~(u64)0;
3657 /* Init IA32_MCi_CTL to all 1s */
3658 for (bank = 0; bank < bank_num; bank++)
3659 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003660
Sean Christopherson92735b12019-08-02 15:06:17 -07003661 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003662out:
3663 return r;
3664}
3665
3666static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3667 struct kvm_x86_mce *mce)
3668{
3669 u64 mcg_cap = vcpu->arch.mcg_cap;
3670 unsigned bank_num = mcg_cap & 0xff;
3671 u64 *banks = vcpu->arch.mce_banks;
3672
3673 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3674 return -EINVAL;
3675 /*
3676 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3677 * reporting is disabled
3678 */
3679 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3680 vcpu->arch.mcg_ctl != ~(u64)0)
3681 return 0;
3682 banks += 4 * mce->bank;
3683 /*
3684 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3685 * reporting is disabled for the bank
3686 */
3687 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3688 return 0;
3689 if (mce->status & MCI_STATUS_UC) {
3690 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003691 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003692 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003693 return 0;
3694 }
3695 if (banks[1] & MCI_STATUS_VAL)
3696 mce->status |= MCI_STATUS_OVER;
3697 banks[2] = mce->addr;
3698 banks[3] = mce->misc;
3699 vcpu->arch.mcg_status = mce->mcg_status;
3700 banks[1] = mce->status;
3701 kvm_queue_exception(vcpu, MC_VECTOR);
3702 } else if (!(banks[1] & MCI_STATUS_VAL)
3703 || !(banks[1] & MCI_STATUS_UC)) {
3704 if (banks[1] & MCI_STATUS_VAL)
3705 mce->status |= MCI_STATUS_OVER;
3706 banks[2] = mce->addr;
3707 banks[3] = mce->misc;
3708 banks[1] = mce->status;
3709 } else
3710 banks[1] |= MCI_STATUS_OVER;
3711 return 0;
3712}
3713
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003714static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3715 struct kvm_vcpu_events *events)
3716{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003717 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003718
Wanpeng Li664f8e22017-08-24 03:35:09 -07003719 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003720 * The API doesn't provide the instruction length for software
3721 * exceptions, so don't report them. As long as the guest RIP
3722 * isn't advanced, we should expect to encounter the exception
3723 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003724 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003725 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3726 events->exception.injected = 0;
3727 events->exception.pending = 0;
3728 } else {
3729 events->exception.injected = vcpu->arch.exception.injected;
3730 events->exception.pending = vcpu->arch.exception.pending;
3731 /*
3732 * For ABI compatibility, deliberately conflate
3733 * pending and injected exceptions when
3734 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3735 */
3736 if (!vcpu->kvm->arch.exception_payload_enabled)
3737 events->exception.injected |=
3738 vcpu->arch.exception.pending;
3739 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003740 events->exception.nr = vcpu->arch.exception.nr;
3741 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3742 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003743 events->exception_has_payload = vcpu->arch.exception.has_payload;
3744 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003745
Jan Kiszka03b82a32010-02-15 10:45:41 +01003746 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003747 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003748 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003749 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003750 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003751
3752 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003753 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003754 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003755 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003756
Jan Kiszka66450a22013-03-13 12:42:34 +01003757 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003758
Paolo Bonzinif0778252015-04-01 15:06:40 +02003759 events->smi.smm = is_smm(vcpu);
3760 events->smi.pending = vcpu->arch.smi_pending;
3761 events->smi.smm_inside_nmi =
3762 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3763 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3764
Jan Kiszkadab4b912009-12-06 18:24:15 +01003765 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003766 | KVM_VCPUEVENT_VALID_SHADOW
3767 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003768 if (vcpu->kvm->arch.exception_payload_enabled)
3769 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3770
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003771 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003772}
3773
Sean Christophersonc5833c72019-04-02 08:03:10 -07003774static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003775
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003776static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3777 struct kvm_vcpu_events *events)
3778{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003779 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003780 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003781 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003782 | KVM_VCPUEVENT_VALID_SMM
3783 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003784 return -EINVAL;
3785
Jim Mattson59073aa2018-10-16 14:29:20 -07003786 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3787 if (!vcpu->kvm->arch.exception_payload_enabled)
3788 return -EINVAL;
3789 if (events->exception.pending)
3790 events->exception.injected = 0;
3791 else
3792 events->exception_has_payload = 0;
3793 } else {
3794 events->exception.pending = 0;
3795 events->exception_has_payload = 0;
3796 }
3797
3798 if ((events->exception.injected || events->exception.pending) &&
3799 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003800 return -EINVAL;
3801
David Hildenbrand28bf2882017-03-23 11:46:03 +01003802 /* INITs are latched while in SMM */
3803 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3804 (events->smi.smm || events->smi.pending) &&
3805 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3806 return -EINVAL;
3807
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003808 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003809 vcpu->arch.exception.injected = events->exception.injected;
3810 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003811 vcpu->arch.exception.nr = events->exception.nr;
3812 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3813 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003814 vcpu->arch.exception.has_payload = events->exception_has_payload;
3815 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003816
Liran Alon04140b42018-03-23 03:01:31 +03003817 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003818 vcpu->arch.interrupt.nr = events->interrupt.nr;
3819 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003820 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3821 kvm_x86_ops->set_interrupt_shadow(vcpu,
3822 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003823
3824 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003825 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3826 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003827 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3828
Jan Kiszka66450a22013-03-13 12:42:34 +01003829 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003830 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003831 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003832
Paolo Bonzinif0778252015-04-01 15:06:40 +02003833 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003834 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3835 if (events->smi.smm)
3836 vcpu->arch.hflags |= HF_SMM_MASK;
3837 else
3838 vcpu->arch.hflags &= ~HF_SMM_MASK;
3839 kvm_smm_changed(vcpu);
3840 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003841
Paolo Bonzinif0778252015-04-01 15:06:40 +02003842 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003843
3844 if (events->smi.smm) {
3845 if (events->smi.smm_inside_nmi)
3846 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003847 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003848 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003849 }
3850
3851 if (lapic_in_kernel(vcpu)) {
3852 if (events->smi.latched_init)
3853 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3854 else
3855 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003856 }
3857 }
3858
Avi Kivity3842d132010-07-27 12:30:24 +03003859 kvm_make_request(KVM_REQ_EVENT, vcpu);
3860
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003861 return 0;
3862}
3863
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003864static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3865 struct kvm_debugregs *dbgregs)
3866{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003867 unsigned long val;
3868
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003869 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003870 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003871 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003872 dbgregs->dr7 = vcpu->arch.dr7;
3873 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003874 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003875}
3876
3877static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3878 struct kvm_debugregs *dbgregs)
3879{
3880 if (dbgregs->flags)
3881 return -EINVAL;
3882
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003883 if (dbgregs->dr6 & ~0xffffffffull)
3884 return -EINVAL;
3885 if (dbgregs->dr7 & ~0xffffffffull)
3886 return -EINVAL;
3887
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003888 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003889 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003890 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003891 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003892 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003893 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003894
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003895 return 0;
3896}
3897
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003898#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3899
3900static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3901{
Marc Orrb666a4b2018-11-06 14:53:56 -08003902 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003903 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003904 u64 valid;
3905
3906 /*
3907 * Copy legacy XSAVE area, to avoid complications with CPUID
3908 * leaves 0 and 1 in the loop below.
3909 */
3910 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3911
3912 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01003913 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003914 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3915
3916 /*
3917 * Copy each region from the possibly compacted offset to the
3918 * non-compacted offset.
3919 */
Dave Hansend91cab72015-09-02 16:31:26 -07003920 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003921 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003922 u64 xfeature_mask = valid & -valid;
3923 int xfeature_nr = fls64(xfeature_mask) - 1;
3924 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003925
3926 if (src) {
3927 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003928 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003929 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003930 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003931 memcpy(dest + offset, &vcpu->arch.pkru,
3932 sizeof(vcpu->arch.pkru));
3933 else
3934 memcpy(dest + offset, src, size);
3935
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003936 }
3937
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003938 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003939 }
3940}
3941
3942static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3943{
Marc Orrb666a4b2018-11-06 14:53:56 -08003944 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003945 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3946 u64 valid;
3947
3948 /*
3949 * Copy legacy XSAVE area, to avoid complications with CPUID
3950 * leaves 0 and 1 in the loop below.
3951 */
3952 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3953
3954 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003955 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02003956 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02003957 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003958
3959 /*
3960 * Copy each region from the non-compacted offset to the
3961 * possibly compacted offset.
3962 */
Dave Hansend91cab72015-09-02 16:31:26 -07003963 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003964 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003965 u64 xfeature_mask = valid & -valid;
3966 int xfeature_nr = fls64(xfeature_mask) - 1;
3967 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003968
3969 if (dest) {
3970 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003971 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003972 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003973 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003974 memcpy(&vcpu->arch.pkru, src + offset,
3975 sizeof(vcpu->arch.pkru));
3976 else
3977 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003978 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003979
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003980 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003981 }
3982}
3983
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003984static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3985 struct kvm_xsave *guest_xsave)
3986{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003987 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003988 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3989 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003990 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003991 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08003992 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003993 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003994 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07003995 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003996 }
3997}
3998
Wanpeng Lia5758132017-05-11 02:58:55 -07003999#define XSAVE_MXCSR_OFFSET 24
4000
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004001static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4002 struct kvm_xsave *guest_xsave)
4003{
4004 u64 xstate_bv =
4005 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004006 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004007
Borislav Petkovd366bf72016-04-04 22:25:02 +02004008 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004009 /*
4010 * Here we allow setting states that are not present in
4011 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4012 * with old userspace.
4013 */
Wanpeng Lia5758132017-05-11 02:58:55 -07004014 if (xstate_bv & ~kvm_supported_xcr0() ||
4015 mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004016 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004017 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004018 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004019 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4020 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004021 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004022 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004023 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004024 }
4025 return 0;
4026}
4027
4028static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4029 struct kvm_xcrs *guest_xcrs)
4030{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004031 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004032 guest_xcrs->nr_xcrs = 0;
4033 return;
4034 }
4035
4036 guest_xcrs->nr_xcrs = 1;
4037 guest_xcrs->flags = 0;
4038 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4039 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4040}
4041
4042static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4043 struct kvm_xcrs *guest_xcrs)
4044{
4045 int i, r = 0;
4046
Borislav Petkovd366bf72016-04-04 22:25:02 +02004047 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004048 return -EINVAL;
4049
4050 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4051 return -EINVAL;
4052
4053 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4054 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004055 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004056 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004057 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004058 break;
4059 }
4060 if (r)
4061 r = -EINVAL;
4062 return r;
4063}
4064
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004065/*
4066 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4067 * stopped by the hypervisor. This function will be called from the host only.
4068 * EINVAL is returned when the host attempts to set the flag for a guest that
4069 * does not support pv clocks.
4070 */
4071static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4072{
Andy Honig0b794592013-02-20 14:48:10 -08004073 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004074 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004075 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004076 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4077 return 0;
4078}
4079
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004080static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4081 struct kvm_enable_cap *cap)
4082{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004083 int r;
4084 uint16_t vmcs_version;
4085 void __user *user_ptr;
4086
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004087 if (cap->flags)
4088 return -EINVAL;
4089
4090 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004091 case KVM_CAP_HYPERV_SYNIC2:
4092 if (cap->args[0])
4093 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004094 /* fall through */
4095
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004096 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004097 if (!irqchip_in_kernel(vcpu->kvm))
4098 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004099 return kvm_hv_activate_synic(vcpu, cap->cap ==
4100 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004101 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004102 if (!kvm_x86_ops->nested_enable_evmcs)
4103 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004104 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4105 if (!r) {
4106 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4107 if (copy_to_user(user_ptr, &vmcs_version,
4108 sizeof(vmcs_version)))
4109 r = -EFAULT;
4110 }
4111 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004112 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4113 if (!kvm_x86_ops->enable_direct_tlbflush)
4114 return -ENOTTY;
4115
4116 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004117
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004118 default:
4119 return -EINVAL;
4120 }
4121}
4122
Carsten Otte313a3dc2007-10-11 19:16:52 +02004123long kvm_arch_vcpu_ioctl(struct file *filp,
4124 unsigned int ioctl, unsigned long arg)
4125{
4126 struct kvm_vcpu *vcpu = filp->private_data;
4127 void __user *argp = (void __user *)arg;
4128 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004129 union {
4130 struct kvm_lapic_state *lapic;
4131 struct kvm_xsave *xsave;
4132 struct kvm_xcrs *xcrs;
4133 void *buffer;
4134 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004135
Christoffer Dall9b0624712017-12-04 21:35:36 +01004136 vcpu_load(vcpu);
4137
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004138 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004139 switch (ioctl) {
4140 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004141 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004142 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004143 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004144 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4145 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004146
Dave Hansenb772ff32008-08-11 10:01:47 -07004147 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004148 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004149 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004150 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004151 if (r)
4152 goto out;
4153 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004154 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004155 goto out;
4156 r = 0;
4157 break;
4158 }
4159 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004160 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004161 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004162 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004163 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004164 if (IS_ERR(u.lapic)) {
4165 r = PTR_ERR(u.lapic);
4166 goto out_nofree;
4167 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004168
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004169 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004170 break;
4171 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004172 case KVM_INTERRUPT: {
4173 struct kvm_interrupt irq;
4174
4175 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004176 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004177 goto out;
4178 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004179 break;
4180 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004181 case KVM_NMI: {
4182 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004183 break;
4184 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004185 case KVM_SMI: {
4186 r = kvm_vcpu_ioctl_smi(vcpu);
4187 break;
4188 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004189 case KVM_SET_CPUID: {
4190 struct kvm_cpuid __user *cpuid_arg = argp;
4191 struct kvm_cpuid cpuid;
4192
4193 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004194 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004195 goto out;
4196 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004197 break;
4198 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004199 case KVM_SET_CPUID2: {
4200 struct kvm_cpuid2 __user *cpuid_arg = argp;
4201 struct kvm_cpuid2 cpuid;
4202
4203 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004204 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004205 goto out;
4206 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004207 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004208 break;
4209 }
4210 case KVM_GET_CPUID2: {
4211 struct kvm_cpuid2 __user *cpuid_arg = argp;
4212 struct kvm_cpuid2 cpuid;
4213
4214 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004215 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004216 goto out;
4217 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004218 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004219 if (r)
4220 goto out;
4221 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004222 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004223 goto out;
4224 r = 0;
4225 break;
4226 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004227 case KVM_GET_MSRS: {
4228 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004229 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004230 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004231 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004232 }
4233 case KVM_SET_MSRS: {
4234 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004235 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004236 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004237 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004238 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004239 case KVM_TPR_ACCESS_REPORTING: {
4240 struct kvm_tpr_access_ctl tac;
4241
4242 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004243 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004244 goto out;
4245 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4246 if (r)
4247 goto out;
4248 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004249 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004250 goto out;
4251 r = 0;
4252 break;
4253 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004254 case KVM_SET_VAPIC_ADDR: {
4255 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004256 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004257
4258 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004259 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004260 goto out;
4261 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004262 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004263 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004264 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004265 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004266 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004267 break;
4268 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004269 case KVM_X86_SETUP_MCE: {
4270 u64 mcg_cap;
4271
4272 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004273 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004274 goto out;
4275 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4276 break;
4277 }
4278 case KVM_X86_SET_MCE: {
4279 struct kvm_x86_mce mce;
4280
4281 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004282 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004283 goto out;
4284 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4285 break;
4286 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004287 case KVM_GET_VCPU_EVENTS: {
4288 struct kvm_vcpu_events events;
4289
4290 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4291
4292 r = -EFAULT;
4293 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4294 break;
4295 r = 0;
4296 break;
4297 }
4298 case KVM_SET_VCPU_EVENTS: {
4299 struct kvm_vcpu_events events;
4300
4301 r = -EFAULT;
4302 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4303 break;
4304
4305 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4306 break;
4307 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004308 case KVM_GET_DEBUGREGS: {
4309 struct kvm_debugregs dbgregs;
4310
4311 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4312
4313 r = -EFAULT;
4314 if (copy_to_user(argp, &dbgregs,
4315 sizeof(struct kvm_debugregs)))
4316 break;
4317 r = 0;
4318 break;
4319 }
4320 case KVM_SET_DEBUGREGS: {
4321 struct kvm_debugregs dbgregs;
4322
4323 r = -EFAULT;
4324 if (copy_from_user(&dbgregs, argp,
4325 sizeof(struct kvm_debugregs)))
4326 break;
4327
4328 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4329 break;
4330 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004331 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004332 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004333 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004334 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004335 break;
4336
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004337 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004338
4339 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004340 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004341 break;
4342 r = 0;
4343 break;
4344 }
4345 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004346 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004347 if (IS_ERR(u.xsave)) {
4348 r = PTR_ERR(u.xsave);
4349 goto out_nofree;
4350 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004351
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004352 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004353 break;
4354 }
4355 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004356 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004357 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004358 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004359 break;
4360
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004361 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004362
4363 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004364 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004365 sizeof(struct kvm_xcrs)))
4366 break;
4367 r = 0;
4368 break;
4369 }
4370 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004371 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004372 if (IS_ERR(u.xcrs)) {
4373 r = PTR_ERR(u.xcrs);
4374 goto out_nofree;
4375 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004376
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004377 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004378 break;
4379 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004380 case KVM_SET_TSC_KHZ: {
4381 u32 user_tsc_khz;
4382
4383 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004384 user_tsc_khz = (u32)arg;
4385
4386 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4387 goto out;
4388
Zachary Amsdencc578282012-02-03 15:43:50 -02004389 if (user_tsc_khz == 0)
4390 user_tsc_khz = tsc_khz;
4391
Haozhong Zhang381d5852015-10-20 15:39:04 +08004392 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4393 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004394
Joerg Roedel92a1f122011-03-25 09:44:51 +01004395 goto out;
4396 }
4397 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004398 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004399 goto out;
4400 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004401 case KVM_KVMCLOCK_CTRL: {
4402 r = kvm_set_guest_paused(vcpu);
4403 goto out;
4404 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004405 case KVM_ENABLE_CAP: {
4406 struct kvm_enable_cap cap;
4407
4408 r = -EFAULT;
4409 if (copy_from_user(&cap, argp, sizeof(cap)))
4410 goto out;
4411 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4412 break;
4413 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004414 case KVM_GET_NESTED_STATE: {
4415 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4416 u32 user_data_size;
4417
4418 r = -EINVAL;
4419 if (!kvm_x86_ops->get_nested_state)
4420 break;
4421
4422 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004423 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004424 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004425 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004426
4427 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4428 user_data_size);
4429 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004430 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004431
4432 if (r > user_data_size) {
4433 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004434 r = -EFAULT;
4435 else
4436 r = -E2BIG;
4437 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004438 }
Liran Alon26b471c2018-09-16 14:28:20 +03004439
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004440 r = 0;
4441 break;
4442 }
4443 case KVM_SET_NESTED_STATE: {
4444 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4445 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004446 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004447
4448 r = -EINVAL;
4449 if (!kvm_x86_ops->set_nested_state)
4450 break;
4451
Liran Alon26b471c2018-09-16 14:28:20 +03004452 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004453 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004454 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004455
Liran Alon26b471c2018-09-16 14:28:20 +03004456 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004457 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004458 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004459
4460 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004461 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4462 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004463 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004464
4465 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004466 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4467 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004468 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004469
Sean Christophersonad5996d2019-11-22 08:58:18 -08004470 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004471 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004472 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004473 break;
4474 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004475 case KVM_GET_SUPPORTED_HV_CPUID: {
4476 struct kvm_cpuid2 __user *cpuid_arg = argp;
4477 struct kvm_cpuid2 cpuid;
4478
4479 r = -EFAULT;
4480 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4481 goto out;
4482
4483 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4484 cpuid_arg->entries);
4485 if (r)
4486 goto out;
4487
4488 r = -EFAULT;
4489 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4490 goto out;
4491 r = 0;
4492 break;
4493 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004494 default:
4495 r = -EINVAL;
4496 }
4497out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004498 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004499out_nofree:
4500 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004501 return r;
4502}
4503
Souptick Joarder1499fa82018-04-19 00:49:58 +05304504vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004505{
4506 return VM_FAULT_SIGBUS;
4507}
4508
Carsten Otte1fe779f2007-10-29 16:08:35 +01004509static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4510{
4511 int ret;
4512
4513 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004514 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004515 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4516 return ret;
4517}
4518
Sheng Yangb927a3c2009-07-21 10:42:48 +08004519static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4520 u64 ident_addr)
4521{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004522 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004523}
4524
Carsten Otte1fe779f2007-10-29 16:08:35 +01004525static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004526 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004527{
4528 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4529 return -EINVAL;
4530
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004531 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004532
4533 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004534 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004535
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004536 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004537 return 0;
4538}
4539
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004540static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004541{
Dave Hansen39de71e2010-08-19 18:11:14 -07004542 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004543}
4544
Carsten Otte1fe779f2007-10-29 16:08:35 +01004545static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4546{
David Hildenbrand90bca052017-04-07 10:50:23 +02004547 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004548 int r;
4549
4550 r = 0;
4551 switch (chip->chip_id) {
4552 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004553 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004554 sizeof(struct kvm_pic_state));
4555 break;
4556 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004557 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004558 sizeof(struct kvm_pic_state));
4559 break;
4560 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004561 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004562 break;
4563 default:
4564 r = -EINVAL;
4565 break;
4566 }
4567 return r;
4568}
4569
4570static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4571{
David Hildenbrand90bca052017-04-07 10:50:23 +02004572 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004573 int r;
4574
4575 r = 0;
4576 switch (chip->chip_id) {
4577 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004578 spin_lock(&pic->lock);
4579 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004580 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004581 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004582 break;
4583 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004584 spin_lock(&pic->lock);
4585 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004586 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004587 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004588 break;
4589 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004590 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004591 break;
4592 default:
4593 r = -EINVAL;
4594 break;
4595 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004596 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004597 return r;
4598}
4599
Sheng Yange0f63cb2008-03-04 00:50:59 +08004600static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4601{
Radim Krčmář34f39412016-03-02 22:56:50 +01004602 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4603
4604 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4605
4606 mutex_lock(&kps->lock);
4607 memcpy(ps, &kps->channels, sizeof(*ps));
4608 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304609 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004610}
4611
4612static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4613{
Andrew Honig01856042015-11-18 14:50:23 -08004614 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004615 struct kvm_pit *pit = kvm->arch.vpit;
4616
4617 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004618 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004619 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004620 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4621 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304622 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004623}
4624
4625static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4626{
Beth Kone9f42752009-07-07 11:50:38 -04004627 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4628 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4629 sizeof(ps->channels));
4630 ps->flags = kvm->arch.vpit->pit_state.flags;
4631 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004632 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304633 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004634}
4635
4636static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4637{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304638 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004639 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004640 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004641 struct kvm_pit *pit = kvm->arch.vpit;
4642
4643 mutex_lock(&pit->pit_state.lock);
4644 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004645 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4646 if (!prev_legacy && cur_legacy)
4647 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004648 memcpy(&pit->pit_state.channels, &ps->channels,
4649 sizeof(pit->pit_state.channels));
4650 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004651 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004652 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004653 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004654 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304655 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004656}
4657
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004658static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4659 struct kvm_reinject_control *control)
4660{
Radim Krčmář71474e22016-03-02 22:56:45 +01004661 struct kvm_pit *pit = kvm->arch.vpit;
4662
4663 if (!pit)
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004664 return -ENXIO;
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004665
Radim Krčmář71474e22016-03-02 22:56:45 +01004666 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4667 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4668 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4669 */
4670 mutex_lock(&pit->pit_state.lock);
4671 kvm_pit_set_reinject(pit, control->pit_reinject);
4672 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004673
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004674 return 0;
4675}
4676
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004677/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004678 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
4679 * @kvm: kvm instance
4680 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004681 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004682 * Steps 1-4 below provide general overview of dirty page logging. See
4683 * kvm_get_dirty_log_protect() function description for additional details.
4684 *
4685 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
4686 * always flush the TLB (step 4) even if previous step failed and the dirty
4687 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
4688 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
4689 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004690 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004691 * 1. Take a snapshot of the bit and clear it if needed.
4692 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004693 * 3. Copy the snapshot to the userspace.
4694 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004695 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004696int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004697{
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004698 bool flush = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004699 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004700
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004701 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004702
Kai Huang88178fd2015-01-28 10:54:27 +08004703 /*
4704 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4705 */
4706 if (kvm_x86_ops->flush_log_dirty)
4707 kvm_x86_ops->flush_log_dirty(kvm);
4708
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004709 r = kvm_get_dirty_log_protect(kvm, log, &flush);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004710
4711 /*
4712 * All the TLBs can be flushed out of mmu lock, see the comments in
4713 * kvm_mmu_slot_remove_write_access().
4714 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004715 lockdep_assert_held(&kvm->slots_lock);
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004716 if (flush)
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004717 kvm_flush_remote_tlbs(kvm);
4718
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004719 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004720 return r;
4721}
4722
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02004723int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
4724{
4725 bool flush = false;
4726 int r;
4727
4728 mutex_lock(&kvm->slots_lock);
4729
4730 /*
4731 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4732 */
4733 if (kvm_x86_ops->flush_log_dirty)
4734 kvm_x86_ops->flush_log_dirty(kvm);
4735
4736 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
4737
4738 /*
4739 * All the TLBs can be flushed out of mmu lock, see the comments in
4740 * kvm_mmu_slot_remove_write_access().
4741 */
4742 lockdep_assert_held(&kvm->slots_lock);
4743 if (flush)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004744 kvm_flush_remote_tlbs(kvm);
4745
4746 mutex_unlock(&kvm->slots_lock);
4747 return r;
Avi Kivity367e1312009-08-26 14:57:07 +03004748}
Dave Hansenf0d66272008-08-11 10:01:45 -07004749
4750int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4751 bool line_status)
4752{
4753 if (!irqchip_in_kernel(kvm))
4754 return -ENXIO;
4755
Beth Kone9f42752009-07-07 11:50:38 -04004756 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Dave Hansenf0d66272008-08-11 10:01:45 -07004757 irq_event->irq, irq_event->level,
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004758 line_status);
Dave Hansenf0d66272008-08-11 10:01:45 -07004759 return 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004760}
4761
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004762int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4763 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004764{
4765 int r;
4766
4767 if (cap->flags)
4768 return -EINVAL;
4769
4770 switch (cap->cap) {
4771 case KVM_CAP_DISABLE_QUIRKS:
4772 kvm->arch.disabled_quirks = cap->args[0];
4773 r = 0;
4774 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004775 case KVM_CAP_SPLIT_IRQCHIP: {
4776 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004777 r = -EINVAL;
4778 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4779 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004780 r = -EEXIST;
4781 if (irqchip_in_kernel(kvm))
4782 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004783 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004784 goto split_irqchip_unlock;
4785 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004786 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004787 goto split_irqchip_unlock;
4788 /* Pairs with irqchip_in_kernel. */
4789 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004790 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004791 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004792 r = 0;
4793split_irqchip_unlock:
4794 mutex_unlock(&kvm->lock);
4795 break;
4796 }
Radim Krčmář371313132016-07-12 22:09:27 +02004797 case KVM_CAP_X2APIC_API:
4798 r = -EINVAL;
4799 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4800 break;
4801
4802 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4803 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004804 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4805 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004806
4807 r = 0;
4808 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004809 case KVM_CAP_X86_DISABLE_EXITS:
4810 r = -EINVAL;
4811 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4812 break;
4813
4814 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4815 kvm_can_mwait_in_guest())
4816 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004817 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004818 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004819 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4820 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004821 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4822 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004823 r = 0;
4824 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004825 case KVM_CAP_MSR_PLATFORM_INFO:
4826 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4827 r = 0;
4828 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004829 case KVM_CAP_EXCEPTION_PAYLOAD:
4830 kvm->arch.exception_payload_enabled = cap->args[0];
4831 r = 0;
4832 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004833 default:
4834 r = -EINVAL;
4835 break;
4836 }
4837 return r;
4838}
4839
Carsten Otte1fe779f2007-10-29 16:08:35 +01004840long kvm_arch_vm_ioctl(struct file *filp,
4841 unsigned int ioctl, unsigned long arg)
4842{
4843 struct kvm *kvm = filp->private_data;
4844 void __user *argp = (void __user *)arg;
4845 int r = -ENOTTY;
4846 /*
4847 * This union makes it completely explicit to gcc-3.x
4848 * that these two variables' stack usage should be
4849 * combined, not added together.
4850 */
4851 union {
4852 struct kvm_pit_state ps;
4853 struct kvm_pit_state2 ps2;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004854 struct kvm_pit_config pit_config;
4855 } u;
4856
4857 switch (ioctl) {
4858 case KVM_SET_TSS_ADDR:
4859 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004860 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004861 case KVM_SET_IDENTITY_MAP_ADDR: {
4862 u64 ident_addr;
4863
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004864 mutex_lock(&kvm->lock);
4865 r = -EINVAL;
4866 if (kvm->created_vcpus)
4867 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004868 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004869 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004870 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004871 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004872set_identity_unlock:
4873 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004874 break;
4875 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004876 case KVM_SET_NR_MMU_PAGES:
4877 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004878 break;
4879 case KVM_GET_NR_MMU_PAGES:
4880 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4881 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004882 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004883 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004884
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004885 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004886 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004887 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004888
Avi Kivity3e515702012-03-05 14:23:29 +02004889 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004890 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004891 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004892
4893 r = kvm_pic_init(kvm);
4894 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004895 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004896
4897 r = kvm_ioapic_init(kvm);
4898 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004899 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004900 goto create_irqchip_unlock;
4901 }
4902
Avi Kivity399ec802008-11-19 13:58:46 +02004903 r = kvm_setup_default_irq_routing(kvm);
4904 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004905 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004906 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004907 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004908 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004909 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004910 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004911 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004912 create_irqchip_unlock:
4913 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004914 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004915 }
Sheng Yang78376992008-01-28 05:10:22 +08004916 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004917 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4918 goto create_pit;
4919 case KVM_CREATE_PIT2:
4920 r = -EFAULT;
4921 if (copy_from_user(&u.pit_config, argp,
4922 sizeof(struct kvm_pit_config)))
4923 goto out;
4924 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004925 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004926 r = -EEXIST;
4927 if (kvm->arch.vpit)
4928 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004929 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004930 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004931 if (kvm->arch.vpit)
4932 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004933 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004934 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004935 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004936 case KVM_GET_IRQCHIP: {
4937 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004938 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004939
Sasha Levinff5c2c02011-12-04 19:36:29 +02004940 chip = memdup_user(argp, sizeof(*chip));
4941 if (IS_ERR(chip)) {
4942 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004943 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004944 }
4945
Carsten Otte1fe779f2007-10-29 16:08:35 +01004946 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004947 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004948 goto get_irqchip_out;
4949 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4950 if (r)
4951 goto get_irqchip_out;
4952 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004953 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004954 goto get_irqchip_out;
4955 r = 0;
4956 get_irqchip_out:
4957 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004958 break;
4959 }
4960 case KVM_SET_IRQCHIP: {
4961 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004962 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004963
Sasha Levinff5c2c02011-12-04 19:36:29 +02004964 chip = memdup_user(argp, sizeof(*chip));
4965 if (IS_ERR(chip)) {
4966 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004967 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004968 }
4969
Carsten Otte1fe779f2007-10-29 16:08:35 +01004970 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004971 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004972 goto set_irqchip_out;
4973 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07004974 set_irqchip_out:
4975 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004976 break;
4977 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08004978 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004979 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004980 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004981 goto out;
4982 r = -ENXIO;
4983 if (!kvm->arch.vpit)
4984 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004985 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004986 if (r)
4987 goto out;
4988 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004989 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004990 goto out;
4991 r = 0;
4992 break;
4993 }
4994 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004995 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004996 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004997 goto out;
4998 r = -ENXIO;
4999 if (!kvm->arch.vpit)
5000 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005001 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005002 break;
5003 }
Beth Kone9f42752009-07-07 11:50:38 -04005004 case KVM_GET_PIT2: {
5005 r = -ENXIO;
5006 if (!kvm->arch.vpit)
5007 goto out;
5008 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5009 if (r)
5010 goto out;
5011 r = -EFAULT;
5012 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5013 goto out;
5014 r = 0;
5015 break;
5016 }
5017 case KVM_SET_PIT2: {
5018 r = -EFAULT;
5019 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5020 goto out;
5021 r = -ENXIO;
5022 if (!kvm->arch.vpit)
5023 goto out;
5024 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005025 break;
5026 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005027 case KVM_REINJECT_CONTROL: {
5028 struct kvm_reinject_control control;
5029 r = -EFAULT;
5030 if (copy_from_user(&control, argp, sizeof(control)))
5031 goto out;
5032 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005033 break;
5034 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005035 case KVM_SET_BOOT_CPU_ID:
5036 r = 0;
5037 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005038 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005039 r = -EBUSY;
5040 else
5041 kvm->arch.bsp_vcpu_id = arg;
5042 mutex_unlock(&kvm->lock);
5043 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005044 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005045 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005046 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005047 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005048 goto out;
5049 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005050 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005051 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005052 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005053 r = 0;
5054 break;
5055 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005056 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005057 struct kvm_clock_data user_ns;
5058 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005059
5060 r = -EFAULT;
5061 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5062 goto out;
5063
5064 r = -EINVAL;
5065 if (user_ns.flags)
5066 goto out;
5067
5068 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005069 /*
5070 * TODO: userspace has to take care of races with VCPU_RUN, so
5071 * kvm_gen_update_masterclock() can be cut down to locked
5072 * pvclock_update_vm_gtod_copy().
5073 */
5074 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005075 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005076 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005077 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005078 break;
5079 }
5080 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005081 struct kvm_clock_data user_ns;
5082 u64 now_ns;
5083
Marcelo Tosattie891a322017-04-17 12:51:37 -03005084 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005085 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005086 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005087 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005088
5089 r = -EFAULT;
5090 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5091 goto out;
5092 r = 0;
5093 break;
5094 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005095 case KVM_MEMORY_ENCRYPT_OP: {
5096 r = -ENOTTY;
5097 if (kvm_x86_ops->mem_enc_op)
5098 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5099 break;
5100 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005101 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5102 struct kvm_enc_region region;
5103
5104 r = -EFAULT;
5105 if (copy_from_user(&region, argp, sizeof(region)))
5106 goto out;
5107
5108 r = -ENOTTY;
5109 if (kvm_x86_ops->mem_enc_reg_region)
5110 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5111 break;
5112 }
5113 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5114 struct kvm_enc_region region;
5115
5116 r = -EFAULT;
5117 if (copy_from_user(&region, argp, sizeof(region)))
5118 goto out;
5119
5120 r = -ENOTTY;
5121 if (kvm_x86_ops->mem_enc_unreg_region)
5122 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5123 break;
5124 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005125 case KVM_HYPERV_EVENTFD: {
5126 struct kvm_hyperv_eventfd hvevfd;
5127
5128 r = -EFAULT;
5129 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5130 goto out;
5131 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5132 break;
5133 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005134 case KVM_SET_PMU_EVENT_FILTER:
5135 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5136 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005137 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005138 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005139 }
5140out:
5141 return r;
5142}
5143
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005144static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005145{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005146 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005147 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005148 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005149
Jim Mattsone2ada662019-08-21 11:20:04 -07005150 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005151 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005152
5153 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005154
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005155 num_msrs_to_save = 0;
5156 num_emulated_msrs = 0;
5157 num_msr_based_features = 0;
5158
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005159 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5160 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005161 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005162
5163 /*
5164 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005165 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005166 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005167 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005168 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005169 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005170 continue;
5171 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005172 case MSR_TSC_AUX:
5173 if (!kvm_x86_ops->rdtscp_supported())
5174 continue;
5175 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005176 case MSR_IA32_RTIT_CTL:
5177 case MSR_IA32_RTIT_STATUS:
5178 if (!kvm_x86_ops->pt_supported())
5179 continue;
5180 break;
5181 case MSR_IA32_RTIT_CR3_MATCH:
5182 if (!kvm_x86_ops->pt_supported() ||
5183 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5184 continue;
5185 break;
5186 case MSR_IA32_RTIT_OUTPUT_BASE:
5187 case MSR_IA32_RTIT_OUTPUT_MASK:
5188 if (!kvm_x86_ops->pt_supported() ||
5189 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5190 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5191 continue;
5192 break;
5193 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
5194 if (!kvm_x86_ops->pt_supported() ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005195 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005196 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5197 continue;
5198 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005199 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005200 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005201 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5202 continue;
5203 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005204 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005205 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005206 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5207 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005208 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005209 default:
5210 break;
5211 }
5212
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005213 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005214 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005215
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005216 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5217 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005218 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005219
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005220 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005221 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005222
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005223 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005224 struct kvm_msr_entry msr;
5225
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005226 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005227 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005228 continue;
5229
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005230 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005231 }
Carsten Otte043405e2007-10-10 17:16:19 +02005232}
5233
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005234static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5235 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005236{
Avi Kivity70252a12010-01-19 12:51:22 +02005237 int handled = 0;
5238 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005239
Avi Kivity70252a12010-01-19 12:51:22 +02005240 do {
5241 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005242 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005243 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5244 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005245 break;
5246 handled += n;
5247 addr += n;
5248 len -= n;
5249 v += n;
5250 } while (len);
5251
5252 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005253}
5254
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005255static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005256{
Avi Kivity70252a12010-01-19 12:51:22 +02005257 int handled = 0;
5258 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005259
Avi Kivity70252a12010-01-19 12:51:22 +02005260 do {
5261 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005262 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005263 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5264 addr, n, v))
5265 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005266 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005267 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005268 handled += n;
5269 addr += n;
5270 len -= n;
5271 v += n;
5272 } while (len);
5273
5274 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005275}
5276
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005277static void kvm_set_segment(struct kvm_vcpu *vcpu,
5278 struct kvm_segment *var, int seg)
5279{
5280 kvm_x86_ops->set_segment(vcpu, var, seg);
5281}
5282
5283void kvm_get_segment(struct kvm_vcpu *vcpu,
5284 struct kvm_segment *var, int seg)
5285{
5286 kvm_x86_ops->get_segment(vcpu, var, seg);
5287}
5288
Paolo Bonzini54987b72014-09-02 13:23:06 +02005289gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5290 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005291{
5292 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005293
5294 BUG_ON(!mmu_is_nested(vcpu));
5295
5296 /* NPT walks are always user-walks */
5297 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005298 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005299
5300 return t_gpa;
5301}
5302
Avi Kivityab9ae312010-11-22 17:53:26 +02005303gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5304 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005305{
5306 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005307 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005308}
5309
Avi Kivityab9ae312010-11-22 17:53:26 +02005310 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5311 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005312{
5313 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5314 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005315 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005316}
5317
Avi Kivityab9ae312010-11-22 17:53:26 +02005318gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5319 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005320{
5321 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5322 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005323 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005324}
5325
5326/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005327gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5328 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005329{
Avi Kivityab9ae312010-11-22 17:53:26 +02005330 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005331}
5332
5333static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5334 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005335 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005336{
5337 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005338 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005339
5340 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005341 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005342 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005343 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005344 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005345 int ret;
5346
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005347 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005348 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005349 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5350 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005351 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005352 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005353 goto out;
5354 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005355
Izik Eidus77c20022008-12-29 01:42:19 +02005356 bytes -= toread;
5357 data += toread;
5358 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005359 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005360out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005361 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005362}
Izik Eidus77c20022008-12-29 01:42:19 +02005363
Gleb Natapov1871c602010-02-10 14:21:32 +02005364/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005365static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5366 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005367 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005368{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005369 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005370 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005371 unsigned offset;
5372 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005373
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005374 /* Inline kvm_read_guest_virt_helper for speed. */
5375 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5376 exception);
5377 if (unlikely(gpa == UNMAPPED_GVA))
5378 return X86EMUL_PROPAGATE_FAULT;
5379
5380 offset = addr & (PAGE_SIZE-1);
5381 if (WARN_ON(offset + bytes > PAGE_SIZE))
5382 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005383 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5384 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005385 if (unlikely(ret < 0))
5386 return X86EMUL_IO_NEEDED;
5387
5388 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005389}
5390
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005391int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005392 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005393 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005394{
5395 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005396
Paolo Bonzini353c0952019-01-29 18:41:16 +01005397 /*
5398 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5399 * is returned, but our callers are not ready for that and they blindly
5400 * call kvm_inject_page_fault. Ensure that they at least do not leak
5401 * uninitialized kernel stack memory into cr2 and error code.
5402 */
5403 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005404 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005405 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005406}
Nadav Har'El064aea72011-05-25 23:04:56 +03005407EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005408
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005409static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5410 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005411 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005412{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005413 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005414 u32 access = 0;
5415
5416 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5417 access |= PFERR_USER_MASK;
5418
5419 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005420}
5421
Radim Krčmář7a036a62015-10-30 16:36:24 +01005422static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5423 unsigned long addr, void *val, unsigned int bytes)
5424{
5425 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5426 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5427
5428 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5429}
5430
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005431static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5432 struct kvm_vcpu *vcpu, u32 access,
5433 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005434{
5435 void *data = val;
5436 int r = X86EMUL_CONTINUE;
5437
5438 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005439 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005440 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005441 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005442 unsigned offset = addr & (PAGE_SIZE-1);
5443 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5444 int ret;
5445
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005446 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005447 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005448 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005449 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005450 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005451 goto out;
5452 }
5453
5454 bytes -= towrite;
5455 data += towrite;
5456 addr += towrite;
5457 }
5458out:
5459 return r;
5460}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005461
5462static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005463 unsigned int bytes, struct x86_exception *exception,
5464 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005465{
5466 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005467 u32 access = PFERR_WRITE_MASK;
5468
5469 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5470 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005471
5472 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005473 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005474}
5475
5476int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5477 unsigned int bytes, struct x86_exception *exception)
5478{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005479 /* kvm_write_guest_virt_system can pull in tons of pages. */
5480 vcpu->arch.l1tf_flush_l1d = true;
5481
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005482 /*
5483 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5484 * is returned, but our callers are not ready for that and they blindly
5485 * call kvm_inject_page_fault. Ensure that they at least do not leak
5486 * uninitialized kernel stack memory into cr2 and error code.
5487 */
5488 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005489 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5490 PFERR_WRITE_MASK, exception);
5491}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005492EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005493
Wanpeng Li082d06e2018-04-03 16:28:48 -07005494int handle_ud(struct kvm_vcpu *vcpu)
5495{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005496 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005497 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005498 char sig[5]; /* ud2; .ascii "kvm" */
5499 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005500
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005501 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005502 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5503 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005504 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005505 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005506 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005507 }
5508
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005509 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005510}
5511EXPORT_SYMBOL_GPL(handle_ud);
5512
Tom Lendacky0f89b202016-12-14 14:59:23 -05005513static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5514 gpa_t gpa, bool write)
5515{
5516 /* For APIC access vmexit */
5517 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5518 return 1;
5519
5520 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5521 trace_vcpu_match_mmio(gva, gpa, write, true);
5522 return 1;
5523 }
5524
5525 return 0;
5526}
5527
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005528static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5529 gpa_t *gpa, struct x86_exception *exception,
5530 bool write)
5531{
Avi Kivity97d64b72012-09-12 14:52:00 +03005532 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5533 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005534
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005535 /*
5536 * currently PKRU is only applied to ept enabled guest so
5537 * there is no pkey in EPT page table for L1 guest or EPT
5538 * shadow page table for L2 guest.
5539 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005540 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005541 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005542 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005543 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5544 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005545 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005546 return 1;
5547 }
5548
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005549 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5550
5551 if (*gpa == UNMAPPED_GVA)
5552 return -1;
5553
Tom Lendacky0f89b202016-12-14 14:59:23 -05005554 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005555}
5556
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005557int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005558 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005559{
5560 int ret;
5561
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005562 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005563 if (ret < 0)
5564 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005565 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005566 return 1;
5567}
5568
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005569struct read_write_emulator_ops {
5570 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5571 int bytes);
5572 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5573 void *val, int bytes);
5574 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5575 int bytes, void *val);
5576 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5577 void *val, int bytes);
5578 bool write;
5579};
5580
5581static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5582{
5583 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005584 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005585 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005586 vcpu->mmio_read_completed = 0;
5587 return 1;
5588 }
5589
5590 return 0;
5591}
5592
5593static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5594 void *val, int bytes)
5595{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005596 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005597}
5598
5599static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5600 void *val, int bytes)
5601{
5602 return emulator_write_phys(vcpu, gpa, val, bytes);
5603}
5604
5605static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5606{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005607 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005608 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5609}
5610
5611static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5612 void *val, int bytes)
5613{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005614 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005615 return X86EMUL_IO_NEEDED;
5616}
5617
5618static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5619 void *val, int bytes)
5620{
Avi Kivityf78146b2012-04-18 19:22:47 +03005621 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5622
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005623 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005624 return X86EMUL_CONTINUE;
5625}
5626
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005627static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005628 .read_write_prepare = read_prepare,
5629 .read_write_emulate = read_emulate,
5630 .read_write_mmio = vcpu_mmio_read,
5631 .read_write_exit_mmio = read_exit_mmio,
5632};
5633
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005634static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005635 .read_write_emulate = write_emulate,
5636 .read_write_mmio = write_mmio,
5637 .read_write_exit_mmio = write_exit_mmio,
5638 .write = true,
5639};
5640
Xiao Guangrong22388a32011-07-13 14:32:31 +08005641static int emulator_read_write_onepage(unsigned long addr, void *val,
5642 unsigned int bytes,
5643 struct x86_exception *exception,
5644 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005645 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005646{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005647 gpa_t gpa;
5648 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005649 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005650 struct kvm_mmio_fragment *frag;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005651 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
5652
5653 /*
5654 * If the exit was due to a NPF we may already have a GPA.
5655 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5656 * Note, this cannot be used on string operations since string
5657 * operation using rep will only have the initial GPA from the NPF
5658 * occurred.
5659 */
5660 if (vcpu->arch.gpa_available &&
5661 emulator_can_use_gpa(ctxt) &&
Brijesh Singh618232e2017-08-17 18:36:57 +02005662 (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
5663 gpa = vcpu->arch.gpa_val;
5664 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5665 } else {
5666 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5667 if (ret < 0)
5668 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005669 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005670
Brijesh Singh618232e2017-08-17 18:36:57 +02005671 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005672 return X86EMUL_CONTINUE;
5673
Carsten Ottebbd9b642007-10-30 18:44:21 +01005674 /*
5675 * Is this MMIO handled locally?
5676 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005677 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005678 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005679 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005680
Avi Kivity70252a12010-01-19 12:51:22 +02005681 gpa += handled;
5682 bytes -= handled;
5683 val += handled;
5684
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005685 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5686 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5687 frag->gpa = gpa;
5688 frag->data = val;
5689 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005690 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005691}
5692
Xiubo Li52eb5a62015-03-13 17:39:45 +08005693static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5694 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005695 void *val, unsigned int bytes,
5696 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005697 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005698{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005699 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005700 gpa_t gpa;
5701 int rc;
5702
5703 if (ops->read_write_prepare &&
5704 ops->read_write_prepare(vcpu, val, bytes))
5705 return X86EMUL_CONTINUE;
5706
5707 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005708
Carsten Ottebbd9b642007-10-30 18:44:21 +01005709 /* Crossing a page boundary? */
5710 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005711 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005712
5713 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005714 rc = emulator_read_write_onepage(addr, val, now, exception,
5715 vcpu, ops);
5716
Carsten Ottebbd9b642007-10-30 18:44:21 +01005717 if (rc != X86EMUL_CONTINUE)
5718 return rc;
5719 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005720 if (ctxt->mode != X86EMUL_MODE_PROT64)
5721 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005722 val += now;
5723 bytes -= now;
5724 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005725
Avi Kivityf78146b2012-04-18 19:22:47 +03005726 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5727 vcpu, ops);
5728 if (rc != X86EMUL_CONTINUE)
5729 return rc;
5730
5731 if (!vcpu->mmio_nr_fragments)
5732 return rc;
5733
5734 gpa = vcpu->mmio_fragments[0].gpa;
5735
5736 vcpu->mmio_needed = 1;
5737 vcpu->mmio_cur_fragment = 0;
5738
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005739 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005740 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5741 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5742 vcpu->run->mmio.phys_addr = gpa;
5743
5744 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005745}
5746
5747static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5748 unsigned long addr,
5749 void *val,
5750 unsigned int bytes,
5751 struct x86_exception *exception)
5752{
5753 return emulator_read_write(ctxt, addr, val, bytes,
5754 exception, &read_emultor);
5755}
5756
Xiubo Li52eb5a62015-03-13 17:39:45 +08005757static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005758 unsigned long addr,
5759 const void *val,
5760 unsigned int bytes,
5761 struct x86_exception *exception)
5762{
5763 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5764 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005765}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005766
Avi Kivitydaea3e72010-03-15 13:59:54 +02005767#define CMPXCHG_TYPE(t, ptr, old, new) \
5768 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5769
5770#ifdef CONFIG_X86_64
5771# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5772#else
5773# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005774 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005775#endif
5776
Avi Kivity0f65dd72011-04-20 13:37:53 +03005777static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5778 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005779 const void *old,
5780 const void *new,
5781 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005782 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005783{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005784 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005785 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005786 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005787 char *kaddr;
5788 bool exchanged;
5789
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005790 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005791 if (bytes > 8 || (bytes & (bytes - 1)))
5792 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005793
Avi Kivitydaea3e72010-03-15 13:59:54 +02005794 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005795
Avi Kivitydaea3e72010-03-15 13:59:54 +02005796 if (gpa == UNMAPPED_GVA ||
5797 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5798 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005799
Avi Kivitydaea3e72010-03-15 13:59:54 +02005800 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5801 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005802
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005803 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005804 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005805
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005806 kaddr = map.hva + offset_in_page(gpa);
5807
Avi Kivitydaea3e72010-03-15 13:59:54 +02005808 switch (bytes) {
5809 case 1:
5810 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5811 break;
5812 case 2:
5813 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5814 break;
5815 case 4:
5816 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5817 break;
5818 case 8:
5819 exchanged = CMPXCHG64(kaddr, old, new);
5820 break;
5821 default:
5822 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005823 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005824
5825 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005826
5827 if (!exchanged)
5828 return X86EMUL_CMPXCHG_FAILED;
5829
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005830 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005831
5832 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005833
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005834emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005835 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005836
Avi Kivity0f65dd72011-04-20 13:37:53 +03005837 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005838}
5839
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005840static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5841{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005842 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005843
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005844 for (i = 0; i < vcpu->arch.pio.count; i++) {
5845 if (vcpu->arch.pio.in)
5846 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5847 vcpu->arch.pio.size, pd);
5848 else
5849 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5850 vcpu->arch.pio.port, vcpu->arch.pio.size,
5851 pd);
5852 if (r)
5853 break;
5854 pd += vcpu->arch.pio.size;
5855 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005856 return r;
5857}
5858
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005859static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5860 unsigned short port, void *val,
5861 unsigned int count, bool in)
5862{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005863 vcpu->arch.pio.port = port;
5864 vcpu->arch.pio.in = in;
5865 vcpu->arch.pio.count = count;
5866 vcpu->arch.pio.size = size;
5867
5868 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5869 vcpu->arch.pio.count = 0;
5870 return 1;
5871 }
5872
5873 vcpu->run->exit_reason = KVM_EXIT_IO;
5874 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5875 vcpu->run->io.size = size;
5876 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5877 vcpu->run->io.count = count;
5878 vcpu->run->io.port = port;
5879
5880 return 0;
5881}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005882
Avi Kivityca1d4a92011-04-20 13:37:53 +03005883static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5884 int size, unsigned short port, void *val,
5885 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005886{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005887 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005888 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005889
Gleb Natapov79729952010-03-18 15:20:24 +02005890 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005891 goto data_avail;
5892
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005893 memset(vcpu->arch.pio_data, 0, size * count);
5894
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005895 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5896 if (ret) {
5897data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005898 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005899 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005900 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005901 return 1;
5902 }
5903
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005904 return 0;
5905}
5906
Avi Kivityca1d4a92011-04-20 13:37:53 +03005907static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5908 int size, unsigned short port,
5909 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005910{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005911 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5912
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005913 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005914 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005915 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005916}
5917
Carsten Ottebbd9b642007-10-30 18:44:21 +01005918static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5919{
5920 return kvm_x86_ops->get_segment_base(vcpu, seg);
5921}
5922
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005923static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005924{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005925 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005926}
5927
Jiang Biaoae6a2372016-11-07 08:54:51 +08005928static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005929{
5930 if (!need_emulate_wbinvd(vcpu))
5931 return X86EMUL_CONTINUE;
5932
5933 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005934 int cpu = get_cpu();
5935
5936 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005937 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5938 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005939 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005940 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005941 } else
5942 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005943 return X86EMUL_CONTINUE;
5944}
Joel Schopp5cb56052015-03-02 13:43:31 -06005945
5946int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
5947{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005948 kvm_emulate_wbinvd_noskip(vcpu);
5949 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06005950}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005951EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
5952
Joel Schopp5cb56052015-03-02 13:43:31 -06005953
5954
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005955static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
5956{
Joel Schopp5cb56052015-03-02 13:43:31 -06005957 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005958}
5959
Xiubo Li52eb5a62015-03-13 17:39:45 +08005960static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
5961 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005962{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005963 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005964}
5965
Xiubo Li52eb5a62015-03-13 17:39:45 +08005966static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
5967 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005968{
Gleb Natapov338dbc92010-04-28 19:15:32 +03005969
Avi Kivity717746e2011-04-20 13:37:53 +03005970 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005971}
5972
Gleb Natapov52a46612010-03-18 15:20:03 +02005973static u64 mk_cr_64(u64 curr_cr, u32 new_val)
5974{
5975 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
5976}
5977
Avi Kivity717746e2011-04-20 13:37:53 +03005978static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02005979{
Avi Kivity717746e2011-04-20 13:37:53 +03005980 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02005981 unsigned long value;
5982
5983 switch (cr) {
5984 case 0:
5985 value = kvm_read_cr0(vcpu);
5986 break;
5987 case 2:
5988 value = vcpu->arch.cr2;
5989 break;
5990 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005991 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02005992 break;
5993 case 4:
5994 value = kvm_read_cr4(vcpu);
5995 break;
5996 case 8:
5997 value = kvm_get_cr8(vcpu);
5998 break;
5999 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006000 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006001 return 0;
6002 }
6003
6004 return value;
6005}
6006
Avi Kivity717746e2011-04-20 13:37:53 +03006007static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006008{
Avi Kivity717746e2011-04-20 13:37:53 +03006009 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006010 int res = 0;
6011
Gleb Natapov52a46612010-03-18 15:20:03 +02006012 switch (cr) {
6013 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006014 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006015 break;
6016 case 2:
6017 vcpu->arch.cr2 = val;
6018 break;
6019 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006020 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006021 break;
6022 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006023 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006024 break;
6025 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006026 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006027 break;
6028 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006029 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006030 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006031 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006032
6033 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006034}
6035
Avi Kivity717746e2011-04-20 13:37:53 +03006036static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006037{
Avi Kivity717746e2011-04-20 13:37:53 +03006038 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006039}
6040
Avi Kivity4bff1e862011-04-20 13:37:53 +03006041static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006042{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006043 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006044}
6045
Avi Kivity4bff1e862011-04-20 13:37:53 +03006046static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006047{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006048 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006049}
6050
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006051static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6052{
6053 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6054}
6055
6056static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6057{
6058 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6059}
6060
Avi Kivity4bff1e862011-04-20 13:37:53 +03006061static unsigned long emulator_get_cached_segment_base(
6062 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006063{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006064 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006065}
6066
Avi Kivity1aa36612011-04-27 13:20:30 +03006067static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6068 struct desc_struct *desc, u32 *base3,
6069 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006070{
6071 struct kvm_segment var;
6072
Avi Kivity4bff1e862011-04-20 13:37:53 +03006073 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006074 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006075
Gleb Natapov378a8b02013-01-21 15:36:48 +02006076 if (var.unusable) {
6077 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006078 if (base3)
6079 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006080 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006081 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006082
6083 if (var.g)
6084 var.limit >>= 12;
6085 set_desc_limit(desc, var.limit);
6086 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006087#ifdef CONFIG_X86_64
6088 if (base3)
6089 *base3 = var.base >> 32;
6090#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006091 desc->type = var.type;
6092 desc->s = var.s;
6093 desc->dpl = var.dpl;
6094 desc->p = var.present;
6095 desc->avl = var.avl;
6096 desc->l = var.l;
6097 desc->d = var.db;
6098 desc->g = var.g;
6099
6100 return true;
6101}
6102
Avi Kivity1aa36612011-04-27 13:20:30 +03006103static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6104 struct desc_struct *desc, u32 base3,
6105 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006106{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006107 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006108 struct kvm_segment var;
6109
Avi Kivity1aa36612011-04-27 13:20:30 +03006110 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006111 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006112#ifdef CONFIG_X86_64
6113 var.base |= ((u64)base3) << 32;
6114#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006115 var.limit = get_desc_limit(desc);
6116 if (desc->g)
6117 var.limit = (var.limit << 12) | 0xfff;
6118 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006119 var.dpl = desc->dpl;
6120 var.db = desc->d;
6121 var.s = desc->s;
6122 var.l = desc->l;
6123 var.g = desc->g;
6124 var.avl = desc->avl;
6125 var.present = desc->p;
6126 var.unusable = !var.present;
6127 var.padding = 0;
6128
6129 kvm_set_segment(vcpu, &var, seg);
6130 return;
6131}
6132
Avi Kivity717746e2011-04-20 13:37:53 +03006133static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6134 u32 msr_index, u64 *pdata)
6135{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006136 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006137}
6138
6139static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6140 u32 msr_index, u64 data)
6141{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006142 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006143}
6144
Paolo Bonzini64d60672015-05-07 11:36:11 +02006145static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6146{
6147 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6148
6149 return vcpu->arch.smbase;
6150}
6151
6152static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6153{
6154 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6155
6156 vcpu->arch.smbase = smbase;
6157}
6158
Nadav Amit67f4d422014-06-02 18:34:09 +03006159static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6160 u32 pmc)
6161{
Like Xu98ff80f2019-10-27 18:52:40 +08006162 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006163}
6164
Avi Kivity222d21a2011-11-10 14:57:30 +02006165static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6166 u32 pmc, u64 *pdata)
6167{
Wei Huangc6702c92015-06-19 13:44:45 +02006168 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006169}
6170
Avi Kivity6c3287f2011-04-20 15:43:05 +03006171static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6172{
6173 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6174}
6175
Avi Kivity29535382011-04-20 13:37:53 +03006176static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006177 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006178 enum x86_intercept_stage stage)
6179{
Avi Kivity29535382011-04-20 13:37:53 +03006180 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006181}
6182
Yu Zhange911eb32017-08-24 20:27:52 +08006183static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6184 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006185{
Yu Zhange911eb32017-08-24 20:27:52 +08006186 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006187}
6188
Avi Kivitydd856ef2012-08-27 23:46:17 +03006189static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6190{
6191 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6192}
6193
6194static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6195{
6196 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6197}
6198
Nadav Amit801806d2015-01-26 09:32:23 +02006199static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6200{
6201 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6202}
6203
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006204static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6205{
6206 return emul_to_vcpu(ctxt)->arch.hflags;
6207}
6208
6209static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6210{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006211 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006212}
6213
Sean Christophersoned193212019-04-02 08:03:09 -07006214static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6215 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006216{
Sean Christophersoned193212019-04-02 08:03:09 -07006217 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006218}
6219
Sean Christophersonc5833c72019-04-02 08:03:10 -07006220static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6221{
6222 kvm_smm_changed(emul_to_vcpu(ctxt));
6223}
6224
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006225static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6226{
6227 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6228}
6229
Mathias Krause0225fb52012-08-30 01:30:16 +02006230static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006231 .read_gpr = emulator_read_gpr,
6232 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006233 .read_std = emulator_read_std,
6234 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006235 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006236 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006237 .read_emulated = emulator_read_emulated,
6238 .write_emulated = emulator_write_emulated,
6239 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006240 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006241 .pio_in_emulated = emulator_pio_in_emulated,
6242 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006243 .get_segment = emulator_get_segment,
6244 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006245 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006246 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006247 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006248 .set_gdt = emulator_set_gdt,
6249 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006250 .get_cr = emulator_get_cr,
6251 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006252 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006253 .get_dr = emulator_get_dr,
6254 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006255 .get_smbase = emulator_get_smbase,
6256 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006257 .set_msr = emulator_set_msr,
6258 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006259 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006260 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006261 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006262 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006263 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006264 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006265 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02006266 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006267 .get_hflags = emulator_get_hflags,
6268 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006269 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006270 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006271 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006272};
6273
Gleb Natapov95cb2292010-04-28 19:15:43 +03006274static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6275{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006276 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006277 /*
6278 * an sti; sti; sequence only disable interrupts for the first
6279 * instruction. So, if the last instruction, be it emulated or
6280 * not, left the system with the INT_STI flag enabled, it
6281 * means that the last instruction is an sti. We should not
6282 * leave the flag on in this case. The same goes for mov ss
6283 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006284 if (int_shadow & mask)
6285 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006286 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006287 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006288 if (!mask)
6289 kvm_make_request(KVM_REQ_EVENT, vcpu);
6290 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006291}
6292
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006293static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006294{
6295 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006296 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006297 return kvm_propagate_fault(vcpu, &ctxt->exception);
6298
6299 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006300 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6301 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006302 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006303 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006304 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006305}
6306
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006307static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6308{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006309 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006310 int cs_db, cs_l;
6311
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006312 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6313
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006314 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006315 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6316
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006317 ctxt->eip = kvm_rip_read(vcpu);
6318 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6319 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006320 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006321 cs_db ? X86EMUL_MODE_PROT32 :
6322 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006323 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006324 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6325 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006326
Avi Kivitydd856ef2012-08-27 23:46:17 +03006327 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006328 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006329}
6330
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006331void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006332{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006333 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006334 int ret;
6335
6336 init_emulate_ctxt(vcpu);
6337
Avi Kivity9dac77f2011-06-01 15:34:25 +03006338 ctxt->op_bytes = 2;
6339 ctxt->ad_bytes = 2;
6340 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006341 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006342
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006343 if (ret != X86EMUL_CONTINUE) {
6344 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6345 } else {
6346 ctxt->eip = ctxt->_eip;
6347 kvm_rip_write(vcpu, ctxt->eip);
6348 kvm_set_rflags(vcpu, ctxt->eflags);
6349 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006350}
6351EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6352
Liran Alone2366172018-03-12 13:12:49 +02006353static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006354{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006355 ++vcpu->stat.insn_emulation_fail;
6356 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006357
Sean Christopherson42cbf062019-08-27 14:40:31 -07006358 if (emulation_type & EMULTYPE_VMWARE_GP) {
6359 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006360 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006361 }
Liran Alone2366172018-03-12 13:12:49 +02006362
Sean Christopherson738fece2019-08-27 14:40:34 -07006363 if (emulation_type & EMULTYPE_SKIP) {
6364 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6365 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6366 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006367 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006368 }
6369
Sean Christopherson22da61c2019-08-27 14:40:28 -07006370 kvm_queue_exception(vcpu, UD_VECTOR);
6371
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006372 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006373 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6374 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6375 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006376 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006377 }
Liran Alone2366172018-03-12 13:12:49 +02006378
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006379 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006380}
6381
Sean Christopherson736c2912019-12-06 15:57:14 -08006382static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006383 bool write_fault_to_shadow_pgtable,
6384 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006385{
Sean Christopherson736c2912019-12-06 15:57:14 -08006386 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006387 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006388
Sean Christopherson384bf222018-08-23 13:56:49 -07006389 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006390 return false;
6391
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006392 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6393 return false;
6394
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006395 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006396 /*
6397 * Write permission should be allowed since only
6398 * write access need to be emulated.
6399 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006400 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006401
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006402 /*
6403 * If the mapping is invalid in guest, let cpu retry
6404 * it to generate fault.
6405 */
6406 if (gpa == UNMAPPED_GVA)
6407 return true;
6408 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006409
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006410 /*
6411 * Do not retry the unhandleable instruction if it faults on the
6412 * readonly host memory, otherwise it will goto a infinite loop:
6413 * retry instruction -> write #PF -> emulation fail -> retry
6414 * instruction -> ...
6415 */
6416 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006417
6418 /*
6419 * If the instruction failed on the error pfn, it can not be fixed,
6420 * report the error to userspace.
6421 */
6422 if (is_error_noslot_pfn(pfn))
6423 return false;
6424
6425 kvm_release_pfn_clean(pfn);
6426
6427 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006428 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006429 unsigned int indirect_shadow_pages;
6430
6431 spin_lock(&vcpu->kvm->mmu_lock);
6432 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6433 spin_unlock(&vcpu->kvm->mmu_lock);
6434
6435 if (indirect_shadow_pages)
6436 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6437
Gleb Natapova6f177e2010-07-08 12:41:12 +03006438 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006439 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006440
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006441 /*
6442 * if emulation was due to access to shadowed page table
6443 * and it failed try to unshadow page and re-enter the
6444 * guest to let CPU execute the instruction.
6445 */
6446 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006447
6448 /*
6449 * If the access faults on its page table, it can not
6450 * be fixed by unprotecting shadow page and it should
6451 * be reported to userspace.
6452 */
6453 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006454}
6455
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006456static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006457 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006458{
6459 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006460 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006461
6462 last_retry_eip = vcpu->arch.last_retry_eip;
6463 last_retry_addr = vcpu->arch.last_retry_addr;
6464
6465 /*
6466 * If the emulation is caused by #PF and it is non-page_table
6467 * writing instruction, it means the VM-EXIT is caused by shadow
6468 * page protected, we can zap the shadow page and retry this
6469 * instruction directly.
6470 *
6471 * Note: if the guest uses a non-page-table modifying instruction
6472 * on the PDE that points to the instruction, then we will unmap
6473 * the instruction and go to an infinite loop. So, we cache the
6474 * last retried eip and the last fault address, if we meet the eip
6475 * and the address again, we can break out of the potential infinite
6476 * loop.
6477 */
6478 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6479
Sean Christopherson384bf222018-08-23 13:56:49 -07006480 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006481 return false;
6482
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006483 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6484 return false;
6485
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006486 if (x86_page_table_writing_insn(ctxt))
6487 return false;
6488
Sean Christopherson736c2912019-12-06 15:57:14 -08006489 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006490 return false;
6491
6492 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006493 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006494
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006495 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006496 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006497
Xiao Guangrong22368022013-01-13 23:44:12 +08006498 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006499
6500 return true;
6501}
6502
Gleb Natapov716d51a2012-09-03 15:24:26 +03006503static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6504static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6505
Paolo Bonzini64d60672015-05-07 11:36:11 +02006506static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006507{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006508 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006509 /* This is a good place to trace that we are exiting SMM. */
6510 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6511
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006512 /* Process a latched INIT or SMI, if any. */
6513 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006514 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006515
6516 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006517}
6518
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006519static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6520 unsigned long *db)
6521{
6522 u32 dr6 = 0;
6523 int i;
6524 u32 enable, rwlen;
6525
6526 enable = dr7;
6527 rwlen = dr7 >> 16;
6528 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6529 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6530 dr6 |= (1 << i);
6531 return dr6;
6532}
6533
Sean Christopherson120c2c42019-08-27 14:40:29 -07006534static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006535{
6536 struct kvm_run *kvm_run = vcpu->run;
6537
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006538 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6539 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6540 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6541 kvm_run->debug.arch.exception = DB_VECTOR;
6542 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006543 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006544 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006545 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006546 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006547}
6548
Kyle Huey6affcbe2016-11-29 12:40:40 -08006549int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6550{
6551 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006552 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006553
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006554 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006555 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006556 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006557
6558 /*
6559 * rflags is the old, "raw" value of the flags. The new value has
6560 * not been saved yet.
6561 *
6562 * This is correct even for TF set by the guest, because "the
6563 * processor will not generate this exception after the instruction
6564 * that sets the TF flag".
6565 */
6566 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006567 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006568 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006569}
6570EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6571
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006572static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6573{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006574 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6575 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006576 struct kvm_run *kvm_run = vcpu->run;
6577 unsigned long eip = kvm_get_linear_rip(vcpu);
6578 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006579 vcpu->arch.guest_debug_dr7,
6580 vcpu->arch.eff_db);
6581
6582 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006583 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006584 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006585 kvm_run->debug.arch.exception = DB_VECTOR;
6586 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006587 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006588 return true;
6589 }
6590 }
6591
Nadav Amit4161a562014-07-17 01:19:31 +03006592 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6593 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006594 unsigned long eip = kvm_get_linear_rip(vcpu);
6595 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006596 vcpu->arch.dr7,
6597 vcpu->arch.db);
6598
6599 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006600 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006601 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006602 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006603 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006604 return true;
6605 }
6606 }
6607
6608 return false;
6609}
6610
Liran Alon04789b62018-03-12 13:12:50 +02006611static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6612{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006613 switch (ctxt->opcode_len) {
6614 case 1:
6615 switch (ctxt->b) {
6616 case 0xe4: /* IN */
6617 case 0xe5:
6618 case 0xec:
6619 case 0xed:
6620 case 0xe6: /* OUT */
6621 case 0xe7:
6622 case 0xee:
6623 case 0xef:
6624 case 0x6c: /* INS */
6625 case 0x6d:
6626 case 0x6e: /* OUTS */
6627 case 0x6f:
6628 return true;
6629 }
6630 break;
6631 case 2:
6632 switch (ctxt->b) {
6633 case 0x33: /* RDPMC */
6634 return true;
6635 }
6636 break;
Liran Alon04789b62018-03-12 13:12:50 +02006637 }
6638
6639 return false;
6640}
6641
Sean Christopherson736c2912019-12-06 15:57:14 -08006642int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6643 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006644{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006645 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006646 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006647 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006648 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006649
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006650 vcpu->arch.l1tf_flush_l1d = true;
6651
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006652 /*
6653 * Clear write_fault_to_shadow_pgtable here to ensure it is
6654 * never reused.
6655 */
6656 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006657 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006658
Sheng Yang571008d2008-01-02 14:49:22 +08006659 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006660 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006661
6662 /*
6663 * We will reenter on the same instruction since
6664 * we do not set complete_userspace_io. This does not
6665 * handle watchpoints yet, those would be handled in
6666 * the emulate_ops.
6667 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006668 if (!(emulation_type & EMULTYPE_SKIP) &&
6669 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006670 return r;
6671
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006672 ctxt->interruptibility = 0;
6673 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006674 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006675 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006676
Borislav Petkovb51e9742013-09-22 16:44:52 +02006677 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006678
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006679 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006680
Avi Kivitye46479f2010-04-11 13:05:16 +03006681 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006682 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006683 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006684 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006685 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6686 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006687 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006688 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006689 if (reexecute_instruction(vcpu, cr2_or_gpa,
6690 write_fault_to_spt,
6691 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006692 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006693 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006694 /*
6695 * #UD should result in just EMULATION_FAILED, and trap-like
6696 * exception should not be encountered during decode.
6697 */
6698 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6699 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006700 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006701 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006702 }
Liran Alone2366172018-03-12 13:12:49 +02006703 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006704 }
6705 }
6706
Sean Christopherson42cbf062019-08-27 14:40:31 -07006707 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6708 !is_vmware_backdoor_opcode(ctxt)) {
6709 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006710 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006711 }
Liran Alon04789b62018-03-12 13:12:50 +02006712
Sean Christopherson1957aa62019-08-27 14:40:39 -07006713 /*
6714 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6715 * for kvm_skip_emulated_instruction(). The caller is responsible for
6716 * updating interruptibility state and injecting single-step #DBs.
6717 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006718 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006719 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006720 if (ctxt->eflags & X86_EFLAGS_RF)
6721 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006722 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006723 }
6724
Sean Christopherson736c2912019-12-06 15:57:14 -08006725 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006726 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006727
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006728 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006729 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006730 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6731 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006732 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006733 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006734
Gleb Natapov5cd21912010-03-18 15:20:26 +02006735restart:
Tom Lendacky0f89b202016-12-14 14:59:23 -05006736 /* Save the faulting GPA (cr2) in the address field */
Sean Christopherson736c2912019-12-06 15:57:14 -08006737 ctxt->exception.address = cr2_or_gpa;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006738
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006739 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006740
Joerg Roedel775fde82011-04-04 12:39:24 +02006741 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006742 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006743
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006744 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006745 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006746 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006747 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006748
Liran Alone2366172018-03-12 13:12:49 +02006749 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006750 }
6751
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006752 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006753 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006754 if (inject_emulated_exception(vcpu))
6755 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006756 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006757 if (!vcpu->arch.pio.in) {
6758 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006759 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006760 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006761 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006762 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6763 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006764 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006765 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006766 ++vcpu->stat.mmio_exits;
6767
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006768 if (!vcpu->mmio_is_write)
6769 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006770 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006771 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006772 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006773 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006774 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006775 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006776
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006777 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006778 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006779 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006780 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006781 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006782 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6783 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006784 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006785 r = kvm_vcpu_do_singlestep(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006786 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006787 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006788
6789 /*
6790 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6791 * do nothing, and it will be requested again as soon as
6792 * the shadow expires. But we still need to check here,
6793 * because POPF has no interrupt shadow.
6794 */
6795 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6796 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006797 } else
6798 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006799
Gleb Natapove85d28f2010-07-29 15:11:52 +03006800 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006801}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006802
6803int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6804{
6805 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6806}
6807EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6808
6809int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6810 void *insn, int insn_len)
6811{
6812 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6813}
6814EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006815
Sean Christopherson8764ed52019-04-29 07:04:15 -07006816static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6817{
6818 vcpu->arch.pio.count = 0;
6819 return 1;
6820}
6821
Sean Christopherson45def772019-03-11 20:01:05 -07006822static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6823{
6824 vcpu->arch.pio.count = 0;
6825
6826 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6827 return 1;
6828
6829 return kvm_skip_emulated_instruction(vcpu);
6830}
6831
Sean Christophersondca7f122018-03-08 08:57:27 -08006832static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6833 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006834{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006835 unsigned long val = kvm_rax_read(vcpu);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006836 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
6837 size, port, &val, 1);
Sean Christopherson8764ed52019-04-29 07:04:15 -07006838 if (ret)
6839 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006840
Sean Christopherson8764ed52019-04-29 07:04:15 -07006841 /*
6842 * Workaround userspace that relies on old KVM behavior of %rip being
6843 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6844 */
6845 if (port == 0x7e &&
6846 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6847 vcpu->arch.complete_userspace_io =
6848 complete_fast_pio_out_port_0x7e;
6849 kvm_skip_emulated_instruction(vcpu);
6850 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006851 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6852 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6853 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006854 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006855}
Carsten Ottede7d7892007-10-30 18:44:25 +01006856
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006857static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6858{
6859 unsigned long val;
6860
6861 /* We should only ever be called with arch.pio.count equal to 1 */
6862 BUG_ON(vcpu->arch.pio.count != 1);
6863
Sean Christopherson45def772019-03-11 20:01:05 -07006864 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6865 vcpu->arch.pio.count = 0;
6866 return 1;
6867 }
6868
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006869 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006870 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006871
6872 /*
6873 * Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform
6874 * the copy and tracing
6875 */
6876 emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size,
6877 vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006878 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006879
Sean Christopherson45def772019-03-11 20:01:05 -07006880 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006881}
6882
Sean Christophersondca7f122018-03-08 08:57:27 -08006883static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6884 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006885{
6886 unsigned long val;
6887 int ret;
6888
6889 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006890 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006891
6892 ret = emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, size, port,
6893 &val, 1);
6894 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07006895 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006896 return ret;
6897 }
6898
Sean Christopherson45def772019-03-11 20:01:05 -07006899 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006900 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
6901
6902 return 0;
6903}
Sean Christophersondca7f122018-03-08 08:57:27 -08006904
6905int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
6906{
Sean Christopherson45def772019-03-11 20:01:05 -07006907 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08006908
Sean Christophersondca7f122018-03-08 08:57:27 -08006909 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07006910 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08006911 else
Sean Christopherson45def772019-03-11 20:01:05 -07006912 ret = kvm_fast_pio_out(vcpu, size, port);
6913 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08006914}
6915EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006916
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006917static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006918{
Tejun Heo0a3aee02010-12-18 16:28:55 +01006919 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006920 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006921}
6922
6923static void tsc_khz_changed(void *data)
6924{
6925 struct cpufreq_freqs *freq = data;
6926 unsigned long khz = 0;
6927
6928 if (data)
6929 khz = freq->new;
6930 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6931 khz = cpufreq_quick_get(raw_smp_processor_id());
6932 if (!khz)
6933 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01006934 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006935}
6936
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006937#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006938static void kvm_hyperv_tsc_notifier(void)
6939{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006940 struct kvm *kvm;
6941 struct kvm_vcpu *vcpu;
6942 int cpu;
6943
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006944 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006945 list_for_each_entry(kvm, &vm_list, vm_list)
6946 kvm_make_mclock_inprogress_request(kvm);
6947
6948 hyperv_stop_tsc_emulation();
6949
6950 /* TSC frequency always matches when on Hyper-V */
6951 for_each_present_cpu(cpu)
6952 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
6953 kvm_max_guest_tsc_khz = tsc_khz;
6954
6955 list_for_each_entry(kvm, &vm_list, vm_list) {
6956 struct kvm_arch *ka = &kvm->arch;
6957
6958 spin_lock(&ka->pvclock_gtod_sync_lock);
6959
6960 pvclock_update_vm_gtod_copy(kvm);
6961
6962 kvm_for_each_vcpu(cpu, vcpu, kvm)
6963 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
6964
6965 kvm_for_each_vcpu(cpu, vcpu, kvm)
6966 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
6967
6968 spin_unlock(&ka->pvclock_gtod_sync_lock);
6969 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006970 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006971}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006972#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006973
Viresh Kumardf240142019-04-29 15:03:58 +05306974static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006975{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006976 struct kvm *kvm;
6977 struct kvm_vcpu *vcpu;
6978 int i, send_ipi = 0;
6979
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006980 /*
6981 * We allow guests to temporarily run on slowing clocks,
6982 * provided we notify them after, or to run on accelerating
6983 * clocks, provided we notify them before. Thus time never
6984 * goes backwards.
6985 *
6986 * However, we have a problem. We can't atomically update
6987 * the frequency of a given CPU from this function; it is
6988 * merely a notifier, which can be called from any CPU.
6989 * Changing the TSC frequency at arbitrary points in time
6990 * requires a recomputation of local variables related to
6991 * the TSC for each VCPU. We must flag these local variables
6992 * to be updated and be sure the update takes place with the
6993 * new frequency before any guests proceed.
6994 *
6995 * Unfortunately, the combination of hotplug CPU and frequency
6996 * change creates an intractable locking scenario; the order
6997 * of when these callouts happen is undefined with respect to
6998 * CPU hotplug, and they can race with each other. As such,
6999 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7000 * undefined; you can actually have a CPU frequency change take
7001 * place in between the computation of X and the setting of the
7002 * variable. To protect against this problem, all updates of
7003 * the per_cpu tsc_khz variable are done in an interrupt
7004 * protected IPI, and all callers wishing to update the value
7005 * must wait for a synchronous IPI to complete (which is trivial
7006 * if the caller is on the CPU already). This establishes the
7007 * necessary total order on variable updates.
7008 *
7009 * Note that because a guest time update may take place
7010 * anytime after the setting of the VCPU's request bit, the
7011 * correct TSC value must be set before the request. However,
7012 * to ensure the update actually makes it to any guest which
7013 * starts running in hardware virtualization between the set
7014 * and the acquisition of the spinlock, we must also ping the
7015 * CPU after setting the request bit.
7016 *
7017 */
7018
Viresh Kumardf240142019-04-29 15:03:58 +05307019 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007020
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007021 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007022 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007023 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307024 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007025 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007026 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007027 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007028 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007029 }
7030 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007031 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007032
7033 if (freq->old < freq->new && send_ipi) {
7034 /*
7035 * We upscale the frequency. Must make the guest
7036 * doesn't see old kvmclock values while running with
7037 * the new frequency, otherwise we risk the guest sees
7038 * time go backwards.
7039 *
7040 * In case we update the frequency for another cpu
7041 * (which might be in guest context) send an interrupt
7042 * to kick the cpu out of guest context. Next time
7043 * guest context is entered kvmclock will be updated,
7044 * so the guest will not see stale values.
7045 */
Viresh Kumardf240142019-04-29 15:03:58 +05307046 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007047 }
Viresh Kumardf240142019-04-29 15:03:58 +05307048}
7049
7050static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7051 void *data)
7052{
7053 struct cpufreq_freqs *freq = data;
7054 int cpu;
7055
7056 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7057 return 0;
7058 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7059 return 0;
7060
7061 for_each_cpu(cpu, freq->policy->cpus)
7062 __kvmclock_cpufreq_notifier(freq, cpu);
7063
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007064 return 0;
7065}
7066
7067static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007068 .notifier_call = kvmclock_cpufreq_notifier
7069};
7070
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007071static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007072{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007073 tsc_khz_changed(NULL);
7074 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007075}
7076
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007077static void kvm_timer_init(void)
7078{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007079 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307080
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007081 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007082#ifdef CONFIG_CPU_FREQ
7083 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007084 int cpu;
7085
Zachary Amsdenc2855452010-09-18 14:38:15 -10007086 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007087 cpu = get_cpu();
7088 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007089 if (policy.cpuinfo.max_freq)
7090 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007091 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007092#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007093 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7094 CPUFREQ_TRANSITION_NOTIFIER);
7095 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307096
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007097 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007098 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007099}
7100
Andi Kleendd60d212017-07-25 17:20:32 -07007101DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7102EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007103
Gleb Natapovf5132b02011-11-10 14:57:22 +02007104int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007105{
Alex,Shi086c9852011-10-20 15:34:01 +08007106 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007107}
7108
7109static int kvm_is_user_mode(void)
7110{
7111 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007112
Alex,Shi086c9852011-10-20 15:34:01 +08007113 if (__this_cpu_read(current_vcpu))
7114 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007115
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007116 return user_mode != 0;
7117}
7118
7119static unsigned long kvm_get_guest_ip(void)
7120{
7121 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007122
Alex,Shi086c9852011-10-20 15:34:01 +08007123 if (__this_cpu_read(current_vcpu))
7124 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007125
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007126 return ip;
7127}
7128
Luwei Kang8479e042019-02-18 19:26:07 -05007129static void kvm_handle_intel_pt_intr(void)
7130{
7131 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7132
7133 kvm_make_request(KVM_REQ_PMI, vcpu);
7134 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7135 (unsigned long *)&vcpu->arch.pmu.global_status);
7136}
7137
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007138static struct perf_guest_info_callbacks kvm_guest_cbs = {
7139 .is_in_guest = kvm_is_in_guest,
7140 .is_user_mode = kvm_is_user_mode,
7141 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007142 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007143};
7144
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007145#ifdef CONFIG_X86_64
7146static void pvclock_gtod_update_fn(struct work_struct *work)
7147{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007148 struct kvm *kvm;
7149
7150 struct kvm_vcpu *vcpu;
7151 int i;
7152
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007153 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007154 list_for_each_entry(kvm, &vm_list, vm_list)
7155 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007156 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007157 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007158 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007159}
7160
7161static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7162
7163/*
7164 * Notification about pvclock gtod data update.
7165 */
7166static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7167 void *priv)
7168{
7169 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7170 struct timekeeper *tk = priv;
7171
7172 update_pvclock_gtod(tk);
7173
7174 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007175 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007176 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007177 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007178 atomic_read(&kvm_guest_has_master_clock) != 0)
7179 queue_work(system_long_wq, &pvclock_gtod_work);
7180
7181 return 0;
7182}
7183
7184static struct notifier_block pvclock_gtod_notifier = {
7185 .notifier_call = pvclock_gtod_notify,
7186};
7187#endif
7188
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007189int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007190{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007191 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007192 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007193
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007194 if (kvm_x86_ops) {
7195 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007196 r = -EEXIST;
7197 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007198 }
7199
7200 if (!ops->cpu_has_kvm_support()) {
7201 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007202 r = -EOPNOTSUPP;
7203 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007204 }
7205 if (ops->disabled_by_bios()) {
7206 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007207 r = -EOPNOTSUPP;
7208 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007209 }
7210
Marc Orrb666a4b2018-11-06 14:53:56 -08007211 /*
7212 * KVM explicitly assumes that the guest has an FPU and
7213 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7214 * vCPU's FPU state as a fxregs_state struct.
7215 */
7216 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7217 printk(KERN_ERR "kvm: inadequate fpu\n");
7218 r = -EOPNOTSUPP;
7219 goto out;
7220 }
7221
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007222 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007223 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007224 __alignof__(struct fpu), SLAB_ACCOUNT,
7225 NULL);
7226 if (!x86_fpu_cache) {
7227 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7228 goto out;
7229 }
7230
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007231 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7232 if (!shared_msrs) {
7233 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Marc Orrb666a4b2018-11-06 14:53:56 -08007234 goto out_free_x86_fpu_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007235 }
7236
Avi Kivity97db56c2008-01-13 13:23:56 +02007237 r = kvm_mmu_module_init();
7238 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007239 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007240
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007241 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007242
Sheng Yang7b523452008-04-25 21:13:50 +08007243 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007244 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007245 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007246 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007247
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007248 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7249
Borislav Petkovd366bf72016-04-04 22:25:02 +02007250 if (boot_cpu_has(X86_FEATURE_XSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +08007251 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
7252
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007253 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007254 if (pi_inject_timer == -1)
7255 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007256#ifdef CONFIG_X86_64
7257 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007258
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007259 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007260 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007261#endif
7262
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007263 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007264
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007265out_free_percpu:
7266 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007267out_free_x86_fpu_cache:
7268 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007269out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007270 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007271}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007272
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007273void kvm_arch_exit(void)
7274{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007275#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007276 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007277 clear_hv_tscchange_cb();
7278#endif
David Matlackcef84c32016-12-16 14:30:36 -08007279 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007280 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7281
Jan Kiszka888d2562009-04-17 19:24:58 +02007282 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7283 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7284 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007285 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007286#ifdef CONFIG_X86_64
7287 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7288#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007289 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007290 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007291 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007292 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007293}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007294
Joel Schopp5cb56052015-03-02 13:43:31 -06007295int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007296{
7297 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007298 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007299 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007300 return 1;
7301 } else {
7302 vcpu->run->exit_reason = KVM_EXIT_HLT;
7303 return 0;
7304 }
7305}
Joel Schopp5cb56052015-03-02 13:43:31 -06007306EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7307
7308int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7309{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007310 int ret = kvm_skip_emulated_instruction(vcpu);
7311 /*
7312 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7313 * KVM_EXIT_DEBUG here.
7314 */
7315 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007316}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007317EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7318
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007319#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007320static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7321 unsigned long clock_type)
7322{
7323 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007324 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007325 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007326 int ret;
7327
7328 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7329 return -KVM_EOPNOTSUPP;
7330
7331 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7332 return -KVM_EOPNOTSUPP;
7333
7334 clock_pairing.sec = ts.tv_sec;
7335 clock_pairing.nsec = ts.tv_nsec;
7336 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7337 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007338 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007339
7340 ret = 0;
7341 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7342 sizeof(struct kvm_clock_pairing)))
7343 ret = -KVM_EFAULT;
7344
7345 return ret;
7346}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007347#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007348
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307349/*
7350 * kvm_pv_kick_cpu_op: Kick a vcpu.
7351 *
7352 * @apicid - apicid of vcpu to be kicked.
7353 */
7354static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7355{
Raghavendra K T24d21662013-08-26 14:18:35 +05307356 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307357
Peter Xu150a84f2019-12-04 20:07:21 +01007358 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007359 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007360 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307361 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007362 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307363
Raghavendra K T24d21662013-08-26 14:18:35 +05307364 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007365 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307366}
7367
Andrey Smetanind62caab2015-11-10 15:36:33 +03007368void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
7369{
Paolo Bonzinif7589cc2019-01-30 17:18:59 +01007370 if (!lapic_in_kernel(vcpu)) {
7371 WARN_ON_ONCE(vcpu->arch.apicv_active);
7372 return;
7373 }
7374 if (!vcpu->arch.apicv_active)
7375 return;
7376
Andrey Smetanind62caab2015-11-10 15:36:33 +03007377 vcpu->arch.apicv_active = false;
7378 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
7379}
7380
Wanpeng Li71506292019-06-11 20:23:49 +08007381static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7382{
7383 struct kvm_vcpu *target = NULL;
7384 struct kvm_apic_map *map;
7385
7386 rcu_read_lock();
7387 map = rcu_dereference(kvm->arch.apic_map);
7388
7389 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7390 target = map->phys_map[dest_id]->vcpu;
7391
7392 rcu_read_unlock();
7393
Wanpeng Li266e85a2019-07-24 17:43:13 +08007394 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007395 kvm_vcpu_yield_to(target);
7396}
7397
Hollis Blanchard8776e512007-10-31 17:24:24 -05007398int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7399{
7400 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007401 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007402
Radim Krčmář696ca772018-05-24 17:50:56 +02007403 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7404 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007405
Sean Christophersonde3cd112019-04-30 10:36:17 -07007406 nr = kvm_rax_read(vcpu);
7407 a0 = kvm_rbx_read(vcpu);
7408 a1 = kvm_rcx_read(vcpu);
7409 a2 = kvm_rdx_read(vcpu);
7410 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007411
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007412 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007413
Nadav Amita449c7a2014-06-18 17:19:24 +03007414 op_64_bit = is_64_bit_mode(vcpu);
7415 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007416 nr &= 0xFFFFFFFF;
7417 a0 &= 0xFFFFFFFF;
7418 a1 &= 0xFFFFFFFF;
7419 a2 &= 0xFFFFFFFF;
7420 a3 &= 0xFFFFFFFF;
7421 }
7422
Jan Kiszka07708c42009-08-03 18:43:28 +02007423 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7424 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007425 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007426 }
7427
Hollis Blanchard8776e512007-10-31 17:24:24 -05007428 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007429 case KVM_HC_VAPIC_POLL_IRQ:
7430 ret = 0;
7431 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307432 case KVM_HC_KICK_CPU:
7433 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007434 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307435 ret = 0;
7436 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007437#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007438 case KVM_HC_CLOCK_PAIRING:
7439 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7440 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007441#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007442 case KVM_HC_SEND_IPI:
7443 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7444 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007445 case KVM_HC_SCHED_YIELD:
7446 kvm_sched_yield(vcpu->kvm, a0);
7447 ret = 0;
7448 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007449 default:
7450 ret = -KVM_ENOSYS;
7451 break;
7452 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007453out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007454 if (!op_64_bit)
7455 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007456 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007457
Amit Shahf11c3a82008-02-21 01:00:30 +05307458 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007459 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007460}
7461EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7462
Jan Kiszkab6785de2012-09-20 07:43:17 +02007463static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007464{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007465 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007466 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007467 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007468
Hollis Blanchard8776e512007-10-31 17:24:24 -05007469 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007470
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007471 return emulator_write_emulated(ctxt, rip, instruction, 3,
7472 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007473}
7474
Avi Kivity851ba692009-08-24 11:10:17 +03007475static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007476{
Matt Gingell782d4222015-11-16 15:26:00 -08007477 return vcpu->run->request_interrupt_window &&
7478 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007482{
Avi Kivity851ba692009-08-24 11:10:17 +03007483 struct kvm_run *kvm_run = vcpu->run;
7484
Jan Kiszka91586a32009-10-05 13:07:21 +02007485 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007486 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007487 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007488 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007489 kvm_run->ready_for_interrupt_injection =
7490 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007491 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007492}
7493
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007494static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7495{
7496 int max_irr, tpr;
7497
7498 if (!kvm_x86_ops->update_cr8_intercept)
7499 return;
7500
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007501 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007502 return;
7503
Andrey Smetanind62caab2015-11-10 15:36:33 +03007504 if (vcpu->arch.apicv_active)
7505 return;
7506
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007507 if (!vcpu->arch.apic->vapic_addr)
7508 max_irr = kvm_lapic_find_highest_irr(vcpu);
7509 else
7510 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007511
7512 if (max_irr != -1)
7513 max_irr >>= 4;
7514
7515 tpr = kvm_lapic_get_cr8(vcpu);
7516
7517 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7518}
7519
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007520static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007521{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007522 int r;
7523
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007524 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007525
Liran Alon1a680e32018-03-23 03:01:33 +03007526 if (vcpu->arch.exception.injected)
7527 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007528 /*
Liran Alona042c262018-03-23 03:01:32 +03007529 * Do not inject an NMI or interrupt if there is a pending
7530 * exception. Exceptions and interrupts are recognized at
7531 * instruction boundaries, i.e. the start of an instruction.
7532 * Trap-like exceptions, e.g. #DB, have higher priority than
7533 * NMIs and interrupts, i.e. traps are recognized before an
7534 * NMI/interrupt that's pending on the same instruction.
7535 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7536 * priority, but are only generated (pended) during instruction
7537 * execution, i.e. a pending fault-like exception means the
7538 * fault occurred on the *previous* instruction and must be
7539 * serviced prior to recognizing any new events in order to
7540 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007541 */
Liran Alon1a680e32018-03-23 03:01:33 +03007542 else if (!vcpu->arch.exception.pending) {
7543 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007544 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007545 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007546 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007547 }
7548
Liran Alon1a680e32018-03-23 03:01:33 +03007549 /*
7550 * Call check_nested_events() even if we reinjected a previous event
7551 * in order for caller to determine if it should require immediate-exit
7552 * from L2 to L1 due to pending L1 events which require exit
7553 * from L2 to L1.
7554 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007555 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7556 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7557 if (r != 0)
7558 return r;
7559 }
7560
7561 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007562 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007563 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7564 vcpu->arch.exception.has_error_code,
7565 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007566
Liran Alon1a680e32018-03-23 03:01:33 +03007567 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007568 vcpu->arch.exception.pending = false;
7569 vcpu->arch.exception.injected = true;
7570
Nadav Amitd6e8c852014-07-24 14:51:24 +03007571 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7572 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7573 X86_EFLAGS_RF);
7574
Jim Mattsonf10c7292018-10-16 14:29:23 -07007575 if (vcpu->arch.exception.nr == DB_VECTOR) {
7576 /*
7577 * This code assumes that nSVM doesn't use
7578 * check_nested_events(). If it does, the
7579 * DR6/DR7 changes should happen before L1
7580 * gets a #VMEXIT for an intercepted #DB in
7581 * L2. (Under VMX, on the other hand, the
7582 * DR6/DR7 changes should not happen in the
7583 * event of a VM-exit to L1 for an intercepted
7584 * #DB in L2.)
7585 */
7586 kvm_deliver_exception_payload(vcpu);
7587 if (vcpu->arch.dr7 & DR7_GD) {
7588 vcpu->arch.dr7 &= ~DR7_GD;
7589 kvm_update_dr7(vcpu);
7590 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007591 }
7592
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007593 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007594 }
7595
7596 /* Don't consider new event if we re-injected an event */
7597 if (kvm_event_needs_reinjection(vcpu))
7598 return 0;
7599
7600 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7601 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007602 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007603 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007604 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007605 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007606 --vcpu->arch.nmi_pending;
7607 vcpu->arch.nmi_injected = true;
7608 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007609 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007610 /*
7611 * Because interrupts can be injected asynchronously, we are
7612 * calling check_nested_events again here to avoid a race condition.
7613 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7614 * proposal and current concerns. Perhaps we should be setting
7615 * KVM_REQ_EVENT only on certain events and not unconditionally?
7616 */
7617 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7618 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7619 if (r != 0)
7620 return r;
7621 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007622 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007623 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7624 false);
7625 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007626 }
7627 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007628
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007629 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007630}
7631
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007632static void process_nmi(struct kvm_vcpu *vcpu)
7633{
7634 unsigned limit = 2;
7635
7636 /*
7637 * x86 is limited to one NMI running, and one NMI pending after it.
7638 * If an NMI is already in progress, limit further NMIs to just one.
7639 * Otherwise, allow two (and we'll inject the first one immediately).
7640 */
7641 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7642 limit = 1;
7643
7644 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7645 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7646 kvm_make_request(KVM_REQ_EVENT, vcpu);
7647}
7648
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007649static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007650{
7651 u32 flags = 0;
7652 flags |= seg->g << 23;
7653 flags |= seg->db << 22;
7654 flags |= seg->l << 21;
7655 flags |= seg->avl << 20;
7656 flags |= seg->present << 15;
7657 flags |= seg->dpl << 13;
7658 flags |= seg->s << 12;
7659 flags |= seg->type << 8;
7660 return flags;
7661}
7662
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007663static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007664{
7665 struct kvm_segment seg;
7666 int offset;
7667
7668 kvm_get_segment(vcpu, &seg, n);
7669 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7670
7671 if (n < 3)
7672 offset = 0x7f84 + n * 12;
7673 else
7674 offset = 0x7f2c + (n - 3) * 12;
7675
7676 put_smstate(u32, buf, offset + 8, seg.base);
7677 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007678 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007679}
7680
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007681#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007682static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007683{
7684 struct kvm_segment seg;
7685 int offset;
7686 u16 flags;
7687
7688 kvm_get_segment(vcpu, &seg, n);
7689 offset = 0x7e00 + n * 16;
7690
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007691 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007692 put_smstate(u16, buf, offset, seg.selector);
7693 put_smstate(u16, buf, offset + 2, flags);
7694 put_smstate(u32, buf, offset + 4, seg.limit);
7695 put_smstate(u64, buf, offset + 8, seg.base);
7696}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007697#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007698
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007699static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007700{
7701 struct desc_ptr dt;
7702 struct kvm_segment seg;
7703 unsigned long val;
7704 int i;
7705
7706 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7707 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7708 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7709 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7710
7711 for (i = 0; i < 8; i++)
7712 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7713
7714 kvm_get_dr(vcpu, 6, &val);
7715 put_smstate(u32, buf, 0x7fcc, (u32)val);
7716 kvm_get_dr(vcpu, 7, &val);
7717 put_smstate(u32, buf, 0x7fc8, (u32)val);
7718
7719 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7720 put_smstate(u32, buf, 0x7fc4, seg.selector);
7721 put_smstate(u32, buf, 0x7f64, seg.base);
7722 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007723 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007724
7725 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7726 put_smstate(u32, buf, 0x7fc0, seg.selector);
7727 put_smstate(u32, buf, 0x7f80, seg.base);
7728 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007729 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007730
7731 kvm_x86_ops->get_gdt(vcpu, &dt);
7732 put_smstate(u32, buf, 0x7f74, dt.address);
7733 put_smstate(u32, buf, 0x7f70, dt.size);
7734
7735 kvm_x86_ops->get_idt(vcpu, &dt);
7736 put_smstate(u32, buf, 0x7f58, dt.address);
7737 put_smstate(u32, buf, 0x7f54, dt.size);
7738
7739 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007740 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007741
7742 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7743
7744 /* revision id */
7745 put_smstate(u32, buf, 0x7efc, 0x00020000);
7746 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7747}
7748
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007749#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007750static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007751{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007752 struct desc_ptr dt;
7753 struct kvm_segment seg;
7754 unsigned long val;
7755 int i;
7756
7757 for (i = 0; i < 16; i++)
7758 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7759
7760 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7761 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7762
7763 kvm_get_dr(vcpu, 6, &val);
7764 put_smstate(u64, buf, 0x7f68, val);
7765 kvm_get_dr(vcpu, 7, &val);
7766 put_smstate(u64, buf, 0x7f60, val);
7767
7768 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7769 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7770 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7771
7772 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7773
7774 /* revision id */
7775 put_smstate(u32, buf, 0x7efc, 0x00020064);
7776
7777 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7778
7779 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7780 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007781 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007782 put_smstate(u32, buf, 0x7e94, seg.limit);
7783 put_smstate(u64, buf, 0x7e98, seg.base);
7784
7785 kvm_x86_ops->get_idt(vcpu, &dt);
7786 put_smstate(u32, buf, 0x7e84, dt.size);
7787 put_smstate(u64, buf, 0x7e88, dt.address);
7788
7789 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7790 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007791 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007792 put_smstate(u32, buf, 0x7e74, seg.limit);
7793 put_smstate(u64, buf, 0x7e78, seg.base);
7794
7795 kvm_x86_ops->get_gdt(vcpu, &dt);
7796 put_smstate(u32, buf, 0x7e64, dt.size);
7797 put_smstate(u64, buf, 0x7e68, dt.address);
7798
7799 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007800 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007801}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007802#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007803
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007804static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007805{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007806 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007807 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007808 char buf[512];
7809 u32 cr0;
7810
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007811 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007812 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007813#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007814 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007815 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007816 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007817#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007818 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007819
Ladi Prosek0234bf82017-10-11 16:54:40 +02007820 /*
7821 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7822 * vCPU state (e.g. leave guest mode) after we've saved the state into
7823 * the SMM state-save area.
7824 */
7825 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7826
7827 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007828 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007829
7830 if (kvm_x86_ops->get_nmi_mask(vcpu))
7831 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7832 else
7833 kvm_x86_ops->set_nmi_mask(vcpu, true);
7834
7835 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7836 kvm_rip_write(vcpu, 0x8000);
7837
7838 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7839 kvm_x86_ops->set_cr0(vcpu, cr0);
7840 vcpu->arch.cr0 = cr0;
7841
7842 kvm_x86_ops->set_cr4(vcpu, 0);
7843
Paolo Bonzini18c36262015-08-07 12:27:54 +02007844 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7845 dt.address = dt.size = 0;
7846 kvm_x86_ops->set_idt(vcpu, &dt);
7847
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007848 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7849
7850 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7851 cs.base = vcpu->arch.smbase;
7852
7853 ds.selector = 0;
7854 ds.base = 0;
7855
7856 cs.limit = ds.limit = 0xffffffff;
7857 cs.type = ds.type = 0x3;
7858 cs.dpl = ds.dpl = 0;
7859 cs.db = ds.db = 0;
7860 cs.s = ds.s = 1;
7861 cs.l = ds.l = 0;
7862 cs.g = ds.g = 1;
7863 cs.avl = ds.avl = 0;
7864 cs.present = ds.present = 1;
7865 cs.unusable = ds.unusable = 0;
7866 cs.padding = ds.padding = 0;
7867
7868 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7869 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7870 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7871 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7872 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7873 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7874
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007875#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007876 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007877 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007878#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007879
7880 kvm_update_cpuid(vcpu);
7881 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007882}
7883
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007884static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007885{
7886 vcpu->arch.smi_pending = true;
7887 kvm_make_request(KVM_REQ_EVENT, vcpu);
7888}
7889
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05007890void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
7891 unsigned long *vcpu_bitmap)
7892{
7893 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05007894
7895 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
7896
Mao Wenandb5a95e2019-11-19 11:06:40 +08007897 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
7898 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05007899
7900 free_cpumask_var(cpus);
7901}
7902
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01007903void kvm_make_scan_ioapic_request(struct kvm *kvm)
7904{
7905 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
7906}
7907
Yang Zhang3d81bc72013-04-11 19:25:13 +08007908static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08007909{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08007910 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08007911 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08007912
Andrey Smetanin63086302015-11-10 15:36:32 +03007913 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007914
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07007915 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03007916 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007917 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02007918 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03007919 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08007920 if (ioapic_in_kernel(vcpu->kvm))
7921 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007922 }
Liran Alone40ff1d2018-03-21 02:50:31 +02007923
7924 if (is_guest_mode(vcpu))
7925 vcpu->arch.load_eoi_exitmap_pending = true;
7926 else
7927 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
7928}
7929
7930static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
7931{
7932 u64 eoi_exit_bitmap[4];
7933
7934 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7935 return;
7936
Andrey Smetanin5c9194122015-11-10 15:36:34 +03007937 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
7938 vcpu_to_synic(vcpu)->vec_bitmap, 256);
7939 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007940}
7941
Michal Hocko93065ac2018-08-21 21:52:33 -07007942int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
7943 unsigned long start, unsigned long end,
7944 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01007945{
7946 unsigned long apic_address;
7947
7948 /*
7949 * The physical address of apic access page is stored in the VMCS.
7950 * Update it when it becomes invalid.
7951 */
7952 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
7953 if (start <= apic_address && apic_address < end)
7954 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07007955
7956 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01007957}
7958
Tang Chen4256f432014-09-24 15:57:54 +08007959void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
7960{
Tang Chenc24ae0d2014-09-24 15:57:58 +08007961 struct page *page = NULL;
7962
Paolo Bonzini35754c92015-07-29 12:05:37 +02007963 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02007964 return;
7965
Tang Chen4256f432014-09-24 15:57:54 +08007966 if (!kvm_x86_ops->set_apic_access_page_addr)
7967 return;
7968
Tang Chenc24ae0d2014-09-24 15:57:58 +08007969 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02007970 if (is_error_page(page))
7971 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08007972 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
7973
7974 /*
7975 * Do not pin apic access page in memory, the MMU notifier
7976 * will call us again if it is migrated or swapped out.
7977 */
7978 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08007979}
Tang Chen4256f432014-09-24 15:57:54 +08007980
Sean Christophersond264ee02018-08-27 15:21:12 -07007981void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
7982{
7983 smp_send_reschedule(vcpu->cpu);
7984}
7985EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
7986
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007987/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01007988 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007989 * exiting to the userspace. Otherwise, the value will be returned to the
7990 * userspace.
7991 */
Avi Kivity851ba692009-08-24 11:10:17 +03007992static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007993{
7994 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08007995 bool req_int_win =
7996 dm_request_for_irq_injection(vcpu) &&
7997 kvm_cpu_accept_dm_intr(vcpu);
7998
Jan Kiszka730dca42013-04-28 10:50:52 +02007999 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008000
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008001 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008002 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8003 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8004 r = 0;
8005 goto out;
8006 }
8007 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008008 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008009 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008010 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008011 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008012 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8013 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03008014 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8015 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008016 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8017 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008018 if (unlikely(r))
8019 goto out;
8020 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008021 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008022 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07008023 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
8024 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008025 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008026 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008027 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008028 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008029 r = 0;
8030 goto out;
8031 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008032 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008033 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008034 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008035 r = 0;
8036 goto out;
8037 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008038 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8039 /* Page is swapped out. Do synthetic halt */
8040 vcpu->arch.apf.halted = true;
8041 r = 1;
8042 goto out;
8043 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008044 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8045 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008046 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8047 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008048 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8049 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008050 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008051 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008052 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008053 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008054 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8055 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8056 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008057 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008058 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8059 vcpu->run->eoi.vector =
8060 vcpu->arch.pending_ioapic_eoi;
8061 r = 0;
8062 goto out;
8063 }
8064 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008065 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8066 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008067 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8068 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008069 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8070 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008071 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8072 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8073 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8074 r = 0;
8075 goto out;
8076 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008077 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8078 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8079 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8080 r = 0;
8081 goto out;
8082 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008083 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8084 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8085 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8086 r = 0;
8087 goto out;
8088 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008089
8090 /*
8091 * KVM_REQ_HV_STIMER has to be processed after
8092 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8093 * depend on the guest clock being up-to-date
8094 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008095 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8096 kvm_hv_process_stimers(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008097 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008098
Avi Kivityb463a6f2010-07-20 15:06:17 +03008099 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008100 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008101 kvm_apic_accept_events(vcpu);
8102 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8103 r = 1;
8104 goto out;
8105 }
8106
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008107 if (inject_pending_event(vcpu, req_int_win) != 0)
8108 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008109 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008110 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008111 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008112 * SMIs have three cases:
8113 * 1) They can be nested, and then there is nothing to
8114 * do here because RSM will cause a vmexit anyway.
8115 * 2) There is an ISA-specific reason why SMI cannot be
8116 * injected, and the moment when this changes can be
8117 * intercepted.
8118 * 3) Or the SMI can be pending because
8119 * inject_pending_event has completed the injection
8120 * of an IRQ or NMI from the previous vmexit, and
8121 * then we request an immediate exit to inject the
8122 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008123 */
8124 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008125 if (!kvm_x86_ops->enable_smi_window(vcpu))
8126 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008127 if (vcpu->arch.nmi_pending)
8128 kvm_x86_ops->enable_nmi_window(vcpu);
8129 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8130 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008131 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008132 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008133
Avi Kivity3842d132010-07-27 12:30:24 +03008134 if (kvm_lapic_enabled(vcpu)) {
8135 update_cr8_intercept(vcpu);
8136 kvm_lapic_sync_to_vapic(vcpu);
8137 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008138 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008139
Avi Kivityd8368af2012-05-14 18:07:56 +03008140 r = kvm_mmu_reload(vcpu);
8141 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008142 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008143 }
8144
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008145 preempt_disable();
8146
8147 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008148
8149 /*
8150 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8151 * IPI are then delayed after guest entry, which ensures that they
8152 * result in virtual interrupt delivery.
8153 */
8154 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008155 vcpu->mode = IN_GUEST_MODE;
8156
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008157 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8158
Lan Tianyu0f127d12016-03-13 11:10:29 +08008159 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008160 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008161 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008162 *
Luwei Kang81b01662019-01-31 16:52:02 +08008163 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008164 * pairs with the memory barrier implicit in pi_test_and_set_on
8165 * (see vmx_deliver_posted_interrupt).
8166 *
8167 * 3) This also orders the write to mode from any reads to the page
8168 * tables done while the VCPU is running. Please see the comment
8169 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008170 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008171 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008172
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008173 /*
8174 * This handles the case where a posted interrupt was
8175 * notified with kvm_vcpu_kick.
8176 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008177 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8178 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008179
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008180 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008181 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008182 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008183 smp_wmb();
8184 local_irq_enable();
8185 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008186 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008187 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008188 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008189 }
8190
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008191 if (req_immediate_exit) {
8192 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008193 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008194 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008195
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008196 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008197 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008198
Wanpeng Lie7517322019-07-22 12:26:20 +08008199 /* The preempt notifier should have taken care of the FPU already. */
8200 WARN_ON_ONCE(test_thread_flag(TIF_NEED_FPU_LOAD));
Rik van Riel5f409e22019-04-03 18:41:52 +02008201
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008202 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008203 set_debugreg(0, 7);
8204 set_debugreg(vcpu->arch.eff_db[0], 0);
8205 set_debugreg(vcpu->arch.eff_db[1], 1);
8206 set_debugreg(vcpu->arch.eff_db[2], 2);
8207 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008208 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008209 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008210 }
8211
Avi Kivity851ba692009-08-24 11:10:17 +03008212 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008213
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008214 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008215 * Do this here before restoring debug registers on the host. And
8216 * since we do this before handling the vmexit, a DR access vmexit
8217 * can (a) read the correct value of the debug registers, (b) set
8218 * KVM_DEBUGREG_WONT_EXIT again.
8219 */
8220 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008221 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8222 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008223 kvm_update_dr0123(vcpu);
8224 kvm_update_dr6(vcpu);
8225 kvm_update_dr7(vcpu);
8226 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008227 }
8228
8229 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008230 * If the guest has used debug registers, at least dr7
8231 * will be disabled while returning to the host.
8232 * If we don't have active breakpoints in the host, we don't
8233 * care about the messed up debug address registers. But if
8234 * we have some of them active, restore the old state.
8235 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008236 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008237 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008238
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008239 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008240
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008241 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008242 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008243
Sean Christopherson95b5a482019-04-19 22:50:59 -07008244 kvm_x86_ops->handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008245
Sean Christophersond7a08882019-07-10 09:07:34 -07008246 /*
8247 * Consume any pending interrupts, including the possible source of
8248 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8249 * An instruction is required after local_irq_enable() to fully unblock
8250 * interrupts on processors that implement an interrupt shadow, the
8251 * stat.exits increment will do nicely.
8252 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008253 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008254 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008255 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008256 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008257 kvm_after_interrupt(vcpu);
8258
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008259 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008260 if (lapic_in_kernel(vcpu)) {
8261 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8262 if (delta != S64_MIN) {
8263 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8264 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8265 }
8266 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008267
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008268 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008269 preempt_enable();
8270
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008271 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008272
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008273 /*
8274 * Profile KVM exit RIPs:
8275 */
8276 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008277 unsigned long rip = kvm_rip_read(vcpu);
8278 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008279 }
8280
Zachary Amsdencc578282012-02-03 15:43:50 -02008281 if (unlikely(vcpu->arch.tsc_always_catchup))
8282 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008283
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008284 if (vcpu->arch.apic_attention)
8285 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008286
Brijesh Singh618232e2017-08-17 18:36:57 +02008287 vcpu->arch.gpa_available = false;
Avi Kivity851ba692009-08-24 11:10:17 +03008288 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008289 return r;
8290
8291cancel_injection:
8292 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008293 if (unlikely(vcpu->arch.apic_attention))
8294 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008295out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008296 return r;
8297}
8298
Paolo Bonzini362c6982015-02-06 12:48:04 +01008299static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8300{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008301 if (!kvm_arch_vcpu_runnable(vcpu) &&
8302 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008303 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8304 kvm_vcpu_block(vcpu);
8305 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008306
8307 if (kvm_x86_ops->post_block)
8308 kvm_x86_ops->post_block(vcpu);
8309
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008310 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8311 return 1;
8312 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008313
Paolo Bonzini362c6982015-02-06 12:48:04 +01008314 kvm_apic_accept_events(vcpu);
8315 switch(vcpu->arch.mp_state) {
8316 case KVM_MP_STATE_HALTED:
8317 vcpu->arch.pv.pv_unhalted = false;
8318 vcpu->arch.mp_state =
8319 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008320 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008321 case KVM_MP_STATE_RUNNABLE:
8322 vcpu->arch.apf.halted = false;
8323 break;
8324 case KVM_MP_STATE_INIT_RECEIVED:
8325 break;
8326 default:
8327 return -EINTR;
8328 break;
8329 }
8330 return 1;
8331}
8332
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008333static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8334{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008335 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8336 kvm_x86_ops->check_nested_events(vcpu, false);
8337
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008338 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8339 !vcpu->arch.apf.halted);
8340}
8341
Paolo Bonzini362c6982015-02-06 12:48:04 +01008342static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008343{
8344 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008345 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008346
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008347 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008348 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008349
Paolo Bonzini362c6982015-02-06 12:48:04 +01008350 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008351 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008352 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008353 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008354 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008355 }
8356
Gleb Natapov09cec752009-03-23 15:11:44 +02008357 if (r <= 0)
8358 break;
8359
Radim Krčmář72875d82017-04-26 22:32:19 +02008360 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008361 if (kvm_cpu_has_pending_timer(vcpu))
8362 kvm_inject_pending_timer_irqs(vcpu);
8363
Matt Gingell782d4222015-11-16 15:26:00 -08008364 if (dm_request_for_irq_injection(vcpu) &&
8365 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008366 r = 0;
8367 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008368 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008369 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008370 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008371
8372 kvm_check_async_pf_completion(vcpu);
8373
Gleb Natapov09cec752009-03-23 15:11:44 +02008374 if (signal_pending(current)) {
8375 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008376 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008377 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008378 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008379 }
8380 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008381 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008382 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008383 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008384 }
8385 }
8386
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008387 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008388
8389 return r;
8390}
8391
Gleb Natapov716d51a2012-09-03 15:24:26 +03008392static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8393{
8394 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008395
Gleb Natapov716d51a2012-09-03 15:24:26 +03008396 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008397 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008398 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008399 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008400}
8401
8402static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8403{
8404 BUG_ON(!vcpu->arch.pio.count);
8405
8406 return complete_emulated_io(vcpu);
8407}
8408
Avi Kivityf78146b2012-04-18 19:22:47 +03008409/*
8410 * Implements the following, as a state machine:
8411 *
8412 * read:
8413 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008414 * for each mmio piece in the fragment
8415 * write gpa, len
8416 * exit
8417 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008418 * execute insn
8419 *
8420 * write:
8421 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008422 * for each mmio piece in the fragment
8423 * write gpa, len
8424 * copy data
8425 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008426 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008427static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008428{
8429 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008430 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008431 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008432
Gleb Natapov716d51a2012-09-03 15:24:26 +03008433 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008434
Gleb Natapov716d51a2012-09-03 15:24:26 +03008435 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008436 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8437 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008438 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008439 memcpy(frag->data, run->mmio.data, len);
8440
8441 if (frag->len <= 8) {
8442 /* Switch to the next fragment. */
8443 frag++;
8444 vcpu->mmio_cur_fragment++;
8445 } else {
8446 /* Go forward to the next mmio piece. */
8447 frag->data += len;
8448 frag->gpa += len;
8449 frag->len -= len;
8450 }
8451
Andrew Honiga08d3b32014-02-27 19:35:14 +01008452 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008453 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008454
8455 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008456 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008457 return 1;
8458 vcpu->mmio_read_completed = 1;
8459 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008460 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008461
Gleb Natapov716d51a2012-09-03 15:24:26 +03008462 run->exit_reason = KVM_EXIT_MMIO;
8463 run->mmio.phys_addr = frag->gpa;
8464 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008465 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8466 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008467 run->mmio.is_write = vcpu->mmio_is_write;
8468 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8469 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008470}
8471
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008472/* Swap (qemu) user FPU context for the guest FPU context. */
8473static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8474{
Rik van Riel5f409e22019-04-03 18:41:52 +02008475 fpregs_lock();
8476
Sean Christopherson95145c22019-12-09 12:05:17 -08008477 /*
8478 * Reloading userspace's FPU is handled by kvm_arch_vcpu_load(), both
8479 * for direct calls from userspace (via vcpu_load()) and if this task
8480 * is preempted (via kvm_sched_in()) between vcpu_load() and now.
8481 */
8482 WARN_ON_ONCE(test_thread_flag(TIF_NEED_FPU_LOAD));
8483
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008484 copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008485 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008486 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008487 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008488
8489 fpregs_mark_activate();
8490 fpregs_unlock();
8491
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008492 trace_kvm_fpu(1);
8493}
8494
8495/* When vcpu_run ends, restore user space FPU context. */
8496static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8497{
Rik van Riel5f409e22019-04-03 18:41:52 +02008498 fpregs_lock();
8499
Marc Orrb666a4b2018-11-06 14:53:56 -08008500 copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008501 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008502
8503 fpregs_mark_activate();
8504 fpregs_unlock();
8505
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008506 ++vcpu->stat.fpu_reload;
8507 trace_kvm_fpu(0);
8508}
8509
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008510int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8511{
8512 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008513
Christoffer Dallaccb7572017-12-04 21:35:25 +01008514 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008515 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008516 kvm_load_guest_fpu(vcpu);
8517
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008518 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008519 if (kvm_run->immediate_exit) {
8520 r = -EINTR;
8521 goto out;
8522 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008523 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008524 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008525 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008526 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008527 if (signal_pending(current)) {
8528 r = -EINTR;
8529 vcpu->run->exit_reason = KVM_EXIT_INTR;
8530 ++vcpu->stat.signal_exits;
8531 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008532 goto out;
8533 }
8534
Ken Hofsass01643c52018-01-31 16:03:36 -08008535 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8536 r = -EINVAL;
8537 goto out;
8538 }
8539
8540 if (vcpu->run->kvm_dirty_regs) {
8541 r = sync_regs(vcpu);
8542 if (r != 0)
8543 goto out;
8544 }
8545
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008546 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008547 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008548 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8549 r = -EINVAL;
8550 goto out;
8551 }
8552 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008553
Gleb Natapov716d51a2012-09-03 15:24:26 +03008554 if (unlikely(vcpu->arch.complete_userspace_io)) {
8555 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8556 vcpu->arch.complete_userspace_io = NULL;
8557 r = cui(vcpu);
8558 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008559 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008560 } else
8561 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008562
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008563 if (kvm_run->immediate_exit)
8564 r = -EINTR;
8565 else
8566 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008567
8568out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008569 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008570 if (vcpu->run->kvm_valid_regs)
8571 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008572 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008573 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008574
Christoffer Dallaccb7572017-12-04 21:35:25 +01008575 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008576 return r;
8577}
8578
Ken Hofsass01643c52018-01-31 16:03:36 -08008579static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008580{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008581 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8582 /*
8583 * We are here if userspace calls get_regs() in the middle of
8584 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008585 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008586 * that usually, but some bad designed PV devices (vmware
8587 * backdoor interface) need this to work
8588 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03008589 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008590 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8591 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008592 regs->rax = kvm_rax_read(vcpu);
8593 regs->rbx = kvm_rbx_read(vcpu);
8594 regs->rcx = kvm_rcx_read(vcpu);
8595 regs->rdx = kvm_rdx_read(vcpu);
8596 regs->rsi = kvm_rsi_read(vcpu);
8597 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008598 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008599 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008600#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008601 regs->r8 = kvm_r8_read(vcpu);
8602 regs->r9 = kvm_r9_read(vcpu);
8603 regs->r10 = kvm_r10_read(vcpu);
8604 regs->r11 = kvm_r11_read(vcpu);
8605 regs->r12 = kvm_r12_read(vcpu);
8606 regs->r13 = kvm_r13_read(vcpu);
8607 regs->r14 = kvm_r14_read(vcpu);
8608 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008609#endif
8610
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008611 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008612 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008613}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008614
Ken Hofsass01643c52018-01-31 16:03:36 -08008615int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8616{
8617 vcpu_load(vcpu);
8618 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008619 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008620 return 0;
8621}
8622
Ken Hofsass01643c52018-01-31 16:03:36 -08008623static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008624{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008625 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8626 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8627
Sean Christophersonde3cd112019-04-30 10:36:17 -07008628 kvm_rax_write(vcpu, regs->rax);
8629 kvm_rbx_write(vcpu, regs->rbx);
8630 kvm_rcx_write(vcpu, regs->rcx);
8631 kvm_rdx_write(vcpu, regs->rdx);
8632 kvm_rsi_write(vcpu, regs->rsi);
8633 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008634 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008635 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008636#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008637 kvm_r8_write(vcpu, regs->r8);
8638 kvm_r9_write(vcpu, regs->r9);
8639 kvm_r10_write(vcpu, regs->r10);
8640 kvm_r11_write(vcpu, regs->r11);
8641 kvm_r12_write(vcpu, regs->r12);
8642 kvm_r13_write(vcpu, regs->r13);
8643 kvm_r14_write(vcpu, regs->r14);
8644 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008645#endif
8646
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008647 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008648 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008649
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008650 vcpu->arch.exception.pending = false;
8651
Avi Kivity3842d132010-07-27 12:30:24 +03008652 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008653}
Avi Kivity3842d132010-07-27 12:30:24 +03008654
Ken Hofsass01643c52018-01-31 16:03:36 -08008655int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8656{
8657 vcpu_load(vcpu);
8658 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008659 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008660 return 0;
8661}
8662
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008663void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8664{
8665 struct kvm_segment cs;
8666
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008667 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008668 *db = cs.db;
8669 *l = cs.l;
8670}
8671EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8672
Ken Hofsass01643c52018-01-31 16:03:36 -08008673static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008674{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008675 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008676
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008677 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8678 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8679 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8680 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8681 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8682 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008683
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008684 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8685 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008686
8687 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008688 sregs->idt.limit = dt.size;
8689 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008690 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008691 sregs->gdt.limit = dt.size;
8692 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008693
Avi Kivity4d4ec082009-12-29 18:07:30 +02008694 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008695 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008696 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008697 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008698 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008699 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008700 sregs->apic_base = kvm_get_apic_base(vcpu);
8701
Jordan Borgner0e96f312018-10-28 12:58:28 +00008702 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008703
Liran Alon04140b42018-03-23 03:01:31 +03008704 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008705 set_bit(vcpu->arch.interrupt.nr,
8706 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008707}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008708
Ken Hofsass01643c52018-01-31 16:03:36 -08008709int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8710 struct kvm_sregs *sregs)
8711{
8712 vcpu_load(vcpu);
8713 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008714 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008715 return 0;
8716}
8717
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008718int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8719 struct kvm_mp_state *mp_state)
8720{
Christoffer Dallfd232562017-12-04 21:35:30 +01008721 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008722 if (kvm_mpx_supported())
8723 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008724
Jan Kiszka66450a22013-03-13 12:42:34 +01008725 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308726 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8727 vcpu->arch.pv.pv_unhalted)
8728 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8729 else
8730 mp_state->mp_state = vcpu->arch.mp_state;
8731
Sean Christophersonf958bd22019-12-09 12:19:31 -08008732 if (kvm_mpx_supported())
8733 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008734 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008735 return 0;
8736}
8737
8738int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8739 struct kvm_mp_state *mp_state)
8740{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008741 int ret = -EINVAL;
8742
8743 vcpu_load(vcpu);
8744
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008745 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008746 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008747 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008748
Liran Alon27cbe7d2019-11-11 11:16:40 +02008749 /*
8750 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8751 * INIT state; latched init should be reported using
8752 * KVM_SET_VCPU_EVENTS, so reject it here.
8753 */
8754 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008755 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8756 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008757 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008758
Jan Kiszka66450a22013-03-13 12:42:34 +01008759 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8760 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8761 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8762 } else
8763 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008764 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008765
8766 ret = 0;
8767out:
8768 vcpu_put(vcpu);
8769 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008770}
8771
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008772int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8773 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008774{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008775 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008776 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008777
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008778 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008779
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008780 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008781 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008782 if (ret) {
8783 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8784 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8785 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008786 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008787 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008788
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008789 kvm_rip_write(vcpu, ctxt->eip);
8790 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008791 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008792 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008793}
8794EXPORT_SYMBOL_GPL(kvm_task_switch);
8795
Peng Hao3140c152018-04-02 09:15:32 +08008796static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008797{
Tianyu Lan37b95952018-01-16 17:34:07 +08008798 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008799 /*
8800 * When EFER.LME and CR0.PG are set, the processor is in
8801 * 64-bit mode (though maybe in a 32-bit code segment).
8802 * CR4.PAE and EFER.LMA must be set.
8803 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008804 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008805 || !(sregs->efer & EFER_LMA))
8806 return -EINVAL;
8807 } else {
8808 /*
8809 * Not in 64-bit mode: EFER.LMA is clear and the code
8810 * segment cannot be 64-bit.
8811 */
8812 if (sregs->efer & EFER_LMA || sregs->cs.l)
8813 return -EINVAL;
8814 }
8815
Wanpeng Li3ca94192019-09-18 17:50:10 +08008816 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05008817}
8818
Ken Hofsass01643c52018-01-31 16:03:36 -08008819static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008820{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008821 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008822 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008823 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008824 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008825 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008826 int ret = -EINVAL;
8827
Lan Tianyuf2981032017-12-14 03:01:52 -05008828 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008829 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008830
Jim Mattsond3802282017-08-10 10:14:13 -07008831 apic_base_msr.data = sregs->apic_base;
8832 apic_base_msr.host_initiated = true;
8833 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008834 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01008835
Gleb Natapov89a27f42010-02-16 10:51:48 +02008836 dt.size = sregs->idt.limit;
8837 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008838 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008839 dt.size = sregs->gdt.limit;
8840 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008841 kvm_x86_ops->set_gdt(vcpu, &dt);
8842
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008843 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008844 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02008845 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07008846 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008847
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008848 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008849
Avi Kivityf6801df2010-01-21 15:31:50 +02008850 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008851 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008852
Avi Kivity4d4ec082009-12-29 18:07:30 +02008853 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008854 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00008855 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008856
Avi Kivityfc78f512009-12-07 12:16:48 +02008857 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05008858 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
8859 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008860 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05008861 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02008862 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008863
8864 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02008865 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02008866 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02008867 mmu_reset_needed = 1;
8868 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008869 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008870
8871 if (mmu_reset_needed)
8872 kvm_mmu_reset_context(vcpu);
8873
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03008874 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03008875 pending_vec = find_first_bit(
8876 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
8877 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008878 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03008879 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008880 }
8881
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008882 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8883 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8884 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8885 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8886 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8887 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008888
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008889 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8890 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008891
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03008892 update_cr8_intercept(vcpu);
8893
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008894 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03008895 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008896 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02008897 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008898 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
8899
Avi Kivity3842d132010-07-27 12:30:24 +03008900 kvm_make_request(KVM_REQ_EVENT, vcpu);
8901
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008902 ret = 0;
8903out:
Ken Hofsass01643c52018-01-31 16:03:36 -08008904 return ret;
8905}
8906
8907int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
8908 struct kvm_sregs *sregs)
8909{
8910 int ret;
8911
8912 vcpu_load(vcpu);
8913 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008914 vcpu_put(vcpu);
8915 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008916}
8917
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008918int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
8919 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008920{
Jan Kiszka355be0b2009-10-03 00:31:21 +02008921 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008922 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008923
Christoffer Dall66b56562017-12-04 21:35:33 +01008924 vcpu_load(vcpu);
8925
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008926 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
8927 r = -EBUSY;
8928 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03008929 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008930 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
8931 kvm_queue_exception(vcpu, DB_VECTOR);
8932 else
8933 kvm_queue_exception(vcpu, BP_VECTOR);
8934 }
8935
Jan Kiszka91586a32009-10-05 13:07:21 +02008936 /*
8937 * Read rflags as long as potentially injected trace flags are still
8938 * filtered out.
8939 */
8940 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008941
8942 vcpu->guest_debug = dbg->control;
8943 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
8944 vcpu->guest_debug = 0;
8945
8946 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008947 for (i = 0; i < KVM_NR_DB_REGS; ++i)
8948 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02008949 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008950 } else {
8951 for (i = 0; i < KVM_NR_DB_REGS; i++)
8952 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008953 }
Jan Kiszkac8639012012-09-21 05:42:55 +02008954 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008955
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008956 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8957 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
8958 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008959
Jan Kiszka91586a32009-10-05 13:07:21 +02008960 /*
8961 * Trigger an rflags update that will inject or remove the trace
8962 * flags.
8963 */
8964 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008965
Paolo Bonzinia96036b2015-11-10 11:55:36 +01008966 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008967
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008968 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008969
Avi Kivity2122ff52010-05-13 11:25:04 +03008970out:
Christoffer Dall66b56562017-12-04 21:35:33 +01008971 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008972 return r;
8973}
8974
8975/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08008976 * Translate a guest virtual address to a guest physical address.
8977 */
8978int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
8979 struct kvm_translation *tr)
8980{
8981 unsigned long vaddr = tr->linear_address;
8982 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008983 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008984
Christoffer Dall1da5b612017-12-04 21:35:32 +01008985 vcpu_load(vcpu);
8986
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008987 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02008988 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008989 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008990 tr->physical_address = gpa;
8991 tr->valid = gpa != UNMAPPED_GVA;
8992 tr->writeable = 1;
8993 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008994
Christoffer Dall1da5b612017-12-04 21:35:32 +01008995 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008996 return 0;
8997}
8998
Hollis Blanchardd0752062007-10-31 17:24:25 -05008999int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9000{
Christoffer Dall13931232017-12-04 21:35:34 +01009001 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009002
Christoffer Dall13931232017-12-04 21:35:34 +01009003 vcpu_load(vcpu);
9004
Marc Orrb666a4b2018-11-06 14:53:56 -08009005 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009006 memcpy(fpu->fpr, fxsave->st_space, 128);
9007 fpu->fcw = fxsave->cwd;
9008 fpu->fsw = fxsave->swd;
9009 fpu->ftwx = fxsave->twd;
9010 fpu->last_opcode = fxsave->fop;
9011 fpu->last_ip = fxsave->rip;
9012 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009013 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009014
Christoffer Dall13931232017-12-04 21:35:34 +01009015 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009016 return 0;
9017}
9018
9019int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9020{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009021 struct fxregs_state *fxsave;
9022
9023 vcpu_load(vcpu);
9024
Marc Orrb666a4b2018-11-06 14:53:56 -08009025 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009026
Hollis Blanchardd0752062007-10-31 17:24:25 -05009027 memcpy(fxsave->st_space, fpu->fpr, 128);
9028 fxsave->cwd = fpu->fcw;
9029 fxsave->swd = fpu->fsw;
9030 fxsave->twd = fpu->ftwx;
9031 fxsave->fop = fpu->last_opcode;
9032 fxsave->rip = fpu->last_ip;
9033 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009034 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009035
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009036 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009037 return 0;
9038}
9039
Ken Hofsass01643c52018-01-31 16:03:36 -08009040static void store_regs(struct kvm_vcpu *vcpu)
9041{
9042 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9043
9044 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9045 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9046
9047 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9048 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9049
9050 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9051 kvm_vcpu_ioctl_x86_get_vcpu_events(
9052 vcpu, &vcpu->run->s.regs.events);
9053}
9054
9055static int sync_regs(struct kvm_vcpu *vcpu)
9056{
9057 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9058 return -EINVAL;
9059
9060 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9061 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9062 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9063 }
9064 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9065 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9066 return -EINVAL;
9067 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9068 }
9069 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9070 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9071 vcpu, &vcpu->run->s.regs.events))
9072 return -EINVAL;
9073 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9074 }
9075
9076 return 0;
9077}
9078
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009079static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009080{
Marc Orrb666a4b2018-11-06 14:53:56 -08009081 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009082 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009083 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009084 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009085
Dexuan Cui2acf9232010-06-10 11:27:12 +08009086 /*
9087 * Ensure guest xcr0 is valid for loading
9088 */
Dave Hansend91cab72015-09-02 16:31:26 -07009089 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009090
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009091 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009092}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009093
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009094void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
9095{
Ido Yarivbd768e12016-10-21 12:39:57 -04009096 void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
9097
Glauber Costa12f9a482011-02-01 14:16:40 -05009098 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01009099
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009100 kvm_x86_ops->vcpu_free(vcpu);
Ido Yarivbd768e12016-10-21 12:39:57 -04009101 free_cpumask_var(wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009102}
9103
9104struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
9105 unsigned int id)
9106{
Liang Lic447e762015-05-21 04:41:25 +08009107 struct kvm_vcpu *vcpu;
9108
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009109 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Zachary Amsden6755bae2010-08-19 22:07:22 -10009110 printk_once(KERN_WARNING
9111 "kvm: SMP vm created on host with unstable TSC; "
9112 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009113
9114 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
9115
Liang Lic447e762015-05-21 04:41:25 +08009116 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02009117}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009118
Avi Kivity26e52152007-11-20 15:30:24 +02009119int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
9120{
Sean Christopherson0cf91352019-03-07 15:43:02 -08009121 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009122 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009123 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009124 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009125 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009126 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009127 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009128 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009129}
9130
Dominik Dingel31928aa2014-12-04 15:47:07 +01009131void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009132{
Will Auld8fe8ab42012-11-29 12:42:12 -08009133 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009134 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009135
Roman Kagand3457c82017-07-14 17:13:20 +03009136 kvm_hv_vcpu_postcreate(vcpu);
9137
Christoffer Dallec7660c2017-12-04 21:35:23 +01009138 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009139 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009140 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009141 msr.data = 0x0;
9142 msr.index = MSR_IA32_TSC;
9143 msr.host_initiated = true;
9144 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009145 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009146
9147 /* poll control enabled by default */
9148 vcpu->arch.msr_kvm_poll_control = 1;
9149
Christoffer Dallec7660c2017-12-04 21:35:23 +01009150 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009151
Marcelo Tosatti630994b2015-05-12 22:42:04 -03009152 if (!kvmclock_periodic_sync)
9153 return;
9154
Andrew Jones332967a2014-02-28 12:52:55 +01009155 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9156 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009157}
9158
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009159void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009160{
Gleb Natapov344d9582010-10-14 11:22:50 +02009161 vcpu->arch.apf.msr_val = 0;
9162
Christoffer Dallec7660c2017-12-04 21:35:23 +01009163 vcpu_load(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009164 kvm_mmu_unload(vcpu);
9165 vcpu_put(vcpu);
9166
9167 kvm_x86_ops->vcpu_free(vcpu);
9168}
9169
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009170void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009171{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009172 kvm_lapic_reset(vcpu, init_event);
9173
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009174 vcpu->arch.hflags = 0;
9175
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009176 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009177 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009178 atomic_set(&vcpu->arch.nmi_queued, 0);
9179 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009180 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009181 kvm_clear_interrupt_queue(vcpu);
9182 kvm_clear_exception_queue(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07009183 vcpu->arch.exception.pending = false;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009184
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009185 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009186 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009187 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009188 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009189 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009190 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009191
Nadav Amit11190222015-04-02 03:10:38 +03009192 vcpu->arch.cr2 = 0;
9193
Avi Kivity3842d132010-07-27 12:30:24 +03009194 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009195 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009196 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009197
Glauber Costa12f9a482011-02-01 14:16:40 -05009198 kvmclock_reset(vcpu);
9199
Gleb Natapovaf585b92010-10-14 11:22:46 +02009200 kvm_clear_async_pf_completion_queue(vcpu);
9201 kvm_async_pf_hash_reset(vcpu);
9202 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009203
Wanpeng Lia554d202017-10-11 05:10:19 -07009204 if (kvm_mpx_supported()) {
9205 void *mpx_state_buffer;
9206
9207 /*
9208 * To avoid have the INIT path from kvm_apic_has_events() that be
9209 * called with loaded FPU and does not let userspace fix the state.
9210 */
Rik van Rielf775b132017-11-14 16:54:23 -05009211 if (init_event)
9212 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009213 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009214 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009215 if (mpx_state_buffer)
9216 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009217 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009218 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009219 if (mpx_state_buffer)
9220 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009221 if (init_event)
9222 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009223 }
9224
Paolo Bonzini64d60672015-05-07 11:36:11 +02009225 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009226 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009227 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009228
Kyle Hueydb2336a2017-03-20 01:16:28 -07009229 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009230
9231 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009232 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009233
Julian Stecklina66f7b722012-12-05 15:26:19 +01009234 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9235 vcpu->arch.regs_avail = ~0;
9236 vcpu->arch.regs_dirty = ~0;
9237
Wanpeng Lia554d202017-10-11 05:10:19 -07009238 vcpu->arch.ia32_xss = 0;
9239
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009240 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009241}
9242
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009243void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009244{
9245 struct kvm_segment cs;
9246
9247 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9248 cs.selector = vector << 8;
9249 cs.base = vector << 12;
9250 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9251 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009252}
9253
Radim Krčmář13a34e02014-08-28 15:13:03 +02009254int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009255{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009256 struct kvm *kvm;
9257 struct kvm_vcpu *vcpu;
9258 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009259 int ret;
9260 u64 local_tsc;
9261 u64 max_tsc = 0;
9262 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009263
9264 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009265 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009266 if (ret != 0)
9267 return ret;
9268
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009269 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009270 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009271 list_for_each_entry(kvm, &vm_list, vm_list) {
9272 kvm_for_each_vcpu(i, vcpu, kvm) {
9273 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009274 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009275 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9276 backwards_tsc = true;
9277 if (vcpu->arch.last_host_tsc > max_tsc)
9278 max_tsc = vcpu->arch.last_host_tsc;
9279 }
9280 }
9281 }
9282
9283 /*
9284 * Sometimes, even reliable TSCs go backwards. This happens on
9285 * platforms that reset TSC during suspend or hibernate actions, but
9286 * maintain synchronization. We must compensate. Fortunately, we can
9287 * detect that condition here, which happens early in CPU bringup,
9288 * before any KVM threads can be running. Unfortunately, we can't
9289 * bring the TSCs fully up to date with real time, as we aren't yet far
9290 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009291 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009292 * variables that haven't been updated yet.
9293 *
9294 * So we simply find the maximum observed TSC above, then record the
9295 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9296 * the adjustment will be applied. Note that we accumulate
9297 * adjustments, in case multiple suspend cycles happen before some VCPU
9298 * gets a chance to run again. In the event that no KVM threads get a
9299 * chance to run, we will miss the entire elapsed period, as we'll have
9300 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9301 * loose cycle time. This isn't too big a deal, since the loss will be
9302 * uniform across all VCPUs (not to mention the scenario is extremely
9303 * unlikely). It is possible that a second hibernate recovery happens
9304 * much faster than a first, causing the observed TSC here to be
9305 * smaller; this would require additional padding adjustment, which is
9306 * why we set last_host_tsc to the local tsc observed here.
9307 *
9308 * N.B. - this code below runs only on platforms with reliable TSC,
9309 * as that is the only way backwards_tsc is set above. Also note
9310 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9311 * have the same delta_cyc adjustment applied if backwards_tsc
9312 * is detected. Note further, this adjustment is only done once,
9313 * as we reset last_host_tsc on all VCPUs to stop this from being
9314 * called multiple times (one for each physical CPU bringup).
9315 *
Guo Chao4a969982012-06-28 15:17:27 +08009316 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009317 * will be compensated by the logic in vcpu_load, which sets the TSC to
9318 * catchup mode. This will catchup all VCPUs to real time, but cannot
9319 * guarantee that they stay in perfect synchronization.
9320 */
9321 if (backwards_tsc) {
9322 u64 delta_cyc = max_tsc - local_tsc;
9323 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009324 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009325 kvm_for_each_vcpu(i, vcpu, kvm) {
9326 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9327 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009328 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009329 }
9330
9331 /*
9332 * We have to disable TSC offset matching.. if you were
9333 * booting a VM while issuing an S4 host suspend....
9334 * you may have some problem. Solving this issue is
9335 * left as an exercise to the reader.
9336 */
9337 kvm->arch.last_tsc_nsec = 0;
9338 kvm->arch.last_tsc_write = 0;
9339 }
9340
9341 }
9342 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009343}
9344
Radim Krčmář13a34e02014-08-28 15:13:03 +02009345void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009346{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009347 kvm_x86_ops->hardware_disable();
9348 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009349}
9350
9351int kvm_arch_hardware_setup(void)
9352{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009353 int r;
9354
9355 r = kvm_x86_ops->hardware_setup();
9356 if (r != 0)
9357 return r;
9358
Haozhong Zhang35181e82015-10-20 15:39:03 +08009359 if (kvm_has_tsc_control) {
9360 /*
9361 * Make sure the user can only configure tsc_khz values that
9362 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009363 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009364 * be 1 on all machines.
9365 */
9366 u64 max = min(0x7fffffffULL,
9367 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9368 kvm_max_guest_tsc_khz = max;
9369
Haozhong Zhangad7218832015-10-20 15:39:02 +08009370 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009371 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009372
Aaron Lewis139a12c2019-10-21 16:30:25 -07009373 if (boot_cpu_has(X86_FEATURE_XSAVES))
9374 rdmsrl(MSR_IA32_XSS, host_xss);
9375
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009376 kvm_init_msr_list();
9377 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009378}
9379
9380void kvm_arch_hardware_unsetup(void)
9381{
9382 kvm_x86_ops->hardware_unsetup();
9383}
9384
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009385int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009386{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009387 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009388}
9389
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009390bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9391{
9392 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9393}
9394EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9395
9396bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9397{
9398 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9399}
9400
Gleb Natapov54e98182012-08-05 15:58:32 +03009401struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009402EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009403
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009404int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
9405{
9406 struct page *page;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009407 int r;
9408
Avi Kivity9aabc88f2010-07-29 15:11:50 +03009409 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
David Hildenbrand26de7982017-08-24 20:51:25 +02009410 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03009411 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009412 else
Avi Kivitya4535292008-04-13 17:54:35 +03009413 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009414
9415 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9416 if (!page) {
9417 r = -ENOMEM;
9418 goto fail;
9419 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009420 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009421
Zachary Amsdencc578282012-02-03 15:43:50 -02009422 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10009423
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009424 r = kvm_mmu_create(vcpu);
9425 if (r < 0)
9426 goto fail_free_pio_data;
9427
David Hildenbrand26de7982017-08-24 20:51:25 +02009428 if (irqchip_in_kernel(vcpu->kvm)) {
Suthikulpanit, Suravee2cf9af02019-09-13 19:00:49 +00009429 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu->kvm);
Sean Christopherson39497d72019-04-17 10:15:32 -07009430 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009431 if (r < 0)
9432 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03009433 } else
9434 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009435
Huang Ying890ca9a2009-05-11 16:48:15 +08009436 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
Ben Gardon254272c2019-02-11 11:02:50 -08009437 GFP_KERNEL_ACCOUNT);
Huang Ying890ca9a2009-05-11 16:48:15 +08009438 if (!vcpu->arch.mce_banks) {
9439 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08009440 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08009441 }
9442 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9443
Ben Gardon254272c2019-02-11 11:02:50 -08009444 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9445 GFP_KERNEL_ACCOUNT)) {
Wei Yongjunf1797352013-04-18 07:41:00 +08009446 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009447 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08009448 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009449
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009450 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01009451
Paolo Bonzini4344ee92013-10-02 16:06:16 +02009452 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02009453
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03009454 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9455
Radim Krčmář74545702015-04-27 15:11:25 +02009456 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9457
Gleb Natapovaf585b92010-10-14 11:22:46 +02009458 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009459 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009460
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009461 vcpu->arch.pending_external_vector = -1;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009462 vcpu->arch.preempted_in_kernel = false;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009463
Andrey Smetanin5c9194122015-11-10 15:36:34 +03009464 kvm_hv_vcpu_init(vcpu);
9465
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009466 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009467
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009468fail_free_mce_banks:
9469 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08009470fail_free_lapic:
9471 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009472fail_mmu_destroy:
9473 kvm_mmu_destroy(vcpu);
9474fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009475 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009476fail:
9477 return r;
9478}
9479
9480void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
9481{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009482 int idx;
9483
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009484 kvm_hv_vcpu_uninit(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009485 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08009486 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009487 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009488 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009489 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009490 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009491 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02009492 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03009493 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009494}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009495
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009496void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9497{
Like Xub35e5542019-10-27 18:52:43 +08009498 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9499
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009500 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009501 if (pmu->version && unlikely(pmu->event_count)) {
9502 pmu->need_cleanup = true;
9503 kvm_make_request(KVM_REQ_PMU, vcpu);
9504 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009505 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009506}
9507
Carsten Ottee08b9632012-01-04 10:25:20 +01009508int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009509{
Carsten Ottee08b9632012-01-04 10:25:20 +01009510 if (type)
9511 return -EINVAL;
9512
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009513 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009514 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009515 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009516 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009517 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009518 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009519
Sheng Yang5550af42008-10-15 20:15:06 +08009520 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9521 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009522 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9523 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9524 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009525
Jan Kiszka038f8c12011-02-04 10:49:11 +01009526 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009527 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009528 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9529
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009530 kvm->arch.kvmclock_offset = -ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009531 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009532
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009533 kvm->arch.guest_can_read_msr_platform_info = true;
9534
Andrew Jones7e44e442014-02-28 12:52:54 +01009535 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009536 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009537
Roman Kagancbc02362018-02-01 16:48:31 +03009538 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009539 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009540 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009541
Sean Christopherson92735b12019-08-02 15:06:17 -07009542 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009543}
9544
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009545int kvm_arch_post_init_vm(struct kvm *kvm)
9546{
9547 return kvm_mmu_post_init_vm(kvm);
9548}
9549
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009550static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9551{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009552 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009553 kvm_mmu_unload(vcpu);
9554 vcpu_put(vcpu);
9555}
9556
9557static void kvm_free_vcpus(struct kvm *kvm)
9558{
9559 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009560 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009561
9562 /*
9563 * Unpin any mmu pages first.
9564 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009565 kvm_for_each_vcpu(i, vcpu, kvm) {
9566 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009567 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009568 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009569 kvm_for_each_vcpu(i, vcpu, kvm)
9570 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009571
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009572 mutex_lock(&kvm->lock);
9573 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9574 kvm->vcpus[i] = NULL;
9575
9576 atomic_set(&kvm->online_vcpus, 0);
9577 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009578}
9579
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009580void kvm_arch_sync_events(struct kvm *kvm)
9581{
Andrew Jones332967a2014-02-28 12:52:55 +01009582 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009583 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009584 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009585}
9586
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009587int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009588{
9589 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02009590 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009591 struct kvm_memslots *slots = kvm_memslots(kvm);
9592 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009593
9594 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009595 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9596 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009597
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009598 slot = id_to_memslot(slots, id);
9599 if (size) {
Paolo Bonzinib21629d2016-06-01 14:09:18 +02009600 if (slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009601 return -EEXIST;
9602
9603 /*
9604 * MAP_SHARED to prevent internal slot pages from being moved
9605 * by fork()/COW.
9606 */
9607 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9608 MAP_SHARED | MAP_ANONYMOUS, 0);
9609 if (IS_ERR((void *)hva))
9610 return PTR_ERR((void *)hva);
9611 } else {
9612 if (!slot->npages)
9613 return 0;
9614
9615 hva = 0;
9616 }
9617
9618 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009619 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009620 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009621
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009622 m.slot = id | (i << 16);
9623 m.flags = 0;
9624 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009625 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009626 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009627 r = __kvm_set_memory_region(kvm, &m);
9628 if (r < 0)
9629 return r;
9630 }
9631
Eric Biggers103c7632018-01-31 17:30:21 -08009632 if (!size)
9633 vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009634
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009635 return 0;
9636}
9637EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9638
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009639int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009640{
9641 int r;
9642
9643 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009644 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009645 mutex_unlock(&kvm->slots_lock);
9646
9647 return r;
9648}
9649EXPORT_SYMBOL_GPL(x86_set_memory_region);
9650
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009651void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9652{
9653 kvm_mmu_pre_destroy_vm(kvm);
9654}
9655
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009656void kvm_arch_destroy_vm(struct kvm *kvm)
9657{
Andrew Honig27469d22013-04-18 09:38:14 -07009658 if (current->mm == kvm->mm) {
9659 /*
9660 * Free memory regions allocated on behalf of userspace,
9661 * unless the the memory map has changed due to process exit
9662 * or fd copying.
9663 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009664 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
9665 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
9666 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07009667 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009668 if (kvm_x86_ops->vm_destroy)
9669 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009670 kvm_pic_destroy(kvm);
9671 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009672 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009673 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009674 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009675 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009676 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009677 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009678}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009679
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309680void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009681 struct kvm_memory_slot *dont)
9682{
9683 int i;
9684
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009685 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
9686 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009687 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009688 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009689 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009690 if (i == 0)
9691 continue;
9692
9693 if (!dont || free->arch.lpage_info[i - 1] !=
9694 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009695 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009696 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009697 }
9698 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009699
9700 kvm_page_track_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009701}
9702
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309703int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
9704 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009705{
9706 int i;
9707
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009708 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009709 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009710 unsigned long ugfn;
9711 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009712 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009713
9714 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9715 slot->base_gfn, level) + 1;
9716
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009717 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009718 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009719 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009720 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009721 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009722 if (i == 0)
9723 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009724
Ben Gardon254272c2019-02-11 11:02:50 -08009725 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009726 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009727 goto out_free;
9728
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009729 slot->arch.lpage_info[i - 1] = linfo;
9730
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009731 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009732 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009733 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009734 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009735 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9736 /*
9737 * If the gfn and userspace address are not aligned wrt each
9738 * other, or if explicitly asked to, disable large page
9739 * support for this slot
9740 */
9741 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9742 !kvm_largepages_enabled()) {
9743 unsigned long j;
9744
9745 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009746 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009747 }
9748 }
9749
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009750 if (kvm_page_track_create_memslot(slot, npages))
9751 goto out_free;
9752
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009753 return 0;
9754
9755out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009756 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009757 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009758 slot->arch.rmap[i] = NULL;
9759 if (i == 0)
9760 continue;
9761
Thomas Huth548ef282015-02-24 21:29:25 +01009762 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009763 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009764 }
9765 return -ENOMEM;
9766}
9767
Sean Christopherson15248252019-02-05 12:54:17 -08009768void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009769{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009770 /*
9771 * memslots->generation has been incremented.
9772 * mmio generation may have reached its maximum value.
9773 */
Sean Christopherson15248252019-02-05 12:54:17 -08009774 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009775}
9776
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009777int kvm_arch_prepare_memory_region(struct kvm *kvm,
9778 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009779 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009780 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009781{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009782 return 0;
9783}
9784
Kai Huang88178fd2015-01-28 10:54:27 +08009785static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9786 struct kvm_memory_slot *new)
9787{
9788 /* Still write protect RO slot */
9789 if (new->flags & KVM_MEM_READONLY) {
9790 kvm_mmu_slot_remove_write_access(kvm, new);
9791 return;
9792 }
9793
9794 /*
9795 * Call kvm_x86_ops dirty logging hooks when they are valid.
9796 *
9797 * kvm_x86_ops->slot_disable_log_dirty is called when:
9798 *
9799 * - KVM_MR_CREATE with dirty logging is disabled
9800 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9801 *
9802 * The reason is, in case of PML, we need to set D-bit for any slots
9803 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +08009804 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +08009805 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +08009806 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +08009807 * logging disabled for memory slots.
9808 *
9809 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
9810 * to dirty logging mode.
9811 *
9812 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
9813 *
9814 * In case of write protect:
9815 *
9816 * Write protect all pages for dirty logging.
9817 *
9818 * All the sptes including the large sptes which point to this
9819 * slot are set to readonly. We can not create any new large
9820 * spte on this slot until the end of the logging.
9821 *
9822 * See the comments in fast_page_fault().
9823 */
9824 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9825 if (kvm_x86_ops->slot_enable_log_dirty)
9826 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9827 else
9828 kvm_mmu_slot_remove_write_access(kvm, new);
9829 } else {
9830 if (kvm_x86_ops->slot_disable_log_dirty)
9831 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
9832 }
9833}
9834
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009835void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009836 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009837 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009838 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009839 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009840{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08009841 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +08009842 kvm_mmu_change_mmu_pages(kvm,
9843 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +08009844
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009845 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009846 * Dirty logging tracks sptes in 4k granularity, meaning that large
9847 * sptes have to be split. If live migration is successful, the guest
9848 * in the source machine will be destroyed and large sptes will be
9849 * created in the destination. However, if the guest continues to run
9850 * in the source machine (for example if live migration fails), small
9851 * sptes will remain around and cause bad performance.
9852 *
9853 * Scan sptes if dirty logging has been stopped, dropping those
9854 * which can be collapsed into a single large-page spte. Later
9855 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -07009856 *
9857 * There is no need to do this in any of the following cases:
9858 * CREATE: No dirty mappings will already exist.
9859 * MOVE/DELETE: The old mappings will already have been cleaned up by
9860 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009861 */
Sean Christopherson319109a2019-09-11 12:19:52 -07009862 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009863 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
9864 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
9865 kvm_mmu_zap_collapsible_sptes(kvm, new);
9866
9867 /*
Kai Huang88178fd2015-01-28 10:54:27 +08009868 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08009869 *
Kai Huang88178fd2015-01-28 10:54:27 +08009870 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
9871 * been zapped so no dirty logging staff is needed for old slot. For
9872 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
9873 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009874 *
9875 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009876 */
Kai Huang88178fd2015-01-28 10:54:27 +08009877 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009878 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08009879}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009880
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009881void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009882{
Sean Christopherson7390de12019-02-05 13:01:31 -08009883 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009884}
9885
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009886void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
9887 struct kvm_memory_slot *slot)
9888{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +08009889 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009890}
9891
Liran Alone6c67d82018-09-04 10:56:52 +03009892static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
9893{
9894 return (is_guest_mode(vcpu) &&
9895 kvm_x86_ops->guest_apic_has_interrupt &&
9896 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
9897}
9898
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009899static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
9900{
9901 if (!list_empty_careful(&vcpu->async_pf.done))
9902 return true;
9903
9904 if (kvm_apic_has_events(vcpu))
9905 return true;
9906
9907 if (vcpu->arch.pv.pv_unhalted)
9908 return true;
9909
Wanpeng Lia5f01f82017-09-13 04:04:01 -07009910 if (vcpu->arch.exception.pending)
9911 return true;
9912
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009913 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9914 (vcpu->arch.nmi_pending &&
9915 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009916 return true;
9917
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009918 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
9919 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +02009920 return true;
9921
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009922 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +03009923 (kvm_cpu_has_interrupt(vcpu) ||
9924 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009925 return true;
9926
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009927 if (kvm_hv_has_stimer_pending(vcpu))
9928 return true;
9929
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009930 return false;
9931}
9932
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009933int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
9934{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009935 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009936}
Zhang Xiantao57361992007-12-17 14:21:40 +08009937
Wanpeng Li17e433b2019-08-05 10:03:19 +08009938bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
9939{
9940 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
9941 return true;
9942
9943 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9944 kvm_test_request(KVM_REQ_SMI, vcpu) ||
9945 kvm_test_request(KVM_REQ_EVENT, vcpu))
9946 return true;
9947
9948 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
9949 return true;
9950
9951 return false;
9952}
9953
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009954bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
9955{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009956 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009957}
9958
Christoffer Dallb6d33832012-03-08 16:44:24 -05009959int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08009960{
Christoffer Dallb6d33832012-03-08 16:44:24 -05009961 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08009962}
Gleb Natapov78646122009-03-23 12:12:11 +02009963
9964int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
9965{
9966 return kvm_x86_ops->interrupt_allowed(vcpu);
9967}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009968
Nadav Amit82b32772014-11-02 11:54:45 +02009969unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
9970{
9971 if (is_64_bit_mode(vcpu))
9972 return kvm_rip_read(vcpu);
9973 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
9974 kvm_rip_read(vcpu));
9975}
9976EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
9977
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009978bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
9979{
Nadav Amit82b32772014-11-02 11:54:45 +02009980 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009981}
9982EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
9983
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009984unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
9985{
9986 unsigned long rflags;
9987
9988 rflags = kvm_x86_ops->get_rflags(vcpu);
9989 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01009990 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009991 return rflags;
9992}
9993EXPORT_SYMBOL_GPL(kvm_get_rflags);
9994
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009995static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009996{
9997 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009998 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01009999 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010000 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010001}
10002
10003void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10004{
10005 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010006 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010007}
10008EXPORT_SYMBOL_GPL(kvm_set_rflags);
10009
Gleb Natapov56028d02010-10-17 18:13:42 +020010010void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10011{
10012 int r;
10013
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010014 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010015 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010016 return;
10017
10018 r = kvm_mmu_reload(vcpu);
10019 if (unlikely(r))
10020 return;
10021
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010022 if (!vcpu->arch.mmu->direct_map &&
10023 work->arch.cr3 != vcpu->arch.mmu->get_cr3(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010024 return;
10025
Sean Christopherson736c2912019-12-06 15:57:14 -080010026 vcpu->arch.mmu->page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010027}
10028
Gleb Natapovaf585b92010-10-14 11:22:46 +020010029static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10030{
10031 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10032}
10033
10034static inline u32 kvm_async_pf_next_probe(u32 key)
10035{
10036 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10037}
10038
10039static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10040{
10041 u32 key = kvm_async_pf_hash_fn(gfn);
10042
10043 while (vcpu->arch.apf.gfns[key] != ~0)
10044 key = kvm_async_pf_next_probe(key);
10045
10046 vcpu->arch.apf.gfns[key] = gfn;
10047}
10048
10049static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10050{
10051 int i;
10052 u32 key = kvm_async_pf_hash_fn(gfn);
10053
10054 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010055 (vcpu->arch.apf.gfns[key] != gfn &&
10056 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010057 key = kvm_async_pf_next_probe(key);
10058
10059 return key;
10060}
10061
10062bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10063{
10064 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10065}
10066
10067static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10068{
10069 u32 i, j, k;
10070
10071 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10072 while (true) {
10073 vcpu->arch.apf.gfns[i] = ~0;
10074 do {
10075 j = kvm_async_pf_next_probe(j);
10076 if (vcpu->arch.apf.gfns[j] == ~0)
10077 return;
10078 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10079 /*
10080 * k lies cyclically in ]i,j]
10081 * | i.k.j |
10082 * |....j i.k.| or |.k..j i...|
10083 */
10084 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10085 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10086 i = j;
10087 }
10088}
10089
Gleb Natapov7c907052010-10-14 11:22:53 +020010090static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10091{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010092
10093 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10094 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010095}
10096
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010097static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10098{
10099
10100 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10101 sizeof(u32));
10102}
10103
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010104static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10105{
10106 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10107 return false;
10108
10109 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10110 (vcpu->arch.apf.send_user_only &&
10111 kvm_x86_ops->get_cpl(vcpu) == 0))
10112 return false;
10113
10114 return true;
10115}
10116
10117bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10118{
10119 if (unlikely(!lapic_in_kernel(vcpu) ||
10120 kvm_event_needs_reinjection(vcpu) ||
10121 vcpu->arch.exception.pending))
10122 return false;
10123
10124 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10125 return false;
10126
10127 /*
10128 * If interrupts are off we cannot even use an artificial
10129 * halt state.
10130 */
10131 return kvm_x86_ops->interrupt_allowed(vcpu);
10132}
10133
Gleb Natapovaf585b92010-10-14 11:22:46 +020010134void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10135 struct kvm_async_pf *work)
10136{
Avi Kivity6389ee92010-11-29 16:12:30 +020010137 struct x86_exception fault;
10138
Sean Christopherson736c2912019-12-06 15:57:14 -080010139 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010140 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010141
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010142 if (kvm_can_deliver_async_pf(vcpu) &&
10143 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010144 fault.vector = PF_VECTOR;
10145 fault.error_code_valid = true;
10146 fault.error_code = 0;
10147 fault.nested_page_fault = false;
10148 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010149 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010150 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010151 } else {
10152 /*
10153 * It is not possible to deliver a paravirtualized asynchronous
10154 * page fault, but putting the guest in an artificial halt state
10155 * can be beneficial nevertheless: if an interrupt arrives, we
10156 * can deliver it timely and perhaps the guest will schedule
10157 * another process. When the instruction that triggered a page
10158 * fault is retried, hopefully the page will be ready in the host.
10159 */
10160 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010161 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010162}
10163
10164void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10165 struct kvm_async_pf *work)
10166{
Avi Kivity6389ee92010-11-29 16:12:30 +020010167 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010168 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010169
chai wenf2e10662013-10-14 22:22:33 +080010170 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010171 work->arch.token = ~0; /* broadcast wakeup */
10172 else
10173 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010174 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010175
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010176 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10177 !apf_get_user(vcpu, &val)) {
10178 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10179 vcpu->arch.exception.pending &&
10180 vcpu->arch.exception.nr == PF_VECTOR &&
10181 !apf_put_user(vcpu, 0)) {
10182 vcpu->arch.exception.injected = false;
10183 vcpu->arch.exception.pending = false;
10184 vcpu->arch.exception.nr = 0;
10185 vcpu->arch.exception.has_error_code = false;
10186 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010187 vcpu->arch.exception.has_payload = false;
10188 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010189 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10190 fault.vector = PF_VECTOR;
10191 fault.error_code_valid = true;
10192 fault.error_code = 0;
10193 fault.nested_page_fault = false;
10194 fault.address = work->arch.token;
10195 fault.async_page_fault = true;
10196 kvm_inject_page_fault(vcpu, &fault);
10197 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010198 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010199 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010200 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010201}
10202
10203bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10204{
10205 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10206 return true;
10207 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010208 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010209}
10210
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010211void kvm_arch_start_assignment(struct kvm *kvm)
10212{
10213 atomic_inc(&kvm->arch.assigned_device_count);
10214}
10215EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10216
10217void kvm_arch_end_assignment(struct kvm *kvm)
10218{
10219 atomic_dec(&kvm->arch.assigned_device_count);
10220}
10221EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10222
10223bool kvm_arch_has_assigned_device(struct kvm *kvm)
10224{
10225 return atomic_read(&kvm->arch.assigned_device_count);
10226}
10227EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10228
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010229void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10230{
10231 atomic_inc(&kvm->arch.noncoherent_dma_count);
10232}
10233EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10234
10235void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10236{
10237 atomic_dec(&kvm->arch.noncoherent_dma_count);
10238}
10239EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10240
10241bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10242{
10243 return atomic_read(&kvm->arch.noncoherent_dma_count);
10244}
10245EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10246
Alex Williamson14717e22016-05-05 11:58:35 -060010247bool kvm_arch_has_irq_bypass(void)
10248{
Sean Christopherson92735b12019-08-02 15:06:17 -070010249 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010250}
10251
Feng Wu87276882015-09-18 22:29:40 +080010252int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10253 struct irq_bypass_producer *prod)
10254{
10255 struct kvm_kernel_irqfd *irqfd =
10256 container_of(cons, struct kvm_kernel_irqfd, consumer);
10257
Alex Williamson14717e22016-05-05 11:58:35 -060010258 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010259
Alex Williamson14717e22016-05-05 11:58:35 -060010260 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10261 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010262}
10263
10264void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10265 struct irq_bypass_producer *prod)
10266{
10267 int ret;
10268 struct kvm_kernel_irqfd *irqfd =
10269 container_of(cons, struct kvm_kernel_irqfd, consumer);
10270
Feng Wu87276882015-09-18 22:29:40 +080010271 WARN_ON(irqfd->producer != prod);
10272 irqfd->producer = NULL;
10273
10274 /*
10275 * When producer of consumer is unregistered, we change back to
10276 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010277 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010278 * int this case doesn't want to receive the interrupts.
10279 */
10280 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10281 if (ret)
10282 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10283 " fails: %d\n", irqfd->consumer.token, ret);
10284}
10285
10286int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10287 uint32_t guest_irq, bool set)
10288{
Feng Wu87276882015-09-18 22:29:40 +080010289 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10290}
10291
Feng Wu520040142016-01-25 16:53:33 +080010292bool kvm_vector_hashing_enabled(void)
10293{
10294 return vector_hashing;
10295}
10296EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
10297
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010298bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10299{
10300 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10301}
10302EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10303
10304
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010305EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010306EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010307EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10308EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10309EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10310EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010311EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010312EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010313EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010314EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010315EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010316EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010317EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010318EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010319EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010320EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010321EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010322EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010323EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10324EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);