Commit | Line | Data |
---|---|---|
20c8ccb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
043405e1 CO |
2 | /* |
3 | * Kernel-based Virtual Machine driver for Linux | |
4 | * | |
5 | * derived from drivers/kvm/kvm_main.c | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
4d5c5d0f BAY |
8 | * Copyright (C) 2008 Qumranet, Inc. |
9 | * Copyright IBM Corporation, 2008 | |
9611c187 | 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
043405e1 CO |
11 | * |
12 | * Authors: | |
13 | * Avi Kivity <avi@qumranet.com> | |
14 | * Yaniv Kamay <yaniv@qumranet.com> | |
4d5c5d0f BAY |
15 | * Amit Shah <amit.shah@qumranet.com> |
16 | * Ben-Ami Yassour <benami@il.ibm.com> | |
043405e1 CO |
17 | */ |
18 | ||
edf88417 | 19 | #include <linux/kvm_host.h> |
313a3dc7 | 20 | #include "irq.h" |
88197e6a | 21 | #include "ioapic.h" |
1d737c8a | 22 | #include "mmu.h" |
7837699f | 23 | #include "i8254.h" |
37817f29 | 24 | #include "tss.h" |
5fdbf976 | 25 | #include "kvm_cache_regs.h" |
2f728d66 | 26 | #include "kvm_emulate.h" |
26eef70c | 27 | #include "x86.h" |
00b27a3e | 28 | #include "cpuid.h" |
474a5bb9 | 29 | #include "pmu.h" |
e83d5887 | 30 | #include "hyperv.h" |
8df14af4 | 31 | #include "lapic.h" |
23200b7a | 32 | #include "xen.h" |
313a3dc7 | 33 | |
18068523 | 34 | #include <linux/clocksource.h> |
4d5c5d0f | 35 | #include <linux/interrupt.h> |
313a3dc7 CO |
36 | #include <linux/kvm.h> |
37 | #include <linux/fs.h> | |
38 | #include <linux/vmalloc.h> | |
1767e931 PG |
39 | #include <linux/export.h> |
40 | #include <linux/moduleparam.h> | |
0de10343 | 41 | #include <linux/mman.h> |
2bacc55c | 42 | #include <linux/highmem.h> |
19de40a8 | 43 | #include <linux/iommu.h> |
62c476c7 | 44 | #include <linux/intel-iommu.h> |
c8076604 | 45 | #include <linux/cpufreq.h> |
18863bdd | 46 | #include <linux/user-return-notifier.h> |
a983fb23 | 47 | #include <linux/srcu.h> |
5a0e3ad6 | 48 | #include <linux/slab.h> |
ff9d07a0 | 49 | #include <linux/perf_event.h> |
7bee342a | 50 | #include <linux/uaccess.h> |
af585b92 | 51 | #include <linux/hash.h> |
a1b60c1c | 52 | #include <linux/pci.h> |
16e8d74d MT |
53 | #include <linux/timekeeper_internal.h> |
54 | #include <linux/pvclock_gtod.h> | |
87276880 FW |
55 | #include <linux/kvm_irqfd.h> |
56 | #include <linux/irqbypass.h> | |
3905f9ad | 57 | #include <linux/sched/stat.h> |
0c5f81da | 58 | #include <linux/sched/isolation.h> |
d0ec49d4 | 59 | #include <linux/mem_encrypt.h> |
72c3c0fe | 60 | #include <linux/entry-kvm.h> |
3905f9ad | 61 | |
aec51dc4 | 62 | #include <trace/events/kvm.h> |
2ed152af | 63 | |
24f1e32c | 64 | #include <asm/debugreg.h> |
d825ed0a | 65 | #include <asm/msr.h> |
a5f61300 | 66 | #include <asm/desc.h> |
890ca9ae | 67 | #include <asm/mce.h> |
f89e32e0 | 68 | #include <linux/kernel_stat.h> |
78f7f1e5 | 69 | #include <asm/fpu/internal.h> /* Ugh! */ |
1d5f066e | 70 | #include <asm/pvclock.h> |
217fc9cf | 71 | #include <asm/div64.h> |
efc64404 | 72 | #include <asm/irq_remapping.h> |
b0c39dc6 | 73 | #include <asm/mshyperv.h> |
0092e434 | 74 | #include <asm/hypervisor.h> |
9715092f | 75 | #include <asm/tlbflush.h> |
bf8c55d8 | 76 | #include <asm/intel_pt.h> |
b3dc0695 | 77 | #include <asm/emulate_prefix.h> |
dd2cb348 | 78 | #include <clocksource/hyperv_timer.h> |
043405e1 | 79 | |
d1898b73 DH |
80 | #define CREATE_TRACE_POINTS |
81 | #include "trace.h" | |
82 | ||
313a3dc7 | 83 | #define MAX_IO_MSRS 256 |
890ca9ae | 84 | #define KVM_MAX_MCE_BANKS 32 |
c45dcc71 AR |
85 | u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; |
86 | EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); | |
890ca9ae | 87 | |
0f65dd70 | 88 | #define emul_to_vcpu(ctxt) \ |
c9b8b07c | 89 | ((struct kvm_vcpu *)(ctxt)->vcpu) |
0f65dd70 | 90 | |
50a37eb4 JR |
91 | /* EFER defaults: |
92 | * - enable syscall per default because its emulated by KVM | |
93 | * - enable LME and LMA per default on 64 bit KVM | |
94 | */ | |
95 | #ifdef CONFIG_X86_64 | |
1260edbe LJ |
96 | static |
97 | u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); | |
50a37eb4 | 98 | #else |
1260edbe | 99 | static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); |
50a37eb4 | 100 | #endif |
313a3dc7 | 101 | |
b11306b5 SC |
102 | static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; |
103 | ||
c519265f RK |
104 | #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ |
105 | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) | |
37131313 | 106 | |
cb142eb7 | 107 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
7460fb4a | 108 | static void process_nmi(struct kvm_vcpu *vcpu); |
1f7becf1 | 109 | static void process_smi(struct kvm_vcpu *vcpu); |
ee2cd4b7 | 110 | static void enter_smm(struct kvm_vcpu *vcpu); |
6addfc42 | 111 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); |
01643c51 KH |
112 | static void store_regs(struct kvm_vcpu *vcpu); |
113 | static int sync_regs(struct kvm_vcpu *vcpu); | |
674eea0f | 114 | |
afaf0b2f | 115 | struct kvm_x86_ops kvm_x86_ops __read_mostly; |
5fdbf976 | 116 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
97896d04 | 117 | |
9af5471b JB |
118 | #define KVM_X86_OP(func) \ |
119 | DEFINE_STATIC_CALL_NULL(kvm_x86_##func, \ | |
120 | *(((struct kvm_x86_ops *)0)->func)); | |
121 | #define KVM_X86_OP_NULL KVM_X86_OP | |
122 | #include <asm/kvm-x86-ops.h> | |
123 | EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits); | |
124 | EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg); | |
125 | EXPORT_STATIC_CALL_GPL(kvm_x86_tlb_flush_current); | |
126 | ||
893590c7 | 127 | static bool __read_mostly ignore_msrs = 0; |
476bc001 | 128 | module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR); |
ed85c068 | 129 | |
d855066f | 130 | bool __read_mostly report_ignored_msrs = true; |
fab0aa3b | 131 | module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); |
d855066f | 132 | EXPORT_SYMBOL_GPL(report_ignored_msrs); |
fab0aa3b | 133 | |
4c27625b | 134 | unsigned int min_timer_period_us = 200; |
9ed96e87 MT |
135 | module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); |
136 | ||
630994b3 MT |
137 | static bool __read_mostly kvmclock_periodic_sync = true; |
138 | module_param(kvmclock_periodic_sync, bool, S_IRUGO); | |
139 | ||
893590c7 | 140 | bool __read_mostly kvm_has_tsc_control; |
92a1f12d | 141 | EXPORT_SYMBOL_GPL(kvm_has_tsc_control); |
893590c7 | 142 | u32 __read_mostly kvm_max_guest_tsc_khz; |
92a1f12d | 143 | EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz); |
bc9b961b HZ |
144 | u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; |
145 | EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits); | |
146 | u64 __read_mostly kvm_max_tsc_scaling_ratio; | |
147 | EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio); | |
64672c95 YJ |
148 | u64 __read_mostly kvm_default_tsc_scaling_ratio; |
149 | EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); | |
fe6b6bc8 CQ |
150 | bool __read_mostly kvm_has_bus_lock_exit; |
151 | EXPORT_SYMBOL_GPL(kvm_has_bus_lock_exit); | |
92a1f12d | 152 | |
cc578287 | 153 | /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ |
893590c7 | 154 | static u32 __read_mostly tsc_tolerance_ppm = 250; |
cc578287 ZA |
155 | module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); |
156 | ||
c3941d9e SC |
157 | /* |
158 | * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables | |
159 | * adaptive tuning starting from default advancment of 1000ns. '0' disables | |
160 | * advancement entirely. Any other value is used as-is and disables adaptive | |
161 | * tuning, i.e. allows priveleged userspace to set an exact advancement time. | |
162 | */ | |
163 | static int __read_mostly lapic_timer_advance_ns = -1; | |
0e6edceb | 164 | module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR); |
d0659d94 | 165 | |
52004014 FW |
166 | static bool __read_mostly vector_hashing = true; |
167 | module_param(vector_hashing, bool, S_IRUGO); | |
168 | ||
c4ae60e4 LA |
169 | bool __read_mostly enable_vmware_backdoor = false; |
170 | module_param(enable_vmware_backdoor, bool, S_IRUGO); | |
171 | EXPORT_SYMBOL_GPL(enable_vmware_backdoor); | |
172 | ||
6c86eedc WL |
173 | static bool __read_mostly force_emulation_prefix = false; |
174 | module_param(force_emulation_prefix, bool, S_IRUGO); | |
175 | ||
0c5f81da WL |
176 | int __read_mostly pi_inject_timer = -1; |
177 | module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); | |
178 | ||
7e34fbd0 SC |
179 | /* |
180 | * Restoring the host value for MSRs that are only consumed when running in | |
181 | * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU | |
182 | * returns to userspace, i.e. the kernel can run with the guest's value. | |
183 | */ | |
184 | #define KVM_MAX_NR_USER_RETURN_MSRS 16 | |
18863bdd | 185 | |
7e34fbd0 | 186 | struct kvm_user_return_msrs_global { |
18863bdd | 187 | int nr; |
7e34fbd0 | 188 | u32 msrs[KVM_MAX_NR_USER_RETURN_MSRS]; |
18863bdd AK |
189 | }; |
190 | ||
7e34fbd0 | 191 | struct kvm_user_return_msrs { |
18863bdd AK |
192 | struct user_return_notifier urn; |
193 | bool registered; | |
7e34fbd0 | 194 | struct kvm_user_return_msr_values { |
2bf78fa7 SY |
195 | u64 host; |
196 | u64 curr; | |
7e34fbd0 | 197 | } values[KVM_MAX_NR_USER_RETURN_MSRS]; |
18863bdd AK |
198 | }; |
199 | ||
7e34fbd0 SC |
200 | static struct kvm_user_return_msrs_global __read_mostly user_return_msrs_global; |
201 | static struct kvm_user_return_msrs __percpu *user_return_msrs; | |
18863bdd | 202 | |
cfc48181 SC |
203 | #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ |
204 | | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ | |
205 | | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | |
206 | | XFEATURE_MASK_PKRU) | |
207 | ||
91661989 SC |
208 | u64 __read_mostly host_efer; |
209 | EXPORT_SYMBOL_GPL(host_efer); | |
210 | ||
b96e6506 | 211 | bool __read_mostly allow_smaller_maxphyaddr = 0; |
3edd6839 MG |
212 | EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr); |
213 | ||
86137773 TL |
214 | u64 __read_mostly host_xss; |
215 | EXPORT_SYMBOL_GPL(host_xss); | |
408e9a31 PB |
216 | u64 __read_mostly supported_xss; |
217 | EXPORT_SYMBOL_GPL(supported_xss); | |
139a12cf | 218 | |
417bc304 | 219 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
812756a8 EGE |
220 | VCPU_STAT("pf_fixed", pf_fixed), |
221 | VCPU_STAT("pf_guest", pf_guest), | |
222 | VCPU_STAT("tlb_flush", tlb_flush), | |
223 | VCPU_STAT("invlpg", invlpg), | |
224 | VCPU_STAT("exits", exits), | |
225 | VCPU_STAT("io_exits", io_exits), | |
226 | VCPU_STAT("mmio_exits", mmio_exits), | |
227 | VCPU_STAT("signal_exits", signal_exits), | |
228 | VCPU_STAT("irq_window", irq_window_exits), | |
229 | VCPU_STAT("nmi_window", nmi_window_exits), | |
230 | VCPU_STAT("halt_exits", halt_exits), | |
231 | VCPU_STAT("halt_successful_poll", halt_successful_poll), | |
232 | VCPU_STAT("halt_attempted_poll", halt_attempted_poll), | |
233 | VCPU_STAT("halt_poll_invalid", halt_poll_invalid), | |
234 | VCPU_STAT("halt_wakeup", halt_wakeup), | |
235 | VCPU_STAT("hypercalls", hypercalls), | |
236 | VCPU_STAT("request_irq", request_irq_exits), | |
237 | VCPU_STAT("irq_exits", irq_exits), | |
238 | VCPU_STAT("host_state_reload", host_state_reload), | |
239 | VCPU_STAT("fpu_reload", fpu_reload), | |
240 | VCPU_STAT("insn_emulation", insn_emulation), | |
241 | VCPU_STAT("insn_emulation_fail", insn_emulation_fail), | |
242 | VCPU_STAT("irq_injections", irq_injections), | |
243 | VCPU_STAT("nmi_injections", nmi_injections), | |
244 | VCPU_STAT("req_event", req_event), | |
245 | VCPU_STAT("l1d_flush", l1d_flush), | |
cb953129 DM |
246 | VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns), |
247 | VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), | |
43c11d91 | 248 | VCPU_STAT("nested_run", nested_run), |
812756a8 EGE |
249 | VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped), |
250 | VM_STAT("mmu_pte_write", mmu_pte_write), | |
812756a8 EGE |
251 | VM_STAT("mmu_pde_zapped", mmu_pde_zapped), |
252 | VM_STAT("mmu_flooded", mmu_flooded), | |
253 | VM_STAT("mmu_recycled", mmu_recycled), | |
254 | VM_STAT("mmu_cache_miss", mmu_cache_miss), | |
255 | VM_STAT("mmu_unsync", mmu_unsync), | |
256 | VM_STAT("remote_tlb_flush", remote_tlb_flush), | |
257 | VM_STAT("largepages", lpages, .mode = 0444), | |
258 | VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444), | |
259 | VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions), | |
417bc304 HB |
260 | { NULL } |
261 | }; | |
262 | ||
2acf923e | 263 | u64 __read_mostly host_xcr0; |
cfc48181 SC |
264 | u64 __read_mostly supported_xcr0; |
265 | EXPORT_SYMBOL_GPL(supported_xcr0); | |
2acf923e | 266 | |
80fbd280 | 267 | static struct kmem_cache *x86_fpu_cache; |
b666a4b6 | 268 | |
c9b8b07c SC |
269 | static struct kmem_cache *x86_emulator_cache; |
270 | ||
6abe9c13 PX |
271 | /* |
272 | * When called, it means the previous get/set msr reached an invalid msr. | |
cc4cb017 | 273 | * Return true if we want to ignore/silent this failed msr access. |
6abe9c13 | 274 | */ |
cc4cb017 ML |
275 | static bool kvm_msr_ignored_check(struct kvm_vcpu *vcpu, u32 msr, |
276 | u64 data, bool write) | |
6abe9c13 PX |
277 | { |
278 | const char *op = write ? "wrmsr" : "rdmsr"; | |
279 | ||
280 | if (ignore_msrs) { | |
281 | if (report_ignored_msrs) | |
d383b314 TI |
282 | kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n", |
283 | op, msr, data); | |
6abe9c13 | 284 | /* Mask the error */ |
cc4cb017 | 285 | return true; |
6abe9c13 | 286 | } else { |
d383b314 TI |
287 | kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n", |
288 | op, msr, data); | |
cc4cb017 | 289 | return false; |
6abe9c13 PX |
290 | } |
291 | } | |
292 | ||
c9b8b07c SC |
293 | static struct kmem_cache *kvm_alloc_emulator_cache(void) |
294 | { | |
06add254 SC |
295 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
296 | unsigned int size = sizeof(struct x86_emulate_ctxt); | |
297 | ||
298 | return kmem_cache_create_usercopy("x86_emulator", size, | |
c9b8b07c | 299 | __alignof__(struct x86_emulate_ctxt), |
06add254 SC |
300 | SLAB_ACCOUNT, useroffset, |
301 | size - useroffset, NULL); | |
c9b8b07c SC |
302 | } |
303 | ||
b6785def | 304 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
d6aa1000 | 305 | |
af585b92 GN |
306 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
307 | { | |
308 | int i; | |
dd03bcaa | 309 | for (i = 0; i < ASYNC_PF_PER_VCPU; i++) |
af585b92 GN |
310 | vcpu->arch.apf.gfns[i] = ~0; |
311 | } | |
312 | ||
18863bdd AK |
313 | static void kvm_on_user_return(struct user_return_notifier *urn) |
314 | { | |
315 | unsigned slot; | |
7e34fbd0 SC |
316 | struct kvm_user_return_msrs *msrs |
317 | = container_of(urn, struct kvm_user_return_msrs, urn); | |
318 | struct kvm_user_return_msr_values *values; | |
1650b4eb IA |
319 | unsigned long flags; |
320 | ||
321 | /* | |
322 | * Disabling irqs at this point since the following code could be | |
323 | * interrupted and executed through kvm_arch_hardware_disable() | |
324 | */ | |
325 | local_irq_save(flags); | |
7e34fbd0 SC |
326 | if (msrs->registered) { |
327 | msrs->registered = false; | |
1650b4eb IA |
328 | user_return_notifier_unregister(urn); |
329 | } | |
330 | local_irq_restore(flags); | |
7e34fbd0 SC |
331 | for (slot = 0; slot < user_return_msrs_global.nr; ++slot) { |
332 | values = &msrs->values[slot]; | |
2bf78fa7 | 333 | if (values->host != values->curr) { |
7e34fbd0 | 334 | wrmsrl(user_return_msrs_global.msrs[slot], values->host); |
2bf78fa7 | 335 | values->curr = values->host; |
18863bdd AK |
336 | } |
337 | } | |
18863bdd AK |
338 | } |
339 | ||
7e34fbd0 | 340 | void kvm_define_user_return_msr(unsigned slot, u32 msr) |
2bf78fa7 | 341 | { |
7e34fbd0 SC |
342 | BUG_ON(slot >= KVM_MAX_NR_USER_RETURN_MSRS); |
343 | user_return_msrs_global.msrs[slot] = msr; | |
344 | if (slot >= user_return_msrs_global.nr) | |
345 | user_return_msrs_global.nr = slot + 1; | |
18863bdd | 346 | } |
7e34fbd0 | 347 | EXPORT_SYMBOL_GPL(kvm_define_user_return_msr); |
18863bdd | 348 | |
7e34fbd0 | 349 | static void kvm_user_return_msr_cpu_online(void) |
18863bdd | 350 | { |
05c19c2f | 351 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 352 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
05c19c2f SC |
353 | u64 value; |
354 | int i; | |
18863bdd | 355 | |
7e34fbd0 SC |
356 | for (i = 0; i < user_return_msrs_global.nr; ++i) { |
357 | rdmsrl_safe(user_return_msrs_global.msrs[i], &value); | |
358 | msrs->values[i].host = value; | |
359 | msrs->values[i].curr = value; | |
05c19c2f | 360 | } |
18863bdd AK |
361 | } |
362 | ||
7e34fbd0 | 363 | int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) |
18863bdd | 364 | { |
013f6a5d | 365 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 366 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
8b3c3104 | 367 | int err; |
18863bdd | 368 | |
7e34fbd0 SC |
369 | value = (value & mask) | (msrs->values[slot].host & ~mask); |
370 | if (value == msrs->values[slot].curr) | |
8b3c3104 | 371 | return 0; |
7e34fbd0 | 372 | err = wrmsrl_safe(user_return_msrs_global.msrs[slot], value); |
8b3c3104 AH |
373 | if (err) |
374 | return 1; | |
375 | ||
7e34fbd0 SC |
376 | msrs->values[slot].curr = value; |
377 | if (!msrs->registered) { | |
378 | msrs->urn.on_user_return = kvm_on_user_return; | |
379 | user_return_notifier_register(&msrs->urn); | |
380 | msrs->registered = true; | |
18863bdd | 381 | } |
8b3c3104 | 382 | return 0; |
18863bdd | 383 | } |
7e34fbd0 | 384 | EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); |
18863bdd | 385 | |
13a34e06 | 386 | static void drop_user_return_notifiers(void) |
3548bab5 | 387 | { |
013f6a5d | 388 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 389 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
3548bab5 | 390 | |
7e34fbd0 SC |
391 | if (msrs->registered) |
392 | kvm_on_user_return(&msrs->urn); | |
3548bab5 AK |
393 | } |
394 | ||
6866b83e CO |
395 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
396 | { | |
8a5a87d9 | 397 | return vcpu->arch.apic_base; |
6866b83e CO |
398 | } |
399 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
400 | ||
58871649 JM |
401 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
402 | { | |
403 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); | |
404 | } | |
405 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); | |
406 | ||
58cb628d JK |
407 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
408 | { | |
58871649 JM |
409 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
410 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); | |
a8ac864a | 411 | u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | |
d6321d49 | 412 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); |
58cb628d | 413 | |
58871649 | 414 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
58cb628d | 415 | return 1; |
58871649 JM |
416 | if (!msr_info->host_initiated) { |
417 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) | |
418 | return 1; | |
419 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) | |
420 | return 1; | |
421 | } | |
58cb628d JK |
422 | |
423 | kvm_lapic_set_base(vcpu, msr_info->data); | |
4abaffce | 424 | kvm_recalculate_apic_map(vcpu->kvm); |
58cb628d | 425 | return 0; |
6866b83e CO |
426 | } |
427 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
428 | ||
3ebccdf3 | 429 | asmlinkage __visible noinstr void kvm_spurious_fault(void) |
e3ba45b8 GL |
430 | { |
431 | /* Fault while not rebooting. We want the trace. */ | |
b4fdcf60 | 432 | BUG_ON(!kvm_rebooting); |
e3ba45b8 GL |
433 | } |
434 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); | |
435 | ||
3fd28fce ED |
436 | #define EXCPT_BENIGN 0 |
437 | #define EXCPT_CONTRIBUTORY 1 | |
438 | #define EXCPT_PF 2 | |
439 | ||
440 | static int exception_class(int vector) | |
441 | { | |
442 | switch (vector) { | |
443 | case PF_VECTOR: | |
444 | return EXCPT_PF; | |
445 | case DE_VECTOR: | |
446 | case TS_VECTOR: | |
447 | case NP_VECTOR: | |
448 | case SS_VECTOR: | |
449 | case GP_VECTOR: | |
450 | return EXCPT_CONTRIBUTORY; | |
451 | default: | |
452 | break; | |
453 | } | |
454 | return EXCPT_BENIGN; | |
455 | } | |
456 | ||
d6e8c854 NA |
457 | #define EXCPT_FAULT 0 |
458 | #define EXCPT_TRAP 1 | |
459 | #define EXCPT_ABORT 2 | |
460 | #define EXCPT_INTERRUPT 3 | |
461 | ||
462 | static int exception_type(int vector) | |
463 | { | |
464 | unsigned int mask; | |
465 | ||
466 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) | |
467 | return EXCPT_INTERRUPT; | |
468 | ||
469 | mask = 1 << vector; | |
470 | ||
471 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ | |
472 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) | |
473 | return EXCPT_TRAP; | |
474 | ||
475 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) | |
476 | return EXCPT_ABORT; | |
477 | ||
478 | /* Reserved exceptions will result in fault */ | |
479 | return EXCPT_FAULT; | |
480 | } | |
481 | ||
da998b46 JM |
482 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
483 | { | |
484 | unsigned nr = vcpu->arch.exception.nr; | |
485 | bool has_payload = vcpu->arch.exception.has_payload; | |
486 | unsigned long payload = vcpu->arch.exception.payload; | |
487 | ||
488 | if (!has_payload) | |
489 | return; | |
490 | ||
491 | switch (nr) { | |
f10c729f JM |
492 | case DB_VECTOR: |
493 | /* | |
494 | * "Certain debug exceptions may clear bit 0-3. The | |
495 | * remaining contents of the DR6 register are never | |
496 | * cleared by the processor". | |
497 | */ | |
498 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; | |
499 | /* | |
9a3ecd5e CQ |
500 | * In order to reflect the #DB exception payload in guest |
501 | * dr6, three components need to be considered: active low | |
502 | * bit, FIXED_1 bits and active high bits (e.g. DR6_BD, | |
503 | * DR6_BS and DR6_BT) | |
504 | * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits. | |
505 | * In the target guest dr6: | |
506 | * FIXED_1 bits should always be set. | |
507 | * Active low bits should be cleared if 1-setting in payload. | |
508 | * Active high bits should be set if 1-setting in payload. | |
509 | * | |
510 | * Note, the payload is compatible with the pending debug | |
511 | * exceptions/exit qualification under VMX, that active_low bits | |
512 | * are active high in payload. | |
513 | * So they need to be flipped for DR6. | |
f10c729f | 514 | */ |
9a3ecd5e | 515 | vcpu->arch.dr6 |= DR6_ACTIVE_LOW; |
f10c729f | 516 | vcpu->arch.dr6 |= payload; |
9a3ecd5e | 517 | vcpu->arch.dr6 ^= payload & DR6_ACTIVE_LOW; |
307f1cfa OU |
518 | |
519 | /* | |
520 | * The #DB payload is defined as compatible with the 'pending | |
521 | * debug exceptions' field under VMX, not DR6. While bit 12 is | |
522 | * defined in the 'pending debug exceptions' field (enabled | |
523 | * breakpoint), it is reserved and must be zero in DR6. | |
524 | */ | |
525 | vcpu->arch.dr6 &= ~BIT(12); | |
f10c729f | 526 | break; |
da998b46 JM |
527 | case PF_VECTOR: |
528 | vcpu->arch.cr2 = payload; | |
529 | break; | |
530 | } | |
531 | ||
532 | vcpu->arch.exception.has_payload = false; | |
533 | vcpu->arch.exception.payload = 0; | |
534 | } | |
535 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); | |
536 | ||
3fd28fce | 537 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
ce7ddec4 | 538 | unsigned nr, bool has_error, u32 error_code, |
91e86d22 | 539 | bool has_payload, unsigned long payload, bool reinject) |
3fd28fce ED |
540 | { |
541 | u32 prev_nr; | |
542 | int class1, class2; | |
543 | ||
3842d135 AK |
544 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
545 | ||
664f8e26 | 546 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
3fd28fce | 547 | queue: |
664f8e26 WL |
548 | if (reinject) { |
549 | /* | |
550 | * On vmentry, vcpu->arch.exception.pending is only | |
551 | * true if an event injection was blocked by | |
552 | * nested_run_pending. In that case, however, | |
553 | * vcpu_enter_guest requests an immediate exit, | |
554 | * and the guest shouldn't proceed far enough to | |
555 | * need reinjection. | |
556 | */ | |
557 | WARN_ON_ONCE(vcpu->arch.exception.pending); | |
558 | vcpu->arch.exception.injected = true; | |
91e86d22 JM |
559 | if (WARN_ON_ONCE(has_payload)) { |
560 | /* | |
561 | * A reinjected event has already | |
562 | * delivered its payload. | |
563 | */ | |
564 | has_payload = false; | |
565 | payload = 0; | |
566 | } | |
664f8e26 WL |
567 | } else { |
568 | vcpu->arch.exception.pending = true; | |
569 | vcpu->arch.exception.injected = false; | |
570 | } | |
3fd28fce ED |
571 | vcpu->arch.exception.has_error_code = has_error; |
572 | vcpu->arch.exception.nr = nr; | |
573 | vcpu->arch.exception.error_code = error_code; | |
91e86d22 JM |
574 | vcpu->arch.exception.has_payload = has_payload; |
575 | vcpu->arch.exception.payload = payload; | |
a06230b6 | 576 | if (!is_guest_mode(vcpu)) |
da998b46 | 577 | kvm_deliver_exception_payload(vcpu); |
3fd28fce ED |
578 | return; |
579 | } | |
580 | ||
581 | /* to check exception */ | |
582 | prev_nr = vcpu->arch.exception.nr; | |
583 | if (prev_nr == DF_VECTOR) { | |
584 | /* triple fault -> shutdown */ | |
a8eeb04a | 585 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
3fd28fce ED |
586 | return; |
587 | } | |
588 | class1 = exception_class(prev_nr); | |
589 | class2 = exception_class(nr); | |
590 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) | |
591 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { | |
664f8e26 WL |
592 | /* |
593 | * Generate double fault per SDM Table 5-5. Set | |
594 | * exception.pending = true so that the double fault | |
595 | * can trigger a nested vmexit. | |
596 | */ | |
3fd28fce | 597 | vcpu->arch.exception.pending = true; |
664f8e26 | 598 | vcpu->arch.exception.injected = false; |
3fd28fce ED |
599 | vcpu->arch.exception.has_error_code = true; |
600 | vcpu->arch.exception.nr = DF_VECTOR; | |
601 | vcpu->arch.exception.error_code = 0; | |
c851436a JM |
602 | vcpu->arch.exception.has_payload = false; |
603 | vcpu->arch.exception.payload = 0; | |
3fd28fce ED |
604 | } else |
605 | /* replace previous exception with a new one in a hope | |
606 | that instruction re-execution will regenerate lost | |
607 | exception */ | |
608 | goto queue; | |
609 | } | |
610 | ||
298101da AK |
611 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
612 | { | |
91e86d22 | 613 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
298101da AK |
614 | } |
615 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
616 | ||
ce7ddec4 JR |
617 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
618 | { | |
91e86d22 | 619 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
ce7ddec4 JR |
620 | } |
621 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); | |
622 | ||
4d5523cf PB |
623 | void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
624 | unsigned long payload) | |
f10c729f JM |
625 | { |
626 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); | |
627 | } | |
4d5523cf | 628 | EXPORT_SYMBOL_GPL(kvm_queue_exception_p); |
f10c729f | 629 | |
da998b46 JM |
630 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
631 | u32 error_code, unsigned long payload) | |
632 | { | |
633 | kvm_multiple_exception(vcpu, nr, true, error_code, | |
634 | true, payload, false); | |
635 | } | |
636 | ||
6affcbed | 637 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
c3c91fee | 638 | { |
db8fcefa AP |
639 | if (err) |
640 | kvm_inject_gp(vcpu, 0); | |
641 | else | |
6affcbed KH |
642 | return kvm_skip_emulated_instruction(vcpu); |
643 | ||
644 | return 1; | |
db8fcefa AP |
645 | } |
646 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); | |
8df25a32 | 647 | |
6389ee94 | 648 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
c3c91fee AK |
649 | { |
650 | ++vcpu->stat.pf_guest; | |
adfe20fb WL |
651 | vcpu->arch.exception.nested_apf = |
652 | is_guest_mode(vcpu) && fault->async_page_fault; | |
da998b46 | 653 | if (vcpu->arch.exception.nested_apf) { |
adfe20fb | 654 | vcpu->arch.apf.nested_apf_token = fault->address; |
da998b46 JM |
655 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
656 | } else { | |
657 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, | |
658 | fault->address); | |
659 | } | |
c3c91fee | 660 | } |
27d6c865 | 661 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
c3c91fee | 662 | |
53b3d8e9 SC |
663 | bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, |
664 | struct x86_exception *fault) | |
d4f8cf66 | 665 | { |
0cd665bd | 666 | struct kvm_mmu *fault_mmu; |
53b3d8e9 SC |
667 | WARN_ON_ONCE(fault->vector != PF_VECTOR); |
668 | ||
0cd665bd PB |
669 | fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : |
670 | vcpu->arch.walk_mmu; | |
ef54bcfe | 671 | |
ee1fa209 JS |
672 | /* |
673 | * Invalidate the TLB entry for the faulting address, if it exists, | |
674 | * else the access will fault indefinitely (and to emulate hardware). | |
675 | */ | |
676 | if ((fault->error_code & PFERR_PRESENT_MASK) && | |
677 | !(fault->error_code & PFERR_RSVD_MASK)) | |
678 | kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address, | |
679 | fault_mmu->root_hpa); | |
680 | ||
681 | fault_mmu->inject_page_fault(vcpu, fault); | |
ef54bcfe | 682 | return fault->nested_page_fault; |
d4f8cf66 | 683 | } |
53b3d8e9 | 684 | EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault); |
d4f8cf66 | 685 | |
3419ffc8 SY |
686 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
687 | { | |
7460fb4a AK |
688 | atomic_inc(&vcpu->arch.nmi_queued); |
689 | kvm_make_request(KVM_REQ_NMI, vcpu); | |
3419ffc8 SY |
690 | } |
691 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
692 | ||
298101da AK |
693 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
694 | { | |
91e86d22 | 695 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
298101da AK |
696 | } |
697 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
698 | ||
ce7ddec4 JR |
699 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
700 | { | |
91e86d22 | 701 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
ce7ddec4 JR |
702 | } |
703 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); | |
704 | ||
0a79b009 AK |
705 | /* |
706 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue | |
707 | * a #GP and return false. | |
708 | */ | |
709 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) | |
298101da | 710 | { |
b3646477 | 711 | if (static_call(kvm_x86_get_cpl)(vcpu) <= required_cpl) |
0a79b009 AK |
712 | return true; |
713 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
714 | return false; | |
298101da | 715 | } |
0a79b009 | 716 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
298101da | 717 | |
16f8a6f9 NA |
718 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
719 | { | |
720 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) | |
721 | return true; | |
722 | ||
723 | kvm_queue_exception(vcpu, UD_VECTOR); | |
724 | return false; | |
725 | } | |
726 | EXPORT_SYMBOL_GPL(kvm_require_dr); | |
727 | ||
ec92fe44 JR |
728 | /* |
729 | * This function will be used to read from the physical memory of the currently | |
54bf36aa | 730 | * running guest. The difference to kvm_vcpu_read_guest_page is that this function |
ec92fe44 JR |
731 | * can read from guest physical or from the guest's guest physical memory. |
732 | */ | |
733 | int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, | |
734 | gfn_t ngfn, void *data, int offset, int len, | |
735 | u32 access) | |
736 | { | |
54987b7a | 737 | struct x86_exception exception; |
ec92fe44 JR |
738 | gfn_t real_gfn; |
739 | gpa_t ngpa; | |
740 | ||
741 | ngpa = gfn_to_gpa(ngfn); | |
54987b7a | 742 | real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception); |
ec92fe44 JR |
743 | if (real_gfn == UNMAPPED_GVA) |
744 | return -EFAULT; | |
745 | ||
746 | real_gfn = gpa_to_gfn(real_gfn); | |
747 | ||
54bf36aa | 748 | return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len); |
ec92fe44 JR |
749 | } |
750 | EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu); | |
751 | ||
69b0049a | 752 | static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
3d06b8bf JR |
753 | void *data, int offset, int len, u32 access) |
754 | { | |
755 | return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn, | |
756 | data, offset, len, access); | |
757 | } | |
758 | ||
16cfacc8 SC |
759 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
760 | { | |
5b7f575c | 761 | return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); |
16cfacc8 SC |
762 | } |
763 | ||
a03490ed | 764 | /* |
16cfacc8 | 765 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
a03490ed | 766 | */ |
ff03a073 | 767 | int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) |
a03490ed CO |
768 | { |
769 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
770 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
771 | int i; | |
772 | int ret; | |
ff03a073 | 773 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
a03490ed | 774 | |
ff03a073 JR |
775 | ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte, |
776 | offset * sizeof(u64), sizeof(pdpte), | |
777 | PFERR_USER_MASK|PFERR_WRITE_MASK); | |
a03490ed CO |
778 | if (ret < 0) { |
779 | ret = 0; | |
780 | goto out; | |
781 | } | |
782 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
812f30b2 | 783 | if ((pdpte[i] & PT_PRESENT_MASK) && |
16cfacc8 | 784 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
a03490ed CO |
785 | ret = 0; |
786 | goto out; | |
787 | } | |
788 | } | |
789 | ret = 1; | |
790 | ||
ff03a073 | 791 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
cb3c1e2f SC |
792 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); |
793 | ||
a03490ed | 794 | out: |
a03490ed CO |
795 | |
796 | return ret; | |
797 | } | |
cc4b6871 | 798 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 799 | |
9ed38ffa | 800 | bool pdptrs_changed(struct kvm_vcpu *vcpu) |
d835dfec | 801 | { |
ff03a073 | 802 | u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)]; |
3d06b8bf JR |
803 | int offset; |
804 | gfn_t gfn; | |
d835dfec AK |
805 | int r; |
806 | ||
bf03d4f9 | 807 | if (!is_pae_paging(vcpu)) |
d835dfec AK |
808 | return false; |
809 | ||
cb3c1e2f | 810 | if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR)) |
6de4f3ad AK |
811 | return true; |
812 | ||
a512177e PB |
813 | gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT; |
814 | offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1); | |
3d06b8bf JR |
815 | r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte), |
816 | PFERR_USER_MASK | PFERR_WRITE_MASK); | |
d835dfec | 817 | if (r < 0) |
7f7f0d9c | 818 | return true; |
d835dfec | 819 | |
7f7f0d9c | 820 | return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0; |
d835dfec | 821 | } |
9ed38ffa | 822 | EXPORT_SYMBOL_GPL(pdptrs_changed); |
d835dfec | 823 | |
f27ad38a TL |
824 | void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0) |
825 | { | |
826 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP; | |
827 | ||
828 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { | |
829 | kvm_clear_async_pf_completion_queue(vcpu); | |
830 | kvm_async_pf_hash_reset(vcpu); | |
831 | } | |
832 | ||
833 | if ((cr0 ^ old_cr0) & update_bits) | |
834 | kvm_mmu_reset_context(vcpu); | |
835 | ||
836 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && | |
837 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && | |
838 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) | |
839 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); | |
840 | } | |
841 | EXPORT_SYMBOL_GPL(kvm_post_set_cr0); | |
842 | ||
49a9b07e | 843 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed | 844 | { |
aad82703 | 845 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
d42e3fae | 846 | unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG; |
aad82703 | 847 | |
f9a48e6a AK |
848 | cr0 |= X86_CR0_ET; |
849 | ||
ab344828 | 850 | #ifdef CONFIG_X86_64 |
0f12244f GN |
851 | if (cr0 & 0xffffffff00000000UL) |
852 | return 1; | |
ab344828 GN |
853 | #endif |
854 | ||
855 | cr0 &= ~CR0_RESERVED_BITS; | |
a03490ed | 856 | |
0f12244f GN |
857 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
858 | return 1; | |
a03490ed | 859 | |
0f12244f GN |
860 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
861 | return 1; | |
a03490ed | 862 | |
a03490ed | 863 | #ifdef CONFIG_X86_64 |
05487215 SC |
864 | if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && |
865 | (cr0 & X86_CR0_PG)) { | |
866 | int cs_db, cs_l; | |
867 | ||
868 | if (!is_pae(vcpu)) | |
869 | return 1; | |
b3646477 | 870 | static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l); |
05487215 | 871 | if (cs_l) |
0f12244f | 872 | return 1; |
a03490ed | 873 | } |
05487215 SC |
874 | #endif |
875 | if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && | |
876 | is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) && | |
877 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu))) | |
878 | return 1; | |
a03490ed | 879 | |
ad756a16 MJ |
880 | if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) |
881 | return 1; | |
882 | ||
b3646477 | 883 | static_call(kvm_x86_set_cr0)(vcpu, cr0); |
a03490ed | 884 | |
f27ad38a | 885 | kvm_post_set_cr0(vcpu, old_cr0, cr0); |
b18d5431 | 886 | |
0f12244f GN |
887 | return 0; |
888 | } | |
2d3ad1f4 | 889 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 890 | |
2d3ad1f4 | 891 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 892 | { |
49a9b07e | 893 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
a03490ed | 894 | } |
2d3ad1f4 | 895 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 896 | |
139a12cf | 897 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 898 | { |
16809ecd TL |
899 | if (vcpu->arch.guest_state_protected) |
900 | return; | |
901 | ||
139a12cf AL |
902 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
903 | ||
904 | if (vcpu->arch.xcr0 != host_xcr0) | |
905 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); | |
906 | ||
907 | if (vcpu->arch.xsaves_enabled && | |
908 | vcpu->arch.ia32_xss != host_xss) | |
909 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); | |
910 | } | |
37486135 BM |
911 | |
912 | if (static_cpu_has(X86_FEATURE_PKU) && | |
913 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
914 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) && | |
915 | vcpu->arch.pkru != vcpu->arch.host_pkru) | |
916 | __write_pkru(vcpu->arch.pkru); | |
42bdf991 | 917 | } |
139a12cf | 918 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
42bdf991 | 919 | |
139a12cf | 920 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 921 | { |
16809ecd TL |
922 | if (vcpu->arch.guest_state_protected) |
923 | return; | |
924 | ||
37486135 BM |
925 | if (static_cpu_has(X86_FEATURE_PKU) && |
926 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
927 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) { | |
928 | vcpu->arch.pkru = rdpkru(); | |
929 | if (vcpu->arch.pkru != vcpu->arch.host_pkru) | |
930 | __write_pkru(vcpu->arch.host_pkru); | |
931 | } | |
932 | ||
139a12cf AL |
933 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
934 | ||
935 | if (vcpu->arch.xcr0 != host_xcr0) | |
936 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); | |
937 | ||
938 | if (vcpu->arch.xsaves_enabled && | |
939 | vcpu->arch.ia32_xss != host_xss) | |
940 | wrmsrl(MSR_IA32_XSS, host_xss); | |
941 | } | |
942 | ||
42bdf991 | 943 | } |
139a12cf | 944 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
42bdf991 | 945 | |
69b0049a | 946 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
2acf923e | 947 | { |
56c103ec LJ |
948 | u64 xcr0 = xcr; |
949 | u64 old_xcr0 = vcpu->arch.xcr0; | |
46c34cb0 | 950 | u64 valid_bits; |
2acf923e DC |
951 | |
952 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ | |
953 | if (index != XCR_XFEATURE_ENABLED_MASK) | |
954 | return 1; | |
d91cab78 | 955 | if (!(xcr0 & XFEATURE_MASK_FP)) |
2acf923e | 956 | return 1; |
d91cab78 | 957 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
2acf923e | 958 | return 1; |
46c34cb0 PB |
959 | |
960 | /* | |
961 | * Do not allow the guest to set bits that we do not support | |
962 | * saving. However, xcr0 bit 0 is always set, even if the | |
963 | * emulated CPU does not support XSAVE (see fx_init). | |
964 | */ | |
d91cab78 | 965 | valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; |
46c34cb0 | 966 | if (xcr0 & ~valid_bits) |
2acf923e | 967 | return 1; |
46c34cb0 | 968 | |
d91cab78 DH |
969 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
970 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) | |
390bd528 LJ |
971 | return 1; |
972 | ||
d91cab78 DH |
973 | if (xcr0 & XFEATURE_MASK_AVX512) { |
974 | if (!(xcr0 & XFEATURE_MASK_YMM)) | |
612263b3 | 975 | return 1; |
d91cab78 | 976 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
612263b3 CP |
977 | return 1; |
978 | } | |
2acf923e | 979 | vcpu->arch.xcr0 = xcr0; |
56c103ec | 980 | |
d91cab78 | 981 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
aedbaf4f | 982 | kvm_update_cpuid_runtime(vcpu); |
2acf923e DC |
983 | return 0; |
984 | } | |
985 | ||
92f9895c | 986 | int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu) |
2acf923e | 987 | { |
92f9895c SC |
988 | if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || |
989 | __kvm_set_xcr(vcpu, kvm_rcx_read(vcpu), kvm_read_edx_eax(vcpu))) { | |
990 | kvm_inject_gp(vcpu, 0); | |
991 | return 1; | |
992 | } | |
bbefd4fc | 993 | |
92f9895c | 994 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e | 995 | } |
92f9895c | 996 | EXPORT_SYMBOL_GPL(kvm_emulate_xsetbv); |
2acf923e | 997 | |
ee69c92b | 998 | bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed | 999 | { |
b11306b5 | 1000 | if (cr4 & cr4_reserved_bits) |
ee69c92b | 1001 | return false; |
b9baba86 | 1002 | |
b899c132 | 1003 | if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) |
ee69c92b | 1004 | return false; |
3ca94192 | 1005 | |
b3646477 | 1006 | return static_call(kvm_x86_is_valid_cr4)(vcpu, cr4); |
3ca94192 | 1007 | } |
ee69c92b | 1008 | EXPORT_SYMBOL_GPL(kvm_is_valid_cr4); |
3ca94192 | 1009 | |
5b51cb13 TL |
1010 | void kvm_post_set_cr4(struct kvm_vcpu *vcpu, unsigned long old_cr4, unsigned long cr4) |
1011 | { | |
1012 | unsigned long mmu_role_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | | |
1013 | X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE; | |
1014 | ||
1015 | if (((cr4 ^ old_cr4) & mmu_role_bits) || | |
1016 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) | |
1017 | kvm_mmu_reset_context(vcpu); | |
3ca94192 | 1018 | } |
5b51cb13 | 1019 | EXPORT_SYMBOL_GPL(kvm_post_set_cr4); |
3ca94192 WL |
1020 | |
1021 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
1022 | { | |
1023 | unsigned long old_cr4 = kvm_read_cr4(vcpu); | |
1024 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | | |
cb957adb | 1025 | X86_CR4_SMEP; |
3ca94192 | 1026 | |
ee69c92b | 1027 | if (!kvm_is_valid_cr4(vcpu, cr4)) |
ae3e61e1 PB |
1028 | return 1; |
1029 | ||
a03490ed | 1030 | if (is_long_mode(vcpu)) { |
0f12244f GN |
1031 | if (!(cr4 & X86_CR4_PAE)) |
1032 | return 1; | |
d74fcfc1 SC |
1033 | if ((cr4 ^ old_cr4) & X86_CR4_LA57) |
1034 | return 1; | |
a2edf57f AK |
1035 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
1036 | && ((cr4 ^ old_cr4) & pdptr_bits) | |
9f8fe504 AK |
1037 | && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
1038 | kvm_read_cr3(vcpu))) | |
0f12244f GN |
1039 | return 1; |
1040 | ||
ad756a16 | 1041 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
d6321d49 | 1042 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
ad756a16 MJ |
1043 | return 1; |
1044 | ||
1045 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ | |
1046 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) | |
1047 | return 1; | |
1048 | } | |
1049 | ||
b3646477 | 1050 | static_call(kvm_x86_set_cr4)(vcpu, cr4); |
a03490ed | 1051 | |
5b51cb13 | 1052 | kvm_post_set_cr4(vcpu, old_cr4, cr4); |
2acf923e | 1053 | |
0f12244f GN |
1054 | return 0; |
1055 | } | |
2d3ad1f4 | 1056 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 1057 | |
2390218b | 1058 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 1059 | { |
ade61e28 | 1060 | bool skip_tlb_flush = false; |
ac146235 | 1061 | #ifdef CONFIG_X86_64 |
c19986fe JS |
1062 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
1063 | ||
ade61e28 | 1064 | if (pcid_enabled) { |
208320ba JS |
1065 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
1066 | cr3 &= ~X86_CR3_PCID_NOFLUSH; | |
ade61e28 | 1067 | } |
ac146235 | 1068 | #endif |
9d88fca7 | 1069 | |
9f8fe504 | 1070 | if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { |
956bf353 JS |
1071 | if (!skip_tlb_flush) { |
1072 | kvm_mmu_sync_roots(vcpu); | |
eeeb4f67 | 1073 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
956bf353 | 1074 | } |
0f12244f | 1075 | return 0; |
d835dfec AK |
1076 | } |
1077 | ||
ca29e145 | 1078 | if (is_long_mode(vcpu) && kvm_vcpu_is_illegal_gpa(vcpu, cr3)) |
d1cd3ce9 | 1079 | return 1; |
bf03d4f9 PB |
1080 | else if (is_pae_paging(vcpu) && |
1081 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) | |
346874c9 | 1082 | return 1; |
a03490ed | 1083 | |
be01e8e2 | 1084 | kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush); |
0f12244f | 1085 | vcpu->arch.cr3 = cr3; |
cb3c1e2f | 1086 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
7c390d35 | 1087 | |
0f12244f GN |
1088 | return 0; |
1089 | } | |
2d3ad1f4 | 1090 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 1091 | |
eea1cff9 | 1092 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed | 1093 | { |
0f12244f GN |
1094 | if (cr8 & CR8_RESERVED_BITS) |
1095 | return 1; | |
35754c98 | 1096 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1097 | kvm_lapic_set_tpr(vcpu, cr8); |
1098 | else | |
ad312c7c | 1099 | vcpu->arch.cr8 = cr8; |
0f12244f GN |
1100 | return 0; |
1101 | } | |
2d3ad1f4 | 1102 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 1103 | |
2d3ad1f4 | 1104 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed | 1105 | { |
35754c98 | 1106 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1107 | return kvm_lapic_get_cr8(vcpu); |
1108 | else | |
ad312c7c | 1109 | return vcpu->arch.cr8; |
a03490ed | 1110 | } |
2d3ad1f4 | 1111 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 1112 | |
ae561ede NA |
1113 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
1114 | { | |
1115 | int i; | |
1116 | ||
1117 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { | |
1118 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
1119 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
1120 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; | |
1121 | } | |
1122 | } | |
1123 | ||
7c86663b | 1124 | void kvm_update_dr7(struct kvm_vcpu *vcpu) |
c8639010 JK |
1125 | { |
1126 | unsigned long dr7; | |
1127 | ||
1128 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) | |
1129 | dr7 = vcpu->arch.guest_debug_dr7; | |
1130 | else | |
1131 | dr7 = vcpu->arch.dr7; | |
b3646477 | 1132 | static_call(kvm_x86_set_dr7)(vcpu, dr7); |
360b948d PB |
1133 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
1134 | if (dr7 & DR7_BP_EN_MASK) | |
1135 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; | |
c8639010 | 1136 | } |
7c86663b | 1137 | EXPORT_SYMBOL_GPL(kvm_update_dr7); |
c8639010 | 1138 | |
6f43ed01 NA |
1139 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
1140 | { | |
1141 | u64 fixed = DR6_FIXED_1; | |
1142 | ||
d6321d49 | 1143 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
6f43ed01 NA |
1144 | fixed |= DR6_RTM; |
1145 | return fixed; | |
1146 | } | |
1147 | ||
996ff542 | 1148 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
020df079 | 1149 | { |
ea740059 MP |
1150 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1151 | ||
020df079 GN |
1152 | switch (dr) { |
1153 | case 0 ... 3: | |
ea740059 | 1154 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
020df079 GN |
1155 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
1156 | vcpu->arch.eff_db[dr] = val; | |
1157 | break; | |
1158 | case 4: | |
020df079 | 1159 | case 6: |
f5f6145e | 1160 | if (!kvm_dr6_valid(val)) |
996ff542 | 1161 | return 1; /* #GP */ |
6f43ed01 | 1162 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
020df079 GN |
1163 | break; |
1164 | case 5: | |
020df079 | 1165 | default: /* 7 */ |
b91991bf | 1166 | if (!kvm_dr7_valid(val)) |
996ff542 | 1167 | return 1; /* #GP */ |
020df079 | 1168 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
c8639010 | 1169 | kvm_update_dr7(vcpu); |
020df079 GN |
1170 | break; |
1171 | } | |
1172 | ||
1173 | return 0; | |
1174 | } | |
1175 | EXPORT_SYMBOL_GPL(kvm_set_dr); | |
1176 | ||
29d6ca41 | 1177 | void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
020df079 | 1178 | { |
ea740059 MP |
1179 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1180 | ||
020df079 GN |
1181 | switch (dr) { |
1182 | case 0 ... 3: | |
ea740059 | 1183 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
020df079 GN |
1184 | break; |
1185 | case 4: | |
020df079 | 1186 | case 6: |
5679b803 | 1187 | *val = vcpu->arch.dr6; |
020df079 GN |
1188 | break; |
1189 | case 5: | |
020df079 GN |
1190 | default: /* 7 */ |
1191 | *val = vcpu->arch.dr7; | |
1192 | break; | |
1193 | } | |
338dbc97 | 1194 | } |
020df079 GN |
1195 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
1196 | ||
c483c454 | 1197 | int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu) |
022cd0e8 | 1198 | { |
de3cd117 | 1199 | u32 ecx = kvm_rcx_read(vcpu); |
022cd0e8 | 1200 | u64 data; |
022cd0e8 | 1201 | |
c483c454 SC |
1202 | if (kvm_pmu_rdpmc(vcpu, ecx, &data)) { |
1203 | kvm_inject_gp(vcpu, 0); | |
1204 | return 1; | |
1205 | } | |
1206 | ||
de3cd117 SC |
1207 | kvm_rax_write(vcpu, (u32)data); |
1208 | kvm_rdx_write(vcpu, data >> 32); | |
c483c454 | 1209 | return kvm_skip_emulated_instruction(vcpu); |
022cd0e8 | 1210 | } |
c483c454 | 1211 | EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc); |
022cd0e8 | 1212 | |
043405e1 CO |
1213 | /* |
1214 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1215 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
1216 | * | |
7a5ee6ed CQ |
1217 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
1218 | * extract the supported MSRs from the related const lists. | |
1219 | * msrs_to_save is selected from the msrs_to_save_all to reflect the | |
e3267cbb | 1220 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
7a5ee6ed | 1221 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
62ef68bb | 1222 | * may depend on host virtualization features rather than host cpu features. |
043405e1 | 1223 | */ |
e3267cbb | 1224 | |
7a5ee6ed | 1225 | static const u32 msrs_to_save_all[] = { |
043405e1 | 1226 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
8c06585d | 1227 | MSR_STAR, |
043405e1 CO |
1228 | #ifdef CONFIG_X86_64 |
1229 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
1230 | #endif | |
b3897a49 | 1231 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
32ad73db | 1232 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
2bdb76c0 | 1233 | MSR_IA32_SPEC_CTRL, |
bf8c55d8 CP |
1234 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
1235 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, | |
1236 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, | |
1237 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, | |
1238 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, | |
1239 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, | |
6e3ba4ab TX |
1240 | MSR_IA32_UMWAIT_CONTROL, |
1241 | ||
e2ada66e JM |
1242 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
1243 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3, | |
1244 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, | |
1245 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, | |
1246 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, | |
1247 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, | |
1248 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, | |
1249 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, | |
1250 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, | |
1251 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, | |
1252 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, | |
1253 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, | |
1254 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, | |
e2ada66e JM |
1255 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
1256 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, | |
1257 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, | |
1258 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, | |
1259 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, | |
1260 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, | |
1261 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, | |
1262 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, | |
1263 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, | |
043405e1 CO |
1264 | }; |
1265 | ||
7a5ee6ed | 1266 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
043405e1 CO |
1267 | static unsigned num_msrs_to_save; |
1268 | ||
7a5ee6ed | 1269 | static const u32 emulated_msrs_all[] = { |
62ef68bb PB |
1270 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
1271 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, | |
1272 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, | |
1273 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, | |
72c139ba | 1274 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
e7d9513b AS |
1275 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
1276 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, | |
e516cebb | 1277 | HV_X64_MSR_RESET, |
11c4b1ca | 1278 | HV_X64_MSR_VP_INDEX, |
9eec50b8 | 1279 | HV_X64_MSR_VP_RUNTIME, |
5c919412 | 1280 | HV_X64_MSR_SCONTROL, |
1f4b34f8 | 1281 | HV_X64_MSR_STIMER0_CONFIG, |
d4abc577 | 1282 | HV_X64_MSR_VP_ASSIST_PAGE, |
a2e164e7 VK |
1283 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
1284 | HV_X64_MSR_TSC_EMULATION_STATUS, | |
f97f5a56 JD |
1285 | HV_X64_MSR_SYNDBG_OPTIONS, |
1286 | HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS, | |
1287 | HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER, | |
1288 | HV_X64_MSR_SYNDBG_PENDING_BUFFER, | |
a2e164e7 VK |
1289 | |
1290 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, | |
557a961a | 1291 | MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, |
62ef68bb | 1292 | |
ba904635 | 1293 | MSR_IA32_TSC_ADJUST, |
a3e06bbe | 1294 | MSR_IA32_TSCDEADLINE, |
2bdb76c0 | 1295 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1296 | MSR_IA32_PERF_CAPABILITIES, |
043405e1 | 1297 | MSR_IA32_MISC_ENABLE, |
908e75f3 AK |
1298 | MSR_IA32_MCG_STATUS, |
1299 | MSR_IA32_MCG_CTL, | |
c45dcc71 | 1300 | MSR_IA32_MCG_EXT_CTL, |
64d60670 | 1301 | MSR_IA32_SMBASE, |
52797bf9 | 1302 | MSR_SMI_COUNT, |
db2336a8 KH |
1303 | MSR_PLATFORM_INFO, |
1304 | MSR_MISC_FEATURES_ENABLES, | |
bc226f07 | 1305 | MSR_AMD64_VIRT_SPEC_CTRL, |
6c6a2ab9 | 1306 | MSR_IA32_POWER_CTL, |
99634e3e | 1307 | MSR_IA32_UCODE_REV, |
191c8137 | 1308 | |
95c5c7c7 PB |
1309 | /* |
1310 | * The following list leaves out MSRs whose values are determined | |
1311 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. | |
1312 | * We always support the "true" VMX control MSRs, even if the host | |
1313 | * processor does not, so I am putting these registers here rather | |
7a5ee6ed | 1314 | * than in msrs_to_save_all. |
95c5c7c7 PB |
1315 | */ |
1316 | MSR_IA32_VMX_BASIC, | |
1317 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1318 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1319 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1320 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1321 | MSR_IA32_VMX_MISC, | |
1322 | MSR_IA32_VMX_CR0_FIXED0, | |
1323 | MSR_IA32_VMX_CR4_FIXED0, | |
1324 | MSR_IA32_VMX_VMCS_ENUM, | |
1325 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1326 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1327 | MSR_IA32_VMX_VMFUNC, | |
1328 | ||
191c8137 | 1329 | MSR_K7_HWCR, |
2d5ba19b | 1330 | MSR_KVM_POLL_CONTROL, |
043405e1 CO |
1331 | }; |
1332 | ||
7a5ee6ed | 1333 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
62ef68bb PB |
1334 | static unsigned num_emulated_msrs; |
1335 | ||
801e459a TL |
1336 | /* |
1337 | * List of msr numbers which are used to expose MSR-based features that | |
1338 | * can be used by a hypervisor to validate requested CPU features. | |
1339 | */ | |
7a5ee6ed | 1340 | static const u32 msr_based_features_all[] = { |
1389309c PB |
1341 | MSR_IA32_VMX_BASIC, |
1342 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1343 | MSR_IA32_VMX_PINBASED_CTLS, | |
1344 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1345 | MSR_IA32_VMX_PROCBASED_CTLS, | |
1346 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1347 | MSR_IA32_VMX_EXIT_CTLS, | |
1348 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1349 | MSR_IA32_VMX_ENTRY_CTLS, | |
1350 | MSR_IA32_VMX_MISC, | |
1351 | MSR_IA32_VMX_CR0_FIXED0, | |
1352 | MSR_IA32_VMX_CR0_FIXED1, | |
1353 | MSR_IA32_VMX_CR4_FIXED0, | |
1354 | MSR_IA32_VMX_CR4_FIXED1, | |
1355 | MSR_IA32_VMX_VMCS_ENUM, | |
1356 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1357 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1358 | MSR_IA32_VMX_VMFUNC, | |
1359 | ||
d1d93fa9 | 1360 | MSR_F10H_DECFG, |
518e7b94 | 1361 | MSR_IA32_UCODE_REV, |
cd283252 | 1362 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1363 | MSR_IA32_PERF_CAPABILITIES, |
801e459a TL |
1364 | }; |
1365 | ||
7a5ee6ed | 1366 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
801e459a TL |
1367 | static unsigned int num_msr_based_features; |
1368 | ||
4d22c17c | 1369 | static u64 kvm_get_arch_capabilities(void) |
5b76a3cf | 1370 | { |
4d22c17c | 1371 | u64 data = 0; |
5b76a3cf | 1372 | |
4d22c17c XL |
1373 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
1374 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); | |
5b76a3cf | 1375 | |
b8e8c830 PB |
1376 | /* |
1377 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that | |
1378 | * the nested hypervisor runs with NX huge pages. If it is not, | |
1379 | * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other | |
1380 | * L1 guests, so it need not worry about its own (L2) guests. | |
1381 | */ | |
1382 | data |= ARCH_CAP_PSCHANGE_MC_NO; | |
1383 | ||
5b76a3cf PB |
1384 | /* |
1385 | * If we're doing cache flushes (either "always" or "cond") | |
1386 | * we will do one whenever the guest does a vmlaunch/vmresume. | |
1387 | * If an outer hypervisor is doing the cache flush for us | |
1388 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that | |
1389 | * capability to the guest too, and if EPT is disabled we're not | |
1390 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will | |
1391 | * require a nested hypervisor to do a flush of its own. | |
1392 | */ | |
1393 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) | |
1394 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; | |
1395 | ||
0c54914d PB |
1396 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
1397 | data |= ARCH_CAP_RDCL_NO; | |
1398 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1399 | data |= ARCH_CAP_SSB_NO; | |
1400 | if (!boot_cpu_has_bug(X86_BUG_MDS)) | |
1401 | data |= ARCH_CAP_MDS_NO; | |
1402 | ||
7131636e PB |
1403 | if (!boot_cpu_has(X86_FEATURE_RTM)) { |
1404 | /* | |
1405 | * If RTM=0 because the kernel has disabled TSX, the host might | |
1406 | * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0 | |
1407 | * and therefore knows that there cannot be TAA) but keep | |
1408 | * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts, | |
1409 | * and we want to allow migrating those guests to tsx=off hosts. | |
1410 | */ | |
1411 | data &= ~ARCH_CAP_TAA_NO; | |
1412 | } else if (!boot_cpu_has_bug(X86_BUG_TAA)) { | |
cbbaa272 | 1413 | data |= ARCH_CAP_TAA_NO; |
7131636e PB |
1414 | } else { |
1415 | /* | |
1416 | * Nothing to do here; we emulate TSX_CTRL if present on the | |
1417 | * host so the guest can choose between disabling TSX or | |
1418 | * using VERW to clear CPU buffers. | |
1419 | */ | |
1420 | } | |
e1d38b63 | 1421 | |
5b76a3cf PB |
1422 | return data; |
1423 | } | |
5b76a3cf | 1424 | |
66421c1e WL |
1425 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
1426 | { | |
1427 | switch (msr->index) { | |
cd283252 | 1428 | case MSR_IA32_ARCH_CAPABILITIES: |
5b76a3cf PB |
1429 | msr->data = kvm_get_arch_capabilities(); |
1430 | break; | |
1431 | case MSR_IA32_UCODE_REV: | |
cd283252 | 1432 | rdmsrl_safe(msr->index, &msr->data); |
518e7b94 | 1433 | break; |
66421c1e | 1434 | default: |
b3646477 | 1435 | return static_call(kvm_x86_get_msr_feature)(msr); |
66421c1e WL |
1436 | } |
1437 | return 0; | |
1438 | } | |
1439 | ||
801e459a TL |
1440 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
1441 | { | |
1442 | struct kvm_msr_entry msr; | |
66421c1e | 1443 | int r; |
801e459a TL |
1444 | |
1445 | msr.index = index; | |
66421c1e | 1446 | r = kvm_get_msr_feature(&msr); |
12bc2132 PX |
1447 | |
1448 | if (r == KVM_MSR_RET_INVALID) { | |
1449 | /* Unconditionally clear the output for simplicity */ | |
1450 | *data = 0; | |
cc4cb017 ML |
1451 | if (kvm_msr_ignored_check(vcpu, index, 0, false)) |
1452 | r = 0; | |
12bc2132 PX |
1453 | } |
1454 | ||
66421c1e WL |
1455 | if (r) |
1456 | return r; | |
801e459a TL |
1457 | |
1458 | *data = msr.data; | |
1459 | ||
1460 | return 0; | |
1461 | } | |
1462 | ||
11988499 | 1463 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
15c4a640 | 1464 | { |
1b4d56b8 | 1465 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
11988499 | 1466 | return false; |
1b2fd70c | 1467 | |
1b4d56b8 | 1468 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
11988499 | 1469 | return false; |
d8017474 | 1470 | |
0a629563 SC |
1471 | if (efer & (EFER_LME | EFER_LMA) && |
1472 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
1473 | return false; | |
1474 | ||
1475 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) | |
1476 | return false; | |
d8017474 | 1477 | |
384bb783 | 1478 | return true; |
11988499 SC |
1479 | |
1480 | } | |
1481 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) | |
1482 | { | |
1483 | if (efer & efer_reserved_bits) | |
1484 | return false; | |
1485 | ||
1486 | return __kvm_valid_efer(vcpu, efer); | |
384bb783 JK |
1487 | } |
1488 | EXPORT_SYMBOL_GPL(kvm_valid_efer); | |
1489 | ||
11988499 | 1490 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
384bb783 JK |
1491 | { |
1492 | u64 old_efer = vcpu->arch.efer; | |
11988499 | 1493 | u64 efer = msr_info->data; |
72f211ec | 1494 | int r; |
384bb783 | 1495 | |
11988499 | 1496 | if (efer & efer_reserved_bits) |
66f61c92 | 1497 | return 1; |
384bb783 | 1498 | |
11988499 SC |
1499 | if (!msr_info->host_initiated) { |
1500 | if (!__kvm_valid_efer(vcpu, efer)) | |
1501 | return 1; | |
1502 | ||
1503 | if (is_paging(vcpu) && | |
1504 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) | |
1505 | return 1; | |
1506 | } | |
384bb783 | 1507 | |
15c4a640 | 1508 | efer &= ~EFER_LMA; |
f6801dff | 1509 | efer |= vcpu->arch.efer & EFER_LMA; |
15c4a640 | 1510 | |
b3646477 | 1511 | r = static_call(kvm_x86_set_efer)(vcpu, efer); |
72f211ec ML |
1512 | if (r) { |
1513 | WARN_ON(r > 0); | |
1514 | return r; | |
1515 | } | |
a3d204e2 | 1516 | |
aad82703 SY |
1517 | /* Update reserved bits */ |
1518 | if ((efer ^ old_efer) & EFER_NX) | |
1519 | kvm_mmu_reset_context(vcpu); | |
1520 | ||
b69e8cae | 1521 | return 0; |
15c4a640 CO |
1522 | } |
1523 | ||
f2b4b7dd JR |
1524 | void kvm_enable_efer_bits(u64 mask) |
1525 | { | |
1526 | efer_reserved_bits &= ~mask; | |
1527 | } | |
1528 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
1529 | ||
51de8151 AG |
1530 | bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type) |
1531 | { | |
1a155254 AG |
1532 | struct kvm *kvm = vcpu->kvm; |
1533 | struct msr_bitmap_range *ranges = kvm->arch.msr_filter.ranges; | |
1534 | u32 count = kvm->arch.msr_filter.count; | |
1535 | u32 i; | |
1536 | bool r = kvm->arch.msr_filter.default_allow; | |
1537 | int idx; | |
1538 | ||
9389b9d5 SC |
1539 | /* MSR filtering not set up or x2APIC enabled, allow everything */ |
1540 | if (!count || (index >= 0x800 && index <= 0x8ff)) | |
1a155254 AG |
1541 | return true; |
1542 | ||
1543 | /* Prevent collision with set_msr_filter */ | |
1544 | idx = srcu_read_lock(&kvm->srcu); | |
1545 | ||
1546 | for (i = 0; i < count; i++) { | |
1547 | u32 start = ranges[i].base; | |
1548 | u32 end = start + ranges[i].nmsrs; | |
1549 | u32 flags = ranges[i].flags; | |
1550 | unsigned long *bitmap = ranges[i].bitmap; | |
1551 | ||
1552 | if ((index >= start) && (index < end) && (flags & type)) { | |
1553 | r = !!test_bit(index - start, bitmap); | |
1554 | break; | |
1555 | } | |
1556 | } | |
1557 | ||
1558 | srcu_read_unlock(&kvm->srcu, idx); | |
1559 | ||
1560 | return r; | |
51de8151 AG |
1561 | } |
1562 | EXPORT_SYMBOL_GPL(kvm_msr_allowed); | |
1563 | ||
15c4a640 | 1564 | /* |
f20935d8 SC |
1565 | * Write @data into the MSR specified by @index. Select MSR specific fault |
1566 | * checks are bypassed if @host_initiated is %true. | |
15c4a640 CO |
1567 | * Returns 0 on success, non-0 otherwise. |
1568 | * Assumes vcpu_load() was already called. | |
1569 | */ | |
f20935d8 SC |
1570 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
1571 | bool host_initiated) | |
15c4a640 | 1572 | { |
f20935d8 SC |
1573 | struct msr_data msr; |
1574 | ||
1a155254 | 1575 | if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE)) |
cc4cb017 | 1576 | return KVM_MSR_RET_FILTERED; |
1a155254 | 1577 | |
f20935d8 | 1578 | switch (index) { |
854e8bb1 NA |
1579 | case MSR_FS_BASE: |
1580 | case MSR_GS_BASE: | |
1581 | case MSR_KERNEL_GS_BASE: | |
1582 | case MSR_CSTAR: | |
1583 | case MSR_LSTAR: | |
f20935d8 | 1584 | if (is_noncanonical_address(data, vcpu)) |
854e8bb1 NA |
1585 | return 1; |
1586 | break; | |
1587 | case MSR_IA32_SYSENTER_EIP: | |
1588 | case MSR_IA32_SYSENTER_ESP: | |
1589 | /* | |
1590 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if | |
1591 | * non-canonical address is written on Intel but not on | |
1592 | * AMD (which ignores the top 32-bits, because it does | |
1593 | * not implement 64-bit SYSENTER). | |
1594 | * | |
1595 | * 64-bit code should hence be able to write a non-canonical | |
1596 | * value on AMD. Making the address canonical ensures that | |
1597 | * vmentry does not fail on Intel after writing a non-canonical | |
1598 | * value, and that something deterministic happens if the guest | |
1599 | * invokes 64-bit SYSENTER. | |
1600 | */ | |
f20935d8 | 1601 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
854e8bb1 | 1602 | } |
f20935d8 SC |
1603 | |
1604 | msr.data = data; | |
1605 | msr.index = index; | |
1606 | msr.host_initiated = host_initiated; | |
1607 | ||
b3646477 | 1608 | return static_call(kvm_x86_set_msr)(vcpu, &msr); |
15c4a640 CO |
1609 | } |
1610 | ||
6abe9c13 PX |
1611 | static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, |
1612 | u32 index, u64 data, bool host_initiated) | |
1613 | { | |
1614 | int ret = __kvm_set_msr(vcpu, index, data, host_initiated); | |
1615 | ||
1616 | if (ret == KVM_MSR_RET_INVALID) | |
cc4cb017 ML |
1617 | if (kvm_msr_ignored_check(vcpu, index, data, true)) |
1618 | ret = 0; | |
6abe9c13 PX |
1619 | |
1620 | return ret; | |
1621 | } | |
1622 | ||
313a3dc7 | 1623 | /* |
f20935d8 SC |
1624 | * Read the MSR specified by @index into @data. Select MSR specific fault |
1625 | * checks are bypassed if @host_initiated is %true. | |
1626 | * Returns 0 on success, non-0 otherwise. | |
1627 | * Assumes vcpu_load() was already called. | |
313a3dc7 | 1628 | */ |
edef5c36 PB |
1629 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
1630 | bool host_initiated) | |
609e36d3 PB |
1631 | { |
1632 | struct msr_data msr; | |
f20935d8 | 1633 | int ret; |
609e36d3 | 1634 | |
1a155254 | 1635 | if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ)) |
cc4cb017 | 1636 | return KVM_MSR_RET_FILTERED; |
1a155254 | 1637 | |
609e36d3 | 1638 | msr.index = index; |
f20935d8 | 1639 | msr.host_initiated = host_initiated; |
609e36d3 | 1640 | |
b3646477 | 1641 | ret = static_call(kvm_x86_get_msr)(vcpu, &msr); |
f20935d8 SC |
1642 | if (!ret) |
1643 | *data = msr.data; | |
1644 | return ret; | |
609e36d3 PB |
1645 | } |
1646 | ||
6abe9c13 PX |
1647 | static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu, |
1648 | u32 index, u64 *data, bool host_initiated) | |
1649 | { | |
1650 | int ret = __kvm_get_msr(vcpu, index, data, host_initiated); | |
1651 | ||
1652 | if (ret == KVM_MSR_RET_INVALID) { | |
1653 | /* Unconditionally clear *data for simplicity */ | |
1654 | *data = 0; | |
cc4cb017 ML |
1655 | if (kvm_msr_ignored_check(vcpu, index, 0, false)) |
1656 | ret = 0; | |
6abe9c13 PX |
1657 | } |
1658 | ||
1659 | return ret; | |
1660 | } | |
1661 | ||
f20935d8 | 1662 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
313a3dc7 | 1663 | { |
6abe9c13 | 1664 | return kvm_get_msr_ignored_check(vcpu, index, data, false); |
f20935d8 SC |
1665 | } |
1666 | EXPORT_SYMBOL_GPL(kvm_get_msr); | |
8fe8ab46 | 1667 | |
f20935d8 SC |
1668 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
1669 | { | |
6abe9c13 | 1670 | return kvm_set_msr_ignored_check(vcpu, index, data, false); |
f20935d8 SC |
1671 | } |
1672 | EXPORT_SYMBOL_GPL(kvm_set_msr); | |
1673 | ||
8b474427 | 1674 | static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu) |
1ae09954 | 1675 | { |
8b474427 PB |
1676 | int err = vcpu->run->msr.error; |
1677 | if (!err) { | |
1ae09954 AG |
1678 | kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); |
1679 | kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); | |
1680 | } | |
1681 | ||
b3646477 | 1682 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, err); |
1ae09954 AG |
1683 | } |
1684 | ||
1685 | static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu) | |
1686 | { | |
b3646477 | 1687 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error); |
1ae09954 AG |
1688 | } |
1689 | ||
1690 | static u64 kvm_msr_reason(int r) | |
1691 | { | |
1692 | switch (r) { | |
cc4cb017 | 1693 | case KVM_MSR_RET_INVALID: |
1ae09954 | 1694 | return KVM_MSR_EXIT_REASON_UNKNOWN; |
cc4cb017 | 1695 | case KVM_MSR_RET_FILTERED: |
1a155254 | 1696 | return KVM_MSR_EXIT_REASON_FILTER; |
1ae09954 AG |
1697 | default: |
1698 | return KVM_MSR_EXIT_REASON_INVAL; | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index, | |
1703 | u32 exit_reason, u64 data, | |
1704 | int (*completion)(struct kvm_vcpu *vcpu), | |
1705 | int r) | |
1706 | { | |
1707 | u64 msr_reason = kvm_msr_reason(r); | |
1708 | ||
1709 | /* Check if the user wanted to know about this MSR fault */ | |
1710 | if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) | |
1711 | return 0; | |
1712 | ||
1713 | vcpu->run->exit_reason = exit_reason; | |
1714 | vcpu->run->msr.error = 0; | |
1715 | memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); | |
1716 | vcpu->run->msr.reason = msr_reason; | |
1717 | vcpu->run->msr.index = index; | |
1718 | vcpu->run->msr.data = data; | |
1719 | vcpu->arch.complete_userspace_io = completion; | |
1720 | ||
1721 | return 1; | |
1722 | } | |
1723 | ||
1724 | static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r) | |
1725 | { | |
1726 | return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0, | |
1727 | complete_emulated_rdmsr, r); | |
1728 | } | |
1729 | ||
1730 | static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r) | |
1731 | { | |
1732 | return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data, | |
1733 | complete_emulated_wrmsr, r); | |
1734 | } | |
1735 | ||
1edce0a9 SC |
1736 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
1737 | { | |
1738 | u32 ecx = kvm_rcx_read(vcpu); | |
1739 | u64 data; | |
1ae09954 AG |
1740 | int r; |
1741 | ||
1742 | r = kvm_get_msr(vcpu, ecx, &data); | |
1edce0a9 | 1743 | |
1ae09954 AG |
1744 | /* MSR read failed? See if we should ask user space */ |
1745 | if (r && kvm_get_msr_user_space(vcpu, ecx, r)) { | |
1746 | /* Bounce to user space */ | |
1747 | return 0; | |
1748 | } | |
1749 | ||
8b474427 PB |
1750 | if (!r) { |
1751 | trace_kvm_msr_read(ecx, data); | |
1752 | ||
1753 | kvm_rax_write(vcpu, data & -1u); | |
1754 | kvm_rdx_write(vcpu, (data >> 32) & -1u); | |
1755 | } else { | |
1edce0a9 | 1756 | trace_kvm_msr_read_ex(ecx); |
1edce0a9 SC |
1757 | } |
1758 | ||
b3646477 | 1759 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, r); |
1edce0a9 SC |
1760 | } |
1761 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); | |
1762 | ||
1763 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) | |
1764 | { | |
1765 | u32 ecx = kvm_rcx_read(vcpu); | |
1766 | u64 data = kvm_read_edx_eax(vcpu); | |
1ae09954 | 1767 | int r; |
1edce0a9 | 1768 | |
1ae09954 AG |
1769 | r = kvm_set_msr(vcpu, ecx, data); |
1770 | ||
1771 | /* MSR write failed? See if we should ask user space */ | |
7dffecaf | 1772 | if (r && kvm_set_msr_user_space(vcpu, ecx, data, r)) |
1ae09954 AG |
1773 | /* Bounce to user space */ |
1774 | return 0; | |
7dffecaf ML |
1775 | |
1776 | /* Signal all other negative errors to userspace */ | |
1777 | if (r < 0) | |
1778 | return r; | |
1ae09954 | 1779 | |
8b474427 PB |
1780 | if (!r) |
1781 | trace_kvm_msr_write(ecx, data); | |
1782 | else | |
1edce0a9 | 1783 | trace_kvm_msr_write_ex(ecx, data); |
1edce0a9 | 1784 | |
b3646477 | 1785 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, r); |
1edce0a9 SC |
1786 | } |
1787 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); | |
1788 | ||
5ff3a351 SC |
1789 | int kvm_emulate_as_nop(struct kvm_vcpu *vcpu) |
1790 | { | |
1791 | return kvm_skip_emulated_instruction(vcpu); | |
1792 | } | |
1793 | EXPORT_SYMBOL_GPL(kvm_emulate_as_nop); | |
1794 | ||
1795 | int kvm_emulate_invd(struct kvm_vcpu *vcpu) | |
1796 | { | |
1797 | /* Treat an INVD instruction as a NOP and just skip it. */ | |
1798 | return kvm_emulate_as_nop(vcpu); | |
1799 | } | |
1800 | EXPORT_SYMBOL_GPL(kvm_emulate_invd); | |
1801 | ||
1802 | int kvm_emulate_mwait(struct kvm_vcpu *vcpu) | |
1803 | { | |
1804 | pr_warn_once("kvm: MWAIT instruction emulated as NOP!\n"); | |
1805 | return kvm_emulate_as_nop(vcpu); | |
1806 | } | |
1807 | EXPORT_SYMBOL_GPL(kvm_emulate_mwait); | |
1808 | ||
1809 | int kvm_handle_invalid_op(struct kvm_vcpu *vcpu) | |
1810 | { | |
1811 | kvm_queue_exception(vcpu, UD_VECTOR); | |
1812 | return 1; | |
1813 | } | |
1814 | EXPORT_SYMBOL_GPL(kvm_handle_invalid_op); | |
1815 | ||
1816 | int kvm_emulate_monitor(struct kvm_vcpu *vcpu) | |
1817 | { | |
1818 | pr_warn_once("kvm: MONITOR instruction emulated as NOP!\n"); | |
1819 | return kvm_emulate_as_nop(vcpu); | |
1820 | } | |
1821 | EXPORT_SYMBOL_GPL(kvm_emulate_monitor); | |
1822 | ||
d89d04ab | 1823 | static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) |
5a9f5443 | 1824 | { |
4ae7dc97 | 1825 | xfer_to_guest_mode_prepare(); |
5a9f5443 | 1826 | return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || |
72c3c0fe | 1827 | xfer_to_guest_mode_work_pending(); |
5a9f5443 | 1828 | } |
5a9f5443 | 1829 | |
1e9e2622 WL |
1830 | /* |
1831 | * The fast path for frequent and performance sensitive wrmsr emulation, | |
1832 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces | |
1833 | * the latency of virtual IPI by avoiding the expensive bits of transitioning | |
1834 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the | |
1835 | * other cases which must be called after interrupts are enabled on the host. | |
1836 | */ | |
1837 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) | |
1838 | { | |
e1be9ac8 WL |
1839 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) |
1840 | return 1; | |
1841 | ||
1842 | if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) && | |
1e9e2622 | 1843 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
4064a4c6 WL |
1844 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED) && |
1845 | ((u32)(data >> 32) != X2APIC_BROADCAST)) { | |
1e9e2622 | 1846 | |
d5361678 WL |
1847 | data &= ~(1 << 12); |
1848 | kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32)); | |
1e9e2622 | 1849 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32)); |
d5361678 WL |
1850 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data); |
1851 | trace_kvm_apic_write(APIC_ICR, (u32)data); | |
1852 | return 0; | |
1e9e2622 WL |
1853 | } |
1854 | ||
1855 | return 1; | |
1856 | } | |
1857 | ||
ae95f566 WL |
1858 | static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) |
1859 | { | |
1860 | if (!kvm_can_use_hv_timer(vcpu)) | |
1861 | return 1; | |
1862 | ||
1863 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
1864 | return 0; | |
1865 | } | |
1866 | ||
404d5d7b | 1867 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
1e9e2622 WL |
1868 | { |
1869 | u32 msr = kvm_rcx_read(vcpu); | |
8a1038de | 1870 | u64 data; |
404d5d7b | 1871 | fastpath_t ret = EXIT_FASTPATH_NONE; |
1e9e2622 WL |
1872 | |
1873 | switch (msr) { | |
1874 | case APIC_BASE_MSR + (APIC_ICR >> 4): | |
8a1038de | 1875 | data = kvm_read_edx_eax(vcpu); |
404d5d7b WL |
1876 | if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { |
1877 | kvm_skip_emulated_instruction(vcpu); | |
1878 | ret = EXIT_FASTPATH_EXIT_HANDLED; | |
80bc97f2 | 1879 | } |
1e9e2622 | 1880 | break; |
ae95f566 WL |
1881 | case MSR_IA32_TSCDEADLINE: |
1882 | data = kvm_read_edx_eax(vcpu); | |
1883 | if (!handle_fastpath_set_tscdeadline(vcpu, data)) { | |
1884 | kvm_skip_emulated_instruction(vcpu); | |
1885 | ret = EXIT_FASTPATH_REENTER_GUEST; | |
1886 | } | |
1887 | break; | |
1e9e2622 | 1888 | default: |
404d5d7b | 1889 | break; |
1e9e2622 WL |
1890 | } |
1891 | ||
404d5d7b | 1892 | if (ret != EXIT_FASTPATH_NONE) |
1e9e2622 | 1893 | trace_kvm_msr_write(msr, data); |
1e9e2622 | 1894 | |
404d5d7b | 1895 | return ret; |
1e9e2622 WL |
1896 | } |
1897 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); | |
1898 | ||
f20935d8 SC |
1899 | /* |
1900 | * Adapt set_msr() to msr_io()'s calling convention | |
1901 | */ | |
1902 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1903 | { | |
6abe9c13 | 1904 | return kvm_get_msr_ignored_check(vcpu, index, data, true); |
f20935d8 SC |
1905 | } |
1906 | ||
1907 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1908 | { | |
6abe9c13 | 1909 | return kvm_set_msr_ignored_check(vcpu, index, *data, true); |
313a3dc7 CO |
1910 | } |
1911 | ||
16e8d74d | 1912 | #ifdef CONFIG_X86_64 |
53fafdbb MT |
1913 | struct pvclock_clock { |
1914 | int vclock_mode; | |
1915 | u64 cycle_last; | |
1916 | u64 mask; | |
1917 | u32 mult; | |
1918 | u32 shift; | |
917f9475 PB |
1919 | u64 base_cycles; |
1920 | u64 offset; | |
53fafdbb MT |
1921 | }; |
1922 | ||
16e8d74d MT |
1923 | struct pvclock_gtod_data { |
1924 | seqcount_t seq; | |
1925 | ||
53fafdbb MT |
1926 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
1927 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ | |
16e8d74d | 1928 | |
917f9475 | 1929 | ktime_t offs_boot; |
55dd00a7 | 1930 | u64 wall_time_sec; |
16e8d74d MT |
1931 | }; |
1932 | ||
1933 | static struct pvclock_gtod_data pvclock_gtod_data; | |
1934 | ||
1935 | static void update_pvclock_gtod(struct timekeeper *tk) | |
1936 | { | |
1937 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; | |
1938 | ||
1939 | write_seqcount_begin(&vdata->seq); | |
1940 | ||
1941 | /* copy pvclock gtod data */ | |
b95a8a27 | 1942 | vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; |
876e7881 PZ |
1943 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
1944 | vdata->clock.mask = tk->tkr_mono.mask; | |
1945 | vdata->clock.mult = tk->tkr_mono.mult; | |
1946 | vdata->clock.shift = tk->tkr_mono.shift; | |
917f9475 PB |
1947 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
1948 | vdata->clock.offset = tk->tkr_mono.base; | |
16e8d74d | 1949 | |
b95a8a27 | 1950 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; |
53fafdbb MT |
1951 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
1952 | vdata->raw_clock.mask = tk->tkr_raw.mask; | |
1953 | vdata->raw_clock.mult = tk->tkr_raw.mult; | |
1954 | vdata->raw_clock.shift = tk->tkr_raw.shift; | |
917f9475 PB |
1955 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
1956 | vdata->raw_clock.offset = tk->tkr_raw.base; | |
16e8d74d | 1957 | |
55dd00a7 MT |
1958 | vdata->wall_time_sec = tk->xtime_sec; |
1959 | ||
917f9475 | 1960 | vdata->offs_boot = tk->offs_boot; |
53fafdbb | 1961 | |
16e8d74d MT |
1962 | write_seqcount_end(&vdata->seq); |
1963 | } | |
8171cd68 PB |
1964 | |
1965 | static s64 get_kvmclock_base_ns(void) | |
1966 | { | |
1967 | /* Count up from boot time, but with the frequency of the raw clock. */ | |
1968 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); | |
1969 | } | |
1970 | #else | |
1971 | static s64 get_kvmclock_base_ns(void) | |
1972 | { | |
1973 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ | |
1974 | return ktime_get_boottime_ns(); | |
1975 | } | |
16e8d74d MT |
1976 | #endif |
1977 | ||
629b5348 | 1978 | void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs) |
18068523 | 1979 | { |
9ed3c444 AK |
1980 | int version; |
1981 | int r; | |
50d0a0f9 | 1982 | struct pvclock_wall_clock wc; |
629b5348 | 1983 | u32 wc_sec_hi; |
8171cd68 | 1984 | u64 wall_nsec; |
18068523 GOC |
1985 | |
1986 | if (!wall_clock) | |
1987 | return; | |
1988 | ||
9ed3c444 AK |
1989 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
1990 | if (r) | |
1991 | return; | |
1992 | ||
1993 | if (version & 1) | |
1994 | ++version; /* first time write, random junk */ | |
1995 | ||
1996 | ++version; | |
18068523 | 1997 | |
1dab1345 NK |
1998 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
1999 | return; | |
18068523 | 2000 | |
50d0a0f9 GH |
2001 | /* |
2002 | * The guest calculates current wall clock time by adding | |
34c238a1 | 2003 | * system time (updated by kvm_guest_time_update below) to the |
8171cd68 | 2004 | * wall clock specified here. We do the reverse here. |
50d0a0f9 | 2005 | */ |
8171cd68 | 2006 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
50d0a0f9 | 2007 | |
8171cd68 PB |
2008 | wc.nsec = do_div(wall_nsec, 1000000000); |
2009 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ | |
50d0a0f9 | 2010 | wc.version = version; |
18068523 GOC |
2011 | |
2012 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
2013 | ||
629b5348 JM |
2014 | if (sec_hi_ofs) { |
2015 | wc_sec_hi = wall_nsec >> 32; | |
2016 | kvm_write_guest(kvm, wall_clock + sec_hi_ofs, | |
2017 | &wc_sec_hi, sizeof(wc_sec_hi)); | |
2018 | } | |
2019 | ||
18068523 GOC |
2020 | version++; |
2021 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
2022 | } |
2023 | ||
5b9bb0eb OU |
2024 | static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time, |
2025 | bool old_msr, bool host_initiated) | |
2026 | { | |
2027 | struct kvm_arch *ka = &vcpu->kvm->arch; | |
2028 | ||
2029 | if (vcpu->vcpu_id == 0 && !host_initiated) { | |
1e293d1a | 2030 | if (ka->boot_vcpu_runs_old_kvmclock != old_msr) |
5b9bb0eb OU |
2031 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
2032 | ||
2033 | ka->boot_vcpu_runs_old_kvmclock = old_msr; | |
2034 | } | |
2035 | ||
2036 | vcpu->arch.time = system_time; | |
2037 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); | |
2038 | ||
2039 | /* we verify if the enable bit is set... */ | |
2040 | vcpu->arch.pv_time_enabled = false; | |
2041 | if (!(system_time & 1)) | |
2042 | return; | |
2043 | ||
2044 | if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, | |
2045 | &vcpu->arch.pv_time, system_time & ~1ULL, | |
2046 | sizeof(struct pvclock_vcpu_time_info))) | |
2047 | vcpu->arch.pv_time_enabled = true; | |
2048 | ||
2049 | return; | |
2050 | } | |
2051 | ||
50d0a0f9 GH |
2052 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
2053 | { | |
b51012de PB |
2054 | do_shl32_div32(dividend, divisor); |
2055 | return dividend; | |
50d0a0f9 GH |
2056 | } |
2057 | ||
3ae13faa | 2058 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
5f4e3f88 | 2059 | s8 *pshift, u32 *pmultiplier) |
50d0a0f9 | 2060 | { |
5f4e3f88 | 2061 | uint64_t scaled64; |
50d0a0f9 GH |
2062 | int32_t shift = 0; |
2063 | uint64_t tps64; | |
2064 | uint32_t tps32; | |
2065 | ||
3ae13faa PB |
2066 | tps64 = base_hz; |
2067 | scaled64 = scaled_hz; | |
50933623 | 2068 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
50d0a0f9 GH |
2069 | tps64 >>= 1; |
2070 | shift--; | |
2071 | } | |
2072 | ||
2073 | tps32 = (uint32_t)tps64; | |
50933623 JK |
2074 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
2075 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) | |
5f4e3f88 ZA |
2076 | scaled64 >>= 1; |
2077 | else | |
2078 | tps32 <<= 1; | |
50d0a0f9 GH |
2079 | shift++; |
2080 | } | |
2081 | ||
5f4e3f88 ZA |
2082 | *pshift = shift; |
2083 | *pmultiplier = div_frac(scaled64, tps32); | |
50d0a0f9 GH |
2084 | } |
2085 | ||
d828199e | 2086 | #ifdef CONFIG_X86_64 |
16e8d74d | 2087 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
d828199e | 2088 | #endif |
16e8d74d | 2089 | |
c8076604 | 2090 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
69b0049a | 2091 | static unsigned long max_tsc_khz; |
c8076604 | 2092 | |
cc578287 | 2093 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
1e993611 | 2094 | { |
cc578287 ZA |
2095 | u64 v = (u64)khz * (1000000 + ppm); |
2096 | do_div(v, 1000000); | |
2097 | return v; | |
1e993611 JR |
2098 | } |
2099 | ||
381d585c HZ |
2100 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
2101 | { | |
2102 | u64 ratio; | |
2103 | ||
2104 | /* Guest TSC same frequency as host TSC? */ | |
2105 | if (!scale) { | |
2106 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
2107 | return 0; | |
2108 | } | |
2109 | ||
2110 | /* TSC scaling supported? */ | |
2111 | if (!kvm_has_tsc_control) { | |
2112 | if (user_tsc_khz > tsc_khz) { | |
2113 | vcpu->arch.tsc_catchup = 1; | |
2114 | vcpu->arch.tsc_always_catchup = 1; | |
2115 | return 0; | |
2116 | } else { | |
3f16a5c3 | 2117 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
381d585c HZ |
2118 | return -1; |
2119 | } | |
2120 | } | |
2121 | ||
2122 | /* TSC scaling required - calculate ratio */ | |
2123 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, | |
2124 | user_tsc_khz, tsc_khz); | |
2125 | ||
2126 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { | |
3f16a5c3 PB |
2127 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
2128 | user_tsc_khz); | |
381d585c HZ |
2129 | return -1; |
2130 | } | |
2131 | ||
2132 | vcpu->arch.tsc_scaling_ratio = ratio; | |
2133 | return 0; | |
2134 | } | |
2135 | ||
4941b8cb | 2136 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
759379dd | 2137 | { |
cc578287 ZA |
2138 | u32 thresh_lo, thresh_hi; |
2139 | int use_scaling = 0; | |
217fc9cf | 2140 | |
03ba32ca | 2141 | /* tsc_khz can be zero if TSC calibration fails */ |
4941b8cb | 2142 | if (user_tsc_khz == 0) { |
ad721883 HZ |
2143 | /* set tsc_scaling_ratio to a safe value */ |
2144 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
381d585c | 2145 | return -1; |
ad721883 | 2146 | } |
03ba32ca | 2147 | |
c285545f | 2148 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
3ae13faa | 2149 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
cc578287 ZA |
2150 | &vcpu->arch.virtual_tsc_shift, |
2151 | &vcpu->arch.virtual_tsc_mult); | |
4941b8cb | 2152 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
cc578287 ZA |
2153 | |
2154 | /* | |
2155 | * Compute the variation in TSC rate which is acceptable | |
2156 | * within the range of tolerance and decide if the | |
2157 | * rate being applied is within that bounds of the hardware | |
2158 | * rate. If so, no scaling or compensation need be done. | |
2159 | */ | |
2160 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); | |
2161 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); | |
4941b8cb PB |
2162 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
2163 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); | |
cc578287 ZA |
2164 | use_scaling = 1; |
2165 | } | |
4941b8cb | 2166 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
c285545f ZA |
2167 | } |
2168 | ||
2169 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) | |
2170 | { | |
e26101b1 | 2171 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
cc578287 ZA |
2172 | vcpu->arch.virtual_tsc_mult, |
2173 | vcpu->arch.virtual_tsc_shift); | |
e26101b1 | 2174 | tsc += vcpu->arch.this_tsc_write; |
c285545f ZA |
2175 | return tsc; |
2176 | } | |
2177 | ||
b0c39dc6 VK |
2178 | static inline int gtod_is_based_on_tsc(int mode) |
2179 | { | |
b95a8a27 | 2180 | return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; |
b0c39dc6 VK |
2181 | } |
2182 | ||
69b0049a | 2183 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
b48aa97e MT |
2184 | { |
2185 | #ifdef CONFIG_X86_64 | |
2186 | bool vcpus_matched; | |
b48aa97e MT |
2187 | struct kvm_arch *ka = &vcpu->kvm->arch; |
2188 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2189 | ||
2190 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
2191 | atomic_read(&vcpu->kvm->online_vcpus)); | |
2192 | ||
7f187922 MT |
2193 | /* |
2194 | * Once the masterclock is enabled, always perform request in | |
2195 | * order to update it. | |
2196 | * | |
2197 | * In order to enable masterclock, the host clocksource must be TSC | |
2198 | * and the vcpus need to have matched TSCs. When that happens, | |
2199 | * perform request to enable masterclock. | |
2200 | */ | |
2201 | if (ka->use_master_clock || | |
b0c39dc6 | 2202 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
b48aa97e MT |
2203 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
2204 | ||
2205 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, | |
2206 | atomic_read(&vcpu->kvm->online_vcpus), | |
2207 | ka->use_master_clock, gtod->clock.vclock_mode); | |
2208 | #endif | |
2209 | } | |
2210 | ||
35181e86 HZ |
2211 | /* |
2212 | * Multiply tsc by a fixed point number represented by ratio. | |
2213 | * | |
2214 | * The most significant 64-N bits (mult) of ratio represent the | |
2215 | * integral part of the fixed point number; the remaining N bits | |
2216 | * (frac) represent the fractional part, ie. ratio represents a fixed | |
2217 | * point number (mult + frac * 2^(-N)). | |
2218 | * | |
2219 | * N equals to kvm_tsc_scaling_ratio_frac_bits. | |
2220 | */ | |
2221 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) | |
2222 | { | |
2223 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); | |
2224 | } | |
2225 | ||
2226 | u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) | |
2227 | { | |
2228 | u64 _tsc = tsc; | |
2229 | u64 ratio = vcpu->arch.tsc_scaling_ratio; | |
2230 | ||
2231 | if (ratio != kvm_default_tsc_scaling_ratio) | |
2232 | _tsc = __scale_tsc(ratio, tsc); | |
2233 | ||
2234 | return _tsc; | |
2235 | } | |
2236 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); | |
2237 | ||
07c1419a HZ |
2238 | static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
2239 | { | |
2240 | u64 tsc; | |
2241 | ||
2242 | tsc = kvm_scale_tsc(vcpu, rdtsc()); | |
2243 | ||
2244 | return target_tsc - tsc; | |
2245 | } | |
2246 | ||
4ba76538 HZ |
2247 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
2248 | { | |
56ba77a4 | 2249 | return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc); |
4ba76538 HZ |
2250 | } |
2251 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); | |
2252 | ||
a545ab6a LC |
2253 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
2254 | { | |
56ba77a4 | 2255 | vcpu->arch.l1_tsc_offset = offset; |
b3646477 | 2256 | vcpu->arch.tsc_offset = static_call(kvm_x86_write_l1_tsc_offset)(vcpu, offset); |
a545ab6a LC |
2257 | } |
2258 | ||
b0c39dc6 VK |
2259 | static inline bool kvm_check_tsc_unstable(void) |
2260 | { | |
2261 | #ifdef CONFIG_X86_64 | |
2262 | /* | |
2263 | * TSC is marked unstable when we're running on Hyper-V, | |
2264 | * 'TSC page' clocksource is good. | |
2265 | */ | |
b95a8a27 | 2266 | if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) |
b0c39dc6 VK |
2267 | return false; |
2268 | #endif | |
2269 | return check_tsc_unstable(); | |
2270 | } | |
2271 | ||
0c899c25 | 2272 | static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) |
99e3e30a ZA |
2273 | { |
2274 | struct kvm *kvm = vcpu->kvm; | |
f38e098f | 2275 | u64 offset, ns, elapsed; |
99e3e30a | 2276 | unsigned long flags; |
b48aa97e | 2277 | bool matched; |
0d3da0d2 | 2278 | bool already_matched; |
c5e8ec8e | 2279 | bool synchronizing = false; |
99e3e30a | 2280 | |
038f8c11 | 2281 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
07c1419a | 2282 | offset = kvm_compute_tsc_offset(vcpu, data); |
8171cd68 | 2283 | ns = get_kvmclock_base_ns(); |
f38e098f | 2284 | elapsed = ns - kvm->arch.last_tsc_nsec; |
5d3cb0f6 | 2285 | |
03ba32ca | 2286 | if (vcpu->arch.virtual_tsc_khz) { |
0c899c25 | 2287 | if (data == 0) { |
bd8fab39 DP |
2288 | /* |
2289 | * detection of vcpu initialization -- need to sync | |
2290 | * with other vCPUs. This particularly helps to keep | |
2291 | * kvm_clock stable after CPU hotplug | |
2292 | */ | |
2293 | synchronizing = true; | |
2294 | } else { | |
2295 | u64 tsc_exp = kvm->arch.last_tsc_write + | |
2296 | nsec_to_cycles(vcpu, elapsed); | |
2297 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; | |
2298 | /* | |
2299 | * Special case: TSC write with a small delta (1 second) | |
2300 | * of virtual cycle time against real time is | |
2301 | * interpreted as an attempt to synchronize the CPU. | |
2302 | */ | |
2303 | synchronizing = data < tsc_exp + tsc_hz && | |
2304 | data + tsc_hz > tsc_exp; | |
2305 | } | |
c5e8ec8e | 2306 | } |
f38e098f ZA |
2307 | |
2308 | /* | |
5d3cb0f6 ZA |
2309 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
2310 | * TSC, we add elapsed time in this computation. We could let the | |
2311 | * compensation code attempt to catch up if we fall behind, but | |
2312 | * it's better to try to match offsets from the beginning. | |
2313 | */ | |
c5e8ec8e | 2314 | if (synchronizing && |
5d3cb0f6 | 2315 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
b0c39dc6 | 2316 | if (!kvm_check_tsc_unstable()) { |
e26101b1 | 2317 | offset = kvm->arch.cur_tsc_offset; |
f38e098f | 2318 | } else { |
857e4099 | 2319 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
5d3cb0f6 | 2320 | data += delta; |
07c1419a | 2321 | offset = kvm_compute_tsc_offset(vcpu, data); |
f38e098f | 2322 | } |
b48aa97e | 2323 | matched = true; |
0d3da0d2 | 2324 | already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); |
e26101b1 ZA |
2325 | } else { |
2326 | /* | |
2327 | * We split periods of matched TSC writes into generations. | |
2328 | * For each generation, we track the original measured | |
2329 | * nanosecond time, offset, and write, so if TSCs are in | |
2330 | * sync, we can match exact offset, and if not, we can match | |
4a969980 | 2331 | * exact software computation in compute_guest_tsc() |
e26101b1 ZA |
2332 | * |
2333 | * These values are tracked in kvm->arch.cur_xxx variables. | |
2334 | */ | |
2335 | kvm->arch.cur_tsc_generation++; | |
2336 | kvm->arch.cur_tsc_nsec = ns; | |
2337 | kvm->arch.cur_tsc_write = data; | |
2338 | kvm->arch.cur_tsc_offset = offset; | |
b48aa97e | 2339 | matched = false; |
f38e098f | 2340 | } |
e26101b1 ZA |
2341 | |
2342 | /* | |
2343 | * We also track th most recent recorded KHZ, write and time to | |
2344 | * allow the matching interval to be extended at each write. | |
2345 | */ | |
f38e098f ZA |
2346 | kvm->arch.last_tsc_nsec = ns; |
2347 | kvm->arch.last_tsc_write = data; | |
5d3cb0f6 | 2348 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; |
99e3e30a | 2349 | |
b183aa58 | 2350 | vcpu->arch.last_guest_tsc = data; |
e26101b1 ZA |
2351 | |
2352 | /* Keep track of which generation this VCPU has synchronized to */ | |
2353 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; | |
2354 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; | |
2355 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; | |
2356 | ||
a545ab6a | 2357 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
e26101b1 | 2358 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
b48aa97e MT |
2359 | |
2360 | spin_lock(&kvm->arch.pvclock_gtod_sync_lock); | |
0d3da0d2 | 2361 | if (!matched) { |
b48aa97e | 2362 | kvm->arch.nr_vcpus_matched_tsc = 0; |
0d3da0d2 TG |
2363 | } else if (!already_matched) { |
2364 | kvm->arch.nr_vcpus_matched_tsc++; | |
2365 | } | |
b48aa97e MT |
2366 | |
2367 | kvm_track_tsc_matching(vcpu); | |
2368 | spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); | |
99e3e30a | 2369 | } |
e26101b1 | 2370 | |
58ea6767 HZ |
2371 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
2372 | s64 adjustment) | |
2373 | { | |
56ba77a4 | 2374 | u64 tsc_offset = vcpu->arch.l1_tsc_offset; |
326e7425 | 2375 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
58ea6767 HZ |
2376 | } |
2377 | ||
2378 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) | |
2379 | { | |
2380 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) | |
2381 | WARN_ON(adjustment < 0); | |
2382 | adjustment = kvm_scale_tsc(vcpu, (u64) adjustment); | |
ea26e4ec | 2383 | adjust_tsc_offset_guest(vcpu, adjustment); |
58ea6767 HZ |
2384 | } |
2385 | ||
d828199e MT |
2386 | #ifdef CONFIG_X86_64 |
2387 | ||
a5a1d1c2 | 2388 | static u64 read_tsc(void) |
d828199e | 2389 | { |
a5a1d1c2 | 2390 | u64 ret = (u64)rdtsc_ordered(); |
03b9730b | 2391 | u64 last = pvclock_gtod_data.clock.cycle_last; |
d828199e MT |
2392 | |
2393 | if (likely(ret >= last)) | |
2394 | return ret; | |
2395 | ||
2396 | /* | |
2397 | * GCC likes to generate cmov here, but this branch is extremely | |
6a6256f9 | 2398 | * predictable (it's just a function of time and the likely is |
d828199e MT |
2399 | * very likely) and there's a data dependence, so force GCC |
2400 | * to generate a branch instead. I don't barrier() because | |
2401 | * we don't actually need a barrier, and if this function | |
2402 | * ever gets inlined it will generate worse code. | |
2403 | */ | |
2404 | asm volatile (""); | |
2405 | return last; | |
2406 | } | |
2407 | ||
53fafdbb MT |
2408 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
2409 | int *mode) | |
d828199e MT |
2410 | { |
2411 | long v; | |
b0c39dc6 VK |
2412 | u64 tsc_pg_val; |
2413 | ||
53fafdbb | 2414 | switch (clock->vclock_mode) { |
b95a8a27 | 2415 | case VDSO_CLOCKMODE_HVCLOCK: |
b0c39dc6 VK |
2416 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
2417 | tsc_timestamp); | |
2418 | if (tsc_pg_val != U64_MAX) { | |
2419 | /* TSC page valid */ | |
b95a8a27 | 2420 | *mode = VDSO_CLOCKMODE_HVCLOCK; |
53fafdbb MT |
2421 | v = (tsc_pg_val - clock->cycle_last) & |
2422 | clock->mask; | |
b0c39dc6 VK |
2423 | } else { |
2424 | /* TSC page invalid */ | |
b95a8a27 | 2425 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 VK |
2426 | } |
2427 | break; | |
b95a8a27 TG |
2428 | case VDSO_CLOCKMODE_TSC: |
2429 | *mode = VDSO_CLOCKMODE_TSC; | |
b0c39dc6 | 2430 | *tsc_timestamp = read_tsc(); |
53fafdbb MT |
2431 | v = (*tsc_timestamp - clock->cycle_last) & |
2432 | clock->mask; | |
b0c39dc6 VK |
2433 | break; |
2434 | default: | |
b95a8a27 | 2435 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 | 2436 | } |
d828199e | 2437 | |
b95a8a27 | 2438 | if (*mode == VDSO_CLOCKMODE_NONE) |
b0c39dc6 | 2439 | *tsc_timestamp = v = 0; |
d828199e | 2440 | |
53fafdbb | 2441 | return v * clock->mult; |
d828199e MT |
2442 | } |
2443 | ||
53fafdbb | 2444 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
d828199e | 2445 | { |
cbcf2dd3 | 2446 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
d828199e | 2447 | unsigned long seq; |
d828199e | 2448 | int mode; |
cbcf2dd3 | 2449 | u64 ns; |
d828199e | 2450 | |
d828199e MT |
2451 | do { |
2452 | seq = read_seqcount_begin(>od->seq); | |
917f9475 | 2453 | ns = gtod->raw_clock.base_cycles; |
53fafdbb | 2454 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
917f9475 PB |
2455 | ns >>= gtod->raw_clock.shift; |
2456 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); | |
d828199e | 2457 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
cbcf2dd3 | 2458 | *t = ns; |
d828199e MT |
2459 | |
2460 | return mode; | |
2461 | } | |
2462 | ||
899a31f5 | 2463 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
55dd00a7 MT |
2464 | { |
2465 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2466 | unsigned long seq; | |
2467 | int mode; | |
2468 | u64 ns; | |
2469 | ||
2470 | do { | |
2471 | seq = read_seqcount_begin(>od->seq); | |
55dd00a7 | 2472 | ts->tv_sec = gtod->wall_time_sec; |
917f9475 | 2473 | ns = gtod->clock.base_cycles; |
53fafdbb | 2474 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
55dd00a7 MT |
2475 | ns >>= gtod->clock.shift; |
2476 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); | |
2477 | ||
2478 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); | |
2479 | ts->tv_nsec = ns; | |
2480 | ||
2481 | return mode; | |
2482 | } | |
2483 | ||
b0c39dc6 VK |
2484 | /* returns true if host is using TSC based clocksource */ |
2485 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) | |
d828199e | 2486 | { |
d828199e | 2487 | /* checked again under seqlock below */ |
b0c39dc6 | 2488 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
d828199e MT |
2489 | return false; |
2490 | ||
53fafdbb | 2491 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
b0c39dc6 | 2492 | tsc_timestamp)); |
d828199e | 2493 | } |
55dd00a7 | 2494 | |
b0c39dc6 | 2495 | /* returns true if host is using TSC based clocksource */ |
899a31f5 | 2496 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
b0c39dc6 | 2497 | u64 *tsc_timestamp) |
55dd00a7 MT |
2498 | { |
2499 | /* checked again under seqlock below */ | |
b0c39dc6 | 2500 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
55dd00a7 MT |
2501 | return false; |
2502 | ||
b0c39dc6 | 2503 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
55dd00a7 | 2504 | } |
d828199e MT |
2505 | #endif |
2506 | ||
2507 | /* | |
2508 | * | |
b48aa97e MT |
2509 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
2510 | * across virtual CPUs, the following condition is possible. | |
2511 | * Each numbered line represents an event visible to both | |
d828199e MT |
2512 | * CPUs at the next numbered event. |
2513 | * | |
2514 | * "timespecX" represents host monotonic time. "tscX" represents | |
2515 | * RDTSC value. | |
2516 | * | |
2517 | * VCPU0 on CPU0 | VCPU1 on CPU1 | |
2518 | * | |
2519 | * 1. read timespec0,tsc0 | |
2520 | * 2. | timespec1 = timespec0 + N | |
2521 | * | tsc1 = tsc0 + M | |
2522 | * 3. transition to guest | transition to guest | |
2523 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | | |
2524 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) | |
2525 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) | |
2526 | * | |
2527 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: | |
2528 | * | |
2529 | * - ret0 < ret1 | |
2530 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) | |
2531 | * ... | |
2532 | * - 0 < N - M => M < N | |
2533 | * | |
2534 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not | |
2535 | * always the case (the difference between two distinct xtime instances | |
2536 | * might be smaller then the difference between corresponding TSC reads, | |
2537 | * when updating guest vcpus pvclock areas). | |
2538 | * | |
2539 | * To avoid that problem, do not allow visibility of distinct | |
2540 | * system_timestamp/tsc_timestamp values simultaneously: use a master | |
2541 | * copy of host monotonic time values. Update that master copy | |
2542 | * in lockstep. | |
2543 | * | |
b48aa97e | 2544 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
d828199e MT |
2545 | * |
2546 | */ | |
2547 | ||
2548 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) | |
2549 | { | |
2550 | #ifdef CONFIG_X86_64 | |
2551 | struct kvm_arch *ka = &kvm->arch; | |
2552 | int vclock_mode; | |
b48aa97e MT |
2553 | bool host_tsc_clocksource, vcpus_matched; |
2554 | ||
2555 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
2556 | atomic_read(&kvm->online_vcpus)); | |
d828199e MT |
2557 | |
2558 | /* | |
2559 | * If the host uses TSC clock, then passthrough TSC as stable | |
2560 | * to the guest. | |
2561 | */ | |
b48aa97e | 2562 | host_tsc_clocksource = kvm_get_time_and_clockread( |
d828199e MT |
2563 | &ka->master_kernel_ns, |
2564 | &ka->master_cycle_now); | |
2565 | ||
16a96021 | 2566 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
a826faf1 | 2567 | && !ka->backwards_tsc_observed |
54750f2c | 2568 | && !ka->boot_vcpu_runs_old_kvmclock; |
b48aa97e | 2569 | |
d828199e MT |
2570 | if (ka->use_master_clock) |
2571 | atomic_set(&kvm_guest_has_master_clock, 1); | |
2572 | ||
2573 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; | |
b48aa97e MT |
2574 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
2575 | vcpus_matched); | |
d828199e MT |
2576 | #endif |
2577 | } | |
2578 | ||
2860c4b1 PB |
2579 | void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
2580 | { | |
2581 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); | |
2582 | } | |
2583 | ||
2e762ff7 MT |
2584 | static void kvm_gen_update_masterclock(struct kvm *kvm) |
2585 | { | |
2586 | #ifdef CONFIG_X86_64 | |
2587 | int i; | |
2588 | struct kvm_vcpu *vcpu; | |
2589 | struct kvm_arch *ka = &kvm->arch; | |
2590 | ||
2591 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2592 | kvm_make_mclock_inprogress_request(kvm); | |
2593 | /* no guest entries from this point */ | |
2594 | pvclock_update_vm_gtod_copy(kvm); | |
2595 | ||
2596 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 2597 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
2e762ff7 MT |
2598 | |
2599 | /* guest entries allowed */ | |
2600 | kvm_for_each_vcpu(i, vcpu, kvm) | |
72875d8a | 2601 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
2e762ff7 MT |
2602 | |
2603 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2604 | #endif | |
2605 | } | |
2606 | ||
e891a32e | 2607 | u64 get_kvmclock_ns(struct kvm *kvm) |
108b249c | 2608 | { |
108b249c | 2609 | struct kvm_arch *ka = &kvm->arch; |
8b953440 | 2610 | struct pvclock_vcpu_time_info hv_clock; |
e2c2206a | 2611 | u64 ret; |
108b249c | 2612 | |
8b953440 PB |
2613 | spin_lock(&ka->pvclock_gtod_sync_lock); |
2614 | if (!ka->use_master_clock) { | |
2615 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
8171cd68 | 2616 | return get_kvmclock_base_ns() + ka->kvmclock_offset; |
108b249c PB |
2617 | } |
2618 | ||
8b953440 PB |
2619 | hv_clock.tsc_timestamp = ka->master_cycle_now; |
2620 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; | |
2621 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2622 | ||
e2c2206a WL |
2623 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
2624 | get_cpu(); | |
2625 | ||
e70b57a6 WL |
2626 | if (__this_cpu_read(cpu_tsc_khz)) { |
2627 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, | |
2628 | &hv_clock.tsc_shift, | |
2629 | &hv_clock.tsc_to_system_mul); | |
2630 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); | |
2631 | } else | |
8171cd68 | 2632 | ret = get_kvmclock_base_ns() + ka->kvmclock_offset; |
e2c2206a WL |
2633 | |
2634 | put_cpu(); | |
2635 | ||
2636 | return ret; | |
108b249c PB |
2637 | } |
2638 | ||
aa096aa0 JM |
2639 | static void kvm_setup_pvclock_page(struct kvm_vcpu *v, |
2640 | struct gfn_to_hva_cache *cache, | |
2641 | unsigned int offset) | |
0d6dd2ff PB |
2642 | { |
2643 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
2644 | struct pvclock_vcpu_time_info guest_hv_clock; | |
2645 | ||
aa096aa0 JM |
2646 | if (unlikely(kvm_read_guest_offset_cached(v->kvm, cache, |
2647 | &guest_hv_clock, offset, sizeof(guest_hv_clock)))) | |
0d6dd2ff PB |
2648 | return; |
2649 | ||
2650 | /* This VCPU is paused, but it's legal for a guest to read another | |
2651 | * VCPU's kvmclock, so we really have to follow the specification where | |
2652 | * it says that version is odd if data is being modified, and even after | |
2653 | * it is consistent. | |
2654 | * | |
2655 | * Version field updates must be kept separate. This is because | |
2656 | * kvm_write_guest_cached might use a "rep movs" instruction, and | |
2657 | * writes within a string instruction are weakly ordered. So there | |
2658 | * are three writes overall. | |
2659 | * | |
2660 | * As a small optimization, only write the version field in the first | |
2661 | * and third write. The vcpu->pv_time cache is still valid, because the | |
2662 | * version field is the first in the struct. | |
2663 | */ | |
2664 | BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0); | |
2665 | ||
51c4b8bb LA |
2666 | if (guest_hv_clock.version & 1) |
2667 | ++guest_hv_clock.version; /* first time write, random junk */ | |
2668 | ||
0d6dd2ff | 2669 | vcpu->hv_clock.version = guest_hv_clock.version + 1; |
aa096aa0 JM |
2670 | kvm_write_guest_offset_cached(v->kvm, cache, |
2671 | &vcpu->hv_clock, offset, | |
2672 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2673 | |
2674 | smp_wmb(); | |
2675 | ||
2676 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ | |
2677 | vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); | |
2678 | ||
2679 | if (vcpu->pvclock_set_guest_stopped_request) { | |
2680 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; | |
2681 | vcpu->pvclock_set_guest_stopped_request = false; | |
2682 | } | |
2683 | ||
2684 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); | |
2685 | ||
aa096aa0 JM |
2686 | kvm_write_guest_offset_cached(v->kvm, cache, |
2687 | &vcpu->hv_clock, offset, | |
2688 | sizeof(vcpu->hv_clock)); | |
0d6dd2ff PB |
2689 | |
2690 | smp_wmb(); | |
2691 | ||
2692 | vcpu->hv_clock.version++; | |
aa096aa0 JM |
2693 | kvm_write_guest_offset_cached(v->kvm, cache, |
2694 | &vcpu->hv_clock, offset, | |
2695 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2696 | } |
2697 | ||
34c238a1 | 2698 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
18068523 | 2699 | { |
78db6a50 | 2700 | unsigned long flags, tgt_tsc_khz; |
18068523 | 2701 | struct kvm_vcpu_arch *vcpu = &v->arch; |
d828199e | 2702 | struct kvm_arch *ka = &v->kvm->arch; |
f25e656d | 2703 | s64 kernel_ns; |
d828199e | 2704 | u64 tsc_timestamp, host_tsc; |
51d59c6b | 2705 | u8 pvclock_flags; |
d828199e MT |
2706 | bool use_master_clock; |
2707 | ||
2708 | kernel_ns = 0; | |
2709 | host_tsc = 0; | |
18068523 | 2710 | |
d828199e MT |
2711 | /* |
2712 | * If the host uses TSC clock, then passthrough TSC as stable | |
2713 | * to the guest. | |
2714 | */ | |
2715 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2716 | use_master_clock = ka->use_master_clock; | |
2717 | if (use_master_clock) { | |
2718 | host_tsc = ka->master_cycle_now; | |
2719 | kernel_ns = ka->master_kernel_ns; | |
2720 | } | |
2721 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
c09664bb MT |
2722 | |
2723 | /* Keep irq disabled to prevent changes to the clock */ | |
2724 | local_irq_save(flags); | |
78db6a50 PB |
2725 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
2726 | if (unlikely(tgt_tsc_khz == 0)) { | |
c09664bb MT |
2727 | local_irq_restore(flags); |
2728 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); | |
2729 | return 1; | |
2730 | } | |
d828199e | 2731 | if (!use_master_clock) { |
4ea1636b | 2732 | host_tsc = rdtsc(); |
8171cd68 | 2733 | kernel_ns = get_kvmclock_base_ns(); |
d828199e MT |
2734 | } |
2735 | ||
4ba76538 | 2736 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
d828199e | 2737 | |
c285545f ZA |
2738 | /* |
2739 | * We may have to catch up the TSC to match elapsed wall clock | |
2740 | * time for two reasons, even if kvmclock is used. | |
2741 | * 1) CPU could have been running below the maximum TSC rate | |
2742 | * 2) Broken TSC compensation resets the base at each VCPU | |
2743 | * entry to avoid unknown leaps of TSC even when running | |
2744 | * again on the same CPU. This may cause apparent elapsed | |
2745 | * time to disappear, and the guest to stand still or run | |
2746 | * very slowly. | |
2747 | */ | |
2748 | if (vcpu->tsc_catchup) { | |
2749 | u64 tsc = compute_guest_tsc(v, kernel_ns); | |
2750 | if (tsc > tsc_timestamp) { | |
f1e2b260 | 2751 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
c285545f ZA |
2752 | tsc_timestamp = tsc; |
2753 | } | |
50d0a0f9 GH |
2754 | } |
2755 | ||
18068523 GOC |
2756 | local_irq_restore(flags); |
2757 | ||
0d6dd2ff | 2758 | /* With all the info we got, fill in the values */ |
18068523 | 2759 | |
78db6a50 PB |
2760 | if (kvm_has_tsc_control) |
2761 | tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz); | |
2762 | ||
2763 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { | |
3ae13faa | 2764 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
5f4e3f88 ZA |
2765 | &vcpu->hv_clock.tsc_shift, |
2766 | &vcpu->hv_clock.tsc_to_system_mul); | |
78db6a50 | 2767 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
8cfdc000 ZA |
2768 | } |
2769 | ||
1d5f066e | 2770 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
759379dd | 2771 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
28e4639a | 2772 | vcpu->last_guest_tsc = tsc_timestamp; |
51d59c6b | 2773 | |
d828199e | 2774 | /* If the host uses TSC clocksource, then it is stable */ |
0d6dd2ff | 2775 | pvclock_flags = 0; |
d828199e MT |
2776 | if (use_master_clock) |
2777 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; | |
2778 | ||
78c0337a MT |
2779 | vcpu->hv_clock.flags = pvclock_flags; |
2780 | ||
095cf55d | 2781 | if (vcpu->pv_time_enabled) |
aa096aa0 JM |
2782 | kvm_setup_pvclock_page(v, &vcpu->pv_time, 0); |
2783 | if (vcpu->xen.vcpu_info_set) | |
2784 | kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_info_cache, | |
2785 | offsetof(struct compat_vcpu_info, time)); | |
f2340cd9 JM |
2786 | if (vcpu->xen.vcpu_time_info_set) |
2787 | kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0); | |
095cf55d PB |
2788 | if (v == kvm_get_vcpu(v->kvm, 0)) |
2789 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); | |
8cfdc000 | 2790 | return 0; |
c8076604 GH |
2791 | } |
2792 | ||
0061d53d MT |
2793 | /* |
2794 | * kvmclock updates which are isolated to a given vcpu, such as | |
2795 | * vcpu->cpu migration, should not allow system_timestamp from | |
2796 | * the rest of the vcpus to remain static. Otherwise ntp frequency | |
2797 | * correction applies to one vcpu's system_timestamp but not | |
2798 | * the others. | |
2799 | * | |
2800 | * So in those cases, request a kvmclock update for all vcpus. | |
7e44e449 AJ |
2801 | * We need to rate-limit these requests though, as they can |
2802 | * considerably slow guests that have a large number of vcpus. | |
2803 | * The time for a remote vcpu to update its kvmclock is bound | |
2804 | * by the delay we use to rate-limit the updates. | |
0061d53d MT |
2805 | */ |
2806 | ||
7e44e449 AJ |
2807 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
2808 | ||
2809 | static void kvmclock_update_fn(struct work_struct *work) | |
0061d53d MT |
2810 | { |
2811 | int i; | |
7e44e449 AJ |
2812 | struct delayed_work *dwork = to_delayed_work(work); |
2813 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2814 | kvmclock_update_work); | |
2815 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
0061d53d MT |
2816 | struct kvm_vcpu *vcpu; |
2817 | ||
2818 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
105b21bb | 2819 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0061d53d MT |
2820 | kvm_vcpu_kick(vcpu); |
2821 | } | |
2822 | } | |
2823 | ||
7e44e449 AJ |
2824 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
2825 | { | |
2826 | struct kvm *kvm = v->kvm; | |
2827 | ||
105b21bb | 2828 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
7e44e449 AJ |
2829 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
2830 | KVMCLOCK_UPDATE_DELAY); | |
2831 | } | |
2832 | ||
332967a3 AJ |
2833 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
2834 | ||
2835 | static void kvmclock_sync_fn(struct work_struct *work) | |
2836 | { | |
2837 | struct delayed_work *dwork = to_delayed_work(work); | |
2838 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2839 | kvmclock_sync_work); | |
2840 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
2841 | ||
630994b3 MT |
2842 | if (!kvmclock_periodic_sync) |
2843 | return; | |
2844 | ||
332967a3 AJ |
2845 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
2846 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
2847 | KVMCLOCK_SYNC_PERIOD); | |
2848 | } | |
2849 | ||
191c8137 BP |
2850 | /* |
2851 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. | |
2852 | */ | |
2853 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) | |
2854 | { | |
2855 | /* McStatusWrEn enabled? */ | |
23493d0a | 2856 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
191c8137 BP |
2857 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
2858 | ||
2859 | return false; | |
2860 | } | |
2861 | ||
9ffd986c | 2862 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 2863 | { |
890ca9ae HY |
2864 | u64 mcg_cap = vcpu->arch.mcg_cap; |
2865 | unsigned bank_num = mcg_cap & 0xff; | |
9ffd986c WL |
2866 | u32 msr = msr_info->index; |
2867 | u64 data = msr_info->data; | |
890ca9ae | 2868 | |
15c4a640 | 2869 | switch (msr) { |
15c4a640 | 2870 | case MSR_IA32_MCG_STATUS: |
890ca9ae | 2871 | vcpu->arch.mcg_status = data; |
15c4a640 | 2872 | break; |
c7ac679c | 2873 | case MSR_IA32_MCG_CTL: |
44883f01 PB |
2874 | if (!(mcg_cap & MCG_CTL_P) && |
2875 | (data || !msr_info->host_initiated)) | |
890ca9ae HY |
2876 | return 1; |
2877 | if (data != 0 && data != ~(u64)0) | |
44883f01 | 2878 | return 1; |
890ca9ae HY |
2879 | vcpu->arch.mcg_ctl = data; |
2880 | break; | |
2881 | default: | |
2882 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 2883 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
2884 | u32 offset = array_index_nospec( |
2885 | msr - MSR_IA32_MC0_CTL, | |
2886 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
2887 | ||
114be429 AP |
2888 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
2889 | * some Linux kernels though clear bit 10 in bank 4 to | |
2890 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore | |
2891 | * this to avoid an uncatched #GP in the guest | |
2892 | */ | |
890ca9ae | 2893 | if ((offset & 0x3) == 0 && |
114be429 | 2894 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
890ca9ae | 2895 | return -1; |
191c8137 BP |
2896 | |
2897 | /* MCi_STATUS */ | |
9ffd986c | 2898 | if (!msr_info->host_initiated && |
191c8137 BP |
2899 | (offset & 0x3) == 1 && data != 0) { |
2900 | if (!can_set_mci_status(vcpu)) | |
2901 | return -1; | |
2902 | } | |
2903 | ||
890ca9ae HY |
2904 | vcpu->arch.mce_banks[offset] = data; |
2905 | break; | |
2906 | } | |
2907 | return 1; | |
2908 | } | |
2909 | return 0; | |
2910 | } | |
2911 | ||
2635b5c4 VK |
2912 | static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) |
2913 | { | |
2914 | u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; | |
2915 | ||
2916 | return (vcpu->arch.apf.msr_en_val & mask) == mask; | |
2917 | } | |
2918 | ||
344d9588 GN |
2919 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
2920 | { | |
2921 | gpa_t gpa = data & ~0x3f; | |
2922 | ||
2635b5c4 VK |
2923 | /* Bits 4:5 are reserved, Should be zero */ |
2924 | if (data & 0x30) | |
344d9588 GN |
2925 | return 1; |
2926 | ||
66570e96 OU |
2927 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) && |
2928 | (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT)) | |
2929 | return 1; | |
2930 | ||
2931 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) && | |
2932 | (data & KVM_ASYNC_PF_DELIVERY_AS_INT)) | |
2933 | return 1; | |
2934 | ||
9d3c447c | 2935 | if (!lapic_in_kernel(vcpu)) |
d831de17 | 2936 | return data ? 1 : 0; |
9d3c447c | 2937 | |
2635b5c4 | 2938 | vcpu->arch.apf.msr_en_val = data; |
344d9588 | 2939 | |
2635b5c4 | 2940 | if (!kvm_pv_async_pf_enabled(vcpu)) { |
344d9588 GN |
2941 | kvm_clear_async_pf_completion_queue(vcpu); |
2942 | kvm_async_pf_hash_reset(vcpu); | |
2943 | return 0; | |
2944 | } | |
2945 | ||
4e335d9e | 2946 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
68fd66f1 | 2947 | sizeof(u64))) |
344d9588 GN |
2948 | return 1; |
2949 | ||
6adba527 | 2950 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
52a5c155 | 2951 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
2635b5c4 | 2952 | |
344d9588 | 2953 | kvm_async_pf_wakeup_all(vcpu); |
2635b5c4 VK |
2954 | |
2955 | return 0; | |
2956 | } | |
2957 | ||
2958 | static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data) | |
2959 | { | |
2960 | /* Bits 8-63 are reserved */ | |
2961 | if (data >> 8) | |
2962 | return 1; | |
2963 | ||
2964 | if (!lapic_in_kernel(vcpu)) | |
2965 | return 1; | |
2966 | ||
2967 | vcpu->arch.apf.msr_int_val = data; | |
2968 | ||
2969 | vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; | |
2970 | ||
344d9588 GN |
2971 | return 0; |
2972 | } | |
2973 | ||
12f9a48f GC |
2974 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
2975 | { | |
0b79459b | 2976 | vcpu->arch.pv_time_enabled = false; |
49dedf0d | 2977 | vcpu->arch.time = 0; |
12f9a48f GC |
2978 | } |
2979 | ||
7780938c | 2980 | static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) |
f38a7b75 WL |
2981 | { |
2982 | ++vcpu->stat.tlb_flush; | |
b3646477 | 2983 | static_call(kvm_x86_tlb_flush_all)(vcpu); |
f38a7b75 WL |
2984 | } |
2985 | ||
0baedd79 VK |
2986 | static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) |
2987 | { | |
2988 | ++vcpu->stat.tlb_flush; | |
b3646477 | 2989 | static_call(kvm_x86_tlb_flush_guest)(vcpu); |
0baedd79 VK |
2990 | } |
2991 | ||
c9aaa895 GC |
2992 | static void record_steal_time(struct kvm_vcpu *vcpu) |
2993 | { | |
b0431382 BO |
2994 | struct kvm_host_map map; |
2995 | struct kvm_steal_time *st; | |
2996 | ||
30b5c851 DW |
2997 | if (kvm_xen_msr_enabled(vcpu->kvm)) { |
2998 | kvm_xen_runstate_set_running(vcpu); | |
2999 | return; | |
3000 | } | |
3001 | ||
c9aaa895 GC |
3002 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
3003 | return; | |
3004 | ||
b0431382 BO |
3005 | /* -EAGAIN is returned in atomic context so we can just return. */ |
3006 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, | |
3007 | &map, &vcpu->arch.st.cache, false)) | |
c9aaa895 GC |
3008 | return; |
3009 | ||
b0431382 BO |
3010 | st = map.hva + |
3011 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
3012 | ||
f38a7b75 WL |
3013 | /* |
3014 | * Doing a TLB flush here, on the guest's behalf, can avoid | |
3015 | * expensive IPIs. | |
3016 | */ | |
66570e96 OU |
3017 | if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { |
3018 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, | |
3019 | st->preempted & KVM_VCPU_FLUSH_TLB); | |
3020 | if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) | |
3021 | kvm_vcpu_flush_tlb_guest(vcpu); | |
3022 | } | |
0b9f6c46 | 3023 | |
a6bd811f | 3024 | vcpu->arch.st.preempted = 0; |
35f3fae1 | 3025 | |
b0431382 BO |
3026 | if (st->version & 1) |
3027 | st->version += 1; /* first time write, random junk */ | |
35f3fae1 | 3028 | |
b0431382 | 3029 | st->version += 1; |
35f3fae1 WL |
3030 | |
3031 | smp_wmb(); | |
3032 | ||
b0431382 | 3033 | st->steal += current->sched_info.run_delay - |
c54cdf14 LC |
3034 | vcpu->arch.st.last_steal; |
3035 | vcpu->arch.st.last_steal = current->sched_info.run_delay; | |
35f3fae1 | 3036 | |
35f3fae1 WL |
3037 | smp_wmb(); |
3038 | ||
b0431382 | 3039 | st->version += 1; |
c9aaa895 | 3040 | |
b0431382 | 3041 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); |
c9aaa895 GC |
3042 | } |
3043 | ||
8fe8ab46 | 3044 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 3045 | { |
5753785f | 3046 | bool pr = false; |
8fe8ab46 WA |
3047 | u32 msr = msr_info->index; |
3048 | u64 data = msr_info->data; | |
5753785f | 3049 | |
1232f8e6 | 3050 | if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) |
23200b7a | 3051 | return kvm_xen_write_hypercall_page(vcpu, data); |
1232f8e6 | 3052 | |
15c4a640 | 3053 | switch (msr) { |
2e32b719 | 3054 | case MSR_AMD64_NB_CFG: |
2e32b719 BP |
3055 | case MSR_IA32_UCODE_WRITE: |
3056 | case MSR_VM_HSAVE_PA: | |
3057 | case MSR_AMD64_PATCH_LOADER: | |
3058 | case MSR_AMD64_BU_CFG2: | |
405a353a | 3059 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3060 | case MSR_F15H_EX_CFG: |
2e32b719 BP |
3061 | break; |
3062 | ||
518e7b94 WL |
3063 | case MSR_IA32_UCODE_REV: |
3064 | if (msr_info->host_initiated) | |
3065 | vcpu->arch.microcode_version = data; | |
3066 | break; | |
0cf9135b SC |
3067 | case MSR_IA32_ARCH_CAPABILITIES: |
3068 | if (!msr_info->host_initiated) | |
3069 | return 1; | |
3070 | vcpu->arch.arch_capabilities = data; | |
3071 | break; | |
d574c539 VK |
3072 | case MSR_IA32_PERF_CAPABILITIES: { |
3073 | struct kvm_msr_entry msr_ent = {.index = msr, .data = 0}; | |
3074 | ||
3075 | if (!msr_info->host_initiated) | |
3076 | return 1; | |
3077 | if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM) && kvm_get_msr_feature(&msr_ent)) | |
3078 | return 1; | |
3079 | if (data & ~msr_ent.data) | |
3080 | return 1; | |
3081 | ||
3082 | vcpu->arch.perf_capabilities = data; | |
3083 | ||
3084 | return 0; | |
3085 | } | |
15c4a640 | 3086 | case MSR_EFER: |
11988499 | 3087 | return set_efer(vcpu, msr_info); |
8f1589d9 AP |
3088 | case MSR_K7_HWCR: |
3089 | data &= ~(u64)0x40; /* ignore flush filter disable */ | |
82494028 | 3090 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
a223c313 | 3091 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
191c8137 BP |
3092 | |
3093 | /* Handle McStatusWrEn */ | |
3094 | if (data == BIT_ULL(18)) { | |
3095 | vcpu->arch.msr_hwcr = data; | |
3096 | } else if (data != 0) { | |
a737f256 CD |
3097 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
3098 | data); | |
8f1589d9 AP |
3099 | return 1; |
3100 | } | |
15c4a640 | 3101 | break; |
f7c6d140 AP |
3102 | case MSR_FAM10H_MMIO_CONF_BASE: |
3103 | if (data != 0) { | |
a737f256 CD |
3104 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
3105 | "0x%llx\n", data); | |
f7c6d140 AP |
3106 | return 1; |
3107 | } | |
15c4a640 | 3108 | break; |
9ba075a6 | 3109 | case 0x200 ... 0x2ff: |
ff53604b | 3110 | return kvm_mtrr_set_msr(vcpu, msr, data); |
15c4a640 | 3111 | case MSR_IA32_APICBASE: |
58cb628d | 3112 | return kvm_set_apic_base(vcpu, msr_info); |
bf10bd0b | 3113 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
0105d1a5 | 3114 | return kvm_x2apic_msr_write(vcpu, msr, data); |
a3e06bbe LJ |
3115 | case MSR_IA32_TSCDEADLINE: |
3116 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
3117 | break; | |
ba904635 | 3118 | case MSR_IA32_TSC_ADJUST: |
d6321d49 | 3119 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
ba904635 | 3120 | if (!msr_info->host_initiated) { |
d913b904 | 3121 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
d7add054 | 3122 | adjust_tsc_offset_guest(vcpu, adj); |
ba904635 WA |
3123 | } |
3124 | vcpu->arch.ia32_tsc_adjust_msr = data; | |
3125 | } | |
3126 | break; | |
15c4a640 | 3127 | case MSR_IA32_MISC_ENABLE: |
511a8556 WL |
3128 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
3129 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { | |
3130 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) | |
3131 | return 1; | |
3132 | vcpu->arch.ia32_misc_enable_msr = data; | |
aedbaf4f | 3133 | kvm_update_cpuid_runtime(vcpu); |
511a8556 WL |
3134 | } else { |
3135 | vcpu->arch.ia32_misc_enable_msr = data; | |
3136 | } | |
15c4a640 | 3137 | break; |
64d60670 PB |
3138 | case MSR_IA32_SMBASE: |
3139 | if (!msr_info->host_initiated) | |
3140 | return 1; | |
3141 | vcpu->arch.smbase = data; | |
3142 | break; | |
73f624f4 PB |
3143 | case MSR_IA32_POWER_CTL: |
3144 | vcpu->arch.msr_ia32_power_ctl = data; | |
3145 | break; | |
dd259935 | 3146 | case MSR_IA32_TSC: |
0c899c25 PB |
3147 | if (msr_info->host_initiated) { |
3148 | kvm_synchronize_tsc(vcpu, data); | |
3149 | } else { | |
3150 | u64 adj = kvm_compute_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; | |
3151 | adjust_tsc_offset_guest(vcpu, adj); | |
3152 | vcpu->arch.ia32_tsc_adjust_msr += adj; | |
3153 | } | |
dd259935 | 3154 | break; |
864e2ab2 AL |
3155 | case MSR_IA32_XSS: |
3156 | if (!msr_info->host_initiated && | |
3157 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
3158 | return 1; | |
3159 | /* | |
a1bead2a SC |
3160 | * KVM supports exposing PT to the guest, but does not support |
3161 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than | |
3162 | * XSAVES/XRSTORS to save/restore PT MSRs. | |
864e2ab2 | 3163 | */ |
408e9a31 | 3164 | if (data & ~supported_xss) |
864e2ab2 AL |
3165 | return 1; |
3166 | vcpu->arch.ia32_xss = data; | |
3167 | break; | |
52797bf9 LA |
3168 | case MSR_SMI_COUNT: |
3169 | if (!msr_info->host_initiated) | |
3170 | return 1; | |
3171 | vcpu->arch.smi_count = data; | |
3172 | break; | |
11c6bffa | 3173 | case MSR_KVM_WALL_CLOCK_NEW: |
66570e96 OU |
3174 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3175 | return 1; | |
3176 | ||
629b5348 JM |
3177 | vcpu->kvm->arch.wall_clock = data; |
3178 | kvm_write_wall_clock(vcpu->kvm, data, 0); | |
66570e96 | 3179 | break; |
18068523 | 3180 | case MSR_KVM_WALL_CLOCK: |
66570e96 OU |
3181 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3182 | return 1; | |
3183 | ||
629b5348 JM |
3184 | vcpu->kvm->arch.wall_clock = data; |
3185 | kvm_write_wall_clock(vcpu->kvm, data, 0); | |
18068523 | 3186 | break; |
11c6bffa | 3187 | case MSR_KVM_SYSTEM_TIME_NEW: |
66570e96 OU |
3188 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3189 | return 1; | |
3190 | ||
5b9bb0eb OU |
3191 | kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); |
3192 | break; | |
3193 | case MSR_KVM_SYSTEM_TIME: | |
66570e96 OU |
3194 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3195 | return 1; | |
3196 | ||
3197 | kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); | |
18068523 | 3198 | break; |
344d9588 | 3199 | case MSR_KVM_ASYNC_PF_EN: |
66570e96 OU |
3200 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3201 | return 1; | |
3202 | ||
344d9588 GN |
3203 | if (kvm_pv_enable_async_pf(vcpu, data)) |
3204 | return 1; | |
3205 | break; | |
2635b5c4 | 3206 | case MSR_KVM_ASYNC_PF_INT: |
66570e96 OU |
3207 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
3208 | return 1; | |
3209 | ||
2635b5c4 VK |
3210 | if (kvm_pv_enable_async_pf_int(vcpu, data)) |
3211 | return 1; | |
3212 | break; | |
557a961a | 3213 | case MSR_KVM_ASYNC_PF_ACK: |
66570e96 OU |
3214 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3215 | return 1; | |
557a961a VK |
3216 | if (data & 0x1) { |
3217 | vcpu->arch.apf.pageready_pending = false; | |
3218 | kvm_check_async_pf_completion(vcpu); | |
3219 | } | |
3220 | break; | |
c9aaa895 | 3221 | case MSR_KVM_STEAL_TIME: |
66570e96 OU |
3222 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
3223 | return 1; | |
c9aaa895 GC |
3224 | |
3225 | if (unlikely(!sched_info_on())) | |
3226 | return 1; | |
3227 | ||
3228 | if (data & KVM_STEAL_RESERVED_MASK) | |
3229 | return 1; | |
3230 | ||
c9aaa895 GC |
3231 | vcpu->arch.st.msr_val = data; |
3232 | ||
3233 | if (!(data & KVM_MSR_ENABLED)) | |
3234 | break; | |
3235 | ||
c9aaa895 GC |
3236 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
3237 | ||
3238 | break; | |
ae7a2a3f | 3239 | case MSR_KVM_PV_EOI_EN: |
66570e96 OU |
3240 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
3241 | return 1; | |
3242 | ||
72bbf935 | 3243 | if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) |
ae7a2a3f MT |
3244 | return 1; |
3245 | break; | |
c9aaa895 | 3246 | |
2d5ba19b | 3247 | case MSR_KVM_POLL_CONTROL: |
66570e96 OU |
3248 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
3249 | return 1; | |
3250 | ||
2d5ba19b MT |
3251 | /* only enable bit supported */ |
3252 | if (data & (-1ULL << 1)) | |
3253 | return 1; | |
3254 | ||
3255 | vcpu->arch.msr_kvm_poll_control = data; | |
3256 | break; | |
3257 | ||
890ca9ae HY |
3258 | case MSR_IA32_MCG_CTL: |
3259 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 3260 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
9ffd986c | 3261 | return set_msr_mce(vcpu, msr_info); |
71db6023 | 3262 | |
6912ac32 WH |
3263 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
3264 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
df561f66 GS |
3265 | pr = true; |
3266 | fallthrough; | |
6912ac32 WH |
3267 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3268 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3269 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3270 | return kvm_pmu_set_msr(vcpu, msr_info); |
5753785f GN |
3271 | |
3272 | if (pr || data != 0) | |
a737f256 CD |
3273 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
3274 | "0x%x data 0x%llx\n", msr, data); | |
5753785f | 3275 | break; |
84e0cefa JS |
3276 | case MSR_K7_CLK_CTL: |
3277 | /* | |
3278 | * Ignore all writes to this no longer documented MSR. | |
3279 | * Writes are only relevant for old K7 processors, | |
3280 | * all pre-dating SVM, but a recommended workaround from | |
4a969980 | 3281 | * AMD for these chips. It is possible to specify the |
84e0cefa JS |
3282 | * affected processor models on the command line, hence |
3283 | * the need to ignore the workaround. | |
3284 | */ | |
3285 | break; | |
55cd8e5a | 3286 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3287 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3288 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3289 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3290 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3291 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3292 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3293 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3294 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e7d9513b AS |
3295 | return kvm_hv_set_msr_common(vcpu, msr, data, |
3296 | msr_info->host_initiated); | |
91c9c3ed | 3297 | case MSR_IA32_BBL_CR_CTL3: |
3298 | /* Drop writes to this legacy MSR -- see rdmsr | |
3299 | * counterpart for further detail. | |
3300 | */ | |
fab0aa3b EM |
3301 | if (report_ignored_msrs) |
3302 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", | |
3303 | msr, data); | |
91c9c3ed | 3304 | break; |
2b036c6b | 3305 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3306 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3307 | return 1; |
3308 | vcpu->arch.osvw.length = data; | |
3309 | break; | |
3310 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3311 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3312 | return 1; |
3313 | vcpu->arch.osvw.status = data; | |
3314 | break; | |
db2336a8 KH |
3315 | case MSR_PLATFORM_INFO: |
3316 | if (!msr_info->host_initiated || | |
db2336a8 KH |
3317 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
3318 | cpuid_fault_enabled(vcpu))) | |
3319 | return 1; | |
3320 | vcpu->arch.msr_platform_info = data; | |
3321 | break; | |
3322 | case MSR_MISC_FEATURES_ENABLES: | |
3323 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || | |
3324 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && | |
3325 | !supports_cpuid_fault(vcpu))) | |
3326 | return 1; | |
3327 | vcpu->arch.msr_misc_features_enables = data; | |
3328 | break; | |
15c4a640 | 3329 | default: |
c6702c9d | 3330 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3331 | return kvm_pmu_set_msr(vcpu, msr_info); |
6abe9c13 | 3332 | return KVM_MSR_RET_INVALID; |
15c4a640 CO |
3333 | } |
3334 | return 0; | |
3335 | } | |
3336 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
3337 | ||
44883f01 | 3338 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
15c4a640 CO |
3339 | { |
3340 | u64 data; | |
890ca9ae HY |
3341 | u64 mcg_cap = vcpu->arch.mcg_cap; |
3342 | unsigned bank_num = mcg_cap & 0xff; | |
15c4a640 CO |
3343 | |
3344 | switch (msr) { | |
15c4a640 CO |
3345 | case MSR_IA32_P5_MC_ADDR: |
3346 | case MSR_IA32_P5_MC_TYPE: | |
890ca9ae HY |
3347 | data = 0; |
3348 | break; | |
15c4a640 | 3349 | case MSR_IA32_MCG_CAP: |
890ca9ae HY |
3350 | data = vcpu->arch.mcg_cap; |
3351 | break; | |
c7ac679c | 3352 | case MSR_IA32_MCG_CTL: |
44883f01 | 3353 | if (!(mcg_cap & MCG_CTL_P) && !host) |
890ca9ae HY |
3354 | return 1; |
3355 | data = vcpu->arch.mcg_ctl; | |
3356 | break; | |
3357 | case MSR_IA32_MCG_STATUS: | |
3358 | data = vcpu->arch.mcg_status; | |
3359 | break; | |
3360 | default: | |
3361 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 3362 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
3363 | u32 offset = array_index_nospec( |
3364 | msr - MSR_IA32_MC0_CTL, | |
3365 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
3366 | ||
890ca9ae HY |
3367 | data = vcpu->arch.mce_banks[offset]; |
3368 | break; | |
3369 | } | |
3370 | return 1; | |
3371 | } | |
3372 | *pdata = data; | |
3373 | return 0; | |
3374 | } | |
3375 | ||
609e36d3 | 3376 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
890ca9ae | 3377 | { |
609e36d3 | 3378 | switch (msr_info->index) { |
890ca9ae | 3379 | case MSR_IA32_PLATFORM_ID: |
15c4a640 | 3380 | case MSR_IA32_EBL_CR_POWERON: |
b5e2fec0 AG |
3381 | case MSR_IA32_LASTBRANCHFROMIP: |
3382 | case MSR_IA32_LASTBRANCHTOIP: | |
3383 | case MSR_IA32_LASTINTFROMIP: | |
3384 | case MSR_IA32_LASTINTTOIP: | |
60af2ecd | 3385 | case MSR_K8_SYSCFG: |
3afb1121 PB |
3386 | case MSR_K8_TSEG_ADDR: |
3387 | case MSR_K8_TSEG_MASK: | |
61a6bd67 | 3388 | case MSR_VM_HSAVE_PA: |
1fdbd48c | 3389 | case MSR_K8_INT_PENDING_MSG: |
c323c0e5 | 3390 | case MSR_AMD64_NB_CFG: |
f7c6d140 | 3391 | case MSR_FAM10H_MMIO_CONF_BASE: |
2e32b719 | 3392 | case MSR_AMD64_BU_CFG2: |
0c2df2a1 | 3393 | case MSR_IA32_PERF_CTL: |
405a353a | 3394 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3395 | case MSR_F15H_EX_CFG: |
2ca1a06a VS |
3396 | /* |
3397 | * Intel Sandy Bridge CPUs must support the RAPL (running average power | |
3398 | * limit) MSRs. Just return 0, as we do not want to expose the host | |
3399 | * data here. Do not conditionalize this on CPUID, as KVM does not do | |
3400 | * so for existing CPU-specific MSRs. | |
3401 | */ | |
3402 | case MSR_RAPL_POWER_UNIT: | |
3403 | case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */ | |
3404 | case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */ | |
3405 | case MSR_PKG_ENERGY_STATUS: /* Total package */ | |
3406 | case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ | |
609e36d3 | 3407 | msr_info->data = 0; |
15c4a640 | 3408 | break; |
c51eb52b | 3409 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
c28fa560 VK |
3410 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
3411 | return kvm_pmu_get_msr(vcpu, msr_info); | |
3412 | if (!msr_info->host_initiated) | |
3413 | return 1; | |
3414 | msr_info->data = 0; | |
3415 | break; | |
6912ac32 WH |
3416 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3417 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: | |
3418 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
3419 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3420 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3421 | return kvm_pmu_get_msr(vcpu, msr_info); |
609e36d3 | 3422 | msr_info->data = 0; |
5753785f | 3423 | break; |
742bc670 | 3424 | case MSR_IA32_UCODE_REV: |
518e7b94 | 3425 | msr_info->data = vcpu->arch.microcode_version; |
742bc670 | 3426 | break; |
0cf9135b SC |
3427 | case MSR_IA32_ARCH_CAPABILITIES: |
3428 | if (!msr_info->host_initiated && | |
3429 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) | |
3430 | return 1; | |
3431 | msr_info->data = vcpu->arch.arch_capabilities; | |
3432 | break; | |
d574c539 VK |
3433 | case MSR_IA32_PERF_CAPABILITIES: |
3434 | if (!msr_info->host_initiated && | |
3435 | !guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) | |
3436 | return 1; | |
3437 | msr_info->data = vcpu->arch.perf_capabilities; | |
3438 | break; | |
73f624f4 PB |
3439 | case MSR_IA32_POWER_CTL: |
3440 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; | |
3441 | break; | |
cc5b54dd ML |
3442 | case MSR_IA32_TSC: { |
3443 | /* | |
3444 | * Intel SDM states that MSR_IA32_TSC read adds the TSC offset | |
3445 | * even when not intercepted. AMD manual doesn't explicitly | |
3446 | * state this but appears to behave the same. | |
3447 | * | |
ee6fa053 | 3448 | * On userspace reads and writes, however, we unconditionally |
c0623f5e | 3449 | * return L1's TSC value to ensure backwards-compatible |
ee6fa053 | 3450 | * behavior for migration. |
cc5b54dd ML |
3451 | */ |
3452 | u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset : | |
3453 | vcpu->arch.tsc_offset; | |
3454 | ||
3455 | msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + tsc_offset; | |
dd259935 | 3456 | break; |
cc5b54dd | 3457 | } |
9ba075a6 | 3458 | case MSR_MTRRcap: |
9ba075a6 | 3459 | case 0x200 ... 0x2ff: |
ff53604b | 3460 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
15c4a640 | 3461 | case 0xcd: /* fsb frequency */ |
609e36d3 | 3462 | msr_info->data = 3; |
15c4a640 | 3463 | break; |
7b914098 JS |
3464 | /* |
3465 | * MSR_EBC_FREQUENCY_ID | |
3466 | * Conservative value valid for even the basic CPU models. | |
3467 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of | |
3468 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, | |
3469 | * and 266MHz for model 3, or 4. Set Core Clock | |
3470 | * Frequency to System Bus Frequency Ratio to 1 (bits | |
3471 | * 31:24) even though these are only valid for CPU | |
3472 | * models > 2, however guests may end up dividing or | |
3473 | * multiplying by zero otherwise. | |
3474 | */ | |
3475 | case MSR_EBC_FREQUENCY_ID: | |
609e36d3 | 3476 | msr_info->data = 1 << 24; |
7b914098 | 3477 | break; |
15c4a640 | 3478 | case MSR_IA32_APICBASE: |
609e36d3 | 3479 | msr_info->data = kvm_get_apic_base(vcpu); |
15c4a640 | 3480 | break; |
bf10bd0b | 3481 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
609e36d3 | 3482 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
a3e06bbe | 3483 | case MSR_IA32_TSCDEADLINE: |
609e36d3 | 3484 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
a3e06bbe | 3485 | break; |
ba904635 | 3486 | case MSR_IA32_TSC_ADJUST: |
609e36d3 | 3487 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
ba904635 | 3488 | break; |
15c4a640 | 3489 | case MSR_IA32_MISC_ENABLE: |
609e36d3 | 3490 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 3491 | break; |
64d60670 PB |
3492 | case MSR_IA32_SMBASE: |
3493 | if (!msr_info->host_initiated) | |
3494 | return 1; | |
3495 | msr_info->data = vcpu->arch.smbase; | |
15c4a640 | 3496 | break; |
52797bf9 LA |
3497 | case MSR_SMI_COUNT: |
3498 | msr_info->data = vcpu->arch.smi_count; | |
3499 | break; | |
847f0ad8 AG |
3500 | case MSR_IA32_PERF_STATUS: |
3501 | /* TSC increment by tick */ | |
609e36d3 | 3502 | msr_info->data = 1000ULL; |
847f0ad8 | 3503 | /* CPU multiplier */ |
b0996ae4 | 3504 | msr_info->data |= (((uint64_t)4ULL) << 40); |
847f0ad8 | 3505 | break; |
15c4a640 | 3506 | case MSR_EFER: |
609e36d3 | 3507 | msr_info->data = vcpu->arch.efer; |
15c4a640 | 3508 | break; |
18068523 | 3509 | case MSR_KVM_WALL_CLOCK: |
1930e5dd OU |
3510 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3511 | return 1; | |
3512 | ||
3513 | msr_info->data = vcpu->kvm->arch.wall_clock; | |
3514 | break; | |
11c6bffa | 3515 | case MSR_KVM_WALL_CLOCK_NEW: |
1930e5dd OU |
3516 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3517 | return 1; | |
3518 | ||
609e36d3 | 3519 | msr_info->data = vcpu->kvm->arch.wall_clock; |
18068523 GOC |
3520 | break; |
3521 | case MSR_KVM_SYSTEM_TIME: | |
1930e5dd OU |
3522 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3523 | return 1; | |
3524 | ||
3525 | msr_info->data = vcpu->arch.time; | |
3526 | break; | |
11c6bffa | 3527 | case MSR_KVM_SYSTEM_TIME_NEW: |
1930e5dd OU |
3528 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3529 | return 1; | |
3530 | ||
609e36d3 | 3531 | msr_info->data = vcpu->arch.time; |
18068523 | 3532 | break; |
344d9588 | 3533 | case MSR_KVM_ASYNC_PF_EN: |
1930e5dd OU |
3534 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3535 | return 1; | |
3536 | ||
2635b5c4 VK |
3537 | msr_info->data = vcpu->arch.apf.msr_en_val; |
3538 | break; | |
3539 | case MSR_KVM_ASYNC_PF_INT: | |
1930e5dd OU |
3540 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
3541 | return 1; | |
3542 | ||
2635b5c4 | 3543 | msr_info->data = vcpu->arch.apf.msr_int_val; |
344d9588 | 3544 | break; |
557a961a | 3545 | case MSR_KVM_ASYNC_PF_ACK: |
1930e5dd OU |
3546 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3547 | return 1; | |
3548 | ||
557a961a VK |
3549 | msr_info->data = 0; |
3550 | break; | |
c9aaa895 | 3551 | case MSR_KVM_STEAL_TIME: |
1930e5dd OU |
3552 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
3553 | return 1; | |
3554 | ||
609e36d3 | 3555 | msr_info->data = vcpu->arch.st.msr_val; |
c9aaa895 | 3556 | break; |
1d92128f | 3557 | case MSR_KVM_PV_EOI_EN: |
1930e5dd OU |
3558 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
3559 | return 1; | |
3560 | ||
609e36d3 | 3561 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
1d92128f | 3562 | break; |
2d5ba19b | 3563 | case MSR_KVM_POLL_CONTROL: |
1930e5dd OU |
3564 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
3565 | return 1; | |
3566 | ||
2d5ba19b MT |
3567 | msr_info->data = vcpu->arch.msr_kvm_poll_control; |
3568 | break; | |
890ca9ae HY |
3569 | case MSR_IA32_P5_MC_ADDR: |
3570 | case MSR_IA32_P5_MC_TYPE: | |
3571 | case MSR_IA32_MCG_CAP: | |
3572 | case MSR_IA32_MCG_CTL: | |
3573 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 3574 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
44883f01 PB |
3575 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
3576 | msr_info->host_initiated); | |
864e2ab2 AL |
3577 | case MSR_IA32_XSS: |
3578 | if (!msr_info->host_initiated && | |
3579 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
3580 | return 1; | |
3581 | msr_info->data = vcpu->arch.ia32_xss; | |
3582 | break; | |
84e0cefa JS |
3583 | case MSR_K7_CLK_CTL: |
3584 | /* | |
3585 | * Provide expected ramp-up count for K7. All other | |
3586 | * are set to zero, indicating minimum divisors for | |
3587 | * every field. | |
3588 | * | |
3589 | * This prevents guest kernels on AMD host with CPU | |
3590 | * type 6, model 8 and higher from exploding due to | |
3591 | * the rdmsr failing. | |
3592 | */ | |
609e36d3 | 3593 | msr_info->data = 0x20000000; |
84e0cefa | 3594 | break; |
55cd8e5a | 3595 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3596 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3597 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3598 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3599 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3600 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3601 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3602 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3603 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e83d5887 | 3604 | return kvm_hv_get_msr_common(vcpu, |
44883f01 PB |
3605 | msr_info->index, &msr_info->data, |
3606 | msr_info->host_initiated); | |
91c9c3ed | 3607 | case MSR_IA32_BBL_CR_CTL3: |
3608 | /* This legacy MSR exists but isn't fully documented in current | |
3609 | * silicon. It is however accessed by winxp in very narrow | |
3610 | * scenarios where it sets bit #19, itself documented as | |
3611 | * a "reserved" bit. Best effort attempt to source coherent | |
3612 | * read data here should the balance of the register be | |
3613 | * interpreted by the guest: | |
3614 | * | |
3615 | * L2 cache control register 3: 64GB range, 256KB size, | |
3616 | * enabled, latency 0x1, configured | |
3617 | */ | |
609e36d3 | 3618 | msr_info->data = 0xbe702111; |
91c9c3ed | 3619 | break; |
2b036c6b | 3620 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3621 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3622 | return 1; |
609e36d3 | 3623 | msr_info->data = vcpu->arch.osvw.length; |
2b036c6b BO |
3624 | break; |
3625 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3626 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3627 | return 1; |
609e36d3 | 3628 | msr_info->data = vcpu->arch.osvw.status; |
2b036c6b | 3629 | break; |
db2336a8 | 3630 | case MSR_PLATFORM_INFO: |
6fbbde9a DS |
3631 | if (!msr_info->host_initiated && |
3632 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) | |
3633 | return 1; | |
db2336a8 KH |
3634 | msr_info->data = vcpu->arch.msr_platform_info; |
3635 | break; | |
3636 | case MSR_MISC_FEATURES_ENABLES: | |
3637 | msr_info->data = vcpu->arch.msr_misc_features_enables; | |
3638 | break; | |
191c8137 BP |
3639 | case MSR_K7_HWCR: |
3640 | msr_info->data = vcpu->arch.msr_hwcr; | |
3641 | break; | |
15c4a640 | 3642 | default: |
c6702c9d | 3643 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3644 | return kvm_pmu_get_msr(vcpu, msr_info); |
6abe9c13 | 3645 | return KVM_MSR_RET_INVALID; |
15c4a640 | 3646 | } |
15c4a640 CO |
3647 | return 0; |
3648 | } | |
3649 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
3650 | ||
313a3dc7 CO |
3651 | /* |
3652 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
3653 | * | |
3654 | * @return number of msrs set successfully. | |
3655 | */ | |
3656 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
3657 | struct kvm_msr_entry *entries, | |
3658 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3659 | unsigned index, u64 *data)) | |
3660 | { | |
801e459a | 3661 | int i; |
313a3dc7 | 3662 | |
313a3dc7 CO |
3663 | for (i = 0; i < msrs->nmsrs; ++i) |
3664 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
3665 | break; | |
3666 | ||
313a3dc7 CO |
3667 | return i; |
3668 | } | |
3669 | ||
3670 | /* | |
3671 | * Read or write a bunch of msrs. Parameters are user addresses. | |
3672 | * | |
3673 | * @return number of msrs set successfully. | |
3674 | */ | |
3675 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
3676 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3677 | unsigned index, u64 *data), | |
3678 | int writeback) | |
3679 | { | |
3680 | struct kvm_msrs msrs; | |
3681 | struct kvm_msr_entry *entries; | |
3682 | int r, n; | |
3683 | unsigned size; | |
3684 | ||
3685 | r = -EFAULT; | |
0e96f31e | 3686 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
313a3dc7 CO |
3687 | goto out; |
3688 | ||
3689 | r = -E2BIG; | |
3690 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
3691 | goto out; | |
3692 | ||
313a3dc7 | 3693 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
ff5c2c03 SL |
3694 | entries = memdup_user(user_msrs->entries, size); |
3695 | if (IS_ERR(entries)) { | |
3696 | r = PTR_ERR(entries); | |
313a3dc7 | 3697 | goto out; |
ff5c2c03 | 3698 | } |
313a3dc7 CO |
3699 | |
3700 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
3701 | if (r < 0) | |
3702 | goto out_free; | |
3703 | ||
3704 | r = -EFAULT; | |
3705 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
3706 | goto out_free; | |
3707 | ||
3708 | r = n; | |
3709 | ||
3710 | out_free: | |
7a73c028 | 3711 | kfree(entries); |
313a3dc7 CO |
3712 | out: |
3713 | return r; | |
3714 | } | |
3715 | ||
4d5422ce WL |
3716 | static inline bool kvm_can_mwait_in_guest(void) |
3717 | { | |
3718 | return boot_cpu_has(X86_FEATURE_MWAIT) && | |
8e9b29b6 KA |
3719 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
3720 | boot_cpu_has(X86_FEATURE_ARAT); | |
4d5422ce WL |
3721 | } |
3722 | ||
c21d54f0 VK |
3723 | static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu, |
3724 | struct kvm_cpuid2 __user *cpuid_arg) | |
3725 | { | |
3726 | struct kvm_cpuid2 cpuid; | |
3727 | int r; | |
3728 | ||
3729 | r = -EFAULT; | |
3730 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) | |
3731 | return r; | |
3732 | ||
3733 | r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
3734 | if (r) | |
3735 | return r; | |
3736 | ||
3737 | r = -EFAULT; | |
3738 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) | |
3739 | return r; | |
3740 | ||
3741 | return 0; | |
3742 | } | |
3743 | ||
784aa3d7 | 3744 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
018d00d2 | 3745 | { |
4d5422ce | 3746 | int r = 0; |
018d00d2 ZX |
3747 | |
3748 | switch (ext) { | |
3749 | case KVM_CAP_IRQCHIP: | |
3750 | case KVM_CAP_HLT: | |
3751 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
018d00d2 | 3752 | case KVM_CAP_SET_TSS_ADDR: |
07716717 | 3753 | case KVM_CAP_EXT_CPUID: |
9c15bb1d | 3754 | case KVM_CAP_EXT_EMUL_CPUID: |
c8076604 | 3755 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 3756 | case KVM_CAP_PIT: |
a28e4f5a | 3757 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 3758 | case KVM_CAP_MP_STATE: |
ed848624 | 3759 | case KVM_CAP_SYNC_MMU: |
a355c85c | 3760 | case KVM_CAP_USER_NMI: |
52d939a0 | 3761 | case KVM_CAP_REINJECT_CONTROL: |
4925663a | 3762 | case KVM_CAP_IRQ_INJECT_STATUS: |
d34e6b17 | 3763 | case KVM_CAP_IOEVENTFD: |
f848a5a8 | 3764 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
c5ff41ce | 3765 | case KVM_CAP_PIT2: |
e9f42757 | 3766 | case KVM_CAP_PIT_STATE2: |
b927a3ce | 3767 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
3cfc3092 | 3768 | case KVM_CAP_VCPU_EVENTS: |
55cd8e5a | 3769 | case KVM_CAP_HYPERV: |
10388a07 | 3770 | case KVM_CAP_HYPERV_VAPIC: |
c25bc163 | 3771 | case KVM_CAP_HYPERV_SPIN: |
5c919412 | 3772 | case KVM_CAP_HYPERV_SYNIC: |
efc479e6 | 3773 | case KVM_CAP_HYPERV_SYNIC2: |
d3457c87 | 3774 | case KVM_CAP_HYPERV_VP_INDEX: |
faeb7833 | 3775 | case KVM_CAP_HYPERV_EVENTFD: |
c1aea919 | 3776 | case KVM_CAP_HYPERV_TLBFLUSH: |
214ff83d | 3777 | case KVM_CAP_HYPERV_SEND_IPI: |
2bc39970 | 3778 | case KVM_CAP_HYPERV_CPUID: |
c21d54f0 | 3779 | case KVM_CAP_SYS_HYPERV_CPUID: |
ab9f4ecb | 3780 | case KVM_CAP_PCI_SEGMENT: |
a1efbe77 | 3781 | case KVM_CAP_DEBUGREGS: |
d2be1651 | 3782 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
2d5b5a66 | 3783 | case KVM_CAP_XSAVE: |
344d9588 | 3784 | case KVM_CAP_ASYNC_PF: |
72de5fa4 | 3785 | case KVM_CAP_ASYNC_PF_INT: |
92a1f12d | 3786 | case KVM_CAP_GET_TSC_KHZ: |
1c0b28c2 | 3787 | case KVM_CAP_KVMCLOCK_CTRL: |
4d8b81ab | 3788 | case KVM_CAP_READONLY_MEM: |
5f66b620 | 3789 | case KVM_CAP_HYPERV_TIME: |
100943c5 | 3790 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
defcf51f | 3791 | case KVM_CAP_TSC_DEADLINE_TIMER: |
90de4a18 | 3792 | case KVM_CAP_DISABLE_QUIRKS: |
d71ba788 | 3793 | case KVM_CAP_SET_BOOT_CPU_ID: |
49df6397 | 3794 | case KVM_CAP_SPLIT_IRQCHIP: |
460df4c1 | 3795 | case KVM_CAP_IMMEDIATE_EXIT: |
66bb8a06 | 3796 | case KVM_CAP_PMU_EVENT_FILTER: |
801e459a | 3797 | case KVM_CAP_GET_MSR_FEATURES: |
6fbbde9a | 3798 | case KVM_CAP_MSR_PLATFORM_INFO: |
c4f55198 | 3799 | case KVM_CAP_EXCEPTION_PAYLOAD: |
b9b2782c | 3800 | case KVM_CAP_SET_GUEST_DEBUG: |
1aa561b1 | 3801 | case KVM_CAP_LAST_CPU: |
1ae09954 | 3802 | case KVM_CAP_X86_USER_SPACE_MSR: |
1a155254 | 3803 | case KVM_CAP_X86_MSR_FILTER: |
66570e96 | 3804 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
018d00d2 ZX |
3805 | r = 1; |
3806 | break; | |
7e582ccb ML |
3807 | case KVM_CAP_SET_GUEST_DEBUG2: |
3808 | return KVM_GUESTDBG_VALID_MASK; | |
b59b153d | 3809 | #ifdef CONFIG_KVM_XEN |
23200b7a JM |
3810 | case KVM_CAP_XEN_HVM: |
3811 | r = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR | | |
8d4e7e80 DW |
3812 | KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL | |
3813 | KVM_XEN_HVM_CONFIG_SHARED_INFO; | |
30b5c851 DW |
3814 | if (sched_info_on()) |
3815 | r |= KVM_XEN_HVM_CONFIG_RUNSTATE; | |
23200b7a | 3816 | break; |
b59b153d | 3817 | #endif |
01643c51 KH |
3818 | case KVM_CAP_SYNC_REGS: |
3819 | r = KVM_SYNC_X86_VALID_FIELDS; | |
3820 | break; | |
e3fd9a93 PB |
3821 | case KVM_CAP_ADJUST_CLOCK: |
3822 | r = KVM_CLOCK_TSC_STABLE; | |
3823 | break; | |
4d5422ce | 3824 | case KVM_CAP_X86_DISABLE_EXITS: |
b5170063 WL |
3825 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
3826 | KVM_X86_DISABLE_EXITS_CSTATE; | |
4d5422ce WL |
3827 | if(kvm_can_mwait_in_guest()) |
3828 | r |= KVM_X86_DISABLE_EXITS_MWAIT; | |
668fffa3 | 3829 | break; |
6d396b55 PB |
3830 | case KVM_CAP_X86_SMM: |
3831 | /* SMBASE is usually relocated above 1M on modern chipsets, | |
3832 | * and SMM handlers might indeed rely on 4G segment limits, | |
3833 | * so do not report SMM to be available if real mode is | |
3834 | * emulated via vm86 mode. Still, do not go to great lengths | |
3835 | * to avoid userspace's usage of the feature, because it is a | |
3836 | * fringe case that is not enabled except via specific settings | |
3837 | * of the module parameters. | |
3838 | */ | |
b3646477 | 3839 | r = static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE); |
6d396b55 | 3840 | break; |
774ead3a | 3841 | case KVM_CAP_VAPIC: |
b3646477 | 3842 | r = !static_call(kvm_x86_cpu_has_accelerated_tpr)(); |
774ead3a | 3843 | break; |
f725230a | 3844 | case KVM_CAP_NR_VCPUS: |
8c3ba334 SL |
3845 | r = KVM_SOFT_MAX_VCPUS; |
3846 | break; | |
3847 | case KVM_CAP_MAX_VCPUS: | |
f725230a AK |
3848 | r = KVM_MAX_VCPUS; |
3849 | break; | |
a86cb413 TH |
3850 | case KVM_CAP_MAX_VCPU_ID: |
3851 | r = KVM_MAX_VCPU_ID; | |
3852 | break; | |
a68a6a72 MT |
3853 | case KVM_CAP_PV_MMU: /* obsolete */ |
3854 | r = 0; | |
2f333bcb | 3855 | break; |
890ca9ae HY |
3856 | case KVM_CAP_MCE: |
3857 | r = KVM_MAX_MCE_BANKS; | |
3858 | break; | |
2d5b5a66 | 3859 | case KVM_CAP_XCRS: |
d366bf7e | 3860 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
2d5b5a66 | 3861 | break; |
92a1f12d JR |
3862 | case KVM_CAP_TSC_CONTROL: |
3863 | r = kvm_has_tsc_control; | |
3864 | break; | |
37131313 RK |
3865 | case KVM_CAP_X2APIC_API: |
3866 | r = KVM_X2APIC_API_VALID_FLAGS; | |
3867 | break; | |
8fcc4b59 | 3868 | case KVM_CAP_NESTED_STATE: |
33b22172 PB |
3869 | r = kvm_x86_ops.nested_ops->get_state ? |
3870 | kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; | |
8fcc4b59 | 3871 | break; |
344c6c80 | 3872 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 3873 | r = kvm_x86_ops.enable_direct_tlbflush != NULL; |
5a0165f6 VK |
3874 | break; |
3875 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: | |
33b22172 | 3876 | r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; |
344c6c80 | 3877 | break; |
3edd6839 MG |
3878 | case KVM_CAP_SMALLER_MAXPHYADDR: |
3879 | r = (int) allow_smaller_maxphyaddr; | |
3880 | break; | |
004a0124 AJ |
3881 | case KVM_CAP_STEAL_TIME: |
3882 | r = sched_info_on(); | |
3883 | break; | |
fe6b6bc8 CQ |
3884 | case KVM_CAP_X86_BUS_LOCK_EXIT: |
3885 | if (kvm_has_bus_lock_exit) | |
3886 | r = KVM_BUS_LOCK_DETECTION_OFF | | |
3887 | KVM_BUS_LOCK_DETECTION_EXIT; | |
3888 | else | |
3889 | r = 0; | |
3890 | break; | |
018d00d2 | 3891 | default: |
018d00d2 ZX |
3892 | break; |
3893 | } | |
3894 | return r; | |
3895 | ||
3896 | } | |
3897 | ||
043405e1 CO |
3898 | long kvm_arch_dev_ioctl(struct file *filp, |
3899 | unsigned int ioctl, unsigned long arg) | |
3900 | { | |
3901 | void __user *argp = (void __user *)arg; | |
3902 | long r; | |
3903 | ||
3904 | switch (ioctl) { | |
3905 | case KVM_GET_MSR_INDEX_LIST: { | |
3906 | struct kvm_msr_list __user *user_msr_list = argp; | |
3907 | struct kvm_msr_list msr_list; | |
3908 | unsigned n; | |
3909 | ||
3910 | r = -EFAULT; | |
0e96f31e | 3911 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
043405e1 CO |
3912 | goto out; |
3913 | n = msr_list.nmsrs; | |
62ef68bb | 3914 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
0e96f31e | 3915 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
043405e1 CO |
3916 | goto out; |
3917 | r = -E2BIG; | |
e125e7b6 | 3918 | if (n < msr_list.nmsrs) |
043405e1 CO |
3919 | goto out; |
3920 | r = -EFAULT; | |
3921 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
3922 | num_msrs_to_save * sizeof(u32))) | |
3923 | goto out; | |
e125e7b6 | 3924 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
043405e1 | 3925 | &emulated_msrs, |
62ef68bb | 3926 | num_emulated_msrs * sizeof(u32))) |
043405e1 CO |
3927 | goto out; |
3928 | r = 0; | |
3929 | break; | |
3930 | } | |
9c15bb1d BP |
3931 | case KVM_GET_SUPPORTED_CPUID: |
3932 | case KVM_GET_EMULATED_CPUID: { | |
674eea0f AK |
3933 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
3934 | struct kvm_cpuid2 cpuid; | |
3935 | ||
3936 | r = -EFAULT; | |
0e96f31e | 3937 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
674eea0f | 3938 | goto out; |
9c15bb1d BP |
3939 | |
3940 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, | |
3941 | ioctl); | |
674eea0f AK |
3942 | if (r) |
3943 | goto out; | |
3944 | ||
3945 | r = -EFAULT; | |
0e96f31e | 3946 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
674eea0f AK |
3947 | goto out; |
3948 | r = 0; | |
3949 | break; | |
3950 | } | |
cf6c26ec | 3951 | case KVM_X86_GET_MCE_CAP_SUPPORTED: |
890ca9ae | 3952 | r = -EFAULT; |
c45dcc71 AR |
3953 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
3954 | sizeof(kvm_mce_cap_supported))) | |
890ca9ae HY |
3955 | goto out; |
3956 | r = 0; | |
3957 | break; | |
801e459a TL |
3958 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
3959 | struct kvm_msr_list __user *user_msr_list = argp; | |
3960 | struct kvm_msr_list msr_list; | |
3961 | unsigned int n; | |
3962 | ||
3963 | r = -EFAULT; | |
3964 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) | |
3965 | goto out; | |
3966 | n = msr_list.nmsrs; | |
3967 | msr_list.nmsrs = num_msr_based_features; | |
3968 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) | |
3969 | goto out; | |
3970 | r = -E2BIG; | |
3971 | if (n < msr_list.nmsrs) | |
3972 | goto out; | |
3973 | r = -EFAULT; | |
3974 | if (copy_to_user(user_msr_list->indices, &msr_based_features, | |
3975 | num_msr_based_features * sizeof(u32))) | |
3976 | goto out; | |
3977 | r = 0; | |
3978 | break; | |
3979 | } | |
3980 | case KVM_GET_MSRS: | |
3981 | r = msr_io(NULL, argp, do_get_msr_feature, 1); | |
3982 | break; | |
c21d54f0 VK |
3983 | case KVM_GET_SUPPORTED_HV_CPUID: |
3984 | r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp); | |
3985 | break; | |
043405e1 CO |
3986 | default: |
3987 | r = -EINVAL; | |
cf6c26ec | 3988 | break; |
043405e1 CO |
3989 | } |
3990 | out: | |
3991 | return r; | |
3992 | } | |
3993 | ||
f5f48ee1 SY |
3994 | static void wbinvd_ipi(void *garbage) |
3995 | { | |
3996 | wbinvd(); | |
3997 | } | |
3998 | ||
3999 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
4000 | { | |
e0f0bbc5 | 4001 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
f5f48ee1 SY |
4002 | } |
4003 | ||
313a3dc7 CO |
4004 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
4005 | { | |
f5f48ee1 SY |
4006 | /* Address WBINVD may be executed by guest */ |
4007 | if (need_emulate_wbinvd(vcpu)) { | |
b3646477 | 4008 | if (static_call(kvm_x86_has_wbinvd_exit)()) |
f5f48ee1 SY |
4009 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
4010 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) | |
4011 | smp_call_function_single(vcpu->cpu, | |
4012 | wbinvd_ipi, NULL, 1); | |
4013 | } | |
4014 | ||
b3646477 | 4015 | static_call(kvm_x86_vcpu_load)(vcpu, cpu); |
8f6055cb | 4016 | |
37486135 BM |
4017 | /* Save host pkru register if supported */ |
4018 | vcpu->arch.host_pkru = read_pkru(); | |
4019 | ||
0dd6a6ed ZA |
4020 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
4021 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { | |
4022 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); | |
4023 | vcpu->arch.tsc_offset_adjustment = 0; | |
105b21bb | 4024 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed | 4025 | } |
8f6055cb | 4026 | |
b0c39dc6 | 4027 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
6f526ec5 | 4028 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
4ea1636b | 4029 | rdtsc() - vcpu->arch.last_host_tsc; |
e48672fa ZA |
4030 | if (tsc_delta < 0) |
4031 | mark_tsc_unstable("KVM discovered backwards TSC"); | |
ce7a058a | 4032 | |
b0c39dc6 | 4033 | if (kvm_check_tsc_unstable()) { |
07c1419a | 4034 | u64 offset = kvm_compute_tsc_offset(vcpu, |
b183aa58 | 4035 | vcpu->arch.last_guest_tsc); |
a545ab6a | 4036 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
c285545f | 4037 | vcpu->arch.tsc_catchup = 1; |
c285545f | 4038 | } |
a749e247 PB |
4039 | |
4040 | if (kvm_lapic_hv_timer_in_use(vcpu)) | |
4041 | kvm_lapic_restart_hv_timer(vcpu); | |
4042 | ||
d98d07ca MT |
4043 | /* |
4044 | * On a host with synchronized TSC, there is no need to update | |
4045 | * kvmclock on vcpu->cpu migration | |
4046 | */ | |
4047 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) | |
0061d53d | 4048 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
c285545f | 4049 | if (vcpu->cpu != cpu) |
1bd2009e | 4050 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
e48672fa | 4051 | vcpu->cpu = cpu; |
6b7d7e76 | 4052 | } |
c9aaa895 | 4053 | |
c9aaa895 | 4054 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
313a3dc7 CO |
4055 | } |
4056 | ||
0b9f6c46 PX |
4057 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
4058 | { | |
b0431382 BO |
4059 | struct kvm_host_map map; |
4060 | struct kvm_steal_time *st; | |
15b51dc0 | 4061 | int idx; |
b0431382 | 4062 | |
0b9f6c46 PX |
4063 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
4064 | return; | |
4065 | ||
a6bd811f | 4066 | if (vcpu->arch.st.preempted) |
8c6de56a BO |
4067 | return; |
4068 | ||
15b51dc0 SC |
4069 | /* |
4070 | * Take the srcu lock as memslots will be accessed to check the gfn | |
4071 | * cache generation against the memslots generation. | |
4072 | */ | |
4073 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
4074 | ||
b0431382 BO |
4075 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, |
4076 | &vcpu->arch.st.cache, true)) | |
15b51dc0 | 4077 | goto out; |
b0431382 BO |
4078 | |
4079 | st = map.hva + | |
4080 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
0b9f6c46 | 4081 | |
a6bd811f | 4082 | st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; |
0b9f6c46 | 4083 | |
b0431382 | 4084 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); |
15b51dc0 SC |
4085 | |
4086 | out: | |
4087 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
0b9f6c46 PX |
4088 | } |
4089 | ||
313a3dc7 CO |
4090 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
4091 | { | |
f1c6366e | 4092 | if (vcpu->preempted && !vcpu->arch.guest_state_protected) |
b3646477 | 4093 | vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); |
de63ad4c | 4094 | |
30b5c851 DW |
4095 | if (kvm_xen_msr_enabled(vcpu->kvm)) |
4096 | kvm_xen_runstate_set_preempted(vcpu); | |
4097 | else | |
4098 | kvm_steal_time_set_preempted(vcpu); | |
4099 | ||
b3646477 | 4100 | static_call(kvm_x86_vcpu_put)(vcpu); |
4ea1636b | 4101 | vcpu->arch.last_host_tsc = rdtsc(); |
efdab992 | 4102 | /* |
f9dcf08e RK |
4103 | * If userspace has set any breakpoints or watchpoints, dr6 is restored |
4104 | * on every vmexit, but if not, we might have a stale dr6 from the | |
4105 | * guest. do_debug expects dr6 to be cleared after it runs, do the same. | |
efdab992 | 4106 | */ |
f9dcf08e | 4107 | set_debugreg(0, 6); |
313a3dc7 CO |
4108 | } |
4109 | ||
313a3dc7 CO |
4110 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
4111 | struct kvm_lapic_state *s) | |
4112 | { | |
fa59cc00 | 4113 | if (vcpu->arch.apicv_active) |
b3646477 | 4114 | static_call(kvm_x86_sync_pir_to_irr)(vcpu); |
d62caabb | 4115 | |
a92e2543 | 4116 | return kvm_apic_get_state(vcpu, s); |
313a3dc7 CO |
4117 | } |
4118 | ||
4119 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
4120 | struct kvm_lapic_state *s) | |
4121 | { | |
a92e2543 RK |
4122 | int r; |
4123 | ||
4124 | r = kvm_apic_set_state(vcpu, s); | |
4125 | if (r) | |
4126 | return r; | |
cb142eb7 | 4127 | update_cr8_intercept(vcpu); |
313a3dc7 CO |
4128 | |
4129 | return 0; | |
4130 | } | |
4131 | ||
127a457a MG |
4132 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
4133 | { | |
71cc849b PB |
4134 | /* |
4135 | * We can accept userspace's request for interrupt injection | |
4136 | * as long as we have a place to store the interrupt number. | |
4137 | * The actual injection will happen when the CPU is able to | |
4138 | * deliver the interrupt. | |
4139 | */ | |
4140 | if (kvm_cpu_has_extint(vcpu)) | |
4141 | return false; | |
4142 | ||
4143 | /* Acknowledging ExtINT does not happen if LINT0 is masked. */ | |
127a457a MG |
4144 | return (!lapic_in_kernel(vcpu) || |
4145 | kvm_apic_accept_pic_intr(vcpu)); | |
4146 | } | |
4147 | ||
782d422b MG |
4148 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) |
4149 | { | |
4150 | return kvm_arch_interrupt_allowed(vcpu) && | |
782d422b MG |
4151 | kvm_cpu_accept_dm_intr(vcpu); |
4152 | } | |
4153 | ||
f77bc6a4 ZX |
4154 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
4155 | struct kvm_interrupt *irq) | |
4156 | { | |
02cdb50f | 4157 | if (irq->irq >= KVM_NR_INTERRUPTS) |
f77bc6a4 | 4158 | return -EINVAL; |
1c1a9ce9 SR |
4159 | |
4160 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
4161 | kvm_queue_interrupt(vcpu, irq->irq, false); | |
4162 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
4163 | return 0; | |
4164 | } | |
4165 | ||
4166 | /* | |
4167 | * With in-kernel LAPIC, we only use this to inject EXTINT, so | |
4168 | * fail for in-kernel 8259. | |
4169 | */ | |
4170 | if (pic_in_kernel(vcpu->kvm)) | |
f77bc6a4 | 4171 | return -ENXIO; |
f77bc6a4 | 4172 | |
1c1a9ce9 SR |
4173 | if (vcpu->arch.pending_external_vector != -1) |
4174 | return -EEXIST; | |
f77bc6a4 | 4175 | |
1c1a9ce9 | 4176 | vcpu->arch.pending_external_vector = irq->irq; |
934bf653 | 4177 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f77bc6a4 ZX |
4178 | return 0; |
4179 | } | |
4180 | ||
c4abb7c9 JK |
4181 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
4182 | { | |
c4abb7c9 | 4183 | kvm_inject_nmi(vcpu); |
c4abb7c9 JK |
4184 | |
4185 | return 0; | |
4186 | } | |
4187 | ||
f077825a PB |
4188 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
4189 | { | |
64d60670 PB |
4190 | kvm_make_request(KVM_REQ_SMI, vcpu); |
4191 | ||
f077825a PB |
4192 | return 0; |
4193 | } | |
4194 | ||
b209749f AK |
4195 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
4196 | struct kvm_tpr_access_ctl *tac) | |
4197 | { | |
4198 | if (tac->flags) | |
4199 | return -EINVAL; | |
4200 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
4201 | return 0; | |
4202 | } | |
4203 | ||
890ca9ae HY |
4204 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
4205 | u64 mcg_cap) | |
4206 | { | |
4207 | int r; | |
4208 | unsigned bank_num = mcg_cap & 0xff, bank; | |
4209 | ||
4210 | r = -EINVAL; | |
c4e0e4ab | 4211 | if (!bank_num || bank_num > KVM_MAX_MCE_BANKS) |
890ca9ae | 4212 | goto out; |
c45dcc71 | 4213 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
890ca9ae HY |
4214 | goto out; |
4215 | r = 0; | |
4216 | vcpu->arch.mcg_cap = mcg_cap; | |
4217 | /* Init IA32_MCG_CTL to all 1s */ | |
4218 | if (mcg_cap & MCG_CTL_P) | |
4219 | vcpu->arch.mcg_ctl = ~(u64)0; | |
4220 | /* Init IA32_MCi_CTL to all 1s */ | |
4221 | for (bank = 0; bank < bank_num; bank++) | |
4222 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; | |
c45dcc71 | 4223 | |
b3646477 | 4224 | static_call(kvm_x86_setup_mce)(vcpu); |
890ca9ae HY |
4225 | out: |
4226 | return r; | |
4227 | } | |
4228 | ||
4229 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, | |
4230 | struct kvm_x86_mce *mce) | |
4231 | { | |
4232 | u64 mcg_cap = vcpu->arch.mcg_cap; | |
4233 | unsigned bank_num = mcg_cap & 0xff; | |
4234 | u64 *banks = vcpu->arch.mce_banks; | |
4235 | ||
4236 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) | |
4237 | return -EINVAL; | |
4238 | /* | |
4239 | * if IA32_MCG_CTL is not all 1s, the uncorrected error | |
4240 | * reporting is disabled | |
4241 | */ | |
4242 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && | |
4243 | vcpu->arch.mcg_ctl != ~(u64)0) | |
4244 | return 0; | |
4245 | banks += 4 * mce->bank; | |
4246 | /* | |
4247 | * if IA32_MCi_CTL is not all 1s, the uncorrected error | |
4248 | * reporting is disabled for the bank | |
4249 | */ | |
4250 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) | |
4251 | return 0; | |
4252 | if (mce->status & MCI_STATUS_UC) { | |
4253 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || | |
fc78f519 | 4254 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
a8eeb04a | 4255 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
890ca9ae HY |
4256 | return 0; |
4257 | } | |
4258 | if (banks[1] & MCI_STATUS_VAL) | |
4259 | mce->status |= MCI_STATUS_OVER; | |
4260 | banks[2] = mce->addr; | |
4261 | banks[3] = mce->misc; | |
4262 | vcpu->arch.mcg_status = mce->mcg_status; | |
4263 | banks[1] = mce->status; | |
4264 | kvm_queue_exception(vcpu, MC_VECTOR); | |
4265 | } else if (!(banks[1] & MCI_STATUS_VAL) | |
4266 | || !(banks[1] & MCI_STATUS_UC)) { | |
4267 | if (banks[1] & MCI_STATUS_VAL) | |
4268 | mce->status |= MCI_STATUS_OVER; | |
4269 | banks[2] = mce->addr; | |
4270 | banks[3] = mce->misc; | |
4271 | banks[1] = mce->status; | |
4272 | } else | |
4273 | banks[1] |= MCI_STATUS_OVER; | |
4274 | return 0; | |
4275 | } | |
4276 | ||
3cfc3092 JK |
4277 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
4278 | struct kvm_vcpu_events *events) | |
4279 | { | |
7460fb4a | 4280 | process_nmi(vcpu); |
59073aaf | 4281 | |
1f7becf1 JZ |
4282 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
4283 | process_smi(vcpu); | |
4284 | ||
a06230b6 OU |
4285 | /* |
4286 | * In guest mode, payload delivery should be deferred, | |
4287 | * so that the L1 hypervisor can intercept #PF before | |
4288 | * CR2 is modified (or intercept #DB before DR6 is | |
4289 | * modified under nVMX). Unless the per-VM capability, | |
4290 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of | |
4291 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we | |
4292 | * opportunistically defer the exception payload, deliver it if the | |
4293 | * capability hasn't been requested before processing a | |
4294 | * KVM_GET_VCPU_EVENTS. | |
4295 | */ | |
4296 | if (!vcpu->kvm->arch.exception_payload_enabled && | |
4297 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) | |
4298 | kvm_deliver_exception_payload(vcpu); | |
4299 | ||
664f8e26 | 4300 | /* |
59073aaf JM |
4301 | * The API doesn't provide the instruction length for software |
4302 | * exceptions, so don't report them. As long as the guest RIP | |
4303 | * isn't advanced, we should expect to encounter the exception | |
4304 | * again. | |
664f8e26 | 4305 | */ |
59073aaf JM |
4306 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
4307 | events->exception.injected = 0; | |
4308 | events->exception.pending = 0; | |
4309 | } else { | |
4310 | events->exception.injected = vcpu->arch.exception.injected; | |
4311 | events->exception.pending = vcpu->arch.exception.pending; | |
4312 | /* | |
4313 | * For ABI compatibility, deliberately conflate | |
4314 | * pending and injected exceptions when | |
4315 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. | |
4316 | */ | |
4317 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4318 | events->exception.injected |= | |
4319 | vcpu->arch.exception.pending; | |
4320 | } | |
3cfc3092 JK |
4321 | events->exception.nr = vcpu->arch.exception.nr; |
4322 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; | |
4323 | events->exception.error_code = vcpu->arch.exception.error_code; | |
59073aaf JM |
4324 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
4325 | events->exception_payload = vcpu->arch.exception.payload; | |
3cfc3092 | 4326 | |
03b82a30 | 4327 | events->interrupt.injected = |
04140b41 | 4328 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
3cfc3092 | 4329 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
03b82a30 | 4330 | events->interrupt.soft = 0; |
b3646477 | 4331 | events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); |
3cfc3092 JK |
4332 | |
4333 | events->nmi.injected = vcpu->arch.nmi_injected; | |
7460fb4a | 4334 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
b3646477 | 4335 | events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu); |
97e69aa6 | 4336 | events->nmi.pad = 0; |
3cfc3092 | 4337 | |
66450a21 | 4338 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
3cfc3092 | 4339 | |
f077825a PB |
4340 | events->smi.smm = is_smm(vcpu); |
4341 | events->smi.pending = vcpu->arch.smi_pending; | |
4342 | events->smi.smm_inside_nmi = | |
4343 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); | |
4344 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); | |
4345 | ||
dab4b911 | 4346 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
f077825a PB |
4347 | | KVM_VCPUEVENT_VALID_SHADOW |
4348 | | KVM_VCPUEVENT_VALID_SMM); | |
59073aaf JM |
4349 | if (vcpu->kvm->arch.exception_payload_enabled) |
4350 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; | |
4351 | ||
97e69aa6 | 4352 | memset(&events->reserved, 0, sizeof(events->reserved)); |
3cfc3092 JK |
4353 | } |
4354 | ||
c5833c7a | 4355 | static void kvm_smm_changed(struct kvm_vcpu *vcpu); |
6ef4e07e | 4356 | |
3cfc3092 JK |
4357 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
4358 | struct kvm_vcpu_events *events) | |
4359 | { | |
dab4b911 | 4360 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
48005f64 | 4361 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
f077825a | 4362 | | KVM_VCPUEVENT_VALID_SHADOW |
59073aaf JM |
4363 | | KVM_VCPUEVENT_VALID_SMM |
4364 | | KVM_VCPUEVENT_VALID_PAYLOAD)) | |
3cfc3092 JK |
4365 | return -EINVAL; |
4366 | ||
59073aaf JM |
4367 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
4368 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4369 | return -EINVAL; | |
4370 | if (events->exception.pending) | |
4371 | events->exception.injected = 0; | |
4372 | else | |
4373 | events->exception_has_payload = 0; | |
4374 | } else { | |
4375 | events->exception.pending = 0; | |
4376 | events->exception_has_payload = 0; | |
4377 | } | |
4378 | ||
4379 | if ((events->exception.injected || events->exception.pending) && | |
4380 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) | |
78e546c8 PB |
4381 | return -EINVAL; |
4382 | ||
28bf2888 DH |
4383 | /* INITs are latched while in SMM */ |
4384 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && | |
4385 | (events->smi.smm || events->smi.pending) && | |
4386 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) | |
4387 | return -EINVAL; | |
4388 | ||
7460fb4a | 4389 | process_nmi(vcpu); |
59073aaf JM |
4390 | vcpu->arch.exception.injected = events->exception.injected; |
4391 | vcpu->arch.exception.pending = events->exception.pending; | |
3cfc3092 JK |
4392 | vcpu->arch.exception.nr = events->exception.nr; |
4393 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; | |
4394 | vcpu->arch.exception.error_code = events->exception.error_code; | |
59073aaf JM |
4395 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
4396 | vcpu->arch.exception.payload = events->exception_payload; | |
3cfc3092 | 4397 | |
04140b41 | 4398 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
3cfc3092 JK |
4399 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
4400 | vcpu->arch.interrupt.soft = events->interrupt.soft; | |
48005f64 | 4401 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
b3646477 JB |
4402 | static_call(kvm_x86_set_interrupt_shadow)(vcpu, |
4403 | events->interrupt.shadow); | |
3cfc3092 JK |
4404 | |
4405 | vcpu->arch.nmi_injected = events->nmi.injected; | |
dab4b911 JK |
4406 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
4407 | vcpu->arch.nmi_pending = events->nmi.pending; | |
b3646477 | 4408 | static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked); |
3cfc3092 | 4409 | |
66450a21 | 4410 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
bce87cce | 4411 | lapic_in_kernel(vcpu)) |
66450a21 | 4412 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
3cfc3092 | 4413 | |
f077825a | 4414 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
c5833c7a SC |
4415 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
4416 | if (events->smi.smm) | |
4417 | vcpu->arch.hflags |= HF_SMM_MASK; | |
4418 | else | |
4419 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
4420 | kvm_smm_changed(vcpu); | |
4421 | } | |
6ef4e07e | 4422 | |
f077825a | 4423 | vcpu->arch.smi_pending = events->smi.pending; |
f4ef1910 WL |
4424 | |
4425 | if (events->smi.smm) { | |
4426 | if (events->smi.smm_inside_nmi) | |
4427 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; | |
f077825a | 4428 | else |
f4ef1910 | 4429 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
ff90afa7 LA |
4430 | } |
4431 | ||
4432 | if (lapic_in_kernel(vcpu)) { | |
4433 | if (events->smi.latched_init) | |
4434 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
4435 | else | |
4436 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
f077825a PB |
4437 | } |
4438 | } | |
4439 | ||
3842d135 AK |
4440 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
4441 | ||
3cfc3092 JK |
4442 | return 0; |
4443 | } | |
4444 | ||
a1efbe77 JK |
4445 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
4446 | struct kvm_debugregs *dbgregs) | |
4447 | { | |
73aaf249 JK |
4448 | unsigned long val; |
4449 | ||
a1efbe77 | 4450 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
16f8a6f9 | 4451 | kvm_get_dr(vcpu, 6, &val); |
73aaf249 | 4452 | dbgregs->dr6 = val; |
a1efbe77 JK |
4453 | dbgregs->dr7 = vcpu->arch.dr7; |
4454 | dbgregs->flags = 0; | |
97e69aa6 | 4455 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
a1efbe77 JK |
4456 | } |
4457 | ||
4458 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, | |
4459 | struct kvm_debugregs *dbgregs) | |
4460 | { | |
4461 | if (dbgregs->flags) | |
4462 | return -EINVAL; | |
4463 | ||
fd238002 | 4464 | if (!kvm_dr6_valid(dbgregs->dr6)) |
d14bdb55 | 4465 | return -EINVAL; |
fd238002 | 4466 | if (!kvm_dr7_valid(dbgregs->dr7)) |
d14bdb55 PB |
4467 | return -EINVAL; |
4468 | ||
a1efbe77 | 4469 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
ae561ede | 4470 | kvm_update_dr0123(vcpu); |
a1efbe77 JK |
4471 | vcpu->arch.dr6 = dbgregs->dr6; |
4472 | vcpu->arch.dr7 = dbgregs->dr7; | |
9926c9fd | 4473 | kvm_update_dr7(vcpu); |
a1efbe77 | 4474 | |
a1efbe77 JK |
4475 | return 0; |
4476 | } | |
4477 | ||
df1daba7 PB |
4478 | #define XSTATE_COMPACTION_ENABLED (1ULL << 63) |
4479 | ||
4480 | static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) | |
4481 | { | |
b666a4b6 | 4482 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
400e4b20 | 4483 | u64 xstate_bv = xsave->header.xfeatures; |
df1daba7 PB |
4484 | u64 valid; |
4485 | ||
4486 | /* | |
4487 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4488 | * leaves 0 and 1 in the loop below. | |
4489 | */ | |
4490 | memcpy(dest, xsave, XSAVE_HDR_OFFSET); | |
4491 | ||
4492 | /* Set XSTATE_BV */ | |
00c87e9a | 4493 | xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE; |
df1daba7 PB |
4494 | *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv; |
4495 | ||
4496 | /* | |
4497 | * Copy each region from the possibly compacted offset to the | |
4498 | * non-compacted offset. | |
4499 | */ | |
d91cab78 | 4500 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4501 | while (valid) { |
abd16d68 SAS |
4502 | u64 xfeature_mask = valid & -valid; |
4503 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4504 | void *src = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4505 | |
4506 | if (src) { | |
4507 | u32 size, offset, ecx, edx; | |
abd16d68 | 4508 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4509 | &size, &offset, &ecx, &edx); |
abd16d68 | 4510 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4511 | memcpy(dest + offset, &vcpu->arch.pkru, |
4512 | sizeof(vcpu->arch.pkru)); | |
4513 | else | |
4514 | memcpy(dest + offset, src, size); | |
4515 | ||
df1daba7 PB |
4516 | } |
4517 | ||
abd16d68 | 4518 | valid -= xfeature_mask; |
df1daba7 PB |
4519 | } |
4520 | } | |
4521 | ||
4522 | static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) | |
4523 | { | |
b666a4b6 | 4524 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
df1daba7 PB |
4525 | u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); |
4526 | u64 valid; | |
4527 | ||
4528 | /* | |
4529 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4530 | * leaves 0 and 1 in the loop below. | |
4531 | */ | |
4532 | memcpy(xsave, src, XSAVE_HDR_OFFSET); | |
4533 | ||
4534 | /* Set XSTATE_BV and possibly XCOMP_BV. */ | |
400e4b20 | 4535 | xsave->header.xfeatures = xstate_bv; |
782511b0 | 4536 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
3a54450b | 4537 | xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; |
df1daba7 PB |
4538 | |
4539 | /* | |
4540 | * Copy each region from the non-compacted offset to the | |
4541 | * possibly compacted offset. | |
4542 | */ | |
d91cab78 | 4543 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4544 | while (valid) { |
abd16d68 SAS |
4545 | u64 xfeature_mask = valid & -valid; |
4546 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4547 | void *dest = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4548 | |
4549 | if (dest) { | |
4550 | u32 size, offset, ecx, edx; | |
abd16d68 | 4551 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4552 | &size, &offset, &ecx, &edx); |
abd16d68 | 4553 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4554 | memcpy(&vcpu->arch.pkru, src + offset, |
4555 | sizeof(vcpu->arch.pkru)); | |
4556 | else | |
4557 | memcpy(dest, src + offset, size); | |
ee4100da | 4558 | } |
df1daba7 | 4559 | |
abd16d68 | 4560 | valid -= xfeature_mask; |
df1daba7 PB |
4561 | } |
4562 | } | |
4563 | ||
2d5b5a66 SY |
4564 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
4565 | struct kvm_xsave *guest_xsave) | |
4566 | { | |
ed02b213 TL |
4567 | if (!vcpu->arch.guest_fpu) |
4568 | return; | |
4569 | ||
d366bf7e | 4570 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
df1daba7 PB |
4571 | memset(guest_xsave, 0, sizeof(struct kvm_xsave)); |
4572 | fill_xsave((u8 *) guest_xsave->region, vcpu); | |
4344ee98 | 4573 | } else { |
2d5b5a66 | 4574 | memcpy(guest_xsave->region, |
b666a4b6 | 4575 | &vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4576 | sizeof(struct fxregs_state)); |
2d5b5a66 | 4577 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
d91cab78 | 4578 | XFEATURE_MASK_FPSSE; |
2d5b5a66 SY |
4579 | } |
4580 | } | |
4581 | ||
a575813b WL |
4582 | #define XSAVE_MXCSR_OFFSET 24 |
4583 | ||
2d5b5a66 SY |
4584 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
4585 | struct kvm_xsave *guest_xsave) | |
4586 | { | |
ed02b213 TL |
4587 | u64 xstate_bv; |
4588 | u32 mxcsr; | |
4589 | ||
4590 | if (!vcpu->arch.guest_fpu) | |
4591 | return 0; | |
4592 | ||
4593 | xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; | |
4594 | mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)]; | |
2d5b5a66 | 4595 | |
d366bf7e | 4596 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
d7876f1b PB |
4597 | /* |
4598 | * Here we allow setting states that are not present in | |
4599 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility | |
4600 | * with old userspace. | |
4601 | */ | |
cfc48181 | 4602 | if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask) |
d7876f1b | 4603 | return -EINVAL; |
df1daba7 | 4604 | load_xsave(vcpu, (u8 *)guest_xsave->region); |
d7876f1b | 4605 | } else { |
a575813b WL |
4606 | if (xstate_bv & ~XFEATURE_MASK_FPSSE || |
4607 | mxcsr & ~mxcsr_feature_mask) | |
2d5b5a66 | 4608 | return -EINVAL; |
b666a4b6 | 4609 | memcpy(&vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4610 | guest_xsave->region, sizeof(struct fxregs_state)); |
2d5b5a66 SY |
4611 | } |
4612 | return 0; | |
4613 | } | |
4614 | ||
4615 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, | |
4616 | struct kvm_xcrs *guest_xcrs) | |
4617 | { | |
d366bf7e | 4618 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
2d5b5a66 SY |
4619 | guest_xcrs->nr_xcrs = 0; |
4620 | return; | |
4621 | } | |
4622 | ||
4623 | guest_xcrs->nr_xcrs = 1; | |
4624 | guest_xcrs->flags = 0; | |
4625 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; | |
4626 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; | |
4627 | } | |
4628 | ||
4629 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, | |
4630 | struct kvm_xcrs *guest_xcrs) | |
4631 | { | |
4632 | int i, r = 0; | |
4633 | ||
d366bf7e | 4634 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
2d5b5a66 SY |
4635 | return -EINVAL; |
4636 | ||
4637 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) | |
4638 | return -EINVAL; | |
4639 | ||
4640 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) | |
4641 | /* Only support XCR0 currently */ | |
c67a04cb | 4642 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
2d5b5a66 | 4643 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
c67a04cb | 4644 | guest_xcrs->xcrs[i].value); |
2d5b5a66 SY |
4645 | break; |
4646 | } | |
4647 | if (r) | |
4648 | r = -EINVAL; | |
4649 | return r; | |
4650 | } | |
4651 | ||
1c0b28c2 EM |
4652 | /* |
4653 | * kvm_set_guest_paused() indicates to the guest kernel that it has been | |
4654 | * stopped by the hypervisor. This function will be called from the host only. | |
4655 | * EINVAL is returned when the host attempts to set the flag for a guest that | |
4656 | * does not support pv clocks. | |
4657 | */ | |
4658 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) | |
4659 | { | |
0b79459b | 4660 | if (!vcpu->arch.pv_time_enabled) |
1c0b28c2 | 4661 | return -EINVAL; |
51d59c6b | 4662 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
1c0b28c2 EM |
4663 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
4664 | return 0; | |
4665 | } | |
4666 | ||
5c919412 AS |
4667 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
4668 | struct kvm_enable_cap *cap) | |
4669 | { | |
57b119da VK |
4670 | int r; |
4671 | uint16_t vmcs_version; | |
4672 | void __user *user_ptr; | |
4673 | ||
5c919412 AS |
4674 | if (cap->flags) |
4675 | return -EINVAL; | |
4676 | ||
4677 | switch (cap->cap) { | |
efc479e6 RK |
4678 | case KVM_CAP_HYPERV_SYNIC2: |
4679 | if (cap->args[0]) | |
4680 | return -EINVAL; | |
df561f66 | 4681 | fallthrough; |
b2869f28 | 4682 | |
5c919412 | 4683 | case KVM_CAP_HYPERV_SYNIC: |
546d87e5 WL |
4684 | if (!irqchip_in_kernel(vcpu->kvm)) |
4685 | return -EINVAL; | |
efc479e6 RK |
4686 | return kvm_hv_activate_synic(vcpu, cap->cap == |
4687 | KVM_CAP_HYPERV_SYNIC2); | |
57b119da | 4688 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
33b22172 | 4689 | if (!kvm_x86_ops.nested_ops->enable_evmcs) |
5158917c | 4690 | return -ENOTTY; |
33b22172 | 4691 | r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); |
57b119da VK |
4692 | if (!r) { |
4693 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; | |
4694 | if (copy_to_user(user_ptr, &vmcs_version, | |
4695 | sizeof(vmcs_version))) | |
4696 | r = -EFAULT; | |
4697 | } | |
4698 | return r; | |
344c6c80 | 4699 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 4700 | if (!kvm_x86_ops.enable_direct_tlbflush) |
344c6c80 TL |
4701 | return -ENOTTY; |
4702 | ||
b3646477 | 4703 | return static_call(kvm_x86_enable_direct_tlbflush)(vcpu); |
57b119da | 4704 | |
66570e96 OU |
4705 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
4706 | vcpu->arch.pv_cpuid.enforce = cap->args[0]; | |
01b4f510 OU |
4707 | if (vcpu->arch.pv_cpuid.enforce) |
4708 | kvm_update_pv_runtime(vcpu); | |
66570e96 OU |
4709 | |
4710 | return 0; | |
4711 | ||
5c919412 AS |
4712 | default: |
4713 | return -EINVAL; | |
4714 | } | |
4715 | } | |
4716 | ||
313a3dc7 CO |
4717 | long kvm_arch_vcpu_ioctl(struct file *filp, |
4718 | unsigned int ioctl, unsigned long arg) | |
4719 | { | |
4720 | struct kvm_vcpu *vcpu = filp->private_data; | |
4721 | void __user *argp = (void __user *)arg; | |
4722 | int r; | |
d1ac91d8 AK |
4723 | union { |
4724 | struct kvm_lapic_state *lapic; | |
4725 | struct kvm_xsave *xsave; | |
4726 | struct kvm_xcrs *xcrs; | |
4727 | void *buffer; | |
4728 | } u; | |
4729 | ||
9b062471 CD |
4730 | vcpu_load(vcpu); |
4731 | ||
d1ac91d8 | 4732 | u.buffer = NULL; |
313a3dc7 CO |
4733 | switch (ioctl) { |
4734 | case KVM_GET_LAPIC: { | |
2204ae3c | 4735 | r = -EINVAL; |
bce87cce | 4736 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4737 | goto out; |
254272ce BG |
4738 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
4739 | GFP_KERNEL_ACCOUNT); | |
313a3dc7 | 4740 | |
b772ff36 | 4741 | r = -ENOMEM; |
d1ac91d8 | 4742 | if (!u.lapic) |
b772ff36 | 4743 | goto out; |
d1ac91d8 | 4744 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4745 | if (r) |
4746 | goto out; | |
4747 | r = -EFAULT; | |
d1ac91d8 | 4748 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
313a3dc7 CO |
4749 | goto out; |
4750 | r = 0; | |
4751 | break; | |
4752 | } | |
4753 | case KVM_SET_LAPIC: { | |
2204ae3c | 4754 | r = -EINVAL; |
bce87cce | 4755 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4756 | goto out; |
ff5c2c03 | 4757 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
9b062471 CD |
4758 | if (IS_ERR(u.lapic)) { |
4759 | r = PTR_ERR(u.lapic); | |
4760 | goto out_nofree; | |
4761 | } | |
ff5c2c03 | 4762 | |
d1ac91d8 | 4763 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4764 | break; |
4765 | } | |
f77bc6a4 ZX |
4766 | case KVM_INTERRUPT: { |
4767 | struct kvm_interrupt irq; | |
4768 | ||
4769 | r = -EFAULT; | |
0e96f31e | 4770 | if (copy_from_user(&irq, argp, sizeof(irq))) |
f77bc6a4 ZX |
4771 | goto out; |
4772 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
f77bc6a4 ZX |
4773 | break; |
4774 | } | |
c4abb7c9 JK |
4775 | case KVM_NMI: { |
4776 | r = kvm_vcpu_ioctl_nmi(vcpu); | |
c4abb7c9 JK |
4777 | break; |
4778 | } | |
f077825a PB |
4779 | case KVM_SMI: { |
4780 | r = kvm_vcpu_ioctl_smi(vcpu); | |
4781 | break; | |
4782 | } | |
313a3dc7 CO |
4783 | case KVM_SET_CPUID: { |
4784 | struct kvm_cpuid __user *cpuid_arg = argp; | |
4785 | struct kvm_cpuid cpuid; | |
4786 | ||
4787 | r = -EFAULT; | |
0e96f31e | 4788 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
313a3dc7 CO |
4789 | goto out; |
4790 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
313a3dc7 CO |
4791 | break; |
4792 | } | |
07716717 DK |
4793 | case KVM_SET_CPUID2: { |
4794 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4795 | struct kvm_cpuid2 cpuid; | |
4796 | ||
4797 | r = -EFAULT; | |
0e96f31e | 4798 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4799 | goto out; |
4800 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
19355475 | 4801 | cpuid_arg->entries); |
07716717 DK |
4802 | break; |
4803 | } | |
4804 | case KVM_GET_CPUID2: { | |
4805 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4806 | struct kvm_cpuid2 cpuid; | |
4807 | ||
4808 | r = -EFAULT; | |
0e96f31e | 4809 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4810 | goto out; |
4811 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
19355475 | 4812 | cpuid_arg->entries); |
07716717 DK |
4813 | if (r) |
4814 | goto out; | |
4815 | r = -EFAULT; | |
0e96f31e | 4816 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
07716717 DK |
4817 | goto out; |
4818 | r = 0; | |
4819 | break; | |
4820 | } | |
801e459a TL |
4821 | case KVM_GET_MSRS: { |
4822 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
609e36d3 | 4823 | r = msr_io(vcpu, argp, do_get_msr, 1); |
801e459a | 4824 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4825 | break; |
801e459a TL |
4826 | } |
4827 | case KVM_SET_MSRS: { | |
4828 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
313a3dc7 | 4829 | r = msr_io(vcpu, argp, do_set_msr, 0); |
801e459a | 4830 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4831 | break; |
801e459a | 4832 | } |
b209749f AK |
4833 | case KVM_TPR_ACCESS_REPORTING: { |
4834 | struct kvm_tpr_access_ctl tac; | |
4835 | ||
4836 | r = -EFAULT; | |
0e96f31e | 4837 | if (copy_from_user(&tac, argp, sizeof(tac))) |
b209749f AK |
4838 | goto out; |
4839 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
4840 | if (r) | |
4841 | goto out; | |
4842 | r = -EFAULT; | |
0e96f31e | 4843 | if (copy_to_user(argp, &tac, sizeof(tac))) |
b209749f AK |
4844 | goto out; |
4845 | r = 0; | |
4846 | break; | |
4847 | }; | |
b93463aa AK |
4848 | case KVM_SET_VAPIC_ADDR: { |
4849 | struct kvm_vapic_addr va; | |
7301d6ab | 4850 | int idx; |
b93463aa AK |
4851 | |
4852 | r = -EINVAL; | |
35754c98 | 4853 | if (!lapic_in_kernel(vcpu)) |
b93463aa AK |
4854 | goto out; |
4855 | r = -EFAULT; | |
0e96f31e | 4856 | if (copy_from_user(&va, argp, sizeof(va))) |
b93463aa | 4857 | goto out; |
7301d6ab | 4858 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
fda4e2e8 | 4859 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
7301d6ab | 4860 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b93463aa AK |
4861 | break; |
4862 | } | |
890ca9ae HY |
4863 | case KVM_X86_SETUP_MCE: { |
4864 | u64 mcg_cap; | |
4865 | ||
4866 | r = -EFAULT; | |
0e96f31e | 4867 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
890ca9ae HY |
4868 | goto out; |
4869 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); | |
4870 | break; | |
4871 | } | |
4872 | case KVM_X86_SET_MCE: { | |
4873 | struct kvm_x86_mce mce; | |
4874 | ||
4875 | r = -EFAULT; | |
0e96f31e | 4876 | if (copy_from_user(&mce, argp, sizeof(mce))) |
890ca9ae HY |
4877 | goto out; |
4878 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); | |
4879 | break; | |
4880 | } | |
3cfc3092 JK |
4881 | case KVM_GET_VCPU_EVENTS: { |
4882 | struct kvm_vcpu_events events; | |
4883 | ||
4884 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); | |
4885 | ||
4886 | r = -EFAULT; | |
4887 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) | |
4888 | break; | |
4889 | r = 0; | |
4890 | break; | |
4891 | } | |
4892 | case KVM_SET_VCPU_EVENTS: { | |
4893 | struct kvm_vcpu_events events; | |
4894 | ||
4895 | r = -EFAULT; | |
4896 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) | |
4897 | break; | |
4898 | ||
4899 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); | |
4900 | break; | |
4901 | } | |
a1efbe77 JK |
4902 | case KVM_GET_DEBUGREGS: { |
4903 | struct kvm_debugregs dbgregs; | |
4904 | ||
4905 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); | |
4906 | ||
4907 | r = -EFAULT; | |
4908 | if (copy_to_user(argp, &dbgregs, | |
4909 | sizeof(struct kvm_debugregs))) | |
4910 | break; | |
4911 | r = 0; | |
4912 | break; | |
4913 | } | |
4914 | case KVM_SET_DEBUGREGS: { | |
4915 | struct kvm_debugregs dbgregs; | |
4916 | ||
4917 | r = -EFAULT; | |
4918 | if (copy_from_user(&dbgregs, argp, | |
4919 | sizeof(struct kvm_debugregs))) | |
4920 | break; | |
4921 | ||
4922 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); | |
4923 | break; | |
4924 | } | |
2d5b5a66 | 4925 | case KVM_GET_XSAVE: { |
254272ce | 4926 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4927 | r = -ENOMEM; |
d1ac91d8 | 4928 | if (!u.xsave) |
2d5b5a66 SY |
4929 | break; |
4930 | ||
d1ac91d8 | 4931 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4932 | |
4933 | r = -EFAULT; | |
d1ac91d8 | 4934 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
2d5b5a66 SY |
4935 | break; |
4936 | r = 0; | |
4937 | break; | |
4938 | } | |
4939 | case KVM_SET_XSAVE: { | |
ff5c2c03 | 4940 | u.xsave = memdup_user(argp, sizeof(*u.xsave)); |
9b062471 CD |
4941 | if (IS_ERR(u.xsave)) { |
4942 | r = PTR_ERR(u.xsave); | |
4943 | goto out_nofree; | |
4944 | } | |
2d5b5a66 | 4945 | |
d1ac91d8 | 4946 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4947 | break; |
4948 | } | |
4949 | case KVM_GET_XCRS: { | |
254272ce | 4950 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4951 | r = -ENOMEM; |
d1ac91d8 | 4952 | if (!u.xcrs) |
2d5b5a66 SY |
4953 | break; |
4954 | ||
d1ac91d8 | 4955 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4956 | |
4957 | r = -EFAULT; | |
d1ac91d8 | 4958 | if (copy_to_user(argp, u.xcrs, |
2d5b5a66 SY |
4959 | sizeof(struct kvm_xcrs))) |
4960 | break; | |
4961 | r = 0; | |
4962 | break; | |
4963 | } | |
4964 | case KVM_SET_XCRS: { | |
ff5c2c03 | 4965 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
9b062471 CD |
4966 | if (IS_ERR(u.xcrs)) { |
4967 | r = PTR_ERR(u.xcrs); | |
4968 | goto out_nofree; | |
4969 | } | |
2d5b5a66 | 4970 | |
d1ac91d8 | 4971 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4972 | break; |
4973 | } | |
92a1f12d JR |
4974 | case KVM_SET_TSC_KHZ: { |
4975 | u32 user_tsc_khz; | |
4976 | ||
4977 | r = -EINVAL; | |
92a1f12d JR |
4978 | user_tsc_khz = (u32)arg; |
4979 | ||
26769f96 MT |
4980 | if (kvm_has_tsc_control && |
4981 | user_tsc_khz >= kvm_max_guest_tsc_khz) | |
92a1f12d JR |
4982 | goto out; |
4983 | ||
cc578287 ZA |
4984 | if (user_tsc_khz == 0) |
4985 | user_tsc_khz = tsc_khz; | |
4986 | ||
381d585c HZ |
4987 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
4988 | r = 0; | |
92a1f12d | 4989 | |
92a1f12d JR |
4990 | goto out; |
4991 | } | |
4992 | case KVM_GET_TSC_KHZ: { | |
cc578287 | 4993 | r = vcpu->arch.virtual_tsc_khz; |
92a1f12d JR |
4994 | goto out; |
4995 | } | |
1c0b28c2 EM |
4996 | case KVM_KVMCLOCK_CTRL: { |
4997 | r = kvm_set_guest_paused(vcpu); | |
4998 | goto out; | |
4999 | } | |
5c919412 AS |
5000 | case KVM_ENABLE_CAP: { |
5001 | struct kvm_enable_cap cap; | |
5002 | ||
5003 | r = -EFAULT; | |
5004 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
5005 | goto out; | |
5006 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
5007 | break; | |
5008 | } | |
8fcc4b59 JM |
5009 | case KVM_GET_NESTED_STATE: { |
5010 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
5011 | u32 user_data_size; | |
5012 | ||
5013 | r = -EINVAL; | |
33b22172 | 5014 | if (!kvm_x86_ops.nested_ops->get_state) |
8fcc4b59 JM |
5015 | break; |
5016 | ||
5017 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); | |
26b471c7 | 5018 | r = -EFAULT; |
8fcc4b59 | 5019 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
26b471c7 | 5020 | break; |
8fcc4b59 | 5021 | |
33b22172 PB |
5022 | r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, |
5023 | user_data_size); | |
8fcc4b59 | 5024 | if (r < 0) |
26b471c7 | 5025 | break; |
8fcc4b59 JM |
5026 | |
5027 | if (r > user_data_size) { | |
5028 | if (put_user(r, &user_kvm_nested_state->size)) | |
26b471c7 LA |
5029 | r = -EFAULT; |
5030 | else | |
5031 | r = -E2BIG; | |
5032 | break; | |
8fcc4b59 | 5033 | } |
26b471c7 | 5034 | |
8fcc4b59 JM |
5035 | r = 0; |
5036 | break; | |
5037 | } | |
5038 | case KVM_SET_NESTED_STATE: { | |
5039 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
5040 | struct kvm_nested_state kvm_state; | |
ad5996d9 | 5041 | int idx; |
8fcc4b59 JM |
5042 | |
5043 | r = -EINVAL; | |
33b22172 | 5044 | if (!kvm_x86_ops.nested_ops->set_state) |
8fcc4b59 JM |
5045 | break; |
5046 | ||
26b471c7 | 5047 | r = -EFAULT; |
8fcc4b59 | 5048 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
26b471c7 | 5049 | break; |
8fcc4b59 | 5050 | |
26b471c7 | 5051 | r = -EINVAL; |
8fcc4b59 | 5052 | if (kvm_state.size < sizeof(kvm_state)) |
26b471c7 | 5053 | break; |
8fcc4b59 JM |
5054 | |
5055 | if (kvm_state.flags & | |
8cab6507 | 5056 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
cc440cda PB |
5057 | | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING |
5058 | | KVM_STATE_NESTED_GIF_SET)) | |
26b471c7 | 5059 | break; |
8fcc4b59 JM |
5060 | |
5061 | /* nested_run_pending implies guest_mode. */ | |
8cab6507 VK |
5062 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
5063 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) | |
26b471c7 | 5064 | break; |
8fcc4b59 | 5065 | |
ad5996d9 | 5066 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
33b22172 | 5067 | r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); |
ad5996d9 | 5068 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8fcc4b59 JM |
5069 | break; |
5070 | } | |
c21d54f0 VK |
5071 | case KVM_GET_SUPPORTED_HV_CPUID: |
5072 | r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp); | |
2bc39970 | 5073 | break; |
b59b153d | 5074 | #ifdef CONFIG_KVM_XEN |
3e324615 DW |
5075 | case KVM_XEN_VCPU_GET_ATTR: { |
5076 | struct kvm_xen_vcpu_attr xva; | |
5077 | ||
5078 | r = -EFAULT; | |
5079 | if (copy_from_user(&xva, argp, sizeof(xva))) | |
5080 | goto out; | |
5081 | r = kvm_xen_vcpu_get_attr(vcpu, &xva); | |
5082 | if (!r && copy_to_user(argp, &xva, sizeof(xva))) | |
5083 | r = -EFAULT; | |
5084 | break; | |
5085 | } | |
5086 | case KVM_XEN_VCPU_SET_ATTR: { | |
5087 | struct kvm_xen_vcpu_attr xva; | |
5088 | ||
5089 | r = -EFAULT; | |
5090 | if (copy_from_user(&xva, argp, sizeof(xva))) | |
5091 | goto out; | |
5092 | r = kvm_xen_vcpu_set_attr(vcpu, &xva); | |
5093 | break; | |
5094 | } | |
b59b153d | 5095 | #endif |
313a3dc7 CO |
5096 | default: |
5097 | r = -EINVAL; | |
5098 | } | |
5099 | out: | |
d1ac91d8 | 5100 | kfree(u.buffer); |
9b062471 CD |
5101 | out_nofree: |
5102 | vcpu_put(vcpu); | |
313a3dc7 CO |
5103 | return r; |
5104 | } | |
5105 | ||
1499fa80 | 5106 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
5b1c1493 CO |
5107 | { |
5108 | return VM_FAULT_SIGBUS; | |
5109 | } | |
5110 | ||
1fe779f8 CO |
5111 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
5112 | { | |
5113 | int ret; | |
5114 | ||
5115 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
951179ce | 5116 | return -EINVAL; |
b3646477 | 5117 | ret = static_call(kvm_x86_set_tss_addr)(kvm, addr); |
1fe779f8 CO |
5118 | return ret; |
5119 | } | |
5120 | ||
b927a3ce SY |
5121 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
5122 | u64 ident_addr) | |
5123 | { | |
b3646477 | 5124 | return static_call(kvm_x86_set_identity_map_addr)(kvm, ident_addr); |
b927a3ce SY |
5125 | } |
5126 | ||
1fe779f8 | 5127 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
bc8a3d89 | 5128 | unsigned long kvm_nr_mmu_pages) |
1fe779f8 CO |
5129 | { |
5130 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
5131 | return -EINVAL; | |
5132 | ||
79fac95e | 5133 | mutex_lock(&kvm->slots_lock); |
1fe779f8 CO |
5134 | |
5135 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 5136 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 5137 | |
79fac95e | 5138 | mutex_unlock(&kvm->slots_lock); |
1fe779f8 CO |
5139 | return 0; |
5140 | } | |
5141 | ||
bc8a3d89 | 5142 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
1fe779f8 | 5143 | { |
39de71ec | 5144 | return kvm->arch.n_max_mmu_pages; |
1fe779f8 CO |
5145 | } |
5146 | ||
1fe779f8 CO |
5147 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
5148 | { | |
90bca052 | 5149 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
5150 | int r; |
5151 | ||
5152 | r = 0; | |
5153 | switch (chip->chip_id) { | |
5154 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 | 5155 | memcpy(&chip->chip.pic, &pic->pics[0], |
1fe779f8 CO |
5156 | sizeof(struct kvm_pic_state)); |
5157 | break; | |
5158 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 | 5159 | memcpy(&chip->chip.pic, &pic->pics[1], |
1fe779f8 CO |
5160 | sizeof(struct kvm_pic_state)); |
5161 | break; | |
5162 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 5163 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
5164 | break; |
5165 | default: | |
5166 | r = -EINVAL; | |
5167 | break; | |
5168 | } | |
5169 | return r; | |
5170 | } | |
5171 | ||
5172 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
5173 | { | |
90bca052 | 5174 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
5175 | int r; |
5176 | ||
5177 | r = 0; | |
5178 | switch (chip->chip_id) { | |
5179 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 DH |
5180 | spin_lock(&pic->lock); |
5181 | memcpy(&pic->pics[0], &chip->chip.pic, | |
1fe779f8 | 5182 | sizeof(struct kvm_pic_state)); |
90bca052 | 5183 | spin_unlock(&pic->lock); |
1fe779f8 CO |
5184 | break; |
5185 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 DH |
5186 | spin_lock(&pic->lock); |
5187 | memcpy(&pic->pics[1], &chip->chip.pic, | |
1fe779f8 | 5188 | sizeof(struct kvm_pic_state)); |
90bca052 | 5189 | spin_unlock(&pic->lock); |
1fe779f8 CO |
5190 | break; |
5191 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 5192 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
5193 | break; |
5194 | default: | |
5195 | r = -EINVAL; | |
5196 | break; | |
5197 | } | |
90bca052 | 5198 | kvm_pic_update_irq(pic); |
1fe779f8 CO |
5199 | return r; |
5200 | } | |
5201 | ||
e0f63cb9 SY |
5202 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
5203 | { | |
34f3941c RK |
5204 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
5205 | ||
5206 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); | |
5207 | ||
5208 | mutex_lock(&kps->lock); | |
5209 | memcpy(ps, &kps->channels, sizeof(*ps)); | |
5210 | mutex_unlock(&kps->lock); | |
2da29bcc | 5211 | return 0; |
e0f63cb9 SY |
5212 | } |
5213 | ||
5214 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
5215 | { | |
0185604c | 5216 | int i; |
09edea72 RK |
5217 | struct kvm_pit *pit = kvm->arch.vpit; |
5218 | ||
5219 | mutex_lock(&pit->pit_state.lock); | |
34f3941c | 5220 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
0185604c | 5221 | for (i = 0; i < 3; i++) |
09edea72 RK |
5222 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
5223 | mutex_unlock(&pit->pit_state.lock); | |
2da29bcc | 5224 | return 0; |
e9f42757 BK |
5225 | } |
5226 | ||
5227 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
5228 | { | |
e9f42757 BK |
5229 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
5230 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, | |
5231 | sizeof(ps->channels)); | |
5232 | ps->flags = kvm->arch.vpit->pit_state.flags; | |
5233 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); | |
97e69aa6 | 5234 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
2da29bcc | 5235 | return 0; |
e9f42757 BK |
5236 | } |
5237 | ||
5238 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
5239 | { | |
2da29bcc | 5240 | int start = 0; |
0185604c | 5241 | int i; |
e9f42757 | 5242 | u32 prev_legacy, cur_legacy; |
09edea72 RK |
5243 | struct kvm_pit *pit = kvm->arch.vpit; |
5244 | ||
5245 | mutex_lock(&pit->pit_state.lock); | |
5246 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; | |
e9f42757 BK |
5247 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
5248 | if (!prev_legacy && cur_legacy) | |
5249 | start = 1; | |
09edea72 RK |
5250 | memcpy(&pit->pit_state.channels, &ps->channels, |
5251 | sizeof(pit->pit_state.channels)); | |
5252 | pit->pit_state.flags = ps->flags; | |
0185604c | 5253 | for (i = 0; i < 3; i++) |
09edea72 | 5254 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
e5e57e7a | 5255 | start && i == 0); |
09edea72 | 5256 | mutex_unlock(&pit->pit_state.lock); |
2da29bcc | 5257 | return 0; |
e0f63cb9 SY |
5258 | } |
5259 | ||
52d939a0 MT |
5260 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
5261 | struct kvm_reinject_control *control) | |
5262 | { | |
71474e2f RK |
5263 | struct kvm_pit *pit = kvm->arch.vpit; |
5264 | ||
71474e2f RK |
5265 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
5266 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL | |
5267 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. | |
5268 | */ | |
5269 | mutex_lock(&pit->pit_state.lock); | |
5270 | kvm_pit_set_reinject(pit, control->pit_reinject); | |
5271 | mutex_unlock(&pit->pit_state.lock); | |
b39c90b6 | 5272 | |
52d939a0 MT |
5273 | return 0; |
5274 | } | |
5275 | ||
0dff0846 | 5276 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
5bb064dc | 5277 | { |
a018eba5 | 5278 | |
88178fd4 | 5279 | /* |
a018eba5 SC |
5280 | * Flush all CPUs' dirty log buffers to the dirty_bitmap. Called |
5281 | * before reporting dirty_bitmap to userspace. KVM flushes the buffers | |
5282 | * on all VM-Exits, thus we only need to kick running vCPUs to force a | |
5283 | * VM-Exit. | |
88178fd4 | 5284 | */ |
a018eba5 SC |
5285 | struct kvm_vcpu *vcpu; |
5286 | int i; | |
5287 | ||
5288 | kvm_for_each_vcpu(i, vcpu, kvm) | |
5289 | kvm_vcpu_kick(vcpu); | |
5bb064dc ZX |
5290 | } |
5291 | ||
aa2fbe6d YZ |
5292 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
5293 | bool line_status) | |
23d43cf9 CD |
5294 | { |
5295 | if (!irqchip_in_kernel(kvm)) | |
5296 | return -ENXIO; | |
5297 | ||
5298 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, | |
aa2fbe6d YZ |
5299 | irq_event->irq, irq_event->level, |
5300 | line_status); | |
23d43cf9 CD |
5301 | return 0; |
5302 | } | |
5303 | ||
e5d83c74 PB |
5304 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
5305 | struct kvm_enable_cap *cap) | |
90de4a18 NA |
5306 | { |
5307 | int r; | |
5308 | ||
5309 | if (cap->flags) | |
5310 | return -EINVAL; | |
5311 | ||
5312 | switch (cap->cap) { | |
5313 | case KVM_CAP_DISABLE_QUIRKS: | |
5314 | kvm->arch.disabled_quirks = cap->args[0]; | |
5315 | r = 0; | |
5316 | break; | |
49df6397 SR |
5317 | case KVM_CAP_SPLIT_IRQCHIP: { |
5318 | mutex_lock(&kvm->lock); | |
b053b2ae SR |
5319 | r = -EINVAL; |
5320 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) | |
5321 | goto split_irqchip_unlock; | |
49df6397 SR |
5322 | r = -EEXIST; |
5323 | if (irqchip_in_kernel(kvm)) | |
5324 | goto split_irqchip_unlock; | |
557abc40 | 5325 | if (kvm->created_vcpus) |
49df6397 SR |
5326 | goto split_irqchip_unlock; |
5327 | r = kvm_setup_empty_irq_routing(kvm); | |
5c0aea0e | 5328 | if (r) |
49df6397 SR |
5329 | goto split_irqchip_unlock; |
5330 | /* Pairs with irqchip_in_kernel. */ | |
5331 | smp_wmb(); | |
49776faf | 5332 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
b053b2ae | 5333 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
49df6397 SR |
5334 | r = 0; |
5335 | split_irqchip_unlock: | |
5336 | mutex_unlock(&kvm->lock); | |
5337 | break; | |
5338 | } | |
37131313 RK |
5339 | case KVM_CAP_X2APIC_API: |
5340 | r = -EINVAL; | |
5341 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) | |
5342 | break; | |
5343 | ||
5344 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) | |
5345 | kvm->arch.x2apic_format = true; | |
c519265f RK |
5346 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
5347 | kvm->arch.x2apic_broadcast_quirk_disabled = true; | |
37131313 RK |
5348 | |
5349 | r = 0; | |
5350 | break; | |
4d5422ce WL |
5351 | case KVM_CAP_X86_DISABLE_EXITS: |
5352 | r = -EINVAL; | |
5353 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) | |
5354 | break; | |
5355 | ||
5356 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && | |
5357 | kvm_can_mwait_in_guest()) | |
5358 | kvm->arch.mwait_in_guest = true; | |
766d3571 | 5359 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
caa057a2 | 5360 | kvm->arch.hlt_in_guest = true; |
b31c114b WL |
5361 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
5362 | kvm->arch.pause_in_guest = true; | |
b5170063 WL |
5363 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
5364 | kvm->arch.cstate_in_guest = true; | |
4d5422ce WL |
5365 | r = 0; |
5366 | break; | |
6fbbde9a DS |
5367 | case KVM_CAP_MSR_PLATFORM_INFO: |
5368 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; | |
5369 | r = 0; | |
c4f55198 JM |
5370 | break; |
5371 | case KVM_CAP_EXCEPTION_PAYLOAD: | |
5372 | kvm->arch.exception_payload_enabled = cap->args[0]; | |
5373 | r = 0; | |
6fbbde9a | 5374 | break; |
1ae09954 AG |
5375 | case KVM_CAP_X86_USER_SPACE_MSR: |
5376 | kvm->arch.user_space_msr_mask = cap->args[0]; | |
5377 | r = 0; | |
5378 | break; | |
fe6b6bc8 CQ |
5379 | case KVM_CAP_X86_BUS_LOCK_EXIT: |
5380 | r = -EINVAL; | |
5381 | if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) | |
5382 | break; | |
5383 | ||
5384 | if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && | |
5385 | (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) | |
5386 | break; | |
5387 | ||
5388 | if (kvm_has_bus_lock_exit && | |
5389 | cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) | |
5390 | kvm->arch.bus_lock_detection_enabled = true; | |
5391 | r = 0; | |
5392 | break; | |
90de4a18 NA |
5393 | default: |
5394 | r = -EINVAL; | |
5395 | break; | |
5396 | } | |
5397 | return r; | |
5398 | } | |
5399 | ||
1a155254 AG |
5400 | static void kvm_clear_msr_filter(struct kvm *kvm) |
5401 | { | |
5402 | u32 i; | |
5403 | u32 count = kvm->arch.msr_filter.count; | |
5404 | struct msr_bitmap_range ranges[16]; | |
5405 | ||
5406 | mutex_lock(&kvm->lock); | |
5407 | kvm->arch.msr_filter.count = 0; | |
5408 | memcpy(ranges, kvm->arch.msr_filter.ranges, count * sizeof(ranges[0])); | |
5409 | mutex_unlock(&kvm->lock); | |
5410 | synchronize_srcu(&kvm->srcu); | |
5411 | ||
5412 | for (i = 0; i < count; i++) | |
5413 | kfree(ranges[i].bitmap); | |
5414 | } | |
5415 | ||
5416 | static int kvm_add_msr_filter(struct kvm *kvm, struct kvm_msr_filter_range *user_range) | |
5417 | { | |
5418 | struct msr_bitmap_range *ranges = kvm->arch.msr_filter.ranges; | |
5419 | struct msr_bitmap_range range; | |
5420 | unsigned long *bitmap = NULL; | |
5421 | size_t bitmap_size; | |
5422 | int r; | |
5423 | ||
5424 | if (!user_range->nmsrs) | |
5425 | return 0; | |
5426 | ||
5427 | bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); | |
5428 | if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE) | |
5429 | return -EINVAL; | |
5430 | ||
5431 | bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); | |
5432 | if (IS_ERR(bitmap)) | |
5433 | return PTR_ERR(bitmap); | |
5434 | ||
5435 | range = (struct msr_bitmap_range) { | |
5436 | .flags = user_range->flags, | |
5437 | .base = user_range->base, | |
5438 | .nmsrs = user_range->nmsrs, | |
5439 | .bitmap = bitmap, | |
5440 | }; | |
5441 | ||
5442 | if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) { | |
5443 | r = -EINVAL; | |
5444 | goto err; | |
5445 | } | |
5446 | ||
5447 | if (!range.flags) { | |
5448 | r = -EINVAL; | |
5449 | goto err; | |
5450 | } | |
5451 | ||
5452 | /* Everything ok, add this range identifier to our global pool */ | |
5453 | ranges[kvm->arch.msr_filter.count] = range; | |
5454 | /* Make sure we filled the array before we tell anyone to walk it */ | |
5455 | smp_wmb(); | |
5456 | kvm->arch.msr_filter.count++; | |
5457 | ||
5458 | return 0; | |
5459 | err: | |
5460 | kfree(bitmap); | |
5461 | return r; | |
5462 | } | |
5463 | ||
5464 | static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp) | |
5465 | { | |
5466 | struct kvm_msr_filter __user *user_msr_filter = argp; | |
5467 | struct kvm_msr_filter filter; | |
5468 | bool default_allow; | |
5469 | int r = 0; | |
043248b3 | 5470 | bool empty = true; |
1a155254 AG |
5471 | u32 i; |
5472 | ||
5473 | if (copy_from_user(&filter, user_msr_filter, sizeof(filter))) | |
5474 | return -EFAULT; | |
5475 | ||
043248b3 PB |
5476 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) |
5477 | empty &= !filter.ranges[i].nmsrs; | |
1a155254 AG |
5478 | |
5479 | default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY); | |
043248b3 PB |
5480 | if (empty && !default_allow) |
5481 | return -EINVAL; | |
5482 | ||
5483 | kvm_clear_msr_filter(kvm); | |
5484 | ||
1a155254 AG |
5485 | kvm->arch.msr_filter.default_allow = default_allow; |
5486 | ||
5487 | /* | |
5488 | * Protect from concurrent calls to this function that could trigger | |
5489 | * a TOCTOU violation on kvm->arch.msr_filter.count. | |
5490 | */ | |
5491 | mutex_lock(&kvm->lock); | |
5492 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) { | |
5493 | r = kvm_add_msr_filter(kvm, &filter.ranges[i]); | |
5494 | if (r) | |
5495 | break; | |
5496 | } | |
5497 | ||
5498 | kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED); | |
5499 | mutex_unlock(&kvm->lock); | |
5500 | ||
5501 | return r; | |
5502 | } | |
5503 | ||
1fe779f8 CO |
5504 | long kvm_arch_vm_ioctl(struct file *filp, |
5505 | unsigned int ioctl, unsigned long arg) | |
5506 | { | |
5507 | struct kvm *kvm = filp->private_data; | |
5508 | void __user *argp = (void __user *)arg; | |
367e1319 | 5509 | int r = -ENOTTY; |
f0d66275 DH |
5510 | /* |
5511 | * This union makes it completely explicit to gcc-3.x | |
5512 | * that these two variables' stack usage should be | |
5513 | * combined, not added together. | |
5514 | */ | |
5515 | union { | |
5516 | struct kvm_pit_state ps; | |
e9f42757 | 5517 | struct kvm_pit_state2 ps2; |
c5ff41ce | 5518 | struct kvm_pit_config pit_config; |
f0d66275 | 5519 | } u; |
1fe779f8 CO |
5520 | |
5521 | switch (ioctl) { | |
5522 | case KVM_SET_TSS_ADDR: | |
5523 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1fe779f8 | 5524 | break; |
b927a3ce SY |
5525 | case KVM_SET_IDENTITY_MAP_ADDR: { |
5526 | u64 ident_addr; | |
5527 | ||
1af1ac91 DH |
5528 | mutex_lock(&kvm->lock); |
5529 | r = -EINVAL; | |
5530 | if (kvm->created_vcpus) | |
5531 | goto set_identity_unlock; | |
b927a3ce | 5532 | r = -EFAULT; |
0e96f31e | 5533 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
1af1ac91 | 5534 | goto set_identity_unlock; |
b927a3ce | 5535 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
1af1ac91 DH |
5536 | set_identity_unlock: |
5537 | mutex_unlock(&kvm->lock); | |
b927a3ce SY |
5538 | break; |
5539 | } | |
1fe779f8 CO |
5540 | case KVM_SET_NR_MMU_PAGES: |
5541 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1fe779f8 CO |
5542 | break; |
5543 | case KVM_GET_NR_MMU_PAGES: | |
5544 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
5545 | break; | |
3ddea128 | 5546 | case KVM_CREATE_IRQCHIP: { |
3ddea128 | 5547 | mutex_lock(&kvm->lock); |
09941366 | 5548 | |
3ddea128 | 5549 | r = -EEXIST; |
35e6eaa3 | 5550 | if (irqchip_in_kernel(kvm)) |
3ddea128 | 5551 | goto create_irqchip_unlock; |
09941366 | 5552 | |
3e515705 | 5553 | r = -EINVAL; |
557abc40 | 5554 | if (kvm->created_vcpus) |
3e515705 | 5555 | goto create_irqchip_unlock; |
09941366 RK |
5556 | |
5557 | r = kvm_pic_init(kvm); | |
5558 | if (r) | |
3ddea128 | 5559 | goto create_irqchip_unlock; |
09941366 RK |
5560 | |
5561 | r = kvm_ioapic_init(kvm); | |
5562 | if (r) { | |
09941366 | 5563 | kvm_pic_destroy(kvm); |
3ddea128 | 5564 | goto create_irqchip_unlock; |
09941366 RK |
5565 | } |
5566 | ||
399ec807 AK |
5567 | r = kvm_setup_default_irq_routing(kvm); |
5568 | if (r) { | |
72bb2fcd | 5569 | kvm_ioapic_destroy(kvm); |
09941366 | 5570 | kvm_pic_destroy(kvm); |
71ba994c | 5571 | goto create_irqchip_unlock; |
399ec807 | 5572 | } |
49776faf | 5573 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
71ba994c | 5574 | smp_wmb(); |
49776faf | 5575 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
3ddea128 MT |
5576 | create_irqchip_unlock: |
5577 | mutex_unlock(&kvm->lock); | |
1fe779f8 | 5578 | break; |
3ddea128 | 5579 | } |
7837699f | 5580 | case KVM_CREATE_PIT: |
c5ff41ce JK |
5581 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
5582 | goto create_pit; | |
5583 | case KVM_CREATE_PIT2: | |
5584 | r = -EFAULT; | |
5585 | if (copy_from_user(&u.pit_config, argp, | |
5586 | sizeof(struct kvm_pit_config))) | |
5587 | goto out; | |
5588 | create_pit: | |
250715a6 | 5589 | mutex_lock(&kvm->lock); |
269e05e4 AK |
5590 | r = -EEXIST; |
5591 | if (kvm->arch.vpit) | |
5592 | goto create_pit_unlock; | |
7837699f | 5593 | r = -ENOMEM; |
c5ff41ce | 5594 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
7837699f SY |
5595 | if (kvm->arch.vpit) |
5596 | r = 0; | |
269e05e4 | 5597 | create_pit_unlock: |
250715a6 | 5598 | mutex_unlock(&kvm->lock); |
7837699f | 5599 | break; |
1fe779f8 CO |
5600 | case KVM_GET_IRQCHIP: { |
5601 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5602 | struct kvm_irqchip *chip; |
1fe779f8 | 5603 | |
ff5c2c03 SL |
5604 | chip = memdup_user(argp, sizeof(*chip)); |
5605 | if (IS_ERR(chip)) { | |
5606 | r = PTR_ERR(chip); | |
1fe779f8 | 5607 | goto out; |
ff5c2c03 SL |
5608 | } |
5609 | ||
1fe779f8 | 5610 | r = -ENXIO; |
826da321 | 5611 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5612 | goto get_irqchip_out; |
5613 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); | |
1fe779f8 | 5614 | if (r) |
f0d66275 | 5615 | goto get_irqchip_out; |
1fe779f8 | 5616 | r = -EFAULT; |
0e96f31e | 5617 | if (copy_to_user(argp, chip, sizeof(*chip))) |
f0d66275 | 5618 | goto get_irqchip_out; |
1fe779f8 | 5619 | r = 0; |
f0d66275 DH |
5620 | get_irqchip_out: |
5621 | kfree(chip); | |
1fe779f8 CO |
5622 | break; |
5623 | } | |
5624 | case KVM_SET_IRQCHIP: { | |
5625 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5626 | struct kvm_irqchip *chip; |
1fe779f8 | 5627 | |
ff5c2c03 SL |
5628 | chip = memdup_user(argp, sizeof(*chip)); |
5629 | if (IS_ERR(chip)) { | |
5630 | r = PTR_ERR(chip); | |
1fe779f8 | 5631 | goto out; |
ff5c2c03 SL |
5632 | } |
5633 | ||
1fe779f8 | 5634 | r = -ENXIO; |
826da321 | 5635 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5636 | goto set_irqchip_out; |
5637 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); | |
f0d66275 DH |
5638 | set_irqchip_out: |
5639 | kfree(chip); | |
1fe779f8 CO |
5640 | break; |
5641 | } | |
e0f63cb9 | 5642 | case KVM_GET_PIT: { |
e0f63cb9 | 5643 | r = -EFAULT; |
f0d66275 | 5644 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5645 | goto out; |
5646 | r = -ENXIO; | |
5647 | if (!kvm->arch.vpit) | |
5648 | goto out; | |
f0d66275 | 5649 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
e0f63cb9 SY |
5650 | if (r) |
5651 | goto out; | |
5652 | r = -EFAULT; | |
f0d66275 | 5653 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5654 | goto out; |
5655 | r = 0; | |
5656 | break; | |
5657 | } | |
5658 | case KVM_SET_PIT: { | |
e0f63cb9 | 5659 | r = -EFAULT; |
0e96f31e | 5660 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
e0f63cb9 | 5661 | goto out; |
7289fdb5 | 5662 | mutex_lock(&kvm->lock); |
e0f63cb9 SY |
5663 | r = -ENXIO; |
5664 | if (!kvm->arch.vpit) | |
7289fdb5 | 5665 | goto set_pit_out; |
f0d66275 | 5666 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
7289fdb5 SR |
5667 | set_pit_out: |
5668 | mutex_unlock(&kvm->lock); | |
e0f63cb9 SY |
5669 | break; |
5670 | } | |
e9f42757 BK |
5671 | case KVM_GET_PIT2: { |
5672 | r = -ENXIO; | |
5673 | if (!kvm->arch.vpit) | |
5674 | goto out; | |
5675 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); | |
5676 | if (r) | |
5677 | goto out; | |
5678 | r = -EFAULT; | |
5679 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) | |
5680 | goto out; | |
5681 | r = 0; | |
5682 | break; | |
5683 | } | |
5684 | case KVM_SET_PIT2: { | |
5685 | r = -EFAULT; | |
5686 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) | |
5687 | goto out; | |
7289fdb5 | 5688 | mutex_lock(&kvm->lock); |
e9f42757 BK |
5689 | r = -ENXIO; |
5690 | if (!kvm->arch.vpit) | |
7289fdb5 | 5691 | goto set_pit2_out; |
e9f42757 | 5692 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
7289fdb5 SR |
5693 | set_pit2_out: |
5694 | mutex_unlock(&kvm->lock); | |
e9f42757 BK |
5695 | break; |
5696 | } | |
52d939a0 MT |
5697 | case KVM_REINJECT_CONTROL: { |
5698 | struct kvm_reinject_control control; | |
5699 | r = -EFAULT; | |
5700 | if (copy_from_user(&control, argp, sizeof(control))) | |
5701 | goto out; | |
cad23e72 ML |
5702 | r = -ENXIO; |
5703 | if (!kvm->arch.vpit) | |
5704 | goto out; | |
52d939a0 | 5705 | r = kvm_vm_ioctl_reinject(kvm, &control); |
52d939a0 MT |
5706 | break; |
5707 | } | |
d71ba788 PB |
5708 | case KVM_SET_BOOT_CPU_ID: |
5709 | r = 0; | |
5710 | mutex_lock(&kvm->lock); | |
557abc40 | 5711 | if (kvm->created_vcpus) |
d71ba788 PB |
5712 | r = -EBUSY; |
5713 | else | |
5714 | kvm->arch.bsp_vcpu_id = arg; | |
5715 | mutex_unlock(&kvm->lock); | |
5716 | break; | |
b59b153d | 5717 | #ifdef CONFIG_KVM_XEN |
ffde22ac | 5718 | case KVM_XEN_HVM_CONFIG: { |
51776043 | 5719 | struct kvm_xen_hvm_config xhc; |
ffde22ac | 5720 | r = -EFAULT; |
51776043 | 5721 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
ffde22ac | 5722 | goto out; |
78e9878c | 5723 | r = kvm_xen_hvm_config(kvm, &xhc); |
ffde22ac ES |
5724 | break; |
5725 | } | |
a76b9641 JM |
5726 | case KVM_XEN_HVM_GET_ATTR: { |
5727 | struct kvm_xen_hvm_attr xha; | |
5728 | ||
5729 | r = -EFAULT; | |
5730 | if (copy_from_user(&xha, argp, sizeof(xha))) | |
ffde22ac | 5731 | goto out; |
a76b9641 JM |
5732 | r = kvm_xen_hvm_get_attr(kvm, &xha); |
5733 | if (!r && copy_to_user(argp, &xha, sizeof(xha))) | |
5734 | r = -EFAULT; | |
5735 | break; | |
5736 | } | |
5737 | case KVM_XEN_HVM_SET_ATTR: { | |
5738 | struct kvm_xen_hvm_attr xha; | |
5739 | ||
5740 | r = -EFAULT; | |
5741 | if (copy_from_user(&xha, argp, sizeof(xha))) | |
5742 | goto out; | |
5743 | r = kvm_xen_hvm_set_attr(kvm, &xha); | |
ffde22ac ES |
5744 | break; |
5745 | } | |
b59b153d | 5746 | #endif |
afbcf7ab | 5747 | case KVM_SET_CLOCK: { |
afbcf7ab GC |
5748 | struct kvm_clock_data user_ns; |
5749 | u64 now_ns; | |
afbcf7ab GC |
5750 | |
5751 | r = -EFAULT; | |
5752 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) | |
5753 | goto out; | |
5754 | ||
5755 | r = -EINVAL; | |
5756 | if (user_ns.flags) | |
5757 | goto out; | |
5758 | ||
5759 | r = 0; | |
0bc48bea RK |
5760 | /* |
5761 | * TODO: userspace has to take care of races with VCPU_RUN, so | |
5762 | * kvm_gen_update_masterclock() can be cut down to locked | |
5763 | * pvclock_update_vm_gtod_copy(). | |
5764 | */ | |
5765 | kvm_gen_update_masterclock(kvm); | |
e891a32e | 5766 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5767 | kvm->arch.kvmclock_offset += user_ns.clock - now_ns; |
0bc48bea | 5768 | kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); |
afbcf7ab GC |
5769 | break; |
5770 | } | |
5771 | case KVM_GET_CLOCK: { | |
afbcf7ab GC |
5772 | struct kvm_clock_data user_ns; |
5773 | u64 now_ns; | |
5774 | ||
e891a32e | 5775 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5776 | user_ns.clock = now_ns; |
e3fd9a93 | 5777 | user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; |
97e69aa6 | 5778 | memset(&user_ns.pad, 0, sizeof(user_ns.pad)); |
afbcf7ab GC |
5779 | |
5780 | r = -EFAULT; | |
5781 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) | |
5782 | goto out; | |
5783 | r = 0; | |
5784 | break; | |
5785 | } | |
5acc5c06 BS |
5786 | case KVM_MEMORY_ENCRYPT_OP: { |
5787 | r = -ENOTTY; | |
afaf0b2f | 5788 | if (kvm_x86_ops.mem_enc_op) |
b3646477 | 5789 | r = static_call(kvm_x86_mem_enc_op)(kvm, argp); |
5acc5c06 BS |
5790 | break; |
5791 | } | |
69eaedee BS |
5792 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
5793 | struct kvm_enc_region region; | |
5794 | ||
5795 | r = -EFAULT; | |
5796 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5797 | goto out; | |
5798 | ||
5799 | r = -ENOTTY; | |
afaf0b2f | 5800 | if (kvm_x86_ops.mem_enc_reg_region) |
b3646477 | 5801 | r = static_call(kvm_x86_mem_enc_reg_region)(kvm, ®ion); |
69eaedee BS |
5802 | break; |
5803 | } | |
5804 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { | |
5805 | struct kvm_enc_region region; | |
5806 | ||
5807 | r = -EFAULT; | |
5808 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5809 | goto out; | |
5810 | ||
5811 | r = -ENOTTY; | |
afaf0b2f | 5812 | if (kvm_x86_ops.mem_enc_unreg_region) |
b3646477 | 5813 | r = static_call(kvm_x86_mem_enc_unreg_region)(kvm, ®ion); |
69eaedee BS |
5814 | break; |
5815 | } | |
faeb7833 RK |
5816 | case KVM_HYPERV_EVENTFD: { |
5817 | struct kvm_hyperv_eventfd hvevfd; | |
5818 | ||
5819 | r = -EFAULT; | |
5820 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) | |
5821 | goto out; | |
5822 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); | |
5823 | break; | |
5824 | } | |
66bb8a06 EH |
5825 | case KVM_SET_PMU_EVENT_FILTER: |
5826 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); | |
5827 | break; | |
1a155254 AG |
5828 | case KVM_X86_SET_MSR_FILTER: |
5829 | r = kvm_vm_ioctl_set_msr_filter(kvm, argp); | |
5830 | break; | |
1fe779f8 | 5831 | default: |
ad6260da | 5832 | r = -ENOTTY; |
1fe779f8 CO |
5833 | } |
5834 | out: | |
5835 | return r; | |
5836 | } | |
5837 | ||
a16b043c | 5838 | static void kvm_init_msr_list(void) |
043405e1 | 5839 | { |
24c29b7a | 5840 | struct x86_pmu_capability x86_pmu; |
043405e1 | 5841 | u32 dummy[2]; |
7a5ee6ed | 5842 | unsigned i; |
043405e1 | 5843 | |
e2ada66e | 5844 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
7a5ee6ed | 5845 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
24c29b7a PB |
5846 | |
5847 | perf_get_x86_pmu_capability(&x86_pmu); | |
e2ada66e | 5848 | |
6cbee2b9 XL |
5849 | num_msrs_to_save = 0; |
5850 | num_emulated_msrs = 0; | |
5851 | num_msr_based_features = 0; | |
5852 | ||
7a5ee6ed CQ |
5853 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
5854 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) | |
043405e1 | 5855 | continue; |
93c4adc7 PB |
5856 | |
5857 | /* | |
5858 | * Even MSRs that are valid in the host may not be exposed | |
9dbe6cf9 | 5859 | * to the guests in some cases. |
93c4adc7 | 5860 | */ |
7a5ee6ed | 5861 | switch (msrs_to_save_all[i]) { |
93c4adc7 | 5862 | case MSR_IA32_BNDCFGS: |
503234b3 | 5863 | if (!kvm_mpx_supported()) |
93c4adc7 PB |
5864 | continue; |
5865 | break; | |
9dbe6cf9 | 5866 | case MSR_TSC_AUX: |
13908510 | 5867 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
9dbe6cf9 PB |
5868 | continue; |
5869 | break; | |
f4cfcd2d ML |
5870 | case MSR_IA32_UMWAIT_CONTROL: |
5871 | if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) | |
5872 | continue; | |
5873 | break; | |
bf8c55d8 CP |
5874 | case MSR_IA32_RTIT_CTL: |
5875 | case MSR_IA32_RTIT_STATUS: | |
7b874c26 | 5876 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
bf8c55d8 CP |
5877 | continue; |
5878 | break; | |
5879 | case MSR_IA32_RTIT_CR3_MATCH: | |
7b874c26 | 5880 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5881 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
5882 | continue; | |
5883 | break; | |
5884 | case MSR_IA32_RTIT_OUTPUT_BASE: | |
5885 | case MSR_IA32_RTIT_OUTPUT_MASK: | |
7b874c26 | 5886 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5887 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
5888 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) | |
5889 | continue; | |
5890 | break; | |
7cb85fc4 | 5891 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
7b874c26 | 5892 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
7a5ee6ed | 5893 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
bf8c55d8 CP |
5894 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
5895 | continue; | |
5896 | break; | |
cf05a67b | 5897 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
7a5ee6ed | 5898 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
24c29b7a PB |
5899 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5900 | continue; | |
5901 | break; | |
cf05a67b | 5902 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
7a5ee6ed | 5903 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
24c29b7a PB |
5904 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5905 | continue; | |
7cb85fc4 | 5906 | break; |
93c4adc7 PB |
5907 | default: |
5908 | break; | |
5909 | } | |
5910 | ||
7a5ee6ed | 5911 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
043405e1 | 5912 | } |
62ef68bb | 5913 | |
7a5ee6ed | 5914 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
b3646477 | 5915 | if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i])) |
bc226f07 | 5916 | continue; |
62ef68bb | 5917 | |
7a5ee6ed | 5918 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
62ef68bb | 5919 | } |
801e459a | 5920 | |
7a5ee6ed | 5921 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
801e459a TL |
5922 | struct kvm_msr_entry msr; |
5923 | ||
7a5ee6ed | 5924 | msr.index = msr_based_features_all[i]; |
66421c1e | 5925 | if (kvm_get_msr_feature(&msr)) |
801e459a TL |
5926 | continue; |
5927 | ||
7a5ee6ed | 5928 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
801e459a | 5929 | } |
043405e1 CO |
5930 | } |
5931 | ||
bda9020e MT |
5932 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
5933 | const void *v) | |
bbd9b64e | 5934 | { |
70252a10 AK |
5935 | int handled = 0; |
5936 | int n; | |
5937 | ||
5938 | do { | |
5939 | n = min(len, 8); | |
bce87cce | 5940 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5941 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
5942 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 AK |
5943 | break; |
5944 | handled += n; | |
5945 | addr += n; | |
5946 | len -= n; | |
5947 | v += n; | |
5948 | } while (len); | |
bbd9b64e | 5949 | |
70252a10 | 5950 | return handled; |
bbd9b64e CO |
5951 | } |
5952 | ||
bda9020e | 5953 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
bbd9b64e | 5954 | { |
70252a10 AK |
5955 | int handled = 0; |
5956 | int n; | |
5957 | ||
5958 | do { | |
5959 | n = min(len, 8); | |
bce87cce | 5960 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5961 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
5962 | addr, n, v)) | |
5963 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 | 5964 | break; |
e39d200f | 5965 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
70252a10 AK |
5966 | handled += n; |
5967 | addr += n; | |
5968 | len -= n; | |
5969 | v += n; | |
5970 | } while (len); | |
bbd9b64e | 5971 | |
70252a10 | 5972 | return handled; |
bbd9b64e CO |
5973 | } |
5974 | ||
2dafc6c2 GN |
5975 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
5976 | struct kvm_segment *var, int seg) | |
5977 | { | |
b3646477 | 5978 | static_call(kvm_x86_set_segment)(vcpu, var, seg); |
2dafc6c2 GN |
5979 | } |
5980 | ||
5981 | void kvm_get_segment(struct kvm_vcpu *vcpu, | |
5982 | struct kvm_segment *var, int seg) | |
5983 | { | |
b3646477 | 5984 | static_call(kvm_x86_get_segment)(vcpu, var, seg); |
2dafc6c2 GN |
5985 | } |
5986 | ||
54987b7a PB |
5987 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
5988 | struct x86_exception *exception) | |
02f59dc9 JR |
5989 | { |
5990 | gpa_t t_gpa; | |
02f59dc9 JR |
5991 | |
5992 | BUG_ON(!mmu_is_nested(vcpu)); | |
5993 | ||
5994 | /* NPT walks are always user-walks */ | |
5995 | access |= PFERR_USER_MASK; | |
44dd3ffa | 5996 | t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception); |
02f59dc9 JR |
5997 | |
5998 | return t_gpa; | |
5999 | } | |
6000 | ||
ab9ae313 AK |
6001 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
6002 | struct x86_exception *exception) | |
1871c602 | 6003 | { |
b3646477 | 6004 | u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
ab9ae313 | 6005 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
6006 | } |
6007 | ||
ab9ae313 AK |
6008 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
6009 | struct x86_exception *exception) | |
1871c602 | 6010 | { |
b3646477 | 6011 | u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 6012 | access |= PFERR_FETCH_MASK; |
ab9ae313 | 6013 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
6014 | } |
6015 | ||
ab9ae313 AK |
6016 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
6017 | struct x86_exception *exception) | |
1871c602 | 6018 | { |
b3646477 | 6019 | u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 6020 | access |= PFERR_WRITE_MASK; |
ab9ae313 | 6021 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
6022 | } |
6023 | ||
6024 | /* uses this to access any guest's mapped memory without checking CPL */ | |
ab9ae313 AK |
6025 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
6026 | struct x86_exception *exception) | |
1871c602 | 6027 | { |
ab9ae313 | 6028 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception); |
1871c602 GN |
6029 | } |
6030 | ||
6031 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, | |
6032 | struct kvm_vcpu *vcpu, u32 access, | |
bcc55cba | 6033 | struct x86_exception *exception) |
bbd9b64e CO |
6034 | { |
6035 | void *data = val; | |
10589a46 | 6036 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
6037 | |
6038 | while (bytes) { | |
14dfe855 | 6039 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access, |
ab9ae313 | 6040 | exception); |
bbd9b64e | 6041 | unsigned offset = addr & (PAGE_SIZE-1); |
77c2002e | 6042 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
bbd9b64e CO |
6043 | int ret; |
6044 | ||
bcc55cba | 6045 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 6046 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa PB |
6047 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
6048 | offset, toread); | |
10589a46 | 6049 | if (ret < 0) { |
c3cd7ffa | 6050 | r = X86EMUL_IO_NEEDED; |
10589a46 MT |
6051 | goto out; |
6052 | } | |
bbd9b64e | 6053 | |
77c2002e IE |
6054 | bytes -= toread; |
6055 | data += toread; | |
6056 | addr += toread; | |
bbd9b64e | 6057 | } |
10589a46 | 6058 | out: |
10589a46 | 6059 | return r; |
bbd9b64e | 6060 | } |
77c2002e | 6061 | |
1871c602 | 6062 | /* used for instruction fetching */ |
0f65dd70 AK |
6063 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
6064 | gva_t addr, void *val, unsigned int bytes, | |
bcc55cba | 6065 | struct x86_exception *exception) |
1871c602 | 6066 | { |
0f65dd70 | 6067 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
b3646477 | 6068 | u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
44583cba PB |
6069 | unsigned offset; |
6070 | int ret; | |
0f65dd70 | 6071 | |
44583cba PB |
6072 | /* Inline kvm_read_guest_virt_helper for speed. */ |
6073 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK, | |
6074 | exception); | |
6075 | if (unlikely(gpa == UNMAPPED_GVA)) | |
6076 | return X86EMUL_PROPAGATE_FAULT; | |
6077 | ||
6078 | offset = addr & (PAGE_SIZE-1); | |
6079 | if (WARN_ON(offset + bytes > PAGE_SIZE)) | |
6080 | bytes = (unsigned)PAGE_SIZE - offset; | |
54bf36aa PB |
6081 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
6082 | offset, bytes); | |
44583cba PB |
6083 | if (unlikely(ret < 0)) |
6084 | return X86EMUL_IO_NEEDED; | |
6085 | ||
6086 | return X86EMUL_CONTINUE; | |
1871c602 GN |
6087 | } |
6088 | ||
ce14e868 | 6089 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
0f65dd70 | 6090 | gva_t addr, void *val, unsigned int bytes, |
bcc55cba | 6091 | struct x86_exception *exception) |
1871c602 | 6092 | { |
b3646477 | 6093 | u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
0f65dd70 | 6094 | |
353c0956 PB |
6095 | /* |
6096 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED | |
6097 | * is returned, but our callers are not ready for that and they blindly | |
6098 | * call kvm_inject_page_fault. Ensure that they at least do not leak | |
6099 | * uninitialized kernel stack memory into cr2 and error code. | |
6100 | */ | |
6101 | memset(exception, 0, sizeof(*exception)); | |
1871c602 | 6102 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
bcc55cba | 6103 | exception); |
1871c602 | 6104 | } |
064aea77 | 6105 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
1871c602 | 6106 | |
ce14e868 PB |
6107 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
6108 | gva_t addr, void *val, unsigned int bytes, | |
3c9fa24c | 6109 | struct x86_exception *exception, bool system) |
1871c602 | 6110 | { |
0f65dd70 | 6111 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
3c9fa24c PB |
6112 | u32 access = 0; |
6113 | ||
b3646477 | 6114 | if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3) |
3c9fa24c PB |
6115 | access |= PFERR_USER_MASK; |
6116 | ||
6117 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); | |
1871c602 GN |
6118 | } |
6119 | ||
7a036a6f RK |
6120 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
6121 | unsigned long addr, void *val, unsigned int bytes) | |
6122 | { | |
6123 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6124 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); | |
6125 | ||
6126 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; | |
6127 | } | |
6128 | ||
ce14e868 PB |
6129 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
6130 | struct kvm_vcpu *vcpu, u32 access, | |
6131 | struct x86_exception *exception) | |
77c2002e IE |
6132 | { |
6133 | void *data = val; | |
6134 | int r = X86EMUL_CONTINUE; | |
6135 | ||
6136 | while (bytes) { | |
14dfe855 | 6137 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, |
ce14e868 | 6138 | access, |
ab9ae313 | 6139 | exception); |
77c2002e IE |
6140 | unsigned offset = addr & (PAGE_SIZE-1); |
6141 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); | |
6142 | int ret; | |
6143 | ||
bcc55cba | 6144 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 6145 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa | 6146 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
77c2002e | 6147 | if (ret < 0) { |
c3cd7ffa | 6148 | r = X86EMUL_IO_NEEDED; |
77c2002e IE |
6149 | goto out; |
6150 | } | |
6151 | ||
6152 | bytes -= towrite; | |
6153 | data += towrite; | |
6154 | addr += towrite; | |
6155 | } | |
6156 | out: | |
6157 | return r; | |
6158 | } | |
ce14e868 PB |
6159 | |
6160 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, | |
3c9fa24c PB |
6161 | unsigned int bytes, struct x86_exception *exception, |
6162 | bool system) | |
ce14e868 PB |
6163 | { |
6164 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
3c9fa24c PB |
6165 | u32 access = PFERR_WRITE_MASK; |
6166 | ||
b3646477 | 6167 | if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3) |
3c9fa24c | 6168 | access |= PFERR_USER_MASK; |
ce14e868 PB |
6169 | |
6170 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, | |
3c9fa24c | 6171 | access, exception); |
ce14e868 PB |
6172 | } |
6173 | ||
6174 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, | |
6175 | unsigned int bytes, struct x86_exception *exception) | |
6176 | { | |
c595ceee PB |
6177 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
6178 | vcpu->arch.l1tf_flush_l1d = true; | |
6179 | ||
ce14e868 PB |
6180 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
6181 | PFERR_WRITE_MASK, exception); | |
6182 | } | |
6a4d7550 | 6183 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
77c2002e | 6184 | |
082d06ed WL |
6185 | int handle_ud(struct kvm_vcpu *vcpu) |
6186 | { | |
b3dc0695 | 6187 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
6c86eedc | 6188 | int emul_type = EMULTYPE_TRAP_UD; |
6c86eedc WL |
6189 | char sig[5]; /* ud2; .ascii "kvm" */ |
6190 | struct x86_exception e; | |
6191 | ||
b3646477 | 6192 | if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, NULL, 0))) |
09e3e2a1 SC |
6193 | return 1; |
6194 | ||
6c86eedc | 6195 | if (force_emulation_prefix && |
3c9fa24c PB |
6196 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
6197 | sig, sizeof(sig), &e) == 0 && | |
b3dc0695 | 6198 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
6c86eedc | 6199 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
b4000606 | 6200 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
6c86eedc | 6201 | } |
082d06ed | 6202 | |
60fc3d02 | 6203 | return kvm_emulate_instruction(vcpu, emul_type); |
082d06ed WL |
6204 | } |
6205 | EXPORT_SYMBOL_GPL(handle_ud); | |
6206 | ||
0f89b207 TL |
6207 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
6208 | gpa_t gpa, bool write) | |
6209 | { | |
6210 | /* For APIC access vmexit */ | |
6211 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
6212 | return 1; | |
6213 | ||
6214 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { | |
6215 | trace_vcpu_match_mmio(gva, gpa, write, true); | |
6216 | return 1; | |
6217 | } | |
6218 | ||
6219 | return 0; | |
6220 | } | |
6221 | ||
af7cc7d1 XG |
6222 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
6223 | gpa_t *gpa, struct x86_exception *exception, | |
6224 | bool write) | |
6225 | { | |
b3646477 | 6226 | u32 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0) |
97d64b78 | 6227 | | (write ? PFERR_WRITE_MASK : 0); |
af7cc7d1 | 6228 | |
be94f6b7 HH |
6229 | /* |
6230 | * currently PKRU is only applied to ept enabled guest so | |
6231 | * there is no pkey in EPT page table for L1 guest or EPT | |
6232 | * shadow page table for L2 guest. | |
6233 | */ | |
97d64b78 | 6234 | if (vcpu_match_mmio_gva(vcpu, gva) |
97ec8c06 | 6235 | && !permission_fault(vcpu, vcpu->arch.walk_mmu, |
871bd034 | 6236 | vcpu->arch.mmio_access, 0, access)) { |
bebb106a XG |
6237 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
6238 | (gva & (PAGE_SIZE - 1)); | |
4f022648 | 6239 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
bebb106a XG |
6240 | return 1; |
6241 | } | |
6242 | ||
af7cc7d1 XG |
6243 | *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
6244 | ||
6245 | if (*gpa == UNMAPPED_GVA) | |
6246 | return -1; | |
6247 | ||
0f89b207 | 6248 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
af7cc7d1 XG |
6249 | } |
6250 | ||
3200f405 | 6251 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
bcc55cba | 6252 | const void *val, int bytes) |
bbd9b64e CO |
6253 | { |
6254 | int ret; | |
6255 | ||
54bf36aa | 6256 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
9f811285 | 6257 | if (ret < 0) |
bbd9b64e | 6258 | return 0; |
0eb05bf2 | 6259 | kvm_page_track_write(vcpu, gpa, val, bytes); |
bbd9b64e CO |
6260 | return 1; |
6261 | } | |
6262 | ||
77d197b2 XG |
6263 | struct read_write_emulator_ops { |
6264 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, | |
6265 | int bytes); | |
6266 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6267 | void *val, int bytes); | |
6268 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6269 | int bytes, void *val); | |
6270 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6271 | void *val, int bytes); | |
6272 | bool write; | |
6273 | }; | |
6274 | ||
6275 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) | |
6276 | { | |
6277 | if (vcpu->mmio_read_completed) { | |
77d197b2 | 6278 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
e39d200f | 6279 | vcpu->mmio_fragments[0].gpa, val); |
77d197b2 XG |
6280 | vcpu->mmio_read_completed = 0; |
6281 | return 1; | |
6282 | } | |
6283 | ||
6284 | return 0; | |
6285 | } | |
6286 | ||
6287 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6288 | void *val, int bytes) | |
6289 | { | |
54bf36aa | 6290 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
77d197b2 XG |
6291 | } |
6292 | ||
6293 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6294 | void *val, int bytes) | |
6295 | { | |
6296 | return emulator_write_phys(vcpu, gpa, val, bytes); | |
6297 | } | |
6298 | ||
6299 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) | |
6300 | { | |
e39d200f | 6301 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
77d197b2 XG |
6302 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
6303 | } | |
6304 | ||
6305 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6306 | void *val, int bytes) | |
6307 | { | |
e39d200f | 6308 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
77d197b2 XG |
6309 | return X86EMUL_IO_NEEDED; |
6310 | } | |
6311 | ||
6312 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
6313 | void *val, int bytes) | |
6314 | { | |
f78146b0 AK |
6315 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
6316 | ||
87da7e66 | 6317 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
77d197b2 XG |
6318 | return X86EMUL_CONTINUE; |
6319 | } | |
6320 | ||
0fbe9b0b | 6321 | static const struct read_write_emulator_ops read_emultor = { |
77d197b2 XG |
6322 | .read_write_prepare = read_prepare, |
6323 | .read_write_emulate = read_emulate, | |
6324 | .read_write_mmio = vcpu_mmio_read, | |
6325 | .read_write_exit_mmio = read_exit_mmio, | |
6326 | }; | |
6327 | ||
0fbe9b0b | 6328 | static const struct read_write_emulator_ops write_emultor = { |
77d197b2 XG |
6329 | .read_write_emulate = write_emulate, |
6330 | .read_write_mmio = write_mmio, | |
6331 | .read_write_exit_mmio = write_exit_mmio, | |
6332 | .write = true, | |
6333 | }; | |
6334 | ||
22388a3c XG |
6335 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
6336 | unsigned int bytes, | |
6337 | struct x86_exception *exception, | |
6338 | struct kvm_vcpu *vcpu, | |
0fbe9b0b | 6339 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 6340 | { |
af7cc7d1 XG |
6341 | gpa_t gpa; |
6342 | int handled, ret; | |
22388a3c | 6343 | bool write = ops->write; |
f78146b0 | 6344 | struct kvm_mmio_fragment *frag; |
c9b8b07c | 6345 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
0f89b207 TL |
6346 | |
6347 | /* | |
6348 | * If the exit was due to a NPF we may already have a GPA. | |
6349 | * If the GPA is present, use it to avoid the GVA to GPA table walk. | |
6350 | * Note, this cannot be used on string operations since string | |
6351 | * operation using rep will only have the initial GPA from the NPF | |
6352 | * occurred. | |
6353 | */ | |
744e699c SC |
6354 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
6355 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { | |
6356 | gpa = ctxt->gpa_val; | |
618232e2 BS |
6357 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
6358 | } else { | |
6359 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); | |
6360 | if (ret < 0) | |
6361 | return X86EMUL_PROPAGATE_FAULT; | |
0f89b207 | 6362 | } |
10589a46 | 6363 | |
618232e2 | 6364 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
bbd9b64e CO |
6365 | return X86EMUL_CONTINUE; |
6366 | ||
bbd9b64e CO |
6367 | /* |
6368 | * Is this MMIO handled locally? | |
6369 | */ | |
22388a3c | 6370 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
70252a10 | 6371 | if (handled == bytes) |
bbd9b64e | 6372 | return X86EMUL_CONTINUE; |
bbd9b64e | 6373 | |
70252a10 AK |
6374 | gpa += handled; |
6375 | bytes -= handled; | |
6376 | val += handled; | |
6377 | ||
87da7e66 XG |
6378 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
6379 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; | |
6380 | frag->gpa = gpa; | |
6381 | frag->data = val; | |
6382 | frag->len = bytes; | |
f78146b0 | 6383 | return X86EMUL_CONTINUE; |
bbd9b64e CO |
6384 | } |
6385 | ||
52eb5a6d XL |
6386 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
6387 | unsigned long addr, | |
22388a3c XG |
6388 | void *val, unsigned int bytes, |
6389 | struct x86_exception *exception, | |
0fbe9b0b | 6390 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 6391 | { |
0f65dd70 | 6392 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
f78146b0 AK |
6393 | gpa_t gpa; |
6394 | int rc; | |
6395 | ||
6396 | if (ops->read_write_prepare && | |
6397 | ops->read_write_prepare(vcpu, val, bytes)) | |
6398 | return X86EMUL_CONTINUE; | |
6399 | ||
6400 | vcpu->mmio_nr_fragments = 0; | |
0f65dd70 | 6401 | |
bbd9b64e CO |
6402 | /* Crossing a page boundary? */ |
6403 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
f78146b0 | 6404 | int now; |
bbd9b64e CO |
6405 | |
6406 | now = -addr & ~PAGE_MASK; | |
22388a3c XG |
6407 | rc = emulator_read_write_onepage(addr, val, now, exception, |
6408 | vcpu, ops); | |
6409 | ||
bbd9b64e CO |
6410 | if (rc != X86EMUL_CONTINUE) |
6411 | return rc; | |
6412 | addr += now; | |
bac15531 NA |
6413 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
6414 | addr = (u32)addr; | |
bbd9b64e CO |
6415 | val += now; |
6416 | bytes -= now; | |
6417 | } | |
22388a3c | 6418 | |
f78146b0 AK |
6419 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
6420 | vcpu, ops); | |
6421 | if (rc != X86EMUL_CONTINUE) | |
6422 | return rc; | |
6423 | ||
6424 | if (!vcpu->mmio_nr_fragments) | |
6425 | return rc; | |
6426 | ||
6427 | gpa = vcpu->mmio_fragments[0].gpa; | |
6428 | ||
6429 | vcpu->mmio_needed = 1; | |
6430 | vcpu->mmio_cur_fragment = 0; | |
6431 | ||
87da7e66 | 6432 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
f78146b0 AK |
6433 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
6434 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
6435 | vcpu->run->mmio.phys_addr = gpa; | |
6436 | ||
6437 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); | |
22388a3c XG |
6438 | } |
6439 | ||
6440 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, | |
6441 | unsigned long addr, | |
6442 | void *val, | |
6443 | unsigned int bytes, | |
6444 | struct x86_exception *exception) | |
6445 | { | |
6446 | return emulator_read_write(ctxt, addr, val, bytes, | |
6447 | exception, &read_emultor); | |
6448 | } | |
6449 | ||
52eb5a6d | 6450 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
22388a3c XG |
6451 | unsigned long addr, |
6452 | const void *val, | |
6453 | unsigned int bytes, | |
6454 | struct x86_exception *exception) | |
6455 | { | |
6456 | return emulator_read_write(ctxt, addr, (void *)val, bytes, | |
6457 | exception, &write_emultor); | |
bbd9b64e | 6458 | } |
bbd9b64e | 6459 | |
daea3e73 AK |
6460 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
6461 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) | |
6462 | ||
6463 | #ifdef CONFIG_X86_64 | |
6464 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) | |
6465 | #else | |
6466 | # define CMPXCHG64(ptr, old, new) \ | |
9749a6c0 | 6467 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
daea3e73 AK |
6468 | #endif |
6469 | ||
0f65dd70 AK |
6470 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
6471 | unsigned long addr, | |
bbd9b64e CO |
6472 | const void *old, |
6473 | const void *new, | |
6474 | unsigned int bytes, | |
0f65dd70 | 6475 | struct x86_exception *exception) |
bbd9b64e | 6476 | { |
42e35f80 | 6477 | struct kvm_host_map map; |
0f65dd70 | 6478 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
9de6fe3c | 6479 | u64 page_line_mask; |
daea3e73 | 6480 | gpa_t gpa; |
daea3e73 AK |
6481 | char *kaddr; |
6482 | bool exchanged; | |
2bacc55c | 6483 | |
daea3e73 AK |
6484 | /* guests cmpxchg8b have to be emulated atomically */ |
6485 | if (bytes > 8 || (bytes & (bytes - 1))) | |
6486 | goto emul_write; | |
10589a46 | 6487 | |
daea3e73 | 6488 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
2bacc55c | 6489 | |
daea3e73 AK |
6490 | if (gpa == UNMAPPED_GVA || |
6491 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
6492 | goto emul_write; | |
2bacc55c | 6493 | |
9de6fe3c XL |
6494 | /* |
6495 | * Emulate the atomic as a straight write to avoid #AC if SLD is | |
6496 | * enabled in the host and the access splits a cache line. | |
6497 | */ | |
6498 | if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) | |
6499 | page_line_mask = ~(cache_line_size() - 1); | |
6500 | else | |
6501 | page_line_mask = PAGE_MASK; | |
6502 | ||
6503 | if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) | |
daea3e73 | 6504 | goto emul_write; |
72dc67a6 | 6505 | |
42e35f80 | 6506 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
c19b8bd6 | 6507 | goto emul_write; |
72dc67a6 | 6508 | |
42e35f80 KA |
6509 | kaddr = map.hva + offset_in_page(gpa); |
6510 | ||
daea3e73 AK |
6511 | switch (bytes) { |
6512 | case 1: | |
6513 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); | |
6514 | break; | |
6515 | case 2: | |
6516 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); | |
6517 | break; | |
6518 | case 4: | |
6519 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); | |
6520 | break; | |
6521 | case 8: | |
6522 | exchanged = CMPXCHG64(kaddr, old, new); | |
6523 | break; | |
6524 | default: | |
6525 | BUG(); | |
2bacc55c | 6526 | } |
42e35f80 KA |
6527 | |
6528 | kvm_vcpu_unmap(vcpu, &map, true); | |
daea3e73 AK |
6529 | |
6530 | if (!exchanged) | |
6531 | return X86EMUL_CMPXCHG_FAILED; | |
6532 | ||
0eb05bf2 | 6533 | kvm_page_track_write(vcpu, gpa, new, bytes); |
8f6abd06 GN |
6534 | |
6535 | return X86EMUL_CONTINUE; | |
4a5f48f6 | 6536 | |
3200f405 | 6537 | emul_write: |
daea3e73 | 6538 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
2bacc55c | 6539 | |
0f65dd70 | 6540 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
bbd9b64e CO |
6541 | } |
6542 | ||
cf8f70bf GN |
6543 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
6544 | { | |
cbfc6c91 | 6545 | int r = 0, i; |
cf8f70bf | 6546 | |
cbfc6c91 WL |
6547 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
6548 | if (vcpu->arch.pio.in) | |
6549 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, | |
6550 | vcpu->arch.pio.size, pd); | |
6551 | else | |
6552 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, | |
6553 | vcpu->arch.pio.port, vcpu->arch.pio.size, | |
6554 | pd); | |
6555 | if (r) | |
6556 | break; | |
6557 | pd += vcpu->arch.pio.size; | |
6558 | } | |
cf8f70bf GN |
6559 | return r; |
6560 | } | |
6561 | ||
6f6fbe98 XG |
6562 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
6563 | unsigned short port, void *val, | |
6564 | unsigned int count, bool in) | |
cf8f70bf | 6565 | { |
cf8f70bf | 6566 | vcpu->arch.pio.port = port; |
6f6fbe98 | 6567 | vcpu->arch.pio.in = in; |
7972995b | 6568 | vcpu->arch.pio.count = count; |
cf8f70bf GN |
6569 | vcpu->arch.pio.size = size; |
6570 | ||
6571 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { | |
7972995b | 6572 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6573 | return 1; |
6574 | } | |
6575 | ||
6576 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
6f6fbe98 | 6577 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
cf8f70bf GN |
6578 | vcpu->run->io.size = size; |
6579 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
6580 | vcpu->run->io.count = count; | |
6581 | vcpu->run->io.port = port; | |
6582 | ||
6583 | return 0; | |
6584 | } | |
6585 | ||
2e3bb4d8 SC |
6586 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
6587 | unsigned short port, void *val, unsigned int count) | |
cf8f70bf | 6588 | { |
6f6fbe98 | 6589 | int ret; |
ca1d4a9e | 6590 | |
6f6fbe98 XG |
6591 | if (vcpu->arch.pio.count) |
6592 | goto data_avail; | |
cf8f70bf | 6593 | |
cbfc6c91 WL |
6594 | memset(vcpu->arch.pio_data, 0, size * count); |
6595 | ||
6f6fbe98 XG |
6596 | ret = emulator_pio_in_out(vcpu, size, port, val, count, true); |
6597 | if (ret) { | |
6598 | data_avail: | |
6599 | memcpy(val, vcpu->arch.pio_data, size * count); | |
1171903d | 6600 | trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data); |
7972995b | 6601 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6602 | return 1; |
6603 | } | |
6604 | ||
cf8f70bf GN |
6605 | return 0; |
6606 | } | |
6607 | ||
2e3bb4d8 SC |
6608 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
6609 | int size, unsigned short port, void *val, | |
6610 | unsigned int count) | |
6f6fbe98 | 6611 | { |
2e3bb4d8 | 6612 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
6f6fbe98 | 6613 | |
2e3bb4d8 | 6614 | } |
6f6fbe98 | 6615 | |
2e3bb4d8 SC |
6616 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
6617 | unsigned short port, const void *val, | |
6618 | unsigned int count) | |
6619 | { | |
6f6fbe98 | 6620 | memcpy(vcpu->arch.pio_data, val, size * count); |
1171903d | 6621 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
6f6fbe98 XG |
6622 | return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); |
6623 | } | |
6624 | ||
2e3bb4d8 SC |
6625 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
6626 | int size, unsigned short port, | |
6627 | const void *val, unsigned int count) | |
6628 | { | |
6629 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); | |
6630 | } | |
6631 | ||
bbd9b64e CO |
6632 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
6633 | { | |
b3646477 | 6634 | return static_call(kvm_x86_get_segment_base)(vcpu, seg); |
bbd9b64e CO |
6635 | } |
6636 | ||
3cb16fe7 | 6637 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
bbd9b64e | 6638 | { |
3cb16fe7 | 6639 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
bbd9b64e CO |
6640 | } |
6641 | ||
ae6a2375 | 6642 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
f5f48ee1 SY |
6643 | { |
6644 | if (!need_emulate_wbinvd(vcpu)) | |
6645 | return X86EMUL_CONTINUE; | |
6646 | ||
b3646477 | 6647 | if (static_call(kvm_x86_has_wbinvd_exit)()) { |
2eec7343 JK |
6648 | int cpu = get_cpu(); |
6649 | ||
6650 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); | |
f5f48ee1 SY |
6651 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
6652 | wbinvd_ipi, NULL, 1); | |
2eec7343 | 6653 | put_cpu(); |
f5f48ee1 | 6654 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
2eec7343 JK |
6655 | } else |
6656 | wbinvd(); | |
f5f48ee1 SY |
6657 | return X86EMUL_CONTINUE; |
6658 | } | |
5cb56059 JS |
6659 | |
6660 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
6661 | { | |
6affcbed KH |
6662 | kvm_emulate_wbinvd_noskip(vcpu); |
6663 | return kvm_skip_emulated_instruction(vcpu); | |
5cb56059 | 6664 | } |
f5f48ee1 SY |
6665 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
6666 | ||
5cb56059 JS |
6667 | |
6668 | ||
bcaf5cc5 AK |
6669 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
6670 | { | |
5cb56059 | 6671 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
bcaf5cc5 AK |
6672 | } |
6673 | ||
29d6ca41 PB |
6674 | static void emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6675 | unsigned long *dest) | |
bbd9b64e | 6676 | { |
29d6ca41 | 6677 | kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
bbd9b64e CO |
6678 | } |
6679 | ||
52eb5a6d XL |
6680 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6681 | unsigned long value) | |
bbd9b64e | 6682 | { |
338dbc97 | 6683 | |
996ff542 | 6684 | return kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
bbd9b64e CO |
6685 | } |
6686 | ||
52a46617 | 6687 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
5fdbf976 | 6688 | { |
52a46617 | 6689 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
5fdbf976 MT |
6690 | } |
6691 | ||
717746e3 | 6692 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
bbd9b64e | 6693 | { |
717746e3 | 6694 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
52a46617 GN |
6695 | unsigned long value; |
6696 | ||
6697 | switch (cr) { | |
6698 | case 0: | |
6699 | value = kvm_read_cr0(vcpu); | |
6700 | break; | |
6701 | case 2: | |
6702 | value = vcpu->arch.cr2; | |
6703 | break; | |
6704 | case 3: | |
9f8fe504 | 6705 | value = kvm_read_cr3(vcpu); |
52a46617 GN |
6706 | break; |
6707 | case 4: | |
6708 | value = kvm_read_cr4(vcpu); | |
6709 | break; | |
6710 | case 8: | |
6711 | value = kvm_get_cr8(vcpu); | |
6712 | break; | |
6713 | default: | |
a737f256 | 6714 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
52a46617 GN |
6715 | return 0; |
6716 | } | |
6717 | ||
6718 | return value; | |
6719 | } | |
6720 | ||
717746e3 | 6721 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
52a46617 | 6722 | { |
717746e3 | 6723 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
0f12244f GN |
6724 | int res = 0; |
6725 | ||
52a46617 GN |
6726 | switch (cr) { |
6727 | case 0: | |
49a9b07e | 6728 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
52a46617 GN |
6729 | break; |
6730 | case 2: | |
6731 | vcpu->arch.cr2 = val; | |
6732 | break; | |
6733 | case 3: | |
2390218b | 6734 | res = kvm_set_cr3(vcpu, val); |
52a46617 GN |
6735 | break; |
6736 | case 4: | |
a83b29c6 | 6737 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
52a46617 GN |
6738 | break; |
6739 | case 8: | |
eea1cff9 | 6740 | res = kvm_set_cr8(vcpu, val); |
52a46617 GN |
6741 | break; |
6742 | default: | |
a737f256 | 6743 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
0f12244f | 6744 | res = -1; |
52a46617 | 6745 | } |
0f12244f GN |
6746 | |
6747 | return res; | |
52a46617 GN |
6748 | } |
6749 | ||
717746e3 | 6750 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
9c537244 | 6751 | { |
b3646477 | 6752 | return static_call(kvm_x86_get_cpl)(emul_to_vcpu(ctxt)); |
9c537244 GN |
6753 | } |
6754 | ||
4bff1e86 | 6755 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
2dafc6c2 | 6756 | { |
b3646477 | 6757 | static_call(kvm_x86_get_gdt)(emul_to_vcpu(ctxt), dt); |
2dafc6c2 GN |
6758 | } |
6759 | ||
4bff1e86 | 6760 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
160ce1f1 | 6761 | { |
b3646477 | 6762 | static_call(kvm_x86_get_idt)(emul_to_vcpu(ctxt), dt); |
160ce1f1 MG |
6763 | } |
6764 | ||
1ac9d0cf AK |
6765 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
6766 | { | |
b3646477 | 6767 | static_call(kvm_x86_set_gdt)(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6768 | } |
6769 | ||
6770 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) | |
6771 | { | |
b3646477 | 6772 | static_call(kvm_x86_set_idt)(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6773 | } |
6774 | ||
4bff1e86 AK |
6775 | static unsigned long emulator_get_cached_segment_base( |
6776 | struct x86_emulate_ctxt *ctxt, int seg) | |
5951c442 | 6777 | { |
4bff1e86 | 6778 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
5951c442 GN |
6779 | } |
6780 | ||
1aa36616 AK |
6781 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
6782 | struct desc_struct *desc, u32 *base3, | |
6783 | int seg) | |
2dafc6c2 GN |
6784 | { |
6785 | struct kvm_segment var; | |
6786 | ||
4bff1e86 | 6787 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
1aa36616 | 6788 | *selector = var.selector; |
2dafc6c2 | 6789 | |
378a8b09 GN |
6790 | if (var.unusable) { |
6791 | memset(desc, 0, sizeof(*desc)); | |
f0367ee1 RK |
6792 | if (base3) |
6793 | *base3 = 0; | |
2dafc6c2 | 6794 | return false; |
378a8b09 | 6795 | } |
2dafc6c2 GN |
6796 | |
6797 | if (var.g) | |
6798 | var.limit >>= 12; | |
6799 | set_desc_limit(desc, var.limit); | |
6800 | set_desc_base(desc, (unsigned long)var.base); | |
5601d05b GN |
6801 | #ifdef CONFIG_X86_64 |
6802 | if (base3) | |
6803 | *base3 = var.base >> 32; | |
6804 | #endif | |
2dafc6c2 GN |
6805 | desc->type = var.type; |
6806 | desc->s = var.s; | |
6807 | desc->dpl = var.dpl; | |
6808 | desc->p = var.present; | |
6809 | desc->avl = var.avl; | |
6810 | desc->l = var.l; | |
6811 | desc->d = var.db; | |
6812 | desc->g = var.g; | |
6813 | ||
6814 | return true; | |
6815 | } | |
6816 | ||
1aa36616 AK |
6817 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
6818 | struct desc_struct *desc, u32 base3, | |
6819 | int seg) | |
2dafc6c2 | 6820 | { |
4bff1e86 | 6821 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
2dafc6c2 GN |
6822 | struct kvm_segment var; |
6823 | ||
1aa36616 | 6824 | var.selector = selector; |
2dafc6c2 | 6825 | var.base = get_desc_base(desc); |
5601d05b GN |
6826 | #ifdef CONFIG_X86_64 |
6827 | var.base |= ((u64)base3) << 32; | |
6828 | #endif | |
2dafc6c2 GN |
6829 | var.limit = get_desc_limit(desc); |
6830 | if (desc->g) | |
6831 | var.limit = (var.limit << 12) | 0xfff; | |
6832 | var.type = desc->type; | |
2dafc6c2 GN |
6833 | var.dpl = desc->dpl; |
6834 | var.db = desc->d; | |
6835 | var.s = desc->s; | |
6836 | var.l = desc->l; | |
6837 | var.g = desc->g; | |
6838 | var.avl = desc->avl; | |
6839 | var.present = desc->p; | |
6840 | var.unusable = !var.present; | |
6841 | var.padding = 0; | |
6842 | ||
6843 | kvm_set_segment(vcpu, &var, seg); | |
6844 | return; | |
6845 | } | |
6846 | ||
717746e3 AK |
6847 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
6848 | u32 msr_index, u64 *pdata) | |
6849 | { | |
1ae09954 AG |
6850 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
6851 | int r; | |
6852 | ||
6853 | r = kvm_get_msr(vcpu, msr_index, pdata); | |
6854 | ||
6855 | if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) { | |
6856 | /* Bounce to user space */ | |
6857 | return X86EMUL_IO_NEEDED; | |
6858 | } | |
6859 | ||
6860 | return r; | |
717746e3 AK |
6861 | } |
6862 | ||
6863 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, | |
6864 | u32 msr_index, u64 data) | |
6865 | { | |
1ae09954 AG |
6866 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
6867 | int r; | |
6868 | ||
6869 | r = kvm_set_msr(vcpu, msr_index, data); | |
6870 | ||
6871 | if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) { | |
6872 | /* Bounce to user space */ | |
6873 | return X86EMUL_IO_NEEDED; | |
6874 | } | |
6875 | ||
6876 | return r; | |
717746e3 AK |
6877 | } |
6878 | ||
64d60670 PB |
6879 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
6880 | { | |
6881 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6882 | ||
6883 | return vcpu->arch.smbase; | |
6884 | } | |
6885 | ||
6886 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) | |
6887 | { | |
6888 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6889 | ||
6890 | vcpu->arch.smbase = smbase; | |
6891 | } | |
6892 | ||
67f4d428 NA |
6893 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
6894 | u32 pmc) | |
6895 | { | |
98ff80f5 | 6896 | return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc); |
67f4d428 NA |
6897 | } |
6898 | ||
222d21aa AK |
6899 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
6900 | u32 pmc, u64 *pdata) | |
6901 | { | |
c6702c9d | 6902 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
222d21aa AK |
6903 | } |
6904 | ||
6c3287f7 AK |
6905 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
6906 | { | |
6907 | emul_to_vcpu(ctxt)->arch.halt_request = 1; | |
6908 | } | |
6909 | ||
2953538e | 6910 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
8a76d7f2 | 6911 | struct x86_instruction_info *info, |
c4f035c6 AK |
6912 | enum x86_intercept_stage stage) |
6913 | { | |
b3646477 | 6914 | return static_call(kvm_x86_check_intercept)(emul_to_vcpu(ctxt), info, stage, |
21f1b8f2 | 6915 | &ctxt->exception); |
c4f035c6 AK |
6916 | } |
6917 | ||
e911eb3b | 6918 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
f91af517 SC |
6919 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
6920 | bool exact_only) | |
bdb42f5a | 6921 | { |
f91af517 | 6922 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
bdb42f5a SB |
6923 | } |
6924 | ||
5ae78e95 SC |
6925 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
6926 | { | |
6927 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); | |
6928 | } | |
6929 | ||
6930 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) | |
6931 | { | |
6932 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); | |
6933 | } | |
6934 | ||
6935 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) | |
6936 | { | |
6937 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); | |
6938 | } | |
6939 | ||
dd856efa AK |
6940 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
6941 | { | |
6942 | return kvm_register_read(emul_to_vcpu(ctxt), reg); | |
6943 | } | |
6944 | ||
6945 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) | |
6946 | { | |
6947 | kvm_register_write(emul_to_vcpu(ctxt), reg, val); | |
6948 | } | |
6949 | ||
801806d9 NA |
6950 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
6951 | { | |
b3646477 | 6952 | static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked); |
801806d9 NA |
6953 | } |
6954 | ||
6ed071f0 LP |
6955 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
6956 | { | |
6957 | return emul_to_vcpu(ctxt)->arch.hflags; | |
6958 | } | |
6959 | ||
6960 | static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) | |
6961 | { | |
c5833c7a | 6962 | emul_to_vcpu(ctxt)->arch.hflags = emul_flags; |
6ed071f0 LP |
6963 | } |
6964 | ||
ed19321f SC |
6965 | static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, |
6966 | const char *smstate) | |
0234bf88 | 6967 | { |
b3646477 | 6968 | return static_call(kvm_x86_pre_leave_smm)(emul_to_vcpu(ctxt), smstate); |
0234bf88 LP |
6969 | } |
6970 | ||
c5833c7a SC |
6971 | static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) |
6972 | { | |
6973 | kvm_smm_changed(emul_to_vcpu(ctxt)); | |
6974 | } | |
6975 | ||
02d4160f VK |
6976 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
6977 | { | |
6978 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); | |
6979 | } | |
6980 | ||
0225fb50 | 6981 | static const struct x86_emulate_ops emulate_ops = { |
dd856efa AK |
6982 | .read_gpr = emulator_read_gpr, |
6983 | .write_gpr = emulator_write_gpr, | |
ce14e868 PB |
6984 | .read_std = emulator_read_std, |
6985 | .write_std = emulator_write_std, | |
7a036a6f | 6986 | .read_phys = kvm_read_guest_phys_system, |
1871c602 | 6987 | .fetch = kvm_fetch_guest_virt, |
bbd9b64e CO |
6988 | .read_emulated = emulator_read_emulated, |
6989 | .write_emulated = emulator_write_emulated, | |
6990 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
3cb16fe7 | 6991 | .invlpg = emulator_invlpg, |
cf8f70bf GN |
6992 | .pio_in_emulated = emulator_pio_in_emulated, |
6993 | .pio_out_emulated = emulator_pio_out_emulated, | |
1aa36616 AK |
6994 | .get_segment = emulator_get_segment, |
6995 | .set_segment = emulator_set_segment, | |
5951c442 | 6996 | .get_cached_segment_base = emulator_get_cached_segment_base, |
2dafc6c2 | 6997 | .get_gdt = emulator_get_gdt, |
160ce1f1 | 6998 | .get_idt = emulator_get_idt, |
1ac9d0cf AK |
6999 | .set_gdt = emulator_set_gdt, |
7000 | .set_idt = emulator_set_idt, | |
52a46617 GN |
7001 | .get_cr = emulator_get_cr, |
7002 | .set_cr = emulator_set_cr, | |
9c537244 | 7003 | .cpl = emulator_get_cpl, |
35aa5375 GN |
7004 | .get_dr = emulator_get_dr, |
7005 | .set_dr = emulator_set_dr, | |
64d60670 PB |
7006 | .get_smbase = emulator_get_smbase, |
7007 | .set_smbase = emulator_set_smbase, | |
717746e3 AK |
7008 | .set_msr = emulator_set_msr, |
7009 | .get_msr = emulator_get_msr, | |
67f4d428 | 7010 | .check_pmc = emulator_check_pmc, |
222d21aa | 7011 | .read_pmc = emulator_read_pmc, |
6c3287f7 | 7012 | .halt = emulator_halt, |
bcaf5cc5 | 7013 | .wbinvd = emulator_wbinvd, |
d6aa1000 | 7014 | .fix_hypercall = emulator_fix_hypercall, |
c4f035c6 | 7015 | .intercept = emulator_intercept, |
bdb42f5a | 7016 | .get_cpuid = emulator_get_cpuid, |
5ae78e95 SC |
7017 | .guest_has_long_mode = emulator_guest_has_long_mode, |
7018 | .guest_has_movbe = emulator_guest_has_movbe, | |
7019 | .guest_has_fxsr = emulator_guest_has_fxsr, | |
801806d9 | 7020 | .set_nmi_mask = emulator_set_nmi_mask, |
6ed071f0 LP |
7021 | .get_hflags = emulator_get_hflags, |
7022 | .set_hflags = emulator_set_hflags, | |
0234bf88 | 7023 | .pre_leave_smm = emulator_pre_leave_smm, |
c5833c7a | 7024 | .post_leave_smm = emulator_post_leave_smm, |
02d4160f | 7025 | .set_xcr = emulator_set_xcr, |
bbd9b64e CO |
7026 | }; |
7027 | ||
95cb2295 GN |
7028 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
7029 | { | |
b3646477 | 7030 | u32 int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); |
95cb2295 GN |
7031 | /* |
7032 | * an sti; sti; sequence only disable interrupts for the first | |
7033 | * instruction. So, if the last instruction, be it emulated or | |
7034 | * not, left the system with the INT_STI flag enabled, it | |
7035 | * means that the last instruction is an sti. We should not | |
7036 | * leave the flag on in this case. The same goes for mov ss | |
7037 | */ | |
37ccdcbe PB |
7038 | if (int_shadow & mask) |
7039 | mask = 0; | |
6addfc42 | 7040 | if (unlikely(int_shadow || mask)) { |
b3646477 | 7041 | static_call(kvm_x86_set_interrupt_shadow)(vcpu, mask); |
6addfc42 PB |
7042 | if (!mask) |
7043 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7044 | } | |
95cb2295 GN |
7045 | } |
7046 | ||
ef54bcfe | 7047 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
54b8486f | 7048 | { |
c9b8b07c | 7049 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
da9cb575 | 7050 | if (ctxt->exception.vector == PF_VECTOR) |
53b3d8e9 | 7051 | return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); |
ef54bcfe PB |
7052 | |
7053 | if (ctxt->exception.error_code_valid) | |
da9cb575 AK |
7054 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
7055 | ctxt->exception.error_code); | |
54b8486f | 7056 | else |
da9cb575 | 7057 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
ef54bcfe | 7058 | return false; |
54b8486f GN |
7059 | } |
7060 | ||
c9b8b07c SC |
7061 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
7062 | { | |
7063 | struct x86_emulate_ctxt *ctxt; | |
7064 | ||
7065 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); | |
7066 | if (!ctxt) { | |
7067 | pr_err("kvm: failed to allocate vcpu's emulator\n"); | |
7068 | return NULL; | |
7069 | } | |
7070 | ||
7071 | ctxt->vcpu = vcpu; | |
7072 | ctxt->ops = &emulate_ops; | |
7073 | vcpu->arch.emulate_ctxt = ctxt; | |
7074 | ||
7075 | return ctxt; | |
7076 | } | |
7077 | ||
8ec4722d MG |
7078 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
7079 | { | |
c9b8b07c | 7080 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d MG |
7081 | int cs_db, cs_l; |
7082 | ||
b3646477 | 7083 | static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l); |
8ec4722d | 7084 | |
744e699c | 7085 | ctxt->gpa_available = false; |
adf52235 | 7086 | ctxt->eflags = kvm_get_rflags(vcpu); |
c8401dda PB |
7087 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
7088 | ||
adf52235 TY |
7089 | ctxt->eip = kvm_rip_read(vcpu); |
7090 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : | |
7091 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : | |
42bf549f | 7092 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
adf52235 TY |
7093 | cs_db ? X86EMUL_MODE_PROT32 : |
7094 | X86EMUL_MODE_PROT16; | |
a584539b | 7095 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
64d60670 PB |
7096 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
7097 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); | |
adf52235 | 7098 | |
dd856efa | 7099 | init_decode_cache(ctxt); |
7ae441ea | 7100 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
8ec4722d MG |
7101 | } |
7102 | ||
9497e1f2 | 7103 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
63995653 | 7104 | { |
c9b8b07c | 7105 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
63995653 MG |
7106 | int ret; |
7107 | ||
7108 | init_emulate_ctxt(vcpu); | |
7109 | ||
9dac77fa AK |
7110 | ctxt->op_bytes = 2; |
7111 | ctxt->ad_bytes = 2; | |
7112 | ctxt->_eip = ctxt->eip + inc_eip; | |
9d74191a | 7113 | ret = emulate_int_real(ctxt, irq); |
63995653 | 7114 | |
9497e1f2 SC |
7115 | if (ret != X86EMUL_CONTINUE) { |
7116 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
7117 | } else { | |
7118 | ctxt->eip = ctxt->_eip; | |
7119 | kvm_rip_write(vcpu, ctxt->eip); | |
7120 | kvm_set_rflags(vcpu, ctxt->eflags); | |
7121 | } | |
63995653 MG |
7122 | } |
7123 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); | |
7124 | ||
e2366171 | 7125 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
6d77dbfc | 7126 | { |
6d77dbfc GN |
7127 | ++vcpu->stat.insn_emulation_fail; |
7128 | trace_kvm_emulate_insn_failed(vcpu); | |
e2366171 | 7129 | |
42cbf068 SC |
7130 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
7131 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 7132 | return 1; |
42cbf068 | 7133 | } |
e2366171 | 7134 | |
738fece4 SC |
7135 | if (emulation_type & EMULTYPE_SKIP) { |
7136 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
7137 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
7138 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 7139 | return 0; |
738fece4 SC |
7140 | } |
7141 | ||
22da61c9 SC |
7142 | kvm_queue_exception(vcpu, UD_VECTOR); |
7143 | ||
b3646477 | 7144 | if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) { |
fc3a9157 JR |
7145 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
7146 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
7147 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 7148 | return 0; |
fc3a9157 | 7149 | } |
e2366171 | 7150 | |
60fc3d02 | 7151 | return 1; |
6d77dbfc GN |
7152 | } |
7153 | ||
736c291c | 7154 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
991eebf9 GN |
7155 | bool write_fault_to_shadow_pgtable, |
7156 | int emulation_type) | |
a6f177ef | 7157 | { |
736c291c | 7158 | gpa_t gpa = cr2_or_gpa; |
ba049e93 | 7159 | kvm_pfn_t pfn; |
a6f177ef | 7160 | |
92daa48b | 7161 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
991eebf9 GN |
7162 | return false; |
7163 | ||
92daa48b SC |
7164 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
7165 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
7166 | return false; |
7167 | ||
44dd3ffa | 7168 | if (!vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
7169 | /* |
7170 | * Write permission should be allowed since only | |
7171 | * write access need to be emulated. | |
7172 | */ | |
736c291c | 7173 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
a6f177ef | 7174 | |
95b3cf69 XG |
7175 | /* |
7176 | * If the mapping is invalid in guest, let cpu retry | |
7177 | * it to generate fault. | |
7178 | */ | |
7179 | if (gpa == UNMAPPED_GVA) | |
7180 | return true; | |
7181 | } | |
a6f177ef | 7182 | |
8e3d9d06 XG |
7183 | /* |
7184 | * Do not retry the unhandleable instruction if it faults on the | |
7185 | * readonly host memory, otherwise it will goto a infinite loop: | |
7186 | * retry instruction -> write #PF -> emulation fail -> retry | |
7187 | * instruction -> ... | |
7188 | */ | |
7189 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); | |
95b3cf69 XG |
7190 | |
7191 | /* | |
7192 | * If the instruction failed on the error pfn, it can not be fixed, | |
7193 | * report the error to userspace. | |
7194 | */ | |
7195 | if (is_error_noslot_pfn(pfn)) | |
7196 | return false; | |
7197 | ||
7198 | kvm_release_pfn_clean(pfn); | |
7199 | ||
7200 | /* The instructions are well-emulated on direct mmu. */ | |
44dd3ffa | 7201 | if (vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
7202 | unsigned int indirect_shadow_pages; |
7203 | ||
531810ca | 7204 | write_lock(&vcpu->kvm->mmu_lock); |
95b3cf69 | 7205 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; |
531810ca | 7206 | write_unlock(&vcpu->kvm->mmu_lock); |
95b3cf69 XG |
7207 | |
7208 | if (indirect_shadow_pages) | |
7209 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
7210 | ||
a6f177ef | 7211 | return true; |
8e3d9d06 | 7212 | } |
a6f177ef | 7213 | |
95b3cf69 XG |
7214 | /* |
7215 | * if emulation was due to access to shadowed page table | |
7216 | * and it failed try to unshadow page and re-enter the | |
7217 | * guest to let CPU execute the instruction. | |
7218 | */ | |
7219 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
93c05d3e XG |
7220 | |
7221 | /* | |
7222 | * If the access faults on its page table, it can not | |
7223 | * be fixed by unprotecting shadow page and it should | |
7224 | * be reported to userspace. | |
7225 | */ | |
7226 | return !write_fault_to_shadow_pgtable; | |
a6f177ef GN |
7227 | } |
7228 | ||
1cb3f3ae | 7229 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
736c291c | 7230 | gpa_t cr2_or_gpa, int emulation_type) |
1cb3f3ae XG |
7231 | { |
7232 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
736c291c | 7233 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
1cb3f3ae XG |
7234 | |
7235 | last_retry_eip = vcpu->arch.last_retry_eip; | |
7236 | last_retry_addr = vcpu->arch.last_retry_addr; | |
7237 | ||
7238 | /* | |
7239 | * If the emulation is caused by #PF and it is non-page_table | |
7240 | * writing instruction, it means the VM-EXIT is caused by shadow | |
7241 | * page protected, we can zap the shadow page and retry this | |
7242 | * instruction directly. | |
7243 | * | |
7244 | * Note: if the guest uses a non-page-table modifying instruction | |
7245 | * on the PDE that points to the instruction, then we will unmap | |
7246 | * the instruction and go to an infinite loop. So, we cache the | |
7247 | * last retried eip and the last fault address, if we meet the eip | |
7248 | * and the address again, we can break out of the potential infinite | |
7249 | * loop. | |
7250 | */ | |
7251 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; | |
7252 | ||
92daa48b | 7253 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
1cb3f3ae XG |
7254 | return false; |
7255 | ||
92daa48b SC |
7256 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
7257 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
7258 | return false; |
7259 | ||
1cb3f3ae XG |
7260 | if (x86_page_table_writing_insn(ctxt)) |
7261 | return false; | |
7262 | ||
736c291c | 7263 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
1cb3f3ae XG |
7264 | return false; |
7265 | ||
7266 | vcpu->arch.last_retry_eip = ctxt->eip; | |
736c291c | 7267 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
1cb3f3ae | 7268 | |
44dd3ffa | 7269 | if (!vcpu->arch.mmu->direct_map) |
736c291c | 7270 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
1cb3f3ae | 7271 | |
22368028 | 7272 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
1cb3f3ae XG |
7273 | |
7274 | return true; | |
7275 | } | |
7276 | ||
716d51ab GN |
7277 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
7278 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); | |
7279 | ||
64d60670 | 7280 | static void kvm_smm_changed(struct kvm_vcpu *vcpu) |
a584539b | 7281 | { |
64d60670 | 7282 | if (!(vcpu->arch.hflags & HF_SMM_MASK)) { |
660a5d51 PB |
7283 | /* This is a good place to trace that we are exiting SMM. */ |
7284 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false); | |
7285 | ||
c43203ca PB |
7286 | /* Process a latched INIT or SMI, if any. */ |
7287 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
64d60670 | 7288 | } |
699023e2 PB |
7289 | |
7290 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
7291 | } |
7292 | ||
4a1e10d5 PB |
7293 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
7294 | unsigned long *db) | |
7295 | { | |
7296 | u32 dr6 = 0; | |
7297 | int i; | |
7298 | u32 enable, rwlen; | |
7299 | ||
7300 | enable = dr7; | |
7301 | rwlen = dr7 >> 16; | |
7302 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) | |
7303 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) | |
7304 | dr6 |= (1 << i); | |
7305 | return dr6; | |
7306 | } | |
7307 | ||
120c2c4f | 7308 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
663f4c61 PB |
7309 | { |
7310 | struct kvm_run *kvm_run = vcpu->run; | |
7311 | ||
c8401dda | 7312 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
9a3ecd5e | 7313 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; |
d5d260c5 | 7314 | kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
c8401dda PB |
7315 | kvm_run->debug.arch.exception = DB_VECTOR; |
7316 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 7317 | return 0; |
663f4c61 | 7318 | } |
120c2c4f | 7319 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
60fc3d02 | 7320 | return 1; |
663f4c61 PB |
7321 | } |
7322 | ||
6affcbed KH |
7323 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
7324 | { | |
b3646477 | 7325 | unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); |
f8ea7c60 | 7326 | int r; |
6affcbed | 7327 | |
b3646477 | 7328 | r = static_call(kvm_x86_skip_emulated_instruction)(vcpu); |
60fc3d02 | 7329 | if (unlikely(!r)) |
f8ea7c60 | 7330 | return 0; |
c8401dda PB |
7331 | |
7332 | /* | |
7333 | * rflags is the old, "raw" value of the flags. The new value has | |
7334 | * not been saved yet. | |
7335 | * | |
7336 | * This is correct even for TF set by the guest, because "the | |
7337 | * processor will not generate this exception after the instruction | |
7338 | * that sets the TF flag". | |
7339 | */ | |
7340 | if (unlikely(rflags & X86_EFLAGS_TF)) | |
120c2c4f | 7341 | r = kvm_vcpu_do_singlestep(vcpu); |
60fc3d02 | 7342 | return r; |
6affcbed KH |
7343 | } |
7344 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); | |
7345 | ||
4a1e10d5 PB |
7346 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
7347 | { | |
4a1e10d5 PB |
7348 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
7349 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { | |
82b32774 NA |
7350 | struct kvm_run *kvm_run = vcpu->run; |
7351 | unsigned long eip = kvm_get_linear_rip(vcpu); | |
7352 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
7353 | vcpu->arch.guest_debug_dr7, |
7354 | vcpu->arch.eff_db); | |
7355 | ||
7356 | if (dr6 != 0) { | |
9a3ecd5e | 7357 | kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; |
82b32774 | 7358 | kvm_run->debug.arch.pc = eip; |
4a1e10d5 PB |
7359 | kvm_run->debug.arch.exception = DB_VECTOR; |
7360 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 7361 | *r = 0; |
4a1e10d5 PB |
7362 | return true; |
7363 | } | |
7364 | } | |
7365 | ||
4161a569 NA |
7366 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
7367 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { | |
82b32774 NA |
7368 | unsigned long eip = kvm_get_linear_rip(vcpu); |
7369 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
7370 | vcpu->arch.dr7, |
7371 | vcpu->arch.db); | |
7372 | ||
7373 | if (dr6 != 0) { | |
4d5523cf | 7374 | kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); |
60fc3d02 | 7375 | *r = 1; |
4a1e10d5 PB |
7376 | return true; |
7377 | } | |
7378 | } | |
7379 | ||
7380 | return false; | |
7381 | } | |
7382 | ||
04789b66 LA |
7383 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
7384 | { | |
2d7921c4 AM |
7385 | switch (ctxt->opcode_len) { |
7386 | case 1: | |
7387 | switch (ctxt->b) { | |
7388 | case 0xe4: /* IN */ | |
7389 | case 0xe5: | |
7390 | case 0xec: | |
7391 | case 0xed: | |
7392 | case 0xe6: /* OUT */ | |
7393 | case 0xe7: | |
7394 | case 0xee: | |
7395 | case 0xef: | |
7396 | case 0x6c: /* INS */ | |
7397 | case 0x6d: | |
7398 | case 0x6e: /* OUTS */ | |
7399 | case 0x6f: | |
7400 | return true; | |
7401 | } | |
7402 | break; | |
7403 | case 2: | |
7404 | switch (ctxt->b) { | |
7405 | case 0x33: /* RDPMC */ | |
7406 | return true; | |
7407 | } | |
7408 | break; | |
04789b66 LA |
7409 | } |
7410 | ||
7411 | return false; | |
7412 | } | |
7413 | ||
4aa2691d WH |
7414 | /* |
7415 | * Decode to be emulated instruction. Return EMULATION_OK if success. | |
7416 | */ | |
7417 | int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, | |
7418 | void *insn, int insn_len) | |
7419 | { | |
7420 | int r = EMULATION_OK; | |
7421 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; | |
7422 | ||
7423 | init_emulate_ctxt(vcpu); | |
7424 | ||
7425 | /* | |
7426 | * We will reenter on the same instruction since we do not set | |
7427 | * complete_userspace_io. This does not handle watchpoints yet, | |
7428 | * those would be handled in the emulate_ops. | |
7429 | */ | |
7430 | if (!(emulation_type & EMULTYPE_SKIP) && | |
7431 | kvm_vcpu_check_breakpoint(vcpu, &r)) | |
7432 | return r; | |
7433 | ||
7434 | ctxt->interruptibility = 0; | |
7435 | ctxt->have_exception = false; | |
7436 | ctxt->exception.vector = -1; | |
7437 | ctxt->perm_ok = false; | |
7438 | ||
7439 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; | |
7440 | ||
7441 | r = x86_decode_insn(ctxt, insn, insn_len); | |
7442 | ||
7443 | trace_kvm_emulate_insn_start(vcpu); | |
7444 | ++vcpu->stat.insn_emulation; | |
7445 | ||
7446 | return r; | |
7447 | } | |
7448 | EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction); | |
7449 | ||
736c291c SC |
7450 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
7451 | int emulation_type, void *insn, int insn_len) | |
bbd9b64e | 7452 | { |
95cb2295 | 7453 | int r; |
c9b8b07c | 7454 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
7ae441ea | 7455 | bool writeback = true; |
09e3e2a1 SC |
7456 | bool write_fault_to_spt; |
7457 | ||
b3646477 | 7458 | if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, insn, insn_len))) |
09e3e2a1 | 7459 | return 1; |
bbd9b64e | 7460 | |
c595ceee PB |
7461 | vcpu->arch.l1tf_flush_l1d = true; |
7462 | ||
93c05d3e XG |
7463 | /* |
7464 | * Clear write_fault_to_shadow_pgtable here to ensure it is | |
7465 | * never reused. | |
7466 | */ | |
09e3e2a1 | 7467 | write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
93c05d3e | 7468 | vcpu->arch.write_fault_to_shadow_pgtable = false; |
8d7d8102 | 7469 | |
571008da | 7470 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
4aa2691d | 7471 | kvm_clear_exception_queue(vcpu); |
4a1e10d5 | 7472 | |
4aa2691d WH |
7473 | r = x86_decode_emulated_instruction(vcpu, emulation_type, |
7474 | insn, insn_len); | |
1d2887e2 | 7475 | if (r != EMULATION_OK) { |
b4000606 | 7476 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
c83fad65 SC |
7477 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
7478 | kvm_queue_exception(vcpu, UD_VECTOR); | |
60fc3d02 | 7479 | return 1; |
c83fad65 | 7480 | } |
736c291c SC |
7481 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
7482 | write_fault_to_spt, | |
7483 | emulation_type)) | |
60fc3d02 | 7484 | return 1; |
8530a79c | 7485 | if (ctxt->have_exception) { |
c8848cee JD |
7486 | /* |
7487 | * #UD should result in just EMULATION_FAILED, and trap-like | |
7488 | * exception should not be encountered during decode. | |
7489 | */ | |
7490 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || | |
7491 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); | |
8530a79c | 7492 | inject_emulated_exception(vcpu); |
60fc3d02 | 7493 | return 1; |
8530a79c | 7494 | } |
e2366171 | 7495 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
7496 | } |
7497 | } | |
7498 | ||
42cbf068 SC |
7499 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
7500 | !is_vmware_backdoor_opcode(ctxt)) { | |
7501 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 7502 | return 1; |
42cbf068 | 7503 | } |
04789b66 | 7504 | |
1957aa63 SC |
7505 | /* |
7506 | * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks | |
7507 | * for kvm_skip_emulated_instruction(). The caller is responsible for | |
7508 | * updating interruptibility state and injecting single-step #DBs. | |
7509 | */ | |
ba8afb6b | 7510 | if (emulation_type & EMULTYPE_SKIP) { |
9dac77fa | 7511 | kvm_rip_write(vcpu, ctxt->_eip); |
bb663c7a NA |
7512 | if (ctxt->eflags & X86_EFLAGS_RF) |
7513 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); | |
60fc3d02 | 7514 | return 1; |
ba8afb6b GN |
7515 | } |
7516 | ||
736c291c | 7517 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
60fc3d02 | 7518 | return 1; |
1cb3f3ae | 7519 | |
7ae441ea | 7520 | /* this is needed for vmware backdoor interface to work since it |
4d2179e1 | 7521 | changes registers values during IO operation */ |
7ae441ea GN |
7522 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
7523 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; | |
dd856efa | 7524 | emulator_invalidate_register_cache(ctxt); |
7ae441ea | 7525 | } |
4d2179e1 | 7526 | |
5cd21917 | 7527 | restart: |
92daa48b SC |
7528 | if (emulation_type & EMULTYPE_PF) { |
7529 | /* Save the faulting GPA (cr2) in the address field */ | |
7530 | ctxt->exception.address = cr2_or_gpa; | |
7531 | ||
7532 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ | |
7533 | if (vcpu->arch.mmu->direct_map) { | |
744e699c SC |
7534 | ctxt->gpa_available = true; |
7535 | ctxt->gpa_val = cr2_or_gpa; | |
92daa48b SC |
7536 | } |
7537 | } else { | |
7538 | /* Sanitize the address out of an abundance of paranoia. */ | |
7539 | ctxt->exception.address = 0; | |
7540 | } | |
0f89b207 | 7541 | |
9d74191a | 7542 | r = x86_emulate_insn(ctxt); |
bbd9b64e | 7543 | |
775fde86 | 7544 | if (r == EMULATION_INTERCEPTED) |
60fc3d02 | 7545 | return 1; |
775fde86 | 7546 | |
d2ddd1c4 | 7547 | if (r == EMULATION_FAILED) { |
736c291c | 7548 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
991eebf9 | 7549 | emulation_type)) |
60fc3d02 | 7550 | return 1; |
c3cd7ffa | 7551 | |
e2366171 | 7552 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
7553 | } |
7554 | ||
9d74191a | 7555 | if (ctxt->have_exception) { |
60fc3d02 | 7556 | r = 1; |
ef54bcfe PB |
7557 | if (inject_emulated_exception(vcpu)) |
7558 | return r; | |
d2ddd1c4 | 7559 | } else if (vcpu->arch.pio.count) { |
0912c977 PB |
7560 | if (!vcpu->arch.pio.in) { |
7561 | /* FIXME: return into emulator if single-stepping. */ | |
3457e419 | 7562 | vcpu->arch.pio.count = 0; |
0912c977 | 7563 | } else { |
7ae441ea | 7564 | writeback = false; |
716d51ab GN |
7565 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
7566 | } | |
60fc3d02 | 7567 | r = 0; |
7ae441ea | 7568 | } else if (vcpu->mmio_needed) { |
bc8a0aaf SC |
7569 | ++vcpu->stat.mmio_exits; |
7570 | ||
7ae441ea GN |
7571 | if (!vcpu->mmio_is_write) |
7572 | writeback = false; | |
60fc3d02 | 7573 | r = 0; |
716d51ab | 7574 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
7ae441ea | 7575 | } else if (r == EMULATION_RESTART) |
5cd21917 | 7576 | goto restart; |
d2ddd1c4 | 7577 | else |
60fc3d02 | 7578 | r = 1; |
f850e2e6 | 7579 | |
7ae441ea | 7580 | if (writeback) { |
b3646477 | 7581 | unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); |
9d74191a | 7582 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
7ae441ea | 7583 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
38827dbd | 7584 | if (!ctxt->have_exception || |
75ee23b3 SC |
7585 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
7586 | kvm_rip_write(vcpu, ctxt->eip); | |
384dea1c | 7587 | if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) |
120c2c4f | 7588 | r = kvm_vcpu_do_singlestep(vcpu); |
afaf0b2f | 7589 | if (kvm_x86_ops.update_emulated_instruction) |
b3646477 | 7590 | static_call(kvm_x86_update_emulated_instruction)(vcpu); |
38827dbd | 7591 | __kvm_set_rflags(vcpu, ctxt->eflags); |
75ee23b3 | 7592 | } |
6addfc42 PB |
7593 | |
7594 | /* | |
7595 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will | |
7596 | * do nothing, and it will be requested again as soon as | |
7597 | * the shadow expires. But we still need to check here, | |
7598 | * because POPF has no interrupt shadow. | |
7599 | */ | |
7600 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) | |
7601 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7ae441ea GN |
7602 | } else |
7603 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; | |
e85d28f8 GN |
7604 | |
7605 | return r; | |
de7d789a | 7606 | } |
c60658d1 SC |
7607 | |
7608 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) | |
7609 | { | |
7610 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); | |
7611 | } | |
7612 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); | |
7613 | ||
7614 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, | |
7615 | void *insn, int insn_len) | |
7616 | { | |
7617 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); | |
7618 | } | |
7619 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); | |
de7d789a | 7620 | |
8764ed55 SC |
7621 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
7622 | { | |
7623 | vcpu->arch.pio.count = 0; | |
7624 | return 1; | |
7625 | } | |
7626 | ||
45def77e SC |
7627 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
7628 | { | |
7629 | vcpu->arch.pio.count = 0; | |
7630 | ||
7631 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) | |
7632 | return 1; | |
7633 | ||
7634 | return kvm_skip_emulated_instruction(vcpu); | |
7635 | } | |
7636 | ||
dca7f128 SC |
7637 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
7638 | unsigned short port) | |
de7d789a | 7639 | { |
de3cd117 | 7640 | unsigned long val = kvm_rax_read(vcpu); |
2e3bb4d8 SC |
7641 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
7642 | ||
8764ed55 SC |
7643 | if (ret) |
7644 | return ret; | |
45def77e | 7645 | |
8764ed55 SC |
7646 | /* |
7647 | * Workaround userspace that relies on old KVM behavior of %rip being | |
7648 | * incremented prior to exiting to userspace to handle "OUT 0x7e". | |
7649 | */ | |
7650 | if (port == 0x7e && | |
7651 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { | |
7652 | vcpu->arch.complete_userspace_io = | |
7653 | complete_fast_pio_out_port_0x7e; | |
7654 | kvm_skip_emulated_instruction(vcpu); | |
7655 | } else { | |
45def77e SC |
7656 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
7657 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; | |
7658 | } | |
8764ed55 | 7659 | return 0; |
de7d789a | 7660 | } |
de7d789a | 7661 | |
8370c3d0 TL |
7662 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
7663 | { | |
7664 | unsigned long val; | |
7665 | ||
7666 | /* We should only ever be called with arch.pio.count equal to 1 */ | |
7667 | BUG_ON(vcpu->arch.pio.count != 1); | |
7668 | ||
45def77e SC |
7669 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
7670 | vcpu->arch.pio.count = 0; | |
7671 | return 1; | |
7672 | } | |
7673 | ||
8370c3d0 | 7674 | /* For size less than 4 we merge, else we zero extend */ |
de3cd117 | 7675 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 TL |
7676 | |
7677 | /* | |
2e3bb4d8 | 7678 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
8370c3d0 TL |
7679 | * the copy and tracing |
7680 | */ | |
2e3bb4d8 | 7681 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
de3cd117 | 7682 | kvm_rax_write(vcpu, val); |
8370c3d0 | 7683 | |
45def77e | 7684 | return kvm_skip_emulated_instruction(vcpu); |
8370c3d0 TL |
7685 | } |
7686 | ||
dca7f128 SC |
7687 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
7688 | unsigned short port) | |
8370c3d0 TL |
7689 | { |
7690 | unsigned long val; | |
7691 | int ret; | |
7692 | ||
7693 | /* For size less than 4 we merge, else we zero extend */ | |
de3cd117 | 7694 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 | 7695 | |
2e3bb4d8 | 7696 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
8370c3d0 | 7697 | if (ret) { |
de3cd117 | 7698 | kvm_rax_write(vcpu, val); |
8370c3d0 TL |
7699 | return ret; |
7700 | } | |
7701 | ||
45def77e | 7702 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
8370c3d0 TL |
7703 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
7704 | ||
7705 | return 0; | |
7706 | } | |
dca7f128 SC |
7707 | |
7708 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) | |
7709 | { | |
45def77e | 7710 | int ret; |
dca7f128 | 7711 | |
dca7f128 | 7712 | if (in) |
45def77e | 7713 | ret = kvm_fast_pio_in(vcpu, size, port); |
dca7f128 | 7714 | else |
45def77e SC |
7715 | ret = kvm_fast_pio_out(vcpu, size, port); |
7716 | return ret && kvm_skip_emulated_instruction(vcpu); | |
dca7f128 SC |
7717 | } |
7718 | EXPORT_SYMBOL_GPL(kvm_fast_pio); | |
8370c3d0 | 7719 | |
251a5fd6 | 7720 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
8cfdc000 | 7721 | { |
0a3aee0d | 7722 | __this_cpu_write(cpu_tsc_khz, 0); |
251a5fd6 | 7723 | return 0; |
8cfdc000 ZA |
7724 | } |
7725 | ||
7726 | static void tsc_khz_changed(void *data) | |
c8076604 | 7727 | { |
8cfdc000 ZA |
7728 | struct cpufreq_freqs *freq = data; |
7729 | unsigned long khz = 0; | |
7730 | ||
7731 | if (data) | |
7732 | khz = freq->new; | |
7733 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) | |
7734 | khz = cpufreq_quick_get(raw_smp_processor_id()); | |
7735 | if (!khz) | |
7736 | khz = tsc_khz; | |
0a3aee0d | 7737 | __this_cpu_write(cpu_tsc_khz, khz); |
c8076604 GH |
7738 | } |
7739 | ||
5fa4ec9c | 7740 | #ifdef CONFIG_X86_64 |
0092e434 VK |
7741 | static void kvm_hyperv_tsc_notifier(void) |
7742 | { | |
0092e434 VK |
7743 | struct kvm *kvm; |
7744 | struct kvm_vcpu *vcpu; | |
7745 | int cpu; | |
7746 | ||
0d9ce162 | 7747 | mutex_lock(&kvm_lock); |
0092e434 VK |
7748 | list_for_each_entry(kvm, &vm_list, vm_list) |
7749 | kvm_make_mclock_inprogress_request(kvm); | |
7750 | ||
7751 | hyperv_stop_tsc_emulation(); | |
7752 | ||
7753 | /* TSC frequency always matches when on Hyper-V */ | |
7754 | for_each_present_cpu(cpu) | |
7755 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | |
7756 | kvm_max_guest_tsc_khz = tsc_khz; | |
7757 | ||
7758 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
7759 | struct kvm_arch *ka = &kvm->arch; | |
7760 | ||
7761 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
7762 | ||
7763 | pvclock_update_vm_gtod_copy(kvm); | |
7764 | ||
7765 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7766 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
7767 | ||
7768 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7769 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); | |
7770 | ||
7771 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
7772 | } | |
0d9ce162 | 7773 | mutex_unlock(&kvm_lock); |
0092e434 | 7774 | } |
5fa4ec9c | 7775 | #endif |
0092e434 | 7776 | |
df24014a | 7777 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
c8076604 | 7778 | { |
c8076604 GH |
7779 | struct kvm *kvm; |
7780 | struct kvm_vcpu *vcpu; | |
7781 | int i, send_ipi = 0; | |
7782 | ||
8cfdc000 ZA |
7783 | /* |
7784 | * We allow guests to temporarily run on slowing clocks, | |
7785 | * provided we notify them after, or to run on accelerating | |
7786 | * clocks, provided we notify them before. Thus time never | |
7787 | * goes backwards. | |
7788 | * | |
7789 | * However, we have a problem. We can't atomically update | |
7790 | * the frequency of a given CPU from this function; it is | |
7791 | * merely a notifier, which can be called from any CPU. | |
7792 | * Changing the TSC frequency at arbitrary points in time | |
7793 | * requires a recomputation of local variables related to | |
7794 | * the TSC for each VCPU. We must flag these local variables | |
7795 | * to be updated and be sure the update takes place with the | |
7796 | * new frequency before any guests proceed. | |
7797 | * | |
7798 | * Unfortunately, the combination of hotplug CPU and frequency | |
7799 | * change creates an intractable locking scenario; the order | |
7800 | * of when these callouts happen is undefined with respect to | |
7801 | * CPU hotplug, and they can race with each other. As such, | |
7802 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is | |
7803 | * undefined; you can actually have a CPU frequency change take | |
7804 | * place in between the computation of X and the setting of the | |
7805 | * variable. To protect against this problem, all updates of | |
7806 | * the per_cpu tsc_khz variable are done in an interrupt | |
7807 | * protected IPI, and all callers wishing to update the value | |
7808 | * must wait for a synchronous IPI to complete (which is trivial | |
7809 | * if the caller is on the CPU already). This establishes the | |
7810 | * necessary total order on variable updates. | |
7811 | * | |
7812 | * Note that because a guest time update may take place | |
7813 | * anytime after the setting of the VCPU's request bit, the | |
7814 | * correct TSC value must be set before the request. However, | |
7815 | * to ensure the update actually makes it to any guest which | |
7816 | * starts running in hardware virtualization between the set | |
7817 | * and the acquisition of the spinlock, we must also ping the | |
7818 | * CPU after setting the request bit. | |
7819 | * | |
7820 | */ | |
7821 | ||
df24014a | 7822 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7823 | |
0d9ce162 | 7824 | mutex_lock(&kvm_lock); |
c8076604 | 7825 | list_for_each_entry(kvm, &vm_list, vm_list) { |
988a2cae | 7826 | kvm_for_each_vcpu(i, vcpu, kvm) { |
df24014a | 7827 | if (vcpu->cpu != cpu) |
c8076604 | 7828 | continue; |
c285545f | 7829 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0d9ce162 | 7830 | if (vcpu->cpu != raw_smp_processor_id()) |
8cfdc000 | 7831 | send_ipi = 1; |
c8076604 GH |
7832 | } |
7833 | } | |
0d9ce162 | 7834 | mutex_unlock(&kvm_lock); |
c8076604 GH |
7835 | |
7836 | if (freq->old < freq->new && send_ipi) { | |
7837 | /* | |
7838 | * We upscale the frequency. Must make the guest | |
7839 | * doesn't see old kvmclock values while running with | |
7840 | * the new frequency, otherwise we risk the guest sees | |
7841 | * time go backwards. | |
7842 | * | |
7843 | * In case we update the frequency for another cpu | |
7844 | * (which might be in guest context) send an interrupt | |
7845 | * to kick the cpu out of guest context. Next time | |
7846 | * guest context is entered kvmclock will be updated, | |
7847 | * so the guest will not see stale values. | |
7848 | */ | |
df24014a | 7849 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7850 | } |
df24014a VK |
7851 | } |
7852 | ||
7853 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
7854 | void *data) | |
7855 | { | |
7856 | struct cpufreq_freqs *freq = data; | |
7857 | int cpu; | |
7858 | ||
7859 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | |
7860 | return 0; | |
7861 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | |
7862 | return 0; | |
7863 | ||
7864 | for_each_cpu(cpu, freq->policy->cpus) | |
7865 | __kvmclock_cpufreq_notifier(freq, cpu); | |
7866 | ||
c8076604 GH |
7867 | return 0; |
7868 | } | |
7869 | ||
7870 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | |
8cfdc000 ZA |
7871 | .notifier_call = kvmclock_cpufreq_notifier |
7872 | }; | |
7873 | ||
251a5fd6 | 7874 | static int kvmclock_cpu_online(unsigned int cpu) |
8cfdc000 | 7875 | { |
251a5fd6 SAS |
7876 | tsc_khz_changed(NULL); |
7877 | return 0; | |
8cfdc000 ZA |
7878 | } |
7879 | ||
b820cc0c ZA |
7880 | static void kvm_timer_init(void) |
7881 | { | |
c285545f | 7882 | max_tsc_khz = tsc_khz; |
460dd42e | 7883 | |
b820cc0c | 7884 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
c285545f | 7885 | #ifdef CONFIG_CPU_FREQ |
aaec7c03 | 7886 | struct cpufreq_policy *policy; |
758f588d BP |
7887 | int cpu; |
7888 | ||
3e26f230 | 7889 | cpu = get_cpu(); |
aaec7c03 | 7890 | policy = cpufreq_cpu_get(cpu); |
9a11997e WL |
7891 | if (policy) { |
7892 | if (policy->cpuinfo.max_freq) | |
7893 | max_tsc_khz = policy->cpuinfo.max_freq; | |
7894 | cpufreq_cpu_put(policy); | |
7895 | } | |
3e26f230 | 7896 | put_cpu(); |
c285545f | 7897 | #endif |
b820cc0c ZA |
7898 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
7899 | CPUFREQ_TRANSITION_NOTIFIER); | |
7900 | } | |
460dd42e | 7901 | |
73c1b41e | 7902 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
251a5fd6 | 7903 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
b820cc0c ZA |
7904 | } |
7905 | ||
dd60d217 AK |
7906 | DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
7907 | EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu); | |
ff9d07a0 | 7908 | |
f5132b01 | 7909 | int kvm_is_in_guest(void) |
ff9d07a0 | 7910 | { |
086c9855 | 7911 | return __this_cpu_read(current_vcpu) != NULL; |
ff9d07a0 ZY |
7912 | } |
7913 | ||
7914 | static int kvm_is_user_mode(void) | |
7915 | { | |
7916 | int user_mode = 3; | |
dcf46b94 | 7917 | |
086c9855 | 7918 | if (__this_cpu_read(current_vcpu)) |
b3646477 | 7919 | user_mode = static_call(kvm_x86_get_cpl)(__this_cpu_read(current_vcpu)); |
dcf46b94 | 7920 | |
ff9d07a0 ZY |
7921 | return user_mode != 0; |
7922 | } | |
7923 | ||
7924 | static unsigned long kvm_get_guest_ip(void) | |
7925 | { | |
7926 | unsigned long ip = 0; | |
dcf46b94 | 7927 | |
086c9855 AS |
7928 | if (__this_cpu_read(current_vcpu)) |
7929 | ip = kvm_rip_read(__this_cpu_read(current_vcpu)); | |
dcf46b94 | 7930 | |
ff9d07a0 ZY |
7931 | return ip; |
7932 | } | |
7933 | ||
8479e04e LK |
7934 | static void kvm_handle_intel_pt_intr(void) |
7935 | { | |
7936 | struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu); | |
7937 | ||
7938 | kvm_make_request(KVM_REQ_PMI, vcpu); | |
7939 | __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, | |
7940 | (unsigned long *)&vcpu->arch.pmu.global_status); | |
7941 | } | |
7942 | ||
ff9d07a0 ZY |
7943 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
7944 | .is_in_guest = kvm_is_in_guest, | |
7945 | .is_user_mode = kvm_is_user_mode, | |
7946 | .get_guest_ip = kvm_get_guest_ip, | |
8479e04e | 7947 | .handle_intel_pt_intr = kvm_handle_intel_pt_intr, |
ff9d07a0 ZY |
7948 | }; |
7949 | ||
16e8d74d MT |
7950 | #ifdef CONFIG_X86_64 |
7951 | static void pvclock_gtod_update_fn(struct work_struct *work) | |
7952 | { | |
d828199e MT |
7953 | struct kvm *kvm; |
7954 | ||
7955 | struct kvm_vcpu *vcpu; | |
7956 | int i; | |
7957 | ||
0d9ce162 | 7958 | mutex_lock(&kvm_lock); |
d828199e MT |
7959 | list_for_each_entry(kvm, &vm_list, vm_list) |
7960 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 7961 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
d828199e | 7962 | atomic_set(&kvm_guest_has_master_clock, 0); |
0d9ce162 | 7963 | mutex_unlock(&kvm_lock); |
16e8d74d MT |
7964 | } |
7965 | ||
7966 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); | |
7967 | ||
7968 | /* | |
7969 | * Notification about pvclock gtod data update. | |
7970 | */ | |
7971 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, | |
7972 | void *priv) | |
7973 | { | |
7974 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
7975 | struct timekeeper *tk = priv; | |
7976 | ||
7977 | update_pvclock_gtod(tk); | |
7978 | ||
7979 | /* disable master clock if host does not trust, or does not | |
b0c39dc6 | 7980 | * use, TSC based clocksource. |
16e8d74d | 7981 | */ |
b0c39dc6 | 7982 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
16e8d74d MT |
7983 | atomic_read(&kvm_guest_has_master_clock) != 0) |
7984 | queue_work(system_long_wq, &pvclock_gtod_work); | |
7985 | ||
7986 | return 0; | |
7987 | } | |
7988 | ||
7989 | static struct notifier_block pvclock_gtod_notifier = { | |
7990 | .notifier_call = pvclock_gtod_notify, | |
7991 | }; | |
7992 | #endif | |
7993 | ||
f8c16bba | 7994 | int kvm_arch_init(void *opaque) |
043405e1 | 7995 | { |
d008dfdb | 7996 | struct kvm_x86_init_ops *ops = opaque; |
b820cc0c | 7997 | int r; |
f8c16bba | 7998 | |
afaf0b2f | 7999 | if (kvm_x86_ops.hardware_enable) { |
f8c16bba | 8000 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
56c6d28a ZX |
8001 | r = -EEXIST; |
8002 | goto out; | |
f8c16bba ZX |
8003 | } |
8004 | ||
8005 | if (!ops->cpu_has_kvm_support()) { | |
ef935c25 | 8006 | pr_err_ratelimited("kvm: no hardware support\n"); |
56c6d28a ZX |
8007 | r = -EOPNOTSUPP; |
8008 | goto out; | |
f8c16bba ZX |
8009 | } |
8010 | if (ops->disabled_by_bios()) { | |
ef935c25 | 8011 | pr_err_ratelimited("kvm: disabled by bios\n"); |
56c6d28a ZX |
8012 | r = -EOPNOTSUPP; |
8013 | goto out; | |
f8c16bba ZX |
8014 | } |
8015 | ||
b666a4b6 MO |
8016 | /* |
8017 | * KVM explicitly assumes that the guest has an FPU and | |
8018 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the | |
8019 | * vCPU's FPU state as a fxregs_state struct. | |
8020 | */ | |
8021 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { | |
8022 | printk(KERN_ERR "kvm: inadequate fpu\n"); | |
8023 | r = -EOPNOTSUPP; | |
8024 | goto out; | |
8025 | } | |
8026 | ||
013f6a5d | 8027 | r = -ENOMEM; |
ed8e4812 | 8028 | x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu), |
b666a4b6 MO |
8029 | __alignof__(struct fpu), SLAB_ACCOUNT, |
8030 | NULL); | |
8031 | if (!x86_fpu_cache) { | |
8032 | printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); | |
8033 | goto out; | |
8034 | } | |
8035 | ||
c9b8b07c SC |
8036 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
8037 | if (!x86_emulator_cache) { | |
8038 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); | |
8039 | goto out_free_x86_fpu_cache; | |
8040 | } | |
8041 | ||
7e34fbd0 SC |
8042 | user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); |
8043 | if (!user_return_msrs) { | |
8044 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); | |
c9b8b07c | 8045 | goto out_free_x86_emulator_cache; |
013f6a5d MT |
8046 | } |
8047 | ||
97db56ce AK |
8048 | r = kvm_mmu_module_init(); |
8049 | if (r) | |
013f6a5d | 8050 | goto out_free_percpu; |
97db56ce | 8051 | |
b820cc0c | 8052 | kvm_timer_init(); |
c8076604 | 8053 | |
ff9d07a0 ZY |
8054 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
8055 | ||
cfc48181 | 8056 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
2acf923e | 8057 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
cfc48181 SC |
8058 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
8059 | } | |
2acf923e | 8060 | |
0c5f81da WL |
8061 | if (pi_inject_timer == -1) |
8062 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); | |
16e8d74d MT |
8063 | #ifdef CONFIG_X86_64 |
8064 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); | |
0092e434 | 8065 | |
5fa4ec9c | 8066 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 | 8067 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
16e8d74d MT |
8068 | #endif |
8069 | ||
f8c16bba | 8070 | return 0; |
56c6d28a | 8071 | |
013f6a5d | 8072 | out_free_percpu: |
7e34fbd0 | 8073 | free_percpu(user_return_msrs); |
c9b8b07c SC |
8074 | out_free_x86_emulator_cache: |
8075 | kmem_cache_destroy(x86_emulator_cache); | |
b666a4b6 MO |
8076 | out_free_x86_fpu_cache: |
8077 | kmem_cache_destroy(x86_fpu_cache); | |
56c6d28a | 8078 | out: |
56c6d28a | 8079 | return r; |
043405e1 | 8080 | } |
8776e519 | 8081 | |
f8c16bba ZX |
8082 | void kvm_arch_exit(void) |
8083 | { | |
0092e434 | 8084 | #ifdef CONFIG_X86_64 |
5fa4ec9c | 8085 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 VK |
8086 | clear_hv_tscchange_cb(); |
8087 | #endif | |
cef84c30 | 8088 | kvm_lapic_exit(); |
ff9d07a0 ZY |
8089 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
8090 | ||
888d256e JK |
8091 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
8092 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, | |
8093 | CPUFREQ_TRANSITION_NOTIFIER); | |
251a5fd6 | 8094 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
16e8d74d MT |
8095 | #ifdef CONFIG_X86_64 |
8096 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); | |
8097 | #endif | |
afaf0b2f | 8098 | kvm_x86_ops.hardware_enable = NULL; |
56c6d28a | 8099 | kvm_mmu_module_exit(); |
7e34fbd0 | 8100 | free_percpu(user_return_msrs); |
b666a4b6 | 8101 | kmem_cache_destroy(x86_fpu_cache); |
b59b153d | 8102 | #ifdef CONFIG_KVM_XEN |
c462f859 | 8103 | static_key_deferred_flush(&kvm_xen_enabled); |
7d6bbebb | 8104 | WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key)); |
b59b153d | 8105 | #endif |
56c6d28a | 8106 | } |
f8c16bba | 8107 | |
872f36eb | 8108 | static int __kvm_vcpu_halt(struct kvm_vcpu *vcpu, int state, int reason) |
8776e519 HB |
8109 | { |
8110 | ++vcpu->stat.halt_exits; | |
35754c98 | 8111 | if (lapic_in_kernel(vcpu)) { |
647daca2 | 8112 | vcpu->arch.mp_state = state; |
8776e519 HB |
8113 | return 1; |
8114 | } else { | |
647daca2 | 8115 | vcpu->run->exit_reason = reason; |
8776e519 HB |
8116 | return 0; |
8117 | } | |
8118 | } | |
647daca2 TL |
8119 | |
8120 | int kvm_vcpu_halt(struct kvm_vcpu *vcpu) | |
8121 | { | |
8122 | return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT); | |
8123 | } | |
5cb56059 JS |
8124 | EXPORT_SYMBOL_GPL(kvm_vcpu_halt); |
8125 | ||
8126 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
8127 | { | |
6affcbed KH |
8128 | int ret = kvm_skip_emulated_instruction(vcpu); |
8129 | /* | |
8130 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered | |
8131 | * KVM_EXIT_DEBUG here. | |
8132 | */ | |
8133 | return kvm_vcpu_halt(vcpu) && ret; | |
5cb56059 | 8134 | } |
8776e519 HB |
8135 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
8136 | ||
647daca2 TL |
8137 | int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu) |
8138 | { | |
8139 | int ret = kvm_skip_emulated_instruction(vcpu); | |
8140 | ||
8141 | return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, KVM_EXIT_AP_RESET_HOLD) && ret; | |
8142 | } | |
8143 | EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold); | |
8144 | ||
8ef81a9a | 8145 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
8146 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
8147 | unsigned long clock_type) | |
8148 | { | |
8149 | struct kvm_clock_pairing clock_pairing; | |
899a31f5 | 8150 | struct timespec64 ts; |
80fbd89c | 8151 | u64 cycle; |
55dd00a7 MT |
8152 | int ret; |
8153 | ||
8154 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) | |
8155 | return -KVM_EOPNOTSUPP; | |
8156 | ||
7ca7f3b9 | 8157 | if (!kvm_get_walltime_and_clockread(&ts, &cycle)) |
55dd00a7 MT |
8158 | return -KVM_EOPNOTSUPP; |
8159 | ||
8160 | clock_pairing.sec = ts.tv_sec; | |
8161 | clock_pairing.nsec = ts.tv_nsec; | |
8162 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); | |
8163 | clock_pairing.flags = 0; | |
bcbfbd8e | 8164 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
55dd00a7 MT |
8165 | |
8166 | ret = 0; | |
8167 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, | |
8168 | sizeof(struct kvm_clock_pairing))) | |
8169 | ret = -KVM_EFAULT; | |
8170 | ||
8171 | return ret; | |
8172 | } | |
8ef81a9a | 8173 | #endif |
55dd00a7 | 8174 | |
6aef266c SV |
8175 | /* |
8176 | * kvm_pv_kick_cpu_op: Kick a vcpu. | |
8177 | * | |
8178 | * @apicid - apicid of vcpu to be kicked. | |
8179 | */ | |
8180 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid) | |
8181 | { | |
24d2166b | 8182 | struct kvm_lapic_irq lapic_irq; |
6aef266c | 8183 | |
150a84fe | 8184 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
c96001c5 | 8185 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
ebd28fcb | 8186 | lapic_irq.level = 0; |
24d2166b | 8187 | lapic_irq.dest_id = apicid; |
93bbf0b8 | 8188 | lapic_irq.msi_redir_hint = false; |
6aef266c | 8189 | |
24d2166b | 8190 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
795a149e | 8191 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
6aef266c SV |
8192 | } |
8193 | ||
4e19c36f SS |
8194 | bool kvm_apicv_activated(struct kvm *kvm) |
8195 | { | |
8196 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); | |
8197 | } | |
8198 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); | |
8199 | ||
8200 | void kvm_apicv_init(struct kvm *kvm, bool enable) | |
8201 | { | |
8202 | if (enable) | |
8203 | clear_bit(APICV_INHIBIT_REASON_DISABLE, | |
8204 | &kvm->arch.apicv_inhibit_reasons); | |
8205 | else | |
8206 | set_bit(APICV_INHIBIT_REASON_DISABLE, | |
8207 | &kvm->arch.apicv_inhibit_reasons); | |
8208 | } | |
8209 | EXPORT_SYMBOL_GPL(kvm_apicv_init); | |
8210 | ||
71506297 WL |
8211 | static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) |
8212 | { | |
8213 | struct kvm_vcpu *target = NULL; | |
8214 | struct kvm_apic_map *map; | |
8215 | ||
8216 | rcu_read_lock(); | |
8217 | map = rcu_dereference(kvm->arch.apic_map); | |
8218 | ||
8219 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) | |
8220 | target = map->phys_map[dest_id]->vcpu; | |
8221 | ||
8222 | rcu_read_unlock(); | |
8223 | ||
266e85a5 | 8224 | if (target && READ_ONCE(target->ready)) |
71506297 WL |
8225 | kvm_vcpu_yield_to(target); |
8226 | } | |
8227 | ||
8776e519 HB |
8228 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
8229 | { | |
8230 | unsigned long nr, a0, a1, a2, a3, ret; | |
6356ee0c | 8231 | int op_64_bit; |
8776e519 | 8232 | |
23200b7a JM |
8233 | if (kvm_xen_hypercall_enabled(vcpu->kvm)) |
8234 | return kvm_xen_hypercall(vcpu); | |
8235 | ||
8f014550 | 8236 | if (kvm_hv_hypercall_enabled(vcpu)) |
696ca779 | 8237 | return kvm_hv_hypercall(vcpu); |
55cd8e5a | 8238 | |
de3cd117 SC |
8239 | nr = kvm_rax_read(vcpu); |
8240 | a0 = kvm_rbx_read(vcpu); | |
8241 | a1 = kvm_rcx_read(vcpu); | |
8242 | a2 = kvm_rdx_read(vcpu); | |
8243 | a3 = kvm_rsi_read(vcpu); | |
8776e519 | 8244 | |
229456fc | 8245 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
2714d1d3 | 8246 | |
a449c7aa NA |
8247 | op_64_bit = is_64_bit_mode(vcpu); |
8248 | if (!op_64_bit) { | |
8776e519 HB |
8249 | nr &= 0xFFFFFFFF; |
8250 | a0 &= 0xFFFFFFFF; | |
8251 | a1 &= 0xFFFFFFFF; | |
8252 | a2 &= 0xFFFFFFFF; | |
8253 | a3 &= 0xFFFFFFFF; | |
8254 | } | |
8255 | ||
b3646477 | 8256 | if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { |
07708c4a | 8257 | ret = -KVM_EPERM; |
696ca779 | 8258 | goto out; |
07708c4a JK |
8259 | } |
8260 | ||
66570e96 OU |
8261 | ret = -KVM_ENOSYS; |
8262 | ||
8776e519 | 8263 | switch (nr) { |
b93463aa AK |
8264 | case KVM_HC_VAPIC_POLL_IRQ: |
8265 | ret = 0; | |
8266 | break; | |
6aef266c | 8267 | case KVM_HC_KICK_CPU: |
66570e96 OU |
8268 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT)) |
8269 | break; | |
8270 | ||
6aef266c | 8271 | kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); |
266e85a5 | 8272 | kvm_sched_yield(vcpu->kvm, a1); |
6aef266c SV |
8273 | ret = 0; |
8274 | break; | |
8ef81a9a | 8275 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
8276 | case KVM_HC_CLOCK_PAIRING: |
8277 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); | |
8278 | break; | |
1ed199a4 | 8279 | #endif |
4180bf1b | 8280 | case KVM_HC_SEND_IPI: |
66570e96 OU |
8281 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI)) |
8282 | break; | |
8283 | ||
4180bf1b WL |
8284 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); |
8285 | break; | |
71506297 | 8286 | case KVM_HC_SCHED_YIELD: |
66570e96 OU |
8287 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD)) |
8288 | break; | |
8289 | ||
71506297 WL |
8290 | kvm_sched_yield(vcpu->kvm, a0); |
8291 | ret = 0; | |
8292 | break; | |
8776e519 HB |
8293 | default: |
8294 | ret = -KVM_ENOSYS; | |
8295 | break; | |
8296 | } | |
696ca779 | 8297 | out: |
a449c7aa NA |
8298 | if (!op_64_bit) |
8299 | ret = (u32)ret; | |
de3cd117 | 8300 | kvm_rax_write(vcpu, ret); |
6356ee0c | 8301 | |
f11c3a8d | 8302 | ++vcpu->stat.hypercalls; |
6356ee0c | 8303 | return kvm_skip_emulated_instruction(vcpu); |
8776e519 HB |
8304 | } |
8305 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
8306 | ||
b6785def | 8307 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
8776e519 | 8308 | { |
d6aa1000 | 8309 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
8776e519 | 8310 | char instruction[3]; |
5fdbf976 | 8311 | unsigned long rip = kvm_rip_read(vcpu); |
8776e519 | 8312 | |
b3646477 | 8313 | static_call(kvm_x86_patch_hypercall)(vcpu, instruction); |
8776e519 | 8314 | |
ce2e852e DV |
8315 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
8316 | &ctxt->exception); | |
8776e519 HB |
8317 | } |
8318 | ||
851ba692 | 8319 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
b6c7a5dc | 8320 | { |
782d422b MG |
8321 | return vcpu->run->request_interrupt_window && |
8322 | likely(!pic_in_kernel(vcpu->kvm)); | |
b6c7a5dc HB |
8323 | } |
8324 | ||
851ba692 | 8325 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
b6c7a5dc | 8326 | { |
851ba692 AK |
8327 | struct kvm_run *kvm_run = vcpu->run; |
8328 | ||
f1c6366e TL |
8329 | /* |
8330 | * if_flag is obsolete and useless, so do not bother | |
8331 | * setting it for SEV-ES guests. Userspace can just | |
8332 | * use kvm_run->ready_for_interrupt_injection. | |
8333 | */ | |
8334 | kvm_run->if_flag = !vcpu->arch.guest_state_protected | |
8335 | && (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
8336 | ||
2d3ad1f4 | 8337 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc | 8338 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
127a457a MG |
8339 | kvm_run->ready_for_interrupt_injection = |
8340 | pic_in_kernel(vcpu->kvm) || | |
782d422b | 8341 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
15aad3be CQ |
8342 | |
8343 | if (is_smm(vcpu)) | |
8344 | kvm_run->flags |= KVM_RUN_X86_SMM; | |
b6c7a5dc HB |
8345 | } |
8346 | ||
95ba8273 GN |
8347 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
8348 | { | |
8349 | int max_irr, tpr; | |
8350 | ||
afaf0b2f | 8351 | if (!kvm_x86_ops.update_cr8_intercept) |
95ba8273 GN |
8352 | return; |
8353 | ||
bce87cce | 8354 | if (!lapic_in_kernel(vcpu)) |
88c808fd AK |
8355 | return; |
8356 | ||
d62caabb AS |
8357 | if (vcpu->arch.apicv_active) |
8358 | return; | |
8359 | ||
8db3baa2 GN |
8360 | if (!vcpu->arch.apic->vapic_addr) |
8361 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
8362 | else | |
8363 | max_irr = -1; | |
95ba8273 GN |
8364 | |
8365 | if (max_irr != -1) | |
8366 | max_irr >>= 4; | |
8367 | ||
8368 | tpr = kvm_lapic_get_cr8(vcpu); | |
8369 | ||
b3646477 | 8370 | static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr); |
95ba8273 GN |
8371 | } |
8372 | ||
b97f0745 | 8373 | |
cb6a32c2 SC |
8374 | int kvm_check_nested_events(struct kvm_vcpu *vcpu) |
8375 | { | |
8376 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) | |
8377 | return -EIO; | |
8378 | ||
8379 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { | |
8380 | kvm_x86_ops.nested_ops->triple_fault(vcpu); | |
8381 | return 1; | |
8382 | } | |
8383 | ||
8384 | return kvm_x86_ops.nested_ops->check_events(vcpu); | |
8385 | } | |
8386 | ||
b97f0745 ML |
8387 | static void kvm_inject_exception(struct kvm_vcpu *vcpu) |
8388 | { | |
8389 | if (vcpu->arch.exception.error_code && !is_protmode(vcpu)) | |
8390 | vcpu->arch.exception.error_code = false; | |
8391 | static_call(kvm_x86_queue_exception)(vcpu); | |
8392 | } | |
8393 | ||
c9d40913 | 8394 | static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) |
95ba8273 | 8395 | { |
b6b8a145 | 8396 | int r; |
c6b22f59 | 8397 | bool can_inject = true; |
b6b8a145 | 8398 | |
95ba8273 | 8399 | /* try to reinject previous events if any */ |
664f8e26 | 8400 | |
c6b22f59 | 8401 | if (vcpu->arch.exception.injected) { |
b97f0745 | 8402 | kvm_inject_exception(vcpu); |
c6b22f59 PB |
8403 | can_inject = false; |
8404 | } | |
664f8e26 | 8405 | /* |
a042c26f LA |
8406 | * Do not inject an NMI or interrupt if there is a pending |
8407 | * exception. Exceptions and interrupts are recognized at | |
8408 | * instruction boundaries, i.e. the start of an instruction. | |
8409 | * Trap-like exceptions, e.g. #DB, have higher priority than | |
8410 | * NMIs and interrupts, i.e. traps are recognized before an | |
8411 | * NMI/interrupt that's pending on the same instruction. | |
8412 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest | |
8413 | * priority, but are only generated (pended) during instruction | |
8414 | * execution, i.e. a pending fault-like exception means the | |
8415 | * fault occurred on the *previous* instruction and must be | |
8416 | * serviced prior to recognizing any new events in order to | |
8417 | * fully complete the previous instruction. | |
664f8e26 | 8418 | */ |
1a680e35 | 8419 | else if (!vcpu->arch.exception.pending) { |
c6b22f59 | 8420 | if (vcpu->arch.nmi_injected) { |
b3646477 | 8421 | static_call(kvm_x86_set_nmi)(vcpu); |
c6b22f59 PB |
8422 | can_inject = false; |
8423 | } else if (vcpu->arch.interrupt.injected) { | |
b3646477 | 8424 | static_call(kvm_x86_set_irq)(vcpu); |
c6b22f59 PB |
8425 | can_inject = false; |
8426 | } | |
664f8e26 WL |
8427 | } |
8428 | ||
3b82b8d7 SC |
8429 | WARN_ON_ONCE(vcpu->arch.exception.injected && |
8430 | vcpu->arch.exception.pending); | |
8431 | ||
1a680e35 LA |
8432 | /* |
8433 | * Call check_nested_events() even if we reinjected a previous event | |
8434 | * in order for caller to determine if it should require immediate-exit | |
8435 | * from L2 to L1 due to pending L1 events which require exit | |
8436 | * from L2 to L1. | |
8437 | */ | |
56083bdf | 8438 | if (is_guest_mode(vcpu)) { |
cb6a32c2 | 8439 | r = kvm_check_nested_events(vcpu); |
c9d40913 PB |
8440 | if (r < 0) |
8441 | goto busy; | |
664f8e26 WL |
8442 | } |
8443 | ||
8444 | /* try to inject new event if pending */ | |
b59bb7bd | 8445 | if (vcpu->arch.exception.pending) { |
5c1c85d0 AK |
8446 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
8447 | vcpu->arch.exception.has_error_code, | |
8448 | vcpu->arch.exception.error_code); | |
d6e8c854 | 8449 | |
664f8e26 WL |
8450 | vcpu->arch.exception.pending = false; |
8451 | vcpu->arch.exception.injected = true; | |
8452 | ||
d6e8c854 NA |
8453 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
8454 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | | |
8455 | X86_EFLAGS_RF); | |
8456 | ||
f10c729f | 8457 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
f10c729f JM |
8458 | kvm_deliver_exception_payload(vcpu); |
8459 | if (vcpu->arch.dr7 & DR7_GD) { | |
8460 | vcpu->arch.dr7 &= ~DR7_GD; | |
8461 | kvm_update_dr7(vcpu); | |
8462 | } | |
6bdf0662 NA |
8463 | } |
8464 | ||
b97f0745 | 8465 | kvm_inject_exception(vcpu); |
c6b22f59 | 8466 | can_inject = false; |
1a680e35 LA |
8467 | } |
8468 | ||
c9d40913 PB |
8469 | /* |
8470 | * Finally, inject interrupt events. If an event cannot be injected | |
8471 | * due to architectural conditions (e.g. IF=0) a window-open exit | |
8472 | * will re-request KVM_REQ_EVENT. Sometimes however an event is pending | |
8473 | * and can architecturally be injected, but we cannot do it right now: | |
8474 | * an interrupt could have arrived just now and we have to inject it | |
8475 | * as a vmexit, or there could already an event in the queue, which is | |
8476 | * indicated by can_inject. In that case we request an immediate exit | |
8477 | * in order to make progress and get back here for another iteration. | |
8478 | * The kvm_x86_ops hooks communicate this by returning -EBUSY. | |
8479 | */ | |
8480 | if (vcpu->arch.smi_pending) { | |
b3646477 | 8481 | r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; |
c9d40913 PB |
8482 | if (r < 0) |
8483 | goto busy; | |
8484 | if (r) { | |
8485 | vcpu->arch.smi_pending = false; | |
8486 | ++vcpu->arch.smi_count; | |
8487 | enter_smm(vcpu); | |
8488 | can_inject = false; | |
8489 | } else | |
b3646477 | 8490 | static_call(kvm_x86_enable_smi_window)(vcpu); |
c9d40913 PB |
8491 | } |
8492 | ||
8493 | if (vcpu->arch.nmi_pending) { | |
b3646477 | 8494 | r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; |
c9d40913 PB |
8495 | if (r < 0) |
8496 | goto busy; | |
8497 | if (r) { | |
8498 | --vcpu->arch.nmi_pending; | |
8499 | vcpu->arch.nmi_injected = true; | |
b3646477 | 8500 | static_call(kvm_x86_set_nmi)(vcpu); |
c9d40913 | 8501 | can_inject = false; |
b3646477 | 8502 | WARN_ON(static_call(kvm_x86_nmi_allowed)(vcpu, true) < 0); |
c9d40913 PB |
8503 | } |
8504 | if (vcpu->arch.nmi_pending) | |
b3646477 | 8505 | static_call(kvm_x86_enable_nmi_window)(vcpu); |
c9d40913 | 8506 | } |
1a680e35 | 8507 | |
c9d40913 | 8508 | if (kvm_cpu_has_injectable_intr(vcpu)) { |
b3646477 | 8509 | r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; |
c9d40913 PB |
8510 | if (r < 0) |
8511 | goto busy; | |
8512 | if (r) { | |
8513 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); | |
b3646477 JB |
8514 | static_call(kvm_x86_set_irq)(vcpu); |
8515 | WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); | |
c9d40913 PB |
8516 | } |
8517 | if (kvm_cpu_has_injectable_intr(vcpu)) | |
b3646477 | 8518 | static_call(kvm_x86_enable_irq_window)(vcpu); |
95ba8273 | 8519 | } |
ee2cd4b7 | 8520 | |
c9d40913 PB |
8521 | if (is_guest_mode(vcpu) && |
8522 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
8523 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
8524 | *req_immediate_exit = true; | |
8525 | ||
8526 | WARN_ON(vcpu->arch.exception.pending); | |
8527 | return; | |
8528 | ||
8529 | busy: | |
8530 | *req_immediate_exit = true; | |
8531 | return; | |
95ba8273 GN |
8532 | } |
8533 | ||
7460fb4a AK |
8534 | static void process_nmi(struct kvm_vcpu *vcpu) |
8535 | { | |
8536 | unsigned limit = 2; | |
8537 | ||
8538 | /* | |
8539 | * x86 is limited to one NMI running, and one NMI pending after it. | |
8540 | * If an NMI is already in progress, limit further NMIs to just one. | |
8541 | * Otherwise, allow two (and we'll inject the first one immediately). | |
8542 | */ | |
b3646477 | 8543 | if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) |
7460fb4a AK |
8544 | limit = 1; |
8545 | ||
8546 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); | |
8547 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); | |
8548 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
8549 | } | |
8550 | ||
ee2cd4b7 | 8551 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
660a5d51 PB |
8552 | { |
8553 | u32 flags = 0; | |
8554 | flags |= seg->g << 23; | |
8555 | flags |= seg->db << 22; | |
8556 | flags |= seg->l << 21; | |
8557 | flags |= seg->avl << 20; | |
8558 | flags |= seg->present << 15; | |
8559 | flags |= seg->dpl << 13; | |
8560 | flags |= seg->s << 12; | |
8561 | flags |= seg->type << 8; | |
8562 | return flags; | |
8563 | } | |
8564 | ||
ee2cd4b7 | 8565 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
8566 | { |
8567 | struct kvm_segment seg; | |
8568 | int offset; | |
8569 | ||
8570 | kvm_get_segment(vcpu, &seg, n); | |
8571 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); | |
8572 | ||
8573 | if (n < 3) | |
8574 | offset = 0x7f84 + n * 12; | |
8575 | else | |
8576 | offset = 0x7f2c + (n - 3) * 12; | |
8577 | ||
8578 | put_smstate(u32, buf, offset + 8, seg.base); | |
8579 | put_smstate(u32, buf, offset + 4, seg.limit); | |
ee2cd4b7 | 8580 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
8581 | } |
8582 | ||
efbb288a | 8583 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 8584 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
8585 | { |
8586 | struct kvm_segment seg; | |
8587 | int offset; | |
8588 | u16 flags; | |
8589 | ||
8590 | kvm_get_segment(vcpu, &seg, n); | |
8591 | offset = 0x7e00 + n * 16; | |
8592 | ||
ee2cd4b7 | 8593 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
660a5d51 PB |
8594 | put_smstate(u16, buf, offset, seg.selector); |
8595 | put_smstate(u16, buf, offset + 2, flags); | |
8596 | put_smstate(u32, buf, offset + 4, seg.limit); | |
8597 | put_smstate(u64, buf, offset + 8, seg.base); | |
8598 | } | |
efbb288a | 8599 | #endif |
660a5d51 | 8600 | |
ee2cd4b7 | 8601 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 PB |
8602 | { |
8603 | struct desc_ptr dt; | |
8604 | struct kvm_segment seg; | |
8605 | unsigned long val; | |
8606 | int i; | |
8607 | ||
8608 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); | |
8609 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); | |
8610 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); | |
8611 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); | |
8612 | ||
8613 | for (i = 0; i < 8; i++) | |
8614 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i)); | |
8615 | ||
8616 | kvm_get_dr(vcpu, 6, &val); | |
8617 | put_smstate(u32, buf, 0x7fcc, (u32)val); | |
8618 | kvm_get_dr(vcpu, 7, &val); | |
8619 | put_smstate(u32, buf, 0x7fc8, (u32)val); | |
8620 | ||
8621 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
8622 | put_smstate(u32, buf, 0x7fc4, seg.selector); | |
8623 | put_smstate(u32, buf, 0x7f64, seg.base); | |
8624 | put_smstate(u32, buf, 0x7f60, seg.limit); | |
ee2cd4b7 | 8625 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
8626 | |
8627 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
8628 | put_smstate(u32, buf, 0x7fc0, seg.selector); | |
8629 | put_smstate(u32, buf, 0x7f80, seg.base); | |
8630 | put_smstate(u32, buf, 0x7f7c, seg.limit); | |
ee2cd4b7 | 8631 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
660a5d51 | 8632 | |
b3646477 | 8633 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
660a5d51 PB |
8634 | put_smstate(u32, buf, 0x7f74, dt.address); |
8635 | put_smstate(u32, buf, 0x7f70, dt.size); | |
8636 | ||
b3646477 | 8637 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
660a5d51 PB |
8638 | put_smstate(u32, buf, 0x7f58, dt.address); |
8639 | put_smstate(u32, buf, 0x7f54, dt.size); | |
8640 | ||
8641 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8642 | enter_smm_save_seg_32(vcpu, buf, i); |
660a5d51 PB |
8643 | |
8644 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); | |
8645 | ||
8646 | /* revision id */ | |
8647 | put_smstate(u32, buf, 0x7efc, 0x00020000); | |
8648 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); | |
8649 | } | |
8650 | ||
b68f3cc7 | 8651 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 8652 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 | 8653 | { |
660a5d51 PB |
8654 | struct desc_ptr dt; |
8655 | struct kvm_segment seg; | |
8656 | unsigned long val; | |
8657 | int i; | |
8658 | ||
8659 | for (i = 0; i < 16; i++) | |
8660 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i)); | |
8661 | ||
8662 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); | |
8663 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); | |
8664 | ||
8665 | kvm_get_dr(vcpu, 6, &val); | |
8666 | put_smstate(u64, buf, 0x7f68, val); | |
8667 | kvm_get_dr(vcpu, 7, &val); | |
8668 | put_smstate(u64, buf, 0x7f60, val); | |
8669 | ||
8670 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); | |
8671 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); | |
8672 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); | |
8673 | ||
8674 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); | |
8675 | ||
8676 | /* revision id */ | |
8677 | put_smstate(u32, buf, 0x7efc, 0x00020064); | |
8678 | ||
8679 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); | |
8680 | ||
8681 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
8682 | put_smstate(u16, buf, 0x7e90, seg.selector); | |
ee2cd4b7 | 8683 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8684 | put_smstate(u32, buf, 0x7e94, seg.limit); |
8685 | put_smstate(u64, buf, 0x7e98, seg.base); | |
8686 | ||
b3646477 | 8687 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
660a5d51 PB |
8688 | put_smstate(u32, buf, 0x7e84, dt.size); |
8689 | put_smstate(u64, buf, 0x7e88, dt.address); | |
8690 | ||
8691 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
8692 | put_smstate(u16, buf, 0x7e70, seg.selector); | |
ee2cd4b7 | 8693 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8694 | put_smstate(u32, buf, 0x7e74, seg.limit); |
8695 | put_smstate(u64, buf, 0x7e78, seg.base); | |
8696 | ||
b3646477 | 8697 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
660a5d51 PB |
8698 | put_smstate(u32, buf, 0x7e64, dt.size); |
8699 | put_smstate(u64, buf, 0x7e68, dt.address); | |
8700 | ||
8701 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8702 | enter_smm_save_seg_64(vcpu, buf, i); |
660a5d51 | 8703 | } |
b68f3cc7 | 8704 | #endif |
660a5d51 | 8705 | |
ee2cd4b7 | 8706 | static void enter_smm(struct kvm_vcpu *vcpu) |
64d60670 | 8707 | { |
660a5d51 | 8708 | struct kvm_segment cs, ds; |
18c3626e | 8709 | struct desc_ptr dt; |
660a5d51 PB |
8710 | char buf[512]; |
8711 | u32 cr0; | |
8712 | ||
660a5d51 | 8713 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true); |
660a5d51 | 8714 | memset(buf, 0, 512); |
b68f3cc7 | 8715 | #ifdef CONFIG_X86_64 |
d6321d49 | 8716 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
ee2cd4b7 | 8717 | enter_smm_save_state_64(vcpu, buf); |
660a5d51 | 8718 | else |
b68f3cc7 | 8719 | #endif |
ee2cd4b7 | 8720 | enter_smm_save_state_32(vcpu, buf); |
660a5d51 | 8721 | |
0234bf88 LP |
8722 | /* |
8723 | * Give pre_enter_smm() a chance to make ISA-specific changes to the | |
8724 | * vCPU state (e.g. leave guest mode) after we've saved the state into | |
8725 | * the SMM state-save area. | |
8726 | */ | |
b3646477 | 8727 | static_call(kvm_x86_pre_enter_smm)(vcpu, buf); |
0234bf88 LP |
8728 | |
8729 | vcpu->arch.hflags |= HF_SMM_MASK; | |
54bf36aa | 8730 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
660a5d51 | 8731 | |
b3646477 | 8732 | if (static_call(kvm_x86_get_nmi_mask)(vcpu)) |
660a5d51 PB |
8733 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
8734 | else | |
b3646477 | 8735 | static_call(kvm_x86_set_nmi_mask)(vcpu, true); |
660a5d51 PB |
8736 | |
8737 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); | |
8738 | kvm_rip_write(vcpu, 0x8000); | |
8739 | ||
8740 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); | |
b3646477 | 8741 | static_call(kvm_x86_set_cr0)(vcpu, cr0); |
660a5d51 PB |
8742 | vcpu->arch.cr0 = cr0; |
8743 | ||
b3646477 | 8744 | static_call(kvm_x86_set_cr4)(vcpu, 0); |
660a5d51 | 8745 | |
18c3626e PB |
8746 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
8747 | dt.address = dt.size = 0; | |
b3646477 | 8748 | static_call(kvm_x86_set_idt)(vcpu, &dt); |
18c3626e | 8749 | |
996ff542 | 8750 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
660a5d51 PB |
8751 | |
8752 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; | |
8753 | cs.base = vcpu->arch.smbase; | |
8754 | ||
8755 | ds.selector = 0; | |
8756 | ds.base = 0; | |
8757 | ||
8758 | cs.limit = ds.limit = 0xffffffff; | |
8759 | cs.type = ds.type = 0x3; | |
8760 | cs.dpl = ds.dpl = 0; | |
8761 | cs.db = ds.db = 0; | |
8762 | cs.s = ds.s = 1; | |
8763 | cs.l = ds.l = 0; | |
8764 | cs.g = ds.g = 1; | |
8765 | cs.avl = ds.avl = 0; | |
8766 | cs.present = ds.present = 1; | |
8767 | cs.unusable = ds.unusable = 0; | |
8768 | cs.padding = ds.padding = 0; | |
8769 | ||
8770 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
8771 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); | |
8772 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); | |
8773 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); | |
8774 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); | |
8775 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); | |
8776 | ||
b68f3cc7 | 8777 | #ifdef CONFIG_X86_64 |
d6321d49 | 8778 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
b3646477 | 8779 | static_call(kvm_x86_set_efer)(vcpu, 0); |
b68f3cc7 | 8780 | #endif |
660a5d51 | 8781 | |
aedbaf4f | 8782 | kvm_update_cpuid_runtime(vcpu); |
660a5d51 | 8783 | kvm_mmu_reset_context(vcpu); |
64d60670 PB |
8784 | } |
8785 | ||
ee2cd4b7 | 8786 | static void process_smi(struct kvm_vcpu *vcpu) |
c43203ca PB |
8787 | { |
8788 | vcpu->arch.smi_pending = true; | |
8789 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
8790 | } | |
8791 | ||
7ee30bc1 NNL |
8792 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
8793 | unsigned long *vcpu_bitmap) | |
8794 | { | |
8795 | cpumask_var_t cpus; | |
7ee30bc1 NNL |
8796 | |
8797 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); | |
8798 | ||
db5a95ec | 8799 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, |
54163a34 | 8800 | NULL, vcpu_bitmap, cpus); |
7ee30bc1 NNL |
8801 | |
8802 | free_cpumask_var(cpus); | |
8803 | } | |
8804 | ||
2860c4b1 PB |
8805 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
8806 | { | |
8807 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); | |
8808 | } | |
8809 | ||
8df14af4 SS |
8810 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
8811 | { | |
8812 | if (!lapic_in_kernel(vcpu)) | |
8813 | return; | |
8814 | ||
8815 | vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm); | |
8816 | kvm_apic_update_apicv(vcpu); | |
b3646477 | 8817 | static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu); |
8df14af4 SS |
8818 | } |
8819 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); | |
8820 | ||
8821 | /* | |
8822 | * NOTE: Do not hold any lock prior to calling this. | |
8823 | * | |
8824 | * In particular, kvm_request_apicv_update() expects kvm->srcu not to be | |
8825 | * locked, because it calls __x86_set_memory_region() which does | |
8826 | * synchronize_srcu(&kvm->srcu). | |
8827 | */ | |
8828 | void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit) | |
8829 | { | |
7d611233 | 8830 | struct kvm_vcpu *except; |
8e205a6b PB |
8831 | unsigned long old, new, expected; |
8832 | ||
afaf0b2f | 8833 | if (!kvm_x86_ops.check_apicv_inhibit_reasons || |
b3646477 | 8834 | !static_call(kvm_x86_check_apicv_inhibit_reasons)(bit)) |
ef8efd7a SS |
8835 | return; |
8836 | ||
8e205a6b PB |
8837 | old = READ_ONCE(kvm->arch.apicv_inhibit_reasons); |
8838 | do { | |
8839 | expected = new = old; | |
8840 | if (activate) | |
8841 | __clear_bit(bit, &new); | |
8842 | else | |
8843 | __set_bit(bit, &new); | |
8844 | if (new == old) | |
8845 | break; | |
8846 | old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new); | |
8847 | } while (old != expected); | |
8848 | ||
8849 | if (!!old == !!new) | |
8850 | return; | |
8df14af4 | 8851 | |
24bbf74c | 8852 | trace_kvm_apicv_update_request(activate, bit); |
afaf0b2f | 8853 | if (kvm_x86_ops.pre_update_apicv_exec_ctrl) |
b3646477 | 8854 | static_call(kvm_x86_pre_update_apicv_exec_ctrl)(kvm, activate); |
7d611233 SS |
8855 | |
8856 | /* | |
8857 | * Sending request to update APICV for all other vcpus, | |
8858 | * while update the calling vcpu immediately instead of | |
8859 | * waiting for another #VMEXIT to handle the request. | |
8860 | */ | |
8861 | except = kvm_get_running_vcpu(); | |
8862 | kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE, | |
8863 | except); | |
8864 | if (except) | |
8865 | kvm_vcpu_update_apicv(except); | |
8df14af4 SS |
8866 | } |
8867 | EXPORT_SYMBOL_GPL(kvm_request_apicv_update); | |
8868 | ||
3d81bc7e | 8869 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
c7c9c56c | 8870 | { |
dcbd3e49 | 8871 | if (!kvm_apic_present(vcpu)) |
3d81bc7e | 8872 | return; |
c7c9c56c | 8873 | |
6308630b | 8874 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
c7c9c56c | 8875 | |
b053b2ae | 8876 | if (irqchip_split(vcpu->kvm)) |
6308630b | 8877 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
db2bdcbb | 8878 | else { |
fa59cc00 | 8879 | if (vcpu->arch.apicv_active) |
b3646477 | 8880 | static_call(kvm_x86_sync_pir_to_irr)(vcpu); |
e97f852f WL |
8881 | if (ioapic_in_kernel(vcpu->kvm)) |
8882 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); | |
db2bdcbb | 8883 | } |
e40ff1d6 LA |
8884 | |
8885 | if (is_guest_mode(vcpu)) | |
8886 | vcpu->arch.load_eoi_exitmap_pending = true; | |
8887 | else | |
8888 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); | |
8889 | } | |
8890 | ||
8891 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) | |
8892 | { | |
8893 | u64 eoi_exit_bitmap[4]; | |
8894 | ||
8895 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) | |
8896 | return; | |
8897 | ||
f2bc14b6 VK |
8898 | if (to_hv_vcpu(vcpu)) |
8899 | bitmap_or((ulong *)eoi_exit_bitmap, | |
8900 | vcpu->arch.ioapic_handled_vectors, | |
8901 | to_hv_synic(vcpu)->vec_bitmap, 256); | |
8902 | ||
b3646477 | 8903 | static_call(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap); |
c7c9c56c YZ |
8904 | } |
8905 | ||
e649b3f0 ET |
8906 | void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
8907 | unsigned long start, unsigned long end) | |
b1394e74 RK |
8908 | { |
8909 | unsigned long apic_address; | |
8910 | ||
8911 | /* | |
8912 | * The physical address of apic access page is stored in the VMCS. | |
8913 | * Update it when it becomes invalid. | |
8914 | */ | |
8915 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); | |
8916 | if (start <= apic_address && apic_address < end) | |
8917 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); | |
8918 | } | |
8919 | ||
4256f43f TC |
8920 | void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
8921 | { | |
35754c98 | 8922 | if (!lapic_in_kernel(vcpu)) |
f439ed27 PB |
8923 | return; |
8924 | ||
afaf0b2f | 8925 | if (!kvm_x86_ops.set_apic_access_page_addr) |
4256f43f TC |
8926 | return; |
8927 | ||
b3646477 | 8928 | static_call(kvm_x86_set_apic_access_page_addr)(vcpu); |
4256f43f | 8929 | } |
4256f43f | 8930 | |
d264ee0c SC |
8931 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
8932 | { | |
8933 | smp_send_reschedule(vcpu->cpu); | |
8934 | } | |
8935 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); | |
8936 | ||
9357d939 | 8937 | /* |
362c698f | 8938 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
9357d939 TY |
8939 | * exiting to the userspace. Otherwise, the value will be returned to the |
8940 | * userspace. | |
8941 | */ | |
851ba692 | 8942 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
b6c7a5dc HB |
8943 | { |
8944 | int r; | |
62a193ed MG |
8945 | bool req_int_win = |
8946 | dm_request_for_irq_injection(vcpu) && | |
8947 | kvm_cpu_accept_dm_intr(vcpu); | |
404d5d7b | 8948 | fastpath_t exit_fastpath; |
62a193ed | 8949 | |
730dca42 | 8950 | bool req_immediate_exit = false; |
b6c7a5dc | 8951 | |
fb04a1ed PX |
8952 | /* Forbid vmenter if vcpu dirty ring is soft-full */ |
8953 | if (unlikely(vcpu->kvm->dirty_ring_size && | |
8954 | kvm_dirty_ring_soft_full(&vcpu->dirty_ring))) { | |
8955 | vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL; | |
8956 | trace_kvm_dirty_ring_exit(vcpu); | |
8957 | r = 0; | |
8958 | goto out; | |
8959 | } | |
8960 | ||
2fa6e1e1 | 8961 | if (kvm_request_pending(vcpu)) { |
729c15c2 | 8962 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
9a78e158 | 8963 | if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { |
671ddc70 JM |
8964 | r = 0; |
8965 | goto out; | |
8966 | } | |
8967 | } | |
a8eeb04a | 8968 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
2e53d63a | 8969 | kvm_mmu_unload(vcpu); |
a8eeb04a | 8970 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
2f599714 | 8971 | __kvm_migrate_timers(vcpu); |
d828199e MT |
8972 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
8973 | kvm_gen_update_masterclock(vcpu->kvm); | |
0061d53d MT |
8974 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
8975 | kvm_gen_kvmclock_update(vcpu); | |
34c238a1 ZA |
8976 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
8977 | r = kvm_guest_time_update(vcpu); | |
8cfdc000 ZA |
8978 | if (unlikely(r)) |
8979 | goto out; | |
8980 | } | |
a8eeb04a | 8981 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
4731d4c7 | 8982 | kvm_mmu_sync_roots(vcpu); |
727a7e27 PB |
8983 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
8984 | kvm_mmu_load_pgd(vcpu); | |
eeeb4f67 | 8985 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
7780938c | 8986 | kvm_vcpu_flush_tlb_all(vcpu); |
eeeb4f67 SC |
8987 | |
8988 | /* Flushing all ASIDs flushes the current ASID... */ | |
8989 | kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
8990 | } | |
8991 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) | |
8992 | kvm_vcpu_flush_tlb_current(vcpu); | |
0baedd79 VK |
8993 | if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) |
8994 | kvm_vcpu_flush_tlb_guest(vcpu); | |
eeeb4f67 | 8995 | |
a8eeb04a | 8996 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
851ba692 | 8997 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
b93463aa AK |
8998 | r = 0; |
8999 | goto out; | |
9000 | } | |
a8eeb04a | 9001 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
cb6a32c2 SC |
9002 | if (is_guest_mode(vcpu)) { |
9003 | kvm_x86_ops.nested_ops->triple_fault(vcpu); | |
9004 | } else { | |
9005 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; | |
9006 | vcpu->mmio_needed = 0; | |
9007 | r = 0; | |
9008 | goto out; | |
9009 | } | |
71c4dfaf | 9010 | } |
af585b92 GN |
9011 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
9012 | /* Page is swapped out. Do synthetic halt */ | |
9013 | vcpu->arch.apf.halted = true; | |
9014 | r = 1; | |
9015 | goto out; | |
9016 | } | |
c9aaa895 GC |
9017 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
9018 | record_steal_time(vcpu); | |
64d60670 PB |
9019 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
9020 | process_smi(vcpu); | |
7460fb4a AK |
9021 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
9022 | process_nmi(vcpu); | |
f5132b01 | 9023 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
c6702c9d | 9024 | kvm_pmu_handle_event(vcpu); |
f5132b01 | 9025 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
c6702c9d | 9026 | kvm_pmu_deliver_pmi(vcpu); |
7543a635 SR |
9027 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
9028 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); | |
9029 | if (test_bit(vcpu->arch.pending_ioapic_eoi, | |
6308630b | 9030 | vcpu->arch.ioapic_handled_vectors)) { |
7543a635 SR |
9031 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
9032 | vcpu->run->eoi.vector = | |
9033 | vcpu->arch.pending_ioapic_eoi; | |
9034 | r = 0; | |
9035 | goto out; | |
9036 | } | |
9037 | } | |
3d81bc7e YZ |
9038 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
9039 | vcpu_scan_ioapic(vcpu); | |
e40ff1d6 LA |
9040 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
9041 | vcpu_load_eoi_exitmap(vcpu); | |
4256f43f TC |
9042 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
9043 | kvm_vcpu_reload_apic_access_page(vcpu); | |
2ce79189 AS |
9044 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
9045 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
9046 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; | |
9047 | r = 0; | |
9048 | goto out; | |
9049 | } | |
e516cebb AS |
9050 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
9051 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
9052 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; | |
9053 | r = 0; | |
9054 | goto out; | |
9055 | } | |
db397571 | 9056 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
9ff5e030 VK |
9057 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
9058 | ||
db397571 | 9059 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
9ff5e030 | 9060 | vcpu->run->hyperv = hv_vcpu->exit; |
db397571 AS |
9061 | r = 0; |
9062 | goto out; | |
9063 | } | |
f3b138c5 AS |
9064 | |
9065 | /* | |
9066 | * KVM_REQ_HV_STIMER has to be processed after | |
9067 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers | |
9068 | * depend on the guest clock being up-to-date | |
9069 | */ | |
1f4b34f8 AS |
9070 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
9071 | kvm_hv_process_stimers(vcpu); | |
8df14af4 SS |
9072 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
9073 | kvm_vcpu_update_apicv(vcpu); | |
557a961a VK |
9074 | if (kvm_check_request(KVM_REQ_APF_READY, vcpu)) |
9075 | kvm_check_async_pf_completion(vcpu); | |
1a155254 | 9076 | if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu)) |
b3646477 | 9077 | static_call(kvm_x86_msr_filter_changed)(vcpu); |
a85863c2 MS |
9078 | |
9079 | if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) | |
9080 | static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); | |
2f52d58c | 9081 | } |
b93463aa | 9082 | |
40da8ccd DW |
9083 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || |
9084 | kvm_xen_has_interrupt(vcpu)) { | |
0f1e261e | 9085 | ++vcpu->stat.req_event; |
66450a21 JK |
9086 | kvm_apic_accept_events(vcpu); |
9087 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { | |
9088 | r = 1; | |
9089 | goto out; | |
9090 | } | |
9091 | ||
c9d40913 PB |
9092 | inject_pending_event(vcpu, &req_immediate_exit); |
9093 | if (req_int_win) | |
b3646477 | 9094 | static_call(kvm_x86_enable_irq_window)(vcpu); |
b463a6f7 AK |
9095 | |
9096 | if (kvm_lapic_enabled(vcpu)) { | |
9097 | update_cr8_intercept(vcpu); | |
9098 | kvm_lapic_sync_to_vapic(vcpu); | |
9099 | } | |
9100 | } | |
9101 | ||
d8368af8 AK |
9102 | r = kvm_mmu_reload(vcpu); |
9103 | if (unlikely(r)) { | |
d905c069 | 9104 | goto cancel_injection; |
d8368af8 AK |
9105 | } |
9106 | ||
b6c7a5dc HB |
9107 | preempt_disable(); |
9108 | ||
b3646477 | 9109 | static_call(kvm_x86_prepare_guest_switch)(vcpu); |
b95234c8 PB |
9110 | |
9111 | /* | |
9112 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt | |
9113 | * IPI are then delayed after guest entry, which ensures that they | |
9114 | * result in virtual interrupt delivery. | |
9115 | */ | |
9116 | local_irq_disable(); | |
6b7e2d09 XG |
9117 | vcpu->mode = IN_GUEST_MODE; |
9118 | ||
01b71917 MT |
9119 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
9120 | ||
0f127d12 | 9121 | /* |
b95234c8 | 9122 | * 1) We should set ->mode before checking ->requests. Please see |
cde9af6e | 9123 | * the comment in kvm_vcpu_exiting_guest_mode(). |
b95234c8 | 9124 | * |
81b01667 | 9125 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
b95234c8 PB |
9126 | * pairs with the memory barrier implicit in pi_test_and_set_on |
9127 | * (see vmx_deliver_posted_interrupt). | |
9128 | * | |
9129 | * 3) This also orders the write to mode from any reads to the page | |
9130 | * tables done while the VCPU is running. Please see the comment | |
9131 | * in kvm_flush_remote_tlbs. | |
6b7e2d09 | 9132 | */ |
01b71917 | 9133 | smp_mb__after_srcu_read_unlock(); |
b6c7a5dc | 9134 | |
b95234c8 PB |
9135 | /* |
9136 | * This handles the case where a posted interrupt was | |
9137 | * notified with kvm_vcpu_kick. | |
9138 | */ | |
fa59cc00 | 9139 | if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active) |
b3646477 | 9140 | static_call(kvm_x86_sync_pir_to_irr)(vcpu); |
32f88400 | 9141 | |
5a9f5443 | 9142 | if (kvm_vcpu_exit_request(vcpu)) { |
6b7e2d09 | 9143 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 9144 | smp_wmb(); |
6c142801 AK |
9145 | local_irq_enable(); |
9146 | preempt_enable(); | |
01b71917 | 9147 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
6c142801 | 9148 | r = 1; |
d905c069 | 9149 | goto cancel_injection; |
6c142801 AK |
9150 | } |
9151 | ||
c43203ca PB |
9152 | if (req_immediate_exit) { |
9153 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
b3646477 | 9154 | static_call(kvm_x86_request_immediate_exit)(vcpu); |
c43203ca | 9155 | } |
d6185f20 | 9156 | |
2620fe26 SC |
9157 | fpregs_assert_state_consistent(); |
9158 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
9159 | switch_fpu_return(); | |
5f409e20 | 9160 | |
42dbaa5a | 9161 | if (unlikely(vcpu->arch.switch_db_regs)) { |
42dbaa5a JK |
9162 | set_debugreg(0, 7); |
9163 | set_debugreg(vcpu->arch.eff_db[0], 0); | |
9164 | set_debugreg(vcpu->arch.eff_db[1], 1); | |
9165 | set_debugreg(vcpu->arch.eff_db[2], 2); | |
9166 | set_debugreg(vcpu->arch.eff_db[3], 3); | |
c77fb5fe | 9167 | set_debugreg(vcpu->arch.dr6, 6); |
ae561ede | 9168 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
42dbaa5a | 9169 | } |
b6c7a5dc | 9170 | |
d89d04ab PB |
9171 | for (;;) { |
9172 | exit_fastpath = static_call(kvm_x86_run)(vcpu); | |
9173 | if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST)) | |
9174 | break; | |
9175 | ||
9176 | if (unlikely(kvm_vcpu_exit_request(vcpu))) { | |
9177 | exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED; | |
9178 | break; | |
9179 | } | |
9180 | ||
9181 | if (vcpu->arch.apicv_active) | |
9182 | static_call(kvm_x86_sync_pir_to_irr)(vcpu); | |
9183 | } | |
b6c7a5dc | 9184 | |
c77fb5fe PB |
9185 | /* |
9186 | * Do this here before restoring debug registers on the host. And | |
9187 | * since we do this before handling the vmexit, a DR access vmexit | |
9188 | * can (a) read the correct value of the debug registers, (b) set | |
9189 | * KVM_DEBUGREG_WONT_EXIT again. | |
9190 | */ | |
9191 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { | |
c77fb5fe | 9192 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
b3646477 | 9193 | static_call(kvm_x86_sync_dirty_debug_regs)(vcpu); |
70e4da7a | 9194 | kvm_update_dr0123(vcpu); |
70e4da7a PB |
9195 | kvm_update_dr7(vcpu); |
9196 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; | |
c77fb5fe PB |
9197 | } |
9198 | ||
24f1e32c FW |
9199 | /* |
9200 | * If the guest has used debug registers, at least dr7 | |
9201 | * will be disabled while returning to the host. | |
9202 | * If we don't have active breakpoints in the host, we don't | |
9203 | * care about the messed up debug address registers. But if | |
9204 | * we have some of them active, restore the old state. | |
9205 | */ | |
59d8eb53 | 9206 | if (hw_breakpoint_active()) |
24f1e32c | 9207 | hw_breakpoint_restore(); |
42dbaa5a | 9208 | |
c967118d | 9209 | vcpu->arch.last_vmentry_cpu = vcpu->cpu; |
4ba76538 | 9210 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
1d5f066e | 9211 | |
6b7e2d09 | 9212 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 9213 | smp_wmb(); |
a547c6db | 9214 | |
b3646477 | 9215 | static_call(kvm_x86_handle_exit_irqoff)(vcpu); |
b6c7a5dc | 9216 | |
d7a08882 SC |
9217 | /* |
9218 | * Consume any pending interrupts, including the possible source of | |
9219 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. | |
9220 | * An instruction is required after local_irq_enable() to fully unblock | |
9221 | * interrupts on processors that implement an interrupt shadow, the | |
9222 | * stat.exits increment will do nicely. | |
9223 | */ | |
9224 | kvm_before_interrupt(vcpu); | |
9225 | local_irq_enable(); | |
b6c7a5dc | 9226 | ++vcpu->stat.exits; |
d7a08882 SC |
9227 | local_irq_disable(); |
9228 | kvm_after_interrupt(vcpu); | |
b6c7a5dc | 9229 | |
ec0671d5 WL |
9230 | if (lapic_in_kernel(vcpu)) { |
9231 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; | |
9232 | if (delta != S64_MIN) { | |
9233 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); | |
9234 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; | |
9235 | } | |
9236 | } | |
b6c7a5dc | 9237 | |
f2485b3e | 9238 | local_irq_enable(); |
b6c7a5dc HB |
9239 | preempt_enable(); |
9240 | ||
f656ce01 | 9241 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
3200f405 | 9242 | |
b6c7a5dc HB |
9243 | /* |
9244 | * Profile KVM exit RIPs: | |
9245 | */ | |
9246 | if (unlikely(prof_on == KVM_PROFILING)) { | |
5fdbf976 MT |
9247 | unsigned long rip = kvm_rip_read(vcpu); |
9248 | profile_hit(KVM_PROFILING, (void *)rip); | |
b6c7a5dc HB |
9249 | } |
9250 | ||
cc578287 ZA |
9251 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
9252 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
298101da | 9253 | |
5cfb1d5a MT |
9254 | if (vcpu->arch.apic_attention) |
9255 | kvm_lapic_sync_from_vapic(vcpu); | |
b93463aa | 9256 | |
b3646477 | 9257 | r = static_call(kvm_x86_handle_exit)(vcpu, exit_fastpath); |
d905c069 MT |
9258 | return r; |
9259 | ||
9260 | cancel_injection: | |
8081ad06 SC |
9261 | if (req_immediate_exit) |
9262 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
b3646477 | 9263 | static_call(kvm_x86_cancel_injection)(vcpu); |
ae7a2a3f MT |
9264 | if (unlikely(vcpu->arch.apic_attention)) |
9265 | kvm_lapic_sync_from_vapic(vcpu); | |
d7690175 MT |
9266 | out: |
9267 | return r; | |
9268 | } | |
b6c7a5dc | 9269 | |
362c698f PB |
9270 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
9271 | { | |
bf9f6ac8 | 9272 | if (!kvm_arch_vcpu_runnable(vcpu) && |
b3646477 | 9273 | (!kvm_x86_ops.pre_block || static_call(kvm_x86_pre_block)(vcpu) == 0)) { |
9c8fd1ba PB |
9274 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
9275 | kvm_vcpu_block(vcpu); | |
9276 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); | |
bf9f6ac8 | 9277 | |
afaf0b2f | 9278 | if (kvm_x86_ops.post_block) |
b3646477 | 9279 | static_call(kvm_x86_post_block)(vcpu); |
bf9f6ac8 | 9280 | |
9c8fd1ba PB |
9281 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
9282 | return 1; | |
9283 | } | |
362c698f PB |
9284 | |
9285 | kvm_apic_accept_events(vcpu); | |
9286 | switch(vcpu->arch.mp_state) { | |
9287 | case KVM_MP_STATE_HALTED: | |
647daca2 | 9288 | case KVM_MP_STATE_AP_RESET_HOLD: |
362c698f PB |
9289 | vcpu->arch.pv.pv_unhalted = false; |
9290 | vcpu->arch.mp_state = | |
9291 | KVM_MP_STATE_RUNNABLE; | |
df561f66 | 9292 | fallthrough; |
362c698f PB |
9293 | case KVM_MP_STATE_RUNNABLE: |
9294 | vcpu->arch.apf.halted = false; | |
9295 | break; | |
9296 | case KVM_MP_STATE_INIT_RECEIVED: | |
9297 | break; | |
9298 | default: | |
9299 | return -EINTR; | |
362c698f PB |
9300 | } |
9301 | return 1; | |
9302 | } | |
09cec754 | 9303 | |
5d9bc648 PB |
9304 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
9305 | { | |
56083bdf | 9306 | if (is_guest_mode(vcpu)) |
cb6a32c2 | 9307 | kvm_check_nested_events(vcpu); |
0ad3bed6 | 9308 | |
5d9bc648 PB |
9309 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
9310 | !vcpu->arch.apf.halted); | |
9311 | } | |
9312 | ||
362c698f | 9313 | static int vcpu_run(struct kvm_vcpu *vcpu) |
d7690175 MT |
9314 | { |
9315 | int r; | |
f656ce01 | 9316 | struct kvm *kvm = vcpu->kvm; |
d7690175 | 9317 | |
f656ce01 | 9318 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
c595ceee | 9319 | vcpu->arch.l1tf_flush_l1d = true; |
d7690175 | 9320 | |
362c698f | 9321 | for (;;) { |
58f800d5 | 9322 | if (kvm_vcpu_running(vcpu)) { |
851ba692 | 9323 | r = vcpu_enter_guest(vcpu); |
bf9f6ac8 | 9324 | } else { |
362c698f | 9325 | r = vcpu_block(kvm, vcpu); |
bf9f6ac8 FW |
9326 | } |
9327 | ||
09cec754 GN |
9328 | if (r <= 0) |
9329 | break; | |
9330 | ||
72875d8a | 9331 | kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu); |
09cec754 GN |
9332 | if (kvm_cpu_has_pending_timer(vcpu)) |
9333 | kvm_inject_pending_timer_irqs(vcpu); | |
9334 | ||
782d422b MG |
9335 | if (dm_request_for_irq_injection(vcpu) && |
9336 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { | |
4ca7dd8c PB |
9337 | r = 0; |
9338 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; | |
09cec754 | 9339 | ++vcpu->stat.request_irq_exits; |
362c698f | 9340 | break; |
09cec754 | 9341 | } |
af585b92 | 9342 | |
f3020b88 | 9343 | if (__xfer_to_guest_mode_work_pending()) { |
f656ce01 | 9344 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
72c3c0fe TG |
9345 | r = xfer_to_guest_mode_handle_work(vcpu); |
9346 | if (r) | |
9347 | return r; | |
f656ce01 | 9348 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
d7690175 | 9349 | } |
b6c7a5dc HB |
9350 | } |
9351 | ||
f656ce01 | 9352 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
b6c7a5dc HB |
9353 | |
9354 | return r; | |
9355 | } | |
9356 | ||
716d51ab GN |
9357 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
9358 | { | |
9359 | int r; | |
60fc3d02 | 9360 | |
716d51ab | 9361 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
0ce97a2b | 9362 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
716d51ab | 9363 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
60fc3d02 | 9364 | return r; |
716d51ab GN |
9365 | } |
9366 | ||
9367 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) | |
9368 | { | |
9369 | BUG_ON(!vcpu->arch.pio.count); | |
9370 | ||
9371 | return complete_emulated_io(vcpu); | |
9372 | } | |
9373 | ||
f78146b0 AK |
9374 | /* |
9375 | * Implements the following, as a state machine: | |
9376 | * | |
9377 | * read: | |
9378 | * for each fragment | |
87da7e66 XG |
9379 | * for each mmio piece in the fragment |
9380 | * write gpa, len | |
9381 | * exit | |
9382 | * copy data | |
f78146b0 AK |
9383 | * execute insn |
9384 | * | |
9385 | * write: | |
9386 | * for each fragment | |
87da7e66 XG |
9387 | * for each mmio piece in the fragment |
9388 | * write gpa, len | |
9389 | * copy data | |
9390 | * exit | |
f78146b0 | 9391 | */ |
716d51ab | 9392 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
5287f194 AK |
9393 | { |
9394 | struct kvm_run *run = vcpu->run; | |
f78146b0 | 9395 | struct kvm_mmio_fragment *frag; |
87da7e66 | 9396 | unsigned len; |
5287f194 | 9397 | |
716d51ab | 9398 | BUG_ON(!vcpu->mmio_needed); |
5287f194 | 9399 | |
716d51ab | 9400 | /* Complete previous fragment */ |
87da7e66 XG |
9401 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
9402 | len = min(8u, frag->len); | |
716d51ab | 9403 | if (!vcpu->mmio_is_write) |
87da7e66 XG |
9404 | memcpy(frag->data, run->mmio.data, len); |
9405 | ||
9406 | if (frag->len <= 8) { | |
9407 | /* Switch to the next fragment. */ | |
9408 | frag++; | |
9409 | vcpu->mmio_cur_fragment++; | |
9410 | } else { | |
9411 | /* Go forward to the next mmio piece. */ | |
9412 | frag->data += len; | |
9413 | frag->gpa += len; | |
9414 | frag->len -= len; | |
9415 | } | |
9416 | ||
a08d3b3b | 9417 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
716d51ab | 9418 | vcpu->mmio_needed = 0; |
0912c977 PB |
9419 | |
9420 | /* FIXME: return into emulator if single-stepping. */ | |
cef4dea0 | 9421 | if (vcpu->mmio_is_write) |
716d51ab GN |
9422 | return 1; |
9423 | vcpu->mmio_read_completed = 1; | |
9424 | return complete_emulated_io(vcpu); | |
9425 | } | |
87da7e66 | 9426 | |
716d51ab GN |
9427 | run->exit_reason = KVM_EXIT_MMIO; |
9428 | run->mmio.phys_addr = frag->gpa; | |
9429 | if (vcpu->mmio_is_write) | |
87da7e66 XG |
9430 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
9431 | run->mmio.len = min(8u, frag->len); | |
716d51ab GN |
9432 | run->mmio.is_write = vcpu->mmio_is_write; |
9433 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; | |
9434 | return 0; | |
5287f194 AK |
9435 | } |
9436 | ||
c9aef3b8 SC |
9437 | static void kvm_save_current_fpu(struct fpu *fpu) |
9438 | { | |
9439 | /* | |
9440 | * If the target FPU state is not resident in the CPU registers, just | |
9441 | * memcpy() from current, else save CPU state directly to the target. | |
9442 | */ | |
9443 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
9444 | memcpy(&fpu->state, ¤t->thread.fpu.state, | |
9445 | fpu_kernel_xstate_size); | |
9446 | else | |
9447 | copy_fpregs_to_fpstate(fpu); | |
9448 | } | |
9449 | ||
822f312d SAS |
9450 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
9451 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
9452 | { | |
5f409e20 RR |
9453 | fpregs_lock(); |
9454 | ||
c9aef3b8 SC |
9455 | kvm_save_current_fpu(vcpu->arch.user_fpu); |
9456 | ||
ed02b213 TL |
9457 | /* |
9458 | * Guests with protected state can't have it set by the hypervisor, | |
9459 | * so skip trying to set it. | |
9460 | */ | |
9461 | if (vcpu->arch.guest_fpu) | |
9462 | /* PKRU is separately restored in kvm_x86_ops.run. */ | |
9463 | __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, | |
9464 | ~XFEATURE_MASK_PKRU); | |
5f409e20 RR |
9465 | |
9466 | fpregs_mark_activate(); | |
9467 | fpregs_unlock(); | |
9468 | ||
822f312d SAS |
9469 | trace_kvm_fpu(1); |
9470 | } | |
9471 | ||
9472 | /* When vcpu_run ends, restore user space FPU context. */ | |
9473 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
9474 | { | |
5f409e20 RR |
9475 | fpregs_lock(); |
9476 | ||
ed02b213 TL |
9477 | /* |
9478 | * Guests with protected state can't have it read by the hypervisor, | |
9479 | * so skip trying to save it. | |
9480 | */ | |
9481 | if (vcpu->arch.guest_fpu) | |
9482 | kvm_save_current_fpu(vcpu->arch.guest_fpu); | |
c9aef3b8 | 9483 | |
d9a710e5 | 9484 | copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state); |
5f409e20 RR |
9485 | |
9486 | fpregs_mark_activate(); | |
9487 | fpregs_unlock(); | |
9488 | ||
822f312d SAS |
9489 | ++vcpu->stat.fpu_reload; |
9490 | trace_kvm_fpu(0); | |
9491 | } | |
9492 | ||
1b94f6f8 | 9493 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
b6c7a5dc | 9494 | { |
1b94f6f8 | 9495 | struct kvm_run *kvm_run = vcpu->run; |
b6c7a5dc | 9496 | int r; |
b6c7a5dc | 9497 | |
accb757d | 9498 | vcpu_load(vcpu); |
20b7035c | 9499 | kvm_sigset_activate(vcpu); |
15aad3be | 9500 | kvm_run->flags = 0; |
5663d8f9 PX |
9501 | kvm_load_guest_fpu(vcpu); |
9502 | ||
a4535290 | 9503 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
2f173d26 JS |
9504 | if (kvm_run->immediate_exit) { |
9505 | r = -EINTR; | |
9506 | goto out; | |
9507 | } | |
b6c7a5dc | 9508 | kvm_vcpu_block(vcpu); |
66450a21 | 9509 | kvm_apic_accept_events(vcpu); |
72875d8a | 9510 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
ac9f6dc0 | 9511 | r = -EAGAIN; |
a0595000 JS |
9512 | if (signal_pending(current)) { |
9513 | r = -EINTR; | |
1b94f6f8 | 9514 | kvm_run->exit_reason = KVM_EXIT_INTR; |
a0595000 JS |
9515 | ++vcpu->stat.signal_exits; |
9516 | } | |
ac9f6dc0 | 9517 | goto out; |
b6c7a5dc HB |
9518 | } |
9519 | ||
1b94f6f8 | 9520 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) { |
01643c51 KH |
9521 | r = -EINVAL; |
9522 | goto out; | |
9523 | } | |
9524 | ||
1b94f6f8 | 9525 | if (kvm_run->kvm_dirty_regs) { |
01643c51 KH |
9526 | r = sync_regs(vcpu); |
9527 | if (r != 0) | |
9528 | goto out; | |
9529 | } | |
9530 | ||
b6c7a5dc | 9531 | /* re-sync apic's tpr */ |
35754c98 | 9532 | if (!lapic_in_kernel(vcpu)) { |
eea1cff9 AP |
9533 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
9534 | r = -EINVAL; | |
9535 | goto out; | |
9536 | } | |
9537 | } | |
b6c7a5dc | 9538 | |
716d51ab GN |
9539 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
9540 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; | |
9541 | vcpu->arch.complete_userspace_io = NULL; | |
9542 | r = cui(vcpu); | |
9543 | if (r <= 0) | |
5663d8f9 | 9544 | goto out; |
716d51ab GN |
9545 | } else |
9546 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); | |
5287f194 | 9547 | |
460df4c1 PB |
9548 | if (kvm_run->immediate_exit) |
9549 | r = -EINTR; | |
9550 | else | |
9551 | r = vcpu_run(vcpu); | |
b6c7a5dc HB |
9552 | |
9553 | out: | |
5663d8f9 | 9554 | kvm_put_guest_fpu(vcpu); |
1b94f6f8 | 9555 | if (kvm_run->kvm_valid_regs) |
01643c51 | 9556 | store_regs(vcpu); |
f1d86e46 | 9557 | post_kvm_run_save(vcpu); |
20b7035c | 9558 | kvm_sigset_deactivate(vcpu); |
b6c7a5dc | 9559 | |
accb757d | 9560 | vcpu_put(vcpu); |
b6c7a5dc HB |
9561 | return r; |
9562 | } | |
9563 | ||
01643c51 | 9564 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 9565 | { |
7ae441ea GN |
9566 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
9567 | /* | |
9568 | * We are here if userspace calls get_regs() in the middle of | |
9569 | * instruction emulation. Registers state needs to be copied | |
4a969980 | 9570 | * back from emulation context to vcpu. Userspace shouldn't do |
7ae441ea GN |
9571 | * that usually, but some bad designed PV devices (vmware |
9572 | * backdoor interface) need this to work | |
9573 | */ | |
c9b8b07c | 9574 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
7ae441ea GN |
9575 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
9576 | } | |
de3cd117 SC |
9577 | regs->rax = kvm_rax_read(vcpu); |
9578 | regs->rbx = kvm_rbx_read(vcpu); | |
9579 | regs->rcx = kvm_rcx_read(vcpu); | |
9580 | regs->rdx = kvm_rdx_read(vcpu); | |
9581 | regs->rsi = kvm_rsi_read(vcpu); | |
9582 | regs->rdi = kvm_rdi_read(vcpu); | |
e9c16c78 | 9583 | regs->rsp = kvm_rsp_read(vcpu); |
de3cd117 | 9584 | regs->rbp = kvm_rbp_read(vcpu); |
b6c7a5dc | 9585 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
9586 | regs->r8 = kvm_r8_read(vcpu); |
9587 | regs->r9 = kvm_r9_read(vcpu); | |
9588 | regs->r10 = kvm_r10_read(vcpu); | |
9589 | regs->r11 = kvm_r11_read(vcpu); | |
9590 | regs->r12 = kvm_r12_read(vcpu); | |
9591 | regs->r13 = kvm_r13_read(vcpu); | |
9592 | regs->r14 = kvm_r14_read(vcpu); | |
9593 | regs->r15 = kvm_r15_read(vcpu); | |
b6c7a5dc HB |
9594 | #endif |
9595 | ||
5fdbf976 | 9596 | regs->rip = kvm_rip_read(vcpu); |
91586a3b | 9597 | regs->rflags = kvm_get_rflags(vcpu); |
01643c51 | 9598 | } |
b6c7a5dc | 9599 | |
01643c51 KH |
9600 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
9601 | { | |
9602 | vcpu_load(vcpu); | |
9603 | __get_regs(vcpu, regs); | |
1fc9b76b | 9604 | vcpu_put(vcpu); |
b6c7a5dc HB |
9605 | return 0; |
9606 | } | |
9607 | ||
01643c51 | 9608 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 9609 | { |
7ae441ea GN |
9610 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
9611 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; | |
9612 | ||
de3cd117 SC |
9613 | kvm_rax_write(vcpu, regs->rax); |
9614 | kvm_rbx_write(vcpu, regs->rbx); | |
9615 | kvm_rcx_write(vcpu, regs->rcx); | |
9616 | kvm_rdx_write(vcpu, regs->rdx); | |
9617 | kvm_rsi_write(vcpu, regs->rsi); | |
9618 | kvm_rdi_write(vcpu, regs->rdi); | |
e9c16c78 | 9619 | kvm_rsp_write(vcpu, regs->rsp); |
de3cd117 | 9620 | kvm_rbp_write(vcpu, regs->rbp); |
b6c7a5dc | 9621 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
9622 | kvm_r8_write(vcpu, regs->r8); |
9623 | kvm_r9_write(vcpu, regs->r9); | |
9624 | kvm_r10_write(vcpu, regs->r10); | |
9625 | kvm_r11_write(vcpu, regs->r11); | |
9626 | kvm_r12_write(vcpu, regs->r12); | |
9627 | kvm_r13_write(vcpu, regs->r13); | |
9628 | kvm_r14_write(vcpu, regs->r14); | |
9629 | kvm_r15_write(vcpu, regs->r15); | |
b6c7a5dc HB |
9630 | #endif |
9631 | ||
5fdbf976 | 9632 | kvm_rip_write(vcpu, regs->rip); |
d73235d1 | 9633 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
b6c7a5dc | 9634 | |
b4f14abd JK |
9635 | vcpu->arch.exception.pending = false; |
9636 | ||
3842d135 | 9637 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
01643c51 | 9638 | } |
3842d135 | 9639 | |
01643c51 KH |
9640 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
9641 | { | |
9642 | vcpu_load(vcpu); | |
9643 | __set_regs(vcpu, regs); | |
875656fe | 9644 | vcpu_put(vcpu); |
b6c7a5dc HB |
9645 | return 0; |
9646 | } | |
9647 | ||
b6c7a5dc HB |
9648 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
9649 | { | |
9650 | struct kvm_segment cs; | |
9651 | ||
3e6e0aab | 9652 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
b6c7a5dc HB |
9653 | *db = cs.db; |
9654 | *l = cs.l; | |
9655 | } | |
9656 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
9657 | ||
01643c51 | 9658 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 9659 | { |
89a27f4d | 9660 | struct desc_ptr dt; |
b6c7a5dc | 9661 | |
5265713a TL |
9662 | if (vcpu->arch.guest_state_protected) |
9663 | goto skip_protected_regs; | |
9664 | ||
3e6e0aab GT |
9665 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
9666 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
9667 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
9668 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
9669 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
9670 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 9671 | |
3e6e0aab GT |
9672 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
9673 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 9674 | |
b3646477 | 9675 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
89a27f4d GN |
9676 | sregs->idt.limit = dt.size; |
9677 | sregs->idt.base = dt.address; | |
b3646477 | 9678 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
89a27f4d GN |
9679 | sregs->gdt.limit = dt.size; |
9680 | sregs->gdt.base = dt.address; | |
b6c7a5dc | 9681 | |
ad312c7c | 9682 | sregs->cr2 = vcpu->arch.cr2; |
9f8fe504 | 9683 | sregs->cr3 = kvm_read_cr3(vcpu); |
5265713a TL |
9684 | |
9685 | skip_protected_regs: | |
9686 | sregs->cr0 = kvm_read_cr0(vcpu); | |
fc78f519 | 9687 | sregs->cr4 = kvm_read_cr4(vcpu); |
2d3ad1f4 | 9688 | sregs->cr8 = kvm_get_cr8(vcpu); |
f6801dff | 9689 | sregs->efer = vcpu->arch.efer; |
b6c7a5dc HB |
9690 | sregs->apic_base = kvm_get_apic_base(vcpu); |
9691 | ||
0e96f31e | 9692 | memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap)); |
b6c7a5dc | 9693 | |
04140b41 | 9694 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
14d0bc1f GN |
9695 | set_bit(vcpu->arch.interrupt.nr, |
9696 | (unsigned long *)sregs->interrupt_bitmap); | |
01643c51 | 9697 | } |
16d7a191 | 9698 | |
01643c51 KH |
9699 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
9700 | struct kvm_sregs *sregs) | |
9701 | { | |
9702 | vcpu_load(vcpu); | |
9703 | __get_sregs(vcpu, sregs); | |
bcdec41c | 9704 | vcpu_put(vcpu); |
b6c7a5dc HB |
9705 | return 0; |
9706 | } | |
9707 | ||
62d9f0db MT |
9708 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
9709 | struct kvm_mp_state *mp_state) | |
9710 | { | |
fd232561 | 9711 | vcpu_load(vcpu); |
f958bd23 SC |
9712 | if (kvm_mpx_supported()) |
9713 | kvm_load_guest_fpu(vcpu); | |
fd232561 | 9714 | |
66450a21 | 9715 | kvm_apic_accept_events(vcpu); |
647daca2 TL |
9716 | if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || |
9717 | vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && | |
9718 | vcpu->arch.pv.pv_unhalted) | |
6aef266c SV |
9719 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; |
9720 | else | |
9721 | mp_state->mp_state = vcpu->arch.mp_state; | |
9722 | ||
f958bd23 SC |
9723 | if (kvm_mpx_supported()) |
9724 | kvm_put_guest_fpu(vcpu); | |
fd232561 | 9725 | vcpu_put(vcpu); |
62d9f0db MT |
9726 | return 0; |
9727 | } | |
9728 | ||
9729 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
9730 | struct kvm_mp_state *mp_state) | |
9731 | { | |
e83dff5e CD |
9732 | int ret = -EINVAL; |
9733 | ||
9734 | vcpu_load(vcpu); | |
9735 | ||
bce87cce | 9736 | if (!lapic_in_kernel(vcpu) && |
66450a21 | 9737 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
e83dff5e | 9738 | goto out; |
66450a21 | 9739 | |
27cbe7d6 LA |
9740 | /* |
9741 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in | |
9742 | * INIT state; latched init should be reported using | |
9743 | * KVM_SET_VCPU_EVENTS, so reject it here. | |
9744 | */ | |
9745 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && | |
28bf2888 DH |
9746 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
9747 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) | |
e83dff5e | 9748 | goto out; |
28bf2888 | 9749 | |
66450a21 JK |
9750 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
9751 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; | |
9752 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); | |
9753 | } else | |
9754 | vcpu->arch.mp_state = mp_state->mp_state; | |
3842d135 | 9755 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
e83dff5e CD |
9756 | |
9757 | ret = 0; | |
9758 | out: | |
9759 | vcpu_put(vcpu); | |
9760 | return ret; | |
62d9f0db MT |
9761 | } |
9762 | ||
7f3d35fd KW |
9763 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
9764 | int reason, bool has_error_code, u32 error_code) | |
b6c7a5dc | 9765 | { |
c9b8b07c | 9766 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d | 9767 | int ret; |
e01c2426 | 9768 | |
8ec4722d | 9769 | init_emulate_ctxt(vcpu); |
c697518a | 9770 | |
7f3d35fd | 9771 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
9d74191a | 9772 | has_error_code, error_code); |
1051778f SC |
9773 | if (ret) { |
9774 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
9775 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
9776 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 9777 | return 0; |
1051778f | 9778 | } |
37817f29 | 9779 | |
9d74191a TY |
9780 | kvm_rip_write(vcpu, ctxt->eip); |
9781 | kvm_set_rflags(vcpu, ctxt->eflags); | |
60fc3d02 | 9782 | return 1; |
37817f29 IE |
9783 | } |
9784 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
9785 | ||
ee69c92b | 9786 | static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
f2981033 | 9787 | { |
37b95951 | 9788 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
f2981033 LT |
9789 | /* |
9790 | * When EFER.LME and CR0.PG are set, the processor is in | |
9791 | * 64-bit mode (though maybe in a 32-bit code segment). | |
9792 | * CR4.PAE and EFER.LMA must be set. | |
9793 | */ | |
ee69c92b SC |
9794 | if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) |
9795 | return false; | |
ca29e145 | 9796 | if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3)) |
c1c35cf7 | 9797 | return false; |
f2981033 LT |
9798 | } else { |
9799 | /* | |
9800 | * Not in 64-bit mode: EFER.LMA is clear and the code | |
9801 | * segment cannot be 64-bit. | |
9802 | */ | |
9803 | if (sregs->efer & EFER_LMA || sregs->cs.l) | |
ee69c92b | 9804 | return false; |
f2981033 LT |
9805 | } |
9806 | ||
ee69c92b | 9807 | return kvm_is_valid_cr4(vcpu, sregs->cr4); |
f2981033 LT |
9808 | } |
9809 | ||
01643c51 | 9810 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 9811 | { |
58cb628d | 9812 | struct msr_data apic_base_msr; |
b6c7a5dc | 9813 | int mmu_reset_needed = 0; |
63f42e02 | 9814 | int pending_vec, max_bits, idx; |
89a27f4d | 9815 | struct desc_ptr dt; |
b4ef9d4e CD |
9816 | int ret = -EINVAL; |
9817 | ||
ee69c92b | 9818 | if (!kvm_is_valid_sregs(vcpu, sregs)) |
8dbfb2bf | 9819 | goto out; |
f2981033 | 9820 | |
d3802286 JM |
9821 | apic_base_msr.data = sregs->apic_base; |
9822 | apic_base_msr.host_initiated = true; | |
9823 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) | |
b4ef9d4e | 9824 | goto out; |
6d1068b3 | 9825 | |
5265713a TL |
9826 | if (vcpu->arch.guest_state_protected) |
9827 | goto skip_protected_regs; | |
9828 | ||
89a27f4d GN |
9829 | dt.size = sregs->idt.limit; |
9830 | dt.address = sregs->idt.base; | |
b3646477 | 9831 | static_call(kvm_x86_set_idt)(vcpu, &dt); |
89a27f4d GN |
9832 | dt.size = sregs->gdt.limit; |
9833 | dt.address = sregs->gdt.base; | |
b3646477 | 9834 | static_call(kvm_x86_set_gdt)(vcpu, &dt); |
b6c7a5dc | 9835 | |
ad312c7c | 9836 | vcpu->arch.cr2 = sregs->cr2; |
9f8fe504 | 9837 | mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
dc7e795e | 9838 | vcpu->arch.cr3 = sregs->cr3; |
cb3c1e2f | 9839 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
b6c7a5dc | 9840 | |
2d3ad1f4 | 9841 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 9842 | |
f6801dff | 9843 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
b3646477 | 9844 | static_call(kvm_x86_set_efer)(vcpu, sregs->efer); |
b6c7a5dc | 9845 | |
4d4ec087 | 9846 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
b3646477 | 9847 | static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0); |
d7306163 | 9848 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 9849 | |
fc78f519 | 9850 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
b3646477 | 9851 | static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4); |
63f42e02 XG |
9852 | |
9853 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
bf03d4f9 | 9854 | if (is_pae_paging(vcpu)) { |
9f8fe504 | 9855 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
7c93be44 MT |
9856 | mmu_reset_needed = 1; |
9857 | } | |
63f42e02 | 9858 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b6c7a5dc HB |
9859 | |
9860 | if (mmu_reset_needed) | |
9861 | kvm_mmu_reset_context(vcpu); | |
9862 | ||
3e6e0aab GT |
9863 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
9864 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
9865 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
9866 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
9867 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
9868 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 9869 | |
3e6e0aab GT |
9870 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
9871 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 9872 | |
5f0269f5 ME |
9873 | update_cr8_intercept(vcpu); |
9874 | ||
9c3e4aab | 9875 | /* Older userspace won't unhalt the vcpu on reset. */ |
c5af89b6 | 9876 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
9c3e4aab | 9877 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
3eeb3288 | 9878 | !is_protmode(vcpu)) |
9c3e4aab MT |
9879 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
9880 | ||
5265713a TL |
9881 | skip_protected_regs: |
9882 | max_bits = KVM_NR_INTERRUPTS; | |
9883 | pending_vec = find_first_bit( | |
9884 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); | |
9885 | if (pending_vec < max_bits) { | |
9886 | kvm_queue_interrupt(vcpu, pending_vec, false); | |
9887 | pr_debug("Set back pending irq %d\n", pending_vec); | |
9888 | } | |
9889 | ||
3842d135 AK |
9890 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
9891 | ||
b4ef9d4e CD |
9892 | ret = 0; |
9893 | out: | |
01643c51 KH |
9894 | return ret; |
9895 | } | |
9896 | ||
9897 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
9898 | struct kvm_sregs *sregs) | |
9899 | { | |
9900 | int ret; | |
9901 | ||
9902 | vcpu_load(vcpu); | |
9903 | ret = __set_sregs(vcpu, sregs); | |
b4ef9d4e CD |
9904 | vcpu_put(vcpu); |
9905 | return ret; | |
b6c7a5dc HB |
9906 | } |
9907 | ||
d0bfb940 JK |
9908 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
9909 | struct kvm_guest_debug *dbg) | |
b6c7a5dc | 9910 | { |
355be0b9 | 9911 | unsigned long rflags; |
ae675ef0 | 9912 | int i, r; |
b6c7a5dc | 9913 | |
8d4846b9 TL |
9914 | if (vcpu->arch.guest_state_protected) |
9915 | return -EINVAL; | |
9916 | ||
66b56562 CD |
9917 | vcpu_load(vcpu); |
9918 | ||
4f926bf2 JK |
9919 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
9920 | r = -EBUSY; | |
9921 | if (vcpu->arch.exception.pending) | |
2122ff5e | 9922 | goto out; |
4f926bf2 JK |
9923 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
9924 | kvm_queue_exception(vcpu, DB_VECTOR); | |
9925 | else | |
9926 | kvm_queue_exception(vcpu, BP_VECTOR); | |
9927 | } | |
9928 | ||
91586a3b JK |
9929 | /* |
9930 | * Read rflags as long as potentially injected trace flags are still | |
9931 | * filtered out. | |
9932 | */ | |
9933 | rflags = kvm_get_rflags(vcpu); | |
355be0b9 JK |
9934 | |
9935 | vcpu->guest_debug = dbg->control; | |
9936 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) | |
9937 | vcpu->guest_debug = 0; | |
9938 | ||
9939 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
ae675ef0 JK |
9940 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
9941 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | |
c8639010 | 9942 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
ae675ef0 JK |
9943 | } else { |
9944 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
9945 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
ae675ef0 | 9946 | } |
c8639010 | 9947 | kvm_update_dr7(vcpu); |
ae675ef0 | 9948 | |
f92653ee JK |
9949 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
9950 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + | |
9951 | get_segment_base(vcpu, VCPU_SREG_CS); | |
94fe45da | 9952 | |
91586a3b JK |
9953 | /* |
9954 | * Trigger an rflags update that will inject or remove the trace | |
9955 | * flags. | |
9956 | */ | |
9957 | kvm_set_rflags(vcpu, rflags); | |
b6c7a5dc | 9958 | |
b3646477 | 9959 | static_call(kvm_x86_update_exception_bitmap)(vcpu); |
b6c7a5dc | 9960 | |
4f926bf2 | 9961 | r = 0; |
d0bfb940 | 9962 | |
2122ff5e | 9963 | out: |
66b56562 | 9964 | vcpu_put(vcpu); |
b6c7a5dc HB |
9965 | return r; |
9966 | } | |
9967 | ||
8b006791 ZX |
9968 | /* |
9969 | * Translate a guest virtual address to a guest physical address. | |
9970 | */ | |
9971 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
9972 | struct kvm_translation *tr) | |
9973 | { | |
9974 | unsigned long vaddr = tr->linear_address; | |
9975 | gpa_t gpa; | |
f656ce01 | 9976 | int idx; |
8b006791 | 9977 | |
1da5b61d CD |
9978 | vcpu_load(vcpu); |
9979 | ||
f656ce01 | 9980 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
1871c602 | 9981 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
f656ce01 | 9982 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8b006791 ZX |
9983 | tr->physical_address = gpa; |
9984 | tr->valid = gpa != UNMAPPED_GVA; | |
9985 | tr->writeable = 1; | |
9986 | tr->usermode = 0; | |
8b006791 | 9987 | |
1da5b61d | 9988 | vcpu_put(vcpu); |
8b006791 ZX |
9989 | return 0; |
9990 | } | |
9991 | ||
d0752060 HB |
9992 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
9993 | { | |
1393123e | 9994 | struct fxregs_state *fxsave; |
d0752060 | 9995 | |
ed02b213 TL |
9996 | if (!vcpu->arch.guest_fpu) |
9997 | return 0; | |
9998 | ||
1393123e | 9999 | vcpu_load(vcpu); |
d0752060 | 10000 | |
b666a4b6 | 10001 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 HB |
10002 | memcpy(fpu->fpr, fxsave->st_space, 128); |
10003 | fpu->fcw = fxsave->cwd; | |
10004 | fpu->fsw = fxsave->swd; | |
10005 | fpu->ftwx = fxsave->twd; | |
10006 | fpu->last_opcode = fxsave->fop; | |
10007 | fpu->last_ip = fxsave->rip; | |
10008 | fpu->last_dp = fxsave->rdp; | |
0e96f31e | 10009 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
d0752060 | 10010 | |
1393123e | 10011 | vcpu_put(vcpu); |
d0752060 HB |
10012 | return 0; |
10013 | } | |
10014 | ||
10015 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
10016 | { | |
6a96bc7f CD |
10017 | struct fxregs_state *fxsave; |
10018 | ||
ed02b213 TL |
10019 | if (!vcpu->arch.guest_fpu) |
10020 | return 0; | |
10021 | ||
6a96bc7f CD |
10022 | vcpu_load(vcpu); |
10023 | ||
b666a4b6 | 10024 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 | 10025 | |
d0752060 HB |
10026 | memcpy(fxsave->st_space, fpu->fpr, 128); |
10027 | fxsave->cwd = fpu->fcw; | |
10028 | fxsave->swd = fpu->fsw; | |
10029 | fxsave->twd = fpu->ftwx; | |
10030 | fxsave->fop = fpu->last_opcode; | |
10031 | fxsave->rip = fpu->last_ip; | |
10032 | fxsave->rdp = fpu->last_dp; | |
0e96f31e | 10033 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
d0752060 | 10034 | |
6a96bc7f | 10035 | vcpu_put(vcpu); |
d0752060 HB |
10036 | return 0; |
10037 | } | |
10038 | ||
01643c51 KH |
10039 | static void store_regs(struct kvm_vcpu *vcpu) |
10040 | { | |
10041 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); | |
10042 | ||
10043 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) | |
10044 | __get_regs(vcpu, &vcpu->run->s.regs.regs); | |
10045 | ||
10046 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) | |
10047 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); | |
10048 | ||
10049 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) | |
10050 | kvm_vcpu_ioctl_x86_get_vcpu_events( | |
10051 | vcpu, &vcpu->run->s.regs.events); | |
10052 | } | |
10053 | ||
10054 | static int sync_regs(struct kvm_vcpu *vcpu) | |
10055 | { | |
10056 | if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS) | |
10057 | return -EINVAL; | |
10058 | ||
10059 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { | |
10060 | __set_regs(vcpu, &vcpu->run->s.regs.regs); | |
10061 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; | |
10062 | } | |
10063 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { | |
10064 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) | |
10065 | return -EINVAL; | |
10066 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; | |
10067 | } | |
10068 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { | |
10069 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( | |
10070 | vcpu, &vcpu->run->s.regs.events)) | |
10071 | return -EINVAL; | |
10072 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; | |
10073 | } | |
10074 | ||
10075 | return 0; | |
10076 | } | |
10077 | ||
0ee6a517 | 10078 | static void fx_init(struct kvm_vcpu *vcpu) |
d0752060 | 10079 | { |
ed02b213 TL |
10080 | if (!vcpu->arch.guest_fpu) |
10081 | return; | |
10082 | ||
b666a4b6 | 10083 | fpstate_init(&vcpu->arch.guest_fpu->state); |
782511b0 | 10084 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
b666a4b6 | 10085 | vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = |
df1daba7 | 10086 | host_xcr0 | XSTATE_COMPACTION_ENABLED; |
d0752060 | 10087 | |
2acf923e DC |
10088 | /* |
10089 | * Ensure guest xcr0 is valid for loading | |
10090 | */ | |
d91cab78 | 10091 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
2acf923e | 10092 | |
ad312c7c | 10093 | vcpu->arch.cr0 |= X86_CR0_ET; |
d0752060 | 10094 | } |
d0752060 | 10095 | |
ed02b213 TL |
10096 | void kvm_free_guest_fpu(struct kvm_vcpu *vcpu) |
10097 | { | |
10098 | if (vcpu->arch.guest_fpu) { | |
10099 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); | |
10100 | vcpu->arch.guest_fpu = NULL; | |
10101 | } | |
10102 | } | |
10103 | EXPORT_SYMBOL_GPL(kvm_free_guest_fpu); | |
10104 | ||
897cc38e | 10105 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
e9b11c17 | 10106 | { |
897cc38e SC |
10107 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
10108 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " | |
10109 | "guest TSC will not be reliable\n"); | |
7f1ea208 | 10110 | |
897cc38e | 10111 | return 0; |
e9b11c17 ZX |
10112 | } |
10113 | ||
e529ef66 | 10114 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
e9b11c17 | 10115 | { |
95a0d01e SC |
10116 | struct page *page; |
10117 | int r; | |
c447e76b | 10118 | |
95a0d01e SC |
10119 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
10120 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
10121 | else | |
10122 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; | |
c447e76b | 10123 | |
95a0d01e | 10124 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
c447e76b | 10125 | |
95a0d01e SC |
10126 | r = kvm_mmu_create(vcpu); |
10127 | if (r < 0) | |
10128 | return r; | |
10129 | ||
10130 | if (irqchip_in_kernel(vcpu->kvm)) { | |
95a0d01e SC |
10131 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
10132 | if (r < 0) | |
10133 | goto fail_mmu_destroy; | |
4e19c36f SS |
10134 | if (kvm_apicv_activated(vcpu->kvm)) |
10135 | vcpu->arch.apicv_active = true; | |
95a0d01e | 10136 | } else |
6e4e3b4d | 10137 | static_branch_inc(&kvm_has_noapic_vcpu); |
95a0d01e SC |
10138 | |
10139 | r = -ENOMEM; | |
10140 | ||
93bb59ca | 10141 | page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
95a0d01e SC |
10142 | if (!page) |
10143 | goto fail_free_lapic; | |
10144 | vcpu->arch.pio_data = page_address(page); | |
10145 | ||
10146 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, | |
10147 | GFP_KERNEL_ACCOUNT); | |
10148 | if (!vcpu->arch.mce_banks) | |
10149 | goto fail_free_pio_data; | |
10150 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; | |
10151 | ||
10152 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, | |
10153 | GFP_KERNEL_ACCOUNT)) | |
10154 | goto fail_free_mce_banks; | |
10155 | ||
c9b8b07c SC |
10156 | if (!alloc_emulate_ctxt(vcpu)) |
10157 | goto free_wbinvd_dirty_mask; | |
10158 | ||
95a0d01e SC |
10159 | vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
10160 | GFP_KERNEL_ACCOUNT); | |
10161 | if (!vcpu->arch.user_fpu) { | |
10162 | pr_err("kvm: failed to allocate userspace's fpu\n"); | |
c9b8b07c | 10163 | goto free_emulate_ctxt; |
95a0d01e SC |
10164 | } |
10165 | ||
10166 | vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, | |
10167 | GFP_KERNEL_ACCOUNT); | |
10168 | if (!vcpu->arch.guest_fpu) { | |
10169 | pr_err("kvm: failed to allocate vcpu's fpu\n"); | |
10170 | goto free_user_fpu; | |
10171 | } | |
10172 | fx_init(vcpu); | |
10173 | ||
95a0d01e | 10174 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
a8ac864a | 10175 | vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); |
95a0d01e SC |
10176 | |
10177 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; | |
10178 | ||
10179 | kvm_async_pf_hash_reset(vcpu); | |
10180 | kvm_pmu_init(vcpu); | |
10181 | ||
10182 | vcpu->arch.pending_external_vector = -1; | |
10183 | vcpu->arch.preempted_in_kernel = false; | |
10184 | ||
b3646477 | 10185 | r = static_call(kvm_x86_vcpu_create)(vcpu); |
95a0d01e SC |
10186 | if (r) |
10187 | goto free_guest_fpu; | |
e9b11c17 | 10188 | |
0cf9135b | 10189 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
e53d88af | 10190 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
19efffa2 | 10191 | kvm_vcpu_mtrr_init(vcpu); |
ec7660cc | 10192 | vcpu_load(vcpu); |
d28bc9dd | 10193 | kvm_vcpu_reset(vcpu, false); |
e1732991 | 10194 | kvm_init_mmu(vcpu, false); |
e9b11c17 | 10195 | vcpu_put(vcpu); |
ec7660cc | 10196 | return 0; |
95a0d01e SC |
10197 | |
10198 | free_guest_fpu: | |
ed02b213 | 10199 | kvm_free_guest_fpu(vcpu); |
95a0d01e SC |
10200 | free_user_fpu: |
10201 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
c9b8b07c SC |
10202 | free_emulate_ctxt: |
10203 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); | |
95a0d01e SC |
10204 | free_wbinvd_dirty_mask: |
10205 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); | |
10206 | fail_free_mce_banks: | |
10207 | kfree(vcpu->arch.mce_banks); | |
10208 | fail_free_pio_data: | |
10209 | free_page((unsigned long)vcpu->arch.pio_data); | |
10210 | fail_free_lapic: | |
10211 | kvm_free_lapic(vcpu); | |
10212 | fail_mmu_destroy: | |
10213 | kvm_mmu_destroy(vcpu); | |
10214 | return r; | |
e9b11c17 ZX |
10215 | } |
10216 | ||
31928aa5 | 10217 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
42897d86 | 10218 | { |
332967a3 | 10219 | struct kvm *kvm = vcpu->kvm; |
42897d86 | 10220 | |
ec7660cc | 10221 | if (mutex_lock_killable(&vcpu->mutex)) |
31928aa5 | 10222 | return; |
ec7660cc | 10223 | vcpu_load(vcpu); |
0c899c25 | 10224 | kvm_synchronize_tsc(vcpu, 0); |
42897d86 | 10225 | vcpu_put(vcpu); |
2d5ba19b MT |
10226 | |
10227 | /* poll control enabled by default */ | |
10228 | vcpu->arch.msr_kvm_poll_control = 1; | |
10229 | ||
ec7660cc | 10230 | mutex_unlock(&vcpu->mutex); |
42897d86 | 10231 | |
b34de572 WL |
10232 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
10233 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
10234 | KVMCLOCK_SYNC_PERIOD); | |
42897d86 MT |
10235 | } |
10236 | ||
d40ccc62 | 10237 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 | 10238 | { |
4cbc418a | 10239 | struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache; |
95a0d01e | 10240 | int idx; |
344d9588 | 10241 | |
4cbc418a PB |
10242 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
10243 | ||
50b143e1 | 10244 | kvmclock_reset(vcpu); |
e9b11c17 | 10245 | |
b3646477 | 10246 | static_call(kvm_x86_vcpu_free)(vcpu); |
50b143e1 | 10247 | |
c9b8b07c | 10248 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
50b143e1 SC |
10249 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
10250 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
ed02b213 | 10251 | kvm_free_guest_fpu(vcpu); |
95a0d01e SC |
10252 | |
10253 | kvm_hv_vcpu_uninit(vcpu); | |
10254 | kvm_pmu_destroy(vcpu); | |
10255 | kfree(vcpu->arch.mce_banks); | |
10256 | kvm_free_lapic(vcpu); | |
10257 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
10258 | kvm_mmu_destroy(vcpu); | |
10259 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
10260 | free_page((unsigned long)vcpu->arch.pio_data); | |
255cbecf | 10261 | kvfree(vcpu->arch.cpuid_entries); |
95a0d01e | 10262 | if (!lapic_in_kernel(vcpu)) |
6e4e3b4d | 10263 | static_branch_dec(&kvm_has_noapic_vcpu); |
e9b11c17 ZX |
10264 | } |
10265 | ||
d28bc9dd | 10266 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e9b11c17 | 10267 | { |
b7e31be3 RK |
10268 | kvm_lapic_reset(vcpu, init_event); |
10269 | ||
e69fab5d PB |
10270 | vcpu->arch.hflags = 0; |
10271 | ||
c43203ca | 10272 | vcpu->arch.smi_pending = 0; |
52797bf9 | 10273 | vcpu->arch.smi_count = 0; |
7460fb4a AK |
10274 | atomic_set(&vcpu->arch.nmi_queued, 0); |
10275 | vcpu->arch.nmi_pending = 0; | |
448fa4a9 | 10276 | vcpu->arch.nmi_injected = false; |
5f7552d4 NA |
10277 | kvm_clear_interrupt_queue(vcpu); |
10278 | kvm_clear_exception_queue(vcpu); | |
448fa4a9 | 10279 | |
42dbaa5a | 10280 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
ae561ede | 10281 | kvm_update_dr0123(vcpu); |
9a3ecd5e | 10282 | vcpu->arch.dr6 = DR6_ACTIVE_LOW; |
42dbaa5a | 10283 | vcpu->arch.dr7 = DR7_FIXED_1; |
c8639010 | 10284 | kvm_update_dr7(vcpu); |
42dbaa5a | 10285 | |
1119022c NA |
10286 | vcpu->arch.cr2 = 0; |
10287 | ||
3842d135 | 10288 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
2635b5c4 VK |
10289 | vcpu->arch.apf.msr_en_val = 0; |
10290 | vcpu->arch.apf.msr_int_val = 0; | |
c9aaa895 | 10291 | vcpu->arch.st.msr_val = 0; |
3842d135 | 10292 | |
12f9a48f GC |
10293 | kvmclock_reset(vcpu); |
10294 | ||
af585b92 GN |
10295 | kvm_clear_async_pf_completion_queue(vcpu); |
10296 | kvm_async_pf_hash_reset(vcpu); | |
10297 | vcpu->arch.apf.halted = false; | |
3842d135 | 10298 | |
ed02b213 | 10299 | if (vcpu->arch.guest_fpu && kvm_mpx_supported()) { |
a554d207 WL |
10300 | void *mpx_state_buffer; |
10301 | ||
10302 | /* | |
10303 | * To avoid have the INIT path from kvm_apic_has_events() that be | |
10304 | * called with loaded FPU and does not let userspace fix the state. | |
10305 | */ | |
f775b13e RR |
10306 | if (init_event) |
10307 | kvm_put_guest_fpu(vcpu); | |
b666a4b6 | 10308 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 10309 | XFEATURE_BNDREGS); |
a554d207 WL |
10310 | if (mpx_state_buffer) |
10311 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); | |
b666a4b6 | 10312 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 10313 | XFEATURE_BNDCSR); |
a554d207 WL |
10314 | if (mpx_state_buffer) |
10315 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); | |
f775b13e RR |
10316 | if (init_event) |
10317 | kvm_load_guest_fpu(vcpu); | |
a554d207 WL |
10318 | } |
10319 | ||
64d60670 | 10320 | if (!init_event) { |
d28bc9dd | 10321 | kvm_pmu_reset(vcpu); |
64d60670 | 10322 | vcpu->arch.smbase = 0x30000; |
db2336a8 | 10323 | |
db2336a8 | 10324 | vcpu->arch.msr_misc_features_enables = 0; |
a554d207 WL |
10325 | |
10326 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; | |
64d60670 | 10327 | } |
f5132b01 | 10328 | |
66f7b72e JS |
10329 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
10330 | vcpu->arch.regs_avail = ~0; | |
10331 | vcpu->arch.regs_dirty = ~0; | |
10332 | ||
a554d207 WL |
10333 | vcpu->arch.ia32_xss = 0; |
10334 | ||
b3646477 | 10335 | static_call(kvm_x86_vcpu_reset)(vcpu, init_event); |
e9b11c17 ZX |
10336 | } |
10337 | ||
2b4a273b | 10338 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
66450a21 JK |
10339 | { |
10340 | struct kvm_segment cs; | |
10341 | ||
10342 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
10343 | cs.selector = vector << 8; | |
10344 | cs.base = vector << 12; | |
10345 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
10346 | kvm_rip_write(vcpu, 0); | |
e9b11c17 | 10347 | } |
647daca2 | 10348 | EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); |
e9b11c17 | 10349 | |
13a34e06 | 10350 | int kvm_arch_hardware_enable(void) |
e9b11c17 | 10351 | { |
ca84d1a2 ZA |
10352 | struct kvm *kvm; |
10353 | struct kvm_vcpu *vcpu; | |
10354 | int i; | |
0dd6a6ed ZA |
10355 | int ret; |
10356 | u64 local_tsc; | |
10357 | u64 max_tsc = 0; | |
10358 | bool stable, backwards_tsc = false; | |
18863bdd | 10359 | |
7e34fbd0 | 10360 | kvm_user_return_msr_cpu_online(); |
b3646477 | 10361 | ret = static_call(kvm_x86_hardware_enable)(); |
0dd6a6ed ZA |
10362 | if (ret != 0) |
10363 | return ret; | |
10364 | ||
4ea1636b | 10365 | local_tsc = rdtsc(); |
b0c39dc6 | 10366 | stable = !kvm_check_tsc_unstable(); |
0dd6a6ed ZA |
10367 | list_for_each_entry(kvm, &vm_list, vm_list) { |
10368 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
10369 | if (!stable && vcpu->cpu == smp_processor_id()) | |
105b21bb | 10370 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
10371 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
10372 | backwards_tsc = true; | |
10373 | if (vcpu->arch.last_host_tsc > max_tsc) | |
10374 | max_tsc = vcpu->arch.last_host_tsc; | |
10375 | } | |
10376 | } | |
10377 | } | |
10378 | ||
10379 | /* | |
10380 | * Sometimes, even reliable TSCs go backwards. This happens on | |
10381 | * platforms that reset TSC during suspend or hibernate actions, but | |
10382 | * maintain synchronization. We must compensate. Fortunately, we can | |
10383 | * detect that condition here, which happens early in CPU bringup, | |
10384 | * before any KVM threads can be running. Unfortunately, we can't | |
10385 | * bring the TSCs fully up to date with real time, as we aren't yet far | |
10386 | * enough into CPU bringup that we know how much real time has actually | |
9285ec4c | 10387 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
0dd6a6ed ZA |
10388 | * variables that haven't been updated yet. |
10389 | * | |
10390 | * So we simply find the maximum observed TSC above, then record the | |
10391 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, | |
10392 | * the adjustment will be applied. Note that we accumulate | |
10393 | * adjustments, in case multiple suspend cycles happen before some VCPU | |
10394 | * gets a chance to run again. In the event that no KVM threads get a | |
10395 | * chance to run, we will miss the entire elapsed period, as we'll have | |
10396 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may | |
10397 | * loose cycle time. This isn't too big a deal, since the loss will be | |
10398 | * uniform across all VCPUs (not to mention the scenario is extremely | |
10399 | * unlikely). It is possible that a second hibernate recovery happens | |
10400 | * much faster than a first, causing the observed TSC here to be | |
10401 | * smaller; this would require additional padding adjustment, which is | |
10402 | * why we set last_host_tsc to the local tsc observed here. | |
10403 | * | |
10404 | * N.B. - this code below runs only on platforms with reliable TSC, | |
10405 | * as that is the only way backwards_tsc is set above. Also note | |
10406 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should | |
10407 | * have the same delta_cyc adjustment applied if backwards_tsc | |
10408 | * is detected. Note further, this adjustment is only done once, | |
10409 | * as we reset last_host_tsc on all VCPUs to stop this from being | |
10410 | * called multiple times (one for each physical CPU bringup). | |
10411 | * | |
4a969980 | 10412 | * Platforms with unreliable TSCs don't have to deal with this, they |
0dd6a6ed ZA |
10413 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
10414 | * catchup mode. This will catchup all VCPUs to real time, but cannot | |
10415 | * guarantee that they stay in perfect synchronization. | |
10416 | */ | |
10417 | if (backwards_tsc) { | |
10418 | u64 delta_cyc = max_tsc - local_tsc; | |
10419 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
a826faf1 | 10420 | kvm->arch.backwards_tsc_observed = true; |
0dd6a6ed ZA |
10421 | kvm_for_each_vcpu(i, vcpu, kvm) { |
10422 | vcpu->arch.tsc_offset_adjustment += delta_cyc; | |
10423 | vcpu->arch.last_host_tsc = local_tsc; | |
105b21bb | 10424 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
10425 | } |
10426 | ||
10427 | /* | |
10428 | * We have to disable TSC offset matching.. if you were | |
10429 | * booting a VM while issuing an S4 host suspend.... | |
10430 | * you may have some problem. Solving this issue is | |
10431 | * left as an exercise to the reader. | |
10432 | */ | |
10433 | kvm->arch.last_tsc_nsec = 0; | |
10434 | kvm->arch.last_tsc_write = 0; | |
10435 | } | |
10436 | ||
10437 | } | |
10438 | return 0; | |
e9b11c17 ZX |
10439 | } |
10440 | ||
13a34e06 | 10441 | void kvm_arch_hardware_disable(void) |
e9b11c17 | 10442 | { |
b3646477 | 10443 | static_call(kvm_x86_hardware_disable)(); |
13a34e06 | 10444 | drop_user_return_notifiers(); |
e9b11c17 ZX |
10445 | } |
10446 | ||
b9904085 | 10447 | int kvm_arch_hardware_setup(void *opaque) |
e9b11c17 | 10448 | { |
d008dfdb | 10449 | struct kvm_x86_init_ops *ops = opaque; |
9e9c3fe4 NA |
10450 | int r; |
10451 | ||
91661989 SC |
10452 | rdmsrl_safe(MSR_EFER, &host_efer); |
10453 | ||
408e9a31 PB |
10454 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
10455 | rdmsrl(MSR_IA32_XSS, host_xss); | |
10456 | ||
d008dfdb | 10457 | r = ops->hardware_setup(); |
9e9c3fe4 NA |
10458 | if (r != 0) |
10459 | return r; | |
10460 | ||
afaf0b2f | 10461 | memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); |
b3646477 | 10462 | kvm_ops_static_call_update(); |
69c6f69a | 10463 | |
408e9a31 PB |
10464 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
10465 | supported_xss = 0; | |
10466 | ||
139f7425 PB |
10467 | #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) |
10468 | cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); | |
10469 | #undef __kvm_cpu_cap_has | |
b11306b5 | 10470 | |
35181e86 HZ |
10471 | if (kvm_has_tsc_control) { |
10472 | /* | |
10473 | * Make sure the user can only configure tsc_khz values that | |
10474 | * fit into a signed integer. | |
273ba457 | 10475 | * A min value is not calculated because it will always |
35181e86 HZ |
10476 | * be 1 on all machines. |
10477 | */ | |
10478 | u64 max = min(0x7fffffffULL, | |
10479 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); | |
10480 | kvm_max_guest_tsc_khz = max; | |
10481 | ||
ad721883 | 10482 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
35181e86 | 10483 | } |
ad721883 | 10484 | |
9e9c3fe4 NA |
10485 | kvm_init_msr_list(); |
10486 | return 0; | |
e9b11c17 ZX |
10487 | } |
10488 | ||
10489 | void kvm_arch_hardware_unsetup(void) | |
10490 | { | |
b3646477 | 10491 | static_call(kvm_x86_hardware_unsetup)(); |
e9b11c17 ZX |
10492 | } |
10493 | ||
b9904085 | 10494 | int kvm_arch_check_processor_compat(void *opaque) |
e9b11c17 | 10495 | { |
f1cdecf5 | 10496 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
d008dfdb | 10497 | struct kvm_x86_init_ops *ops = opaque; |
f1cdecf5 SC |
10498 | |
10499 | WARN_ON(!irqs_disabled()); | |
10500 | ||
139f7425 PB |
10501 | if (__cr4_reserved_bits(cpu_has, c) != |
10502 | __cr4_reserved_bits(cpu_has, &boot_cpu_data)) | |
f1cdecf5 SC |
10503 | return -EIO; |
10504 | ||
d008dfdb | 10505 | return ops->check_processor_compatibility(); |
d71ba788 PB |
10506 | } |
10507 | ||
10508 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) | |
10509 | { | |
10510 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; | |
10511 | } | |
10512 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); | |
10513 | ||
10514 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) | |
10515 | { | |
10516 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; | |
e9b11c17 ZX |
10517 | } |
10518 | ||
6e4e3b4d CL |
10519 | __read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu); |
10520 | EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu); | |
54e9818f | 10521 | |
e790d9ef RK |
10522 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
10523 | { | |
b35e5548 LX |
10524 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
10525 | ||
c595ceee | 10526 | vcpu->arch.l1tf_flush_l1d = true; |
b35e5548 LX |
10527 | if (pmu->version && unlikely(pmu->event_count)) { |
10528 | pmu->need_cleanup = true; | |
10529 | kvm_make_request(KVM_REQ_PMU, vcpu); | |
10530 | } | |
b3646477 | 10531 | static_call(kvm_x86_sched_in)(vcpu, cpu); |
e790d9ef RK |
10532 | } |
10533 | ||
562b6b08 SC |
10534 | void kvm_arch_free_vm(struct kvm *kvm) |
10535 | { | |
05f04ae4 | 10536 | kfree(to_kvm_hv(kvm)->hv_pa_pg); |
562b6b08 | 10537 | vfree(kvm); |
e790d9ef RK |
10538 | } |
10539 | ||
562b6b08 | 10540 | |
e08b9637 | 10541 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
d19a9cd2 | 10542 | { |
e08b9637 CO |
10543 | if (type) |
10544 | return -EINVAL; | |
10545 | ||
6ef768fa | 10546 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
f05e70ac | 10547 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
10605204 | 10548 | INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); |
1aa9b957 | 10549 | INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages); |
4d5c5d0f | 10550 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
e0f0bbc5 | 10551 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
d19a9cd2 | 10552 | |
5550af4d SY |
10553 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
10554 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | |
7a84428a AW |
10555 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
10556 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
10557 | &kvm->arch.irq_sources_bitmap); | |
5550af4d | 10558 | |
038f8c11 | 10559 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
1e08ec4a | 10560 | mutex_init(&kvm->arch.apic_map_lock); |
d828199e MT |
10561 | spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock); |
10562 | ||
8171cd68 | 10563 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
d828199e | 10564 | pvclock_update_vm_gtod_copy(kvm); |
53f658b3 | 10565 | |
6fbbde9a DS |
10566 | kvm->arch.guest_can_read_msr_platform_info = true; |
10567 | ||
7e44e449 | 10568 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
332967a3 | 10569 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
7e44e449 | 10570 | |
cbc0236a | 10571 | kvm_hv_init_vm(kvm); |
0eb05bf2 | 10572 | kvm_page_track_init(kvm); |
13d268ca | 10573 | kvm_mmu_init_vm(kvm); |
0eb05bf2 | 10574 | |
b3646477 | 10575 | return static_call(kvm_x86_vm_init)(kvm); |
d19a9cd2 ZX |
10576 | } |
10577 | ||
1aa9b957 JS |
10578 | int kvm_arch_post_init_vm(struct kvm *kvm) |
10579 | { | |
10580 | return kvm_mmu_post_init_vm(kvm); | |
10581 | } | |
10582 | ||
d19a9cd2 ZX |
10583 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
10584 | { | |
ec7660cc | 10585 | vcpu_load(vcpu); |
d19a9cd2 ZX |
10586 | kvm_mmu_unload(vcpu); |
10587 | vcpu_put(vcpu); | |
10588 | } | |
10589 | ||
10590 | static void kvm_free_vcpus(struct kvm *kvm) | |
10591 | { | |
10592 | unsigned int i; | |
988a2cae | 10593 | struct kvm_vcpu *vcpu; |
d19a9cd2 ZX |
10594 | |
10595 | /* | |
10596 | * Unpin any mmu pages first. | |
10597 | */ | |
af585b92 GN |
10598 | kvm_for_each_vcpu(i, vcpu, kvm) { |
10599 | kvm_clear_async_pf_completion_queue(vcpu); | |
988a2cae | 10600 | kvm_unload_vcpu_mmu(vcpu); |
af585b92 | 10601 | } |
988a2cae | 10602 | kvm_for_each_vcpu(i, vcpu, kvm) |
4543bdc0 | 10603 | kvm_vcpu_destroy(vcpu); |
988a2cae GN |
10604 | |
10605 | mutex_lock(&kvm->lock); | |
10606 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
10607 | kvm->vcpus[i] = NULL; | |
d19a9cd2 | 10608 | |
988a2cae GN |
10609 | atomic_set(&kvm->online_vcpus, 0); |
10610 | mutex_unlock(&kvm->lock); | |
d19a9cd2 ZX |
10611 | } |
10612 | ||
ad8ba2cd SY |
10613 | void kvm_arch_sync_events(struct kvm *kvm) |
10614 | { | |
332967a3 | 10615 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
7e44e449 | 10616 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
aea924f6 | 10617 | kvm_free_pit(kvm); |
ad8ba2cd SY |
10618 | } |
10619 | ||
ff5a983c PX |
10620 | #define ERR_PTR_USR(e) ((void __user *)ERR_PTR(e)) |
10621 | ||
10622 | /** | |
10623 | * __x86_set_memory_region: Setup KVM internal memory slot | |
10624 | * | |
10625 | * @kvm: the kvm pointer to the VM. | |
10626 | * @id: the slot ID to setup. | |
10627 | * @gpa: the GPA to install the slot (unused when @size == 0). | |
10628 | * @size: the size of the slot. Set to zero to uninstall a slot. | |
10629 | * | |
10630 | * This function helps to setup a KVM internal memory slot. Specify | |
10631 | * @size > 0 to install a new slot, while @size == 0 to uninstall a | |
10632 | * slot. The return code can be one of the following: | |
10633 | * | |
10634 | * HVA: on success (uninstall will return a bogus HVA) | |
10635 | * -errno: on error | |
10636 | * | |
10637 | * The caller should always use IS_ERR() to check the return value | |
10638 | * before use. Note, the KVM internal memory slots are guaranteed to | |
10639 | * remain valid and unchanged until the VM is destroyed, i.e., the | |
10640 | * GPA->HVA translation will not change. However, the HVA is a user | |
10641 | * address, i.e. its accessibility is not guaranteed, and must be | |
10642 | * accessed via __copy_{to,from}_user(). | |
10643 | */ | |
10644 | void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, | |
10645 | u32 size) | |
9da0e4d5 PB |
10646 | { |
10647 | int i, r; | |
3f649ab7 | 10648 | unsigned long hva, old_npages; |
f0d648bd | 10649 | struct kvm_memslots *slots = kvm_memslots(kvm); |
0577d1ab | 10650 | struct kvm_memory_slot *slot; |
9da0e4d5 PB |
10651 | |
10652 | /* Called with kvm->slots_lock held. */ | |
1d8007bd | 10653 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
ff5a983c | 10654 | return ERR_PTR_USR(-EINVAL); |
9da0e4d5 | 10655 | |
f0d648bd PB |
10656 | slot = id_to_memslot(slots, id); |
10657 | if (size) { | |
0577d1ab | 10658 | if (slot && slot->npages) |
ff5a983c | 10659 | return ERR_PTR_USR(-EEXIST); |
f0d648bd PB |
10660 | |
10661 | /* | |
10662 | * MAP_SHARED to prevent internal slot pages from being moved | |
10663 | * by fork()/COW. | |
10664 | */ | |
10665 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, | |
10666 | MAP_SHARED | MAP_ANONYMOUS, 0); | |
10667 | if (IS_ERR((void *)hva)) | |
ff5a983c | 10668 | return (void __user *)hva; |
f0d648bd | 10669 | } else { |
0577d1ab | 10670 | if (!slot || !slot->npages) |
46914534 | 10671 | return NULL; |
f0d648bd | 10672 | |
0577d1ab | 10673 | old_npages = slot->npages; |
b66f9bab | 10674 | hva = slot->userspace_addr; |
f0d648bd PB |
10675 | } |
10676 | ||
9da0e4d5 | 10677 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
1d8007bd | 10678 | struct kvm_userspace_memory_region m; |
9da0e4d5 | 10679 | |
1d8007bd PB |
10680 | m.slot = id | (i << 16); |
10681 | m.flags = 0; | |
10682 | m.guest_phys_addr = gpa; | |
f0d648bd | 10683 | m.userspace_addr = hva; |
1d8007bd | 10684 | m.memory_size = size; |
9da0e4d5 PB |
10685 | r = __kvm_set_memory_region(kvm, &m); |
10686 | if (r < 0) | |
ff5a983c | 10687 | return ERR_PTR_USR(r); |
9da0e4d5 PB |
10688 | } |
10689 | ||
103c763c | 10690 | if (!size) |
0577d1ab | 10691 | vm_munmap(hva, old_npages * PAGE_SIZE); |
f0d648bd | 10692 | |
ff5a983c | 10693 | return (void __user *)hva; |
9da0e4d5 PB |
10694 | } |
10695 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); | |
10696 | ||
1aa9b957 JS |
10697 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
10698 | { | |
10699 | kvm_mmu_pre_destroy_vm(kvm); | |
10700 | } | |
10701 | ||
d19a9cd2 ZX |
10702 | void kvm_arch_destroy_vm(struct kvm *kvm) |
10703 | { | |
1a155254 AG |
10704 | u32 i; |
10705 | ||
27469d29 AH |
10706 | if (current->mm == kvm->mm) { |
10707 | /* | |
10708 | * Free memory regions allocated on behalf of userspace, | |
10709 | * unless the the memory map has changed due to process exit | |
10710 | * or fd copying. | |
10711 | */ | |
6a3c623b PX |
10712 | mutex_lock(&kvm->slots_lock); |
10713 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, | |
10714 | 0, 0); | |
10715 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, | |
10716 | 0, 0); | |
10717 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); | |
10718 | mutex_unlock(&kvm->slots_lock); | |
27469d29 | 10719 | } |
b3646477 | 10720 | static_call_cond(kvm_x86_vm_destroy)(kvm); |
1a155254 AG |
10721 | for (i = 0; i < kvm->arch.msr_filter.count; i++) |
10722 | kfree(kvm->arch.msr_filter.ranges[i].bitmap); | |
c761159c PX |
10723 | kvm_pic_destroy(kvm); |
10724 | kvm_ioapic_destroy(kvm); | |
d19a9cd2 | 10725 | kvm_free_vcpus(kvm); |
af1bae54 | 10726 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
66bb8a06 | 10727 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
13d268ca | 10728 | kvm_mmu_uninit_vm(kvm); |
2beb6dad | 10729 | kvm_page_track_cleanup(kvm); |
7d6bbebb | 10730 | kvm_xen_destroy_vm(kvm); |
cbc0236a | 10731 | kvm_hv_destroy_vm(kvm); |
d19a9cd2 | 10732 | } |
0de10343 | 10733 | |
e96c81ee | 10734 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
db3fe4eb TY |
10735 | { |
10736 | int i; | |
10737 | ||
d89cc617 | 10738 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
e96c81ee SC |
10739 | kvfree(slot->arch.rmap[i]); |
10740 | slot->arch.rmap[i] = NULL; | |
10741 | ||
d89cc617 TY |
10742 | if (i == 0) |
10743 | continue; | |
10744 | ||
e96c81ee SC |
10745 | kvfree(slot->arch.lpage_info[i - 1]); |
10746 | slot->arch.lpage_info[i - 1] = NULL; | |
db3fe4eb | 10747 | } |
21ebbeda | 10748 | |
e96c81ee | 10749 | kvm_page_track_free_memslot(slot); |
db3fe4eb TY |
10750 | } |
10751 | ||
0dab98b7 SC |
10752 | static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, |
10753 | unsigned long npages) | |
db3fe4eb TY |
10754 | { |
10755 | int i; | |
10756 | ||
edd4fa37 SC |
10757 | /* |
10758 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The | |
10759 | * old arrays will be freed by __kvm_set_memory_region() if installing | |
10760 | * the new memslot is successful. | |
10761 | */ | |
10762 | memset(&slot->arch, 0, sizeof(slot->arch)); | |
10763 | ||
d89cc617 | 10764 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
92f94f1e | 10765 | struct kvm_lpage_info *linfo; |
db3fe4eb TY |
10766 | unsigned long ugfn; |
10767 | int lpages; | |
d89cc617 | 10768 | int level = i + 1; |
db3fe4eb TY |
10769 | |
10770 | lpages = gfn_to_index(slot->base_gfn + npages - 1, | |
10771 | slot->base_gfn, level) + 1; | |
10772 | ||
d89cc617 | 10773 | slot->arch.rmap[i] = |
778e1cdd | 10774 | kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), |
254272ce | 10775 | GFP_KERNEL_ACCOUNT); |
d89cc617 | 10776 | if (!slot->arch.rmap[i]) |
77d11309 | 10777 | goto out_free; |
d89cc617 TY |
10778 | if (i == 0) |
10779 | continue; | |
77d11309 | 10780 | |
254272ce | 10781 | linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
92f94f1e | 10782 | if (!linfo) |
db3fe4eb TY |
10783 | goto out_free; |
10784 | ||
92f94f1e XG |
10785 | slot->arch.lpage_info[i - 1] = linfo; |
10786 | ||
db3fe4eb | 10787 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10788 | linfo[0].disallow_lpage = 1; |
db3fe4eb | 10789 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10790 | linfo[lpages - 1].disallow_lpage = 1; |
db3fe4eb TY |
10791 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
10792 | /* | |
10793 | * If the gfn and userspace address are not aligned wrt each | |
600087b6 | 10794 | * other, disable large page support for this slot. |
db3fe4eb | 10795 | */ |
600087b6 | 10796 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
db3fe4eb TY |
10797 | unsigned long j; |
10798 | ||
10799 | for (j = 0; j < lpages; ++j) | |
92f94f1e | 10800 | linfo[j].disallow_lpage = 1; |
db3fe4eb TY |
10801 | } |
10802 | } | |
10803 | ||
21ebbeda XG |
10804 | if (kvm_page_track_create_memslot(slot, npages)) |
10805 | goto out_free; | |
10806 | ||
db3fe4eb TY |
10807 | return 0; |
10808 | ||
10809 | out_free: | |
d89cc617 | 10810 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
548ef284 | 10811 | kvfree(slot->arch.rmap[i]); |
d89cc617 TY |
10812 | slot->arch.rmap[i] = NULL; |
10813 | if (i == 0) | |
10814 | continue; | |
10815 | ||
548ef284 | 10816 | kvfree(slot->arch.lpage_info[i - 1]); |
d89cc617 | 10817 | slot->arch.lpage_info[i - 1] = NULL; |
db3fe4eb TY |
10818 | } |
10819 | return -ENOMEM; | |
10820 | } | |
10821 | ||
15248258 | 10822 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
e59dbe09 | 10823 | { |
91724814 BO |
10824 | struct kvm_vcpu *vcpu; |
10825 | int i; | |
10826 | ||
e6dff7d1 TY |
10827 | /* |
10828 | * memslots->generation has been incremented. | |
10829 | * mmio generation may have reached its maximum value. | |
10830 | */ | |
15248258 | 10831 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
91724814 BO |
10832 | |
10833 | /* Force re-initialization of steal_time cache */ | |
10834 | kvm_for_each_vcpu(i, vcpu, kvm) | |
10835 | kvm_vcpu_kick(vcpu); | |
e59dbe09 TY |
10836 | } |
10837 | ||
f7784b8e MT |
10838 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
10839 | struct kvm_memory_slot *memslot, | |
09170a49 | 10840 | const struct kvm_userspace_memory_region *mem, |
7b6195a9 | 10841 | enum kvm_mr_change change) |
0de10343 | 10842 | { |
0dab98b7 SC |
10843 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
10844 | return kvm_alloc_memslot_metadata(memslot, | |
10845 | mem->memory_size >> PAGE_SHIFT); | |
f7784b8e MT |
10846 | return 0; |
10847 | } | |
10848 | ||
a85863c2 MS |
10849 | |
10850 | static void kvm_mmu_update_cpu_dirty_logging(struct kvm *kvm, bool enable) | |
10851 | { | |
10852 | struct kvm_arch *ka = &kvm->arch; | |
10853 | ||
10854 | if (!kvm_x86_ops.cpu_dirty_log_size) | |
10855 | return; | |
10856 | ||
10857 | if ((enable && ++ka->cpu_dirty_logging_count == 1) || | |
10858 | (!enable && --ka->cpu_dirty_logging_count == 0)) | |
10859 | kvm_make_all_cpus_request(kvm, KVM_REQ_UPDATE_CPU_DIRTY_LOGGING); | |
10860 | ||
10861 | WARN_ON_ONCE(ka->cpu_dirty_logging_count < 0); | |
10862 | } | |
10863 | ||
88178fd4 | 10864 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
3741679b AY |
10865 | struct kvm_memory_slot *old, |
10866 | struct kvm_memory_slot *new, | |
10867 | enum kvm_mr_change change) | |
88178fd4 | 10868 | { |
a85863c2 MS |
10869 | bool log_dirty_pages = new->flags & KVM_MEM_LOG_DIRTY_PAGES; |
10870 | ||
3741679b | 10871 | /* |
a85863c2 MS |
10872 | * Update CPU dirty logging if dirty logging is being toggled. This |
10873 | * applies to all operations. | |
3741679b | 10874 | */ |
a85863c2 MS |
10875 | if ((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES) |
10876 | kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages); | |
88178fd4 KH |
10877 | |
10878 | /* | |
a85863c2 | 10879 | * Nothing more to do for RO slots (which can't be dirtied and can't be |
b6e16ae5 | 10880 | * made writable) or CREATE/MOVE/DELETE of a slot. |
88178fd4 | 10881 | * |
b6e16ae5 | 10882 | * For a memslot with dirty logging disabled: |
3741679b AY |
10883 | * CREATE: No dirty mappings will already exist. |
10884 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
10885 | * kvm_arch_flush_shadow_memslot() | |
b6e16ae5 SC |
10886 | * |
10887 | * For a memslot with dirty logging enabled: | |
10888 | * CREATE: No shadow pages exist, thus nothing to write-protect | |
10889 | * and no dirty bits to clear. | |
10890 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
10891 | * kvm_arch_flush_shadow_memslot(). | |
3741679b | 10892 | */ |
3741679b | 10893 | if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY)) |
88178fd4 | 10894 | return; |
3741679b AY |
10895 | |
10896 | /* | |
52f46079 SC |
10897 | * READONLY and non-flags changes were filtered out above, and the only |
10898 | * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty | |
10899 | * logging isn't being toggled on or off. | |
88178fd4 | 10900 | */ |
52f46079 SC |
10901 | if (WARN_ON_ONCE(!((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES))) |
10902 | return; | |
10903 | ||
b6e16ae5 SC |
10904 | if (!log_dirty_pages) { |
10905 | /* | |
10906 | * Dirty logging tracks sptes in 4k granularity, meaning that | |
10907 | * large sptes have to be split. If live migration succeeds, | |
10908 | * the guest in the source machine will be destroyed and large | |
10909 | * sptes will be created in the destination. However, if the | |
10910 | * guest continues to run in the source machine (for example if | |
10911 | * live migration fails), small sptes will remain around and | |
10912 | * cause bad performance. | |
10913 | * | |
10914 | * Scan sptes if dirty logging has been stopped, dropping those | |
10915 | * which can be collapsed into a single large-page spte. Later | |
10916 | * page faults will create the large-page sptes. | |
10917 | */ | |
3741679b | 10918 | kvm_mmu_zap_collapsible_sptes(kvm, new); |
b6e16ae5 | 10919 | } else { |
a1419f8b SC |
10920 | /* By default, write-protect everything to log writes. */ |
10921 | int level = PG_LEVEL_4K; | |
10922 | ||
a018eba5 | 10923 | if (kvm_x86_ops.cpu_dirty_log_size) { |
a1419f8b SC |
10924 | /* |
10925 | * Clear all dirty bits, unless pages are treated as | |
10926 | * dirty from the get-go. | |
10927 | */ | |
a018eba5 SC |
10928 | if (!kvm_dirty_log_manual_protect_and_init_set(kvm)) |
10929 | kvm_mmu_slot_leaf_clear_dirty(kvm, new); | |
3c9bd400 | 10930 | |
a1419f8b SC |
10931 | /* |
10932 | * Write-protect large pages on write so that dirty | |
10933 | * logging happens at 4k granularity. No need to | |
10934 | * write-protect small SPTEs since write accesses are | |
10935 | * logged by the CPU via dirty bits. | |
10936 | */ | |
10937 | level = PG_LEVEL_2M; | |
10938 | } else if (kvm_dirty_log_manual_protect_and_init_set(kvm)) { | |
3c9bd400 JZ |
10939 | /* |
10940 | * If we're with initial-all-set, we don't need | |
10941 | * to write protect any small page because | |
10942 | * they're reported as dirty already. However | |
10943 | * we still need to write-protect huge pages | |
10944 | * so that the page split can happen lazily on | |
10945 | * the first write to the huge page. | |
10946 | */ | |
a1419f8b | 10947 | level = PG_LEVEL_2M; |
3c9bd400 | 10948 | } |
a1419f8b | 10949 | kvm_mmu_slot_remove_write_access(kvm, new, level); |
88178fd4 KH |
10950 | } |
10951 | } | |
10952 | ||
f7784b8e | 10953 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
09170a49 | 10954 | const struct kvm_userspace_memory_region *mem, |
9d4c197c | 10955 | struct kvm_memory_slot *old, |
f36f3f28 | 10956 | const struct kvm_memory_slot *new, |
8482644a | 10957 | enum kvm_mr_change change) |
f7784b8e | 10958 | { |
48c0e4e9 | 10959 | if (!kvm->arch.n_requested_mmu_pages) |
4d66623c WY |
10960 | kvm_mmu_change_mmu_pages(kvm, |
10961 | kvm_mmu_calculate_default_mmu_pages(kvm)); | |
1c91cad4 | 10962 | |
3ea3b7fa | 10963 | /* |
f36f3f28 | 10964 | * FIXME: const-ify all uses of struct kvm_memory_slot. |
c972f3b1 | 10965 | */ |
3741679b | 10966 | kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change); |
21198846 SC |
10967 | |
10968 | /* Free the arrays associated with the old memslot. */ | |
10969 | if (change == KVM_MR_MOVE) | |
e96c81ee | 10970 | kvm_arch_free_memslot(kvm, old); |
0de10343 | 10971 | } |
1d737c8a | 10972 | |
2df72e9b | 10973 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
34d4cb8f | 10974 | { |
7390de1e | 10975 | kvm_mmu_zap_all(kvm); |
34d4cb8f MT |
10976 | } |
10977 | ||
2df72e9b MT |
10978 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
10979 | struct kvm_memory_slot *slot) | |
10980 | { | |
ae7cd873 | 10981 | kvm_page_track_flush_slot(kvm, slot); |
2df72e9b MT |
10982 | } |
10983 | ||
e6c67d8c LA |
10984 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
10985 | { | |
10986 | return (is_guest_mode(vcpu) && | |
afaf0b2f | 10987 | kvm_x86_ops.guest_apic_has_interrupt && |
b3646477 | 10988 | static_call(kvm_x86_guest_apic_has_interrupt)(vcpu)); |
e6c67d8c LA |
10989 | } |
10990 | ||
5d9bc648 PB |
10991 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
10992 | { | |
10993 | if (!list_empty_careful(&vcpu->async_pf.done)) | |
10994 | return true; | |
10995 | ||
10996 | if (kvm_apic_has_events(vcpu)) | |
10997 | return true; | |
10998 | ||
10999 | if (vcpu->arch.pv.pv_unhalted) | |
11000 | return true; | |
11001 | ||
a5f01f8e WL |
11002 | if (vcpu->arch.exception.pending) |
11003 | return true; | |
11004 | ||
47a66eed Z |
11005 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
11006 | (vcpu->arch.nmi_pending && | |
b3646477 | 11007 | static_call(kvm_x86_nmi_allowed)(vcpu, false))) |
5d9bc648 PB |
11008 | return true; |
11009 | ||
47a66eed | 11010 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
a9fa7cb6 | 11011 | (vcpu->arch.smi_pending && |
b3646477 | 11012 | static_call(kvm_x86_smi_allowed)(vcpu, false))) |
73917739 PB |
11013 | return true; |
11014 | ||
5d9bc648 | 11015 | if (kvm_arch_interrupt_allowed(vcpu) && |
e6c67d8c LA |
11016 | (kvm_cpu_has_interrupt(vcpu) || |
11017 | kvm_guest_apic_has_interrupt(vcpu))) | |
5d9bc648 PB |
11018 | return true; |
11019 | ||
1f4b34f8 AS |
11020 | if (kvm_hv_has_stimer_pending(vcpu)) |
11021 | return true; | |
11022 | ||
d2060bd4 SC |
11023 | if (is_guest_mode(vcpu) && |
11024 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
11025 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
11026 | return true; | |
11027 | ||
5d9bc648 PB |
11028 | return false; |
11029 | } | |
11030 | ||
1d737c8a ZX |
11031 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
11032 | { | |
5d9bc648 | 11033 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
1d737c8a | 11034 | } |
5736199a | 11035 | |
17e433b5 WL |
11036 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
11037 | { | |
11038 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) | |
11039 | return true; | |
11040 | ||
11041 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || | |
11042 | kvm_test_request(KVM_REQ_SMI, vcpu) || | |
11043 | kvm_test_request(KVM_REQ_EVENT, vcpu)) | |
11044 | return true; | |
11045 | ||
b3646477 | 11046 | if (vcpu->arch.apicv_active && static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu)) |
17e433b5 WL |
11047 | return true; |
11048 | ||
11049 | return false; | |
11050 | } | |
11051 | ||
199b5763 LM |
11052 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
11053 | { | |
de63ad4c | 11054 | return vcpu->arch.preempted_in_kernel; |
199b5763 LM |
11055 | } |
11056 | ||
b6d33834 | 11057 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
5736199a | 11058 | { |
b6d33834 | 11059 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
5736199a | 11060 | } |
78646121 GN |
11061 | |
11062 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) | |
11063 | { | |
b3646477 | 11064 | return static_call(kvm_x86_interrupt_allowed)(vcpu, false); |
78646121 | 11065 | } |
229456fc | 11066 | |
82b32774 | 11067 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
f92653ee | 11068 | { |
7ed9abfe TL |
11069 | /* Can't read the RIP when guest state is protected, just return 0 */ |
11070 | if (vcpu->arch.guest_state_protected) | |
11071 | return 0; | |
11072 | ||
82b32774 NA |
11073 | if (is_64_bit_mode(vcpu)) |
11074 | return kvm_rip_read(vcpu); | |
11075 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + | |
11076 | kvm_rip_read(vcpu)); | |
11077 | } | |
11078 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); | |
f92653ee | 11079 | |
82b32774 NA |
11080 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
11081 | { | |
11082 | return kvm_get_linear_rip(vcpu) == linear_rip; | |
f92653ee JK |
11083 | } |
11084 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); | |
11085 | ||
94fe45da JK |
11086 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
11087 | { | |
11088 | unsigned long rflags; | |
11089 | ||
b3646477 | 11090 | rflags = static_call(kvm_x86_get_rflags)(vcpu); |
94fe45da | 11091 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
c310bac5 | 11092 | rflags &= ~X86_EFLAGS_TF; |
94fe45da JK |
11093 | return rflags; |
11094 | } | |
11095 | EXPORT_SYMBOL_GPL(kvm_get_rflags); | |
11096 | ||
6addfc42 | 11097 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
94fe45da JK |
11098 | { |
11099 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && | |
f92653ee | 11100 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
c310bac5 | 11101 | rflags |= X86_EFLAGS_TF; |
b3646477 | 11102 | static_call(kvm_x86_set_rflags)(vcpu, rflags); |
6addfc42 PB |
11103 | } |
11104 | ||
11105 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
11106 | { | |
11107 | __kvm_set_rflags(vcpu, rflags); | |
3842d135 | 11108 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
94fe45da JK |
11109 | } |
11110 | EXPORT_SYMBOL_GPL(kvm_set_rflags); | |
11111 | ||
56028d08 GN |
11112 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
11113 | { | |
11114 | int r; | |
11115 | ||
44dd3ffa | 11116 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
f2e10669 | 11117 | work->wakeup_all) |
56028d08 GN |
11118 | return; |
11119 | ||
11120 | r = kvm_mmu_reload(vcpu); | |
11121 | if (unlikely(r)) | |
11122 | return; | |
11123 | ||
44dd3ffa | 11124 | if (!vcpu->arch.mmu->direct_map && |
d8dd54e0 | 11125 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
fb67e14f XG |
11126 | return; |
11127 | ||
7a02674d | 11128 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
56028d08 GN |
11129 | } |
11130 | ||
af585b92 GN |
11131 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
11132 | { | |
dd03bcaa PX |
11133 | BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU)); |
11134 | ||
af585b92 GN |
11135 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
11136 | } | |
11137 | ||
11138 | static inline u32 kvm_async_pf_next_probe(u32 key) | |
11139 | { | |
dd03bcaa | 11140 | return (key + 1) & (ASYNC_PF_PER_VCPU - 1); |
af585b92 GN |
11141 | } |
11142 | ||
11143 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
11144 | { | |
11145 | u32 key = kvm_async_pf_hash_fn(gfn); | |
11146 | ||
11147 | while (vcpu->arch.apf.gfns[key] != ~0) | |
11148 | key = kvm_async_pf_next_probe(key); | |
11149 | ||
11150 | vcpu->arch.apf.gfns[key] = gfn; | |
11151 | } | |
11152 | ||
11153 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) | |
11154 | { | |
11155 | int i; | |
11156 | u32 key = kvm_async_pf_hash_fn(gfn); | |
11157 | ||
dd03bcaa | 11158 | for (i = 0; i < ASYNC_PF_PER_VCPU && |
c7d28c24 XG |
11159 | (vcpu->arch.apf.gfns[key] != gfn && |
11160 | vcpu->arch.apf.gfns[key] != ~0); i++) | |
af585b92 GN |
11161 | key = kvm_async_pf_next_probe(key); |
11162 | ||
11163 | return key; | |
11164 | } | |
11165 | ||
11166 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
11167 | { | |
11168 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; | |
11169 | } | |
11170 | ||
11171 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
11172 | { | |
11173 | u32 i, j, k; | |
11174 | ||
11175 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); | |
0fd46044 PX |
11176 | |
11177 | if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) | |
11178 | return; | |
11179 | ||
af585b92 GN |
11180 | while (true) { |
11181 | vcpu->arch.apf.gfns[i] = ~0; | |
11182 | do { | |
11183 | j = kvm_async_pf_next_probe(j); | |
11184 | if (vcpu->arch.apf.gfns[j] == ~0) | |
11185 | return; | |
11186 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); | |
11187 | /* | |
11188 | * k lies cyclically in ]i,j] | |
11189 | * | i.k.j | | |
11190 | * |....j i.k.| or |.k..j i...| | |
11191 | */ | |
11192 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); | |
11193 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; | |
11194 | i = j; | |
11195 | } | |
11196 | } | |
11197 | ||
68fd66f1 | 11198 | static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu) |
7c90705b | 11199 | { |
68fd66f1 VK |
11200 | u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT; |
11201 | ||
11202 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, | |
11203 | sizeof(reason)); | |
11204 | } | |
11205 | ||
11206 | static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token) | |
11207 | { | |
2635b5c4 | 11208 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
4e335d9e | 11209 | |
2635b5c4 VK |
11210 | return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
11211 | &token, offset, sizeof(token)); | |
11212 | } | |
11213 | ||
11214 | static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu) | |
11215 | { | |
11216 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); | |
11217 | u32 val; | |
11218 | ||
11219 | if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, | |
11220 | &val, offset, sizeof(val))) | |
11221 | return false; | |
11222 | ||
11223 | return !val; | |
7c90705b GN |
11224 | } |
11225 | ||
1dfdb45e PB |
11226 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
11227 | { | |
11228 | if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu)) | |
11229 | return false; | |
11230 | ||
2635b5c4 | 11231 | if (!kvm_pv_async_pf_enabled(vcpu) || |
b3646477 | 11232 | (vcpu->arch.apf.send_user_only && static_call(kvm_x86_get_cpl)(vcpu) == 0)) |
1dfdb45e PB |
11233 | return false; |
11234 | ||
11235 | return true; | |
11236 | } | |
11237 | ||
11238 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) | |
11239 | { | |
11240 | if (unlikely(!lapic_in_kernel(vcpu) || | |
11241 | kvm_event_needs_reinjection(vcpu) || | |
11242 | vcpu->arch.exception.pending)) | |
11243 | return false; | |
11244 | ||
11245 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) | |
11246 | return false; | |
11247 | ||
11248 | /* | |
11249 | * If interrupts are off we cannot even use an artificial | |
11250 | * halt state. | |
11251 | */ | |
c300ab9f | 11252 | return kvm_arch_interrupt_allowed(vcpu); |
1dfdb45e PB |
11253 | } |
11254 | ||
2a18b7e7 | 11255 | bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
af585b92 GN |
11256 | struct kvm_async_pf *work) |
11257 | { | |
6389ee94 AK |
11258 | struct x86_exception fault; |
11259 | ||
736c291c | 11260 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
af585b92 | 11261 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
7c90705b | 11262 | |
1dfdb45e | 11263 | if (kvm_can_deliver_async_pf(vcpu) && |
68fd66f1 | 11264 | !apf_put_user_notpresent(vcpu)) { |
6389ee94 AK |
11265 | fault.vector = PF_VECTOR; |
11266 | fault.error_code_valid = true; | |
11267 | fault.error_code = 0; | |
11268 | fault.nested_page_fault = false; | |
11269 | fault.address = work->arch.token; | |
adfe20fb | 11270 | fault.async_page_fault = true; |
6389ee94 | 11271 | kvm_inject_page_fault(vcpu, &fault); |
2a18b7e7 | 11272 | return true; |
1dfdb45e PB |
11273 | } else { |
11274 | /* | |
11275 | * It is not possible to deliver a paravirtualized asynchronous | |
11276 | * page fault, but putting the guest in an artificial halt state | |
11277 | * can be beneficial nevertheless: if an interrupt arrives, we | |
11278 | * can deliver it timely and perhaps the guest will schedule | |
11279 | * another process. When the instruction that triggered a page | |
11280 | * fault is retried, hopefully the page will be ready in the host. | |
11281 | */ | |
11282 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
2a18b7e7 | 11283 | return false; |
7c90705b | 11284 | } |
af585b92 GN |
11285 | } |
11286 | ||
11287 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, | |
11288 | struct kvm_async_pf *work) | |
11289 | { | |
2635b5c4 VK |
11290 | struct kvm_lapic_irq irq = { |
11291 | .delivery_mode = APIC_DM_FIXED, | |
11292 | .vector = vcpu->arch.apf.vec | |
11293 | }; | |
6389ee94 | 11294 | |
f2e10669 | 11295 | if (work->wakeup_all) |
7c90705b GN |
11296 | work->arch.token = ~0; /* broadcast wakeup */ |
11297 | else | |
11298 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); | |
736c291c | 11299 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
7c90705b | 11300 | |
2a18b7e7 VK |
11301 | if ((work->wakeup_all || work->notpresent_injected) && |
11302 | kvm_pv_async_pf_enabled(vcpu) && | |
557a961a VK |
11303 | !apf_put_user_ready(vcpu, work->arch.token)) { |
11304 | vcpu->arch.apf.pageready_pending = true; | |
2635b5c4 | 11305 | kvm_apic_set_irq(vcpu, &irq, NULL); |
557a961a | 11306 | } |
2635b5c4 | 11307 | |
e6d53e3b | 11308 | vcpu->arch.apf.halted = false; |
a4fa1635 | 11309 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
7c90705b GN |
11310 | } |
11311 | ||
557a961a VK |
11312 | void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) |
11313 | { | |
11314 | kvm_make_request(KVM_REQ_APF_READY, vcpu); | |
11315 | if (!vcpu->arch.apf.pageready_pending) | |
11316 | kvm_vcpu_kick(vcpu); | |
11317 | } | |
11318 | ||
7c0ade6c | 11319 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
7c90705b | 11320 | { |
2635b5c4 | 11321 | if (!kvm_pv_async_pf_enabled(vcpu)) |
7c90705b GN |
11322 | return true; |
11323 | else | |
2635b5c4 | 11324 | return apf_pageready_slot_free(vcpu); |
af585b92 GN |
11325 | } |
11326 | ||
5544eb9b PB |
11327 | void kvm_arch_start_assignment(struct kvm *kvm) |
11328 | { | |
11329 | atomic_inc(&kvm->arch.assigned_device_count); | |
11330 | } | |
11331 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); | |
11332 | ||
11333 | void kvm_arch_end_assignment(struct kvm *kvm) | |
11334 | { | |
11335 | atomic_dec(&kvm->arch.assigned_device_count); | |
11336 | } | |
11337 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); | |
11338 | ||
11339 | bool kvm_arch_has_assigned_device(struct kvm *kvm) | |
11340 | { | |
11341 | return atomic_read(&kvm->arch.assigned_device_count); | |
11342 | } | |
11343 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); | |
11344 | ||
e0f0bbc5 AW |
11345 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
11346 | { | |
11347 | atomic_inc(&kvm->arch.noncoherent_dma_count); | |
11348 | } | |
11349 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); | |
11350 | ||
11351 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) | |
11352 | { | |
11353 | atomic_dec(&kvm->arch.noncoherent_dma_count); | |
11354 | } | |
11355 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); | |
11356 | ||
11357 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) | |
11358 | { | |
11359 | return atomic_read(&kvm->arch.noncoherent_dma_count); | |
11360 | } | |
11361 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); | |
11362 | ||
14717e20 AW |
11363 | bool kvm_arch_has_irq_bypass(void) |
11364 | { | |
92735b1b | 11365 | return true; |
14717e20 AW |
11366 | } |
11367 | ||
87276880 FW |
11368 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
11369 | struct irq_bypass_producer *prod) | |
11370 | { | |
11371 | struct kvm_kernel_irqfd *irqfd = | |
11372 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
2edd9cb7 | 11373 | int ret; |
87276880 | 11374 | |
14717e20 | 11375 | irqfd->producer = prod; |
2edd9cb7 | 11376 | kvm_arch_start_assignment(irqfd->kvm); |
b3646477 | 11377 | ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm, |
2edd9cb7 ZL |
11378 | prod->irq, irqfd->gsi, 1); |
11379 | ||
11380 | if (ret) | |
11381 | kvm_arch_end_assignment(irqfd->kvm); | |
87276880 | 11382 | |
2edd9cb7 | 11383 | return ret; |
87276880 FW |
11384 | } |
11385 | ||
11386 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, | |
11387 | struct irq_bypass_producer *prod) | |
11388 | { | |
11389 | int ret; | |
11390 | struct kvm_kernel_irqfd *irqfd = | |
11391 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
11392 | ||
87276880 FW |
11393 | WARN_ON(irqfd->producer != prod); |
11394 | irqfd->producer = NULL; | |
11395 | ||
11396 | /* | |
11397 | * When producer of consumer is unregistered, we change back to | |
11398 | * remapped mode, so we can re-use the current implementation | |
bb3541f1 | 11399 | * when the irq is masked/disabled or the consumer side (KVM |
87276880 FW |
11400 | * int this case doesn't want to receive the interrupts. |
11401 | */ | |
b3646477 | 11402 | ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
87276880 FW |
11403 | if (ret) |
11404 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" | |
11405 | " fails: %d\n", irqfd->consumer.token, ret); | |
2edd9cb7 ZL |
11406 | |
11407 | kvm_arch_end_assignment(irqfd->kvm); | |
87276880 FW |
11408 | } |
11409 | ||
11410 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, | |
11411 | uint32_t guest_irq, bool set) | |
11412 | { | |
b3646477 | 11413 | return static_call(kvm_x86_update_pi_irte)(kvm, host_irq, guest_irq, set); |
87276880 FW |
11414 | } |
11415 | ||
52004014 FW |
11416 | bool kvm_vector_hashing_enabled(void) |
11417 | { | |
11418 | return vector_hashing; | |
11419 | } | |
52004014 | 11420 | |
2d5ba19b MT |
11421 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
11422 | { | |
11423 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; | |
11424 | } | |
11425 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); | |
11426 | ||
841c2be0 ML |
11427 | |
11428 | int kvm_spec_ctrl_test_value(u64 value) | |
6441fa61 | 11429 | { |
841c2be0 ML |
11430 | /* |
11431 | * test that setting IA32_SPEC_CTRL to given value | |
11432 | * is allowed by the host processor | |
11433 | */ | |
6441fa61 | 11434 | |
841c2be0 ML |
11435 | u64 saved_value; |
11436 | unsigned long flags; | |
11437 | int ret = 0; | |
6441fa61 | 11438 | |
841c2be0 | 11439 | local_irq_save(flags); |
6441fa61 | 11440 | |
841c2be0 ML |
11441 | if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value)) |
11442 | ret = 1; | |
11443 | else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value)) | |
11444 | ret = 1; | |
11445 | else | |
11446 | wrmsrl(MSR_IA32_SPEC_CTRL, saved_value); | |
6441fa61 | 11447 | |
841c2be0 | 11448 | local_irq_restore(flags); |
6441fa61 | 11449 | |
841c2be0 | 11450 | return ret; |
6441fa61 | 11451 | } |
841c2be0 | 11452 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value); |
2d5ba19b | 11453 | |
89786147 MG |
11454 | void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code) |
11455 | { | |
11456 | struct x86_exception fault; | |
19cf4b7e PB |
11457 | u32 access = error_code & |
11458 | (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK); | |
89786147 MG |
11459 | |
11460 | if (!(error_code & PFERR_PRESENT_MASK) || | |
19cf4b7e | 11461 | vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) { |
89786147 MG |
11462 | /* |
11463 | * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page | |
11464 | * tables probably do not match the TLB. Just proceed | |
11465 | * with the error code that the processor gave. | |
11466 | */ | |
11467 | fault.vector = PF_VECTOR; | |
11468 | fault.error_code_valid = true; | |
11469 | fault.error_code = error_code; | |
11470 | fault.nested_page_fault = false; | |
11471 | fault.address = gva; | |
11472 | } | |
11473 | vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); | |
6441fa61 | 11474 | } |
89786147 | 11475 | EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error); |
2d5ba19b | 11476 | |
3f3393b3 BM |
11477 | /* |
11478 | * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns | |
11479 | * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value | |
11480 | * indicates whether exit to userspace is needed. | |
11481 | */ | |
11482 | int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r, | |
11483 | struct x86_exception *e) | |
11484 | { | |
11485 | if (r == X86EMUL_PROPAGATE_FAULT) { | |
11486 | kvm_inject_emulated_page_fault(vcpu, e); | |
11487 | return 1; | |
11488 | } | |
11489 | ||
11490 | /* | |
11491 | * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED | |
11492 | * while handling a VMX instruction KVM could've handled the request | |
11493 | * correctly by exiting to userspace and performing I/O but there | |
11494 | * doesn't seem to be a real use-case behind such requests, just return | |
11495 | * KVM_EXIT_INTERNAL_ERROR for now. | |
11496 | */ | |
11497 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
11498 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
11499 | vcpu->run->internal.ndata = 0; | |
11500 | ||
11501 | return 0; | |
11502 | } | |
11503 | EXPORT_SYMBOL_GPL(kvm_handle_memory_failure); | |
11504 | ||
9715092f BM |
11505 | int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva) |
11506 | { | |
11507 | bool pcid_enabled; | |
11508 | struct x86_exception e; | |
11509 | unsigned i; | |
11510 | unsigned long roots_to_free = 0; | |
11511 | struct { | |
11512 | u64 pcid; | |
11513 | u64 gla; | |
11514 | } operand; | |
11515 | int r; | |
11516 | ||
11517 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); | |
11518 | if (r != X86EMUL_CONTINUE) | |
11519 | return kvm_handle_memory_failure(vcpu, r, &e); | |
11520 | ||
11521 | if (operand.pcid >> 12 != 0) { | |
11522 | kvm_inject_gp(vcpu, 0); | |
11523 | return 1; | |
11524 | } | |
11525 | ||
11526 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); | |
11527 | ||
11528 | switch (type) { | |
11529 | case INVPCID_TYPE_INDIV_ADDR: | |
11530 | if ((!pcid_enabled && (operand.pcid != 0)) || | |
11531 | is_noncanonical_address(operand.gla, vcpu)) { | |
11532 | kvm_inject_gp(vcpu, 0); | |
11533 | return 1; | |
11534 | } | |
11535 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); | |
11536 | return kvm_skip_emulated_instruction(vcpu); | |
11537 | ||
11538 | case INVPCID_TYPE_SINGLE_CTXT: | |
11539 | if (!pcid_enabled && (operand.pcid != 0)) { | |
11540 | kvm_inject_gp(vcpu, 0); | |
11541 | return 1; | |
11542 | } | |
11543 | ||
11544 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { | |
11545 | kvm_mmu_sync_roots(vcpu); | |
11546 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
11547 | } | |
11548 | ||
11549 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) | |
11550 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd) | |
11551 | == operand.pcid) | |
11552 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); | |
11553 | ||
11554 | kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); | |
11555 | /* | |
11556 | * If neither the current cr3 nor any of the prev_roots use the | |
11557 | * given PCID, then nothing needs to be done here because a | |
11558 | * resync will happen anyway before switching to any other CR3. | |
11559 | */ | |
11560 | ||
11561 | return kvm_skip_emulated_instruction(vcpu); | |
11562 | ||
11563 | case INVPCID_TYPE_ALL_NON_GLOBAL: | |
11564 | /* | |
11565 | * Currently, KVM doesn't mark global entries in the shadow | |
11566 | * page tables, so a non-global flush just degenerates to a | |
11567 | * global flush. If needed, we could optimize this later by | |
11568 | * keeping track of global entries in shadow page tables. | |
11569 | */ | |
11570 | ||
11571 | fallthrough; | |
11572 | case INVPCID_TYPE_ALL_INCL_GLOBAL: | |
f66c53b3 | 11573 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
9715092f BM |
11574 | return kvm_skip_emulated_instruction(vcpu); |
11575 | ||
11576 | default: | |
11577 | BUG(); /* We have already checked above that type <= 3 */ | |
11578 | } | |
11579 | } | |
11580 | EXPORT_SYMBOL_GPL(kvm_handle_invpcid); | |
11581 | ||
8f423a80 TL |
11582 | static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu) |
11583 | { | |
11584 | struct kvm_run *run = vcpu->run; | |
11585 | struct kvm_mmio_fragment *frag; | |
11586 | unsigned int len; | |
11587 | ||
11588 | BUG_ON(!vcpu->mmio_needed); | |
11589 | ||
11590 | /* Complete previous fragment */ | |
11591 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; | |
11592 | len = min(8u, frag->len); | |
11593 | if (!vcpu->mmio_is_write) | |
11594 | memcpy(frag->data, run->mmio.data, len); | |
11595 | ||
11596 | if (frag->len <= 8) { | |
11597 | /* Switch to the next fragment. */ | |
11598 | frag++; | |
11599 | vcpu->mmio_cur_fragment++; | |
11600 | } else { | |
11601 | /* Go forward to the next mmio piece. */ | |
11602 | frag->data += len; | |
11603 | frag->gpa += len; | |
11604 | frag->len -= len; | |
11605 | } | |
11606 | ||
11607 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { | |
11608 | vcpu->mmio_needed = 0; | |
11609 | ||
11610 | // VMG change, at this point, we're always done | |
11611 | // RIP has already been advanced | |
11612 | return 1; | |
11613 | } | |
11614 | ||
11615 | // More MMIO is needed | |
11616 | run->mmio.phys_addr = frag->gpa; | |
11617 | run->mmio.len = min(8u, frag->len); | |
11618 | run->mmio.is_write = vcpu->mmio_is_write; | |
11619 | if (run->mmio.is_write) | |
11620 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); | |
11621 | run->exit_reason = KVM_EXIT_MMIO; | |
11622 | ||
11623 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
11624 | ||
11625 | return 0; | |
11626 | } | |
11627 | ||
11628 | int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, | |
11629 | void *data) | |
11630 | { | |
11631 | int handled; | |
11632 | struct kvm_mmio_fragment *frag; | |
11633 | ||
11634 | if (!data) | |
11635 | return -EINVAL; | |
11636 | ||
11637 | handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data); | |
11638 | if (handled == bytes) | |
11639 | return 1; | |
11640 | ||
11641 | bytes -= handled; | |
11642 | gpa += handled; | |
11643 | data += handled; | |
11644 | ||
11645 | /*TODO: Check if need to increment number of frags */ | |
11646 | frag = vcpu->mmio_fragments; | |
11647 | vcpu->mmio_nr_fragments = 1; | |
11648 | frag->len = bytes; | |
11649 | frag->gpa = gpa; | |
11650 | frag->data = data; | |
11651 | ||
11652 | vcpu->mmio_needed = 1; | |
11653 | vcpu->mmio_cur_fragment = 0; | |
11654 | ||
11655 | vcpu->run->mmio.phys_addr = gpa; | |
11656 | vcpu->run->mmio.len = min(8u, frag->len); | |
11657 | vcpu->run->mmio.is_write = 1; | |
11658 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); | |
11659 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
11660 | ||
11661 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
11662 | ||
11663 | return 0; | |
11664 | } | |
11665 | EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_write); | |
11666 | ||
11667 | int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, | |
11668 | void *data) | |
11669 | { | |
11670 | int handled; | |
11671 | struct kvm_mmio_fragment *frag; | |
11672 | ||
11673 | if (!data) | |
11674 | return -EINVAL; | |
11675 | ||
11676 | handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data); | |
11677 | if (handled == bytes) | |
11678 | return 1; | |
11679 | ||
11680 | bytes -= handled; | |
11681 | gpa += handled; | |
11682 | data += handled; | |
11683 | ||
11684 | /*TODO: Check if need to increment number of frags */ | |
11685 | frag = vcpu->mmio_fragments; | |
11686 | vcpu->mmio_nr_fragments = 1; | |
11687 | frag->len = bytes; | |
11688 | frag->gpa = gpa; | |
11689 | frag->data = data; | |
11690 | ||
11691 | vcpu->mmio_needed = 1; | |
11692 | vcpu->mmio_cur_fragment = 0; | |
11693 | ||
11694 | vcpu->run->mmio.phys_addr = gpa; | |
11695 | vcpu->run->mmio.len = min(8u, frag->len); | |
11696 | vcpu->run->mmio.is_write = 0; | |
11697 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
11698 | ||
11699 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
11700 | ||
11701 | return 0; | |
11702 | } | |
11703 | EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read); | |
11704 | ||
7ed9abfe TL |
11705 | static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) |
11706 | { | |
11707 | memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data, | |
11708 | vcpu->arch.pio.count * vcpu->arch.pio.size); | |
11709 | vcpu->arch.pio.count = 0; | |
11710 | ||
11711 | return 1; | |
11712 | } | |
11713 | ||
11714 | static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size, | |
11715 | unsigned int port, void *data, unsigned int count) | |
11716 | { | |
11717 | int ret; | |
11718 | ||
11719 | ret = emulator_pio_out_emulated(vcpu->arch.emulate_ctxt, size, port, | |
11720 | data, count); | |
11721 | if (ret) | |
11722 | return ret; | |
11723 | ||
11724 | vcpu->arch.pio.count = 0; | |
11725 | ||
11726 | return 0; | |
11727 | } | |
11728 | ||
11729 | static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, | |
11730 | unsigned int port, void *data, unsigned int count) | |
11731 | { | |
11732 | int ret; | |
11733 | ||
11734 | ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port, | |
11735 | data, count); | |
11736 | if (ret) { | |
11737 | vcpu->arch.pio.count = 0; | |
11738 | } else { | |
11739 | vcpu->arch.guest_ins_data = data; | |
11740 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; | |
11741 | } | |
11742 | ||
11743 | return 0; | |
11744 | } | |
11745 | ||
11746 | int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, | |
11747 | unsigned int port, void *data, unsigned int count, | |
11748 | int in) | |
11749 | { | |
11750 | return in ? kvm_sev_es_ins(vcpu, size, port, data, count) | |
11751 | : kvm_sev_es_outs(vcpu, size, port, data, count); | |
11752 | } | |
11753 | EXPORT_SYMBOL_GPL(kvm_sev_es_string_io); | |
11754 | ||
d95df951 | 11755 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry); |
229456fc | 11756 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
931c33b1 | 11757 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
229456fc MT |
11758 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
11759 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); | |
11760 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); | |
11761 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); | |
0ac406de | 11762 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
d8cabddf | 11763 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
17897f36 | 11764 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
236649de | 11765 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
5497b955 | 11766 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
ec1ff790 | 11767 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
532a46b9 | 11768 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
2e554e8d | 11769 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
489223ed | 11770 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
4f75bcc3 | 11771 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
843e4330 | 11772 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
efc64404 | 11773 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
18f40c53 SS |
11774 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
11775 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); | |
ab56f8e6 | 11776 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
24bbf74c | 11777 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request); |
d523ab6b TL |
11778 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); |
11779 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); | |
59e38b58 TL |
11780 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); |
11781 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); |