]>
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 <[email protected]> | |
14 | * Yaniv Kamay <[email protected]> | |
4d5c5d0f BAY |
15 | * Amit Shah <[email protected]> |
16 | * Ben-Ami Yassour <[email protected]> | |
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> |
7d62874f | 61 | #include <linux/suspend.h> |
3905f9ad | 62 | |
aec51dc4 | 63 | #include <trace/events/kvm.h> |
2ed152af | 64 | |
24f1e32c | 65 | #include <asm/debugreg.h> |
d825ed0a | 66 | #include <asm/msr.h> |
a5f61300 | 67 | #include <asm/desc.h> |
890ca9ae | 68 | #include <asm/mce.h> |
784a4661 | 69 | #include <asm/pkru.h> |
f89e32e0 | 70 | #include <linux/kernel_stat.h> |
a0ff0611 TG |
71 | #include <asm/fpu/api.h> |
72 | #include <asm/fpu/xcr.h> | |
73 | #include <asm/fpu/xstate.h> | |
1d5f066e | 74 | #include <asm/pvclock.h> |
217fc9cf | 75 | #include <asm/div64.h> |
efc64404 | 76 | #include <asm/irq_remapping.h> |
b0c39dc6 | 77 | #include <asm/mshyperv.h> |
0092e434 | 78 | #include <asm/hypervisor.h> |
9715092f | 79 | #include <asm/tlbflush.h> |
bf8c55d8 | 80 | #include <asm/intel_pt.h> |
b3dc0695 | 81 | #include <asm/emulate_prefix.h> |
fe7e9488 | 82 | #include <asm/sgx.h> |
dd2cb348 | 83 | #include <clocksource/hyperv_timer.h> |
043405e1 | 84 | |
d1898b73 DH |
85 | #define CREATE_TRACE_POINTS |
86 | #include "trace.h" | |
87 | ||
313a3dc7 | 88 | #define MAX_IO_MSRS 256 |
890ca9ae | 89 | #define KVM_MAX_MCE_BANKS 32 |
c45dcc71 AR |
90 | u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; |
91 | EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); | |
890ca9ae | 92 | |
6e37ec88 SC |
93 | #define ERR_PTR_USR(e) ((void __user *)ERR_PTR(e)) |
94 | ||
0f65dd70 | 95 | #define emul_to_vcpu(ctxt) \ |
c9b8b07c | 96 | ((struct kvm_vcpu *)(ctxt)->vcpu) |
0f65dd70 | 97 | |
50a37eb4 JR |
98 | /* EFER defaults: |
99 | * - enable syscall per default because its emulated by KVM | |
100 | * - enable LME and LMA per default on 64 bit KVM | |
101 | */ | |
102 | #ifdef CONFIG_X86_64 | |
1260edbe LJ |
103 | static |
104 | u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); | |
50a37eb4 | 105 | #else |
1260edbe | 106 | static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); |
50a37eb4 | 107 | #endif |
313a3dc7 | 108 | |
b11306b5 SC |
109 | static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; |
110 | ||
0dbb1123 AK |
111 | #define KVM_EXIT_HYPERCALL_VALID_MASK (1 << KVM_HC_MAP_GPA_RANGE) |
112 | ||
ba7bb663 DD |
113 | #define KVM_CAP_PMU_VALID_MASK KVM_PMU_CAP_DISABLE |
114 | ||
c519265f RK |
115 | #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ |
116 | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) | |
37131313 | 117 | |
cb142eb7 | 118 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
7460fb4a | 119 | static void process_nmi(struct kvm_vcpu *vcpu); |
1f7becf1 | 120 | static void process_smi(struct kvm_vcpu *vcpu); |
ee2cd4b7 | 121 | static void enter_smm(struct kvm_vcpu *vcpu); |
6addfc42 | 122 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); |
01643c51 KH |
123 | static void store_regs(struct kvm_vcpu *vcpu); |
124 | static int sync_regs(struct kvm_vcpu *vcpu); | |
d2f7d498 | 125 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu); |
674eea0f | 126 | |
6dba9403 ML |
127 | static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); |
128 | static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); | |
129 | ||
afaf0b2f | 130 | struct kvm_x86_ops kvm_x86_ops __read_mostly; |
97896d04 | 131 | |
9af5471b JB |
132 | #define KVM_X86_OP(func) \ |
133 | DEFINE_STATIC_CALL_NULL(kvm_x86_##func, \ | |
134 | *(((struct kvm_x86_ops *)0)->func)); | |
e4fc23ba | 135 | #define KVM_X86_OP_OPTIONAL KVM_X86_OP |
5be2226f | 136 | #define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP |
9af5471b JB |
137 | #include <asm/kvm-x86-ops.h> |
138 | EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits); | |
139 | EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg); | |
9af5471b | 140 | |
893590c7 | 141 | static bool __read_mostly ignore_msrs = 0; |
476bc001 | 142 | module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR); |
ed85c068 | 143 | |
d855066f | 144 | bool __read_mostly report_ignored_msrs = true; |
fab0aa3b | 145 | module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); |
d855066f | 146 | EXPORT_SYMBOL_GPL(report_ignored_msrs); |
fab0aa3b | 147 | |
4c27625b | 148 | unsigned int min_timer_period_us = 200; |
9ed96e87 MT |
149 | module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); |
150 | ||
630994b3 MT |
151 | static bool __read_mostly kvmclock_periodic_sync = true; |
152 | module_param(kvmclock_periodic_sync, bool, S_IRUGO); | |
153 | ||
893590c7 | 154 | bool __read_mostly kvm_has_tsc_control; |
92a1f12d | 155 | EXPORT_SYMBOL_GPL(kvm_has_tsc_control); |
893590c7 | 156 | u32 __read_mostly kvm_max_guest_tsc_khz; |
92a1f12d | 157 | EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz); |
bc9b961b HZ |
158 | u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; |
159 | EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits); | |
160 | u64 __read_mostly kvm_max_tsc_scaling_ratio; | |
161 | EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio); | |
64672c95 YJ |
162 | u64 __read_mostly kvm_default_tsc_scaling_ratio; |
163 | EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); | |
fe6b6bc8 CQ |
164 | bool __read_mostly kvm_has_bus_lock_exit; |
165 | EXPORT_SYMBOL_GPL(kvm_has_bus_lock_exit); | |
92a1f12d | 166 | |
cc578287 | 167 | /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ |
893590c7 | 168 | static u32 __read_mostly tsc_tolerance_ppm = 250; |
cc578287 ZA |
169 | module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); |
170 | ||
c3941d9e SC |
171 | /* |
172 | * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables | |
d9f6e12f | 173 | * adaptive tuning starting from default advancement of 1000ns. '0' disables |
c3941d9e | 174 | * advancement entirely. Any other value is used as-is and disables adaptive |
d9f6e12f | 175 | * tuning, i.e. allows privileged userspace to set an exact advancement time. |
c3941d9e SC |
176 | */ |
177 | static int __read_mostly lapic_timer_advance_ns = -1; | |
0e6edceb | 178 | module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR); |
d0659d94 | 179 | |
52004014 FW |
180 | static bool __read_mostly vector_hashing = true; |
181 | module_param(vector_hashing, bool, S_IRUGO); | |
182 | ||
c4ae60e4 LA |
183 | bool __read_mostly enable_vmware_backdoor = false; |
184 | module_param(enable_vmware_backdoor, bool, S_IRUGO); | |
185 | EXPORT_SYMBOL_GPL(enable_vmware_backdoor); | |
186 | ||
6c86eedc WL |
187 | static bool __read_mostly force_emulation_prefix = false; |
188 | module_param(force_emulation_prefix, bool, S_IRUGO); | |
189 | ||
0c5f81da WL |
190 | int __read_mostly pi_inject_timer = -1; |
191 | module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); | |
192 | ||
4732f244 LX |
193 | /* Enable/disable PMU virtualization */ |
194 | bool __read_mostly enable_pmu = true; | |
195 | EXPORT_SYMBOL_GPL(enable_pmu); | |
196 | module_param(enable_pmu, bool, 0444); | |
197 | ||
cb00a70b | 198 | bool __read_mostly eager_page_split = true; |
a3fe5dbd DM |
199 | module_param(eager_page_split, bool, 0644); |
200 | ||
7e34fbd0 SC |
201 | /* |
202 | * Restoring the host value for MSRs that are only consumed when running in | |
203 | * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU | |
204 | * returns to userspace, i.e. the kernel can run with the guest's value. | |
205 | */ | |
206 | #define KVM_MAX_NR_USER_RETURN_MSRS 16 | |
18863bdd | 207 | |
7e34fbd0 | 208 | struct kvm_user_return_msrs { |
18863bdd AK |
209 | struct user_return_notifier urn; |
210 | bool registered; | |
7e34fbd0 | 211 | struct kvm_user_return_msr_values { |
2bf78fa7 SY |
212 | u64 host; |
213 | u64 curr; | |
7e34fbd0 | 214 | } values[KVM_MAX_NR_USER_RETURN_MSRS]; |
18863bdd AK |
215 | }; |
216 | ||
9cc39a5a SC |
217 | u32 __read_mostly kvm_nr_uret_msrs; |
218 | EXPORT_SYMBOL_GPL(kvm_nr_uret_msrs); | |
219 | static u32 __read_mostly kvm_uret_msrs_list[KVM_MAX_NR_USER_RETURN_MSRS]; | |
7e34fbd0 | 220 | static struct kvm_user_return_msrs __percpu *user_return_msrs; |
18863bdd | 221 | |
cfc48181 SC |
222 | #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ |
223 | | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ | |
224 | | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | |
86aff7a4 | 225 | | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) |
cfc48181 | 226 | |
91661989 SC |
227 | u64 __read_mostly host_efer; |
228 | EXPORT_SYMBOL_GPL(host_efer); | |
229 | ||
b96e6506 | 230 | bool __read_mostly allow_smaller_maxphyaddr = 0; |
3edd6839 MG |
231 | EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr); |
232 | ||
fdf513e3 VK |
233 | bool __read_mostly enable_apicv = true; |
234 | EXPORT_SYMBOL_GPL(enable_apicv); | |
235 | ||
86137773 TL |
236 | u64 __read_mostly host_xss; |
237 | EXPORT_SYMBOL_GPL(host_xss); | |
408e9a31 PB |
238 | u64 __read_mostly supported_xss; |
239 | EXPORT_SYMBOL_GPL(supported_xss); | |
139a12cf | 240 | |
fcfe1bae JZ |
241 | const struct _kvm_stats_desc kvm_vm_stats_desc[] = { |
242 | KVM_GENERIC_VM_STATS(), | |
243 | STATS_DESC_COUNTER(VM, mmu_shadow_zapped), | |
244 | STATS_DESC_COUNTER(VM, mmu_pte_write), | |
245 | STATS_DESC_COUNTER(VM, mmu_pde_zapped), | |
246 | STATS_DESC_COUNTER(VM, mmu_flooded), | |
247 | STATS_DESC_COUNTER(VM, mmu_recycled), | |
248 | STATS_DESC_COUNTER(VM, mmu_cache_miss), | |
249 | STATS_DESC_ICOUNTER(VM, mmu_unsync), | |
71f51d2c MZ |
250 | STATS_DESC_ICOUNTER(VM, pages_4k), |
251 | STATS_DESC_ICOUNTER(VM, pages_2m), | |
252 | STATS_DESC_ICOUNTER(VM, pages_1g), | |
fcfe1bae | 253 | STATS_DESC_ICOUNTER(VM, nx_lpage_splits), |
ec1cf69c | 254 | STATS_DESC_PCOUNTER(VM, max_mmu_rmap_size), |
bc9e9e67 | 255 | STATS_DESC_PCOUNTER(VM, max_mmu_page_hash_collisions) |
fcfe1bae | 256 | }; |
fcfe1bae JZ |
257 | |
258 | const struct kvm_stats_header kvm_vm_stats_header = { | |
259 | .name_size = KVM_STATS_NAME_SIZE, | |
260 | .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), | |
261 | .id_offset = sizeof(struct kvm_stats_header), | |
262 | .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, | |
263 | .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + | |
264 | sizeof(kvm_vm_stats_desc), | |
265 | }; | |
266 | ||
ce55c049 JZ |
267 | const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { |
268 | KVM_GENERIC_VCPU_STATS(), | |
269 | STATS_DESC_COUNTER(VCPU, pf_fixed), | |
270 | STATS_DESC_COUNTER(VCPU, pf_guest), | |
271 | STATS_DESC_COUNTER(VCPU, tlb_flush), | |
272 | STATS_DESC_COUNTER(VCPU, invlpg), | |
273 | STATS_DESC_COUNTER(VCPU, exits), | |
274 | STATS_DESC_COUNTER(VCPU, io_exits), | |
275 | STATS_DESC_COUNTER(VCPU, mmio_exits), | |
276 | STATS_DESC_COUNTER(VCPU, signal_exits), | |
277 | STATS_DESC_COUNTER(VCPU, irq_window_exits), | |
278 | STATS_DESC_COUNTER(VCPU, nmi_window_exits), | |
279 | STATS_DESC_COUNTER(VCPU, l1d_flush), | |
280 | STATS_DESC_COUNTER(VCPU, halt_exits), | |
281 | STATS_DESC_COUNTER(VCPU, request_irq_exits), | |
282 | STATS_DESC_COUNTER(VCPU, irq_exits), | |
283 | STATS_DESC_COUNTER(VCPU, host_state_reload), | |
284 | STATS_DESC_COUNTER(VCPU, fpu_reload), | |
285 | STATS_DESC_COUNTER(VCPU, insn_emulation), | |
286 | STATS_DESC_COUNTER(VCPU, insn_emulation_fail), | |
287 | STATS_DESC_COUNTER(VCPU, hypercalls), | |
288 | STATS_DESC_COUNTER(VCPU, irq_injections), | |
289 | STATS_DESC_COUNTER(VCPU, nmi_injections), | |
290 | STATS_DESC_COUNTER(VCPU, req_event), | |
291 | STATS_DESC_COUNTER(VCPU, nested_run), | |
292 | STATS_DESC_COUNTER(VCPU, directed_yield_attempted), | |
293 | STATS_DESC_COUNTER(VCPU, directed_yield_successful), | |
294 | STATS_DESC_ICOUNTER(VCPU, guest_mode) | |
295 | }; | |
ce55c049 JZ |
296 | |
297 | const struct kvm_stats_header kvm_vcpu_stats_header = { | |
298 | .name_size = KVM_STATS_NAME_SIZE, | |
299 | .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc), | |
300 | .id_offset = sizeof(struct kvm_stats_header), | |
301 | .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, | |
302 | .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + | |
303 | sizeof(kvm_vcpu_stats_desc), | |
304 | }; | |
305 | ||
2acf923e | 306 | u64 __read_mostly host_xcr0; |
cfc48181 SC |
307 | u64 __read_mostly supported_xcr0; |
308 | EXPORT_SYMBOL_GPL(supported_xcr0); | |
2acf923e | 309 | |
c9b8b07c SC |
310 | static struct kmem_cache *x86_emulator_cache; |
311 | ||
6abe9c13 PX |
312 | /* |
313 | * When called, it means the previous get/set msr reached an invalid msr. | |
cc4cb017 | 314 | * Return true if we want to ignore/silent this failed msr access. |
6abe9c13 | 315 | */ |
d632826f | 316 | static bool kvm_msr_ignored_check(u32 msr, u64 data, bool write) |
6abe9c13 PX |
317 | { |
318 | const char *op = write ? "wrmsr" : "rdmsr"; | |
319 | ||
320 | if (ignore_msrs) { | |
321 | if (report_ignored_msrs) | |
d383b314 TI |
322 | kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n", |
323 | op, msr, data); | |
6abe9c13 | 324 | /* Mask the error */ |
cc4cb017 | 325 | return true; |
6abe9c13 | 326 | } else { |
d383b314 TI |
327 | kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n", |
328 | op, msr, data); | |
cc4cb017 | 329 | return false; |
6abe9c13 PX |
330 | } |
331 | } | |
332 | ||
c9b8b07c SC |
333 | static struct kmem_cache *kvm_alloc_emulator_cache(void) |
334 | { | |
06add254 SC |
335 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
336 | unsigned int size = sizeof(struct x86_emulate_ctxt); | |
337 | ||
338 | return kmem_cache_create_usercopy("x86_emulator", size, | |
c9b8b07c | 339 | __alignof__(struct x86_emulate_ctxt), |
06add254 SC |
340 | SLAB_ACCOUNT, useroffset, |
341 | size - useroffset, NULL); | |
c9b8b07c SC |
342 | } |
343 | ||
b6785def | 344 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
d6aa1000 | 345 | |
af585b92 GN |
346 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
347 | { | |
348 | int i; | |
dd03bcaa | 349 | for (i = 0; i < ASYNC_PF_PER_VCPU; i++) |
af585b92 GN |
350 | vcpu->arch.apf.gfns[i] = ~0; |
351 | } | |
352 | ||
18863bdd AK |
353 | static void kvm_on_user_return(struct user_return_notifier *urn) |
354 | { | |
355 | unsigned slot; | |
7e34fbd0 SC |
356 | struct kvm_user_return_msrs *msrs |
357 | = container_of(urn, struct kvm_user_return_msrs, urn); | |
358 | struct kvm_user_return_msr_values *values; | |
1650b4eb IA |
359 | unsigned long flags; |
360 | ||
361 | /* | |
362 | * Disabling irqs at this point since the following code could be | |
363 | * interrupted and executed through kvm_arch_hardware_disable() | |
364 | */ | |
365 | local_irq_save(flags); | |
7e34fbd0 SC |
366 | if (msrs->registered) { |
367 | msrs->registered = false; | |
1650b4eb IA |
368 | user_return_notifier_unregister(urn); |
369 | } | |
370 | local_irq_restore(flags); | |
9cc39a5a | 371 | for (slot = 0; slot < kvm_nr_uret_msrs; ++slot) { |
7e34fbd0 | 372 | values = &msrs->values[slot]; |
2bf78fa7 | 373 | if (values->host != values->curr) { |
9cc39a5a | 374 | wrmsrl(kvm_uret_msrs_list[slot], values->host); |
2bf78fa7 | 375 | values->curr = values->host; |
18863bdd AK |
376 | } |
377 | } | |
18863bdd AK |
378 | } |
379 | ||
e5fda4bb | 380 | static int kvm_probe_user_return_msr(u32 msr) |
5104d7ff SC |
381 | { |
382 | u64 val; | |
383 | int ret; | |
384 | ||
385 | preempt_disable(); | |
386 | ret = rdmsrl_safe(msr, &val); | |
387 | if (ret) | |
388 | goto out; | |
389 | ret = wrmsrl_safe(msr, val); | |
390 | out: | |
391 | preempt_enable(); | |
392 | return ret; | |
393 | } | |
5104d7ff | 394 | |
e5fda4bb | 395 | int kvm_add_user_return_msr(u32 msr) |
2bf78fa7 | 396 | { |
e5fda4bb SC |
397 | BUG_ON(kvm_nr_uret_msrs >= KVM_MAX_NR_USER_RETURN_MSRS); |
398 | ||
399 | if (kvm_probe_user_return_msr(msr)) | |
400 | return -1; | |
401 | ||
402 | kvm_uret_msrs_list[kvm_nr_uret_msrs] = msr; | |
403 | return kvm_nr_uret_msrs++; | |
18863bdd | 404 | } |
e5fda4bb | 405 | EXPORT_SYMBOL_GPL(kvm_add_user_return_msr); |
18863bdd | 406 | |
8ea8b8d6 SC |
407 | int kvm_find_user_return_msr(u32 msr) |
408 | { | |
409 | int i; | |
410 | ||
9cc39a5a SC |
411 | for (i = 0; i < kvm_nr_uret_msrs; ++i) { |
412 | if (kvm_uret_msrs_list[i] == msr) | |
8ea8b8d6 SC |
413 | return i; |
414 | } | |
415 | return -1; | |
416 | } | |
417 | EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); | |
418 | ||
7e34fbd0 | 419 | static void kvm_user_return_msr_cpu_online(void) |
18863bdd | 420 | { |
05c19c2f | 421 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 422 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
05c19c2f SC |
423 | u64 value; |
424 | int i; | |
18863bdd | 425 | |
9cc39a5a SC |
426 | for (i = 0; i < kvm_nr_uret_msrs; ++i) { |
427 | rdmsrl_safe(kvm_uret_msrs_list[i], &value); | |
7e34fbd0 SC |
428 | msrs->values[i].host = value; |
429 | msrs->values[i].curr = value; | |
05c19c2f | 430 | } |
18863bdd AK |
431 | } |
432 | ||
7e34fbd0 | 433 | int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) |
18863bdd | 434 | { |
013f6a5d | 435 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 436 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
8b3c3104 | 437 | int err; |
18863bdd | 438 | |
7e34fbd0 SC |
439 | value = (value & mask) | (msrs->values[slot].host & ~mask); |
440 | if (value == msrs->values[slot].curr) | |
8b3c3104 | 441 | return 0; |
9cc39a5a | 442 | err = wrmsrl_safe(kvm_uret_msrs_list[slot], value); |
8b3c3104 AH |
443 | if (err) |
444 | return 1; | |
445 | ||
7e34fbd0 SC |
446 | msrs->values[slot].curr = value; |
447 | if (!msrs->registered) { | |
448 | msrs->urn.on_user_return = kvm_on_user_return; | |
449 | user_return_notifier_register(&msrs->urn); | |
450 | msrs->registered = true; | |
18863bdd | 451 | } |
8b3c3104 | 452 | return 0; |
18863bdd | 453 | } |
7e34fbd0 | 454 | EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); |
18863bdd | 455 | |
13a34e06 | 456 | static void drop_user_return_notifiers(void) |
3548bab5 | 457 | { |
013f6a5d | 458 | unsigned int cpu = smp_processor_id(); |
7e34fbd0 | 459 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
3548bab5 | 460 | |
7e34fbd0 SC |
461 | if (msrs->registered) |
462 | kvm_on_user_return(&msrs->urn); | |
3548bab5 AK |
463 | } |
464 | ||
6866b83e CO |
465 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
466 | { | |
8a5a87d9 | 467 | return vcpu->arch.apic_base; |
6866b83e CO |
468 | } |
469 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
470 | ||
58871649 JM |
471 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
472 | { | |
473 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); | |
474 | } | |
475 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); | |
476 | ||
58cb628d JK |
477 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
478 | { | |
58871649 JM |
479 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
480 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); | |
a8ac864a | 481 | u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | |
d6321d49 | 482 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); |
58cb628d | 483 | |
58871649 | 484 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
58cb628d | 485 | return 1; |
58871649 JM |
486 | if (!msr_info->host_initiated) { |
487 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) | |
488 | return 1; | |
489 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) | |
490 | return 1; | |
491 | } | |
58cb628d JK |
492 | |
493 | kvm_lapic_set_base(vcpu, msr_info->data); | |
4abaffce | 494 | kvm_recalculate_apic_map(vcpu->kvm); |
58cb628d | 495 | return 0; |
6866b83e CO |
496 | } |
497 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
498 | ||
ad0577c3 SC |
499 | /* |
500 | * Handle a fault on a hardware virtualization (VMX or SVM) instruction. | |
501 | * | |
502 | * Hardware virtualization extension instructions may fault if a reboot turns | |
503 | * off virtualization while processes are running. Usually after catching the | |
504 | * fault we just panic; during reboot instead the instruction is ignored. | |
505 | */ | |
506 | noinstr void kvm_spurious_fault(void) | |
e3ba45b8 GL |
507 | { |
508 | /* Fault while not rebooting. We want the trace. */ | |
b4fdcf60 | 509 | BUG_ON(!kvm_rebooting); |
e3ba45b8 GL |
510 | } |
511 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); | |
512 | ||
3fd28fce ED |
513 | #define EXCPT_BENIGN 0 |
514 | #define EXCPT_CONTRIBUTORY 1 | |
515 | #define EXCPT_PF 2 | |
516 | ||
517 | static int exception_class(int vector) | |
518 | { | |
519 | switch (vector) { | |
520 | case PF_VECTOR: | |
521 | return EXCPT_PF; | |
522 | case DE_VECTOR: | |
523 | case TS_VECTOR: | |
524 | case NP_VECTOR: | |
525 | case SS_VECTOR: | |
526 | case GP_VECTOR: | |
527 | return EXCPT_CONTRIBUTORY; | |
528 | default: | |
529 | break; | |
530 | } | |
531 | return EXCPT_BENIGN; | |
532 | } | |
533 | ||
d6e8c854 NA |
534 | #define EXCPT_FAULT 0 |
535 | #define EXCPT_TRAP 1 | |
536 | #define EXCPT_ABORT 2 | |
537 | #define EXCPT_INTERRUPT 3 | |
538 | ||
539 | static int exception_type(int vector) | |
540 | { | |
541 | unsigned int mask; | |
542 | ||
543 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) | |
544 | return EXCPT_INTERRUPT; | |
545 | ||
546 | mask = 1 << vector; | |
547 | ||
548 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ | |
549 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) | |
550 | return EXCPT_TRAP; | |
551 | ||
552 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) | |
553 | return EXCPT_ABORT; | |
554 | ||
555 | /* Reserved exceptions will result in fault */ | |
556 | return EXCPT_FAULT; | |
557 | } | |
558 | ||
da998b46 JM |
559 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
560 | { | |
561 | unsigned nr = vcpu->arch.exception.nr; | |
562 | bool has_payload = vcpu->arch.exception.has_payload; | |
563 | unsigned long payload = vcpu->arch.exception.payload; | |
564 | ||
565 | if (!has_payload) | |
566 | return; | |
567 | ||
568 | switch (nr) { | |
f10c729f JM |
569 | case DB_VECTOR: |
570 | /* | |
571 | * "Certain debug exceptions may clear bit 0-3. The | |
572 | * remaining contents of the DR6 register are never | |
573 | * cleared by the processor". | |
574 | */ | |
575 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; | |
576 | /* | |
9a3ecd5e CQ |
577 | * In order to reflect the #DB exception payload in guest |
578 | * dr6, three components need to be considered: active low | |
579 | * bit, FIXED_1 bits and active high bits (e.g. DR6_BD, | |
580 | * DR6_BS and DR6_BT) | |
581 | * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits. | |
582 | * In the target guest dr6: | |
583 | * FIXED_1 bits should always be set. | |
584 | * Active low bits should be cleared if 1-setting in payload. | |
585 | * Active high bits should be set if 1-setting in payload. | |
586 | * | |
587 | * Note, the payload is compatible with the pending debug | |
588 | * exceptions/exit qualification under VMX, that active_low bits | |
589 | * are active high in payload. | |
590 | * So they need to be flipped for DR6. | |
f10c729f | 591 | */ |
9a3ecd5e | 592 | vcpu->arch.dr6 |= DR6_ACTIVE_LOW; |
f10c729f | 593 | vcpu->arch.dr6 |= payload; |
9a3ecd5e | 594 | vcpu->arch.dr6 ^= payload & DR6_ACTIVE_LOW; |
307f1cfa OU |
595 | |
596 | /* | |
597 | * The #DB payload is defined as compatible with the 'pending | |
598 | * debug exceptions' field under VMX, not DR6. While bit 12 is | |
599 | * defined in the 'pending debug exceptions' field (enabled | |
600 | * breakpoint), it is reserved and must be zero in DR6. | |
601 | */ | |
602 | vcpu->arch.dr6 &= ~BIT(12); | |
f10c729f | 603 | break; |
da998b46 JM |
604 | case PF_VECTOR: |
605 | vcpu->arch.cr2 = payload; | |
606 | break; | |
607 | } | |
608 | ||
609 | vcpu->arch.exception.has_payload = false; | |
610 | vcpu->arch.exception.payload = 0; | |
611 | } | |
612 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); | |
613 | ||
3fd28fce | 614 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
ce7ddec4 | 615 | unsigned nr, bool has_error, u32 error_code, |
91e86d22 | 616 | bool has_payload, unsigned long payload, bool reinject) |
3fd28fce ED |
617 | { |
618 | u32 prev_nr; | |
619 | int class1, class2; | |
620 | ||
3842d135 AK |
621 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
622 | ||
664f8e26 | 623 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
3fd28fce | 624 | queue: |
664f8e26 WL |
625 | if (reinject) { |
626 | /* | |
627 | * On vmentry, vcpu->arch.exception.pending is only | |
628 | * true if an event injection was blocked by | |
629 | * nested_run_pending. In that case, however, | |
630 | * vcpu_enter_guest requests an immediate exit, | |
631 | * and the guest shouldn't proceed far enough to | |
632 | * need reinjection. | |
633 | */ | |
634 | WARN_ON_ONCE(vcpu->arch.exception.pending); | |
635 | vcpu->arch.exception.injected = true; | |
91e86d22 JM |
636 | if (WARN_ON_ONCE(has_payload)) { |
637 | /* | |
638 | * A reinjected event has already | |
639 | * delivered its payload. | |
640 | */ | |
641 | has_payload = false; | |
642 | payload = 0; | |
643 | } | |
664f8e26 WL |
644 | } else { |
645 | vcpu->arch.exception.pending = true; | |
646 | vcpu->arch.exception.injected = false; | |
647 | } | |
3fd28fce ED |
648 | vcpu->arch.exception.has_error_code = has_error; |
649 | vcpu->arch.exception.nr = nr; | |
650 | vcpu->arch.exception.error_code = error_code; | |
91e86d22 JM |
651 | vcpu->arch.exception.has_payload = has_payload; |
652 | vcpu->arch.exception.payload = payload; | |
a06230b6 | 653 | if (!is_guest_mode(vcpu)) |
da998b46 | 654 | kvm_deliver_exception_payload(vcpu); |
3fd28fce ED |
655 | return; |
656 | } | |
657 | ||
658 | /* to check exception */ | |
659 | prev_nr = vcpu->arch.exception.nr; | |
660 | if (prev_nr == DF_VECTOR) { | |
661 | /* triple fault -> shutdown */ | |
a8eeb04a | 662 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
3fd28fce ED |
663 | return; |
664 | } | |
665 | class1 = exception_class(prev_nr); | |
666 | class2 = exception_class(nr); | |
667 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) | |
668 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { | |
664f8e26 WL |
669 | /* |
670 | * Generate double fault per SDM Table 5-5. Set | |
671 | * exception.pending = true so that the double fault | |
672 | * can trigger a nested vmexit. | |
673 | */ | |
3fd28fce | 674 | vcpu->arch.exception.pending = true; |
664f8e26 | 675 | vcpu->arch.exception.injected = false; |
3fd28fce ED |
676 | vcpu->arch.exception.has_error_code = true; |
677 | vcpu->arch.exception.nr = DF_VECTOR; | |
678 | vcpu->arch.exception.error_code = 0; | |
c851436a JM |
679 | vcpu->arch.exception.has_payload = false; |
680 | vcpu->arch.exception.payload = 0; | |
3fd28fce ED |
681 | } else |
682 | /* replace previous exception with a new one in a hope | |
683 | that instruction re-execution will regenerate lost | |
684 | exception */ | |
685 | goto queue; | |
686 | } | |
687 | ||
298101da AK |
688 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
689 | { | |
91e86d22 | 690 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
298101da AK |
691 | } |
692 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
693 | ||
ce7ddec4 JR |
694 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
695 | { | |
91e86d22 | 696 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
ce7ddec4 JR |
697 | } |
698 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); | |
699 | ||
4d5523cf PB |
700 | void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
701 | unsigned long payload) | |
f10c729f JM |
702 | { |
703 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); | |
704 | } | |
4d5523cf | 705 | EXPORT_SYMBOL_GPL(kvm_queue_exception_p); |
f10c729f | 706 | |
da998b46 JM |
707 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
708 | u32 error_code, unsigned long payload) | |
709 | { | |
710 | kvm_multiple_exception(vcpu, nr, true, error_code, | |
711 | true, payload, false); | |
712 | } | |
713 | ||
6affcbed | 714 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
c3c91fee | 715 | { |
db8fcefa AP |
716 | if (err) |
717 | kvm_inject_gp(vcpu, 0); | |
718 | else | |
6affcbed KH |
719 | return kvm_skip_emulated_instruction(vcpu); |
720 | ||
721 | return 1; | |
db8fcefa AP |
722 | } |
723 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); | |
8df25a32 | 724 | |
d2f7d498 HW |
725 | static int complete_emulated_insn_gp(struct kvm_vcpu *vcpu, int err) |
726 | { | |
727 | if (err) { | |
728 | kvm_inject_gp(vcpu, 0); | |
729 | return 1; | |
730 | } | |
731 | ||
732 | return kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE | EMULTYPE_SKIP | | |
733 | EMULTYPE_COMPLETE_USER_EXIT); | |
734 | } | |
735 | ||
6389ee94 | 736 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
c3c91fee AK |
737 | { |
738 | ++vcpu->stat.pf_guest; | |
adfe20fb WL |
739 | vcpu->arch.exception.nested_apf = |
740 | is_guest_mode(vcpu) && fault->async_page_fault; | |
da998b46 | 741 | if (vcpu->arch.exception.nested_apf) { |
adfe20fb | 742 | vcpu->arch.apf.nested_apf_token = fault->address; |
da998b46 JM |
743 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
744 | } else { | |
745 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, | |
746 | fault->address); | |
747 | } | |
c3c91fee | 748 | } |
27d6c865 | 749 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
c3c91fee | 750 | |
53b3d8e9 SC |
751 | bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, |
752 | struct x86_exception *fault) | |
d4f8cf66 | 753 | { |
0cd665bd | 754 | struct kvm_mmu *fault_mmu; |
53b3d8e9 SC |
755 | WARN_ON_ONCE(fault->vector != PF_VECTOR); |
756 | ||
0cd665bd PB |
757 | fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : |
758 | vcpu->arch.walk_mmu; | |
ef54bcfe | 759 | |
ee1fa209 JS |
760 | /* |
761 | * Invalidate the TLB entry for the faulting address, if it exists, | |
762 | * else the access will fault indefinitely (and to emulate hardware). | |
763 | */ | |
764 | if ((fault->error_code & PFERR_PRESENT_MASK) && | |
765 | !(fault->error_code & PFERR_RSVD_MASK)) | |
766 | kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address, | |
b9e5603c | 767 | fault_mmu->root.hpa); |
ee1fa209 JS |
768 | |
769 | fault_mmu->inject_page_fault(vcpu, fault); | |
ef54bcfe | 770 | return fault->nested_page_fault; |
d4f8cf66 | 771 | } |
53b3d8e9 | 772 | EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault); |
d4f8cf66 | 773 | |
3419ffc8 SY |
774 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
775 | { | |
7460fb4a AK |
776 | atomic_inc(&vcpu->arch.nmi_queued); |
777 | kvm_make_request(KVM_REQ_NMI, vcpu); | |
3419ffc8 SY |
778 | } |
779 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
780 | ||
298101da AK |
781 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
782 | { | |
91e86d22 | 783 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
298101da AK |
784 | } |
785 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
786 | ||
ce7ddec4 JR |
787 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
788 | { | |
91e86d22 | 789 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
ce7ddec4 JR |
790 | } |
791 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); | |
792 | ||
0a79b009 AK |
793 | /* |
794 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue | |
795 | * a #GP and return false. | |
796 | */ | |
797 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) | |
298101da | 798 | { |
b3646477 | 799 | if (static_call(kvm_x86_get_cpl)(vcpu) <= required_cpl) |
0a79b009 AK |
800 | return true; |
801 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
802 | return false; | |
298101da | 803 | } |
0a79b009 | 804 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
298101da | 805 | |
16f8a6f9 NA |
806 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
807 | { | |
808 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) | |
809 | return true; | |
810 | ||
811 | kvm_queue_exception(vcpu, UD_VECTOR); | |
812 | return false; | |
813 | } | |
814 | EXPORT_SYMBOL_GPL(kvm_require_dr); | |
815 | ||
16cfacc8 SC |
816 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
817 | { | |
5b7f575c | 818 | return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); |
16cfacc8 SC |
819 | } |
820 | ||
a03490ed | 821 | /* |
16cfacc8 | 822 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
a03490ed | 823 | */ |
2df4a5eb | 824 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 825 | { |
2df4a5eb | 826 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
a03490ed | 827 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
15cabbc2 | 828 | gpa_t real_gpa; |
a03490ed CO |
829 | int i; |
830 | int ret; | |
ff03a073 | 831 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
a03490ed | 832 | |
15cabbc2 SC |
833 | /* |
834 | * If the MMU is nested, CR3 holds an L2 GPA and needs to be translated | |
835 | * to an L1 GPA. | |
836 | */ | |
c59a0f57 LJ |
837 | real_gpa = kvm_translate_gpa(vcpu, mmu, gfn_to_gpa(pdpt_gfn), |
838 | PFERR_USER_MASK | PFERR_WRITE_MASK, NULL); | |
15cabbc2 SC |
839 | if (real_gpa == UNMAPPED_GVA) |
840 | return 0; | |
841 | ||
94c641ba | 842 | /* Note the offset, PDPTRs are 32 byte aligned when using PAE paging. */ |
15cabbc2 | 843 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(real_gpa), pdpte, |
94c641ba | 844 | cr3 & GENMASK(11, 5), sizeof(pdpte)); |
15cabbc2 SC |
845 | if (ret < 0) |
846 | return 0; | |
847 | ||
a03490ed | 848 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
812f30b2 | 849 | if ((pdpte[i] & PT_PRESENT_MASK) && |
16cfacc8 | 850 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
15cabbc2 | 851 | return 0; |
a03490ed CO |
852 | } |
853 | } | |
a03490ed | 854 | |
6b123c3a LJ |
855 | /* |
856 | * Marking VCPU_EXREG_PDPTR dirty doesn't work for !tdp_enabled. | |
857 | * Shadow page roots need to be reconstructed instead. | |
858 | */ | |
859 | if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) | |
0c1c92f1 | 860 | kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); |
6b123c3a | 861 | |
46cbc040 PB |
862 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
863 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); | |
864 | kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); | |
158a48ec ML |
865 | vcpu->arch.pdptrs_from_userspace = false; |
866 | ||
15cabbc2 | 867 | return 1; |
a03490ed | 868 | } |
cc4b6871 | 869 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 870 | |
f27ad38a TL |
871 | void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0) |
872 | { | |
f27ad38a TL |
873 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { |
874 | kvm_clear_async_pf_completion_queue(vcpu); | |
875 | kvm_async_pf_hash_reset(vcpu); | |
b5f61c03 PB |
876 | |
877 | /* | |
878 | * Clearing CR0.PG is defined to flush the TLB from the guest's | |
879 | * perspective. | |
880 | */ | |
881 | if (!(cr0 & X86_CR0_PG)) | |
882 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); | |
f27ad38a TL |
883 | } |
884 | ||
20f632bd | 885 | if ((cr0 ^ old_cr0) & KVM_MMU_CR0_ROLE_BITS) |
f27ad38a TL |
886 | kvm_mmu_reset_context(vcpu); |
887 | ||
888 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && | |
889 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && | |
890 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) | |
891 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); | |
892 | } | |
893 | EXPORT_SYMBOL_GPL(kvm_post_set_cr0); | |
894 | ||
49a9b07e | 895 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed | 896 | { |
aad82703 | 897 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
aad82703 | 898 | |
f9a48e6a AK |
899 | cr0 |= X86_CR0_ET; |
900 | ||
ab344828 | 901 | #ifdef CONFIG_X86_64 |
0f12244f GN |
902 | if (cr0 & 0xffffffff00000000UL) |
903 | return 1; | |
ab344828 GN |
904 | #endif |
905 | ||
906 | cr0 &= ~CR0_RESERVED_BITS; | |
a03490ed | 907 | |
0f12244f GN |
908 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
909 | return 1; | |
a03490ed | 910 | |
0f12244f GN |
911 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
912 | return 1; | |
a03490ed | 913 | |
a03490ed | 914 | #ifdef CONFIG_X86_64 |
05487215 SC |
915 | if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && |
916 | (cr0 & X86_CR0_PG)) { | |
917 | int cs_db, cs_l; | |
918 | ||
919 | if (!is_pae(vcpu)) | |
920 | return 1; | |
b3646477 | 921 | static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l); |
05487215 | 922 | if (cs_l) |
0f12244f | 923 | return 1; |
a03490ed | 924 | } |
05487215 SC |
925 | #endif |
926 | if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && | |
e63f315d | 927 | is_pae(vcpu) && ((cr0 ^ old_cr0) & X86_CR0_PDPTR_BITS) && |
2df4a5eb | 928 | !load_pdptrs(vcpu, kvm_read_cr3(vcpu))) |
05487215 | 929 | return 1; |
a03490ed | 930 | |
777ab82d LJ |
931 | if (!(cr0 & X86_CR0_PG) && |
932 | (is_64_bit_mode(vcpu) || kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))) | |
ad756a16 MJ |
933 | return 1; |
934 | ||
b3646477 | 935 | static_call(kvm_x86_set_cr0)(vcpu, cr0); |
a03490ed | 936 | |
f27ad38a | 937 | kvm_post_set_cr0(vcpu, old_cr0, cr0); |
b18d5431 | 938 | |
0f12244f GN |
939 | return 0; |
940 | } | |
2d3ad1f4 | 941 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 942 | |
2d3ad1f4 | 943 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 944 | { |
49a9b07e | 945 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
a03490ed | 946 | } |
2d3ad1f4 | 947 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 948 | |
139a12cf | 949 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 950 | { |
16809ecd TL |
951 | if (vcpu->arch.guest_state_protected) |
952 | return; | |
953 | ||
139a12cf AL |
954 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
955 | ||
956 | if (vcpu->arch.xcr0 != host_xcr0) | |
957 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); | |
958 | ||
959 | if (vcpu->arch.xsaves_enabled && | |
960 | vcpu->arch.ia32_xss != host_xss) | |
961 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); | |
962 | } | |
37486135 BM |
963 | |
964 | if (static_cpu_has(X86_FEATURE_PKU) && | |
965 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
966 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) && | |
967 | vcpu->arch.pkru != vcpu->arch.host_pkru) | |
72a6c08c | 968 | write_pkru(vcpu->arch.pkru); |
42bdf991 | 969 | } |
139a12cf | 970 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
42bdf991 | 971 | |
139a12cf | 972 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 973 | { |
16809ecd TL |
974 | if (vcpu->arch.guest_state_protected) |
975 | return; | |
976 | ||
37486135 BM |
977 | if (static_cpu_has(X86_FEATURE_PKU) && |
978 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
979 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) { | |
980 | vcpu->arch.pkru = rdpkru(); | |
981 | if (vcpu->arch.pkru != vcpu->arch.host_pkru) | |
72a6c08c | 982 | write_pkru(vcpu->arch.host_pkru); |
37486135 BM |
983 | } |
984 | ||
139a12cf AL |
985 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
986 | ||
987 | if (vcpu->arch.xcr0 != host_xcr0) | |
988 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); | |
989 | ||
990 | if (vcpu->arch.xsaves_enabled && | |
991 | vcpu->arch.ia32_xss != host_xss) | |
992 | wrmsrl(MSR_IA32_XSS, host_xss); | |
993 | } | |
994 | ||
42bdf991 | 995 | } |
139a12cf | 996 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
42bdf991 | 997 | |
988896bb LB |
998 | static inline u64 kvm_guest_supported_xcr0(struct kvm_vcpu *vcpu) |
999 | { | |
1000 | return vcpu->arch.guest_fpu.fpstate->user_xfeatures; | |
1001 | } | |
1002 | ||
ba1f77c5 | 1003 | #ifdef CONFIG_X86_64 |
988896bb LB |
1004 | static inline u64 kvm_guest_supported_xfd(struct kvm_vcpu *vcpu) |
1005 | { | |
1006 | return kvm_guest_supported_xcr0(vcpu) & XFEATURE_MASK_USER_DYNAMIC; | |
1007 | } | |
ba1f77c5 | 1008 | #endif |
988896bb | 1009 | |
69b0049a | 1010 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
2acf923e | 1011 | { |
56c103ec LJ |
1012 | u64 xcr0 = xcr; |
1013 | u64 old_xcr0 = vcpu->arch.xcr0; | |
46c34cb0 | 1014 | u64 valid_bits; |
2acf923e DC |
1015 | |
1016 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ | |
1017 | if (index != XCR_XFEATURE_ENABLED_MASK) | |
1018 | return 1; | |
d91cab78 | 1019 | if (!(xcr0 & XFEATURE_MASK_FP)) |
2acf923e | 1020 | return 1; |
d91cab78 | 1021 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
2acf923e | 1022 | return 1; |
46c34cb0 PB |
1023 | |
1024 | /* | |
1025 | * Do not allow the guest to set bits that we do not support | |
1026 | * saving. However, xcr0 bit 0 is always set, even if the | |
e8f65b9b | 1027 | * emulated CPU does not support XSAVE (see kvm_vcpu_reset()). |
46c34cb0 | 1028 | */ |
988896bb | 1029 | valid_bits = kvm_guest_supported_xcr0(vcpu) | XFEATURE_MASK_FP; |
46c34cb0 | 1030 | if (xcr0 & ~valid_bits) |
2acf923e | 1031 | return 1; |
46c34cb0 | 1032 | |
d91cab78 DH |
1033 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
1034 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) | |
390bd528 LJ |
1035 | return 1; |
1036 | ||
d91cab78 DH |
1037 | if (xcr0 & XFEATURE_MASK_AVX512) { |
1038 | if (!(xcr0 & XFEATURE_MASK_YMM)) | |
612263b3 | 1039 | return 1; |
d91cab78 | 1040 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
612263b3 CP |
1041 | return 1; |
1042 | } | |
86aff7a4 JL |
1043 | |
1044 | if ((xcr0 & XFEATURE_MASK_XTILE) && | |
1045 | ((xcr0 & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE)) | |
1046 | return 1; | |
1047 | ||
2acf923e | 1048 | vcpu->arch.xcr0 = xcr0; |
56c103ec | 1049 | |
d91cab78 | 1050 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
aedbaf4f | 1051 | kvm_update_cpuid_runtime(vcpu); |
2acf923e DC |
1052 | return 0; |
1053 | } | |
1054 | ||
92f9895c | 1055 | int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu) |
2acf923e | 1056 | { |
92f9895c SC |
1057 | if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || |
1058 | __kvm_set_xcr(vcpu, kvm_rcx_read(vcpu), kvm_read_edx_eax(vcpu))) { | |
1059 | kvm_inject_gp(vcpu, 0); | |
1060 | return 1; | |
1061 | } | |
bbefd4fc | 1062 | |
92f9895c | 1063 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e | 1064 | } |
92f9895c | 1065 | EXPORT_SYMBOL_GPL(kvm_emulate_xsetbv); |
2acf923e | 1066 | |
ee69c92b | 1067 | bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed | 1068 | { |
b11306b5 | 1069 | if (cr4 & cr4_reserved_bits) |
ee69c92b | 1070 | return false; |
b9baba86 | 1071 | |
b899c132 | 1072 | if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) |
ee69c92b | 1073 | return false; |
3ca94192 | 1074 | |
b3646477 | 1075 | return static_call(kvm_x86_is_valid_cr4)(vcpu, cr4); |
3ca94192 | 1076 | } |
ee69c92b | 1077 | EXPORT_SYMBOL_GPL(kvm_is_valid_cr4); |
3ca94192 | 1078 | |
5b51cb13 TL |
1079 | void kvm_post_set_cr4(struct kvm_vcpu *vcpu, unsigned long old_cr4, unsigned long cr4) |
1080 | { | |
b5f61c03 PB |
1081 | if ((cr4 ^ old_cr4) & KVM_MMU_CR4_ROLE_BITS) |
1082 | kvm_mmu_reset_context(vcpu); | |
1083 | ||
509bfe3d | 1084 | /* |
509bfe3d LJ |
1085 | * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB |
1086 | * according to the SDM; however, stale prev_roots could be reused | |
1087 | * incorrectly in the future after a MOV to CR3 with NOFLUSH=1, so we | |
b5f61c03 PB |
1088 | * free them all. This is *not* a superset of KVM_REQ_TLB_FLUSH_GUEST |
1089 | * or KVM_REQ_TLB_FLUSH_CURRENT, because the hardware TLB is not flushed, | |
1090 | * so fall through. | |
509bfe3d | 1091 | */ |
b5f61c03 PB |
1092 | if (!tdp_enabled && |
1093 | (cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) | |
f6d0a252 | 1094 | kvm_mmu_unload(vcpu); |
b5f61c03 PB |
1095 | |
1096 | /* | |
1097 | * The TLB has to be flushed for all PCIDs if any of the following | |
1098 | * (architecturally required) changes happen: | |
1099 | * - CR4.PCIDE is changed from 1 to 0 | |
1100 | * - CR4.PGE is toggled | |
1101 | * | |
1102 | * This is a superset of KVM_REQ_TLB_FLUSH_CURRENT. | |
1103 | */ | |
1104 | if (((cr4 ^ old_cr4) & X86_CR4_PGE) || | |
1105 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) | |
55261738 | 1106 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
b5f61c03 PB |
1107 | |
1108 | /* | |
1109 | * The TLB has to be flushed for the current PCID if any of the | |
1110 | * following (architecturally required) changes happen: | |
1111 | * - CR4.SMEP is changed from 0 to 1 | |
1112 | * - CR4.PAE is toggled | |
1113 | */ | |
1114 | else if (((cr4 ^ old_cr4) & X86_CR4_PAE) || | |
1115 | ((cr4 & X86_CR4_SMEP) && !(old_cr4 & X86_CR4_SMEP))) | |
1116 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
1117 | ||
3ca94192 | 1118 | } |
5b51cb13 | 1119 | EXPORT_SYMBOL_GPL(kvm_post_set_cr4); |
3ca94192 WL |
1120 | |
1121 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
1122 | { | |
1123 | unsigned long old_cr4 = kvm_read_cr4(vcpu); | |
3ca94192 | 1124 | |
ee69c92b | 1125 | if (!kvm_is_valid_cr4(vcpu, cr4)) |
ae3e61e1 PB |
1126 | return 1; |
1127 | ||
a03490ed | 1128 | if (is_long_mode(vcpu)) { |
0f12244f GN |
1129 | if (!(cr4 & X86_CR4_PAE)) |
1130 | return 1; | |
d74fcfc1 SC |
1131 | if ((cr4 ^ old_cr4) & X86_CR4_LA57) |
1132 | return 1; | |
a2edf57f | 1133 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
a37ebdce | 1134 | && ((cr4 ^ old_cr4) & X86_CR4_PDPTR_BITS) |
2df4a5eb | 1135 | && !load_pdptrs(vcpu, kvm_read_cr3(vcpu))) |
0f12244f GN |
1136 | return 1; |
1137 | ||
ad756a16 | 1138 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
d6321d49 | 1139 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
ad756a16 MJ |
1140 | return 1; |
1141 | ||
1142 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ | |
1143 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) | |
1144 | return 1; | |
1145 | } | |
1146 | ||
b3646477 | 1147 | static_call(kvm_x86_set_cr4)(vcpu, cr4); |
a03490ed | 1148 | |
5b51cb13 | 1149 | kvm_post_set_cr4(vcpu, old_cr4, cr4); |
2acf923e | 1150 | |
0f12244f GN |
1151 | return 0; |
1152 | } | |
2d3ad1f4 | 1153 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 1154 | |
21823fbd SC |
1155 | static void kvm_invalidate_pcid(struct kvm_vcpu *vcpu, unsigned long pcid) |
1156 | { | |
1157 | struct kvm_mmu *mmu = vcpu->arch.mmu; | |
1158 | unsigned long roots_to_free = 0; | |
1159 | int i; | |
1160 | ||
e45e9e39 LJ |
1161 | /* |
1162 | * MOV CR3 and INVPCID are usually not intercepted when using TDP, but | |
1163 | * this is reachable when running EPT=1 and unrestricted_guest=0, and | |
1164 | * also via the emulator. KVM's TDP page tables are not in the scope of | |
1165 | * the invalidation, but the guest's TLB entries need to be flushed as | |
1166 | * the CPU may have cached entries in its TLB for the target PCID. | |
1167 | */ | |
1168 | if (unlikely(tdp_enabled)) { | |
1169 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); | |
1170 | return; | |
1171 | } | |
1172 | ||
21823fbd SC |
1173 | /* |
1174 | * If neither the current CR3 nor any of the prev_roots use the given | |
1175 | * PCID, then nothing needs to be done here because a resync will | |
1176 | * happen anyway before switching to any other CR3. | |
1177 | */ | |
1178 | if (kvm_get_active_pcid(vcpu) == pcid) { | |
e62f1aa8 | 1179 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
21823fbd SC |
1180 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
1181 | } | |
1182 | ||
509bfe3d LJ |
1183 | /* |
1184 | * If PCID is disabled, there is no need to free prev_roots even if the | |
1185 | * PCIDs for them are also 0, because MOV to CR3 always flushes the TLB | |
1186 | * with PCIDE=0. | |
1187 | */ | |
1188 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) | |
1189 | return; | |
1190 | ||
21823fbd SC |
1191 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
1192 | if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) | |
1193 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); | |
1194 | ||
0c1c92f1 | 1195 | kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); |
21823fbd SC |
1196 | } |
1197 | ||
2390218b | 1198 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 1199 | { |
ade61e28 | 1200 | bool skip_tlb_flush = false; |
21823fbd | 1201 | unsigned long pcid = 0; |
ac146235 | 1202 | #ifdef CONFIG_X86_64 |
c19986fe JS |
1203 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
1204 | ||
ade61e28 | 1205 | if (pcid_enabled) { |
208320ba JS |
1206 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
1207 | cr3 &= ~X86_CR3_PCID_NOFLUSH; | |
21823fbd | 1208 | pcid = cr3 & X86_CR3_PCID_MASK; |
ade61e28 | 1209 | } |
ac146235 | 1210 | #endif |
9d88fca7 | 1211 | |
c7313155 | 1212 | /* PDPTRs are always reloaded for PAE paging. */ |
21823fbd SC |
1213 | if (cr3 == kvm_read_cr3(vcpu) && !is_pae_paging(vcpu)) |
1214 | goto handle_tlb_flush; | |
d835dfec | 1215 | |
886bbcc7 SC |
1216 | /* |
1217 | * Do not condition the GPA check on long mode, this helper is used to | |
1218 | * stuff CR3, e.g. for RSM emulation, and there is no guarantee that | |
1219 | * the current vCPU mode is accurate. | |
1220 | */ | |
1221 | if (kvm_vcpu_is_illegal_gpa(vcpu, cr3)) | |
d1cd3ce9 | 1222 | return 1; |
886bbcc7 | 1223 | |
2df4a5eb | 1224 | if (is_pae_paging(vcpu) && !load_pdptrs(vcpu, cr3)) |
346874c9 | 1225 | return 1; |
a03490ed | 1226 | |
21823fbd | 1227 | if (cr3 != kvm_read_cr3(vcpu)) |
b5129100 | 1228 | kvm_mmu_new_pgd(vcpu, cr3); |
21823fbd | 1229 | |
0f12244f | 1230 | vcpu->arch.cr3 = cr3; |
3883bc9d | 1231 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); |
405329fc | 1232 | /* Do not call post_set_cr3, we do not get here for confidential guests. */ |
7c390d35 | 1233 | |
21823fbd SC |
1234 | handle_tlb_flush: |
1235 | /* | |
1236 | * A load of CR3 that flushes the TLB flushes only the current PCID, | |
1237 | * even if PCID is disabled, in which case PCID=0 is flushed. It's a | |
1238 | * moot point in the end because _disabling_ PCID will flush all PCIDs, | |
1239 | * and it's impossible to use a non-zero PCID when PCID is disabled, | |
1240 | * i.e. only PCID=0 can be relevant. | |
1241 | */ | |
1242 | if (!skip_tlb_flush) | |
1243 | kvm_invalidate_pcid(vcpu, pcid); | |
1244 | ||
0f12244f GN |
1245 | return 0; |
1246 | } | |
2d3ad1f4 | 1247 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 1248 | |
eea1cff9 | 1249 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed | 1250 | { |
0f12244f GN |
1251 | if (cr8 & CR8_RESERVED_BITS) |
1252 | return 1; | |
35754c98 | 1253 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1254 | kvm_lapic_set_tpr(vcpu, cr8); |
1255 | else | |
ad312c7c | 1256 | vcpu->arch.cr8 = cr8; |
0f12244f GN |
1257 | return 0; |
1258 | } | |
2d3ad1f4 | 1259 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 1260 | |
2d3ad1f4 | 1261 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed | 1262 | { |
35754c98 | 1263 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1264 | return kvm_lapic_get_cr8(vcpu); |
1265 | else | |
ad312c7c | 1266 | return vcpu->arch.cr8; |
a03490ed | 1267 | } |
2d3ad1f4 | 1268 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 1269 | |
ae561ede NA |
1270 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
1271 | { | |
1272 | int i; | |
1273 | ||
1274 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { | |
1275 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
1276 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
ae561ede NA |
1277 | } |
1278 | } | |
1279 | ||
7c86663b | 1280 | void kvm_update_dr7(struct kvm_vcpu *vcpu) |
c8639010 JK |
1281 | { |
1282 | unsigned long dr7; | |
1283 | ||
1284 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) | |
1285 | dr7 = vcpu->arch.guest_debug_dr7; | |
1286 | else | |
1287 | dr7 = vcpu->arch.dr7; | |
b3646477 | 1288 | static_call(kvm_x86_set_dr7)(vcpu, dr7); |
360b948d PB |
1289 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
1290 | if (dr7 & DR7_BP_EN_MASK) | |
1291 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; | |
c8639010 | 1292 | } |
7c86663b | 1293 | EXPORT_SYMBOL_GPL(kvm_update_dr7); |
c8639010 | 1294 | |
6f43ed01 NA |
1295 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
1296 | { | |
1297 | u64 fixed = DR6_FIXED_1; | |
1298 | ||
d6321d49 | 1299 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
6f43ed01 | 1300 | fixed |= DR6_RTM; |
e8ea85fb CQ |
1301 | |
1302 | if (!guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT)) | |
1303 | fixed |= DR6_BUS_LOCK; | |
6f43ed01 NA |
1304 | return fixed; |
1305 | } | |
1306 | ||
996ff542 | 1307 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
020df079 | 1308 | { |
ea740059 MP |
1309 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1310 | ||
020df079 GN |
1311 | switch (dr) { |
1312 | case 0 ... 3: | |
ea740059 | 1313 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
020df079 GN |
1314 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
1315 | vcpu->arch.eff_db[dr] = val; | |
1316 | break; | |
1317 | case 4: | |
020df079 | 1318 | case 6: |
f5f6145e | 1319 | if (!kvm_dr6_valid(val)) |
996ff542 | 1320 | return 1; /* #GP */ |
6f43ed01 | 1321 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
020df079 GN |
1322 | break; |
1323 | case 5: | |
020df079 | 1324 | default: /* 7 */ |
b91991bf | 1325 | if (!kvm_dr7_valid(val)) |
996ff542 | 1326 | return 1; /* #GP */ |
020df079 | 1327 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
c8639010 | 1328 | kvm_update_dr7(vcpu); |
020df079 GN |
1329 | break; |
1330 | } | |
1331 | ||
1332 | return 0; | |
1333 | } | |
1334 | EXPORT_SYMBOL_GPL(kvm_set_dr); | |
1335 | ||
29d6ca41 | 1336 | void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
020df079 | 1337 | { |
ea740059 MP |
1338 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1339 | ||
020df079 GN |
1340 | switch (dr) { |
1341 | case 0 ... 3: | |
ea740059 | 1342 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
020df079 GN |
1343 | break; |
1344 | case 4: | |
020df079 | 1345 | case 6: |
5679b803 | 1346 | *val = vcpu->arch.dr6; |
020df079 GN |
1347 | break; |
1348 | case 5: | |
020df079 GN |
1349 | default: /* 7 */ |
1350 | *val = vcpu->arch.dr7; | |
1351 | break; | |
1352 | } | |
338dbc97 | 1353 | } |
020df079 GN |
1354 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
1355 | ||
c483c454 | 1356 | int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu) |
022cd0e8 | 1357 | { |
de3cd117 | 1358 | u32 ecx = kvm_rcx_read(vcpu); |
022cd0e8 | 1359 | u64 data; |
022cd0e8 | 1360 | |
c483c454 SC |
1361 | if (kvm_pmu_rdpmc(vcpu, ecx, &data)) { |
1362 | kvm_inject_gp(vcpu, 0); | |
1363 | return 1; | |
1364 | } | |
1365 | ||
de3cd117 SC |
1366 | kvm_rax_write(vcpu, (u32)data); |
1367 | kvm_rdx_write(vcpu, data >> 32); | |
c483c454 | 1368 | return kvm_skip_emulated_instruction(vcpu); |
022cd0e8 | 1369 | } |
c483c454 | 1370 | EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc); |
022cd0e8 | 1371 | |
043405e1 CO |
1372 | /* |
1373 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1374 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
1375 | * | |
7a5ee6ed CQ |
1376 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
1377 | * extract the supported MSRs from the related const lists. | |
1378 | * msrs_to_save is selected from the msrs_to_save_all to reflect the | |
e3267cbb | 1379 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
7a5ee6ed | 1380 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
62ef68bb | 1381 | * may depend on host virtualization features rather than host cpu features. |
043405e1 | 1382 | */ |
e3267cbb | 1383 | |
7a5ee6ed | 1384 | static const u32 msrs_to_save_all[] = { |
043405e1 | 1385 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
8c06585d | 1386 | MSR_STAR, |
043405e1 CO |
1387 | #ifdef CONFIG_X86_64 |
1388 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
1389 | #endif | |
b3897a49 | 1390 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
32ad73db | 1391 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
2bdb76c0 | 1392 | MSR_IA32_SPEC_CTRL, |
bf8c55d8 CP |
1393 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
1394 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, | |
1395 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, | |
1396 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, | |
1397 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, | |
1398 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, | |
6e3ba4ab TX |
1399 | MSR_IA32_UMWAIT_CONTROL, |
1400 | ||
e2ada66e | 1401 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
9fb12fe5 | 1402 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, |
e2ada66e JM |
1403 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, |
1404 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, | |
1405 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, | |
1406 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, | |
1407 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, | |
1408 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, | |
1409 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, | |
1410 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, | |
1411 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, | |
1412 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, | |
1413 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, | |
e2ada66e JM |
1414 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
1415 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, | |
1416 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, | |
1417 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, | |
1418 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, | |
1419 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, | |
1420 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, | |
1421 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, | |
1422 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, | |
e1fc1553 FM |
1423 | |
1424 | MSR_K7_EVNTSEL0, MSR_K7_EVNTSEL1, MSR_K7_EVNTSEL2, MSR_K7_EVNTSEL3, | |
1425 | MSR_K7_PERFCTR0, MSR_K7_PERFCTR1, MSR_K7_PERFCTR2, MSR_K7_PERFCTR3, | |
1426 | MSR_F15H_PERF_CTL0, MSR_F15H_PERF_CTL1, MSR_F15H_PERF_CTL2, | |
1427 | MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5, | |
1428 | MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2, | |
1429 | MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5, | |
548e8365 | 1430 | MSR_IA32_XFD, MSR_IA32_XFD_ERR, |
043405e1 CO |
1431 | }; |
1432 | ||
7a5ee6ed | 1433 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
043405e1 CO |
1434 | static unsigned num_msrs_to_save; |
1435 | ||
7a5ee6ed | 1436 | static const u32 emulated_msrs_all[] = { |
62ef68bb PB |
1437 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
1438 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, | |
1439 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, | |
1440 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, | |
72c139ba | 1441 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
e7d9513b AS |
1442 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
1443 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, | |
e516cebb | 1444 | HV_X64_MSR_RESET, |
11c4b1ca | 1445 | HV_X64_MSR_VP_INDEX, |
9eec50b8 | 1446 | HV_X64_MSR_VP_RUNTIME, |
5c919412 | 1447 | HV_X64_MSR_SCONTROL, |
1f4b34f8 | 1448 | HV_X64_MSR_STIMER0_CONFIG, |
d4abc577 | 1449 | HV_X64_MSR_VP_ASSIST_PAGE, |
a2e164e7 VK |
1450 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
1451 | HV_X64_MSR_TSC_EMULATION_STATUS, | |
f97f5a56 JD |
1452 | HV_X64_MSR_SYNDBG_OPTIONS, |
1453 | HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS, | |
1454 | HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER, | |
1455 | HV_X64_MSR_SYNDBG_PENDING_BUFFER, | |
a2e164e7 VK |
1456 | |
1457 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, | |
557a961a | 1458 | MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, |
62ef68bb | 1459 | |
ba904635 | 1460 | MSR_IA32_TSC_ADJUST, |
09141ec0 | 1461 | MSR_IA32_TSC_DEADLINE, |
2bdb76c0 | 1462 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1463 | MSR_IA32_PERF_CAPABILITIES, |
043405e1 | 1464 | MSR_IA32_MISC_ENABLE, |
908e75f3 AK |
1465 | MSR_IA32_MCG_STATUS, |
1466 | MSR_IA32_MCG_CTL, | |
c45dcc71 | 1467 | MSR_IA32_MCG_EXT_CTL, |
64d60670 | 1468 | MSR_IA32_SMBASE, |
52797bf9 | 1469 | MSR_SMI_COUNT, |
db2336a8 KH |
1470 | MSR_PLATFORM_INFO, |
1471 | MSR_MISC_FEATURES_ENABLES, | |
bc226f07 | 1472 | MSR_AMD64_VIRT_SPEC_CTRL, |
5228eb96 | 1473 | MSR_AMD64_TSC_RATIO, |
6c6a2ab9 | 1474 | MSR_IA32_POWER_CTL, |
99634e3e | 1475 | MSR_IA32_UCODE_REV, |
191c8137 | 1476 | |
95c5c7c7 PB |
1477 | /* |
1478 | * The following list leaves out MSRs whose values are determined | |
1479 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. | |
1480 | * We always support the "true" VMX control MSRs, even if the host | |
1481 | * processor does not, so I am putting these registers here rather | |
7a5ee6ed | 1482 | * than in msrs_to_save_all. |
95c5c7c7 PB |
1483 | */ |
1484 | MSR_IA32_VMX_BASIC, | |
1485 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1486 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1487 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1488 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1489 | MSR_IA32_VMX_MISC, | |
1490 | MSR_IA32_VMX_CR0_FIXED0, | |
1491 | MSR_IA32_VMX_CR4_FIXED0, | |
1492 | MSR_IA32_VMX_VMCS_ENUM, | |
1493 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1494 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1495 | MSR_IA32_VMX_VMFUNC, | |
1496 | ||
191c8137 | 1497 | MSR_K7_HWCR, |
2d5ba19b | 1498 | MSR_KVM_POLL_CONTROL, |
043405e1 CO |
1499 | }; |
1500 | ||
7a5ee6ed | 1501 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
62ef68bb PB |
1502 | static unsigned num_emulated_msrs; |
1503 | ||
801e459a TL |
1504 | /* |
1505 | * List of msr numbers which are used to expose MSR-based features that | |
1506 | * can be used by a hypervisor to validate requested CPU features. | |
1507 | */ | |
7a5ee6ed | 1508 | static const u32 msr_based_features_all[] = { |
1389309c PB |
1509 | MSR_IA32_VMX_BASIC, |
1510 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1511 | MSR_IA32_VMX_PINBASED_CTLS, | |
1512 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1513 | MSR_IA32_VMX_PROCBASED_CTLS, | |
1514 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1515 | MSR_IA32_VMX_EXIT_CTLS, | |
1516 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1517 | MSR_IA32_VMX_ENTRY_CTLS, | |
1518 | MSR_IA32_VMX_MISC, | |
1519 | MSR_IA32_VMX_CR0_FIXED0, | |
1520 | MSR_IA32_VMX_CR0_FIXED1, | |
1521 | MSR_IA32_VMX_CR4_FIXED0, | |
1522 | MSR_IA32_VMX_CR4_FIXED1, | |
1523 | MSR_IA32_VMX_VMCS_ENUM, | |
1524 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1525 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1526 | MSR_IA32_VMX_VMFUNC, | |
1527 | ||
d1d93fa9 | 1528 | MSR_F10H_DECFG, |
518e7b94 | 1529 | MSR_IA32_UCODE_REV, |
cd283252 | 1530 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1531 | MSR_IA32_PERF_CAPABILITIES, |
801e459a TL |
1532 | }; |
1533 | ||
7a5ee6ed | 1534 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
801e459a TL |
1535 | static unsigned int num_msr_based_features; |
1536 | ||
4d22c17c | 1537 | static u64 kvm_get_arch_capabilities(void) |
5b76a3cf | 1538 | { |
4d22c17c | 1539 | u64 data = 0; |
5b76a3cf | 1540 | |
4d22c17c XL |
1541 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
1542 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); | |
5b76a3cf | 1543 | |
b8e8c830 PB |
1544 | /* |
1545 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that | |
1546 | * the nested hypervisor runs with NX huge pages. If it is not, | |
d9f6e12f | 1547 | * L1 is anyway vulnerable to ITLB_MULTIHIT exploits from other |
b8e8c830 PB |
1548 | * L1 guests, so it need not worry about its own (L2) guests. |
1549 | */ | |
1550 | data |= ARCH_CAP_PSCHANGE_MC_NO; | |
1551 | ||
5b76a3cf PB |
1552 | /* |
1553 | * If we're doing cache flushes (either "always" or "cond") | |
1554 | * we will do one whenever the guest does a vmlaunch/vmresume. | |
1555 | * If an outer hypervisor is doing the cache flush for us | |
1556 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that | |
1557 | * capability to the guest too, and if EPT is disabled we're not | |
1558 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will | |
1559 | * require a nested hypervisor to do a flush of its own. | |
1560 | */ | |
1561 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) | |
1562 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; | |
1563 | ||
0c54914d PB |
1564 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
1565 | data |= ARCH_CAP_RDCL_NO; | |
1566 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1567 | data |= ARCH_CAP_SSB_NO; | |
1568 | if (!boot_cpu_has_bug(X86_BUG_MDS)) | |
1569 | data |= ARCH_CAP_MDS_NO; | |
1570 | ||
7131636e PB |
1571 | if (!boot_cpu_has(X86_FEATURE_RTM)) { |
1572 | /* | |
1573 | * If RTM=0 because the kernel has disabled TSX, the host might | |
1574 | * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0 | |
1575 | * and therefore knows that there cannot be TAA) but keep | |
1576 | * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts, | |
1577 | * and we want to allow migrating those guests to tsx=off hosts. | |
1578 | */ | |
1579 | data &= ~ARCH_CAP_TAA_NO; | |
1580 | } else if (!boot_cpu_has_bug(X86_BUG_TAA)) { | |
cbbaa272 | 1581 | data |= ARCH_CAP_TAA_NO; |
7131636e PB |
1582 | } else { |
1583 | /* | |
1584 | * Nothing to do here; we emulate TSX_CTRL if present on the | |
1585 | * host so the guest can choose between disabling TSX or | |
1586 | * using VERW to clear CPU buffers. | |
1587 | */ | |
1588 | } | |
e1d38b63 | 1589 | |
5b76a3cf PB |
1590 | return data; |
1591 | } | |
5b76a3cf | 1592 | |
66421c1e WL |
1593 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
1594 | { | |
1595 | switch (msr->index) { | |
cd283252 | 1596 | case MSR_IA32_ARCH_CAPABILITIES: |
5b76a3cf PB |
1597 | msr->data = kvm_get_arch_capabilities(); |
1598 | break; | |
1599 | case MSR_IA32_UCODE_REV: | |
cd283252 | 1600 | rdmsrl_safe(msr->index, &msr->data); |
518e7b94 | 1601 | break; |
66421c1e | 1602 | default: |
b3646477 | 1603 | return static_call(kvm_x86_get_msr_feature)(msr); |
66421c1e WL |
1604 | } |
1605 | return 0; | |
1606 | } | |
1607 | ||
801e459a TL |
1608 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
1609 | { | |
1610 | struct kvm_msr_entry msr; | |
66421c1e | 1611 | int r; |
801e459a TL |
1612 | |
1613 | msr.index = index; | |
66421c1e | 1614 | r = kvm_get_msr_feature(&msr); |
12bc2132 PX |
1615 | |
1616 | if (r == KVM_MSR_RET_INVALID) { | |
1617 | /* Unconditionally clear the output for simplicity */ | |
1618 | *data = 0; | |
d632826f | 1619 | if (kvm_msr_ignored_check(index, 0, false)) |
cc4cb017 | 1620 | r = 0; |
12bc2132 PX |
1621 | } |
1622 | ||
66421c1e WL |
1623 | if (r) |
1624 | return r; | |
801e459a TL |
1625 | |
1626 | *data = msr.data; | |
1627 | ||
1628 | return 0; | |
1629 | } | |
1630 | ||
11988499 | 1631 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
15c4a640 | 1632 | { |
1b4d56b8 | 1633 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
11988499 | 1634 | return false; |
1b2fd70c | 1635 | |
1b4d56b8 | 1636 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
11988499 | 1637 | return false; |
d8017474 | 1638 | |
0a629563 SC |
1639 | if (efer & (EFER_LME | EFER_LMA) && |
1640 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
1641 | return false; | |
1642 | ||
1643 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) | |
1644 | return false; | |
d8017474 | 1645 | |
384bb783 | 1646 | return true; |
11988499 SC |
1647 | |
1648 | } | |
1649 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) | |
1650 | { | |
1651 | if (efer & efer_reserved_bits) | |
1652 | return false; | |
1653 | ||
1654 | return __kvm_valid_efer(vcpu, efer); | |
384bb783 JK |
1655 | } |
1656 | EXPORT_SYMBOL_GPL(kvm_valid_efer); | |
1657 | ||
11988499 | 1658 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
384bb783 JK |
1659 | { |
1660 | u64 old_efer = vcpu->arch.efer; | |
11988499 | 1661 | u64 efer = msr_info->data; |
72f211ec | 1662 | int r; |
384bb783 | 1663 | |
11988499 | 1664 | if (efer & efer_reserved_bits) |
66f61c92 | 1665 | return 1; |
384bb783 | 1666 | |
11988499 SC |
1667 | if (!msr_info->host_initiated) { |
1668 | if (!__kvm_valid_efer(vcpu, efer)) | |
1669 | return 1; | |
1670 | ||
1671 | if (is_paging(vcpu) && | |
1672 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) | |
1673 | return 1; | |
1674 | } | |
384bb783 | 1675 | |
15c4a640 | 1676 | efer &= ~EFER_LMA; |
f6801dff | 1677 | efer |= vcpu->arch.efer & EFER_LMA; |
15c4a640 | 1678 | |
b3646477 | 1679 | r = static_call(kvm_x86_set_efer)(vcpu, efer); |
72f211ec ML |
1680 | if (r) { |
1681 | WARN_ON(r > 0); | |
1682 | return r; | |
1683 | } | |
a3d204e2 | 1684 | |
d6174299 | 1685 | if ((efer ^ old_efer) & KVM_MMU_EFER_ROLE_BITS) |
aad82703 SY |
1686 | kvm_mmu_reset_context(vcpu); |
1687 | ||
b69e8cae | 1688 | return 0; |
15c4a640 CO |
1689 | } |
1690 | ||
f2b4b7dd JR |
1691 | void kvm_enable_efer_bits(u64 mask) |
1692 | { | |
1693 | efer_reserved_bits &= ~mask; | |
1694 | } | |
1695 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
1696 | ||
51de8151 AG |
1697 | bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type) |
1698 | { | |
b318e8de SC |
1699 | struct kvm_x86_msr_filter *msr_filter; |
1700 | struct msr_bitmap_range *ranges; | |
1a155254 | 1701 | struct kvm *kvm = vcpu->kvm; |
b318e8de | 1702 | bool allowed; |
1a155254 | 1703 | int idx; |
b318e8de | 1704 | u32 i; |
1a155254 | 1705 | |
b318e8de SC |
1706 | /* x2APIC MSRs do not support filtering. */ |
1707 | if (index >= 0x800 && index <= 0x8ff) | |
1a155254 AG |
1708 | return true; |
1709 | ||
1a155254 AG |
1710 | idx = srcu_read_lock(&kvm->srcu); |
1711 | ||
b318e8de SC |
1712 | msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); |
1713 | if (!msr_filter) { | |
1714 | allowed = true; | |
1715 | goto out; | |
1716 | } | |
1717 | ||
1718 | allowed = msr_filter->default_allow; | |
1719 | ranges = msr_filter->ranges; | |
1720 | ||
1721 | for (i = 0; i < msr_filter->count; i++) { | |
1a155254 AG |
1722 | u32 start = ranges[i].base; |
1723 | u32 end = start + ranges[i].nmsrs; | |
1724 | u32 flags = ranges[i].flags; | |
1725 | unsigned long *bitmap = ranges[i].bitmap; | |
1726 | ||
1727 | if ((index >= start) && (index < end) && (flags & type)) { | |
b318e8de | 1728 | allowed = !!test_bit(index - start, bitmap); |
1a155254 AG |
1729 | break; |
1730 | } | |
1731 | } | |
1732 | ||
b318e8de | 1733 | out: |
1a155254 AG |
1734 | srcu_read_unlock(&kvm->srcu, idx); |
1735 | ||
b318e8de | 1736 | return allowed; |
51de8151 AG |
1737 | } |
1738 | EXPORT_SYMBOL_GPL(kvm_msr_allowed); | |
1739 | ||
15c4a640 | 1740 | /* |
f20935d8 SC |
1741 | * Write @data into the MSR specified by @index. Select MSR specific fault |
1742 | * checks are bypassed if @host_initiated is %true. | |
15c4a640 CO |
1743 | * Returns 0 on success, non-0 otherwise. |
1744 | * Assumes vcpu_load() was already called. | |
1745 | */ | |
f20935d8 SC |
1746 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
1747 | bool host_initiated) | |
15c4a640 | 1748 | { |
f20935d8 SC |
1749 | struct msr_data msr; |
1750 | ||
1751 | switch (index) { | |
854e8bb1 NA |
1752 | case MSR_FS_BASE: |
1753 | case MSR_GS_BASE: | |
1754 | case MSR_KERNEL_GS_BASE: | |
1755 | case MSR_CSTAR: | |
1756 | case MSR_LSTAR: | |
f20935d8 | 1757 | if (is_noncanonical_address(data, vcpu)) |
854e8bb1 NA |
1758 | return 1; |
1759 | break; | |
1760 | case MSR_IA32_SYSENTER_EIP: | |
1761 | case MSR_IA32_SYSENTER_ESP: | |
1762 | /* | |
1763 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if | |
1764 | * non-canonical address is written on Intel but not on | |
1765 | * AMD (which ignores the top 32-bits, because it does | |
1766 | * not implement 64-bit SYSENTER). | |
1767 | * | |
1768 | * 64-bit code should hence be able to write a non-canonical | |
1769 | * value on AMD. Making the address canonical ensures that | |
1770 | * vmentry does not fail on Intel after writing a non-canonical | |
1771 | * value, and that something deterministic happens if the guest | |
1772 | * invokes 64-bit SYSENTER. | |
1773 | */ | |
f20935d8 | 1774 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
61a05d44 SC |
1775 | break; |
1776 | case MSR_TSC_AUX: | |
1777 | if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX)) | |
1778 | return 1; | |
1779 | ||
1780 | if (!host_initiated && | |
1781 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) && | |
1782 | !guest_cpuid_has(vcpu, X86_FEATURE_RDPID)) | |
1783 | return 1; | |
1784 | ||
1785 | /* | |
1786 | * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has | |
1787 | * incomplete and conflicting architectural behavior. Current | |
1788 | * AMD CPUs completely ignore bits 63:32, i.e. they aren't | |
1789 | * reserved and always read as zeros. Enforce Intel's reserved | |
1790 | * bits check if and only if the guest CPU is Intel, and clear | |
1791 | * the bits in all other cases. This ensures cross-vendor | |
1792 | * migration will provide consistent behavior for the guest. | |
1793 | */ | |
1794 | if (guest_cpuid_is_intel(vcpu) && (data >> 32) != 0) | |
1795 | return 1; | |
1796 | ||
1797 | data = (u32)data; | |
1798 | break; | |
854e8bb1 | 1799 | } |
f20935d8 SC |
1800 | |
1801 | msr.data = data; | |
1802 | msr.index = index; | |
1803 | msr.host_initiated = host_initiated; | |
1804 | ||
b3646477 | 1805 | return static_call(kvm_x86_set_msr)(vcpu, &msr); |
15c4a640 CO |
1806 | } |
1807 | ||
6abe9c13 PX |
1808 | static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, |
1809 | u32 index, u64 data, bool host_initiated) | |
1810 | { | |
1811 | int ret = __kvm_set_msr(vcpu, index, data, host_initiated); | |
1812 | ||
1813 | if (ret == KVM_MSR_RET_INVALID) | |
d632826f | 1814 | if (kvm_msr_ignored_check(index, data, true)) |
cc4cb017 | 1815 | ret = 0; |
6abe9c13 PX |
1816 | |
1817 | return ret; | |
1818 | } | |
1819 | ||
313a3dc7 | 1820 | /* |
f20935d8 SC |
1821 | * Read the MSR specified by @index into @data. Select MSR specific fault |
1822 | * checks are bypassed if @host_initiated is %true. | |
1823 | * Returns 0 on success, non-0 otherwise. | |
1824 | * Assumes vcpu_load() was already called. | |
313a3dc7 | 1825 | */ |
edef5c36 PB |
1826 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
1827 | bool host_initiated) | |
609e36d3 PB |
1828 | { |
1829 | struct msr_data msr; | |
f20935d8 | 1830 | int ret; |
609e36d3 | 1831 | |
61a05d44 SC |
1832 | switch (index) { |
1833 | case MSR_TSC_AUX: | |
1834 | if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX)) | |
1835 | return 1; | |
1836 | ||
1837 | if (!host_initiated && | |
1838 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) && | |
1839 | !guest_cpuid_has(vcpu, X86_FEATURE_RDPID)) | |
1840 | return 1; | |
1841 | break; | |
1842 | } | |
1843 | ||
609e36d3 | 1844 | msr.index = index; |
f20935d8 | 1845 | msr.host_initiated = host_initiated; |
609e36d3 | 1846 | |
b3646477 | 1847 | ret = static_call(kvm_x86_get_msr)(vcpu, &msr); |
f20935d8 SC |
1848 | if (!ret) |
1849 | *data = msr.data; | |
1850 | return ret; | |
609e36d3 PB |
1851 | } |
1852 | ||
6abe9c13 PX |
1853 | static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu, |
1854 | u32 index, u64 *data, bool host_initiated) | |
1855 | { | |
1856 | int ret = __kvm_get_msr(vcpu, index, data, host_initiated); | |
1857 | ||
1858 | if (ret == KVM_MSR_RET_INVALID) { | |
1859 | /* Unconditionally clear *data for simplicity */ | |
1860 | *data = 0; | |
d632826f | 1861 | if (kvm_msr_ignored_check(index, 0, false)) |
cc4cb017 | 1862 | ret = 0; |
6abe9c13 PX |
1863 | } |
1864 | ||
1865 | return ret; | |
1866 | } | |
1867 | ||
ac8d6cad HW |
1868 | static int kvm_get_msr_with_filter(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
1869 | { | |
1870 | if (!kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ)) | |
1871 | return KVM_MSR_RET_FILTERED; | |
1872 | return kvm_get_msr_ignored_check(vcpu, index, data, false); | |
1873 | } | |
1874 | ||
1875 | static int kvm_set_msr_with_filter(struct kvm_vcpu *vcpu, u32 index, u64 data) | |
1876 | { | |
1877 | if (!kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE)) | |
1878 | return KVM_MSR_RET_FILTERED; | |
1879 | return kvm_set_msr_ignored_check(vcpu, index, data, false); | |
1880 | } | |
1881 | ||
f20935d8 | 1882 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
313a3dc7 | 1883 | { |
6abe9c13 | 1884 | return kvm_get_msr_ignored_check(vcpu, index, data, false); |
f20935d8 SC |
1885 | } |
1886 | EXPORT_SYMBOL_GPL(kvm_get_msr); | |
8fe8ab46 | 1887 | |
f20935d8 SC |
1888 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
1889 | { | |
6abe9c13 | 1890 | return kvm_set_msr_ignored_check(vcpu, index, data, false); |
f20935d8 SC |
1891 | } |
1892 | EXPORT_SYMBOL_GPL(kvm_set_msr); | |
1893 | ||
d2f7d498 | 1894 | static void complete_userspace_rdmsr(struct kvm_vcpu *vcpu) |
1ae09954 | 1895 | { |
d2f7d498 | 1896 | if (!vcpu->run->msr.error) { |
1ae09954 AG |
1897 | kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); |
1898 | kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); | |
1899 | } | |
d2f7d498 | 1900 | } |
1ae09954 | 1901 | |
d2f7d498 HW |
1902 | static int complete_emulated_msr_access(struct kvm_vcpu *vcpu) |
1903 | { | |
1904 | return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); | |
1ae09954 AG |
1905 | } |
1906 | ||
d2f7d498 HW |
1907 | static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu) |
1908 | { | |
1909 | complete_userspace_rdmsr(vcpu); | |
1910 | return complete_emulated_msr_access(vcpu); | |
1911 | } | |
1912 | ||
1913 | static int complete_fast_msr_access(struct kvm_vcpu *vcpu) | |
1ae09954 | 1914 | { |
b3646477 | 1915 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error); |
1ae09954 AG |
1916 | } |
1917 | ||
d2f7d498 HW |
1918 | static int complete_fast_rdmsr(struct kvm_vcpu *vcpu) |
1919 | { | |
1920 | complete_userspace_rdmsr(vcpu); | |
1921 | return complete_fast_msr_access(vcpu); | |
1922 | } | |
1923 | ||
1ae09954 AG |
1924 | static u64 kvm_msr_reason(int r) |
1925 | { | |
1926 | switch (r) { | |
cc4cb017 | 1927 | case KVM_MSR_RET_INVALID: |
1ae09954 | 1928 | return KVM_MSR_EXIT_REASON_UNKNOWN; |
cc4cb017 | 1929 | case KVM_MSR_RET_FILTERED: |
1a155254 | 1930 | return KVM_MSR_EXIT_REASON_FILTER; |
1ae09954 AG |
1931 | default: |
1932 | return KVM_MSR_EXIT_REASON_INVAL; | |
1933 | } | |
1934 | } | |
1935 | ||
1936 | static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index, | |
1937 | u32 exit_reason, u64 data, | |
1938 | int (*completion)(struct kvm_vcpu *vcpu), | |
1939 | int r) | |
1940 | { | |
1941 | u64 msr_reason = kvm_msr_reason(r); | |
1942 | ||
1943 | /* Check if the user wanted to know about this MSR fault */ | |
1944 | if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) | |
1945 | return 0; | |
1946 | ||
1947 | vcpu->run->exit_reason = exit_reason; | |
1948 | vcpu->run->msr.error = 0; | |
1949 | memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); | |
1950 | vcpu->run->msr.reason = msr_reason; | |
1951 | vcpu->run->msr.index = index; | |
1952 | vcpu->run->msr.data = data; | |
1953 | vcpu->arch.complete_userspace_io = completion; | |
1954 | ||
1955 | return 1; | |
1956 | } | |
1957 | ||
1edce0a9 SC |
1958 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
1959 | { | |
1960 | u32 ecx = kvm_rcx_read(vcpu); | |
1961 | u64 data; | |
1ae09954 AG |
1962 | int r; |
1963 | ||
ac8d6cad | 1964 | r = kvm_get_msr_with_filter(vcpu, ecx, &data); |
1edce0a9 | 1965 | |
8b474427 PB |
1966 | if (!r) { |
1967 | trace_kvm_msr_read(ecx, data); | |
1968 | ||
1969 | kvm_rax_write(vcpu, data & -1u); | |
1970 | kvm_rdx_write(vcpu, (data >> 32) & -1u); | |
1971 | } else { | |
d2f7d498 HW |
1972 | /* MSR read failed? See if we should ask user space */ |
1973 | if (kvm_msr_user_space(vcpu, ecx, KVM_EXIT_X86_RDMSR, 0, | |
1974 | complete_fast_rdmsr, r)) | |
1975 | return 0; | |
1edce0a9 | 1976 | trace_kvm_msr_read_ex(ecx); |
1edce0a9 SC |
1977 | } |
1978 | ||
b3646477 | 1979 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, r); |
1edce0a9 SC |
1980 | } |
1981 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); | |
1982 | ||
1983 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) | |
1984 | { | |
1985 | u32 ecx = kvm_rcx_read(vcpu); | |
1986 | u64 data = kvm_read_edx_eax(vcpu); | |
1ae09954 | 1987 | int r; |
1edce0a9 | 1988 | |
ac8d6cad | 1989 | r = kvm_set_msr_with_filter(vcpu, ecx, data); |
1ae09954 | 1990 | |
d2f7d498 | 1991 | if (!r) { |
8b474427 | 1992 | trace_kvm_msr_write(ecx, data); |
d2f7d498 HW |
1993 | } else { |
1994 | /* MSR write failed? See if we should ask user space */ | |
1995 | if (kvm_msr_user_space(vcpu, ecx, KVM_EXIT_X86_WRMSR, data, | |
1996 | complete_fast_msr_access, r)) | |
1997 | return 0; | |
1998 | /* Signal all other negative errors to userspace */ | |
1999 | if (r < 0) | |
2000 | return r; | |
1edce0a9 | 2001 | trace_kvm_msr_write_ex(ecx, data); |
d2f7d498 | 2002 | } |
1edce0a9 | 2003 | |
b3646477 | 2004 | return static_call(kvm_x86_complete_emulated_msr)(vcpu, r); |
1edce0a9 SC |
2005 | } |
2006 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); | |
2007 | ||
5ff3a351 SC |
2008 | int kvm_emulate_as_nop(struct kvm_vcpu *vcpu) |
2009 | { | |
2010 | return kvm_skip_emulated_instruction(vcpu); | |
2011 | } | |
2012 | EXPORT_SYMBOL_GPL(kvm_emulate_as_nop); | |
2013 | ||
2014 | int kvm_emulate_invd(struct kvm_vcpu *vcpu) | |
2015 | { | |
2016 | /* Treat an INVD instruction as a NOP and just skip it. */ | |
2017 | return kvm_emulate_as_nop(vcpu); | |
2018 | } | |
2019 | EXPORT_SYMBOL_GPL(kvm_emulate_invd); | |
2020 | ||
2021 | int kvm_emulate_mwait(struct kvm_vcpu *vcpu) | |
2022 | { | |
2023 | pr_warn_once("kvm: MWAIT instruction emulated as NOP!\n"); | |
2024 | return kvm_emulate_as_nop(vcpu); | |
2025 | } | |
2026 | EXPORT_SYMBOL_GPL(kvm_emulate_mwait); | |
2027 | ||
2028 | int kvm_handle_invalid_op(struct kvm_vcpu *vcpu) | |
2029 | { | |
2030 | kvm_queue_exception(vcpu, UD_VECTOR); | |
2031 | return 1; | |
2032 | } | |
2033 | EXPORT_SYMBOL_GPL(kvm_handle_invalid_op); | |
2034 | ||
2035 | int kvm_emulate_monitor(struct kvm_vcpu *vcpu) | |
2036 | { | |
2037 | pr_warn_once("kvm: MONITOR instruction emulated as NOP!\n"); | |
2038 | return kvm_emulate_as_nop(vcpu); | |
2039 | } | |
2040 | EXPORT_SYMBOL_GPL(kvm_emulate_monitor); | |
2041 | ||
d89d04ab | 2042 | static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) |
5a9f5443 | 2043 | { |
4ae7dc97 | 2044 | xfer_to_guest_mode_prepare(); |
5a9f5443 | 2045 | return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || |
72c3c0fe | 2046 | xfer_to_guest_mode_work_pending(); |
5a9f5443 | 2047 | } |
5a9f5443 | 2048 | |
1e9e2622 WL |
2049 | /* |
2050 | * The fast path for frequent and performance sensitive wrmsr emulation, | |
2051 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces | |
2052 | * the latency of virtual IPI by avoiding the expensive bits of transitioning | |
2053 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the | |
2054 | * other cases which must be called after interrupts are enabled on the host. | |
2055 | */ | |
2056 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) | |
2057 | { | |
e1be9ac8 WL |
2058 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) |
2059 | return 1; | |
2060 | ||
2061 | if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) && | |
bd17f417 SC |
2062 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
2063 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED) && | |
b9964ee3 SC |
2064 | ((u32)(data >> 32) != X2APIC_BROADCAST)) |
2065 | return kvm_x2apic_icr_write(vcpu->arch.apic, data); | |
1e9e2622 WL |
2066 | |
2067 | return 1; | |
2068 | } | |
2069 | ||
ae95f566 WL |
2070 | static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) |
2071 | { | |
2072 | if (!kvm_can_use_hv_timer(vcpu)) | |
2073 | return 1; | |
2074 | ||
2075 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
2076 | return 0; | |
2077 | } | |
2078 | ||
404d5d7b | 2079 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
1e9e2622 WL |
2080 | { |
2081 | u32 msr = kvm_rcx_read(vcpu); | |
8a1038de | 2082 | u64 data; |
404d5d7b | 2083 | fastpath_t ret = EXIT_FASTPATH_NONE; |
1e9e2622 WL |
2084 | |
2085 | switch (msr) { | |
2086 | case APIC_BASE_MSR + (APIC_ICR >> 4): | |
8a1038de | 2087 | data = kvm_read_edx_eax(vcpu); |
404d5d7b WL |
2088 | if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { |
2089 | kvm_skip_emulated_instruction(vcpu); | |
2090 | ret = EXIT_FASTPATH_EXIT_HANDLED; | |
80bc97f2 | 2091 | } |
1e9e2622 | 2092 | break; |
09141ec0 | 2093 | case MSR_IA32_TSC_DEADLINE: |
ae95f566 WL |
2094 | data = kvm_read_edx_eax(vcpu); |
2095 | if (!handle_fastpath_set_tscdeadline(vcpu, data)) { | |
2096 | kvm_skip_emulated_instruction(vcpu); | |
2097 | ret = EXIT_FASTPATH_REENTER_GUEST; | |
2098 | } | |
2099 | break; | |
1e9e2622 | 2100 | default: |
404d5d7b | 2101 | break; |
1e9e2622 WL |
2102 | } |
2103 | ||
404d5d7b | 2104 | if (ret != EXIT_FASTPATH_NONE) |
1e9e2622 | 2105 | trace_kvm_msr_write(msr, data); |
1e9e2622 | 2106 | |
404d5d7b | 2107 | return ret; |
1e9e2622 WL |
2108 | } |
2109 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); | |
2110 | ||
f20935d8 SC |
2111 | /* |
2112 | * Adapt set_msr() to msr_io()'s calling convention | |
2113 | */ | |
2114 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
2115 | { | |
6abe9c13 | 2116 | return kvm_get_msr_ignored_check(vcpu, index, data, true); |
f20935d8 SC |
2117 | } |
2118 | ||
2119 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
2120 | { | |
6abe9c13 | 2121 | return kvm_set_msr_ignored_check(vcpu, index, *data, true); |
313a3dc7 CO |
2122 | } |
2123 | ||
16e8d74d | 2124 | #ifdef CONFIG_X86_64 |
53fafdbb MT |
2125 | struct pvclock_clock { |
2126 | int vclock_mode; | |
2127 | u64 cycle_last; | |
2128 | u64 mask; | |
2129 | u32 mult; | |
2130 | u32 shift; | |
917f9475 PB |
2131 | u64 base_cycles; |
2132 | u64 offset; | |
53fafdbb MT |
2133 | }; |
2134 | ||
16e8d74d MT |
2135 | struct pvclock_gtod_data { |
2136 | seqcount_t seq; | |
2137 | ||
53fafdbb MT |
2138 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
2139 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ | |
16e8d74d | 2140 | |
917f9475 | 2141 | ktime_t offs_boot; |
55dd00a7 | 2142 | u64 wall_time_sec; |
16e8d74d MT |
2143 | }; |
2144 | ||
2145 | static struct pvclock_gtod_data pvclock_gtod_data; | |
2146 | ||
2147 | static void update_pvclock_gtod(struct timekeeper *tk) | |
2148 | { | |
2149 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; | |
2150 | ||
2151 | write_seqcount_begin(&vdata->seq); | |
2152 | ||
2153 | /* copy pvclock gtod data */ | |
b95a8a27 | 2154 | vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; |
876e7881 PZ |
2155 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
2156 | vdata->clock.mask = tk->tkr_mono.mask; | |
2157 | vdata->clock.mult = tk->tkr_mono.mult; | |
2158 | vdata->clock.shift = tk->tkr_mono.shift; | |
917f9475 PB |
2159 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
2160 | vdata->clock.offset = tk->tkr_mono.base; | |
16e8d74d | 2161 | |
b95a8a27 | 2162 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; |
53fafdbb MT |
2163 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
2164 | vdata->raw_clock.mask = tk->tkr_raw.mask; | |
2165 | vdata->raw_clock.mult = tk->tkr_raw.mult; | |
2166 | vdata->raw_clock.shift = tk->tkr_raw.shift; | |
917f9475 PB |
2167 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
2168 | vdata->raw_clock.offset = tk->tkr_raw.base; | |
16e8d74d | 2169 | |
55dd00a7 MT |
2170 | vdata->wall_time_sec = tk->xtime_sec; |
2171 | ||
917f9475 | 2172 | vdata->offs_boot = tk->offs_boot; |
53fafdbb | 2173 | |
16e8d74d MT |
2174 | write_seqcount_end(&vdata->seq); |
2175 | } | |
8171cd68 PB |
2176 | |
2177 | static s64 get_kvmclock_base_ns(void) | |
2178 | { | |
2179 | /* Count up from boot time, but with the frequency of the raw clock. */ | |
2180 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); | |
2181 | } | |
2182 | #else | |
2183 | static s64 get_kvmclock_base_ns(void) | |
2184 | { | |
2185 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ | |
2186 | return ktime_get_boottime_ns(); | |
2187 | } | |
16e8d74d MT |
2188 | #endif |
2189 | ||
55749769 | 2190 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs) |
18068523 | 2191 | { |
9ed3c444 AK |
2192 | int version; |
2193 | int r; | |
50d0a0f9 | 2194 | struct pvclock_wall_clock wc; |
629b5348 | 2195 | u32 wc_sec_hi; |
8171cd68 | 2196 | u64 wall_nsec; |
18068523 GOC |
2197 | |
2198 | if (!wall_clock) | |
2199 | return; | |
2200 | ||
9ed3c444 AK |
2201 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
2202 | if (r) | |
2203 | return; | |
2204 | ||
2205 | if (version & 1) | |
2206 | ++version; /* first time write, random junk */ | |
2207 | ||
2208 | ++version; | |
18068523 | 2209 | |
1dab1345 NK |
2210 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
2211 | return; | |
18068523 | 2212 | |
50d0a0f9 GH |
2213 | /* |
2214 | * The guest calculates current wall clock time by adding | |
34c238a1 | 2215 | * system time (updated by kvm_guest_time_update below) to the |
8171cd68 | 2216 | * wall clock specified here. We do the reverse here. |
50d0a0f9 | 2217 | */ |
8171cd68 | 2218 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
50d0a0f9 | 2219 | |
8171cd68 PB |
2220 | wc.nsec = do_div(wall_nsec, 1000000000); |
2221 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ | |
50d0a0f9 | 2222 | wc.version = version; |
18068523 GOC |
2223 | |
2224 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
2225 | ||
629b5348 JM |
2226 | if (sec_hi_ofs) { |
2227 | wc_sec_hi = wall_nsec >> 32; | |
2228 | kvm_write_guest(kvm, wall_clock + sec_hi_ofs, | |
2229 | &wc_sec_hi, sizeof(wc_sec_hi)); | |
2230 | } | |
2231 | ||
18068523 GOC |
2232 | version++; |
2233 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
2234 | } |
2235 | ||
5b9bb0eb OU |
2236 | static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time, |
2237 | bool old_msr, bool host_initiated) | |
2238 | { | |
2239 | struct kvm_arch *ka = &vcpu->kvm->arch; | |
2240 | ||
2241 | if (vcpu->vcpu_id == 0 && !host_initiated) { | |
1e293d1a | 2242 | if (ka->boot_vcpu_runs_old_kvmclock != old_msr) |
5b9bb0eb OU |
2243 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
2244 | ||
2245 | ka->boot_vcpu_runs_old_kvmclock = old_msr; | |
2246 | } | |
2247 | ||
2248 | vcpu->arch.time = system_time; | |
2249 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); | |
2250 | ||
2251 | /* we verify if the enable bit is set... */ | |
916d3608 DW |
2252 | if (system_time & 1) { |
2253 | kvm_gfn_to_pfn_cache_init(vcpu->kvm, &vcpu->arch.pv_time, vcpu, | |
2254 | KVM_HOST_USES_PFN, system_time & ~1ULL, | |
2255 | sizeof(struct pvclock_vcpu_time_info)); | |
2256 | } else { | |
2257 | kvm_gfn_to_pfn_cache_destroy(vcpu->kvm, &vcpu->arch.pv_time); | |
2258 | } | |
5b9bb0eb OU |
2259 | |
2260 | return; | |
2261 | } | |
2262 | ||
50d0a0f9 GH |
2263 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
2264 | { | |
b51012de PB |
2265 | do_shl32_div32(dividend, divisor); |
2266 | return dividend; | |
50d0a0f9 GH |
2267 | } |
2268 | ||
3ae13faa | 2269 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
5f4e3f88 | 2270 | s8 *pshift, u32 *pmultiplier) |
50d0a0f9 | 2271 | { |
5f4e3f88 | 2272 | uint64_t scaled64; |
50d0a0f9 GH |
2273 | int32_t shift = 0; |
2274 | uint64_t tps64; | |
2275 | uint32_t tps32; | |
2276 | ||
3ae13faa PB |
2277 | tps64 = base_hz; |
2278 | scaled64 = scaled_hz; | |
50933623 | 2279 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
50d0a0f9 GH |
2280 | tps64 >>= 1; |
2281 | shift--; | |
2282 | } | |
2283 | ||
2284 | tps32 = (uint32_t)tps64; | |
50933623 JK |
2285 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
2286 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) | |
5f4e3f88 ZA |
2287 | scaled64 >>= 1; |
2288 | else | |
2289 | tps32 <<= 1; | |
50d0a0f9 GH |
2290 | shift++; |
2291 | } | |
2292 | ||
5f4e3f88 ZA |
2293 | *pshift = shift; |
2294 | *pmultiplier = div_frac(scaled64, tps32); | |
50d0a0f9 GH |
2295 | } |
2296 | ||
d828199e | 2297 | #ifdef CONFIG_X86_64 |
16e8d74d | 2298 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
d828199e | 2299 | #endif |
16e8d74d | 2300 | |
c8076604 | 2301 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
69b0049a | 2302 | static unsigned long max_tsc_khz; |
c8076604 | 2303 | |
cc578287 | 2304 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
1e993611 | 2305 | { |
cc578287 ZA |
2306 | u64 v = (u64)khz * (1000000 + ppm); |
2307 | do_div(v, 1000000); | |
2308 | return v; | |
1e993611 JR |
2309 | } |
2310 | ||
1ab9287a IS |
2311 | static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier); |
2312 | ||
381d585c HZ |
2313 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
2314 | { | |
2315 | u64 ratio; | |
2316 | ||
2317 | /* Guest TSC same frequency as host TSC? */ | |
2318 | if (!scale) { | |
1ab9287a | 2319 | kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio); |
381d585c HZ |
2320 | return 0; |
2321 | } | |
2322 | ||
2323 | /* TSC scaling supported? */ | |
2324 | if (!kvm_has_tsc_control) { | |
2325 | if (user_tsc_khz > tsc_khz) { | |
2326 | vcpu->arch.tsc_catchup = 1; | |
2327 | vcpu->arch.tsc_always_catchup = 1; | |
2328 | return 0; | |
2329 | } else { | |
3f16a5c3 | 2330 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
381d585c HZ |
2331 | return -1; |
2332 | } | |
2333 | } | |
2334 | ||
2335 | /* TSC scaling required - calculate ratio */ | |
2336 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, | |
2337 | user_tsc_khz, tsc_khz); | |
2338 | ||
2339 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { | |
3f16a5c3 PB |
2340 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
2341 | user_tsc_khz); | |
381d585c HZ |
2342 | return -1; |
2343 | } | |
2344 | ||
1ab9287a | 2345 | kvm_vcpu_write_tsc_multiplier(vcpu, ratio); |
381d585c HZ |
2346 | return 0; |
2347 | } | |
2348 | ||
4941b8cb | 2349 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
759379dd | 2350 | { |
cc578287 ZA |
2351 | u32 thresh_lo, thresh_hi; |
2352 | int use_scaling = 0; | |
217fc9cf | 2353 | |
03ba32ca | 2354 | /* tsc_khz can be zero if TSC calibration fails */ |
4941b8cb | 2355 | if (user_tsc_khz == 0) { |
ad721883 | 2356 | /* set tsc_scaling_ratio to a safe value */ |
1ab9287a | 2357 | kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio); |
381d585c | 2358 | return -1; |
ad721883 | 2359 | } |
03ba32ca | 2360 | |
c285545f | 2361 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
3ae13faa | 2362 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
cc578287 ZA |
2363 | &vcpu->arch.virtual_tsc_shift, |
2364 | &vcpu->arch.virtual_tsc_mult); | |
4941b8cb | 2365 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
cc578287 ZA |
2366 | |
2367 | /* | |
2368 | * Compute the variation in TSC rate which is acceptable | |
2369 | * within the range of tolerance and decide if the | |
2370 | * rate being applied is within that bounds of the hardware | |
2371 | * rate. If so, no scaling or compensation need be done. | |
2372 | */ | |
2373 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); | |
2374 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); | |
4941b8cb PB |
2375 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
2376 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); | |
cc578287 ZA |
2377 | use_scaling = 1; |
2378 | } | |
4941b8cb | 2379 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
c285545f ZA |
2380 | } |
2381 | ||
2382 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) | |
2383 | { | |
e26101b1 | 2384 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
cc578287 ZA |
2385 | vcpu->arch.virtual_tsc_mult, |
2386 | vcpu->arch.virtual_tsc_shift); | |
e26101b1 | 2387 | tsc += vcpu->arch.this_tsc_write; |
c285545f ZA |
2388 | return tsc; |
2389 | } | |
2390 | ||
ba1f77c5 | 2391 | #ifdef CONFIG_X86_64 |
b0c39dc6 VK |
2392 | static inline int gtod_is_based_on_tsc(int mode) |
2393 | { | |
b95a8a27 | 2394 | return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; |
b0c39dc6 | 2395 | } |
ba1f77c5 | 2396 | #endif |
b0c39dc6 | 2397 | |
69b0049a | 2398 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
b48aa97e MT |
2399 | { |
2400 | #ifdef CONFIG_X86_64 | |
2401 | bool vcpus_matched; | |
b48aa97e MT |
2402 | struct kvm_arch *ka = &vcpu->kvm->arch; |
2403 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2404 | ||
2405 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
2406 | atomic_read(&vcpu->kvm->online_vcpus)); | |
2407 | ||
7f187922 MT |
2408 | /* |
2409 | * Once the masterclock is enabled, always perform request in | |
2410 | * order to update it. | |
2411 | * | |
2412 | * In order to enable masterclock, the host clocksource must be TSC | |
2413 | * and the vcpus need to have matched TSCs. When that happens, | |
2414 | * perform request to enable masterclock. | |
2415 | */ | |
2416 | if (ka->use_master_clock || | |
b0c39dc6 | 2417 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
b48aa97e MT |
2418 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
2419 | ||
2420 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, | |
2421 | atomic_read(&vcpu->kvm->online_vcpus), | |
2422 | ka->use_master_clock, gtod->clock.vclock_mode); | |
2423 | #endif | |
2424 | } | |
2425 | ||
35181e86 HZ |
2426 | /* |
2427 | * Multiply tsc by a fixed point number represented by ratio. | |
2428 | * | |
2429 | * The most significant 64-N bits (mult) of ratio represent the | |
2430 | * integral part of the fixed point number; the remaining N bits | |
2431 | * (frac) represent the fractional part, ie. ratio represents a fixed | |
2432 | * point number (mult + frac * 2^(-N)). | |
2433 | * | |
2434 | * N equals to kvm_tsc_scaling_ratio_frac_bits. | |
2435 | */ | |
2436 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) | |
2437 | { | |
2438 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); | |
2439 | } | |
2440 | ||
62711e5a | 2441 | u64 kvm_scale_tsc(u64 tsc, u64 ratio) |
35181e86 HZ |
2442 | { |
2443 | u64 _tsc = tsc; | |
35181e86 HZ |
2444 | |
2445 | if (ratio != kvm_default_tsc_scaling_ratio) | |
2446 | _tsc = __scale_tsc(ratio, tsc); | |
2447 | ||
2448 | return _tsc; | |
2449 | } | |
2450 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); | |
2451 | ||
9b399dfd | 2452 | static u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
07c1419a HZ |
2453 | { |
2454 | u64 tsc; | |
2455 | ||
62711e5a | 2456 | tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); |
07c1419a HZ |
2457 | |
2458 | return target_tsc - tsc; | |
2459 | } | |
2460 | ||
4ba76538 HZ |
2461 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
2462 | { | |
fe3eb504 | 2463 | return vcpu->arch.l1_tsc_offset + |
62711e5a | 2464 | kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); |
4ba76538 HZ |
2465 | } |
2466 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); | |
2467 | ||
83150f29 IS |
2468 | u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier) |
2469 | { | |
2470 | u64 nested_offset; | |
2471 | ||
2472 | if (l2_multiplier == kvm_default_tsc_scaling_ratio) | |
2473 | nested_offset = l1_offset; | |
2474 | else | |
2475 | nested_offset = mul_s64_u64_shr((s64) l1_offset, l2_multiplier, | |
2476 | kvm_tsc_scaling_ratio_frac_bits); | |
2477 | ||
2478 | nested_offset += l2_offset; | |
2479 | return nested_offset; | |
2480 | } | |
2481 | EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_offset); | |
2482 | ||
2483 | u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier) | |
2484 | { | |
2485 | if (l2_multiplier != kvm_default_tsc_scaling_ratio) | |
2486 | return mul_u64_u64_shr(l1_multiplier, l2_multiplier, | |
2487 | kvm_tsc_scaling_ratio_frac_bits); | |
2488 | ||
2489 | return l1_multiplier; | |
2490 | } | |
2491 | EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_multiplier); | |
2492 | ||
edcfe540 | 2493 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset) |
a545ab6a | 2494 | { |
edcfe540 IS |
2495 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2496 | vcpu->arch.l1_tsc_offset, | |
2497 | l1_offset); | |
2498 | ||
2499 | vcpu->arch.l1_tsc_offset = l1_offset; | |
2500 | ||
2501 | /* | |
2502 | * If we are here because L1 chose not to trap WRMSR to TSC then | |
2503 | * according to the spec this should set L1's TSC (as opposed to | |
2504 | * setting L1's offset for L2). | |
2505 | */ | |
2506 | if (is_guest_mode(vcpu)) | |
2507 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( | |
2508 | l1_offset, | |
2509 | static_call(kvm_x86_get_l2_tsc_offset)(vcpu), | |
2510 | static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu)); | |
2511 | else | |
2512 | vcpu->arch.tsc_offset = l1_offset; | |
2513 | ||
2514 | static_call(kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset); | |
a545ab6a LC |
2515 | } |
2516 | ||
1ab9287a IS |
2517 | static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier) |
2518 | { | |
2519 | vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; | |
2520 | ||
2521 | /* Userspace is changing the multiplier while L2 is active */ | |
2522 | if (is_guest_mode(vcpu)) | |
2523 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( | |
2524 | l1_multiplier, | |
2525 | static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu)); | |
2526 | else | |
2527 | vcpu->arch.tsc_scaling_ratio = l1_multiplier; | |
2528 | ||
2529 | if (kvm_has_tsc_control) | |
2530 | static_call(kvm_x86_write_tsc_multiplier)( | |
2531 | vcpu, vcpu->arch.tsc_scaling_ratio); | |
2532 | } | |
2533 | ||
b0c39dc6 VK |
2534 | static inline bool kvm_check_tsc_unstable(void) |
2535 | { | |
2536 | #ifdef CONFIG_X86_64 | |
2537 | /* | |
2538 | * TSC is marked unstable when we're running on Hyper-V, | |
2539 | * 'TSC page' clocksource is good. | |
2540 | */ | |
b95a8a27 | 2541 | if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) |
b0c39dc6 VK |
2542 | return false; |
2543 | #endif | |
2544 | return check_tsc_unstable(); | |
2545 | } | |
2546 | ||
58d4277b OU |
2547 | /* |
2548 | * Infers attempts to synchronize the guest's tsc from host writes. Sets the | |
2549 | * offset for the vcpu and tracks the TSC matching generation that the vcpu | |
2550 | * participates in. | |
2551 | */ | |
2552 | static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc, | |
2553 | u64 ns, bool matched) | |
2554 | { | |
2555 | struct kvm *kvm = vcpu->kvm; | |
2556 | ||
2557 | lockdep_assert_held(&kvm->arch.tsc_write_lock); | |
2558 | ||
2559 | /* | |
2560 | * We also track th most recent recorded KHZ, write and time to | |
2561 | * allow the matching interval to be extended at each write. | |
2562 | */ | |
2563 | kvm->arch.last_tsc_nsec = ns; | |
2564 | kvm->arch.last_tsc_write = tsc; | |
2565 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; | |
828ca896 | 2566 | kvm->arch.last_tsc_offset = offset; |
58d4277b OU |
2567 | |
2568 | vcpu->arch.last_guest_tsc = tsc; | |
2569 | ||
2570 | kvm_vcpu_write_tsc_offset(vcpu, offset); | |
2571 | ||
2572 | if (!matched) { | |
2573 | /* | |
2574 | * We split periods of matched TSC writes into generations. | |
2575 | * For each generation, we track the original measured | |
2576 | * nanosecond time, offset, and write, so if TSCs are in | |
2577 | * sync, we can match exact offset, and if not, we can match | |
2578 | * exact software computation in compute_guest_tsc() | |
2579 | * | |
2580 | * These values are tracked in kvm->arch.cur_xxx variables. | |
2581 | */ | |
2582 | kvm->arch.cur_tsc_generation++; | |
2583 | kvm->arch.cur_tsc_nsec = ns; | |
2584 | kvm->arch.cur_tsc_write = tsc; | |
2585 | kvm->arch.cur_tsc_offset = offset; | |
2586 | kvm->arch.nr_vcpus_matched_tsc = 0; | |
2587 | } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { | |
2588 | kvm->arch.nr_vcpus_matched_tsc++; | |
2589 | } | |
2590 | ||
2591 | /* Keep track of which generation this VCPU has synchronized to */ | |
2592 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; | |
2593 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; | |
2594 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; | |
2595 | ||
2596 | kvm_track_tsc_matching(vcpu); | |
2597 | } | |
2598 | ||
0c899c25 | 2599 | static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) |
99e3e30a ZA |
2600 | { |
2601 | struct kvm *kvm = vcpu->kvm; | |
f38e098f | 2602 | u64 offset, ns, elapsed; |
99e3e30a | 2603 | unsigned long flags; |
58d4277b | 2604 | bool matched = false; |
c5e8ec8e | 2605 | bool synchronizing = false; |
99e3e30a | 2606 | |
038f8c11 | 2607 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
9b399dfd | 2608 | offset = kvm_compute_l1_tsc_offset(vcpu, data); |
8171cd68 | 2609 | ns = get_kvmclock_base_ns(); |
f38e098f | 2610 | elapsed = ns - kvm->arch.last_tsc_nsec; |
5d3cb0f6 | 2611 | |
03ba32ca | 2612 | if (vcpu->arch.virtual_tsc_khz) { |
0c899c25 | 2613 | if (data == 0) { |
bd8fab39 DP |
2614 | /* |
2615 | * detection of vcpu initialization -- need to sync | |
2616 | * with other vCPUs. This particularly helps to keep | |
2617 | * kvm_clock stable after CPU hotplug | |
2618 | */ | |
2619 | synchronizing = true; | |
2620 | } else { | |
2621 | u64 tsc_exp = kvm->arch.last_tsc_write + | |
2622 | nsec_to_cycles(vcpu, elapsed); | |
2623 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; | |
2624 | /* | |
2625 | * Special case: TSC write with a small delta (1 second) | |
2626 | * of virtual cycle time against real time is | |
2627 | * interpreted as an attempt to synchronize the CPU. | |
2628 | */ | |
2629 | synchronizing = data < tsc_exp + tsc_hz && | |
2630 | data + tsc_hz > tsc_exp; | |
2631 | } | |
c5e8ec8e | 2632 | } |
f38e098f ZA |
2633 | |
2634 | /* | |
5d3cb0f6 ZA |
2635 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
2636 | * TSC, we add elapsed time in this computation. We could let the | |
2637 | * compensation code attempt to catch up if we fall behind, but | |
2638 | * it's better to try to match offsets from the beginning. | |
2639 | */ | |
c5e8ec8e | 2640 | if (synchronizing && |
5d3cb0f6 | 2641 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
b0c39dc6 | 2642 | if (!kvm_check_tsc_unstable()) { |
e26101b1 | 2643 | offset = kvm->arch.cur_tsc_offset; |
f38e098f | 2644 | } else { |
857e4099 | 2645 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
5d3cb0f6 | 2646 | data += delta; |
9b399dfd | 2647 | offset = kvm_compute_l1_tsc_offset(vcpu, data); |
f38e098f | 2648 | } |
b48aa97e | 2649 | matched = true; |
f38e098f | 2650 | } |
e26101b1 | 2651 | |
58d4277b | 2652 | __kvm_synchronize_tsc(vcpu, offset, data, ns, matched); |
e26101b1 | 2653 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
99e3e30a | 2654 | } |
e26101b1 | 2655 | |
58ea6767 HZ |
2656 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
2657 | s64 adjustment) | |
2658 | { | |
56ba77a4 | 2659 | u64 tsc_offset = vcpu->arch.l1_tsc_offset; |
326e7425 | 2660 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
58ea6767 HZ |
2661 | } |
2662 | ||
2663 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) | |
2664 | { | |
805d705f | 2665 | if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) |
58ea6767 | 2666 | WARN_ON(adjustment < 0); |
62711e5a | 2667 | adjustment = kvm_scale_tsc((u64) adjustment, |
fe3eb504 | 2668 | vcpu->arch.l1_tsc_scaling_ratio); |
ea26e4ec | 2669 | adjust_tsc_offset_guest(vcpu, adjustment); |
58ea6767 HZ |
2670 | } |
2671 | ||
d828199e MT |
2672 | #ifdef CONFIG_X86_64 |
2673 | ||
a5a1d1c2 | 2674 | static u64 read_tsc(void) |
d828199e | 2675 | { |
a5a1d1c2 | 2676 | u64 ret = (u64)rdtsc_ordered(); |
03b9730b | 2677 | u64 last = pvclock_gtod_data.clock.cycle_last; |
d828199e MT |
2678 | |
2679 | if (likely(ret >= last)) | |
2680 | return ret; | |
2681 | ||
2682 | /* | |
2683 | * GCC likes to generate cmov here, but this branch is extremely | |
6a6256f9 | 2684 | * predictable (it's just a function of time and the likely is |
d828199e MT |
2685 | * very likely) and there's a data dependence, so force GCC |
2686 | * to generate a branch instead. I don't barrier() because | |
2687 | * we don't actually need a barrier, and if this function | |
2688 | * ever gets inlined it will generate worse code. | |
2689 | */ | |
2690 | asm volatile (""); | |
2691 | return last; | |
2692 | } | |
2693 | ||
53fafdbb MT |
2694 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
2695 | int *mode) | |
d828199e MT |
2696 | { |
2697 | long v; | |
b0c39dc6 VK |
2698 | u64 tsc_pg_val; |
2699 | ||
53fafdbb | 2700 | switch (clock->vclock_mode) { |
b95a8a27 | 2701 | case VDSO_CLOCKMODE_HVCLOCK: |
b0c39dc6 VK |
2702 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
2703 | tsc_timestamp); | |
2704 | if (tsc_pg_val != U64_MAX) { | |
2705 | /* TSC page valid */ | |
b95a8a27 | 2706 | *mode = VDSO_CLOCKMODE_HVCLOCK; |
53fafdbb MT |
2707 | v = (tsc_pg_val - clock->cycle_last) & |
2708 | clock->mask; | |
b0c39dc6 VK |
2709 | } else { |
2710 | /* TSC page invalid */ | |
b95a8a27 | 2711 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 VK |
2712 | } |
2713 | break; | |
b95a8a27 TG |
2714 | case VDSO_CLOCKMODE_TSC: |
2715 | *mode = VDSO_CLOCKMODE_TSC; | |
b0c39dc6 | 2716 | *tsc_timestamp = read_tsc(); |
53fafdbb MT |
2717 | v = (*tsc_timestamp - clock->cycle_last) & |
2718 | clock->mask; | |
b0c39dc6 VK |
2719 | break; |
2720 | default: | |
b95a8a27 | 2721 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 | 2722 | } |
d828199e | 2723 | |
b95a8a27 | 2724 | if (*mode == VDSO_CLOCKMODE_NONE) |
b0c39dc6 | 2725 | *tsc_timestamp = v = 0; |
d828199e | 2726 | |
53fafdbb | 2727 | return v * clock->mult; |
d828199e MT |
2728 | } |
2729 | ||
53fafdbb | 2730 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
d828199e | 2731 | { |
cbcf2dd3 | 2732 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
d828199e | 2733 | unsigned long seq; |
d828199e | 2734 | int mode; |
cbcf2dd3 | 2735 | u64 ns; |
d828199e | 2736 | |
d828199e MT |
2737 | do { |
2738 | seq = read_seqcount_begin(>od->seq); | |
917f9475 | 2739 | ns = gtod->raw_clock.base_cycles; |
53fafdbb | 2740 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
917f9475 PB |
2741 | ns >>= gtod->raw_clock.shift; |
2742 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); | |
d828199e | 2743 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
cbcf2dd3 | 2744 | *t = ns; |
d828199e MT |
2745 | |
2746 | return mode; | |
2747 | } | |
2748 | ||
899a31f5 | 2749 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
55dd00a7 MT |
2750 | { |
2751 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2752 | unsigned long seq; | |
2753 | int mode; | |
2754 | u64 ns; | |
2755 | ||
2756 | do { | |
2757 | seq = read_seqcount_begin(>od->seq); | |
55dd00a7 | 2758 | ts->tv_sec = gtod->wall_time_sec; |
917f9475 | 2759 | ns = gtod->clock.base_cycles; |
53fafdbb | 2760 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
55dd00a7 MT |
2761 | ns >>= gtod->clock.shift; |
2762 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); | |
2763 | ||
2764 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); | |
2765 | ts->tv_nsec = ns; | |
2766 | ||
2767 | return mode; | |
2768 | } | |
2769 | ||
b0c39dc6 VK |
2770 | /* returns true if host is using TSC based clocksource */ |
2771 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) | |
d828199e | 2772 | { |
d828199e | 2773 | /* checked again under seqlock below */ |
b0c39dc6 | 2774 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
d828199e MT |
2775 | return false; |
2776 | ||
53fafdbb | 2777 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
b0c39dc6 | 2778 | tsc_timestamp)); |
d828199e | 2779 | } |
55dd00a7 | 2780 | |
b0c39dc6 | 2781 | /* returns true if host is using TSC based clocksource */ |
899a31f5 | 2782 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
b0c39dc6 | 2783 | u64 *tsc_timestamp) |
55dd00a7 MT |
2784 | { |
2785 | /* checked again under seqlock below */ | |
b0c39dc6 | 2786 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
55dd00a7 MT |
2787 | return false; |
2788 | ||
b0c39dc6 | 2789 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
55dd00a7 | 2790 | } |
d828199e MT |
2791 | #endif |
2792 | ||
2793 | /* | |
2794 | * | |
b48aa97e MT |
2795 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
2796 | * across virtual CPUs, the following condition is possible. | |
2797 | * Each numbered line represents an event visible to both | |
d828199e MT |
2798 | * CPUs at the next numbered event. |
2799 | * | |
2800 | * "timespecX" represents host monotonic time. "tscX" represents | |
2801 | * RDTSC value. | |
2802 | * | |
2803 | * VCPU0 on CPU0 | VCPU1 on CPU1 | |
2804 | * | |
2805 | * 1. read timespec0,tsc0 | |
2806 | * 2. | timespec1 = timespec0 + N | |
2807 | * | tsc1 = tsc0 + M | |
2808 | * 3. transition to guest | transition to guest | |
2809 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | | |
2810 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) | |
2811 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) | |
2812 | * | |
2813 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: | |
2814 | * | |
2815 | * - ret0 < ret1 | |
2816 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) | |
2817 | * ... | |
2818 | * - 0 < N - M => M < N | |
2819 | * | |
2820 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not | |
2821 | * always the case (the difference between two distinct xtime instances | |
2822 | * might be smaller then the difference between corresponding TSC reads, | |
2823 | * when updating guest vcpus pvclock areas). | |
2824 | * | |
2825 | * To avoid that problem, do not allow visibility of distinct | |
2826 | * system_timestamp/tsc_timestamp values simultaneously: use a master | |
2827 | * copy of host monotonic time values. Update that master copy | |
2828 | * in lockstep. | |
2829 | * | |
b48aa97e | 2830 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
d828199e MT |
2831 | * |
2832 | */ | |
2833 | ||
2834 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) | |
2835 | { | |
2836 | #ifdef CONFIG_X86_64 | |
2837 | struct kvm_arch *ka = &kvm->arch; | |
2838 | int vclock_mode; | |
b48aa97e MT |
2839 | bool host_tsc_clocksource, vcpus_matched; |
2840 | ||
869b4421 | 2841 | lockdep_assert_held(&kvm->arch.tsc_write_lock); |
b48aa97e MT |
2842 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == |
2843 | atomic_read(&kvm->online_vcpus)); | |
d828199e MT |
2844 | |
2845 | /* | |
2846 | * If the host uses TSC clock, then passthrough TSC as stable | |
2847 | * to the guest. | |
2848 | */ | |
b48aa97e | 2849 | host_tsc_clocksource = kvm_get_time_and_clockread( |
d828199e MT |
2850 | &ka->master_kernel_ns, |
2851 | &ka->master_cycle_now); | |
2852 | ||
16a96021 | 2853 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
a826faf1 | 2854 | && !ka->backwards_tsc_observed |
54750f2c | 2855 | && !ka->boot_vcpu_runs_old_kvmclock; |
b48aa97e | 2856 | |
d828199e MT |
2857 | if (ka->use_master_clock) |
2858 | atomic_set(&kvm_guest_has_master_clock, 1); | |
2859 | ||
2860 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; | |
b48aa97e MT |
2861 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
2862 | vcpus_matched); | |
d828199e MT |
2863 | #endif |
2864 | } | |
2865 | ||
6b6fcd28 | 2866 | static void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
2860c4b1 PB |
2867 | { |
2868 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); | |
2869 | } | |
2870 | ||
869b4421 | 2871 | static void __kvm_start_pvclock_update(struct kvm *kvm) |
2e762ff7 | 2872 | { |
869b4421 PB |
2873 | raw_spin_lock_irq(&kvm->arch.tsc_write_lock); |
2874 | write_seqcount_begin(&kvm->arch.pvclock_sc); | |
2875 | } | |
e880c6ea | 2876 | |
869b4421 PB |
2877 | static void kvm_start_pvclock_update(struct kvm *kvm) |
2878 | { | |
2e762ff7 | 2879 | kvm_make_mclock_inprogress_request(kvm); |
c2c647f9 | 2880 | |
2e762ff7 | 2881 | /* no guest entries from this point */ |
869b4421 | 2882 | __kvm_start_pvclock_update(kvm); |
6b6fcd28 | 2883 | } |
2e762ff7 | 2884 | |
6b6fcd28 PB |
2885 | static void kvm_end_pvclock_update(struct kvm *kvm) |
2886 | { | |
2887 | struct kvm_arch *ka = &kvm->arch; | |
2888 | struct kvm_vcpu *vcpu; | |
46808a4c | 2889 | unsigned long i; |
2e762ff7 | 2890 | |
869b4421 PB |
2891 | write_seqcount_end(&ka->pvclock_sc); |
2892 | raw_spin_unlock_irq(&ka->tsc_write_lock); | |
2e762ff7 | 2893 | kvm_for_each_vcpu(i, vcpu, kvm) |
105b21bb | 2894 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
2e762ff7 MT |
2895 | |
2896 | /* guest entries allowed */ | |
2897 | kvm_for_each_vcpu(i, vcpu, kvm) | |
72875d8a | 2898 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
2e762ff7 MT |
2899 | } |
2900 | ||
6b6fcd28 PB |
2901 | static void kvm_update_masterclock(struct kvm *kvm) |
2902 | { | |
2903 | kvm_hv_invalidate_tsc_page(kvm); | |
2904 | kvm_start_pvclock_update(kvm); | |
2905 | pvclock_update_vm_gtod_copy(kvm); | |
2906 | kvm_end_pvclock_update(kvm); | |
2e762ff7 MT |
2907 | } |
2908 | ||
869b4421 PB |
2909 | /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */ |
2910 | static void __get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) | |
108b249c | 2911 | { |
108b249c | 2912 | struct kvm_arch *ka = &kvm->arch; |
8b953440 | 2913 | struct pvclock_vcpu_time_info hv_clock; |
8b953440 | 2914 | |
e2c2206a WL |
2915 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
2916 | get_cpu(); | |
2917 | ||
869b4421 PB |
2918 | data->flags = 0; |
2919 | if (ka->use_master_clock && __this_cpu_read(cpu_tsc_khz)) { | |
c68dc1b5 OU |
2920 | #ifdef CONFIG_X86_64 |
2921 | struct timespec64 ts; | |
2922 | ||
2923 | if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { | |
2924 | data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; | |
2925 | data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; | |
2926 | } else | |
2927 | #endif | |
2928 | data->host_tsc = rdtsc(); | |
2929 | ||
869b4421 PB |
2930 | data->flags |= KVM_CLOCK_TSC_STABLE; |
2931 | hv_clock.tsc_timestamp = ka->master_cycle_now; | |
2932 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; | |
e70b57a6 WL |
2933 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, |
2934 | &hv_clock.tsc_shift, | |
2935 | &hv_clock.tsc_to_system_mul); | |
c68dc1b5 | 2936 | data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); |
55c0cefb OU |
2937 | } else { |
2938 | data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; | |
2939 | } | |
e2c2206a WL |
2940 | |
2941 | put_cpu(); | |
55c0cefb | 2942 | } |
e2c2206a | 2943 | |
869b4421 PB |
2944 | static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) |
2945 | { | |
2946 | struct kvm_arch *ka = &kvm->arch; | |
2947 | unsigned seq; | |
2948 | ||
2949 | do { | |
2950 | seq = read_seqcount_begin(&ka->pvclock_sc); | |
2951 | __get_kvmclock(kvm, data); | |
2952 | } while (read_seqcount_retry(&ka->pvclock_sc, seq)); | |
2953 | } | |
2954 | ||
55c0cefb OU |
2955 | u64 get_kvmclock_ns(struct kvm *kvm) |
2956 | { | |
2957 | struct kvm_clock_data data; | |
2958 | ||
55c0cefb OU |
2959 | get_kvmclock(kvm, &data); |
2960 | return data.clock; | |
108b249c PB |
2961 | } |
2962 | ||
916d3608 DW |
2963 | static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, |
2964 | struct gfn_to_pfn_cache *gpc, | |
2965 | unsigned int offset) | |
2966 | { | |
2967 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
2968 | struct pvclock_vcpu_time_info *guest_hv_clock; | |
2969 | unsigned long flags; | |
2970 | ||
2971 | read_lock_irqsave(&gpc->lock, flags); | |
2972 | while (!kvm_gfn_to_pfn_cache_check(v->kvm, gpc, gpc->gpa, | |
2973 | offset + sizeof(*guest_hv_clock))) { | |
2974 | read_unlock_irqrestore(&gpc->lock, flags); | |
2975 | ||
2976 | if (kvm_gfn_to_pfn_cache_refresh(v->kvm, gpc, gpc->gpa, | |
2977 | offset + sizeof(*guest_hv_clock))) | |
2978 | return; | |
2979 | ||
2980 | read_lock_irqsave(&gpc->lock, flags); | |
2981 | } | |
2982 | ||
2983 | guest_hv_clock = (void *)(gpc->khva + offset); | |
2984 | ||
2985 | /* | |
2986 | * This VCPU is paused, but it's legal for a guest to read another | |
2987 | * VCPU's kvmclock, so we really have to follow the specification where | |
2988 | * it says that version is odd if data is being modified, and even after | |
2989 | * it is consistent. | |
2990 | */ | |
2991 | ||
2992 | guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; | |
2993 | smp_wmb(); | |
2994 | ||
2995 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ | |
2996 | vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); | |
2997 | ||
2998 | if (vcpu->pvclock_set_guest_stopped_request) { | |
2999 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; | |
3000 | vcpu->pvclock_set_guest_stopped_request = false; | |
3001 | } | |
3002 | ||
3003 | memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); | |
3004 | smp_wmb(); | |
3005 | ||
3006 | guest_hv_clock->version = ++vcpu->hv_clock.version; | |
3007 | ||
3008 | mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); | |
3009 | read_unlock_irqrestore(&gpc->lock, flags); | |
3010 | ||
3011 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); | |
3012 | } | |
3013 | ||
34c238a1 | 3014 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
18068523 | 3015 | { |
78db6a50 | 3016 | unsigned long flags, tgt_tsc_khz; |
869b4421 | 3017 | unsigned seq; |
18068523 | 3018 | struct kvm_vcpu_arch *vcpu = &v->arch; |
d828199e | 3019 | struct kvm_arch *ka = &v->kvm->arch; |
f25e656d | 3020 | s64 kernel_ns; |
d828199e | 3021 | u64 tsc_timestamp, host_tsc; |
51d59c6b | 3022 | u8 pvclock_flags; |
d828199e MT |
3023 | bool use_master_clock; |
3024 | ||
3025 | kernel_ns = 0; | |
3026 | host_tsc = 0; | |
18068523 | 3027 | |
d828199e MT |
3028 | /* |
3029 | * If the host uses TSC clock, then passthrough TSC as stable | |
3030 | * to the guest. | |
3031 | */ | |
869b4421 PB |
3032 | do { |
3033 | seq = read_seqcount_begin(&ka->pvclock_sc); | |
3034 | use_master_clock = ka->use_master_clock; | |
3035 | if (use_master_clock) { | |
3036 | host_tsc = ka->master_cycle_now; | |
3037 | kernel_ns = ka->master_kernel_ns; | |
3038 | } | |
3039 | } while (read_seqcount_retry(&ka->pvclock_sc, seq)); | |
c09664bb MT |
3040 | |
3041 | /* Keep irq disabled to prevent changes to the clock */ | |
3042 | local_irq_save(flags); | |
78db6a50 PB |
3043 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
3044 | if (unlikely(tgt_tsc_khz == 0)) { | |
c09664bb MT |
3045 | local_irq_restore(flags); |
3046 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); | |
3047 | return 1; | |
3048 | } | |
d828199e | 3049 | if (!use_master_clock) { |
4ea1636b | 3050 | host_tsc = rdtsc(); |
8171cd68 | 3051 | kernel_ns = get_kvmclock_base_ns(); |
d828199e MT |
3052 | } |
3053 | ||
4ba76538 | 3054 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
d828199e | 3055 | |
c285545f ZA |
3056 | /* |
3057 | * We may have to catch up the TSC to match elapsed wall clock | |
3058 | * time for two reasons, even if kvmclock is used. | |
3059 | * 1) CPU could have been running below the maximum TSC rate | |
3060 | * 2) Broken TSC compensation resets the base at each VCPU | |
3061 | * entry to avoid unknown leaps of TSC even when running | |
3062 | * again on the same CPU. This may cause apparent elapsed | |
3063 | * time to disappear, and the guest to stand still or run | |
3064 | * very slowly. | |
3065 | */ | |
3066 | if (vcpu->tsc_catchup) { | |
3067 | u64 tsc = compute_guest_tsc(v, kernel_ns); | |
3068 | if (tsc > tsc_timestamp) { | |
f1e2b260 | 3069 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
c285545f ZA |
3070 | tsc_timestamp = tsc; |
3071 | } | |
50d0a0f9 GH |
3072 | } |
3073 | ||
18068523 GOC |
3074 | local_irq_restore(flags); |
3075 | ||
0d6dd2ff | 3076 | /* With all the info we got, fill in the values */ |
18068523 | 3077 | |
78db6a50 | 3078 | if (kvm_has_tsc_control) |
62711e5a | 3079 | tgt_tsc_khz = kvm_scale_tsc(tgt_tsc_khz, |
fe3eb504 | 3080 | v->arch.l1_tsc_scaling_ratio); |
78db6a50 PB |
3081 | |
3082 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { | |
3ae13faa | 3083 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
5f4e3f88 ZA |
3084 | &vcpu->hv_clock.tsc_shift, |
3085 | &vcpu->hv_clock.tsc_to_system_mul); | |
78db6a50 | 3086 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
8cfdc000 ZA |
3087 | } |
3088 | ||
1d5f066e | 3089 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
759379dd | 3090 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
28e4639a | 3091 | vcpu->last_guest_tsc = tsc_timestamp; |
51d59c6b | 3092 | |
d828199e | 3093 | /* If the host uses TSC clocksource, then it is stable */ |
0d6dd2ff | 3094 | pvclock_flags = 0; |
d828199e MT |
3095 | if (use_master_clock) |
3096 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; | |
3097 | ||
78c0337a MT |
3098 | vcpu->hv_clock.flags = pvclock_flags; |
3099 | ||
916d3608 DW |
3100 | if (vcpu->pv_time.active) |
3101 | kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0); | |
7caf9571 DW |
3102 | if (vcpu->xen.vcpu_info_cache.active) |
3103 | kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, | |
3104 | offsetof(struct compat_vcpu_info, time)); | |
69d413cf DW |
3105 | if (vcpu->xen.vcpu_time_info_cache.active) |
3106 | kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0); | |
94c245a2 | 3107 | if (!v->vcpu_idx) |
095cf55d | 3108 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); |
8cfdc000 | 3109 | return 0; |
c8076604 GH |
3110 | } |
3111 | ||
0061d53d MT |
3112 | /* |
3113 | * kvmclock updates which are isolated to a given vcpu, such as | |
3114 | * vcpu->cpu migration, should not allow system_timestamp from | |
3115 | * the rest of the vcpus to remain static. Otherwise ntp frequency | |
3116 | * correction applies to one vcpu's system_timestamp but not | |
3117 | * the others. | |
3118 | * | |
3119 | * So in those cases, request a kvmclock update for all vcpus. | |
7e44e449 AJ |
3120 | * We need to rate-limit these requests though, as they can |
3121 | * considerably slow guests that have a large number of vcpus. | |
3122 | * The time for a remote vcpu to update its kvmclock is bound | |
3123 | * by the delay we use to rate-limit the updates. | |
0061d53d MT |
3124 | */ |
3125 | ||
7e44e449 AJ |
3126 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
3127 | ||
3128 | static void kvmclock_update_fn(struct work_struct *work) | |
0061d53d | 3129 | { |
46808a4c | 3130 | unsigned long i; |
7e44e449 AJ |
3131 | struct delayed_work *dwork = to_delayed_work(work); |
3132 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
3133 | kvmclock_update_work); | |
3134 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
0061d53d MT |
3135 | struct kvm_vcpu *vcpu; |
3136 | ||
3137 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
105b21bb | 3138 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0061d53d MT |
3139 | kvm_vcpu_kick(vcpu); |
3140 | } | |
3141 | } | |
3142 | ||
7e44e449 AJ |
3143 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
3144 | { | |
3145 | struct kvm *kvm = v->kvm; | |
3146 | ||
105b21bb | 3147 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
7e44e449 AJ |
3148 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
3149 | KVMCLOCK_UPDATE_DELAY); | |
3150 | } | |
3151 | ||
332967a3 AJ |
3152 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
3153 | ||
3154 | static void kvmclock_sync_fn(struct work_struct *work) | |
3155 | { | |
3156 | struct delayed_work *dwork = to_delayed_work(work); | |
3157 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
3158 | kvmclock_sync_work); | |
3159 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
3160 | ||
630994b3 MT |
3161 | if (!kvmclock_periodic_sync) |
3162 | return; | |
3163 | ||
332967a3 AJ |
3164 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
3165 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
3166 | KVMCLOCK_SYNC_PERIOD); | |
3167 | } | |
3168 | ||
191c8137 BP |
3169 | /* |
3170 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. | |
3171 | */ | |
3172 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) | |
3173 | { | |
3174 | /* McStatusWrEn enabled? */ | |
23493d0a | 3175 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
191c8137 BP |
3176 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
3177 | ||
3178 | return false; | |
3179 | } | |
3180 | ||
9ffd986c | 3181 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 3182 | { |
890ca9ae YH |
3183 | u64 mcg_cap = vcpu->arch.mcg_cap; |
3184 | unsigned bank_num = mcg_cap & 0xff; | |
9ffd986c WL |
3185 | u32 msr = msr_info->index; |
3186 | u64 data = msr_info->data; | |
890ca9ae | 3187 | |
15c4a640 | 3188 | switch (msr) { |
15c4a640 | 3189 | case MSR_IA32_MCG_STATUS: |
890ca9ae | 3190 | vcpu->arch.mcg_status = data; |
15c4a640 | 3191 | break; |
c7ac679c | 3192 | case MSR_IA32_MCG_CTL: |
44883f01 PB |
3193 | if (!(mcg_cap & MCG_CTL_P) && |
3194 | (data || !msr_info->host_initiated)) | |
890ca9ae YH |
3195 | return 1; |
3196 | if (data != 0 && data != ~(u64)0) | |
44883f01 | 3197 | return 1; |
890ca9ae YH |
3198 | vcpu->arch.mcg_ctl = data; |
3199 | break; | |
3200 | default: | |
3201 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 3202 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
3203 | u32 offset = array_index_nospec( |
3204 | msr - MSR_IA32_MC0_CTL, | |
3205 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
3206 | ||
114be429 AP |
3207 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
3208 | * some Linux kernels though clear bit 10 in bank 4 to | |
3209 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore | |
3210 | * this to avoid an uncatched #GP in the guest | |
3211 | */ | |
890ca9ae | 3212 | if ((offset & 0x3) == 0 && |
114be429 | 3213 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
890ca9ae | 3214 | return -1; |
191c8137 BP |
3215 | |
3216 | /* MCi_STATUS */ | |
9ffd986c | 3217 | if (!msr_info->host_initiated && |
191c8137 BP |
3218 | (offset & 0x3) == 1 && data != 0) { |
3219 | if (!can_set_mci_status(vcpu)) | |
3220 | return -1; | |
3221 | } | |
3222 | ||
890ca9ae YH |
3223 | vcpu->arch.mce_banks[offset] = data; |
3224 | break; | |
3225 | } | |
3226 | return 1; | |
3227 | } | |
3228 | return 0; | |
3229 | } | |
3230 | ||
2635b5c4 VK |
3231 | static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) |
3232 | { | |
3233 | u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; | |
3234 | ||
3235 | return (vcpu->arch.apf.msr_en_val & mask) == mask; | |
3236 | } | |
3237 | ||
344d9588 GN |
3238 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
3239 | { | |
3240 | gpa_t gpa = data & ~0x3f; | |
3241 | ||
2635b5c4 VK |
3242 | /* Bits 4:5 are reserved, Should be zero */ |
3243 | if (data & 0x30) | |
344d9588 GN |
3244 | return 1; |
3245 | ||
66570e96 OU |
3246 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) && |
3247 | (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT)) | |
3248 | return 1; | |
3249 | ||
3250 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) && | |
3251 | (data & KVM_ASYNC_PF_DELIVERY_AS_INT)) | |
3252 | return 1; | |
3253 | ||
9d3c447c | 3254 | if (!lapic_in_kernel(vcpu)) |
d831de17 | 3255 | return data ? 1 : 0; |
9d3c447c | 3256 | |
2635b5c4 | 3257 | vcpu->arch.apf.msr_en_val = data; |
344d9588 | 3258 | |
2635b5c4 | 3259 | if (!kvm_pv_async_pf_enabled(vcpu)) { |
344d9588 GN |
3260 | kvm_clear_async_pf_completion_queue(vcpu); |
3261 | kvm_async_pf_hash_reset(vcpu); | |
3262 | return 0; | |
3263 | } | |
3264 | ||
4e335d9e | 3265 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
68fd66f1 | 3266 | sizeof(u64))) |
344d9588 GN |
3267 | return 1; |
3268 | ||
6adba527 | 3269 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
52a5c155 | 3270 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
2635b5c4 | 3271 | |
344d9588 | 3272 | kvm_async_pf_wakeup_all(vcpu); |
2635b5c4 VK |
3273 | |
3274 | return 0; | |
3275 | } | |
3276 | ||
3277 | static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data) | |
3278 | { | |
3279 | /* Bits 8-63 are reserved */ | |
3280 | if (data >> 8) | |
3281 | return 1; | |
3282 | ||
3283 | if (!lapic_in_kernel(vcpu)) | |
3284 | return 1; | |
3285 | ||
3286 | vcpu->arch.apf.msr_int_val = data; | |
3287 | ||
3288 | vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; | |
3289 | ||
344d9588 GN |
3290 | return 0; |
3291 | } | |
3292 | ||
12f9a48f GC |
3293 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
3294 | { | |
916d3608 | 3295 | kvm_gfn_to_pfn_cache_destroy(vcpu->kvm, &vcpu->arch.pv_time); |
49dedf0d | 3296 | vcpu->arch.time = 0; |
12f9a48f GC |
3297 | } |
3298 | ||
7780938c | 3299 | static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) |
f38a7b75 WL |
3300 | { |
3301 | ++vcpu->stat.tlb_flush; | |
e27bc044 | 3302 | static_call(kvm_x86_flush_tlb_all)(vcpu); |
f38a7b75 WL |
3303 | } |
3304 | ||
0baedd79 VK |
3305 | static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) |
3306 | { | |
3307 | ++vcpu->stat.tlb_flush; | |
b53e84ee LJ |
3308 | |
3309 | if (!tdp_enabled) { | |
61b05a9f | 3310 | /* |
b53e84ee LJ |
3311 | * A TLB flush on behalf of the guest is equivalent to |
3312 | * INVPCID(all), toggling CR4.PGE, etc., which requires | |
61b05a9f LJ |
3313 | * a forced sync of the shadow page tables. Ensure all the |
3314 | * roots are synced and the guest TLB in hardware is clean. | |
b53e84ee | 3315 | */ |
61b05a9f LJ |
3316 | kvm_mmu_sync_roots(vcpu); |
3317 | kvm_mmu_sync_prev_roots(vcpu); | |
b53e84ee LJ |
3318 | } |
3319 | ||
e27bc044 | 3320 | static_call(kvm_x86_flush_tlb_guest)(vcpu); |
0baedd79 VK |
3321 | } |
3322 | ||
40e5f908 SC |
3323 | |
3324 | static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu) | |
3325 | { | |
3326 | ++vcpu->stat.tlb_flush; | |
e27bc044 | 3327 | static_call(kvm_x86_flush_tlb_current)(vcpu); |
40e5f908 SC |
3328 | } |
3329 | ||
3330 | /* | |
3331 | * Service "local" TLB flush requests, which are specific to the current MMU | |
3332 | * context. In addition to the generic event handling in vcpu_enter_guest(), | |
3333 | * TLB flushes that are targeted at an MMU context also need to be serviced | |
3334 | * prior before nested VM-Enter/VM-Exit. | |
3335 | */ | |
3336 | void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu) | |
3337 | { | |
3338 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) | |
3339 | kvm_vcpu_flush_tlb_current(vcpu); | |
3340 | ||
3341 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) | |
3342 | kvm_vcpu_flush_tlb_guest(vcpu); | |
3343 | } | |
3344 | EXPORT_SYMBOL_GPL(kvm_service_local_tlb_flush_requests); | |
3345 | ||
c9aaa895 GC |
3346 | static void record_steal_time(struct kvm_vcpu *vcpu) |
3347 | { | |
7e2175eb DW |
3348 | struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; |
3349 | struct kvm_steal_time __user *st; | |
3350 | struct kvm_memslots *slots; | |
3351 | u64 steal; | |
3352 | u32 version; | |
b0431382 | 3353 | |
30b5c851 DW |
3354 | if (kvm_xen_msr_enabled(vcpu->kvm)) { |
3355 | kvm_xen_runstate_set_running(vcpu); | |
3356 | return; | |
3357 | } | |
3358 | ||
c9aaa895 GC |
3359 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
3360 | return; | |
3361 | ||
7e2175eb | 3362 | if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) |
c9aaa895 GC |
3363 | return; |
3364 | ||
7e2175eb DW |
3365 | slots = kvm_memslots(vcpu->kvm); |
3366 | ||
3367 | if (unlikely(slots->generation != ghc->generation || | |
3368 | kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { | |
3369 | gfn_t gfn = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; | |
3370 | ||
3371 | /* We rely on the fact that it fits in a single page. */ | |
3372 | BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); | |
3373 | ||
3374 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gfn, sizeof(*st)) || | |
3375 | kvm_is_error_hva(ghc->hva) || !ghc->memslot) | |
3376 | return; | |
3377 | } | |
3378 | ||
3379 | st = (struct kvm_steal_time __user *)ghc->hva; | |
f38a7b75 WL |
3380 | /* |
3381 | * Doing a TLB flush here, on the guest's behalf, can avoid | |
3382 | * expensive IPIs. | |
3383 | */ | |
66570e96 | 3384 | if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { |
7e2175eb DW |
3385 | u8 st_preempted = 0; |
3386 | int err = -EFAULT; | |
3387 | ||
3e067fd8 PB |
3388 | if (!user_access_begin(st, sizeof(*st))) |
3389 | return; | |
3390 | ||
7e2175eb DW |
3391 | asm volatile("1: xchgb %0, %2\n" |
3392 | "xor %1, %1\n" | |
3393 | "2:\n" | |
3394 | _ASM_EXTABLE_UA(1b, 2b) | |
964b7aa0 DW |
3395 | : "+q" (st_preempted), |
3396 | "+&r" (err), | |
3397 | "+m" (st->preempted)); | |
7e2175eb DW |
3398 | if (err) |
3399 | goto out; | |
3400 | ||
3401 | user_access_end(); | |
3402 | ||
3403 | vcpu->arch.st.preempted = 0; | |
af3511ff | 3404 | |
66570e96 | 3405 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, |
af3511ff LJ |
3406 | st_preempted & KVM_VCPU_FLUSH_TLB); |
3407 | if (st_preempted & KVM_VCPU_FLUSH_TLB) | |
66570e96 | 3408 | kvm_vcpu_flush_tlb_guest(vcpu); |
7e2175eb DW |
3409 | |
3410 | if (!user_access_begin(st, sizeof(*st))) | |
3411 | goto dirty; | |
1eff0ada | 3412 | } else { |
3e067fd8 PB |
3413 | if (!user_access_begin(st, sizeof(*st))) |
3414 | return; | |
3415 | ||
7e2175eb DW |
3416 | unsafe_put_user(0, &st->preempted, out); |
3417 | vcpu->arch.st.preempted = 0; | |
66570e96 | 3418 | } |
0b9f6c46 | 3419 | |
7e2175eb DW |
3420 | unsafe_get_user(version, &st->version, out); |
3421 | if (version & 1) | |
3422 | version += 1; /* first time write, random junk */ | |
35f3fae1 | 3423 | |
7e2175eb DW |
3424 | version += 1; |
3425 | unsafe_put_user(version, &st->version, out); | |
35f3fae1 WL |
3426 | |
3427 | smp_wmb(); | |
3428 | ||
7e2175eb DW |
3429 | unsafe_get_user(steal, &st->steal, out); |
3430 | steal += current->sched_info.run_delay - | |
c54cdf14 LC |
3431 | vcpu->arch.st.last_steal; |
3432 | vcpu->arch.st.last_steal = current->sched_info.run_delay; | |
7e2175eb | 3433 | unsafe_put_user(steal, &st->steal, out); |
35f3fae1 | 3434 | |
7e2175eb DW |
3435 | version += 1; |
3436 | unsafe_put_user(version, &st->version, out); | |
35f3fae1 | 3437 | |
7e2175eb DW |
3438 | out: |
3439 | user_access_end(); | |
3440 | dirty: | |
3441 | mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); | |
c9aaa895 GC |
3442 | } |
3443 | ||
8fe8ab46 | 3444 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 3445 | { |
5753785f | 3446 | bool pr = false; |
8fe8ab46 WA |
3447 | u32 msr = msr_info->index; |
3448 | u64 data = msr_info->data; | |
5753785f | 3449 | |
1232f8e6 | 3450 | if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) |
23200b7a | 3451 | return kvm_xen_write_hypercall_page(vcpu, data); |
1232f8e6 | 3452 | |
15c4a640 | 3453 | switch (msr) { |
2e32b719 | 3454 | case MSR_AMD64_NB_CFG: |
2e32b719 BP |
3455 | case MSR_IA32_UCODE_WRITE: |
3456 | case MSR_VM_HSAVE_PA: | |
3457 | case MSR_AMD64_PATCH_LOADER: | |
3458 | case MSR_AMD64_BU_CFG2: | |
405a353a | 3459 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3460 | case MSR_F15H_EX_CFG: |
2e32b719 BP |
3461 | break; |
3462 | ||
518e7b94 WL |
3463 | case MSR_IA32_UCODE_REV: |
3464 | if (msr_info->host_initiated) | |
3465 | vcpu->arch.microcode_version = data; | |
3466 | break; | |
0cf9135b SC |
3467 | case MSR_IA32_ARCH_CAPABILITIES: |
3468 | if (!msr_info->host_initiated) | |
3469 | return 1; | |
3470 | vcpu->arch.arch_capabilities = data; | |
3471 | break; | |
d574c539 VK |
3472 | case MSR_IA32_PERF_CAPABILITIES: { |
3473 | struct kvm_msr_entry msr_ent = {.index = msr, .data = 0}; | |
3474 | ||
3475 | if (!msr_info->host_initiated) | |
3476 | return 1; | |
1aa2abb3 | 3477 | if (kvm_get_msr_feature(&msr_ent)) |
d574c539 VK |
3478 | return 1; |
3479 | if (data & ~msr_ent.data) | |
3480 | return 1; | |
3481 | ||
3482 | vcpu->arch.perf_capabilities = data; | |
3483 | ||
3484 | return 0; | |
3485 | } | |
15c4a640 | 3486 | case MSR_EFER: |
11988499 | 3487 | return set_efer(vcpu, msr_info); |
8f1589d9 AP |
3488 | case MSR_K7_HWCR: |
3489 | data &= ~(u64)0x40; /* ignore flush filter disable */ | |
82494028 | 3490 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
a223c313 | 3491 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
191c8137 BP |
3492 | |
3493 | /* Handle McStatusWrEn */ | |
3494 | if (data == BIT_ULL(18)) { | |
3495 | vcpu->arch.msr_hwcr = data; | |
3496 | } else if (data != 0) { | |
a737f256 CD |
3497 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
3498 | data); | |
8f1589d9 AP |
3499 | return 1; |
3500 | } | |
15c4a640 | 3501 | break; |
f7c6d140 AP |
3502 | case MSR_FAM10H_MMIO_CONF_BASE: |
3503 | if (data != 0) { | |
a737f256 CD |
3504 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
3505 | "0x%llx\n", data); | |
f7c6d140 AP |
3506 | return 1; |
3507 | } | |
15c4a640 | 3508 | break; |
9ba075a6 | 3509 | case 0x200 ... 0x2ff: |
ff53604b | 3510 | return kvm_mtrr_set_msr(vcpu, msr, data); |
15c4a640 | 3511 | case MSR_IA32_APICBASE: |
58cb628d | 3512 | return kvm_set_apic_base(vcpu, msr_info); |
bf10bd0b | 3513 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
0105d1a5 | 3514 | return kvm_x2apic_msr_write(vcpu, msr, data); |
09141ec0 | 3515 | case MSR_IA32_TSC_DEADLINE: |
a3e06bbe LJ |
3516 | kvm_set_lapic_tscdeadline_msr(vcpu, data); |
3517 | break; | |
ba904635 | 3518 | case MSR_IA32_TSC_ADJUST: |
d6321d49 | 3519 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
ba904635 | 3520 | if (!msr_info->host_initiated) { |
d913b904 | 3521 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
d7add054 | 3522 | adjust_tsc_offset_guest(vcpu, adj); |
d9130a2d ZD |
3523 | /* Before back to guest, tsc_timestamp must be adjusted |
3524 | * as well, otherwise guest's percpu pvclock time could jump. | |
3525 | */ | |
3526 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
ba904635 WA |
3527 | } |
3528 | vcpu->arch.ia32_tsc_adjust_msr = data; | |
3529 | } | |
3530 | break; | |
15c4a640 | 3531 | case MSR_IA32_MISC_ENABLE: |
511a8556 WL |
3532 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
3533 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { | |
3534 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) | |
3535 | return 1; | |
3536 | vcpu->arch.ia32_misc_enable_msr = data; | |
aedbaf4f | 3537 | kvm_update_cpuid_runtime(vcpu); |
511a8556 WL |
3538 | } else { |
3539 | vcpu->arch.ia32_misc_enable_msr = data; | |
3540 | } | |
15c4a640 | 3541 | break; |
64d60670 PB |
3542 | case MSR_IA32_SMBASE: |
3543 | if (!msr_info->host_initiated) | |
3544 | return 1; | |
3545 | vcpu->arch.smbase = data; | |
3546 | break; | |
73f624f4 PB |
3547 | case MSR_IA32_POWER_CTL: |
3548 | vcpu->arch.msr_ia32_power_ctl = data; | |
3549 | break; | |
dd259935 | 3550 | case MSR_IA32_TSC: |
0c899c25 PB |
3551 | if (msr_info->host_initiated) { |
3552 | kvm_synchronize_tsc(vcpu, data); | |
3553 | } else { | |
9b399dfd | 3554 | u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; |
0c899c25 PB |
3555 | adjust_tsc_offset_guest(vcpu, adj); |
3556 | vcpu->arch.ia32_tsc_adjust_msr += adj; | |
3557 | } | |
dd259935 | 3558 | break; |
864e2ab2 AL |
3559 | case MSR_IA32_XSS: |
3560 | if (!msr_info->host_initiated && | |
3561 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
3562 | return 1; | |
3563 | /* | |
a1bead2a SC |
3564 | * KVM supports exposing PT to the guest, but does not support |
3565 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than | |
3566 | * XSAVES/XRSTORS to save/restore PT MSRs. | |
864e2ab2 | 3567 | */ |
408e9a31 | 3568 | if (data & ~supported_xss) |
864e2ab2 AL |
3569 | return 1; |
3570 | vcpu->arch.ia32_xss = data; | |
4c282e51 | 3571 | kvm_update_cpuid_runtime(vcpu); |
864e2ab2 | 3572 | break; |
52797bf9 LA |
3573 | case MSR_SMI_COUNT: |
3574 | if (!msr_info->host_initiated) | |
3575 | return 1; | |
3576 | vcpu->arch.smi_count = data; | |
3577 | break; | |
11c6bffa | 3578 | case MSR_KVM_WALL_CLOCK_NEW: |
66570e96 OU |
3579 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3580 | return 1; | |
3581 | ||
629b5348 JM |
3582 | vcpu->kvm->arch.wall_clock = data; |
3583 | kvm_write_wall_clock(vcpu->kvm, data, 0); | |
66570e96 | 3584 | break; |
18068523 | 3585 | case MSR_KVM_WALL_CLOCK: |
66570e96 OU |
3586 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3587 | return 1; | |
3588 | ||
629b5348 JM |
3589 | vcpu->kvm->arch.wall_clock = data; |
3590 | kvm_write_wall_clock(vcpu->kvm, data, 0); | |
18068523 | 3591 | break; |
11c6bffa | 3592 | case MSR_KVM_SYSTEM_TIME_NEW: |
66570e96 OU |
3593 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3594 | return 1; | |
3595 | ||
5b9bb0eb OU |
3596 | kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); |
3597 | break; | |
3598 | case MSR_KVM_SYSTEM_TIME: | |
66570e96 OU |
3599 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3600 | return 1; | |
3601 | ||
3602 | kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); | |
18068523 | 3603 | break; |
344d9588 | 3604 | case MSR_KVM_ASYNC_PF_EN: |
66570e96 OU |
3605 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3606 | return 1; | |
3607 | ||
344d9588 GN |
3608 | if (kvm_pv_enable_async_pf(vcpu, data)) |
3609 | return 1; | |
3610 | break; | |
2635b5c4 | 3611 | case MSR_KVM_ASYNC_PF_INT: |
66570e96 OU |
3612 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
3613 | return 1; | |
3614 | ||
2635b5c4 VK |
3615 | if (kvm_pv_enable_async_pf_int(vcpu, data)) |
3616 | return 1; | |
3617 | break; | |
557a961a | 3618 | case MSR_KVM_ASYNC_PF_ACK: |
0a31df68 | 3619 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
66570e96 | 3620 | return 1; |
557a961a VK |
3621 | if (data & 0x1) { |
3622 | vcpu->arch.apf.pageready_pending = false; | |
3623 | kvm_check_async_pf_completion(vcpu); | |
3624 | } | |
3625 | break; | |
c9aaa895 | 3626 | case MSR_KVM_STEAL_TIME: |
66570e96 OU |
3627 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
3628 | return 1; | |
c9aaa895 GC |
3629 | |
3630 | if (unlikely(!sched_info_on())) | |
3631 | return 1; | |
3632 | ||
3633 | if (data & KVM_STEAL_RESERVED_MASK) | |
3634 | return 1; | |
3635 | ||
c9aaa895 GC |
3636 | vcpu->arch.st.msr_val = data; |
3637 | ||
3638 | if (!(data & KVM_MSR_ENABLED)) | |
3639 | break; | |
3640 | ||
c9aaa895 GC |
3641 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
3642 | ||
3643 | break; | |
ae7a2a3f | 3644 | case MSR_KVM_PV_EOI_EN: |
66570e96 OU |
3645 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
3646 | return 1; | |
3647 | ||
77c3323f | 3648 | if (kvm_lapic_set_pv_eoi(vcpu, data, sizeof(u8))) |
ae7a2a3f MT |
3649 | return 1; |
3650 | break; | |
c9aaa895 | 3651 | |
2d5ba19b | 3652 | case MSR_KVM_POLL_CONTROL: |
66570e96 OU |
3653 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
3654 | return 1; | |
3655 | ||
2d5ba19b MT |
3656 | /* only enable bit supported */ |
3657 | if (data & (-1ULL << 1)) | |
3658 | return 1; | |
3659 | ||
3660 | vcpu->arch.msr_kvm_poll_control = data; | |
3661 | break; | |
3662 | ||
890ca9ae YH |
3663 | case MSR_IA32_MCG_CTL: |
3664 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 3665 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
9ffd986c | 3666 | return set_msr_mce(vcpu, msr_info); |
71db6023 | 3667 | |
6912ac32 WH |
3668 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
3669 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
df561f66 GS |
3670 | pr = true; |
3671 | fallthrough; | |
6912ac32 WH |
3672 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3673 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3674 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3675 | return kvm_pmu_set_msr(vcpu, msr_info); |
5753785f GN |
3676 | |
3677 | if (pr || data != 0) | |
a737f256 CD |
3678 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
3679 | "0x%x data 0x%llx\n", msr, data); | |
5753785f | 3680 | break; |
84e0cefa JS |
3681 | case MSR_K7_CLK_CTL: |
3682 | /* | |
3683 | * Ignore all writes to this no longer documented MSR. | |
3684 | * Writes are only relevant for old K7 processors, | |
3685 | * all pre-dating SVM, but a recommended workaround from | |
4a969980 | 3686 | * AMD for these chips. It is possible to specify the |
84e0cefa JS |
3687 | * affected processor models on the command line, hence |
3688 | * the need to ignore the workaround. | |
3689 | */ | |
3690 | break; | |
55cd8e5a | 3691 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3692 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3693 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3694 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3695 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3696 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3697 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3698 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3699 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e7d9513b AS |
3700 | return kvm_hv_set_msr_common(vcpu, msr, data, |
3701 | msr_info->host_initiated); | |
91c9c3ed | 3702 | case MSR_IA32_BBL_CR_CTL3: |
3703 | /* Drop writes to this legacy MSR -- see rdmsr | |
3704 | * counterpart for further detail. | |
3705 | */ | |
fab0aa3b EM |
3706 | if (report_ignored_msrs) |
3707 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", | |
3708 | msr, data); | |
91c9c3ed | 3709 | break; |
2b036c6b | 3710 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3711 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3712 | return 1; |
3713 | vcpu->arch.osvw.length = data; | |
3714 | break; | |
3715 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3716 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3717 | return 1; |
3718 | vcpu->arch.osvw.status = data; | |
3719 | break; | |
db2336a8 KH |
3720 | case MSR_PLATFORM_INFO: |
3721 | if (!msr_info->host_initiated || | |
db2336a8 KH |
3722 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
3723 | cpuid_fault_enabled(vcpu))) | |
3724 | return 1; | |
3725 | vcpu->arch.msr_platform_info = data; | |
3726 | break; | |
3727 | case MSR_MISC_FEATURES_ENABLES: | |
3728 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || | |
3729 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && | |
3730 | !supports_cpuid_fault(vcpu))) | |
3731 | return 1; | |
3732 | vcpu->arch.msr_misc_features_enables = data; | |
3733 | break; | |
820a6ee9 JL |
3734 | #ifdef CONFIG_X86_64 |
3735 | case MSR_IA32_XFD: | |
3736 | if (!msr_info->host_initiated && | |
3737 | !guest_cpuid_has(vcpu, X86_FEATURE_XFD)) | |
3738 | return 1; | |
3739 | ||
988896bb | 3740 | if (data & ~kvm_guest_supported_xfd(vcpu)) |
820a6ee9 JL |
3741 | return 1; |
3742 | ||
3743 | fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); | |
3744 | break; | |
548e8365 JL |
3745 | case MSR_IA32_XFD_ERR: |
3746 | if (!msr_info->host_initiated && | |
3747 | !guest_cpuid_has(vcpu, X86_FEATURE_XFD)) | |
3748 | return 1; | |
3749 | ||
988896bb | 3750 | if (data & ~kvm_guest_supported_xfd(vcpu)) |
548e8365 JL |
3751 | return 1; |
3752 | ||
3753 | vcpu->arch.guest_fpu.xfd_err = data; | |
3754 | break; | |
820a6ee9 | 3755 | #endif |
15c4a640 | 3756 | default: |
c6702c9d | 3757 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3758 | return kvm_pmu_set_msr(vcpu, msr_info); |
6abe9c13 | 3759 | return KVM_MSR_RET_INVALID; |
15c4a640 CO |
3760 | } |
3761 | return 0; | |
3762 | } | |
3763 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
3764 | ||
44883f01 | 3765 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
15c4a640 CO |
3766 | { |
3767 | u64 data; | |
890ca9ae YH |
3768 | u64 mcg_cap = vcpu->arch.mcg_cap; |
3769 | unsigned bank_num = mcg_cap & 0xff; | |
15c4a640 CO |
3770 | |
3771 | switch (msr) { | |
15c4a640 CO |
3772 | case MSR_IA32_P5_MC_ADDR: |
3773 | case MSR_IA32_P5_MC_TYPE: | |
890ca9ae YH |
3774 | data = 0; |
3775 | break; | |
15c4a640 | 3776 | case MSR_IA32_MCG_CAP: |
890ca9ae YH |
3777 | data = vcpu->arch.mcg_cap; |
3778 | break; | |
c7ac679c | 3779 | case MSR_IA32_MCG_CTL: |
44883f01 | 3780 | if (!(mcg_cap & MCG_CTL_P) && !host) |
890ca9ae YH |
3781 | return 1; |
3782 | data = vcpu->arch.mcg_ctl; | |
3783 | break; | |
3784 | case MSR_IA32_MCG_STATUS: | |
3785 | data = vcpu->arch.mcg_status; | |
3786 | break; | |
3787 | default: | |
3788 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 3789 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
3790 | u32 offset = array_index_nospec( |
3791 | msr - MSR_IA32_MC0_CTL, | |
3792 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
3793 | ||
890ca9ae YH |
3794 | data = vcpu->arch.mce_banks[offset]; |
3795 | break; | |
3796 | } | |
3797 | return 1; | |
3798 | } | |
3799 | *pdata = data; | |
3800 | return 0; | |
3801 | } | |
3802 | ||
609e36d3 | 3803 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
890ca9ae | 3804 | { |
609e36d3 | 3805 | switch (msr_info->index) { |
890ca9ae | 3806 | case MSR_IA32_PLATFORM_ID: |
15c4a640 | 3807 | case MSR_IA32_EBL_CR_POWERON: |
b5e2fec0 AG |
3808 | case MSR_IA32_LASTBRANCHFROMIP: |
3809 | case MSR_IA32_LASTBRANCHTOIP: | |
3810 | case MSR_IA32_LASTINTFROMIP: | |
3811 | case MSR_IA32_LASTINTTOIP: | |
059e5c32 | 3812 | case MSR_AMD64_SYSCFG: |
3afb1121 PB |
3813 | case MSR_K8_TSEG_ADDR: |
3814 | case MSR_K8_TSEG_MASK: | |
61a6bd67 | 3815 | case MSR_VM_HSAVE_PA: |
1fdbd48c | 3816 | case MSR_K8_INT_PENDING_MSG: |
c323c0e5 | 3817 | case MSR_AMD64_NB_CFG: |
f7c6d140 | 3818 | case MSR_FAM10H_MMIO_CONF_BASE: |
2e32b719 | 3819 | case MSR_AMD64_BU_CFG2: |
0c2df2a1 | 3820 | case MSR_IA32_PERF_CTL: |
405a353a | 3821 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3822 | case MSR_F15H_EX_CFG: |
2ca1a06a VS |
3823 | /* |
3824 | * Intel Sandy Bridge CPUs must support the RAPL (running average power | |
3825 | * limit) MSRs. Just return 0, as we do not want to expose the host | |
3826 | * data here. Do not conditionalize this on CPUID, as KVM does not do | |
3827 | * so for existing CPU-specific MSRs. | |
3828 | */ | |
3829 | case MSR_RAPL_POWER_UNIT: | |
3830 | case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */ | |
3831 | case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */ | |
3832 | case MSR_PKG_ENERGY_STATUS: /* Total package */ | |
3833 | case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ | |
609e36d3 | 3834 | msr_info->data = 0; |
15c4a640 | 3835 | break; |
c51eb52b | 3836 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
c28fa560 VK |
3837 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
3838 | return kvm_pmu_get_msr(vcpu, msr_info); | |
3839 | if (!msr_info->host_initiated) | |
3840 | return 1; | |
3841 | msr_info->data = 0; | |
3842 | break; | |
6912ac32 WH |
3843 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3844 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: | |
3845 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
3846 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3847 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3848 | return kvm_pmu_get_msr(vcpu, msr_info); |
609e36d3 | 3849 | msr_info->data = 0; |
5753785f | 3850 | break; |
742bc670 | 3851 | case MSR_IA32_UCODE_REV: |
518e7b94 | 3852 | msr_info->data = vcpu->arch.microcode_version; |
742bc670 | 3853 | break; |
0cf9135b SC |
3854 | case MSR_IA32_ARCH_CAPABILITIES: |
3855 | if (!msr_info->host_initiated && | |
3856 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) | |
3857 | return 1; | |
3858 | msr_info->data = vcpu->arch.arch_capabilities; | |
3859 | break; | |
d574c539 VK |
3860 | case MSR_IA32_PERF_CAPABILITIES: |
3861 | if (!msr_info->host_initiated && | |
3862 | !guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) | |
3863 | return 1; | |
3864 | msr_info->data = vcpu->arch.perf_capabilities; | |
3865 | break; | |
73f624f4 PB |
3866 | case MSR_IA32_POWER_CTL: |
3867 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; | |
3868 | break; | |
cc5b54dd ML |
3869 | case MSR_IA32_TSC: { |
3870 | /* | |
3871 | * Intel SDM states that MSR_IA32_TSC read adds the TSC offset | |
3872 | * even when not intercepted. AMD manual doesn't explicitly | |
3873 | * state this but appears to behave the same. | |
3874 | * | |
ee6fa053 | 3875 | * On userspace reads and writes, however, we unconditionally |
c0623f5e | 3876 | * return L1's TSC value to ensure backwards-compatible |
ee6fa053 | 3877 | * behavior for migration. |
cc5b54dd | 3878 | */ |
fe3eb504 | 3879 | u64 offset, ratio; |
cc5b54dd | 3880 | |
fe3eb504 IS |
3881 | if (msr_info->host_initiated) { |
3882 | offset = vcpu->arch.l1_tsc_offset; | |
3883 | ratio = vcpu->arch.l1_tsc_scaling_ratio; | |
3884 | } else { | |
3885 | offset = vcpu->arch.tsc_offset; | |
3886 | ratio = vcpu->arch.tsc_scaling_ratio; | |
3887 | } | |
3888 | ||
62711e5a | 3889 | msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; |
dd259935 | 3890 | break; |
cc5b54dd | 3891 | } |
9ba075a6 | 3892 | case MSR_MTRRcap: |
9ba075a6 | 3893 | case 0x200 ... 0x2ff: |
ff53604b | 3894 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
15c4a640 | 3895 | case 0xcd: /* fsb frequency */ |
609e36d3 | 3896 | msr_info->data = 3; |
15c4a640 | 3897 | break; |
7b914098 JS |
3898 | /* |
3899 | * MSR_EBC_FREQUENCY_ID | |
3900 | * Conservative value valid for even the basic CPU models. | |
3901 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of | |
3902 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, | |
3903 | * and 266MHz for model 3, or 4. Set Core Clock | |
3904 | * Frequency to System Bus Frequency Ratio to 1 (bits | |
3905 | * 31:24) even though these are only valid for CPU | |
3906 | * models > 2, however guests may end up dividing or | |
3907 | * multiplying by zero otherwise. | |
3908 | */ | |
3909 | case MSR_EBC_FREQUENCY_ID: | |
609e36d3 | 3910 | msr_info->data = 1 << 24; |
7b914098 | 3911 | break; |
15c4a640 | 3912 | case MSR_IA32_APICBASE: |
609e36d3 | 3913 | msr_info->data = kvm_get_apic_base(vcpu); |
15c4a640 | 3914 | break; |
bf10bd0b | 3915 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
609e36d3 | 3916 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
09141ec0 | 3917 | case MSR_IA32_TSC_DEADLINE: |
609e36d3 | 3918 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
a3e06bbe | 3919 | break; |
ba904635 | 3920 | case MSR_IA32_TSC_ADJUST: |
609e36d3 | 3921 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
ba904635 | 3922 | break; |
15c4a640 | 3923 | case MSR_IA32_MISC_ENABLE: |
609e36d3 | 3924 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 3925 | break; |
64d60670 PB |
3926 | case MSR_IA32_SMBASE: |
3927 | if (!msr_info->host_initiated) | |
3928 | return 1; | |
3929 | msr_info->data = vcpu->arch.smbase; | |
15c4a640 | 3930 | break; |
52797bf9 LA |
3931 | case MSR_SMI_COUNT: |
3932 | msr_info->data = vcpu->arch.smi_count; | |
3933 | break; | |
847f0ad8 AG |
3934 | case MSR_IA32_PERF_STATUS: |
3935 | /* TSC increment by tick */ | |
609e36d3 | 3936 | msr_info->data = 1000ULL; |
847f0ad8 | 3937 | /* CPU multiplier */ |
b0996ae4 | 3938 | msr_info->data |= (((uint64_t)4ULL) << 40); |
847f0ad8 | 3939 | break; |
15c4a640 | 3940 | case MSR_EFER: |
609e36d3 | 3941 | msr_info->data = vcpu->arch.efer; |
15c4a640 | 3942 | break; |
18068523 | 3943 | case MSR_KVM_WALL_CLOCK: |
1930e5dd OU |
3944 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3945 | return 1; | |
3946 | ||
3947 | msr_info->data = vcpu->kvm->arch.wall_clock; | |
3948 | break; | |
11c6bffa | 3949 | case MSR_KVM_WALL_CLOCK_NEW: |
1930e5dd OU |
3950 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3951 | return 1; | |
3952 | ||
609e36d3 | 3953 | msr_info->data = vcpu->kvm->arch.wall_clock; |
18068523 GOC |
3954 | break; |
3955 | case MSR_KVM_SYSTEM_TIME: | |
1930e5dd OU |
3956 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
3957 | return 1; | |
3958 | ||
3959 | msr_info->data = vcpu->arch.time; | |
3960 | break; | |
11c6bffa | 3961 | case MSR_KVM_SYSTEM_TIME_NEW: |
1930e5dd OU |
3962 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
3963 | return 1; | |
3964 | ||
609e36d3 | 3965 | msr_info->data = vcpu->arch.time; |
18068523 | 3966 | break; |
344d9588 | 3967 | case MSR_KVM_ASYNC_PF_EN: |
1930e5dd OU |
3968 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
3969 | return 1; | |
3970 | ||
2635b5c4 VK |
3971 | msr_info->data = vcpu->arch.apf.msr_en_val; |
3972 | break; | |
3973 | case MSR_KVM_ASYNC_PF_INT: | |
1930e5dd OU |
3974 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
3975 | return 1; | |
3976 | ||
2635b5c4 | 3977 | msr_info->data = vcpu->arch.apf.msr_int_val; |
344d9588 | 3978 | break; |
557a961a | 3979 | case MSR_KVM_ASYNC_PF_ACK: |
0a31df68 | 3980 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
1930e5dd OU |
3981 | return 1; |
3982 | ||
557a961a VK |
3983 | msr_info->data = 0; |
3984 | break; | |
c9aaa895 | 3985 | case MSR_KVM_STEAL_TIME: |
1930e5dd OU |
3986 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
3987 | return 1; | |
3988 | ||
609e36d3 | 3989 | msr_info->data = vcpu->arch.st.msr_val; |
c9aaa895 | 3990 | break; |
1d92128f | 3991 | case MSR_KVM_PV_EOI_EN: |
1930e5dd OU |
3992 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
3993 | return 1; | |
3994 | ||
609e36d3 | 3995 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
1d92128f | 3996 | break; |
2d5ba19b | 3997 | case MSR_KVM_POLL_CONTROL: |
1930e5dd OU |
3998 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
3999 | return 1; | |
4000 | ||
2d5ba19b MT |
4001 | msr_info->data = vcpu->arch.msr_kvm_poll_control; |
4002 | break; | |
890ca9ae YH |
4003 | case MSR_IA32_P5_MC_ADDR: |
4004 | case MSR_IA32_P5_MC_TYPE: | |
4005 | case MSR_IA32_MCG_CAP: | |
4006 | case MSR_IA32_MCG_CTL: | |
4007 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 4008 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
44883f01 PB |
4009 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
4010 | msr_info->host_initiated); | |
864e2ab2 AL |
4011 | case MSR_IA32_XSS: |
4012 | if (!msr_info->host_initiated && | |
4013 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
4014 | return 1; | |
4015 | msr_info->data = vcpu->arch.ia32_xss; | |
4016 | break; | |
84e0cefa JS |
4017 | case MSR_K7_CLK_CTL: |
4018 | /* | |
4019 | * Provide expected ramp-up count for K7. All other | |
4020 | * are set to zero, indicating minimum divisors for | |
4021 | * every field. | |
4022 | * | |
4023 | * This prevents guest kernels on AMD host with CPU | |
4024 | * type 6, model 8 and higher from exploding due to | |
4025 | * the rdmsr failing. | |
4026 | */ | |
609e36d3 | 4027 | msr_info->data = 0x20000000; |
84e0cefa | 4028 | break; |
55cd8e5a | 4029 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
4030 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
4031 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
4032 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
4033 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 4034 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
4035 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
4036 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
4037 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e83d5887 | 4038 | return kvm_hv_get_msr_common(vcpu, |
44883f01 PB |
4039 | msr_info->index, &msr_info->data, |
4040 | msr_info->host_initiated); | |
91c9c3ed | 4041 | case MSR_IA32_BBL_CR_CTL3: |
4042 | /* This legacy MSR exists but isn't fully documented in current | |
4043 | * silicon. It is however accessed by winxp in very narrow | |
4044 | * scenarios where it sets bit #19, itself documented as | |
4045 | * a "reserved" bit. Best effort attempt to source coherent | |
4046 | * read data here should the balance of the register be | |
4047 | * interpreted by the guest: | |
4048 | * | |
4049 | * L2 cache control register 3: 64GB range, 256KB size, | |
4050 | * enabled, latency 0x1, configured | |
4051 | */ | |
609e36d3 | 4052 | msr_info->data = 0xbe702111; |
91c9c3ed | 4053 | break; |
2b036c6b | 4054 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 4055 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 4056 | return 1; |
609e36d3 | 4057 | msr_info->data = vcpu->arch.osvw.length; |
2b036c6b BO |
4058 | break; |
4059 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 4060 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 4061 | return 1; |
609e36d3 | 4062 | msr_info->data = vcpu->arch.osvw.status; |
2b036c6b | 4063 | break; |
db2336a8 | 4064 | case MSR_PLATFORM_INFO: |
6fbbde9a DS |
4065 | if (!msr_info->host_initiated && |
4066 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) | |
4067 | return 1; | |
db2336a8 KH |
4068 | msr_info->data = vcpu->arch.msr_platform_info; |
4069 | break; | |
4070 | case MSR_MISC_FEATURES_ENABLES: | |
4071 | msr_info->data = vcpu->arch.msr_misc_features_enables; | |
4072 | break; | |
191c8137 BP |
4073 | case MSR_K7_HWCR: |
4074 | msr_info->data = vcpu->arch.msr_hwcr; | |
4075 | break; | |
820a6ee9 JL |
4076 | #ifdef CONFIG_X86_64 |
4077 | case MSR_IA32_XFD: | |
4078 | if (!msr_info->host_initiated && | |
4079 | !guest_cpuid_has(vcpu, X86_FEATURE_XFD)) | |
4080 | return 1; | |
4081 | ||
4082 | msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; | |
4083 | break; | |
548e8365 JL |
4084 | case MSR_IA32_XFD_ERR: |
4085 | if (!msr_info->host_initiated && | |
4086 | !guest_cpuid_has(vcpu, X86_FEATURE_XFD)) | |
4087 | return 1; | |
4088 | ||
4089 | msr_info->data = vcpu->arch.guest_fpu.xfd_err; | |
4090 | break; | |
820a6ee9 | 4091 | #endif |
15c4a640 | 4092 | default: |
c6702c9d | 4093 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 4094 | return kvm_pmu_get_msr(vcpu, msr_info); |
6abe9c13 | 4095 | return KVM_MSR_RET_INVALID; |
15c4a640 | 4096 | } |
15c4a640 CO |
4097 | return 0; |
4098 | } | |
4099 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
4100 | ||
313a3dc7 CO |
4101 | /* |
4102 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
4103 | * | |
4104 | * @return number of msrs set successfully. | |
4105 | */ | |
4106 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
4107 | struct kvm_msr_entry *entries, | |
4108 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
4109 | unsigned index, u64 *data)) | |
4110 | { | |
801e459a | 4111 | int i; |
313a3dc7 | 4112 | |
313a3dc7 CO |
4113 | for (i = 0; i < msrs->nmsrs; ++i) |
4114 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
4115 | break; | |
4116 | ||
313a3dc7 CO |
4117 | return i; |
4118 | } | |
4119 | ||
4120 | /* | |
4121 | * Read or write a bunch of msrs. Parameters are user addresses. | |
4122 | * | |
4123 | * @return number of msrs set successfully. | |
4124 | */ | |
4125 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
4126 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
4127 | unsigned index, u64 *data), | |
4128 | int writeback) | |
4129 | { | |
4130 | struct kvm_msrs msrs; | |
4131 | struct kvm_msr_entry *entries; | |
4132 | int r, n; | |
4133 | unsigned size; | |
4134 | ||
4135 | r = -EFAULT; | |
0e96f31e | 4136 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
313a3dc7 CO |
4137 | goto out; |
4138 | ||
4139 | r = -E2BIG; | |
4140 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
4141 | goto out; | |
4142 | ||
313a3dc7 | 4143 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
ff5c2c03 SL |
4144 | entries = memdup_user(user_msrs->entries, size); |
4145 | if (IS_ERR(entries)) { | |
4146 | r = PTR_ERR(entries); | |
313a3dc7 | 4147 | goto out; |
ff5c2c03 | 4148 | } |
313a3dc7 CO |
4149 | |
4150 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
4151 | if (r < 0) | |
4152 | goto out_free; | |
4153 | ||
4154 | r = -EFAULT; | |
4155 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
4156 | goto out_free; | |
4157 | ||
4158 | r = n; | |
4159 | ||
4160 | out_free: | |
7a73c028 | 4161 | kfree(entries); |
313a3dc7 CO |
4162 | out: |
4163 | return r; | |
4164 | } | |
4165 | ||
4d5422ce WL |
4166 | static inline bool kvm_can_mwait_in_guest(void) |
4167 | { | |
4168 | return boot_cpu_has(X86_FEATURE_MWAIT) && | |
8e9b29b6 KA |
4169 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
4170 | boot_cpu_has(X86_FEATURE_ARAT); | |
4d5422ce WL |
4171 | } |
4172 | ||
c21d54f0 VK |
4173 | static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu, |
4174 | struct kvm_cpuid2 __user *cpuid_arg) | |
4175 | { | |
4176 | struct kvm_cpuid2 cpuid; | |
4177 | int r; | |
4178 | ||
4179 | r = -EFAULT; | |
4180 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) | |
4181 | return r; | |
4182 | ||
4183 | r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
4184 | if (r) | |
4185 | return r; | |
4186 | ||
4187 | r = -EFAULT; | |
4188 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) | |
4189 | return r; | |
4190 | ||
4191 | return 0; | |
4192 | } | |
4193 | ||
784aa3d7 | 4194 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
018d00d2 | 4195 | { |
4d5422ce | 4196 | int r = 0; |
018d00d2 ZX |
4197 | |
4198 | switch (ext) { | |
4199 | case KVM_CAP_IRQCHIP: | |
4200 | case KVM_CAP_HLT: | |
4201 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
018d00d2 | 4202 | case KVM_CAP_SET_TSS_ADDR: |
07716717 | 4203 | case KVM_CAP_EXT_CPUID: |
9c15bb1d | 4204 | case KVM_CAP_EXT_EMUL_CPUID: |
c8076604 | 4205 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 4206 | case KVM_CAP_PIT: |
a28e4f5a | 4207 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 4208 | case KVM_CAP_MP_STATE: |
ed848624 | 4209 | case KVM_CAP_SYNC_MMU: |
a355c85c | 4210 | case KVM_CAP_USER_NMI: |
52d939a0 | 4211 | case KVM_CAP_REINJECT_CONTROL: |
4925663a | 4212 | case KVM_CAP_IRQ_INJECT_STATUS: |
d34e6b17 | 4213 | case KVM_CAP_IOEVENTFD: |
f848a5a8 | 4214 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
c5ff41ce | 4215 | case KVM_CAP_PIT2: |
e9f42757 | 4216 | case KVM_CAP_PIT_STATE2: |
b927a3ce | 4217 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
3cfc3092 | 4218 | case KVM_CAP_VCPU_EVENTS: |
55cd8e5a | 4219 | case KVM_CAP_HYPERV: |
10388a07 | 4220 | case KVM_CAP_HYPERV_VAPIC: |
c25bc163 | 4221 | case KVM_CAP_HYPERV_SPIN: |
5c919412 | 4222 | case KVM_CAP_HYPERV_SYNIC: |
efc479e6 | 4223 | case KVM_CAP_HYPERV_SYNIC2: |
d3457c87 | 4224 | case KVM_CAP_HYPERV_VP_INDEX: |
faeb7833 | 4225 | case KVM_CAP_HYPERV_EVENTFD: |
c1aea919 | 4226 | case KVM_CAP_HYPERV_TLBFLUSH: |
214ff83d | 4227 | case KVM_CAP_HYPERV_SEND_IPI: |
2bc39970 | 4228 | case KVM_CAP_HYPERV_CPUID: |
644f7067 | 4229 | case KVM_CAP_HYPERV_ENFORCE_CPUID: |
c21d54f0 | 4230 | case KVM_CAP_SYS_HYPERV_CPUID: |
ab9f4ecb | 4231 | case KVM_CAP_PCI_SEGMENT: |
a1efbe77 | 4232 | case KVM_CAP_DEBUGREGS: |
d2be1651 | 4233 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
2d5b5a66 | 4234 | case KVM_CAP_XSAVE: |
344d9588 | 4235 | case KVM_CAP_ASYNC_PF: |
72de5fa4 | 4236 | case KVM_CAP_ASYNC_PF_INT: |
92a1f12d | 4237 | case KVM_CAP_GET_TSC_KHZ: |
1c0b28c2 | 4238 | case KVM_CAP_KVMCLOCK_CTRL: |
4d8b81ab | 4239 | case KVM_CAP_READONLY_MEM: |
5f66b620 | 4240 | case KVM_CAP_HYPERV_TIME: |
100943c5 | 4241 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
defcf51f | 4242 | case KVM_CAP_TSC_DEADLINE_TIMER: |
90de4a18 | 4243 | case KVM_CAP_DISABLE_QUIRKS: |
d71ba788 | 4244 | case KVM_CAP_SET_BOOT_CPU_ID: |
49df6397 | 4245 | case KVM_CAP_SPLIT_IRQCHIP: |
460df4c1 | 4246 | case KVM_CAP_IMMEDIATE_EXIT: |
66bb8a06 | 4247 | case KVM_CAP_PMU_EVENT_FILTER: |
801e459a | 4248 | case KVM_CAP_GET_MSR_FEATURES: |
6fbbde9a | 4249 | case KVM_CAP_MSR_PLATFORM_INFO: |
c4f55198 | 4250 | case KVM_CAP_EXCEPTION_PAYLOAD: |
b9b2782c | 4251 | case KVM_CAP_SET_GUEST_DEBUG: |
1aa561b1 | 4252 | case KVM_CAP_LAST_CPU: |
1ae09954 | 4253 | case KVM_CAP_X86_USER_SPACE_MSR: |
1a155254 | 4254 | case KVM_CAP_X86_MSR_FILTER: |
66570e96 | 4255 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
fe7e9488 SC |
4256 | #ifdef CONFIG_X86_SGX_KVM |
4257 | case KVM_CAP_SGX_ATTRIBUTE: | |
4258 | #endif | |
54526d1f | 4259 | case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM: |
30d7c5d6 | 4260 | case KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM: |
6dba9403 | 4261 | case KVM_CAP_SREGS2: |
19238e75 | 4262 | case KVM_CAP_EXIT_ON_EMULATION_FAILURE: |
828ca896 | 4263 | case KVM_CAP_VCPU_ATTRIBUTES: |
dd6e6312 | 4264 | case KVM_CAP_SYS_ATTRIBUTES: |
8a289785 | 4265 | case KVM_CAP_VAPIC: |
127770ac | 4266 | case KVM_CAP_ENABLE_CAP: |
018d00d2 ZX |
4267 | r = 1; |
4268 | break; | |
0dbb1123 AK |
4269 | case KVM_CAP_EXIT_HYPERCALL: |
4270 | r = KVM_EXIT_HYPERCALL_VALID_MASK; | |
4271 | break; | |
7e582ccb ML |
4272 | case KVM_CAP_SET_GUEST_DEBUG2: |
4273 | return KVM_GUESTDBG_VALID_MASK; | |
b59b153d | 4274 | #ifdef CONFIG_KVM_XEN |
23200b7a JM |
4275 | case KVM_CAP_XEN_HVM: |
4276 | r = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR | | |
8d4e7e80 | 4277 | KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL | |
14243b38 DW |
4278 | KVM_XEN_HVM_CONFIG_SHARED_INFO | |
4279 | KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL; | |
30b5c851 DW |
4280 | if (sched_info_on()) |
4281 | r |= KVM_XEN_HVM_CONFIG_RUNSTATE; | |
23200b7a | 4282 | break; |
b59b153d | 4283 | #endif |
01643c51 KH |
4284 | case KVM_CAP_SYNC_REGS: |
4285 | r = KVM_SYNC_X86_VALID_FIELDS; | |
4286 | break; | |
e3fd9a93 | 4287 | case KVM_CAP_ADJUST_CLOCK: |
c68dc1b5 | 4288 | r = KVM_CLOCK_VALID_FLAGS; |
e3fd9a93 | 4289 | break; |
4d5422ce | 4290 | case KVM_CAP_X86_DISABLE_EXITS: |
b5170063 WL |
4291 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
4292 | KVM_X86_DISABLE_EXITS_CSTATE; | |
4d5422ce WL |
4293 | if(kvm_can_mwait_in_guest()) |
4294 | r |= KVM_X86_DISABLE_EXITS_MWAIT; | |
668fffa3 | 4295 | break; |
6d396b55 PB |
4296 | case KVM_CAP_X86_SMM: |
4297 | /* SMBASE is usually relocated above 1M on modern chipsets, | |
4298 | * and SMM handlers might indeed rely on 4G segment limits, | |
4299 | * so do not report SMM to be available if real mode is | |
4300 | * emulated via vm86 mode. Still, do not go to great lengths | |
4301 | * to avoid userspace's usage of the feature, because it is a | |
4302 | * fringe case that is not enabled except via specific settings | |
4303 | * of the module parameters. | |
4304 | */ | |
b3646477 | 4305 | r = static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE); |
6d396b55 | 4306 | break; |
f725230a | 4307 | case KVM_CAP_NR_VCPUS: |
2845e735 | 4308 | r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS); |
8c3ba334 SL |
4309 | break; |
4310 | case KVM_CAP_MAX_VCPUS: | |
f725230a AK |
4311 | r = KVM_MAX_VCPUS; |
4312 | break; | |
a86cb413 | 4313 | case KVM_CAP_MAX_VCPU_ID: |
a1c42dde | 4314 | r = KVM_MAX_VCPU_IDS; |
a86cb413 | 4315 | break; |
a68a6a72 MT |
4316 | case KVM_CAP_PV_MMU: /* obsolete */ |
4317 | r = 0; | |
2f333bcb | 4318 | break; |
890ca9ae YH |
4319 | case KVM_CAP_MCE: |
4320 | r = KVM_MAX_MCE_BANKS; | |
4321 | break; | |
2d5b5a66 | 4322 | case KVM_CAP_XCRS: |
d366bf7e | 4323 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
2d5b5a66 | 4324 | break; |
92a1f12d JR |
4325 | case KVM_CAP_TSC_CONTROL: |
4326 | r = kvm_has_tsc_control; | |
4327 | break; | |
37131313 RK |
4328 | case KVM_CAP_X2APIC_API: |
4329 | r = KVM_X2APIC_API_VALID_FLAGS; | |
4330 | break; | |
8fcc4b59 | 4331 | case KVM_CAP_NESTED_STATE: |
33b22172 PB |
4332 | r = kvm_x86_ops.nested_ops->get_state ? |
4333 | kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; | |
8fcc4b59 | 4334 | break; |
344c6c80 | 4335 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 4336 | r = kvm_x86_ops.enable_direct_tlbflush != NULL; |
5a0165f6 VK |
4337 | break; |
4338 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: | |
33b22172 | 4339 | r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; |
344c6c80 | 4340 | break; |
3edd6839 MG |
4341 | case KVM_CAP_SMALLER_MAXPHYADDR: |
4342 | r = (int) allow_smaller_maxphyaddr; | |
4343 | break; | |
004a0124 AJ |
4344 | case KVM_CAP_STEAL_TIME: |
4345 | r = sched_info_on(); | |
4346 | break; | |
fe6b6bc8 CQ |
4347 | case KVM_CAP_X86_BUS_LOCK_EXIT: |
4348 | if (kvm_has_bus_lock_exit) | |
4349 | r = KVM_BUS_LOCK_DETECTION_OFF | | |
4350 | KVM_BUS_LOCK_DETECTION_EXIT; | |
4351 | else | |
4352 | r = 0; | |
4353 | break; | |
be50b206 GZ |
4354 | case KVM_CAP_XSAVE2: { |
4355 | u64 guest_perm = xstate_get_guest_group_perm(); | |
4356 | ||
4357 | r = xstate_required_size(supported_xcr0 & guest_perm, false); | |
4358 | if (r < sizeof(struct kvm_xsave)) | |
4359 | r = sizeof(struct kvm_xsave); | |
4360 | break; | |
ba7bb663 DD |
4361 | case KVM_CAP_PMU_CAPABILITY: |
4362 | r = enable_pmu ? KVM_CAP_PMU_VALID_MASK : 0; | |
4363 | break; | |
be50b206 | 4364 | } |
6d849191 OU |
4365 | case KVM_CAP_DISABLE_QUIRKS2: |
4366 | r = KVM_X86_VALID_QUIRKS; | |
4367 | break; | |
018d00d2 | 4368 | default: |
018d00d2 ZX |
4369 | break; |
4370 | } | |
4371 | return r; | |
56f289a8 SC |
4372 | } |
4373 | ||
4374 | static inline void __user *kvm_get_attr_addr(struct kvm_device_attr *attr) | |
4375 | { | |
4376 | void __user *uaddr = (void __user*)(unsigned long)attr->addr; | |
018d00d2 | 4377 | |
56f289a8 | 4378 | if ((u64)(unsigned long)uaddr != attr->addr) |
6e37ec88 | 4379 | return ERR_PTR_USR(-EFAULT); |
56f289a8 | 4380 | return uaddr; |
018d00d2 ZX |
4381 | } |
4382 | ||
dd6e6312 PB |
4383 | static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) |
4384 | { | |
4385 | u64 __user *uaddr = kvm_get_attr_addr(attr); | |
4386 | ||
4387 | if (attr->group) | |
4388 | return -ENXIO; | |
4389 | ||
4390 | if (IS_ERR(uaddr)) | |
4391 | return PTR_ERR(uaddr); | |
4392 | ||
4393 | switch (attr->attr) { | |
4394 | case KVM_X86_XCOMP_GUEST_SUPP: | |
4395 | if (put_user(supported_xcr0, uaddr)) | |
4396 | return -EFAULT; | |
4397 | return 0; | |
4398 | default: | |
4399 | return -ENXIO; | |
4400 | break; | |
4401 | } | |
4402 | } | |
4403 | ||
4404 | static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr) | |
4405 | { | |
4406 | if (attr->group) | |
4407 | return -ENXIO; | |
4408 | ||
4409 | switch (attr->attr) { | |
4410 | case KVM_X86_XCOMP_GUEST_SUPP: | |
4411 | return 0; | |
4412 | default: | |
4413 | return -ENXIO; | |
4414 | } | |
4415 | } | |
4416 | ||
043405e1 CO |
4417 | long kvm_arch_dev_ioctl(struct file *filp, |
4418 | unsigned int ioctl, unsigned long arg) | |
4419 | { | |
4420 | void __user *argp = (void __user *)arg; | |
4421 | long r; | |
4422 | ||
4423 | switch (ioctl) { | |
4424 | case KVM_GET_MSR_INDEX_LIST: { | |
4425 | struct kvm_msr_list __user *user_msr_list = argp; | |
4426 | struct kvm_msr_list msr_list; | |
4427 | unsigned n; | |
4428 | ||
4429 | r = -EFAULT; | |
0e96f31e | 4430 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
043405e1 CO |
4431 | goto out; |
4432 | n = msr_list.nmsrs; | |
62ef68bb | 4433 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
0e96f31e | 4434 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
043405e1 CO |
4435 | goto out; |
4436 | r = -E2BIG; | |
e125e7b6 | 4437 | if (n < msr_list.nmsrs) |
043405e1 CO |
4438 | goto out; |
4439 | r = -EFAULT; | |
4440 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
4441 | num_msrs_to_save * sizeof(u32))) | |
4442 | goto out; | |
e125e7b6 | 4443 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
043405e1 | 4444 | &emulated_msrs, |
62ef68bb | 4445 | num_emulated_msrs * sizeof(u32))) |
043405e1 CO |
4446 | goto out; |
4447 | r = 0; | |
4448 | break; | |
4449 | } | |
9c15bb1d BP |
4450 | case KVM_GET_SUPPORTED_CPUID: |
4451 | case KVM_GET_EMULATED_CPUID: { | |
674eea0f AK |
4452 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
4453 | struct kvm_cpuid2 cpuid; | |
4454 | ||
4455 | r = -EFAULT; | |
0e96f31e | 4456 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
674eea0f | 4457 | goto out; |
9c15bb1d BP |
4458 | |
4459 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, | |
4460 | ioctl); | |
674eea0f AK |
4461 | if (r) |
4462 | goto out; | |
4463 | ||
4464 | r = -EFAULT; | |
0e96f31e | 4465 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
674eea0f AK |
4466 | goto out; |
4467 | r = 0; | |
4468 | break; | |
4469 | } | |
cf6c26ec | 4470 | case KVM_X86_GET_MCE_CAP_SUPPORTED: |
890ca9ae | 4471 | r = -EFAULT; |
c45dcc71 AR |
4472 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
4473 | sizeof(kvm_mce_cap_supported))) | |
890ca9ae YH |
4474 | goto out; |
4475 | r = 0; | |
4476 | break; | |
801e459a TL |
4477 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
4478 | struct kvm_msr_list __user *user_msr_list = argp; | |
4479 | struct kvm_msr_list msr_list; | |
4480 | unsigned int n; | |
4481 | ||
4482 | r = -EFAULT; | |
4483 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) | |
4484 | goto out; | |
4485 | n = msr_list.nmsrs; | |
4486 | msr_list.nmsrs = num_msr_based_features; | |
4487 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) | |
4488 | goto out; | |
4489 | r = -E2BIG; | |
4490 | if (n < msr_list.nmsrs) | |
4491 | goto out; | |
4492 | r = -EFAULT; | |
4493 | if (copy_to_user(user_msr_list->indices, &msr_based_features, | |
4494 | num_msr_based_features * sizeof(u32))) | |
4495 | goto out; | |
4496 | r = 0; | |
4497 | break; | |
4498 | } | |
4499 | case KVM_GET_MSRS: | |
4500 | r = msr_io(NULL, argp, do_get_msr_feature, 1); | |
4501 | break; | |
c21d54f0 VK |
4502 | case KVM_GET_SUPPORTED_HV_CPUID: |
4503 | r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp); | |
4504 | break; | |
dd6e6312 PB |
4505 | case KVM_GET_DEVICE_ATTR: { |
4506 | struct kvm_device_attr attr; | |
4507 | r = -EFAULT; | |
4508 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
4509 | break; | |
4510 | r = kvm_x86_dev_get_attr(&attr); | |
4511 | break; | |
4512 | } | |
4513 | case KVM_HAS_DEVICE_ATTR: { | |
4514 | struct kvm_device_attr attr; | |
4515 | r = -EFAULT; | |
4516 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
4517 | break; | |
4518 | r = kvm_x86_dev_has_attr(&attr); | |
4519 | break; | |
4520 | } | |
043405e1 CO |
4521 | default: |
4522 | r = -EINVAL; | |
cf6c26ec | 4523 | break; |
043405e1 CO |
4524 | } |
4525 | out: | |
4526 | return r; | |
4527 | } | |
4528 | ||
f5f48ee1 SY |
4529 | static void wbinvd_ipi(void *garbage) |
4530 | { | |
4531 | wbinvd(); | |
4532 | } | |
4533 | ||
4534 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
4535 | { | |
e0f0bbc5 | 4536 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
f5f48ee1 SY |
4537 | } |
4538 | ||
313a3dc7 CO |
4539 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
4540 | { | |
f5f48ee1 SY |
4541 | /* Address WBINVD may be executed by guest */ |
4542 | if (need_emulate_wbinvd(vcpu)) { | |
b3646477 | 4543 | if (static_call(kvm_x86_has_wbinvd_exit)()) |
f5f48ee1 SY |
4544 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
4545 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) | |
4546 | smp_call_function_single(vcpu->cpu, | |
4547 | wbinvd_ipi, NULL, 1); | |
4548 | } | |
4549 | ||
b3646477 | 4550 | static_call(kvm_x86_vcpu_load)(vcpu, cpu); |
8f6055cb | 4551 | |
37486135 BM |
4552 | /* Save host pkru register if supported */ |
4553 | vcpu->arch.host_pkru = read_pkru(); | |
4554 | ||
0dd6a6ed ZA |
4555 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
4556 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { | |
4557 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); | |
4558 | vcpu->arch.tsc_offset_adjustment = 0; | |
105b21bb | 4559 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed | 4560 | } |
8f6055cb | 4561 | |
b0c39dc6 | 4562 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
6f526ec5 | 4563 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
4ea1636b | 4564 | rdtsc() - vcpu->arch.last_host_tsc; |
e48672fa ZA |
4565 | if (tsc_delta < 0) |
4566 | mark_tsc_unstable("KVM discovered backwards TSC"); | |
ce7a058a | 4567 | |
b0c39dc6 | 4568 | if (kvm_check_tsc_unstable()) { |
9b399dfd | 4569 | u64 offset = kvm_compute_l1_tsc_offset(vcpu, |
b183aa58 | 4570 | vcpu->arch.last_guest_tsc); |
a545ab6a | 4571 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
c285545f | 4572 | vcpu->arch.tsc_catchup = 1; |
c285545f | 4573 | } |
a749e247 PB |
4574 | |
4575 | if (kvm_lapic_hv_timer_in_use(vcpu)) | |
4576 | kvm_lapic_restart_hv_timer(vcpu); | |
4577 | ||
d98d07ca MT |
4578 | /* |
4579 | * On a host with synchronized TSC, there is no need to update | |
4580 | * kvmclock on vcpu->cpu migration | |
4581 | */ | |
4582 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) | |
0061d53d | 4583 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
c285545f | 4584 | if (vcpu->cpu != cpu) |
1bd2009e | 4585 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
e48672fa | 4586 | vcpu->cpu = cpu; |
6b7d7e76 | 4587 | } |
c9aaa895 | 4588 | |
c9aaa895 | 4589 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
313a3dc7 CO |
4590 | } |
4591 | ||
0b9f6c46 PX |
4592 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
4593 | { | |
7e2175eb DW |
4594 | struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; |
4595 | struct kvm_steal_time __user *st; | |
4596 | struct kvm_memslots *slots; | |
4597 | static const u8 preempted = KVM_VCPU_PREEMPTED; | |
b0431382 | 4598 | |
0b9f6c46 PX |
4599 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
4600 | return; | |
4601 | ||
a6bd811f | 4602 | if (vcpu->arch.st.preempted) |
8c6de56a BO |
4603 | return; |
4604 | ||
7e2175eb DW |
4605 | /* This happens on process exit */ |
4606 | if (unlikely(current->mm != vcpu->kvm->mm)) | |
9c1a0744 | 4607 | return; |
b0431382 | 4608 | |
7e2175eb DW |
4609 | slots = kvm_memslots(vcpu->kvm); |
4610 | ||
4611 | if (unlikely(slots->generation != ghc->generation || | |
4612 | kvm_is_error_hva(ghc->hva) || !ghc->memslot)) | |
9c1a0744 | 4613 | return; |
b0431382 | 4614 | |
7e2175eb DW |
4615 | st = (struct kvm_steal_time __user *)ghc->hva; |
4616 | BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); | |
0b9f6c46 | 4617 | |
7e2175eb DW |
4618 | if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) |
4619 | vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; | |
0b9f6c46 | 4620 | |
7e2175eb | 4621 | mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); |
0b9f6c46 PX |
4622 | } |
4623 | ||
313a3dc7 CO |
4624 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
4625 | { | |
9c1a0744 WL |
4626 | int idx; |
4627 | ||
f1c6366e | 4628 | if (vcpu->preempted && !vcpu->arch.guest_state_protected) |
b3646477 | 4629 | vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); |
de63ad4c | 4630 | |
9c1a0744 WL |
4631 | /* |
4632 | * Take the srcu lock as memslots will be accessed to check the gfn | |
4633 | * cache generation against the memslots generation. | |
4634 | */ | |
4635 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
30b5c851 DW |
4636 | if (kvm_xen_msr_enabled(vcpu->kvm)) |
4637 | kvm_xen_runstate_set_preempted(vcpu); | |
4638 | else | |
4639 | kvm_steal_time_set_preempted(vcpu); | |
9c1a0744 | 4640 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
30b5c851 | 4641 | |
b3646477 | 4642 | static_call(kvm_x86_vcpu_put)(vcpu); |
4ea1636b | 4643 | vcpu->arch.last_host_tsc = rdtsc(); |
313a3dc7 CO |
4644 | } |
4645 | ||
313a3dc7 CO |
4646 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
4647 | struct kvm_lapic_state *s) | |
4648 | { | |
37c4dbf3 | 4649 | static_call_cond(kvm_x86_sync_pir_to_irr)(vcpu); |
d62caabb | 4650 | |
a92e2543 | 4651 | return kvm_apic_get_state(vcpu, s); |
313a3dc7 CO |
4652 | } |
4653 | ||
4654 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
4655 | struct kvm_lapic_state *s) | |
4656 | { | |
a92e2543 RK |
4657 | int r; |
4658 | ||
4659 | r = kvm_apic_set_state(vcpu, s); | |
4660 | if (r) | |
4661 | return r; | |
cb142eb7 | 4662 | update_cr8_intercept(vcpu); |
313a3dc7 CO |
4663 | |
4664 | return 0; | |
4665 | } | |
4666 | ||
127a457a MG |
4667 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
4668 | { | |
71cc849b PB |
4669 | /* |
4670 | * We can accept userspace's request for interrupt injection | |
4671 | * as long as we have a place to store the interrupt number. | |
4672 | * The actual injection will happen when the CPU is able to | |
4673 | * deliver the interrupt. | |
4674 | */ | |
4675 | if (kvm_cpu_has_extint(vcpu)) | |
4676 | return false; | |
4677 | ||
4678 | /* Acknowledging ExtINT does not happen if LINT0 is masked. */ | |
127a457a MG |
4679 | return (!lapic_in_kernel(vcpu) || |
4680 | kvm_apic_accept_pic_intr(vcpu)); | |
4681 | } | |
4682 | ||
782d422b MG |
4683 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) |
4684 | { | |
fa7a549d PB |
4685 | /* |
4686 | * Do not cause an interrupt window exit if an exception | |
4687 | * is pending or an event needs reinjection; userspace | |
4688 | * might want to inject the interrupt manually using KVM_SET_REGS | |
4689 | * or KVM_SET_SREGS. For that to work, we must be at an | |
4690 | * instruction boundary and with no events half-injected. | |
4691 | */ | |
4692 | return (kvm_arch_interrupt_allowed(vcpu) && | |
4693 | kvm_cpu_accept_dm_intr(vcpu) && | |
4694 | !kvm_event_needs_reinjection(vcpu) && | |
4695 | !vcpu->arch.exception.pending); | |
782d422b MG |
4696 | } |
4697 | ||
f77bc6a4 ZX |
4698 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
4699 | struct kvm_interrupt *irq) | |
4700 | { | |
02cdb50f | 4701 | if (irq->irq >= KVM_NR_INTERRUPTS) |
f77bc6a4 | 4702 | return -EINVAL; |
1c1a9ce9 SR |
4703 | |
4704 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
4705 | kvm_queue_interrupt(vcpu, irq->irq, false); | |
4706 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
4707 | return 0; | |
4708 | } | |
4709 | ||
4710 | /* | |
4711 | * With in-kernel LAPIC, we only use this to inject EXTINT, so | |
4712 | * fail for in-kernel 8259. | |
4713 | */ | |
4714 | if (pic_in_kernel(vcpu->kvm)) | |
f77bc6a4 | 4715 | return -ENXIO; |
f77bc6a4 | 4716 | |
1c1a9ce9 SR |
4717 | if (vcpu->arch.pending_external_vector != -1) |
4718 | return -EEXIST; | |
f77bc6a4 | 4719 | |
1c1a9ce9 | 4720 | vcpu->arch.pending_external_vector = irq->irq; |
934bf653 | 4721 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f77bc6a4 ZX |
4722 | return 0; |
4723 | } | |
4724 | ||
c4abb7c9 JK |
4725 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
4726 | { | |
c4abb7c9 | 4727 | kvm_inject_nmi(vcpu); |
c4abb7c9 JK |
4728 | |
4729 | return 0; | |
4730 | } | |
4731 | ||
f077825a PB |
4732 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
4733 | { | |
64d60670 PB |
4734 | kvm_make_request(KVM_REQ_SMI, vcpu); |
4735 | ||
f077825a PB |
4736 | return 0; |
4737 | } | |
4738 | ||
b209749f AK |
4739 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
4740 | struct kvm_tpr_access_ctl *tac) | |
4741 | { | |
4742 | if (tac->flags) | |
4743 | return -EINVAL; | |
4744 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
4745 | return 0; | |
4746 | } | |
4747 | ||
890ca9ae YH |
4748 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
4749 | u64 mcg_cap) | |
4750 | { | |
4751 | int r; | |
4752 | unsigned bank_num = mcg_cap & 0xff, bank; | |
4753 | ||
4754 | r = -EINVAL; | |
c4e0e4ab | 4755 | if (!bank_num || bank_num > KVM_MAX_MCE_BANKS) |
890ca9ae | 4756 | goto out; |
c45dcc71 | 4757 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
890ca9ae YH |
4758 | goto out; |
4759 | r = 0; | |
4760 | vcpu->arch.mcg_cap = mcg_cap; | |
4761 | /* Init IA32_MCG_CTL to all 1s */ | |
4762 | if (mcg_cap & MCG_CTL_P) | |
4763 | vcpu->arch.mcg_ctl = ~(u64)0; | |
4764 | /* Init IA32_MCi_CTL to all 1s */ | |
4765 | for (bank = 0; bank < bank_num; bank++) | |
4766 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; | |
c45dcc71 | 4767 | |
b3646477 | 4768 | static_call(kvm_x86_setup_mce)(vcpu); |
890ca9ae YH |
4769 | out: |
4770 | return r; | |
4771 | } | |
4772 | ||
4773 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, | |
4774 | struct kvm_x86_mce *mce) | |
4775 | { | |
4776 | u64 mcg_cap = vcpu->arch.mcg_cap; | |
4777 | unsigned bank_num = mcg_cap & 0xff; | |
4778 | u64 *banks = vcpu->arch.mce_banks; | |
4779 | ||
4780 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) | |
4781 | return -EINVAL; | |
4782 | /* | |
4783 | * if IA32_MCG_CTL is not all 1s, the uncorrected error | |
4784 | * reporting is disabled | |
4785 | */ | |
4786 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && | |
4787 | vcpu->arch.mcg_ctl != ~(u64)0) | |
4788 | return 0; | |
4789 | banks += 4 * mce->bank; | |
4790 | /* | |
4791 | * if IA32_MCi_CTL is not all 1s, the uncorrected error | |
4792 | * reporting is disabled for the bank | |
4793 | */ | |
4794 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) | |
4795 | return 0; | |
4796 | if (mce->status & MCI_STATUS_UC) { | |
4797 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || | |
fc78f519 | 4798 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
a8eeb04a | 4799 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
890ca9ae YH |
4800 | return 0; |
4801 | } | |
4802 | if (banks[1] & MCI_STATUS_VAL) | |
4803 | mce->status |= MCI_STATUS_OVER; | |
4804 | banks[2] = mce->addr; | |
4805 | banks[3] = mce->misc; | |
4806 | vcpu->arch.mcg_status = mce->mcg_status; | |
4807 | banks[1] = mce->status; | |
4808 | kvm_queue_exception(vcpu, MC_VECTOR); | |
4809 | } else if (!(banks[1] & MCI_STATUS_VAL) | |
4810 | || !(banks[1] & MCI_STATUS_UC)) { | |
4811 | if (banks[1] & MCI_STATUS_VAL) | |
4812 | mce->status |= MCI_STATUS_OVER; | |
4813 | banks[2] = mce->addr; | |
4814 | banks[3] = mce->misc; | |
4815 | banks[1] = mce->status; | |
4816 | } else | |
4817 | banks[1] |= MCI_STATUS_OVER; | |
4818 | return 0; | |
4819 | } | |
4820 | ||
3cfc3092 JK |
4821 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
4822 | struct kvm_vcpu_events *events) | |
4823 | { | |
7460fb4a | 4824 | process_nmi(vcpu); |
59073aaf | 4825 | |
1f7becf1 JZ |
4826 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
4827 | process_smi(vcpu); | |
4828 | ||
a06230b6 OU |
4829 | /* |
4830 | * In guest mode, payload delivery should be deferred, | |
4831 | * so that the L1 hypervisor can intercept #PF before | |
4832 | * CR2 is modified (or intercept #DB before DR6 is | |
4833 | * modified under nVMX). Unless the per-VM capability, | |
4834 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of | |
4835 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we | |
4836 | * opportunistically defer the exception payload, deliver it if the | |
4837 | * capability hasn't been requested before processing a | |
4838 | * KVM_GET_VCPU_EVENTS. | |
4839 | */ | |
4840 | if (!vcpu->kvm->arch.exception_payload_enabled && | |
4841 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) | |
4842 | kvm_deliver_exception_payload(vcpu); | |
4843 | ||
664f8e26 | 4844 | /* |
59073aaf JM |
4845 | * The API doesn't provide the instruction length for software |
4846 | * exceptions, so don't report them. As long as the guest RIP | |
4847 | * isn't advanced, we should expect to encounter the exception | |
4848 | * again. | |
664f8e26 | 4849 | */ |
59073aaf JM |
4850 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
4851 | events->exception.injected = 0; | |
4852 | events->exception.pending = 0; | |
4853 | } else { | |
4854 | events->exception.injected = vcpu->arch.exception.injected; | |
4855 | events->exception.pending = vcpu->arch.exception.pending; | |
4856 | /* | |
4857 | * For ABI compatibility, deliberately conflate | |
4858 | * pending and injected exceptions when | |
4859 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. | |
4860 | */ | |
4861 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4862 | events->exception.injected |= | |
4863 | vcpu->arch.exception.pending; | |
4864 | } | |
3cfc3092 JK |
4865 | events->exception.nr = vcpu->arch.exception.nr; |
4866 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; | |
4867 | events->exception.error_code = vcpu->arch.exception.error_code; | |
59073aaf JM |
4868 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
4869 | events->exception_payload = vcpu->arch.exception.payload; | |
3cfc3092 | 4870 | |
03b82a30 | 4871 | events->interrupt.injected = |
04140b41 | 4872 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
3cfc3092 | 4873 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
03b82a30 | 4874 | events->interrupt.soft = 0; |
b3646477 | 4875 | events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); |
3cfc3092 JK |
4876 | |
4877 | events->nmi.injected = vcpu->arch.nmi_injected; | |
7460fb4a | 4878 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
b3646477 | 4879 | events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu); |
97e69aa6 | 4880 | events->nmi.pad = 0; |
3cfc3092 | 4881 | |
66450a21 | 4882 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
3cfc3092 | 4883 | |
f077825a PB |
4884 | events->smi.smm = is_smm(vcpu); |
4885 | events->smi.pending = vcpu->arch.smi_pending; | |
4886 | events->smi.smm_inside_nmi = | |
4887 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); | |
4888 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); | |
4889 | ||
dab4b911 | 4890 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
f077825a PB |
4891 | | KVM_VCPUEVENT_VALID_SHADOW |
4892 | | KVM_VCPUEVENT_VALID_SMM); | |
59073aaf JM |
4893 | if (vcpu->kvm->arch.exception_payload_enabled) |
4894 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; | |
4895 | ||
97e69aa6 | 4896 | memset(&events->reserved, 0, sizeof(events->reserved)); |
3cfc3092 JK |
4897 | } |
4898 | ||
dc87275f | 4899 | static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm); |
6ef4e07e | 4900 | |
3cfc3092 JK |
4901 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
4902 | struct kvm_vcpu_events *events) | |
4903 | { | |
dab4b911 | 4904 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
48005f64 | 4905 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
f077825a | 4906 | | KVM_VCPUEVENT_VALID_SHADOW |
59073aaf JM |
4907 | | KVM_VCPUEVENT_VALID_SMM |
4908 | | KVM_VCPUEVENT_VALID_PAYLOAD)) | |
3cfc3092 JK |
4909 | return -EINVAL; |
4910 | ||
59073aaf JM |
4911 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
4912 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4913 | return -EINVAL; | |
4914 | if (events->exception.pending) | |
4915 | events->exception.injected = 0; | |
4916 | else | |
4917 | events->exception_has_payload = 0; | |
4918 | } else { | |
4919 | events->exception.pending = 0; | |
4920 | events->exception_has_payload = 0; | |
4921 | } | |
4922 | ||
4923 | if ((events->exception.injected || events->exception.pending) && | |
4924 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) | |
78e546c8 PB |
4925 | return -EINVAL; |
4926 | ||
28bf2888 DH |
4927 | /* INITs are latched while in SMM */ |
4928 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && | |
4929 | (events->smi.smm || events->smi.pending) && | |
4930 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) | |
4931 | return -EINVAL; | |
4932 | ||
7460fb4a | 4933 | process_nmi(vcpu); |
59073aaf JM |
4934 | vcpu->arch.exception.injected = events->exception.injected; |
4935 | vcpu->arch.exception.pending = events->exception.pending; | |
3cfc3092 JK |
4936 | vcpu->arch.exception.nr = events->exception.nr; |
4937 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; | |
4938 | vcpu->arch.exception.error_code = events->exception.error_code; | |
59073aaf JM |
4939 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
4940 | vcpu->arch.exception.payload = events->exception_payload; | |
3cfc3092 | 4941 | |
04140b41 | 4942 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
3cfc3092 JK |
4943 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
4944 | vcpu->arch.interrupt.soft = events->interrupt.soft; | |
48005f64 | 4945 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
b3646477 JB |
4946 | static_call(kvm_x86_set_interrupt_shadow)(vcpu, |
4947 | events->interrupt.shadow); | |
3cfc3092 JK |
4948 | |
4949 | vcpu->arch.nmi_injected = events->nmi.injected; | |
dab4b911 JK |
4950 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
4951 | vcpu->arch.nmi_pending = events->nmi.pending; | |
b3646477 | 4952 | static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked); |
3cfc3092 | 4953 | |
66450a21 | 4954 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
bce87cce | 4955 | lapic_in_kernel(vcpu)) |
66450a21 | 4956 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
3cfc3092 | 4957 | |
f077825a | 4958 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
f7e57078 SC |
4959 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
4960 | kvm_x86_ops.nested_ops->leave_nested(vcpu); | |
dc87275f | 4961 | kvm_smm_changed(vcpu, events->smi.smm); |
f7e57078 | 4962 | } |
6ef4e07e | 4963 | |
f077825a | 4964 | vcpu->arch.smi_pending = events->smi.pending; |
f4ef1910 WL |
4965 | |
4966 | if (events->smi.smm) { | |
4967 | if (events->smi.smm_inside_nmi) | |
4968 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; | |
f077825a | 4969 | else |
f4ef1910 | 4970 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
ff90afa7 LA |
4971 | } |
4972 | ||
4973 | if (lapic_in_kernel(vcpu)) { | |
4974 | if (events->smi.latched_init) | |
4975 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
4976 | else | |
4977 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
f077825a PB |
4978 | } |
4979 | } | |
4980 | ||
3842d135 AK |
4981 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
4982 | ||
3cfc3092 JK |
4983 | return 0; |
4984 | } | |
4985 | ||
a1efbe77 JK |
4986 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
4987 | struct kvm_debugregs *dbgregs) | |
4988 | { | |
73aaf249 JK |
4989 | unsigned long val; |
4990 | ||
a1efbe77 | 4991 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
16f8a6f9 | 4992 | kvm_get_dr(vcpu, 6, &val); |
73aaf249 | 4993 | dbgregs->dr6 = val; |
a1efbe77 JK |
4994 | dbgregs->dr7 = vcpu->arch.dr7; |
4995 | dbgregs->flags = 0; | |
97e69aa6 | 4996 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
a1efbe77 JK |
4997 | } |
4998 | ||
4999 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, | |
5000 | struct kvm_debugregs *dbgregs) | |
5001 | { | |
5002 | if (dbgregs->flags) | |
5003 | return -EINVAL; | |
5004 | ||
fd238002 | 5005 | if (!kvm_dr6_valid(dbgregs->dr6)) |
d14bdb55 | 5006 | return -EINVAL; |
fd238002 | 5007 | if (!kvm_dr7_valid(dbgregs->dr7)) |
d14bdb55 PB |
5008 | return -EINVAL; |
5009 | ||
a1efbe77 | 5010 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
ae561ede | 5011 | kvm_update_dr0123(vcpu); |
a1efbe77 JK |
5012 | vcpu->arch.dr6 = dbgregs->dr6; |
5013 | vcpu->arch.dr7 = dbgregs->dr7; | |
9926c9fd | 5014 | kvm_update_dr7(vcpu); |
a1efbe77 | 5015 | |
a1efbe77 JK |
5016 | return 0; |
5017 | } | |
5018 | ||
2d5b5a66 SY |
5019 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
5020 | struct kvm_xsave *guest_xsave) | |
5021 | { | |
d69c1382 | 5022 | if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) |
ed02b213 TL |
5023 | return; |
5024 | ||
d69c1382 TG |
5025 | fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, |
5026 | guest_xsave->region, | |
5027 | sizeof(guest_xsave->region), | |
5028 | vcpu->arch.pkru); | |
2d5b5a66 SY |
5029 | } |
5030 | ||
be50b206 GZ |
5031 | static void kvm_vcpu_ioctl_x86_get_xsave2(struct kvm_vcpu *vcpu, |
5032 | u8 *state, unsigned int size) | |
5033 | { | |
5034 | if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) | |
5035 | return; | |
5036 | ||
5037 | fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, | |
5038 | state, size, vcpu->arch.pkru); | |
5039 | } | |
5040 | ||
2d5b5a66 SY |
5041 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
5042 | struct kvm_xsave *guest_xsave) | |
5043 | { | |
d69c1382 | 5044 | if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) |
ed02b213 TL |
5045 | return 0; |
5046 | ||
d69c1382 TG |
5047 | return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, |
5048 | guest_xsave->region, | |
5049 | supported_xcr0, &vcpu->arch.pkru); | |
2d5b5a66 SY |
5050 | } |
5051 | ||
5052 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, | |
5053 | struct kvm_xcrs *guest_xcrs) | |
5054 | { | |
d366bf7e | 5055 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
2d5b5a66 SY |
5056 | guest_xcrs->nr_xcrs = 0; |
5057 | return; | |
5058 | } | |
5059 | ||
5060 | guest_xcrs->nr_xcrs = 1; | |
5061 | guest_xcrs->flags = 0; | |
5062 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; | |
5063 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; | |
5064 | } | |
5065 | ||
5066 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, | |
5067 | struct kvm_xcrs *guest_xcrs) | |
5068 | { | |
5069 | int i, r = 0; | |
5070 | ||
d366bf7e | 5071 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
2d5b5a66 SY |
5072 | return -EINVAL; |
5073 | ||
5074 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) | |
5075 | return -EINVAL; | |
5076 | ||
5077 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) | |
5078 | /* Only support XCR0 currently */ | |
c67a04cb | 5079 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
2d5b5a66 | 5080 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
c67a04cb | 5081 | guest_xcrs->xcrs[i].value); |
2d5b5a66 SY |
5082 | break; |
5083 | } | |
5084 | if (r) | |
5085 | r = -EINVAL; | |
5086 | return r; | |
5087 | } | |
5088 | ||
1c0b28c2 EM |
5089 | /* |
5090 | * kvm_set_guest_paused() indicates to the guest kernel that it has been | |
5091 | * stopped by the hypervisor. This function will be called from the host only. | |
5092 | * EINVAL is returned when the host attempts to set the flag for a guest that | |
5093 | * does not support pv clocks. | |
5094 | */ | |
5095 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) | |
5096 | { | |
916d3608 | 5097 | if (!vcpu->arch.pv_time.active) |
1c0b28c2 | 5098 | return -EINVAL; |
51d59c6b | 5099 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
1c0b28c2 EM |
5100 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
5101 | return 0; | |
5102 | } | |
5103 | ||
828ca896 OU |
5104 | static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu, |
5105 | struct kvm_device_attr *attr) | |
5106 | { | |
5107 | int r; | |
5108 | ||
5109 | switch (attr->attr) { | |
5110 | case KVM_VCPU_TSC_OFFSET: | |
5111 | r = 0; | |
5112 | break; | |
5113 | default: | |
5114 | r = -ENXIO; | |
5115 | } | |
5116 | ||
5117 | return r; | |
5118 | } | |
5119 | ||
5120 | static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu, | |
5121 | struct kvm_device_attr *attr) | |
5122 | { | |
56f289a8 | 5123 | u64 __user *uaddr = kvm_get_attr_addr(attr); |
828ca896 OU |
5124 | int r; |
5125 | ||
56f289a8 SC |
5126 | if (IS_ERR(uaddr)) |
5127 | return PTR_ERR(uaddr); | |
828ca896 OU |
5128 | |
5129 | switch (attr->attr) { | |
5130 | case KVM_VCPU_TSC_OFFSET: | |
5131 | r = -EFAULT; | |
5132 | if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) | |
5133 | break; | |
5134 | r = 0; | |
5135 | break; | |
5136 | default: | |
5137 | r = -ENXIO; | |
5138 | } | |
5139 | ||
5140 | return r; | |
5141 | } | |
5142 | ||
5143 | static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu, | |
5144 | struct kvm_device_attr *attr) | |
5145 | { | |
56f289a8 | 5146 | u64 __user *uaddr = kvm_get_attr_addr(attr); |
828ca896 OU |
5147 | struct kvm *kvm = vcpu->kvm; |
5148 | int r; | |
5149 | ||
56f289a8 SC |
5150 | if (IS_ERR(uaddr)) |
5151 | return PTR_ERR(uaddr); | |
828ca896 OU |
5152 | |
5153 | switch (attr->attr) { | |
5154 | case KVM_VCPU_TSC_OFFSET: { | |
5155 | u64 offset, tsc, ns; | |
5156 | unsigned long flags; | |
5157 | bool matched; | |
5158 | ||
5159 | r = -EFAULT; | |
5160 | if (get_user(offset, uaddr)) | |
5161 | break; | |
5162 | ||
5163 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); | |
5164 | ||
5165 | matched = (vcpu->arch.virtual_tsc_khz && | |
5166 | kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && | |
5167 | kvm->arch.last_tsc_offset == offset); | |
5168 | ||
62711e5a | 5169 | tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; |
828ca896 OU |
5170 | ns = get_kvmclock_base_ns(); |
5171 | ||
5172 | __kvm_synchronize_tsc(vcpu, offset, tsc, ns, matched); | |
5173 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); | |
5174 | ||
5175 | r = 0; | |
5176 | break; | |
5177 | } | |
5178 | default: | |
5179 | r = -ENXIO; | |
5180 | } | |
5181 | ||
5182 | return r; | |
5183 | } | |
5184 | ||
5185 | static int kvm_vcpu_ioctl_device_attr(struct kvm_vcpu *vcpu, | |
5186 | unsigned int ioctl, | |
5187 | void __user *argp) | |
5188 | { | |
5189 | struct kvm_device_attr attr; | |
5190 | int r; | |
5191 | ||
5192 | if (copy_from_user(&attr, argp, sizeof(attr))) | |
5193 | return -EFAULT; | |
5194 | ||
5195 | if (attr.group != KVM_VCPU_TSC_CTRL) | |
5196 | return -ENXIO; | |
5197 | ||
5198 | switch (ioctl) { | |
5199 | case KVM_HAS_DEVICE_ATTR: | |
5200 | r = kvm_arch_tsc_has_attr(vcpu, &attr); | |
5201 | break; | |
5202 | case KVM_GET_DEVICE_ATTR: | |
5203 | r = kvm_arch_tsc_get_attr(vcpu, &attr); | |
5204 | break; | |
5205 | case KVM_SET_DEVICE_ATTR: | |
5206 | r = kvm_arch_tsc_set_attr(vcpu, &attr); | |
5207 | break; | |
5208 | } | |
5209 | ||
5210 | return r; | |
5211 | } | |
5212 | ||
5c919412 AS |
5213 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
5214 | struct kvm_enable_cap *cap) | |
5215 | { | |
57b119da VK |
5216 | int r; |
5217 | uint16_t vmcs_version; | |
5218 | void __user *user_ptr; | |
5219 | ||
5c919412 AS |
5220 | if (cap->flags) |
5221 | return -EINVAL; | |
5222 | ||
5223 | switch (cap->cap) { | |
efc479e6 RK |
5224 | case KVM_CAP_HYPERV_SYNIC2: |
5225 | if (cap->args[0]) | |
5226 | return -EINVAL; | |
df561f66 | 5227 | fallthrough; |
b2869f28 | 5228 | |
5c919412 | 5229 | case KVM_CAP_HYPERV_SYNIC: |
546d87e5 WL |
5230 | if (!irqchip_in_kernel(vcpu->kvm)) |
5231 | return -EINVAL; | |
efc479e6 RK |
5232 | return kvm_hv_activate_synic(vcpu, cap->cap == |
5233 | KVM_CAP_HYPERV_SYNIC2); | |
57b119da | 5234 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
33b22172 | 5235 | if (!kvm_x86_ops.nested_ops->enable_evmcs) |
5158917c | 5236 | return -ENOTTY; |
33b22172 | 5237 | r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); |
57b119da VK |
5238 | if (!r) { |
5239 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; | |
5240 | if (copy_to_user(user_ptr, &vmcs_version, | |
5241 | sizeof(vmcs_version))) | |
5242 | r = -EFAULT; | |
5243 | } | |
5244 | return r; | |
344c6c80 | 5245 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 5246 | if (!kvm_x86_ops.enable_direct_tlbflush) |
344c6c80 TL |
5247 | return -ENOTTY; |
5248 | ||
b3646477 | 5249 | return static_call(kvm_x86_enable_direct_tlbflush)(vcpu); |
57b119da | 5250 | |
644f7067 VK |
5251 | case KVM_CAP_HYPERV_ENFORCE_CPUID: |
5252 | return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); | |
5253 | ||
66570e96 OU |
5254 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
5255 | vcpu->arch.pv_cpuid.enforce = cap->args[0]; | |
01b4f510 OU |
5256 | if (vcpu->arch.pv_cpuid.enforce) |
5257 | kvm_update_pv_runtime(vcpu); | |
66570e96 OU |
5258 | |
5259 | return 0; | |
5c919412 AS |
5260 | default: |
5261 | return -EINVAL; | |
5262 | } | |
5263 | } | |
5264 | ||
313a3dc7 CO |
5265 | long kvm_arch_vcpu_ioctl(struct file *filp, |
5266 | unsigned int ioctl, unsigned long arg) | |
5267 | { | |
5268 | struct kvm_vcpu *vcpu = filp->private_data; | |
5269 | void __user *argp = (void __user *)arg; | |
5270 | int r; | |
d1ac91d8 | 5271 | union { |
6dba9403 | 5272 | struct kvm_sregs2 *sregs2; |
d1ac91d8 AK |
5273 | struct kvm_lapic_state *lapic; |
5274 | struct kvm_xsave *xsave; | |
5275 | struct kvm_xcrs *xcrs; | |
5276 | void *buffer; | |
5277 | } u; | |
5278 | ||
9b062471 CD |
5279 | vcpu_load(vcpu); |
5280 | ||
d1ac91d8 | 5281 | u.buffer = NULL; |
313a3dc7 CO |
5282 | switch (ioctl) { |
5283 | case KVM_GET_LAPIC: { | |
2204ae3c | 5284 | r = -EINVAL; |
bce87cce | 5285 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 5286 | goto out; |
254272ce BG |
5287 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
5288 | GFP_KERNEL_ACCOUNT); | |
313a3dc7 | 5289 | |
b772ff36 | 5290 | r = -ENOMEM; |
d1ac91d8 | 5291 | if (!u.lapic) |
b772ff36 | 5292 | goto out; |
d1ac91d8 | 5293 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
313a3dc7 CO |
5294 | if (r) |
5295 | goto out; | |
5296 | r = -EFAULT; | |
d1ac91d8 | 5297 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
313a3dc7 CO |
5298 | goto out; |
5299 | r = 0; | |
5300 | break; | |
5301 | } | |
5302 | case KVM_SET_LAPIC: { | |
2204ae3c | 5303 | r = -EINVAL; |
bce87cce | 5304 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 5305 | goto out; |
ff5c2c03 | 5306 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
9b062471 CD |
5307 | if (IS_ERR(u.lapic)) { |
5308 | r = PTR_ERR(u.lapic); | |
5309 | goto out_nofree; | |
5310 | } | |
ff5c2c03 | 5311 | |
d1ac91d8 | 5312 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
313a3dc7 CO |
5313 | break; |
5314 | } | |
f77bc6a4 ZX |
5315 | case KVM_INTERRUPT: { |
5316 | struct kvm_interrupt irq; | |
5317 | ||
5318 | r = -EFAULT; | |
0e96f31e | 5319 | if (copy_from_user(&irq, argp, sizeof(irq))) |
f77bc6a4 ZX |
5320 | goto out; |
5321 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
f77bc6a4 ZX |
5322 | break; |
5323 | } | |
c4abb7c9 JK |
5324 | case KVM_NMI: { |
5325 | r = kvm_vcpu_ioctl_nmi(vcpu); | |
c4abb7c9 JK |
5326 | break; |
5327 | } | |
f077825a PB |
5328 | case KVM_SMI: { |
5329 | r = kvm_vcpu_ioctl_smi(vcpu); | |
5330 | break; | |
5331 | } | |
313a3dc7 CO |
5332 | case KVM_SET_CPUID: { |
5333 | struct kvm_cpuid __user *cpuid_arg = argp; | |
5334 | struct kvm_cpuid cpuid; | |
5335 | ||
5336 | r = -EFAULT; | |
0e96f31e | 5337 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
313a3dc7 CO |
5338 | goto out; |
5339 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
313a3dc7 CO |
5340 | break; |
5341 | } | |
07716717 DK |
5342 | case KVM_SET_CPUID2: { |
5343 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
5344 | struct kvm_cpuid2 cpuid; | |
5345 | ||
5346 | r = -EFAULT; | |
0e96f31e | 5347 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
5348 | goto out; |
5349 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
19355475 | 5350 | cpuid_arg->entries); |
07716717 DK |
5351 | break; |
5352 | } | |
5353 | case KVM_GET_CPUID2: { | |
5354 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
5355 | struct kvm_cpuid2 cpuid; | |
5356 | ||
5357 | r = -EFAULT; | |
0e96f31e | 5358 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
5359 | goto out; |
5360 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
19355475 | 5361 | cpuid_arg->entries); |
07716717 DK |
5362 | if (r) |
5363 | goto out; | |
5364 | r = -EFAULT; | |
0e96f31e | 5365 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
07716717 DK |
5366 | goto out; |
5367 | r = 0; | |
5368 | break; | |
5369 | } | |
801e459a TL |
5370 | case KVM_GET_MSRS: { |
5371 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
609e36d3 | 5372 | r = msr_io(vcpu, argp, do_get_msr, 1); |
801e459a | 5373 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 5374 | break; |
801e459a TL |
5375 | } |
5376 | case KVM_SET_MSRS: { | |
5377 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
313a3dc7 | 5378 | r = msr_io(vcpu, argp, do_set_msr, 0); |
801e459a | 5379 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 5380 | break; |
801e459a | 5381 | } |
b209749f AK |
5382 | case KVM_TPR_ACCESS_REPORTING: { |
5383 | struct kvm_tpr_access_ctl tac; | |
5384 | ||
5385 | r = -EFAULT; | |
0e96f31e | 5386 | if (copy_from_user(&tac, argp, sizeof(tac))) |
b209749f AK |
5387 | goto out; |
5388 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
5389 | if (r) | |
5390 | goto out; | |
5391 | r = -EFAULT; | |
0e96f31e | 5392 | if (copy_to_user(argp, &tac, sizeof(tac))) |
b209749f AK |
5393 | goto out; |
5394 | r = 0; | |
5395 | break; | |
5396 | }; | |
b93463aa AK |
5397 | case KVM_SET_VAPIC_ADDR: { |
5398 | struct kvm_vapic_addr va; | |
7301d6ab | 5399 | int idx; |
b93463aa AK |
5400 | |
5401 | r = -EINVAL; | |
35754c98 | 5402 | if (!lapic_in_kernel(vcpu)) |
b93463aa AK |
5403 | goto out; |
5404 | r = -EFAULT; | |
0e96f31e | 5405 | if (copy_from_user(&va, argp, sizeof(va))) |
b93463aa | 5406 | goto out; |
7301d6ab | 5407 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
fda4e2e8 | 5408 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
7301d6ab | 5409 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b93463aa AK |
5410 | break; |
5411 | } | |
890ca9ae YH |
5412 | case KVM_X86_SETUP_MCE: { |
5413 | u64 mcg_cap; | |
5414 | ||
5415 | r = -EFAULT; | |
0e96f31e | 5416 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
890ca9ae YH |
5417 | goto out; |
5418 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); | |
5419 | break; | |
5420 | } | |
5421 | case KVM_X86_SET_MCE: { | |
5422 | struct kvm_x86_mce mce; | |
5423 | ||
5424 | r = -EFAULT; | |
0e96f31e | 5425 | if (copy_from_user(&mce, argp, sizeof(mce))) |
890ca9ae YH |
5426 | goto out; |
5427 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); | |
5428 | break; | |
5429 | } | |
3cfc3092 JK |
5430 | case KVM_GET_VCPU_EVENTS: { |
5431 | struct kvm_vcpu_events events; | |
5432 | ||
5433 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); | |
5434 | ||
5435 | r = -EFAULT; | |
5436 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) | |
5437 | break; | |
5438 | r = 0; | |
5439 | break; | |
5440 | } | |
5441 | case KVM_SET_VCPU_EVENTS: { | |
5442 | struct kvm_vcpu_events events; | |
5443 | ||
5444 | r = -EFAULT; | |
5445 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) | |
5446 | break; | |
5447 | ||
5448 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); | |
5449 | break; | |
5450 | } | |
a1efbe77 JK |
5451 | case KVM_GET_DEBUGREGS: { |
5452 | struct kvm_debugregs dbgregs; | |
5453 | ||
5454 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); | |
5455 | ||
5456 | r = -EFAULT; | |
5457 | if (copy_to_user(argp, &dbgregs, | |
5458 | sizeof(struct kvm_debugregs))) | |
5459 | break; | |
5460 | r = 0; | |
5461 | break; | |
5462 | } | |
5463 | case KVM_SET_DEBUGREGS: { | |
5464 | struct kvm_debugregs dbgregs; | |
5465 | ||
5466 | r = -EFAULT; | |
5467 | if (copy_from_user(&dbgregs, argp, | |
5468 | sizeof(struct kvm_debugregs))) | |
5469 | break; | |
5470 | ||
5471 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); | |
5472 | break; | |
5473 | } | |
2d5b5a66 | 5474 | case KVM_GET_XSAVE: { |
be50b206 GZ |
5475 | r = -EINVAL; |
5476 | if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) | |
5477 | break; | |
5478 | ||
254272ce | 5479 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 5480 | r = -ENOMEM; |
d1ac91d8 | 5481 | if (!u.xsave) |
2d5b5a66 SY |
5482 | break; |
5483 | ||
d1ac91d8 | 5484 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
5485 | |
5486 | r = -EFAULT; | |
d1ac91d8 | 5487 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
2d5b5a66 SY |
5488 | break; |
5489 | r = 0; | |
5490 | break; | |
5491 | } | |
5492 | case KVM_SET_XSAVE: { | |
be50b206 GZ |
5493 | int size = vcpu->arch.guest_fpu.uabi_size; |
5494 | ||
5495 | u.xsave = memdup_user(argp, size); | |
9b062471 CD |
5496 | if (IS_ERR(u.xsave)) { |
5497 | r = PTR_ERR(u.xsave); | |
5498 | goto out_nofree; | |
5499 | } | |
2d5b5a66 | 5500 | |
d1ac91d8 | 5501 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
5502 | break; |
5503 | } | |
be50b206 GZ |
5504 | |
5505 | case KVM_GET_XSAVE2: { | |
5506 | int size = vcpu->arch.guest_fpu.uabi_size; | |
5507 | ||
5508 | u.xsave = kzalloc(size, GFP_KERNEL_ACCOUNT); | |
5509 | r = -ENOMEM; | |
5510 | if (!u.xsave) | |
5511 | break; | |
5512 | ||
5513 | kvm_vcpu_ioctl_x86_get_xsave2(vcpu, u.buffer, size); | |
5514 | ||
5515 | r = -EFAULT; | |
5516 | if (copy_to_user(argp, u.xsave, size)) | |
5517 | break; | |
5518 | ||
5519 | r = 0; | |
5520 | break; | |
5521 | } | |
5522 | ||
2d5b5a66 | 5523 | case KVM_GET_XCRS: { |
254272ce | 5524 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 5525 | r = -ENOMEM; |
d1ac91d8 | 5526 | if (!u.xcrs) |
2d5b5a66 SY |
5527 | break; |
5528 | ||
d1ac91d8 | 5529 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
5530 | |
5531 | r = -EFAULT; | |
d1ac91d8 | 5532 | if (copy_to_user(argp, u.xcrs, |
2d5b5a66 SY |
5533 | sizeof(struct kvm_xcrs))) |
5534 | break; | |
5535 | r = 0; | |
5536 | break; | |
5537 | } | |
5538 | case KVM_SET_XCRS: { | |
ff5c2c03 | 5539 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
9b062471 CD |
5540 | if (IS_ERR(u.xcrs)) { |
5541 | r = PTR_ERR(u.xcrs); | |
5542 | goto out_nofree; | |
5543 | } | |
2d5b5a66 | 5544 | |
d1ac91d8 | 5545 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
5546 | break; |
5547 | } | |
92a1f12d JR |
5548 | case KVM_SET_TSC_KHZ: { |
5549 | u32 user_tsc_khz; | |
5550 | ||
5551 | r = -EINVAL; | |
92a1f12d JR |
5552 | user_tsc_khz = (u32)arg; |
5553 | ||
26769f96 MT |
5554 | if (kvm_has_tsc_control && |
5555 | user_tsc_khz >= kvm_max_guest_tsc_khz) | |
92a1f12d JR |
5556 | goto out; |
5557 | ||
cc578287 ZA |
5558 | if (user_tsc_khz == 0) |
5559 | user_tsc_khz = tsc_khz; | |
5560 | ||
381d585c HZ |
5561 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
5562 | r = 0; | |
92a1f12d | 5563 | |
92a1f12d JR |
5564 | goto out; |
5565 | } | |
5566 | case KVM_GET_TSC_KHZ: { | |
cc578287 | 5567 | r = vcpu->arch.virtual_tsc_khz; |
92a1f12d JR |
5568 | goto out; |
5569 | } | |
1c0b28c2 EM |
5570 | case KVM_KVMCLOCK_CTRL: { |
5571 | r = kvm_set_guest_paused(vcpu); | |
5572 | goto out; | |
5573 | } | |
5c919412 AS |
5574 | case KVM_ENABLE_CAP: { |
5575 | struct kvm_enable_cap cap; | |
5576 | ||
5577 | r = -EFAULT; | |
5578 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
5579 | goto out; | |
5580 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
5581 | break; | |
5582 | } | |
8fcc4b59 JM |
5583 | case KVM_GET_NESTED_STATE: { |
5584 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
5585 | u32 user_data_size; | |
5586 | ||
5587 | r = -EINVAL; | |
33b22172 | 5588 | if (!kvm_x86_ops.nested_ops->get_state) |
8fcc4b59 JM |
5589 | break; |
5590 | ||
5591 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); | |
26b471c7 | 5592 | r = -EFAULT; |
8fcc4b59 | 5593 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
26b471c7 | 5594 | break; |
8fcc4b59 | 5595 | |
33b22172 PB |
5596 | r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, |
5597 | user_data_size); | |
8fcc4b59 | 5598 | if (r < 0) |
26b471c7 | 5599 | break; |
8fcc4b59 JM |
5600 | |
5601 | if (r > user_data_size) { | |
5602 | if (put_user(r, &user_kvm_nested_state->size)) | |
26b471c7 LA |
5603 | r = -EFAULT; |
5604 | else | |
5605 | r = -E2BIG; | |
5606 | break; | |
8fcc4b59 | 5607 | } |
26b471c7 | 5608 | |
8fcc4b59 JM |
5609 | r = 0; |
5610 | break; | |
5611 | } | |
5612 | case KVM_SET_NESTED_STATE: { | |
5613 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
5614 | struct kvm_nested_state kvm_state; | |
ad5996d9 | 5615 | int idx; |
8fcc4b59 JM |
5616 | |
5617 | r = -EINVAL; | |
33b22172 | 5618 | if (!kvm_x86_ops.nested_ops->set_state) |
8fcc4b59 JM |
5619 | break; |
5620 | ||
26b471c7 | 5621 | r = -EFAULT; |
8fcc4b59 | 5622 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
26b471c7 | 5623 | break; |
8fcc4b59 | 5624 | |
26b471c7 | 5625 | r = -EINVAL; |
8fcc4b59 | 5626 | if (kvm_state.size < sizeof(kvm_state)) |
26b471c7 | 5627 | break; |
8fcc4b59 JM |
5628 | |
5629 | if (kvm_state.flags & | |
8cab6507 | 5630 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
cc440cda PB |
5631 | | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING |
5632 | | KVM_STATE_NESTED_GIF_SET)) | |
26b471c7 | 5633 | break; |
8fcc4b59 JM |
5634 | |
5635 | /* nested_run_pending implies guest_mode. */ | |
8cab6507 VK |
5636 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
5637 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) | |
26b471c7 | 5638 | break; |
8fcc4b59 | 5639 | |
ad5996d9 | 5640 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
33b22172 | 5641 | r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); |
ad5996d9 | 5642 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8fcc4b59 JM |
5643 | break; |
5644 | } | |
c21d54f0 VK |
5645 | case KVM_GET_SUPPORTED_HV_CPUID: |
5646 | r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp); | |
2bc39970 | 5647 | break; |
b59b153d | 5648 | #ifdef CONFIG_KVM_XEN |
3e324615 DW |
5649 | case KVM_XEN_VCPU_GET_ATTR: { |
5650 | struct kvm_xen_vcpu_attr xva; | |
5651 | ||
5652 | r = -EFAULT; | |
5653 | if (copy_from_user(&xva, argp, sizeof(xva))) | |
5654 | goto out; | |
5655 | r = kvm_xen_vcpu_get_attr(vcpu, &xva); | |
5656 | if (!r && copy_to_user(argp, &xva, sizeof(xva))) | |
5657 | r = -EFAULT; | |
5658 | break; | |
5659 | } | |
5660 | case KVM_XEN_VCPU_SET_ATTR: { | |
5661 | struct kvm_xen_vcpu_attr xva; | |
5662 | ||
5663 | r = -EFAULT; | |
5664 | if (copy_from_user(&xva, argp, sizeof(xva))) | |
5665 | goto out; | |
5666 | r = kvm_xen_vcpu_set_attr(vcpu, &xva); | |
5667 | break; | |
5668 | } | |
b59b153d | 5669 | #endif |
6dba9403 ML |
5670 | case KVM_GET_SREGS2: { |
5671 | u.sregs2 = kzalloc(sizeof(struct kvm_sregs2), GFP_KERNEL); | |
5672 | r = -ENOMEM; | |
5673 | if (!u.sregs2) | |
5674 | goto out; | |
5675 | __get_sregs2(vcpu, u.sregs2); | |
5676 | r = -EFAULT; | |
5677 | if (copy_to_user(argp, u.sregs2, sizeof(struct kvm_sregs2))) | |
5678 | goto out; | |
5679 | r = 0; | |
5680 | break; | |
5681 | } | |
5682 | case KVM_SET_SREGS2: { | |
5683 | u.sregs2 = memdup_user(argp, sizeof(struct kvm_sregs2)); | |
5684 | if (IS_ERR(u.sregs2)) { | |
5685 | r = PTR_ERR(u.sregs2); | |
5686 | u.sregs2 = NULL; | |
5687 | goto out; | |
5688 | } | |
5689 | r = __set_sregs2(vcpu, u.sregs2); | |
5690 | break; | |
5691 | } | |
828ca896 OU |
5692 | case KVM_HAS_DEVICE_ATTR: |
5693 | case KVM_GET_DEVICE_ATTR: | |
5694 | case KVM_SET_DEVICE_ATTR: | |
5695 | r = kvm_vcpu_ioctl_device_attr(vcpu, ioctl, argp); | |
5696 | break; | |
313a3dc7 CO |
5697 | default: |
5698 | r = -EINVAL; | |
5699 | } | |
5700 | out: | |
d1ac91d8 | 5701 | kfree(u.buffer); |
9b062471 CD |
5702 | out_nofree: |
5703 | vcpu_put(vcpu); | |
313a3dc7 CO |
5704 | return r; |
5705 | } | |
5706 | ||
1499fa80 | 5707 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
5b1c1493 CO |
5708 | { |
5709 | return VM_FAULT_SIGBUS; | |
5710 | } | |
5711 | ||
1fe779f8 CO |
5712 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
5713 | { | |
5714 | int ret; | |
5715 | ||
5716 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
951179ce | 5717 | return -EINVAL; |
b3646477 | 5718 | ret = static_call(kvm_x86_set_tss_addr)(kvm, addr); |
1fe779f8 CO |
5719 | return ret; |
5720 | } | |
5721 | ||
b927a3ce SY |
5722 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
5723 | u64 ident_addr) | |
5724 | { | |
b3646477 | 5725 | return static_call(kvm_x86_set_identity_map_addr)(kvm, ident_addr); |
b927a3ce SY |
5726 | } |
5727 | ||
1fe779f8 | 5728 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
bc8a3d89 | 5729 | unsigned long kvm_nr_mmu_pages) |
1fe779f8 CO |
5730 | { |
5731 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
5732 | return -EINVAL; | |
5733 | ||
79fac95e | 5734 | mutex_lock(&kvm->slots_lock); |
1fe779f8 CO |
5735 | |
5736 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 5737 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 5738 | |
79fac95e | 5739 | mutex_unlock(&kvm->slots_lock); |
1fe779f8 CO |
5740 | return 0; |
5741 | } | |
5742 | ||
bc8a3d89 | 5743 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
1fe779f8 | 5744 | { |
39de71ec | 5745 | return kvm->arch.n_max_mmu_pages; |
1fe779f8 CO |
5746 | } |
5747 | ||
1fe779f8 CO |
5748 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
5749 | { | |
90bca052 | 5750 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
5751 | int r; |
5752 | ||
5753 | r = 0; | |
5754 | switch (chip->chip_id) { | |
5755 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 | 5756 | memcpy(&chip->chip.pic, &pic->pics[0], |
1fe779f8 CO |
5757 | sizeof(struct kvm_pic_state)); |
5758 | break; | |
5759 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 | 5760 | memcpy(&chip->chip.pic, &pic->pics[1], |
1fe779f8 CO |
5761 | sizeof(struct kvm_pic_state)); |
5762 | break; | |
5763 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 5764 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
5765 | break; |
5766 | default: | |
5767 | r = -EINVAL; | |
5768 | break; | |
5769 | } | |
5770 | return r; | |
5771 | } | |
5772 | ||
5773 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
5774 | { | |
90bca052 | 5775 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
5776 | int r; |
5777 | ||
5778 | r = 0; | |
5779 | switch (chip->chip_id) { | |
5780 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 DH |
5781 | spin_lock(&pic->lock); |
5782 | memcpy(&pic->pics[0], &chip->chip.pic, | |
1fe779f8 | 5783 | sizeof(struct kvm_pic_state)); |
90bca052 | 5784 | spin_unlock(&pic->lock); |
1fe779f8 CO |
5785 | break; |
5786 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 DH |
5787 | spin_lock(&pic->lock); |
5788 | memcpy(&pic->pics[1], &chip->chip.pic, | |
1fe779f8 | 5789 | sizeof(struct kvm_pic_state)); |
90bca052 | 5790 | spin_unlock(&pic->lock); |
1fe779f8 CO |
5791 | break; |
5792 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 5793 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
5794 | break; |
5795 | default: | |
5796 | r = -EINVAL; | |
5797 | break; | |
5798 | } | |
90bca052 | 5799 | kvm_pic_update_irq(pic); |
1fe779f8 CO |
5800 | return r; |
5801 | } | |
5802 | ||
e0f63cb9 SY |
5803 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
5804 | { | |
34f3941c RK |
5805 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
5806 | ||
5807 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); | |
5808 | ||
5809 | mutex_lock(&kps->lock); | |
5810 | memcpy(ps, &kps->channels, sizeof(*ps)); | |
5811 | mutex_unlock(&kps->lock); | |
2da29bcc | 5812 | return 0; |
e0f63cb9 SY |
5813 | } |
5814 | ||
5815 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
5816 | { | |
0185604c | 5817 | int i; |
09edea72 RK |
5818 | struct kvm_pit *pit = kvm->arch.vpit; |
5819 | ||
5820 | mutex_lock(&pit->pit_state.lock); | |
34f3941c | 5821 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
0185604c | 5822 | for (i = 0; i < 3; i++) |
09edea72 RK |
5823 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
5824 | mutex_unlock(&pit->pit_state.lock); | |
2da29bcc | 5825 | return 0; |
e9f42757 BK |
5826 | } |
5827 | ||
5828 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
5829 | { | |
e9f42757 BK |
5830 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
5831 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, | |
5832 | sizeof(ps->channels)); | |
5833 | ps->flags = kvm->arch.vpit->pit_state.flags; | |
5834 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); | |
97e69aa6 | 5835 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
2da29bcc | 5836 | return 0; |
e9f42757 BK |
5837 | } |
5838 | ||
5839 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
5840 | { | |
2da29bcc | 5841 | int start = 0; |
0185604c | 5842 | int i; |
e9f42757 | 5843 | u32 prev_legacy, cur_legacy; |
09edea72 RK |
5844 | struct kvm_pit *pit = kvm->arch.vpit; |
5845 | ||
5846 | mutex_lock(&pit->pit_state.lock); | |
5847 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; | |
e9f42757 BK |
5848 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
5849 | if (!prev_legacy && cur_legacy) | |
5850 | start = 1; | |
09edea72 RK |
5851 | memcpy(&pit->pit_state.channels, &ps->channels, |
5852 | sizeof(pit->pit_state.channels)); | |
5853 | pit->pit_state.flags = ps->flags; | |
0185604c | 5854 | for (i = 0; i < 3; i++) |
09edea72 | 5855 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
e5e57e7a | 5856 | start && i == 0); |
09edea72 | 5857 | mutex_unlock(&pit->pit_state.lock); |
2da29bcc | 5858 | return 0; |
e0f63cb9 SY |
5859 | } |
5860 | ||
52d939a0 MT |
5861 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
5862 | struct kvm_reinject_control *control) | |
5863 | { | |
71474e2f RK |
5864 | struct kvm_pit *pit = kvm->arch.vpit; |
5865 | ||
71474e2f RK |
5866 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
5867 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL | |
5868 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. | |
5869 | */ | |
5870 | mutex_lock(&pit->pit_state.lock); | |
5871 | kvm_pit_set_reinject(pit, control->pit_reinject); | |
5872 | mutex_unlock(&pit->pit_state.lock); | |
b39c90b6 | 5873 | |
52d939a0 MT |
5874 | return 0; |
5875 | } | |
5876 | ||
0dff0846 | 5877 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
5bb064dc | 5878 | { |
a018eba5 | 5879 | |
88178fd4 | 5880 | /* |
a018eba5 SC |
5881 | * Flush all CPUs' dirty log buffers to the dirty_bitmap. Called |
5882 | * before reporting dirty_bitmap to userspace. KVM flushes the buffers | |
5883 | * on all VM-Exits, thus we only need to kick running vCPUs to force a | |
5884 | * VM-Exit. | |
88178fd4 | 5885 | */ |
a018eba5 | 5886 | struct kvm_vcpu *vcpu; |
46808a4c | 5887 | unsigned long i; |
a018eba5 SC |
5888 | |
5889 | kvm_for_each_vcpu(i, vcpu, kvm) | |
5890 | kvm_vcpu_kick(vcpu); | |
5bb064dc ZX |
5891 | } |
5892 | ||
aa2fbe6d YZ |
5893 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
5894 | bool line_status) | |
23d43cf9 CD |
5895 | { |
5896 | if (!irqchip_in_kernel(kvm)) | |
5897 | return -ENXIO; | |
5898 | ||
5899 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, | |
aa2fbe6d YZ |
5900 | irq_event->irq, irq_event->level, |
5901 | line_status); | |
23d43cf9 CD |
5902 | return 0; |
5903 | } | |
5904 | ||
e5d83c74 PB |
5905 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
5906 | struct kvm_enable_cap *cap) | |
90de4a18 NA |
5907 | { |
5908 | int r; | |
5909 | ||
5910 | if (cap->flags) | |
5911 | return -EINVAL; | |
5912 | ||
5913 | switch (cap->cap) { | |
6d849191 OU |
5914 | case KVM_CAP_DISABLE_QUIRKS2: |
5915 | r = -EINVAL; | |
5916 | if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) | |
5917 | break; | |
5918 | fallthrough; | |
90de4a18 NA |
5919 | case KVM_CAP_DISABLE_QUIRKS: |
5920 | kvm->arch.disabled_quirks = cap->args[0]; | |
5921 | r = 0; | |
5922 | break; | |
49df6397 SR |
5923 | case KVM_CAP_SPLIT_IRQCHIP: { |
5924 | mutex_lock(&kvm->lock); | |
b053b2ae SR |
5925 | r = -EINVAL; |
5926 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) | |
5927 | goto split_irqchip_unlock; | |
49df6397 SR |
5928 | r = -EEXIST; |
5929 | if (irqchip_in_kernel(kvm)) | |
5930 | goto split_irqchip_unlock; | |
557abc40 | 5931 | if (kvm->created_vcpus) |
49df6397 SR |
5932 | goto split_irqchip_unlock; |
5933 | r = kvm_setup_empty_irq_routing(kvm); | |
5c0aea0e | 5934 | if (r) |
49df6397 SR |
5935 | goto split_irqchip_unlock; |
5936 | /* Pairs with irqchip_in_kernel. */ | |
5937 | smp_wmb(); | |
49776faf | 5938 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
b053b2ae | 5939 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
320af55a | 5940 | kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT); |
49df6397 SR |
5941 | r = 0; |
5942 | split_irqchip_unlock: | |
5943 | mutex_unlock(&kvm->lock); | |
5944 | break; | |
5945 | } | |
37131313 RK |
5946 | case KVM_CAP_X2APIC_API: |
5947 | r = -EINVAL; | |
5948 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) | |
5949 | break; | |
5950 | ||
5951 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) | |
5952 | kvm->arch.x2apic_format = true; | |
c519265f RK |
5953 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
5954 | kvm->arch.x2apic_broadcast_quirk_disabled = true; | |
37131313 RK |
5955 | |
5956 | r = 0; | |
5957 | break; | |
4d5422ce WL |
5958 | case KVM_CAP_X86_DISABLE_EXITS: |
5959 | r = -EINVAL; | |
5960 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) | |
5961 | break; | |
5962 | ||
5963 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && | |
5964 | kvm_can_mwait_in_guest()) | |
5965 | kvm->arch.mwait_in_guest = true; | |
766d3571 | 5966 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
caa057a2 | 5967 | kvm->arch.hlt_in_guest = true; |
b31c114b WL |
5968 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
5969 | kvm->arch.pause_in_guest = true; | |
b5170063 WL |
5970 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
5971 | kvm->arch.cstate_in_guest = true; | |
4d5422ce WL |
5972 | r = 0; |
5973 | break; | |
6fbbde9a DS |
5974 | case KVM_CAP_MSR_PLATFORM_INFO: |
5975 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; | |
5976 | r = 0; | |
c4f55198 JM |
5977 | break; |
5978 | case KVM_CAP_EXCEPTION_PAYLOAD: | |
5979 | kvm->arch.exception_payload_enabled = cap->args[0]; | |
5980 | r = 0; | |
6fbbde9a | 5981 | break; |
1ae09954 AG |
5982 | case KVM_CAP_X86_USER_SPACE_MSR: |
5983 | kvm->arch.user_space_msr_mask = cap->args[0]; | |
5984 | r = 0; | |
5985 | break; | |
fe6b6bc8 CQ |
5986 | case KVM_CAP_X86_BUS_LOCK_EXIT: |
5987 | r = -EINVAL; | |
5988 | if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) | |
5989 | break; | |
5990 | ||
5991 | if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && | |
5992 | (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) | |
5993 | break; | |
5994 | ||
5995 | if (kvm_has_bus_lock_exit && | |
5996 | cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) | |
5997 | kvm->arch.bus_lock_detection_enabled = true; | |
5998 | r = 0; | |
5999 | break; | |
fe7e9488 SC |
6000 | #ifdef CONFIG_X86_SGX_KVM |
6001 | case KVM_CAP_SGX_ATTRIBUTE: { | |
6002 | unsigned long allowed_attributes = 0; | |
6003 | ||
6004 | r = sgx_set_attribute(&allowed_attributes, cap->args[0]); | |
6005 | if (r) | |
6006 | break; | |
6007 | ||
6008 | /* KVM only supports the PROVISIONKEY privileged attribute. */ | |
6009 | if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) && | |
6010 | !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY)) | |
6011 | kvm->arch.sgx_provisioning_allowed = true; | |
6012 | else | |
6013 | r = -EINVAL; | |
6014 | break; | |
6015 | } | |
6016 | #endif | |
54526d1f NT |
6017 | case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM: |
6018 | r = -EINVAL; | |
7ad02ef0 SC |
6019 | if (!kvm_x86_ops.vm_copy_enc_context_from) |
6020 | break; | |
6021 | ||
6022 | r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]); | |
6023 | break; | |
b5663931 PG |
6024 | case KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM: |
6025 | r = -EINVAL; | |
7ad02ef0 SC |
6026 | if (!kvm_x86_ops.vm_move_enc_context_from) |
6027 | break; | |
6028 | ||
6029 | r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]); | |
6030 | break; | |
0dbb1123 AK |
6031 | case KVM_CAP_EXIT_HYPERCALL: |
6032 | if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { | |
6033 | r = -EINVAL; | |
6034 | break; | |
6035 | } | |
6036 | kvm->arch.hypercall_exit_enabled = cap->args[0]; | |
6037 | r = 0; | |
6038 | break; | |
19238e75 AL |
6039 | case KVM_CAP_EXIT_ON_EMULATION_FAILURE: |
6040 | r = -EINVAL; | |
6041 | if (cap->args[0] & ~1) | |
6042 | break; | |
6043 | kvm->arch.exit_on_emulation_error = cap->args[0]; | |
6044 | r = 0; | |
6045 | break; | |
ba7bb663 DD |
6046 | case KVM_CAP_PMU_CAPABILITY: |
6047 | r = -EINVAL; | |
6048 | if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) | |
6049 | break; | |
6050 | ||
6051 | mutex_lock(&kvm->lock); | |
6052 | if (!kvm->created_vcpus) { | |
6053 | kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); | |
6054 | r = 0; | |
6055 | } | |
6056 | mutex_unlock(&kvm->lock); | |
6057 | break; | |
90de4a18 NA |
6058 | default: |
6059 | r = -EINVAL; | |
6060 | break; | |
6061 | } | |
6062 | return r; | |
6063 | } | |
6064 | ||
b318e8de SC |
6065 | static struct kvm_x86_msr_filter *kvm_alloc_msr_filter(bool default_allow) |
6066 | { | |
6067 | struct kvm_x86_msr_filter *msr_filter; | |
6068 | ||
6069 | msr_filter = kzalloc(sizeof(*msr_filter), GFP_KERNEL_ACCOUNT); | |
6070 | if (!msr_filter) | |
6071 | return NULL; | |
6072 | ||
6073 | msr_filter->default_allow = default_allow; | |
6074 | return msr_filter; | |
6075 | } | |
6076 | ||
6077 | static void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter) | |
1a155254 AG |
6078 | { |
6079 | u32 i; | |
1a155254 | 6080 | |
b318e8de SC |
6081 | if (!msr_filter) |
6082 | return; | |
6083 | ||
6084 | for (i = 0; i < msr_filter->count; i++) | |
6085 | kfree(msr_filter->ranges[i].bitmap); | |
1a155254 | 6086 | |
b318e8de | 6087 | kfree(msr_filter); |
1a155254 AG |
6088 | } |
6089 | ||
b318e8de SC |
6090 | static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter, |
6091 | struct kvm_msr_filter_range *user_range) | |
1a155254 | 6092 | { |
1a155254 AG |
6093 | unsigned long *bitmap = NULL; |
6094 | size_t bitmap_size; | |
1a155254 AG |
6095 | |
6096 | if (!user_range->nmsrs) | |
6097 | return 0; | |
6098 | ||
aca35288 SC |
6099 | if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) |
6100 | return -EINVAL; | |
6101 | ||
6102 | if (!user_range->flags) | |
6103 | return -EINVAL; | |
6104 | ||
1a155254 AG |
6105 | bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); |
6106 | if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE) | |
6107 | return -EINVAL; | |
6108 | ||
6109 | bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); | |
6110 | if (IS_ERR(bitmap)) | |
6111 | return PTR_ERR(bitmap); | |
6112 | ||
aca35288 | 6113 | msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { |
1a155254 AG |
6114 | .flags = user_range->flags, |
6115 | .base = user_range->base, | |
6116 | .nmsrs = user_range->nmsrs, | |
6117 | .bitmap = bitmap, | |
6118 | }; | |
6119 | ||
b318e8de | 6120 | msr_filter->count++; |
1a155254 | 6121 | return 0; |
1a155254 AG |
6122 | } |
6123 | ||
6124 | static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp) | |
6125 | { | |
6126 | struct kvm_msr_filter __user *user_msr_filter = argp; | |
b318e8de | 6127 | struct kvm_x86_msr_filter *new_filter, *old_filter; |
1a155254 AG |
6128 | struct kvm_msr_filter filter; |
6129 | bool default_allow; | |
043248b3 | 6130 | bool empty = true; |
b318e8de | 6131 | int r = 0; |
1a155254 AG |
6132 | u32 i; |
6133 | ||
6134 | if (copy_from_user(&filter, user_msr_filter, sizeof(filter))) | |
6135 | return -EFAULT; | |
6136 | ||
043248b3 PB |
6137 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) |
6138 | empty &= !filter.ranges[i].nmsrs; | |
1a155254 AG |
6139 | |
6140 | default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY); | |
043248b3 PB |
6141 | if (empty && !default_allow) |
6142 | return -EINVAL; | |
6143 | ||
b318e8de SC |
6144 | new_filter = kvm_alloc_msr_filter(default_allow); |
6145 | if (!new_filter) | |
6146 | return -ENOMEM; | |
1a155254 | 6147 | |
1a155254 | 6148 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) { |
b318e8de SC |
6149 | r = kvm_add_msr_filter(new_filter, &filter.ranges[i]); |
6150 | if (r) { | |
6151 | kvm_free_msr_filter(new_filter); | |
6152 | return r; | |
6153 | } | |
1a155254 AG |
6154 | } |
6155 | ||
b318e8de SC |
6156 | mutex_lock(&kvm->lock); |
6157 | ||
6158 | /* The per-VM filter is protected by kvm->lock... */ | |
6159 | old_filter = srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1); | |
6160 | ||
6161 | rcu_assign_pointer(kvm->arch.msr_filter, new_filter); | |
6162 | synchronize_srcu(&kvm->srcu); | |
6163 | ||
6164 | kvm_free_msr_filter(old_filter); | |
6165 | ||
1a155254 AG |
6166 | kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED); |
6167 | mutex_unlock(&kvm->lock); | |
6168 | ||
b318e8de | 6169 | return 0; |
1a155254 AG |
6170 | } |
6171 | ||
7d62874f SS |
6172 | #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER |
6173 | static int kvm_arch_suspend_notifier(struct kvm *kvm) | |
6174 | { | |
6175 | struct kvm_vcpu *vcpu; | |
46808a4c MZ |
6176 | unsigned long i; |
6177 | int ret = 0; | |
7d62874f SS |
6178 | |
6179 | mutex_lock(&kvm->lock); | |
6180 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
916d3608 | 6181 | if (!vcpu->arch.pv_time.active) |
7d62874f SS |
6182 | continue; |
6183 | ||
6184 | ret = kvm_set_guest_paused(vcpu); | |
6185 | if (ret) { | |
6186 | kvm_err("Failed to pause guest VCPU%d: %d\n", | |
6187 | vcpu->vcpu_id, ret); | |
6188 | break; | |
6189 | } | |
6190 | } | |
6191 | mutex_unlock(&kvm->lock); | |
6192 | ||
6193 | return ret ? NOTIFY_BAD : NOTIFY_DONE; | |
6194 | } | |
6195 | ||
6196 | int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state) | |
6197 | { | |
6198 | switch (state) { | |
6199 | case PM_HIBERNATION_PREPARE: | |
6200 | case PM_SUSPEND_PREPARE: | |
6201 | return kvm_arch_suspend_notifier(kvm); | |
6202 | } | |
6203 | ||
6204 | return NOTIFY_DONE; | |
6205 | } | |
6206 | #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */ | |
6207 | ||
45e6c2fa PB |
6208 | static int kvm_vm_ioctl_get_clock(struct kvm *kvm, void __user *argp) |
6209 | { | |
869b4421 | 6210 | struct kvm_clock_data data = { 0 }; |
45e6c2fa | 6211 | |
55c0cefb | 6212 | get_kvmclock(kvm, &data); |
45e6c2fa PB |
6213 | if (copy_to_user(argp, &data, sizeof(data))) |
6214 | return -EFAULT; | |
6215 | ||
6216 | return 0; | |
6217 | } | |
6218 | ||
6219 | static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp) | |
6220 | { | |
6221 | struct kvm_arch *ka = &kvm->arch; | |
6222 | struct kvm_clock_data data; | |
c68dc1b5 | 6223 | u64 now_raw_ns; |
45e6c2fa PB |
6224 | |
6225 | if (copy_from_user(&data, argp, sizeof(data))) | |
6226 | return -EFAULT; | |
6227 | ||
c68dc1b5 OU |
6228 | /* |
6229 | * Only KVM_CLOCK_REALTIME is used, but allow passing the | |
6230 | * result of KVM_GET_CLOCK back to KVM_SET_CLOCK. | |
6231 | */ | |
6232 | if (data.flags & ~KVM_CLOCK_VALID_FLAGS) | |
45e6c2fa PB |
6233 | return -EINVAL; |
6234 | ||
6235 | kvm_hv_invalidate_tsc_page(kvm); | |
6236 | kvm_start_pvclock_update(kvm); | |
6237 | pvclock_update_vm_gtod_copy(kvm); | |
6238 | ||
6239 | /* | |
6240 | * This pairs with kvm_guest_time_update(): when masterclock is | |
6241 | * in use, we use master_kernel_ns + kvmclock_offset to set | |
6242 | * unsigned 'system_time' so if we use get_kvmclock_ns() (which | |
6243 | * is slightly ahead) here we risk going negative on unsigned | |
6244 | * 'system_time' when 'data.clock' is very small. | |
6245 | */ | |
c68dc1b5 OU |
6246 | if (data.flags & KVM_CLOCK_REALTIME) { |
6247 | u64 now_real_ns = ktime_get_real_ns(); | |
6248 | ||
6249 | /* | |
6250 | * Avoid stepping the kvmclock backwards. | |
6251 | */ | |
6252 | if (now_real_ns > data.realtime) | |
6253 | data.clock += now_real_ns - data.realtime; | |
6254 | } | |
6255 | ||
6256 | if (ka->use_master_clock) | |
6257 | now_raw_ns = ka->master_kernel_ns; | |
45e6c2fa | 6258 | else |
c68dc1b5 OU |
6259 | now_raw_ns = get_kvmclock_base_ns(); |
6260 | ka->kvmclock_offset = data.clock - now_raw_ns; | |
45e6c2fa PB |
6261 | kvm_end_pvclock_update(kvm); |
6262 | return 0; | |
6263 | } | |
6264 | ||
1fe779f8 CO |
6265 | long kvm_arch_vm_ioctl(struct file *filp, |
6266 | unsigned int ioctl, unsigned long arg) | |
6267 | { | |
6268 | struct kvm *kvm = filp->private_data; | |
6269 | void __user *argp = (void __user *)arg; | |
367e1319 | 6270 | int r = -ENOTTY; |
f0d66275 DH |
6271 | /* |
6272 | * This union makes it completely explicit to gcc-3.x | |
6273 | * that these two variables' stack usage should be | |
6274 | * combined, not added together. | |
6275 | */ | |
6276 | union { | |
6277 | struct kvm_pit_state ps; | |
e9f42757 | 6278 | struct kvm_pit_state2 ps2; |
c5ff41ce | 6279 | struct kvm_pit_config pit_config; |
f0d66275 | 6280 | } u; |
1fe779f8 CO |
6281 | |
6282 | switch (ioctl) { | |
6283 | case KVM_SET_TSS_ADDR: | |
6284 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1fe779f8 | 6285 | break; |
b927a3ce SY |
6286 | case KVM_SET_IDENTITY_MAP_ADDR: { |
6287 | u64 ident_addr; | |
6288 | ||
1af1ac91 DH |
6289 | mutex_lock(&kvm->lock); |
6290 | r = -EINVAL; | |
6291 | if (kvm->created_vcpus) | |
6292 | goto set_identity_unlock; | |
b927a3ce | 6293 | r = -EFAULT; |
0e96f31e | 6294 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
1af1ac91 | 6295 | goto set_identity_unlock; |
b927a3ce | 6296 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
1af1ac91 DH |
6297 | set_identity_unlock: |
6298 | mutex_unlock(&kvm->lock); | |
b927a3ce SY |
6299 | break; |
6300 | } | |
1fe779f8 CO |
6301 | case KVM_SET_NR_MMU_PAGES: |
6302 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1fe779f8 CO |
6303 | break; |
6304 | case KVM_GET_NR_MMU_PAGES: | |
6305 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
6306 | break; | |
3ddea128 | 6307 | case KVM_CREATE_IRQCHIP: { |
3ddea128 | 6308 | mutex_lock(&kvm->lock); |
09941366 | 6309 | |
3ddea128 | 6310 | r = -EEXIST; |
35e6eaa3 | 6311 | if (irqchip_in_kernel(kvm)) |
3ddea128 | 6312 | goto create_irqchip_unlock; |
09941366 | 6313 | |
3e515705 | 6314 | r = -EINVAL; |
557abc40 | 6315 | if (kvm->created_vcpus) |
3e515705 | 6316 | goto create_irqchip_unlock; |
09941366 RK |
6317 | |
6318 | r = kvm_pic_init(kvm); | |
6319 | if (r) | |
3ddea128 | 6320 | goto create_irqchip_unlock; |
09941366 RK |
6321 | |
6322 | r = kvm_ioapic_init(kvm); | |
6323 | if (r) { | |
09941366 | 6324 | kvm_pic_destroy(kvm); |
3ddea128 | 6325 | goto create_irqchip_unlock; |
09941366 RK |
6326 | } |
6327 | ||
399ec807 AK |
6328 | r = kvm_setup_default_irq_routing(kvm); |
6329 | if (r) { | |
72bb2fcd | 6330 | kvm_ioapic_destroy(kvm); |
09941366 | 6331 | kvm_pic_destroy(kvm); |
71ba994c | 6332 | goto create_irqchip_unlock; |
399ec807 | 6333 | } |
49776faf | 6334 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
71ba994c | 6335 | smp_wmb(); |
49776faf | 6336 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
320af55a | 6337 | kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT); |
3ddea128 MT |
6338 | create_irqchip_unlock: |
6339 | mutex_unlock(&kvm->lock); | |
1fe779f8 | 6340 | break; |
3ddea128 | 6341 | } |
7837699f | 6342 | case KVM_CREATE_PIT: |
c5ff41ce JK |
6343 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
6344 | goto create_pit; | |
6345 | case KVM_CREATE_PIT2: | |
6346 | r = -EFAULT; | |
6347 | if (copy_from_user(&u.pit_config, argp, | |
6348 | sizeof(struct kvm_pit_config))) | |
6349 | goto out; | |
6350 | create_pit: | |
250715a6 | 6351 | mutex_lock(&kvm->lock); |
269e05e4 AK |
6352 | r = -EEXIST; |
6353 | if (kvm->arch.vpit) | |
6354 | goto create_pit_unlock; | |
7837699f | 6355 | r = -ENOMEM; |
c5ff41ce | 6356 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
7837699f SY |
6357 | if (kvm->arch.vpit) |
6358 | r = 0; | |
269e05e4 | 6359 | create_pit_unlock: |
250715a6 | 6360 | mutex_unlock(&kvm->lock); |
7837699f | 6361 | break; |
1fe779f8 CO |
6362 | case KVM_GET_IRQCHIP: { |
6363 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 6364 | struct kvm_irqchip *chip; |
1fe779f8 | 6365 | |
ff5c2c03 SL |
6366 | chip = memdup_user(argp, sizeof(*chip)); |
6367 | if (IS_ERR(chip)) { | |
6368 | r = PTR_ERR(chip); | |
1fe779f8 | 6369 | goto out; |
ff5c2c03 SL |
6370 | } |
6371 | ||
1fe779f8 | 6372 | r = -ENXIO; |
826da321 | 6373 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
6374 | goto get_irqchip_out; |
6375 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); | |
1fe779f8 | 6376 | if (r) |
f0d66275 | 6377 | goto get_irqchip_out; |
1fe779f8 | 6378 | r = -EFAULT; |
0e96f31e | 6379 | if (copy_to_user(argp, chip, sizeof(*chip))) |
f0d66275 | 6380 | goto get_irqchip_out; |
1fe779f8 | 6381 | r = 0; |
f0d66275 DH |
6382 | get_irqchip_out: |
6383 | kfree(chip); | |
1fe779f8 CO |
6384 | break; |
6385 | } | |
6386 | case KVM_SET_IRQCHIP: { | |
6387 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 6388 | struct kvm_irqchip *chip; |
1fe779f8 | 6389 | |
ff5c2c03 SL |
6390 | chip = memdup_user(argp, sizeof(*chip)); |
6391 | if (IS_ERR(chip)) { | |
6392 | r = PTR_ERR(chip); | |
1fe779f8 | 6393 | goto out; |
ff5c2c03 SL |
6394 | } |
6395 | ||
1fe779f8 | 6396 | r = -ENXIO; |
826da321 | 6397 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
6398 | goto set_irqchip_out; |
6399 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); | |
f0d66275 DH |
6400 | set_irqchip_out: |
6401 | kfree(chip); | |
1fe779f8 CO |
6402 | break; |
6403 | } | |
e0f63cb9 | 6404 | case KVM_GET_PIT: { |
e0f63cb9 | 6405 | r = -EFAULT; |
f0d66275 | 6406 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
6407 | goto out; |
6408 | r = -ENXIO; | |
6409 | if (!kvm->arch.vpit) | |
6410 | goto out; | |
f0d66275 | 6411 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
e0f63cb9 SY |
6412 | if (r) |
6413 | goto out; | |
6414 | r = -EFAULT; | |
f0d66275 | 6415 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
6416 | goto out; |
6417 | r = 0; | |
6418 | break; | |
6419 | } | |
6420 | case KVM_SET_PIT: { | |
e0f63cb9 | 6421 | r = -EFAULT; |
0e96f31e | 6422 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
e0f63cb9 | 6423 | goto out; |
7289fdb5 | 6424 | mutex_lock(&kvm->lock); |
e0f63cb9 SY |
6425 | r = -ENXIO; |
6426 | if (!kvm->arch.vpit) | |
7289fdb5 | 6427 | goto set_pit_out; |
f0d66275 | 6428 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
7289fdb5 SR |
6429 | set_pit_out: |
6430 | mutex_unlock(&kvm->lock); | |
e0f63cb9 SY |
6431 | break; |
6432 | } | |
e9f42757 BK |
6433 | case KVM_GET_PIT2: { |
6434 | r = -ENXIO; | |
6435 | if (!kvm->arch.vpit) | |
6436 | goto out; | |
6437 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); | |
6438 | if (r) | |
6439 | goto out; | |
6440 | r = -EFAULT; | |
6441 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) | |
6442 | goto out; | |
6443 | r = 0; | |
6444 | break; | |
6445 | } | |
6446 | case KVM_SET_PIT2: { | |
6447 | r = -EFAULT; | |
6448 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) | |
6449 | goto out; | |
7289fdb5 | 6450 | mutex_lock(&kvm->lock); |
e9f42757 BK |
6451 | r = -ENXIO; |
6452 | if (!kvm->arch.vpit) | |
7289fdb5 | 6453 | goto set_pit2_out; |
e9f42757 | 6454 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
7289fdb5 SR |
6455 | set_pit2_out: |
6456 | mutex_unlock(&kvm->lock); | |
e9f42757 BK |
6457 | break; |
6458 | } | |
52d939a0 MT |
6459 | case KVM_REINJECT_CONTROL: { |
6460 | struct kvm_reinject_control control; | |
6461 | r = -EFAULT; | |
6462 | if (copy_from_user(&control, argp, sizeof(control))) | |
6463 | goto out; | |
cad23e72 ML |
6464 | r = -ENXIO; |
6465 | if (!kvm->arch.vpit) | |
6466 | goto out; | |
52d939a0 | 6467 | r = kvm_vm_ioctl_reinject(kvm, &control); |
52d939a0 MT |
6468 | break; |
6469 | } | |
d71ba788 PB |
6470 | case KVM_SET_BOOT_CPU_ID: |
6471 | r = 0; | |
6472 | mutex_lock(&kvm->lock); | |
557abc40 | 6473 | if (kvm->created_vcpus) |
d71ba788 PB |
6474 | r = -EBUSY; |
6475 | else | |
6476 | kvm->arch.bsp_vcpu_id = arg; | |
6477 | mutex_unlock(&kvm->lock); | |
6478 | break; | |
b59b153d | 6479 | #ifdef CONFIG_KVM_XEN |
ffde22ac | 6480 | case KVM_XEN_HVM_CONFIG: { |
51776043 | 6481 | struct kvm_xen_hvm_config xhc; |
ffde22ac | 6482 | r = -EFAULT; |
51776043 | 6483 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
ffde22ac | 6484 | goto out; |
78e9878c | 6485 | r = kvm_xen_hvm_config(kvm, &xhc); |
ffde22ac ES |
6486 | break; |
6487 | } | |
a76b9641 JM |
6488 | case KVM_XEN_HVM_GET_ATTR: { |
6489 | struct kvm_xen_hvm_attr xha; | |
6490 | ||
6491 | r = -EFAULT; | |
6492 | if (copy_from_user(&xha, argp, sizeof(xha))) | |
ffde22ac | 6493 | goto out; |
a76b9641 JM |
6494 | r = kvm_xen_hvm_get_attr(kvm, &xha); |
6495 | if (!r && copy_to_user(argp, &xha, sizeof(xha))) | |
6496 | r = -EFAULT; | |
6497 | break; | |
6498 | } | |
6499 | case KVM_XEN_HVM_SET_ATTR: { | |
6500 | struct kvm_xen_hvm_attr xha; | |
6501 | ||
6502 | r = -EFAULT; | |
6503 | if (copy_from_user(&xha, argp, sizeof(xha))) | |
6504 | goto out; | |
6505 | r = kvm_xen_hvm_set_attr(kvm, &xha); | |
ffde22ac ES |
6506 | break; |
6507 | } | |
35025735 DW |
6508 | case KVM_XEN_HVM_EVTCHN_SEND: { |
6509 | struct kvm_irq_routing_xen_evtchn uxe; | |
6510 | ||
6511 | r = -EFAULT; | |
6512 | if (copy_from_user(&uxe, argp, sizeof(uxe))) | |
6513 | goto out; | |
6514 | r = kvm_xen_hvm_evtchn_send(kvm, &uxe); | |
6515 | break; | |
6516 | } | |
b59b153d | 6517 | #endif |
45e6c2fa PB |
6518 | case KVM_SET_CLOCK: |
6519 | r = kvm_vm_ioctl_set_clock(kvm, argp); | |
afbcf7ab | 6520 | break; |
45e6c2fa PB |
6521 | case KVM_GET_CLOCK: |
6522 | r = kvm_vm_ioctl_get_clock(kvm, argp); | |
afbcf7ab | 6523 | break; |
5acc5c06 BS |
6524 | case KVM_MEMORY_ENCRYPT_OP: { |
6525 | r = -ENOTTY; | |
03d004cd SC |
6526 | if (!kvm_x86_ops.mem_enc_ioctl) |
6527 | goto out; | |
6528 | ||
6529 | r = static_call(kvm_x86_mem_enc_ioctl)(kvm, argp); | |
5acc5c06 BS |
6530 | break; |
6531 | } | |
69eaedee BS |
6532 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
6533 | struct kvm_enc_region region; | |
6534 | ||
6535 | r = -EFAULT; | |
6536 | if (copy_from_user(®ion, argp, sizeof(region))) | |
6537 | goto out; | |
6538 | ||
6539 | r = -ENOTTY; | |
03d004cd SC |
6540 | if (!kvm_x86_ops.mem_enc_register_region) |
6541 | goto out; | |
6542 | ||
6543 | r = static_call(kvm_x86_mem_enc_register_region)(kvm, ®ion); | |
69eaedee BS |
6544 | break; |
6545 | } | |
6546 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { | |
6547 | struct kvm_enc_region region; | |
6548 | ||
6549 | r = -EFAULT; | |
6550 | if (copy_from_user(®ion, argp, sizeof(region))) | |
6551 | goto out; | |
6552 | ||
6553 | r = -ENOTTY; | |
03d004cd SC |
6554 | if (!kvm_x86_ops.mem_enc_unregister_region) |
6555 | goto out; | |
6556 | ||
6557 | r = static_call(kvm_x86_mem_enc_unregister_region)(kvm, ®ion); | |
69eaedee BS |
6558 | break; |
6559 | } | |
faeb7833 RK |
6560 | case KVM_HYPERV_EVENTFD: { |
6561 | struct kvm_hyperv_eventfd hvevfd; | |
6562 | ||
6563 | r = -EFAULT; | |
6564 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) | |
6565 | goto out; | |
6566 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); | |
6567 | break; | |
6568 | } | |
66bb8a06 EH |
6569 | case KVM_SET_PMU_EVENT_FILTER: |
6570 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); | |
6571 | break; | |
1a155254 AG |
6572 | case KVM_X86_SET_MSR_FILTER: |
6573 | r = kvm_vm_ioctl_set_msr_filter(kvm, argp); | |
6574 | break; | |
1fe779f8 | 6575 | default: |
ad6260da | 6576 | r = -ENOTTY; |
1fe779f8 CO |
6577 | } |
6578 | out: | |
6579 | return r; | |
6580 | } | |
6581 | ||
a16b043c | 6582 | static void kvm_init_msr_list(void) |
043405e1 | 6583 | { |
24c29b7a | 6584 | struct x86_pmu_capability x86_pmu; |
043405e1 | 6585 | u32 dummy[2]; |
7a5ee6ed | 6586 | unsigned i; |
043405e1 | 6587 | |
e2ada66e | 6588 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
7a5ee6ed | 6589 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
24c29b7a PB |
6590 | |
6591 | perf_get_x86_pmu_capability(&x86_pmu); | |
e2ada66e | 6592 | |
6cbee2b9 XL |
6593 | num_msrs_to_save = 0; |
6594 | num_emulated_msrs = 0; | |
6595 | num_msr_based_features = 0; | |
6596 | ||
7a5ee6ed CQ |
6597 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
6598 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) | |
043405e1 | 6599 | continue; |
93c4adc7 PB |
6600 | |
6601 | /* | |
6602 | * Even MSRs that are valid in the host may not be exposed | |
9dbe6cf9 | 6603 | * to the guests in some cases. |
93c4adc7 | 6604 | */ |
7a5ee6ed | 6605 | switch (msrs_to_save_all[i]) { |
93c4adc7 | 6606 | case MSR_IA32_BNDCFGS: |
503234b3 | 6607 | if (!kvm_mpx_supported()) |
93c4adc7 PB |
6608 | continue; |
6609 | break; | |
9dbe6cf9 | 6610 | case MSR_TSC_AUX: |
36fa06f9 SC |
6611 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) && |
6612 | !kvm_cpu_cap_has(X86_FEATURE_RDPID)) | |
9dbe6cf9 PB |
6613 | continue; |
6614 | break; | |
f4cfcd2d ML |
6615 | case MSR_IA32_UMWAIT_CONTROL: |
6616 | if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) | |
6617 | continue; | |
6618 | break; | |
bf8c55d8 CP |
6619 | case MSR_IA32_RTIT_CTL: |
6620 | case MSR_IA32_RTIT_STATUS: | |
7b874c26 | 6621 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
bf8c55d8 CP |
6622 | continue; |
6623 | break; | |
6624 | case MSR_IA32_RTIT_CR3_MATCH: | |
7b874c26 | 6625 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
6626 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
6627 | continue; | |
6628 | break; | |
6629 | case MSR_IA32_RTIT_OUTPUT_BASE: | |
6630 | case MSR_IA32_RTIT_OUTPUT_MASK: | |
7b874c26 | 6631 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
6632 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
6633 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) | |
6634 | continue; | |
6635 | break; | |
7cb85fc4 | 6636 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
7b874c26 | 6637 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
7a5ee6ed | 6638 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
bf8c55d8 CP |
6639 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
6640 | continue; | |
6641 | break; | |
cf05a67b | 6642 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
7a5ee6ed | 6643 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
24c29b7a PB |
6644 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
6645 | continue; | |
6646 | break; | |
cf05a67b | 6647 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
7a5ee6ed | 6648 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
24c29b7a PB |
6649 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
6650 | continue; | |
7cb85fc4 | 6651 | break; |
820a6ee9 | 6652 | case MSR_IA32_XFD: |
548e8365 | 6653 | case MSR_IA32_XFD_ERR: |
820a6ee9 JL |
6654 | if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) |
6655 | continue; | |
6656 | break; | |
93c4adc7 PB |
6657 | default: |
6658 | break; | |
6659 | } | |
6660 | ||
7a5ee6ed | 6661 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
043405e1 | 6662 | } |
62ef68bb | 6663 | |
7a5ee6ed | 6664 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
b3646477 | 6665 | if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i])) |
bc226f07 | 6666 | continue; |
62ef68bb | 6667 | |
7a5ee6ed | 6668 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
62ef68bb | 6669 | } |
801e459a | 6670 | |
7a5ee6ed | 6671 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
801e459a TL |
6672 | struct kvm_msr_entry msr; |
6673 | ||
7a5ee6ed | 6674 | msr.index = msr_based_features_all[i]; |
66421c1e | 6675 | if (kvm_get_msr_feature(&msr)) |
801e459a TL |
6676 | continue; |
6677 | ||
7a5ee6ed | 6678 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
801e459a | 6679 | } |
043405e1 CO |
6680 | } |
6681 | ||
bda9020e MT |
6682 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
6683 | const void *v) | |
bbd9b64e | 6684 | { |
70252a10 AK |
6685 | int handled = 0; |
6686 | int n; | |
6687 | ||
6688 | do { | |
6689 | n = min(len, 8); | |
bce87cce | 6690 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
6691 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
6692 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 AK |
6693 | break; |
6694 | handled += n; | |
6695 | addr += n; | |
6696 | len -= n; | |
6697 | v += n; | |
6698 | } while (len); | |
bbd9b64e | 6699 | |
70252a10 | 6700 | return handled; |
bbd9b64e CO |
6701 | } |
6702 | ||
bda9020e | 6703 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
bbd9b64e | 6704 | { |
70252a10 AK |
6705 | int handled = 0; |
6706 | int n; | |
6707 | ||
6708 | do { | |
6709 | n = min(len, 8); | |
bce87cce | 6710 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
6711 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
6712 | addr, n, v)) | |
6713 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 | 6714 | break; |
e39d200f | 6715 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
70252a10 AK |
6716 | handled += n; |
6717 | addr += n; | |
6718 | len -= n; | |
6719 | v += n; | |
6720 | } while (len); | |
bbd9b64e | 6721 | |
70252a10 | 6722 | return handled; |
bbd9b64e CO |
6723 | } |
6724 | ||
2dafc6c2 GN |
6725 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
6726 | struct kvm_segment *var, int seg) | |
6727 | { | |
b3646477 | 6728 | static_call(kvm_x86_set_segment)(vcpu, var, seg); |
2dafc6c2 GN |
6729 | } |
6730 | ||
6731 | void kvm_get_segment(struct kvm_vcpu *vcpu, | |
6732 | struct kvm_segment *var, int seg) | |
6733 | { | |
b3646477 | 6734 | static_call(kvm_x86_get_segment)(vcpu, var, seg); |
2dafc6c2 GN |
6735 | } |
6736 | ||
5b22bbe7 | 6737 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 access, |
54987b7a | 6738 | struct x86_exception *exception) |
02f59dc9 | 6739 | { |
1f5a21ee | 6740 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
02f59dc9 | 6741 | gpa_t t_gpa; |
02f59dc9 JR |
6742 | |
6743 | BUG_ON(!mmu_is_nested(vcpu)); | |
6744 | ||
6745 | /* NPT walks are always user-walks */ | |
6746 | access |= PFERR_USER_MASK; | |
1f5a21ee | 6747 | t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); |
02f59dc9 JR |
6748 | |
6749 | return t_gpa; | |
6750 | } | |
6751 | ||
ab9ae313 AK |
6752 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
6753 | struct x86_exception *exception) | |
1871c602 | 6754 | { |
1f5a21ee LJ |
6755 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
6756 | ||
5b22bbe7 | 6757 | u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1f5a21ee | 6758 | return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); |
1871c602 | 6759 | } |
54f958cd | 6760 | EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_read); |
1871c602 | 6761 | |
ab9ae313 AK |
6762 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
6763 | struct x86_exception *exception) | |
1871c602 | 6764 | { |
1f5a21ee LJ |
6765 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
6766 | ||
5b22bbe7 | 6767 | u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 6768 | access |= PFERR_FETCH_MASK; |
1f5a21ee | 6769 | return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); |
1871c602 GN |
6770 | } |
6771 | ||
ab9ae313 AK |
6772 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
6773 | struct x86_exception *exception) | |
1871c602 | 6774 | { |
1f5a21ee LJ |
6775 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
6776 | ||
5b22bbe7 | 6777 | u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 6778 | access |= PFERR_WRITE_MASK; |
1f5a21ee | 6779 | return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); |
1871c602 | 6780 | } |
54f958cd | 6781 | EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_write); |
1871c602 GN |
6782 | |
6783 | /* uses this to access any guest's mapped memory without checking CPL */ | |
ab9ae313 AK |
6784 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
6785 | struct x86_exception *exception) | |
1871c602 | 6786 | { |
1f5a21ee LJ |
6787 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
6788 | ||
6789 | return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); | |
1871c602 GN |
6790 | } |
6791 | ||
6792 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, | |
5b22bbe7 | 6793 | struct kvm_vcpu *vcpu, u64 access, |
bcc55cba | 6794 | struct x86_exception *exception) |
bbd9b64e | 6795 | { |
1f5a21ee | 6796 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
bbd9b64e | 6797 | void *data = val; |
10589a46 | 6798 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
6799 | |
6800 | while (bytes) { | |
1f5a21ee | 6801 | gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); |
bbd9b64e | 6802 | unsigned offset = addr & (PAGE_SIZE-1); |
77c2002e | 6803 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
bbd9b64e CO |
6804 | int ret; |
6805 | ||
bcc55cba | 6806 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 6807 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa PB |
6808 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
6809 | offset, toread); | |
10589a46 | 6810 | if (ret < 0) { |
c3cd7ffa | 6811 | r = X86EMUL_IO_NEEDED; |
10589a46 MT |
6812 | goto out; |
6813 | } | |
bbd9b64e | 6814 | |
77c2002e IE |
6815 | bytes -= toread; |
6816 | data += toread; | |
6817 | addr += toread; | |
bbd9b64e | 6818 | } |
10589a46 | 6819 | out: |
10589a46 | 6820 | return r; |
bbd9b64e | 6821 | } |
77c2002e | 6822 | |
1871c602 | 6823 | /* used for instruction fetching */ |
0f65dd70 AK |
6824 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
6825 | gva_t addr, void *val, unsigned int bytes, | |
bcc55cba | 6826 | struct x86_exception *exception) |
1871c602 | 6827 | { |
0f65dd70 | 6828 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
1f5a21ee | 6829 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
5b22bbe7 | 6830 | u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
44583cba PB |
6831 | unsigned offset; |
6832 | int ret; | |
0f65dd70 | 6833 | |
44583cba | 6834 | /* Inline kvm_read_guest_virt_helper for speed. */ |
1f5a21ee LJ |
6835 | gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, |
6836 | exception); | |
44583cba PB |
6837 | if (unlikely(gpa == UNMAPPED_GVA)) |
6838 | return X86EMUL_PROPAGATE_FAULT; | |
6839 | ||
6840 | offset = addr & (PAGE_SIZE-1); | |
6841 | if (WARN_ON(offset + bytes > PAGE_SIZE)) | |
6842 | bytes = (unsigned)PAGE_SIZE - offset; | |
54bf36aa PB |
6843 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
6844 | offset, bytes); | |
44583cba PB |
6845 | if (unlikely(ret < 0)) |
6846 | return X86EMUL_IO_NEEDED; | |
6847 | ||
6848 | return X86EMUL_CONTINUE; | |
1871c602 GN |
6849 | } |
6850 | ||
ce14e868 | 6851 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
0f65dd70 | 6852 | gva_t addr, void *val, unsigned int bytes, |
bcc55cba | 6853 | struct x86_exception *exception) |
1871c602 | 6854 | { |
5b22bbe7 | 6855 | u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; |
0f65dd70 | 6856 | |
353c0956 PB |
6857 | /* |
6858 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED | |
6859 | * is returned, but our callers are not ready for that and they blindly | |
6860 | * call kvm_inject_page_fault. Ensure that they at least do not leak | |
6861 | * uninitialized kernel stack memory into cr2 and error code. | |
6862 | */ | |
6863 | memset(exception, 0, sizeof(*exception)); | |
1871c602 | 6864 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
bcc55cba | 6865 | exception); |
1871c602 | 6866 | } |
064aea77 | 6867 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
1871c602 | 6868 | |
ce14e868 PB |
6869 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
6870 | gva_t addr, void *val, unsigned int bytes, | |
3c9fa24c | 6871 | struct x86_exception *exception, bool system) |
1871c602 | 6872 | { |
0f65dd70 | 6873 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
5b22bbe7 | 6874 | u64 access = 0; |
3c9fa24c | 6875 | |
4f4aa80e LJ |
6876 | if (system) |
6877 | access |= PFERR_IMPLICIT_ACCESS; | |
6878 | else if (static_call(kvm_x86_get_cpl)(vcpu) == 3) | |
3c9fa24c PB |
6879 | access |= PFERR_USER_MASK; |
6880 | ||
6881 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); | |
1871c602 GN |
6882 | } |
6883 | ||
7a036a6f RK |
6884 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
6885 | unsigned long addr, void *val, unsigned int bytes) | |
6886 | { | |
6887 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6888 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); | |
6889 | ||
6890 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; | |
6891 | } | |
6892 | ||
ce14e868 | 6893 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
5b22bbe7 | 6894 | struct kvm_vcpu *vcpu, u64 access, |
ce14e868 | 6895 | struct x86_exception *exception) |
77c2002e | 6896 | { |
1f5a21ee | 6897 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
77c2002e IE |
6898 | void *data = val; |
6899 | int r = X86EMUL_CONTINUE; | |
6900 | ||
6901 | while (bytes) { | |
1f5a21ee | 6902 | gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); |
77c2002e IE |
6903 | unsigned offset = addr & (PAGE_SIZE-1); |
6904 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); | |
6905 | int ret; | |
6906 | ||
bcc55cba | 6907 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 6908 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa | 6909 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
77c2002e | 6910 | if (ret < 0) { |
c3cd7ffa | 6911 | r = X86EMUL_IO_NEEDED; |
77c2002e IE |
6912 | goto out; |
6913 | } | |
6914 | ||
6915 | bytes -= towrite; | |
6916 | data += towrite; | |
6917 | addr += towrite; | |
6918 | } | |
6919 | out: | |
6920 | return r; | |
6921 | } | |
ce14e868 PB |
6922 | |
6923 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, | |
3c9fa24c PB |
6924 | unsigned int bytes, struct x86_exception *exception, |
6925 | bool system) | |
ce14e868 PB |
6926 | { |
6927 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
5b22bbe7 | 6928 | u64 access = PFERR_WRITE_MASK; |
3c9fa24c | 6929 | |
4f4aa80e LJ |
6930 | if (system) |
6931 | access |= PFERR_IMPLICIT_ACCESS; | |
6932 | else if (static_call(kvm_x86_get_cpl)(vcpu) == 3) | |
3c9fa24c | 6933 | access |= PFERR_USER_MASK; |
ce14e868 PB |
6934 | |
6935 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, | |
3c9fa24c | 6936 | access, exception); |
ce14e868 PB |
6937 | } |
6938 | ||
6939 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, | |
6940 | unsigned int bytes, struct x86_exception *exception) | |
6941 | { | |
c595ceee PB |
6942 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
6943 | vcpu->arch.l1tf_flush_l1d = true; | |
6944 | ||
ce14e868 PB |
6945 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
6946 | PFERR_WRITE_MASK, exception); | |
6947 | } | |
6a4d7550 | 6948 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
77c2002e | 6949 | |
4d31d9ef SC |
6950 | static int kvm_can_emulate_insn(struct kvm_vcpu *vcpu, int emul_type, |
6951 | void *insn, int insn_len) | |
6952 | { | |
6953 | return static_call(kvm_x86_can_emulate_instruction)(vcpu, emul_type, | |
6954 | insn, insn_len); | |
6955 | } | |
6956 | ||
082d06ed WL |
6957 | int handle_ud(struct kvm_vcpu *vcpu) |
6958 | { | |
b3dc0695 | 6959 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
6c86eedc | 6960 | int emul_type = EMULTYPE_TRAP_UD; |
6c86eedc WL |
6961 | char sig[5]; /* ud2; .ascii "kvm" */ |
6962 | struct x86_exception e; | |
6963 | ||
4d31d9ef | 6964 | if (unlikely(!kvm_can_emulate_insn(vcpu, emul_type, NULL, 0))) |
09e3e2a1 SC |
6965 | return 1; |
6966 | ||
6c86eedc | 6967 | if (force_emulation_prefix && |
3c9fa24c PB |
6968 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
6969 | sig, sizeof(sig), &e) == 0 && | |
b3dc0695 | 6970 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
6c86eedc | 6971 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
b4000606 | 6972 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
6c86eedc | 6973 | } |
082d06ed | 6974 | |
60fc3d02 | 6975 | return kvm_emulate_instruction(vcpu, emul_type); |
082d06ed WL |
6976 | } |
6977 | EXPORT_SYMBOL_GPL(handle_ud); | |
6978 | ||
0f89b207 TL |
6979 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
6980 | gpa_t gpa, bool write) | |
6981 | { | |
6982 | /* For APIC access vmexit */ | |
6983 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
6984 | return 1; | |
6985 | ||
6986 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { | |
6987 | trace_vcpu_match_mmio(gva, gpa, write, true); | |
6988 | return 1; | |
6989 | } | |
6990 | ||
6991 | return 0; | |
6992 | } | |
6993 | ||
af7cc7d1 XG |
6994 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
6995 | gpa_t *gpa, struct x86_exception *exception, | |
6996 | bool write) | |
6997 | { | |
1f5a21ee | 6998 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
5b22bbe7 | 6999 | u64 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0) |
97d64b78 | 7000 | | (write ? PFERR_WRITE_MASK : 0); |
af7cc7d1 | 7001 | |
be94f6b7 HH |
7002 | /* |
7003 | * currently PKRU is only applied to ept enabled guest so | |
7004 | * there is no pkey in EPT page table for L1 guest or EPT | |
7005 | * shadow page table for L2 guest. | |
7006 | */ | |
908b7d43 SC |
7007 | if (vcpu_match_mmio_gva(vcpu, gva) && (!is_paging(vcpu) || |
7008 | !permission_fault(vcpu, vcpu->arch.walk_mmu, | |
7009 | vcpu->arch.mmio_access, 0, access))) { | |
bebb106a XG |
7010 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
7011 | (gva & (PAGE_SIZE - 1)); | |
4f022648 | 7012 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
bebb106a XG |
7013 | return 1; |
7014 | } | |
7015 | ||
1f5a21ee | 7016 | *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); |
af7cc7d1 XG |
7017 | |
7018 | if (*gpa == UNMAPPED_GVA) | |
7019 | return -1; | |
7020 | ||
0f89b207 | 7021 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
af7cc7d1 XG |
7022 | } |
7023 | ||
3200f405 | 7024 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
bcc55cba | 7025 | const void *val, int bytes) |
bbd9b64e CO |
7026 | { |
7027 | int ret; | |
7028 | ||
54bf36aa | 7029 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
9f811285 | 7030 | if (ret < 0) |
bbd9b64e | 7031 | return 0; |
0eb05bf2 | 7032 | kvm_page_track_write(vcpu, gpa, val, bytes); |
bbd9b64e CO |
7033 | return 1; |
7034 | } | |
7035 | ||
77d197b2 XG |
7036 | struct read_write_emulator_ops { |
7037 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, | |
7038 | int bytes); | |
7039 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7040 | void *val, int bytes); | |
7041 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7042 | int bytes, void *val); | |
7043 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7044 | void *val, int bytes); | |
7045 | bool write; | |
7046 | }; | |
7047 | ||
7048 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) | |
7049 | { | |
7050 | if (vcpu->mmio_read_completed) { | |
77d197b2 | 7051 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
e39d200f | 7052 | vcpu->mmio_fragments[0].gpa, val); |
77d197b2 XG |
7053 | vcpu->mmio_read_completed = 0; |
7054 | return 1; | |
7055 | } | |
7056 | ||
7057 | return 0; | |
7058 | } | |
7059 | ||
7060 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7061 | void *val, int bytes) | |
7062 | { | |
54bf36aa | 7063 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
77d197b2 XG |
7064 | } |
7065 | ||
7066 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7067 | void *val, int bytes) | |
7068 | { | |
7069 | return emulator_write_phys(vcpu, gpa, val, bytes); | |
7070 | } | |
7071 | ||
7072 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) | |
7073 | { | |
e39d200f | 7074 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
77d197b2 XG |
7075 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
7076 | } | |
7077 | ||
7078 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7079 | void *val, int bytes) | |
7080 | { | |
e39d200f | 7081 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
77d197b2 XG |
7082 | return X86EMUL_IO_NEEDED; |
7083 | } | |
7084 | ||
7085 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
7086 | void *val, int bytes) | |
7087 | { | |
f78146b0 AK |
7088 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
7089 | ||
87da7e66 | 7090 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
77d197b2 XG |
7091 | return X86EMUL_CONTINUE; |
7092 | } | |
7093 | ||
0fbe9b0b | 7094 | static const struct read_write_emulator_ops read_emultor = { |
77d197b2 XG |
7095 | .read_write_prepare = read_prepare, |
7096 | .read_write_emulate = read_emulate, | |
7097 | .read_write_mmio = vcpu_mmio_read, | |
7098 | .read_write_exit_mmio = read_exit_mmio, | |
7099 | }; | |
7100 | ||
0fbe9b0b | 7101 | static const struct read_write_emulator_ops write_emultor = { |
77d197b2 XG |
7102 | .read_write_emulate = write_emulate, |
7103 | .read_write_mmio = write_mmio, | |
7104 | .read_write_exit_mmio = write_exit_mmio, | |
7105 | .write = true, | |
7106 | }; | |
7107 | ||
22388a3c XG |
7108 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
7109 | unsigned int bytes, | |
7110 | struct x86_exception *exception, | |
7111 | struct kvm_vcpu *vcpu, | |
0fbe9b0b | 7112 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 7113 | { |
af7cc7d1 XG |
7114 | gpa_t gpa; |
7115 | int handled, ret; | |
22388a3c | 7116 | bool write = ops->write; |
f78146b0 | 7117 | struct kvm_mmio_fragment *frag; |
c9b8b07c | 7118 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
0f89b207 TL |
7119 | |
7120 | /* | |
7121 | * If the exit was due to a NPF we may already have a GPA. | |
7122 | * If the GPA is present, use it to avoid the GVA to GPA table walk. | |
7123 | * Note, this cannot be used on string operations since string | |
7124 | * operation using rep will only have the initial GPA from the NPF | |
7125 | * occurred. | |
7126 | */ | |
744e699c SC |
7127 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
7128 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { | |
7129 | gpa = ctxt->gpa_val; | |
618232e2 BS |
7130 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
7131 | } else { | |
7132 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); | |
7133 | if (ret < 0) | |
7134 | return X86EMUL_PROPAGATE_FAULT; | |
0f89b207 | 7135 | } |
10589a46 | 7136 | |
618232e2 | 7137 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
bbd9b64e CO |
7138 | return X86EMUL_CONTINUE; |
7139 | ||
bbd9b64e CO |
7140 | /* |
7141 | * Is this MMIO handled locally? | |
7142 | */ | |
22388a3c | 7143 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
70252a10 | 7144 | if (handled == bytes) |
bbd9b64e | 7145 | return X86EMUL_CONTINUE; |
bbd9b64e | 7146 | |
70252a10 AK |
7147 | gpa += handled; |
7148 | bytes -= handled; | |
7149 | val += handled; | |
7150 | ||
87da7e66 XG |
7151 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
7152 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; | |
7153 | frag->gpa = gpa; | |
7154 | frag->data = val; | |
7155 | frag->len = bytes; | |
f78146b0 | 7156 | return X86EMUL_CONTINUE; |
bbd9b64e CO |
7157 | } |
7158 | ||
52eb5a6d XL |
7159 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
7160 | unsigned long addr, | |
22388a3c XG |
7161 | void *val, unsigned int bytes, |
7162 | struct x86_exception *exception, | |
0fbe9b0b | 7163 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 7164 | { |
0f65dd70 | 7165 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
f78146b0 AK |
7166 | gpa_t gpa; |
7167 | int rc; | |
7168 | ||
7169 | if (ops->read_write_prepare && | |
7170 | ops->read_write_prepare(vcpu, val, bytes)) | |
7171 | return X86EMUL_CONTINUE; | |
7172 | ||
7173 | vcpu->mmio_nr_fragments = 0; | |
0f65dd70 | 7174 | |
bbd9b64e CO |
7175 | /* Crossing a page boundary? */ |
7176 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
f78146b0 | 7177 | int now; |
bbd9b64e CO |
7178 | |
7179 | now = -addr & ~PAGE_MASK; | |
22388a3c XG |
7180 | rc = emulator_read_write_onepage(addr, val, now, exception, |
7181 | vcpu, ops); | |
7182 | ||
bbd9b64e CO |
7183 | if (rc != X86EMUL_CONTINUE) |
7184 | return rc; | |
7185 | addr += now; | |
bac15531 NA |
7186 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
7187 | addr = (u32)addr; | |
bbd9b64e CO |
7188 | val += now; |
7189 | bytes -= now; | |
7190 | } | |
22388a3c | 7191 | |
f78146b0 AK |
7192 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
7193 | vcpu, ops); | |
7194 | if (rc != X86EMUL_CONTINUE) | |
7195 | return rc; | |
7196 | ||
7197 | if (!vcpu->mmio_nr_fragments) | |
7198 | return rc; | |
7199 | ||
7200 | gpa = vcpu->mmio_fragments[0].gpa; | |
7201 | ||
7202 | vcpu->mmio_needed = 1; | |
7203 | vcpu->mmio_cur_fragment = 0; | |
7204 | ||
87da7e66 | 7205 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
f78146b0 AK |
7206 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
7207 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
7208 | vcpu->run->mmio.phys_addr = gpa; | |
7209 | ||
7210 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); | |
22388a3c XG |
7211 | } |
7212 | ||
7213 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, | |
7214 | unsigned long addr, | |
7215 | void *val, | |
7216 | unsigned int bytes, | |
7217 | struct x86_exception *exception) | |
7218 | { | |
7219 | return emulator_read_write(ctxt, addr, val, bytes, | |
7220 | exception, &read_emultor); | |
7221 | } | |
7222 | ||
52eb5a6d | 7223 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
22388a3c XG |
7224 | unsigned long addr, |
7225 | const void *val, | |
7226 | unsigned int bytes, | |
7227 | struct x86_exception *exception) | |
7228 | { | |
7229 | return emulator_read_write(ctxt, addr, (void *)val, bytes, | |
7230 | exception, &write_emultor); | |
bbd9b64e | 7231 | } |
bbd9b64e | 7232 | |
daea3e73 AK |
7233 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
7234 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) | |
7235 | ||
7236 | #ifdef CONFIG_X86_64 | |
7237 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) | |
7238 | #else | |
7239 | # define CMPXCHG64(ptr, old, new) \ | |
9749a6c0 | 7240 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
daea3e73 AK |
7241 | #endif |
7242 | ||
0f65dd70 AK |
7243 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
7244 | unsigned long addr, | |
bbd9b64e CO |
7245 | const void *old, |
7246 | const void *new, | |
7247 | unsigned int bytes, | |
0f65dd70 | 7248 | struct x86_exception *exception) |
bbd9b64e | 7249 | { |
42e35f80 | 7250 | struct kvm_host_map map; |
0f65dd70 | 7251 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
9de6fe3c | 7252 | u64 page_line_mask; |
daea3e73 | 7253 | gpa_t gpa; |
daea3e73 AK |
7254 | char *kaddr; |
7255 | bool exchanged; | |
2bacc55c | 7256 | |
daea3e73 AK |
7257 | /* guests cmpxchg8b have to be emulated atomically */ |
7258 | if (bytes > 8 || (bytes & (bytes - 1))) | |
7259 | goto emul_write; | |
10589a46 | 7260 | |
daea3e73 | 7261 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
2bacc55c | 7262 | |
daea3e73 AK |
7263 | if (gpa == UNMAPPED_GVA || |
7264 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
7265 | goto emul_write; | |
2bacc55c | 7266 | |
9de6fe3c XL |
7267 | /* |
7268 | * Emulate the atomic as a straight write to avoid #AC if SLD is | |
7269 | * enabled in the host and the access splits a cache line. | |
7270 | */ | |
7271 | if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) | |
7272 | page_line_mask = ~(cache_line_size() - 1); | |
7273 | else | |
7274 | page_line_mask = PAGE_MASK; | |
7275 | ||
7276 | if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) | |
daea3e73 | 7277 | goto emul_write; |
72dc67a6 | 7278 | |
42e35f80 | 7279 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
c19b8bd6 | 7280 | goto emul_write; |
72dc67a6 | 7281 | |
42e35f80 KA |
7282 | kaddr = map.hva + offset_in_page(gpa); |
7283 | ||
daea3e73 AK |
7284 | switch (bytes) { |
7285 | case 1: | |
7286 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); | |
7287 | break; | |
7288 | case 2: | |
7289 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); | |
7290 | break; | |
7291 | case 4: | |
7292 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); | |
7293 | break; | |
7294 | case 8: | |
7295 | exchanged = CMPXCHG64(kaddr, old, new); | |
7296 | break; | |
7297 | default: | |
7298 | BUG(); | |
2bacc55c | 7299 | } |
42e35f80 KA |
7300 | |
7301 | kvm_vcpu_unmap(vcpu, &map, true); | |
daea3e73 AK |
7302 | |
7303 | if (!exchanged) | |
7304 | return X86EMUL_CMPXCHG_FAILED; | |
7305 | ||
0eb05bf2 | 7306 | kvm_page_track_write(vcpu, gpa, new, bytes); |
8f6abd06 GN |
7307 | |
7308 | return X86EMUL_CONTINUE; | |
4a5f48f6 | 7309 | |
3200f405 | 7310 | emul_write: |
daea3e73 | 7311 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
2bacc55c | 7312 | |
0f65dd70 | 7313 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
bbd9b64e CO |
7314 | } |
7315 | ||
cf8f70bf GN |
7316 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
7317 | { | |
cbfc6c91 | 7318 | int r = 0, i; |
cf8f70bf | 7319 | |
cbfc6c91 WL |
7320 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
7321 | if (vcpu->arch.pio.in) | |
7322 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, | |
7323 | vcpu->arch.pio.size, pd); | |
7324 | else | |
7325 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, | |
7326 | vcpu->arch.pio.port, vcpu->arch.pio.size, | |
7327 | pd); | |
7328 | if (r) | |
7329 | break; | |
7330 | pd += vcpu->arch.pio.size; | |
7331 | } | |
cf8f70bf GN |
7332 | return r; |
7333 | } | |
7334 | ||
6f6fbe98 | 7335 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
3b27de27 | 7336 | unsigned short port, |
6f6fbe98 | 7337 | unsigned int count, bool in) |
cf8f70bf | 7338 | { |
cf8f70bf | 7339 | vcpu->arch.pio.port = port; |
6f6fbe98 | 7340 | vcpu->arch.pio.in = in; |
7972995b | 7341 | vcpu->arch.pio.count = count; |
cf8f70bf GN |
7342 | vcpu->arch.pio.size = size; |
7343 | ||
0d33b1ba | 7344 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) |
cf8f70bf | 7345 | return 1; |
cf8f70bf GN |
7346 | |
7347 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
6f6fbe98 | 7348 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
cf8f70bf GN |
7349 | vcpu->run->io.size = size; |
7350 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
7351 | vcpu->run->io.count = count; | |
7352 | vcpu->run->io.port = port; | |
7353 | ||
7354 | return 0; | |
7355 | } | |
7356 | ||
3b27de27 PB |
7357 | static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
7358 | unsigned short port, unsigned int count) | |
cf8f70bf | 7359 | { |
3b27de27 PB |
7360 | WARN_ON(vcpu->arch.pio.count); |
7361 | memset(vcpu->arch.pio_data, 0, size * count); | |
7362 | return emulator_pio_in_out(vcpu, size, port, count, true); | |
7363 | } | |
ca1d4a9e | 7364 | |
6b5efc93 | 7365 | static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, void *val) |
3b27de27 | 7366 | { |
6b5efc93 PB |
7367 | int size = vcpu->arch.pio.size; |
7368 | unsigned count = vcpu->arch.pio.count; | |
7369 | memcpy(val, vcpu->arch.pio_data, size * count); | |
7370 | trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); | |
3b27de27 PB |
7371 | vcpu->arch.pio.count = 0; |
7372 | } | |
cf8f70bf | 7373 | |
3b27de27 PB |
7374 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
7375 | unsigned short port, void *val, unsigned int count) | |
7376 | { | |
7377 | if (vcpu->arch.pio.count) { | |
d07898ea SC |
7378 | /* |
7379 | * Complete a previous iteration that required userspace I/O. | |
7380 | * Note, @count isn't guaranteed to match pio.count as userspace | |
7381 | * can modify ECX before rerunning the vCPU. Ignore any such | |
7382 | * shenanigans as KVM doesn't support modifying the rep count, | |
7383 | * and the emulator ensures @count doesn't overflow the buffer. | |
7384 | */ | |
3b27de27 PB |
7385 | } else { |
7386 | int r = __emulator_pio_in(vcpu, size, port, count); | |
7387 | if (!r) | |
7388 | return r; | |
cbfc6c91 | 7389 | |
3b27de27 | 7390 | /* Results already available, fall through. */ |
cf8f70bf GN |
7391 | } |
7392 | ||
6b5efc93 | 7393 | complete_emulator_pio_in(vcpu, val); |
3b27de27 | 7394 | return 1; |
cf8f70bf GN |
7395 | } |
7396 | ||
2e3bb4d8 SC |
7397 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
7398 | int size, unsigned short port, void *val, | |
7399 | unsigned int count) | |
6f6fbe98 | 7400 | { |
2e3bb4d8 | 7401 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
6f6fbe98 | 7402 | |
2e3bb4d8 | 7403 | } |
6f6fbe98 | 7404 | |
2e3bb4d8 SC |
7405 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
7406 | unsigned short port, const void *val, | |
7407 | unsigned int count) | |
7408 | { | |
0d33b1ba PB |
7409 | int ret; |
7410 | ||
6f6fbe98 | 7411 | memcpy(vcpu->arch.pio_data, val, size * count); |
1171903d | 7412 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
3b27de27 | 7413 | ret = emulator_pio_in_out(vcpu, size, port, count, false); |
0d33b1ba PB |
7414 | if (ret) |
7415 | vcpu->arch.pio.count = 0; | |
7416 | ||
7417 | return ret; | |
6f6fbe98 XG |
7418 | } |
7419 | ||
2e3bb4d8 SC |
7420 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
7421 | int size, unsigned short port, | |
7422 | const void *val, unsigned int count) | |
7423 | { | |
7424 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); | |
7425 | } | |
7426 | ||
bbd9b64e CO |
7427 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
7428 | { | |
b3646477 | 7429 | return static_call(kvm_x86_get_segment_base)(vcpu, seg); |
bbd9b64e CO |
7430 | } |
7431 | ||
3cb16fe7 | 7432 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
bbd9b64e | 7433 | { |
3cb16fe7 | 7434 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
bbd9b64e CO |
7435 | } |
7436 | ||
ae6a2375 | 7437 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
f5f48ee1 SY |
7438 | { |
7439 | if (!need_emulate_wbinvd(vcpu)) | |
7440 | return X86EMUL_CONTINUE; | |
7441 | ||
b3646477 | 7442 | if (static_call(kvm_x86_has_wbinvd_exit)()) { |
2eec7343 JK |
7443 | int cpu = get_cpu(); |
7444 | ||
7445 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); | |
c2162e13 | 7446 | on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, |
f5f48ee1 | 7447 | wbinvd_ipi, NULL, 1); |
2eec7343 | 7448 | put_cpu(); |
f5f48ee1 | 7449 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
2eec7343 JK |
7450 | } else |
7451 | wbinvd(); | |
f5f48ee1 SY |
7452 | return X86EMUL_CONTINUE; |
7453 | } | |
5cb56059 JS |
7454 | |
7455 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
7456 | { | |
6affcbed KH |
7457 | kvm_emulate_wbinvd_noskip(vcpu); |
7458 | return kvm_skip_emulated_instruction(vcpu); | |
5cb56059 | 7459 | } |
f5f48ee1 SY |
7460 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
7461 | ||
5cb56059 JS |
7462 | |
7463 | ||
bcaf5cc5 AK |
7464 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
7465 | { | |
5cb56059 | 7466 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
bcaf5cc5 AK |
7467 | } |
7468 | ||
29d6ca41 PB |
7469 | static void emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
7470 | unsigned long *dest) | |
bbd9b64e | 7471 | { |
29d6ca41 | 7472 | kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
bbd9b64e CO |
7473 | } |
7474 | ||
52eb5a6d XL |
7475 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
7476 | unsigned long value) | |
bbd9b64e | 7477 | { |
338dbc97 | 7478 | |
996ff542 | 7479 | return kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
bbd9b64e CO |
7480 | } |
7481 | ||
52a46617 | 7482 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
5fdbf976 | 7483 | { |
52a46617 | 7484 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
5fdbf976 MT |
7485 | } |
7486 | ||
717746e3 | 7487 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
bbd9b64e | 7488 | { |
717746e3 | 7489 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
52a46617 GN |
7490 | unsigned long value; |
7491 | ||
7492 | switch (cr) { | |
7493 | case 0: | |
7494 | value = kvm_read_cr0(vcpu); | |
7495 | break; | |
7496 | case 2: | |
7497 | value = vcpu->arch.cr2; | |
7498 | break; | |
7499 | case 3: | |
9f8fe504 | 7500 | value = kvm_read_cr3(vcpu); |
52a46617 GN |
7501 | break; |
7502 | case 4: | |
7503 | value = kvm_read_cr4(vcpu); | |
7504 | break; | |
7505 | case 8: | |
7506 | value = kvm_get_cr8(vcpu); | |
7507 | break; | |
7508 | default: | |
a737f256 | 7509 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
52a46617 GN |
7510 | return 0; |
7511 | } | |
7512 | ||
7513 | return value; | |
7514 | } | |
7515 | ||
717746e3 | 7516 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
52a46617 | 7517 | { |
717746e3 | 7518 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
0f12244f GN |
7519 | int res = 0; |
7520 | ||
52a46617 GN |
7521 | switch (cr) { |
7522 | case 0: | |
49a9b07e | 7523 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
52a46617 GN |
7524 | break; |
7525 | case 2: | |
7526 | vcpu->arch.cr2 = val; | |
7527 | break; | |
7528 | case 3: | |
2390218b | 7529 | res = kvm_set_cr3(vcpu, val); |
52a46617 GN |
7530 | break; |
7531 | case 4: | |
a83b29c6 | 7532 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
52a46617 GN |
7533 | break; |
7534 | case 8: | |
eea1cff9 | 7535 | res = kvm_set_cr8(vcpu, val); |
52a46617 GN |
7536 | break; |
7537 | default: | |
a737f256 | 7538 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
0f12244f | 7539 | res = -1; |
52a46617 | 7540 | } |
0f12244f GN |
7541 | |
7542 | return res; | |
52a46617 GN |
7543 | } |
7544 | ||
717746e3 | 7545 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
9c537244 | 7546 | { |
b3646477 | 7547 | return static_call(kvm_x86_get_cpl)(emul_to_vcpu(ctxt)); |
9c537244 GN |
7548 | } |
7549 | ||
4bff1e86 | 7550 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
2dafc6c2 | 7551 | { |
b3646477 | 7552 | static_call(kvm_x86_get_gdt)(emul_to_vcpu(ctxt), dt); |
2dafc6c2 GN |
7553 | } |
7554 | ||
4bff1e86 | 7555 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
160ce1f1 | 7556 | { |
b3646477 | 7557 | static_call(kvm_x86_get_idt)(emul_to_vcpu(ctxt), dt); |
160ce1f1 MG |
7558 | } |
7559 | ||
1ac9d0cf AK |
7560 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
7561 | { | |
b3646477 | 7562 | static_call(kvm_x86_set_gdt)(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
7563 | } |
7564 | ||
7565 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) | |
7566 | { | |
b3646477 | 7567 | static_call(kvm_x86_set_idt)(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
7568 | } |
7569 | ||
4bff1e86 AK |
7570 | static unsigned long emulator_get_cached_segment_base( |
7571 | struct x86_emulate_ctxt *ctxt, int seg) | |
5951c442 | 7572 | { |
4bff1e86 | 7573 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
5951c442 GN |
7574 | } |
7575 | ||
1aa36616 AK |
7576 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
7577 | struct desc_struct *desc, u32 *base3, | |
7578 | int seg) | |
2dafc6c2 GN |
7579 | { |
7580 | struct kvm_segment var; | |
7581 | ||
4bff1e86 | 7582 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
1aa36616 | 7583 | *selector = var.selector; |
2dafc6c2 | 7584 | |
378a8b09 GN |
7585 | if (var.unusable) { |
7586 | memset(desc, 0, sizeof(*desc)); | |
f0367ee1 RK |
7587 | if (base3) |
7588 | *base3 = 0; | |
2dafc6c2 | 7589 | return false; |
378a8b09 | 7590 | } |
2dafc6c2 GN |
7591 | |
7592 | if (var.g) | |
7593 | var.limit >>= 12; | |
7594 | set_desc_limit(desc, var.limit); | |
7595 | set_desc_base(desc, (unsigned long)var.base); | |
5601d05b GN |
7596 | #ifdef CONFIG_X86_64 |
7597 | if (base3) | |
7598 | *base3 = var.base >> 32; | |
7599 | #endif | |
2dafc6c2 GN |
7600 | desc->type = var.type; |
7601 | desc->s = var.s; | |
7602 | desc->dpl = var.dpl; | |
7603 | desc->p = var.present; | |
7604 | desc->avl = var.avl; | |
7605 | desc->l = var.l; | |
7606 | desc->d = var.db; | |
7607 | desc->g = var.g; | |
7608 | ||
7609 | return true; | |
7610 | } | |
7611 | ||
1aa36616 AK |
7612 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
7613 | struct desc_struct *desc, u32 base3, | |
7614 | int seg) | |
2dafc6c2 | 7615 | { |
4bff1e86 | 7616 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
2dafc6c2 GN |
7617 | struct kvm_segment var; |
7618 | ||
1aa36616 | 7619 | var.selector = selector; |
2dafc6c2 | 7620 | var.base = get_desc_base(desc); |
5601d05b GN |
7621 | #ifdef CONFIG_X86_64 |
7622 | var.base |= ((u64)base3) << 32; | |
7623 | #endif | |
2dafc6c2 GN |
7624 | var.limit = get_desc_limit(desc); |
7625 | if (desc->g) | |
7626 | var.limit = (var.limit << 12) | 0xfff; | |
7627 | var.type = desc->type; | |
2dafc6c2 GN |
7628 | var.dpl = desc->dpl; |
7629 | var.db = desc->d; | |
7630 | var.s = desc->s; | |
7631 | var.l = desc->l; | |
7632 | var.g = desc->g; | |
7633 | var.avl = desc->avl; | |
7634 | var.present = desc->p; | |
7635 | var.unusable = !var.present; | |
7636 | var.padding = 0; | |
7637 | ||
7638 | kvm_set_segment(vcpu, &var, seg); | |
7639 | return; | |
7640 | } | |
7641 | ||
ac8d6cad HW |
7642 | static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt, |
7643 | u32 msr_index, u64 *pdata) | |
717746e3 | 7644 | { |
1ae09954 AG |
7645 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
7646 | int r; | |
7647 | ||
ac8d6cad | 7648 | r = kvm_get_msr_with_filter(vcpu, msr_index, pdata); |
1ae09954 | 7649 | |
d2f7d498 HW |
7650 | if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0, |
7651 | complete_emulated_rdmsr, r)) { | |
1ae09954 AG |
7652 | /* Bounce to user space */ |
7653 | return X86EMUL_IO_NEEDED; | |
7654 | } | |
7655 | ||
7656 | return r; | |
717746e3 AK |
7657 | } |
7658 | ||
ac8d6cad HW |
7659 | static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt, |
7660 | u32 msr_index, u64 data) | |
717746e3 | 7661 | { |
1ae09954 AG |
7662 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
7663 | int r; | |
7664 | ||
ac8d6cad | 7665 | r = kvm_set_msr_with_filter(vcpu, msr_index, data); |
1ae09954 | 7666 | |
d2f7d498 HW |
7667 | if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data, |
7668 | complete_emulated_msr_access, r)) { | |
1ae09954 AG |
7669 | /* Bounce to user space */ |
7670 | return X86EMUL_IO_NEEDED; | |
7671 | } | |
7672 | ||
7673 | return r; | |
717746e3 AK |
7674 | } |
7675 | ||
ac8d6cad HW |
7676 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
7677 | u32 msr_index, u64 *pdata) | |
7678 | { | |
7679 | return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata); | |
7680 | } | |
7681 | ||
7682 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, | |
7683 | u32 msr_index, u64 data) | |
7684 | { | |
7685 | return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data); | |
7686 | } | |
7687 | ||
64d60670 PB |
7688 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
7689 | { | |
7690 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
7691 | ||
7692 | return vcpu->arch.smbase; | |
7693 | } | |
7694 | ||
7695 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) | |
7696 | { | |
7697 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
7698 | ||
7699 | vcpu->arch.smbase = smbase; | |
7700 | } | |
7701 | ||
67f4d428 NA |
7702 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
7703 | u32 pmc) | |
7704 | { | |
e6cd31f1 JM |
7705 | if (kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc)) |
7706 | return 0; | |
7707 | return -EINVAL; | |
67f4d428 NA |
7708 | } |
7709 | ||
222d21aa AK |
7710 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
7711 | u32 pmc, u64 *pdata) | |
7712 | { | |
c6702c9d | 7713 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
222d21aa AK |
7714 | } |
7715 | ||
6c3287f7 AK |
7716 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
7717 | { | |
7718 | emul_to_vcpu(ctxt)->arch.halt_request = 1; | |
7719 | } | |
7720 | ||
2953538e | 7721 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
8a76d7f2 | 7722 | struct x86_instruction_info *info, |
c4f035c6 AK |
7723 | enum x86_intercept_stage stage) |
7724 | { | |
b3646477 | 7725 | return static_call(kvm_x86_check_intercept)(emul_to_vcpu(ctxt), info, stage, |
21f1b8f2 | 7726 | &ctxt->exception); |
c4f035c6 AK |
7727 | } |
7728 | ||
e911eb3b | 7729 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
f91af517 SC |
7730 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
7731 | bool exact_only) | |
bdb42f5a | 7732 | { |
f91af517 | 7733 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
bdb42f5a SB |
7734 | } |
7735 | ||
5ae78e95 SC |
7736 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
7737 | { | |
7738 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); | |
7739 | } | |
7740 | ||
7741 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) | |
7742 | { | |
7743 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); | |
7744 | } | |
7745 | ||
7746 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) | |
7747 | { | |
7748 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); | |
7749 | } | |
7750 | ||
a836839c HW |
7751 | static bool emulator_guest_has_rdpid(struct x86_emulate_ctxt *ctxt) |
7752 | { | |
7753 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_RDPID); | |
7754 | } | |
7755 | ||
dd856efa AK |
7756 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
7757 | { | |
27b4a9c4 | 7758 | return kvm_register_read_raw(emul_to_vcpu(ctxt), reg); |
dd856efa AK |
7759 | } |
7760 | ||
7761 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) | |
7762 | { | |
27b4a9c4 | 7763 | kvm_register_write_raw(emul_to_vcpu(ctxt), reg, val); |
dd856efa AK |
7764 | } |
7765 | ||
801806d9 NA |
7766 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
7767 | { | |
b3646477 | 7768 | static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked); |
801806d9 NA |
7769 | } |
7770 | ||
6ed071f0 LP |
7771 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
7772 | { | |
7773 | return emul_to_vcpu(ctxt)->arch.hflags; | |
7774 | } | |
7775 | ||
edce4654 | 7776 | static void emulator_exiting_smm(struct x86_emulate_ctxt *ctxt) |
6ed071f0 | 7777 | { |
78fcb2c9 SC |
7778 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
7779 | ||
dc87275f | 7780 | kvm_smm_changed(vcpu, false); |
6ed071f0 LP |
7781 | } |
7782 | ||
ecc513e5 | 7783 | static int emulator_leave_smm(struct x86_emulate_ctxt *ctxt, |
ed19321f | 7784 | const char *smstate) |
0234bf88 | 7785 | { |
ecc513e5 | 7786 | return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smstate); |
0234bf88 LP |
7787 | } |
7788 | ||
25b17226 SC |
7789 | static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt) |
7790 | { | |
7791 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, emul_to_vcpu(ctxt)); | |
7792 | } | |
7793 | ||
02d4160f VK |
7794 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
7795 | { | |
7796 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); | |
7797 | } | |
7798 | ||
0225fb50 | 7799 | static const struct x86_emulate_ops emulate_ops = { |
dd856efa AK |
7800 | .read_gpr = emulator_read_gpr, |
7801 | .write_gpr = emulator_write_gpr, | |
ce14e868 PB |
7802 | .read_std = emulator_read_std, |
7803 | .write_std = emulator_write_std, | |
7a036a6f | 7804 | .read_phys = kvm_read_guest_phys_system, |
1871c602 | 7805 | .fetch = kvm_fetch_guest_virt, |
bbd9b64e CO |
7806 | .read_emulated = emulator_read_emulated, |
7807 | .write_emulated = emulator_write_emulated, | |
7808 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
3cb16fe7 | 7809 | .invlpg = emulator_invlpg, |
cf8f70bf GN |
7810 | .pio_in_emulated = emulator_pio_in_emulated, |
7811 | .pio_out_emulated = emulator_pio_out_emulated, | |
1aa36616 AK |
7812 | .get_segment = emulator_get_segment, |
7813 | .set_segment = emulator_set_segment, | |
5951c442 | 7814 | .get_cached_segment_base = emulator_get_cached_segment_base, |
2dafc6c2 | 7815 | .get_gdt = emulator_get_gdt, |
160ce1f1 | 7816 | .get_idt = emulator_get_idt, |
1ac9d0cf AK |
7817 | .set_gdt = emulator_set_gdt, |
7818 | .set_idt = emulator_set_idt, | |
52a46617 GN |
7819 | .get_cr = emulator_get_cr, |
7820 | .set_cr = emulator_set_cr, | |
9c537244 | 7821 | .cpl = emulator_get_cpl, |
35aa5375 GN |
7822 | .get_dr = emulator_get_dr, |
7823 | .set_dr = emulator_set_dr, | |
64d60670 PB |
7824 | .get_smbase = emulator_get_smbase, |
7825 | .set_smbase = emulator_set_smbase, | |
ac8d6cad HW |
7826 | .set_msr_with_filter = emulator_set_msr_with_filter, |
7827 | .get_msr_with_filter = emulator_get_msr_with_filter, | |
717746e3 AK |
7828 | .set_msr = emulator_set_msr, |
7829 | .get_msr = emulator_get_msr, | |
67f4d428 | 7830 | .check_pmc = emulator_check_pmc, |
222d21aa | 7831 | .read_pmc = emulator_read_pmc, |
6c3287f7 | 7832 | .halt = emulator_halt, |
bcaf5cc5 | 7833 | .wbinvd = emulator_wbinvd, |
d6aa1000 | 7834 | .fix_hypercall = emulator_fix_hypercall, |
c4f035c6 | 7835 | .intercept = emulator_intercept, |
bdb42f5a | 7836 | .get_cpuid = emulator_get_cpuid, |
5ae78e95 SC |
7837 | .guest_has_long_mode = emulator_guest_has_long_mode, |
7838 | .guest_has_movbe = emulator_guest_has_movbe, | |
7839 | .guest_has_fxsr = emulator_guest_has_fxsr, | |
a836839c | 7840 | .guest_has_rdpid = emulator_guest_has_rdpid, |
801806d9 | 7841 | .set_nmi_mask = emulator_set_nmi_mask, |
6ed071f0 | 7842 | .get_hflags = emulator_get_hflags, |
edce4654 | 7843 | .exiting_smm = emulator_exiting_smm, |
ecc513e5 | 7844 | .leave_smm = emulator_leave_smm, |
25b17226 | 7845 | .triple_fault = emulator_triple_fault, |
02d4160f | 7846 | .set_xcr = emulator_set_xcr, |
bbd9b64e CO |
7847 | }; |
7848 | ||
95cb2295 GN |
7849 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
7850 | { | |
b3646477 | 7851 | u32 int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); |
95cb2295 GN |
7852 | /* |
7853 | * an sti; sti; sequence only disable interrupts for the first | |
7854 | * instruction. So, if the last instruction, be it emulated or | |
7855 | * not, left the system with the INT_STI flag enabled, it | |
7856 | * means that the last instruction is an sti. We should not | |
7857 | * leave the flag on in this case. The same goes for mov ss | |
7858 | */ | |
37ccdcbe PB |
7859 | if (int_shadow & mask) |
7860 | mask = 0; | |
6addfc42 | 7861 | if (unlikely(int_shadow || mask)) { |
b3646477 | 7862 | static_call(kvm_x86_set_interrupt_shadow)(vcpu, mask); |
6addfc42 PB |
7863 | if (!mask) |
7864 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7865 | } | |
95cb2295 GN |
7866 | } |
7867 | ||
ef54bcfe | 7868 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
54b8486f | 7869 | { |
c9b8b07c | 7870 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
da9cb575 | 7871 | if (ctxt->exception.vector == PF_VECTOR) |
53b3d8e9 | 7872 | return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); |
ef54bcfe PB |
7873 | |
7874 | if (ctxt->exception.error_code_valid) | |
da9cb575 AK |
7875 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
7876 | ctxt->exception.error_code); | |
54b8486f | 7877 | else |
da9cb575 | 7878 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
ef54bcfe | 7879 | return false; |
54b8486f GN |
7880 | } |
7881 | ||
c9b8b07c SC |
7882 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
7883 | { | |
7884 | struct x86_emulate_ctxt *ctxt; | |
7885 | ||
7886 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); | |
7887 | if (!ctxt) { | |
7888 | pr_err("kvm: failed to allocate vcpu's emulator\n"); | |
7889 | return NULL; | |
7890 | } | |
7891 | ||
7892 | ctxt->vcpu = vcpu; | |
7893 | ctxt->ops = &emulate_ops; | |
7894 | vcpu->arch.emulate_ctxt = ctxt; | |
7895 | ||
7896 | return ctxt; | |
7897 | } | |
7898 | ||
8ec4722d MG |
7899 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
7900 | { | |
c9b8b07c | 7901 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d MG |
7902 | int cs_db, cs_l; |
7903 | ||
b3646477 | 7904 | static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l); |
8ec4722d | 7905 | |
744e699c | 7906 | ctxt->gpa_available = false; |
adf52235 | 7907 | ctxt->eflags = kvm_get_rflags(vcpu); |
c8401dda PB |
7908 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
7909 | ||
adf52235 TY |
7910 | ctxt->eip = kvm_rip_read(vcpu); |
7911 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : | |
7912 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : | |
42bf549f | 7913 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
adf52235 TY |
7914 | cs_db ? X86EMUL_MODE_PROT32 : |
7915 | X86EMUL_MODE_PROT16; | |
a584539b | 7916 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
64d60670 PB |
7917 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
7918 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); | |
adf52235 | 7919 | |
da6393cd WL |
7920 | ctxt->interruptibility = 0; |
7921 | ctxt->have_exception = false; | |
7922 | ctxt->exception.vector = -1; | |
7923 | ctxt->perm_ok = false; | |
7924 | ||
dd856efa | 7925 | init_decode_cache(ctxt); |
7ae441ea | 7926 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
8ec4722d MG |
7927 | } |
7928 | ||
9497e1f2 | 7929 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
63995653 | 7930 | { |
c9b8b07c | 7931 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
63995653 MG |
7932 | int ret; |
7933 | ||
7934 | init_emulate_ctxt(vcpu); | |
7935 | ||
9dac77fa AK |
7936 | ctxt->op_bytes = 2; |
7937 | ctxt->ad_bytes = 2; | |
7938 | ctxt->_eip = ctxt->eip + inc_eip; | |
9d74191a | 7939 | ret = emulate_int_real(ctxt, irq); |
63995653 | 7940 | |
9497e1f2 SC |
7941 | if (ret != X86EMUL_CONTINUE) { |
7942 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
7943 | } else { | |
7944 | ctxt->eip = ctxt->_eip; | |
7945 | kvm_rip_write(vcpu, ctxt->eip); | |
7946 | kvm_set_rflags(vcpu, ctxt->eflags); | |
7947 | } | |
63995653 MG |
7948 | } |
7949 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); | |
7950 | ||
e615e355 DE |
7951 | static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, u64 *data, |
7952 | u8 ndata, u8 *insn_bytes, u8 insn_size) | |
19238e75 | 7953 | { |
19238e75 | 7954 | struct kvm_run *run = vcpu->run; |
e615e355 DE |
7955 | u64 info[5]; |
7956 | u8 info_start; | |
7957 | ||
7958 | /* | |
7959 | * Zero the whole array used to retrieve the exit info, as casting to | |
7960 | * u32 for select entries will leave some chunks uninitialized. | |
7961 | */ | |
7962 | memset(&info, 0, sizeof(info)); | |
7963 | ||
7964 | static_call(kvm_x86_get_exit_info)(vcpu, (u32 *)&info[0], &info[1], | |
7965 | &info[2], (u32 *)&info[3], | |
7966 | (u32 *)&info[4]); | |
19238e75 AL |
7967 | |
7968 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
7969 | run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
e615e355 DE |
7970 | |
7971 | /* | |
7972 | * There's currently space for 13 entries, but 5 are used for the exit | |
7973 | * reason and info. Restrict to 4 to reduce the maintenance burden | |
7974 | * when expanding kvm_run.emulation_failure in the future. | |
7975 | */ | |
7976 | if (WARN_ON_ONCE(ndata > 4)) | |
7977 | ndata = 4; | |
7978 | ||
7979 | /* Always include the flags as a 'data' entry. */ | |
7980 | info_start = 1; | |
19238e75 AL |
7981 | run->emulation_failure.flags = 0; |
7982 | ||
7983 | if (insn_size) { | |
e615e355 DE |
7984 | BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + |
7985 | sizeof(run->emulation_failure.insn_bytes) != 16)); | |
7986 | info_start += 2; | |
19238e75 AL |
7987 | run->emulation_failure.flags |= |
7988 | KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES; | |
7989 | run->emulation_failure.insn_size = insn_size; | |
7990 | memset(run->emulation_failure.insn_bytes, 0x90, | |
7991 | sizeof(run->emulation_failure.insn_bytes)); | |
e615e355 | 7992 | memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); |
19238e75 | 7993 | } |
e615e355 DE |
7994 | |
7995 | memcpy(&run->internal.data[info_start], info, sizeof(info)); | |
7996 | memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, | |
7997 | ndata * sizeof(data[0])); | |
7998 | ||
7999 | run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; | |
19238e75 AL |
8000 | } |
8001 | ||
e615e355 DE |
8002 | static void prepare_emulation_ctxt_failure_exit(struct kvm_vcpu *vcpu) |
8003 | { | |
8004 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; | |
8005 | ||
8006 | prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, | |
8007 | ctxt->fetch.end - ctxt->fetch.data); | |
8008 | } | |
8009 | ||
8010 | void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, u64 *data, | |
8011 | u8 ndata) | |
8012 | { | |
8013 | prepare_emulation_failure_exit(vcpu, data, ndata, NULL, 0); | |
19238e75 | 8014 | } |
e615e355 DE |
8015 | EXPORT_SYMBOL_GPL(__kvm_prepare_emulation_failure_exit); |
8016 | ||
8017 | void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu) | |
8018 | { | |
8019 | __kvm_prepare_emulation_failure_exit(vcpu, NULL, 0); | |
8020 | } | |
8021 | EXPORT_SYMBOL_GPL(kvm_prepare_emulation_failure_exit); | |
19238e75 | 8022 | |
e2366171 | 8023 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
6d77dbfc | 8024 | { |
19238e75 AL |
8025 | struct kvm *kvm = vcpu->kvm; |
8026 | ||
6d77dbfc GN |
8027 | ++vcpu->stat.insn_emulation_fail; |
8028 | trace_kvm_emulate_insn_failed(vcpu); | |
e2366171 | 8029 | |
42cbf068 SC |
8030 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
8031 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 8032 | return 1; |
42cbf068 | 8033 | } |
e2366171 | 8034 | |
19238e75 AL |
8035 | if (kvm->arch.exit_on_emulation_error || |
8036 | (emulation_type & EMULTYPE_SKIP)) { | |
e615e355 | 8037 | prepare_emulation_ctxt_failure_exit(vcpu); |
60fc3d02 | 8038 | return 0; |
738fece4 SC |
8039 | } |
8040 | ||
22da61c9 SC |
8041 | kvm_queue_exception(vcpu, UD_VECTOR); |
8042 | ||
b3646477 | 8043 | if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) { |
e615e355 | 8044 | prepare_emulation_ctxt_failure_exit(vcpu); |
60fc3d02 | 8045 | return 0; |
fc3a9157 | 8046 | } |
e2366171 | 8047 | |
60fc3d02 | 8048 | return 1; |
6d77dbfc GN |
8049 | } |
8050 | ||
736c291c | 8051 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
991eebf9 GN |
8052 | bool write_fault_to_shadow_pgtable, |
8053 | int emulation_type) | |
a6f177ef | 8054 | { |
736c291c | 8055 | gpa_t gpa = cr2_or_gpa; |
ba049e93 | 8056 | kvm_pfn_t pfn; |
a6f177ef | 8057 | |
92daa48b | 8058 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
991eebf9 GN |
8059 | return false; |
8060 | ||
92daa48b SC |
8061 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
8062 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
8063 | return false; |
8064 | ||
44dd3ffa | 8065 | if (!vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
8066 | /* |
8067 | * Write permission should be allowed since only | |
8068 | * write access need to be emulated. | |
8069 | */ | |
736c291c | 8070 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
a6f177ef | 8071 | |
95b3cf69 XG |
8072 | /* |
8073 | * If the mapping is invalid in guest, let cpu retry | |
8074 | * it to generate fault. | |
8075 | */ | |
8076 | if (gpa == UNMAPPED_GVA) | |
8077 | return true; | |
8078 | } | |
a6f177ef | 8079 | |
8e3d9d06 XG |
8080 | /* |
8081 | * Do not retry the unhandleable instruction if it faults on the | |
8082 | * readonly host memory, otherwise it will goto a infinite loop: | |
8083 | * retry instruction -> write #PF -> emulation fail -> retry | |
8084 | * instruction -> ... | |
8085 | */ | |
8086 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); | |
95b3cf69 XG |
8087 | |
8088 | /* | |
8089 | * If the instruction failed on the error pfn, it can not be fixed, | |
8090 | * report the error to userspace. | |
8091 | */ | |
8092 | if (is_error_noslot_pfn(pfn)) | |
8093 | return false; | |
8094 | ||
8095 | kvm_release_pfn_clean(pfn); | |
8096 | ||
8097 | /* The instructions are well-emulated on direct mmu. */ | |
44dd3ffa | 8098 | if (vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
8099 | unsigned int indirect_shadow_pages; |
8100 | ||
531810ca | 8101 | write_lock(&vcpu->kvm->mmu_lock); |
95b3cf69 | 8102 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; |
531810ca | 8103 | write_unlock(&vcpu->kvm->mmu_lock); |
95b3cf69 XG |
8104 | |
8105 | if (indirect_shadow_pages) | |
8106 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
8107 | ||
a6f177ef | 8108 | return true; |
8e3d9d06 | 8109 | } |
a6f177ef | 8110 | |
95b3cf69 XG |
8111 | /* |
8112 | * if emulation was due to access to shadowed page table | |
8113 | * and it failed try to unshadow page and re-enter the | |
8114 | * guest to let CPU execute the instruction. | |
8115 | */ | |
8116 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
93c05d3e XG |
8117 | |
8118 | /* | |
8119 | * If the access faults on its page table, it can not | |
8120 | * be fixed by unprotecting shadow page and it should | |
8121 | * be reported to userspace. | |
8122 | */ | |
8123 | return !write_fault_to_shadow_pgtable; | |
a6f177ef GN |
8124 | } |
8125 | ||
1cb3f3ae | 8126 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
736c291c | 8127 | gpa_t cr2_or_gpa, int emulation_type) |
1cb3f3ae XG |
8128 | { |
8129 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
736c291c | 8130 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
1cb3f3ae XG |
8131 | |
8132 | last_retry_eip = vcpu->arch.last_retry_eip; | |
8133 | last_retry_addr = vcpu->arch.last_retry_addr; | |
8134 | ||
8135 | /* | |
8136 | * If the emulation is caused by #PF and it is non-page_table | |
8137 | * writing instruction, it means the VM-EXIT is caused by shadow | |
8138 | * page protected, we can zap the shadow page and retry this | |
8139 | * instruction directly. | |
8140 | * | |
8141 | * Note: if the guest uses a non-page-table modifying instruction | |
8142 | * on the PDE that points to the instruction, then we will unmap | |
8143 | * the instruction and go to an infinite loop. So, we cache the | |
8144 | * last retried eip and the last fault address, if we meet the eip | |
8145 | * and the address again, we can break out of the potential infinite | |
8146 | * loop. | |
8147 | */ | |
8148 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; | |
8149 | ||
92daa48b | 8150 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
1cb3f3ae XG |
8151 | return false; |
8152 | ||
92daa48b SC |
8153 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
8154 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
8155 | return false; |
8156 | ||
1cb3f3ae XG |
8157 | if (x86_page_table_writing_insn(ctxt)) |
8158 | return false; | |
8159 | ||
736c291c | 8160 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
1cb3f3ae XG |
8161 | return false; |
8162 | ||
8163 | vcpu->arch.last_retry_eip = ctxt->eip; | |
736c291c | 8164 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
1cb3f3ae | 8165 | |
44dd3ffa | 8166 | if (!vcpu->arch.mmu->direct_map) |
736c291c | 8167 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
1cb3f3ae | 8168 | |
22368028 | 8169 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
1cb3f3ae XG |
8170 | |
8171 | return true; | |
8172 | } | |
8173 | ||
716d51ab GN |
8174 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
8175 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); | |
8176 | ||
dc87275f | 8177 | static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm) |
a584539b | 8178 | { |
1270e647 | 8179 | trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm); |
0d7ee6f4 | 8180 | |
dc87275f SC |
8181 | if (entering_smm) { |
8182 | vcpu->arch.hflags |= HF_SMM_MASK; | |
8183 | } else { | |
8184 | vcpu->arch.hflags &= ~(HF_SMM_MASK | HF_SMM_INSIDE_NMI_MASK); | |
8185 | ||
c43203ca PB |
8186 | /* Process a latched INIT or SMI, if any. */ |
8187 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
37687c40 ML |
8188 | |
8189 | /* | |
8190 | * Even if KVM_SET_SREGS2 loaded PDPTRs out of band, | |
8191 | * on SMM exit we still need to reload them from | |
8192 | * guest memory | |
8193 | */ | |
8194 | vcpu->arch.pdptrs_from_userspace = false; | |
64d60670 | 8195 | } |
699023e2 PB |
8196 | |
8197 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
8198 | } |
8199 | ||
4a1e10d5 PB |
8200 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
8201 | unsigned long *db) | |
8202 | { | |
8203 | u32 dr6 = 0; | |
8204 | int i; | |
8205 | u32 enable, rwlen; | |
8206 | ||
8207 | enable = dr7; | |
8208 | rwlen = dr7 >> 16; | |
8209 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) | |
8210 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) | |
8211 | dr6 |= (1 << i); | |
8212 | return dr6; | |
8213 | } | |
8214 | ||
120c2c4f | 8215 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
663f4c61 PB |
8216 | { |
8217 | struct kvm_run *kvm_run = vcpu->run; | |
8218 | ||
c8401dda | 8219 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
9a3ecd5e | 8220 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; |
d5d260c5 | 8221 | kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
c8401dda PB |
8222 | kvm_run->debug.arch.exception = DB_VECTOR; |
8223 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 8224 | return 0; |
663f4c61 | 8225 | } |
120c2c4f | 8226 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
60fc3d02 | 8227 | return 1; |
663f4c61 PB |
8228 | } |
8229 | ||
6affcbed KH |
8230 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
8231 | { | |
b3646477 | 8232 | unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); |
f8ea7c60 | 8233 | int r; |
6affcbed | 8234 | |
b3646477 | 8235 | r = static_call(kvm_x86_skip_emulated_instruction)(vcpu); |
60fc3d02 | 8236 | if (unlikely(!r)) |
f8ea7c60 | 8237 | return 0; |
c8401dda | 8238 | |
9cd803d4 EH |
8239 | kvm_pmu_trigger_event(vcpu, PERF_COUNT_HW_INSTRUCTIONS); |
8240 | ||
c8401dda PB |
8241 | /* |
8242 | * rflags is the old, "raw" value of the flags. The new value has | |
8243 | * not been saved yet. | |
8244 | * | |
8245 | * This is correct even for TF set by the guest, because "the | |
8246 | * processor will not generate this exception after the instruction | |
8247 | * that sets the TF flag". | |
8248 | */ | |
8249 | if (unlikely(rflags & X86_EFLAGS_TF)) | |
120c2c4f | 8250 | r = kvm_vcpu_do_singlestep(vcpu); |
60fc3d02 | 8251 | return r; |
6affcbed KH |
8252 | } |
8253 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); | |
8254 | ||
4a1e10d5 PB |
8255 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
8256 | { | |
4a1e10d5 PB |
8257 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
8258 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { | |
82b32774 NA |
8259 | struct kvm_run *kvm_run = vcpu->run; |
8260 | unsigned long eip = kvm_get_linear_rip(vcpu); | |
8261 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
8262 | vcpu->arch.guest_debug_dr7, |
8263 | vcpu->arch.eff_db); | |
8264 | ||
8265 | if (dr6 != 0) { | |
9a3ecd5e | 8266 | kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; |
82b32774 | 8267 | kvm_run->debug.arch.pc = eip; |
4a1e10d5 PB |
8268 | kvm_run->debug.arch.exception = DB_VECTOR; |
8269 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 8270 | *r = 0; |
4a1e10d5 PB |
8271 | return true; |
8272 | } | |
8273 | } | |
8274 | ||
4161a569 NA |
8275 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
8276 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { | |
82b32774 NA |
8277 | unsigned long eip = kvm_get_linear_rip(vcpu); |
8278 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
8279 | vcpu->arch.dr7, |
8280 | vcpu->arch.db); | |
8281 | ||
8282 | if (dr6 != 0) { | |
4d5523cf | 8283 | kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); |
60fc3d02 | 8284 | *r = 1; |
4a1e10d5 PB |
8285 | return true; |
8286 | } | |
8287 | } | |
8288 | ||
8289 | return false; | |
8290 | } | |
8291 | ||
04789b66 LA |
8292 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
8293 | { | |
2d7921c4 AM |
8294 | switch (ctxt->opcode_len) { |
8295 | case 1: | |
8296 | switch (ctxt->b) { | |
8297 | case 0xe4: /* IN */ | |
8298 | case 0xe5: | |
8299 | case 0xec: | |
8300 | case 0xed: | |
8301 | case 0xe6: /* OUT */ | |
8302 | case 0xe7: | |
8303 | case 0xee: | |
8304 | case 0xef: | |
8305 | case 0x6c: /* INS */ | |
8306 | case 0x6d: | |
8307 | case 0x6e: /* OUTS */ | |
8308 | case 0x6f: | |
8309 | return true; | |
8310 | } | |
8311 | break; | |
8312 | case 2: | |
8313 | switch (ctxt->b) { | |
8314 | case 0x33: /* RDPMC */ | |
8315 | return true; | |
8316 | } | |
8317 | break; | |
04789b66 LA |
8318 | } |
8319 | ||
8320 | return false; | |
8321 | } | |
8322 | ||
4aa2691d WH |
8323 | /* |
8324 | * Decode to be emulated instruction. Return EMULATION_OK if success. | |
8325 | */ | |
8326 | int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, | |
8327 | void *insn, int insn_len) | |
8328 | { | |
8329 | int r = EMULATION_OK; | |
8330 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; | |
8331 | ||
8332 | init_emulate_ctxt(vcpu); | |
8333 | ||
8334 | /* | |
8335 | * We will reenter on the same instruction since we do not set | |
8336 | * complete_userspace_io. This does not handle watchpoints yet, | |
8337 | * those would be handled in the emulate_ops. | |
8338 | */ | |
8339 | if (!(emulation_type & EMULTYPE_SKIP) && | |
8340 | kvm_vcpu_check_breakpoint(vcpu, &r)) | |
8341 | return r; | |
8342 | ||
b35491e6 | 8343 | r = x86_decode_insn(ctxt, insn, insn_len, emulation_type); |
4aa2691d WH |
8344 | |
8345 | trace_kvm_emulate_insn_start(vcpu); | |
8346 | ++vcpu->stat.insn_emulation; | |
8347 | ||
8348 | return r; | |
8349 | } | |
8350 | EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction); | |
8351 | ||
736c291c SC |
8352 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
8353 | int emulation_type, void *insn, int insn_len) | |
bbd9b64e | 8354 | { |
95cb2295 | 8355 | int r; |
c9b8b07c | 8356 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
7ae441ea | 8357 | bool writeback = true; |
09e3e2a1 SC |
8358 | bool write_fault_to_spt; |
8359 | ||
4d31d9ef | 8360 | if (unlikely(!kvm_can_emulate_insn(vcpu, emulation_type, insn, insn_len))) |
09e3e2a1 | 8361 | return 1; |
bbd9b64e | 8362 | |
c595ceee PB |
8363 | vcpu->arch.l1tf_flush_l1d = true; |
8364 | ||
93c05d3e XG |
8365 | /* |
8366 | * Clear write_fault_to_shadow_pgtable here to ensure it is | |
8367 | * never reused. | |
8368 | */ | |
09e3e2a1 | 8369 | write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
93c05d3e | 8370 | vcpu->arch.write_fault_to_shadow_pgtable = false; |
8d7d8102 | 8371 | |
571008da | 8372 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
4aa2691d | 8373 | kvm_clear_exception_queue(vcpu); |
4a1e10d5 | 8374 | |
4aa2691d WH |
8375 | r = x86_decode_emulated_instruction(vcpu, emulation_type, |
8376 | insn, insn_len); | |
1d2887e2 | 8377 | if (r != EMULATION_OK) { |
b4000606 | 8378 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
c83fad65 SC |
8379 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
8380 | kvm_queue_exception(vcpu, UD_VECTOR); | |
60fc3d02 | 8381 | return 1; |
c83fad65 | 8382 | } |
736c291c SC |
8383 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
8384 | write_fault_to_spt, | |
8385 | emulation_type)) | |
60fc3d02 | 8386 | return 1; |
8530a79c | 8387 | if (ctxt->have_exception) { |
c8848cee JD |
8388 | /* |
8389 | * #UD should result in just EMULATION_FAILED, and trap-like | |
8390 | * exception should not be encountered during decode. | |
8391 | */ | |
8392 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || | |
8393 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); | |
8530a79c | 8394 | inject_emulated_exception(vcpu); |
60fc3d02 | 8395 | return 1; |
8530a79c | 8396 | } |
e2366171 | 8397 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
8398 | } |
8399 | } | |
8400 | ||
42cbf068 SC |
8401 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
8402 | !is_vmware_backdoor_opcode(ctxt)) { | |
8403 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 8404 | return 1; |
42cbf068 | 8405 | } |
04789b66 | 8406 | |
1957aa63 | 8407 | /* |
906fa904 HW |
8408 | * EMULTYPE_SKIP without EMULTYPE_COMPLETE_USER_EXIT is intended for |
8409 | * use *only* by vendor callbacks for kvm_skip_emulated_instruction(). | |
8410 | * The caller is responsible for updating interruptibility state and | |
8411 | * injecting single-step #DBs. | |
1957aa63 | 8412 | */ |
ba8afb6b | 8413 | if (emulation_type & EMULTYPE_SKIP) { |
5e854864 SC |
8414 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
8415 | ctxt->eip = (u32)ctxt->_eip; | |
8416 | else | |
8417 | ctxt->eip = ctxt->_eip; | |
8418 | ||
906fa904 HW |
8419 | if (emulation_type & EMULTYPE_COMPLETE_USER_EXIT) { |
8420 | r = 1; | |
8421 | goto writeback; | |
8422 | } | |
8423 | ||
5e854864 | 8424 | kvm_rip_write(vcpu, ctxt->eip); |
bb663c7a NA |
8425 | if (ctxt->eflags & X86_EFLAGS_RF) |
8426 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); | |
60fc3d02 | 8427 | return 1; |
ba8afb6b GN |
8428 | } |
8429 | ||
736c291c | 8430 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
60fc3d02 | 8431 | return 1; |
1cb3f3ae | 8432 | |
7ae441ea | 8433 | /* this is needed for vmware backdoor interface to work since it |
4d2179e1 | 8434 | changes registers values during IO operation */ |
7ae441ea GN |
8435 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
8436 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; | |
dd856efa | 8437 | emulator_invalidate_register_cache(ctxt); |
7ae441ea | 8438 | } |
4d2179e1 | 8439 | |
5cd21917 | 8440 | restart: |
92daa48b SC |
8441 | if (emulation_type & EMULTYPE_PF) { |
8442 | /* Save the faulting GPA (cr2) in the address field */ | |
8443 | ctxt->exception.address = cr2_or_gpa; | |
8444 | ||
8445 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ | |
8446 | if (vcpu->arch.mmu->direct_map) { | |
744e699c SC |
8447 | ctxt->gpa_available = true; |
8448 | ctxt->gpa_val = cr2_or_gpa; | |
92daa48b SC |
8449 | } |
8450 | } else { | |
8451 | /* Sanitize the address out of an abundance of paranoia. */ | |
8452 | ctxt->exception.address = 0; | |
8453 | } | |
0f89b207 | 8454 | |
9d74191a | 8455 | r = x86_emulate_insn(ctxt); |
bbd9b64e | 8456 | |
775fde86 | 8457 | if (r == EMULATION_INTERCEPTED) |
60fc3d02 | 8458 | return 1; |
775fde86 | 8459 | |
d2ddd1c4 | 8460 | if (r == EMULATION_FAILED) { |
736c291c | 8461 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
991eebf9 | 8462 | emulation_type)) |
60fc3d02 | 8463 | return 1; |
c3cd7ffa | 8464 | |
e2366171 | 8465 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
8466 | } |
8467 | ||
9d74191a | 8468 | if (ctxt->have_exception) { |
60fc3d02 | 8469 | r = 1; |
ef54bcfe PB |
8470 | if (inject_emulated_exception(vcpu)) |
8471 | return r; | |
d2ddd1c4 | 8472 | } else if (vcpu->arch.pio.count) { |
0912c977 PB |
8473 | if (!vcpu->arch.pio.in) { |
8474 | /* FIXME: return into emulator if single-stepping. */ | |
3457e419 | 8475 | vcpu->arch.pio.count = 0; |
0912c977 | 8476 | } else { |
7ae441ea | 8477 | writeback = false; |
716d51ab GN |
8478 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
8479 | } | |
60fc3d02 | 8480 | r = 0; |
7ae441ea | 8481 | } else if (vcpu->mmio_needed) { |
bc8a0aaf SC |
8482 | ++vcpu->stat.mmio_exits; |
8483 | ||
7ae441ea GN |
8484 | if (!vcpu->mmio_is_write) |
8485 | writeback = false; | |
60fc3d02 | 8486 | r = 0; |
716d51ab | 8487 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
adbfb12d HW |
8488 | } else if (vcpu->arch.complete_userspace_io) { |
8489 | writeback = false; | |
8490 | r = 0; | |
7ae441ea | 8491 | } else if (r == EMULATION_RESTART) |
5cd21917 | 8492 | goto restart; |
d2ddd1c4 | 8493 | else |
60fc3d02 | 8494 | r = 1; |
f850e2e6 | 8495 | |
906fa904 | 8496 | writeback: |
7ae441ea | 8497 | if (writeback) { |
b3646477 | 8498 | unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); |
9d74191a | 8499 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
7ae441ea | 8500 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
38827dbd | 8501 | if (!ctxt->have_exception || |
75ee23b3 | 8502 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
9cd803d4 | 8503 | kvm_pmu_trigger_event(vcpu, PERF_COUNT_HW_INSTRUCTIONS); |
018d70ff EH |
8504 | if (ctxt->is_branch) |
8505 | kvm_pmu_trigger_event(vcpu, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); | |
75ee23b3 | 8506 | kvm_rip_write(vcpu, ctxt->eip); |
384dea1c | 8507 | if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) |
120c2c4f | 8508 | r = kvm_vcpu_do_singlestep(vcpu); |
2a890614 | 8509 | static_call_cond(kvm_x86_update_emulated_instruction)(vcpu); |
38827dbd | 8510 | __kvm_set_rflags(vcpu, ctxt->eflags); |
75ee23b3 | 8511 | } |
6addfc42 PB |
8512 | |
8513 | /* | |
8514 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will | |
8515 | * do nothing, and it will be requested again as soon as | |
8516 | * the shadow expires. But we still need to check here, | |
8517 | * because POPF has no interrupt shadow. | |
8518 | */ | |
8519 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) | |
8520 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7ae441ea GN |
8521 | } else |
8522 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; | |
e85d28f8 GN |
8523 | |
8524 | return r; | |
de7d789a | 8525 | } |
c60658d1 SC |
8526 | |
8527 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) | |
8528 | { | |
8529 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); | |
8530 | } | |
8531 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); | |
8532 | ||
8533 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, | |
8534 | void *insn, int insn_len) | |
8535 | { | |
8536 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); | |
8537 | } | |
8538 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); | |
de7d789a | 8539 | |
8764ed55 SC |
8540 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
8541 | { | |
8542 | vcpu->arch.pio.count = 0; | |
8543 | return 1; | |
8544 | } | |
8545 | ||
45def77e SC |
8546 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
8547 | { | |
8548 | vcpu->arch.pio.count = 0; | |
8549 | ||
8550 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) | |
8551 | return 1; | |
8552 | ||
8553 | return kvm_skip_emulated_instruction(vcpu); | |
8554 | } | |
8555 | ||
dca7f128 SC |
8556 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
8557 | unsigned short port) | |
de7d789a | 8558 | { |
de3cd117 | 8559 | unsigned long val = kvm_rax_read(vcpu); |
2e3bb4d8 SC |
8560 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
8561 | ||
8764ed55 SC |
8562 | if (ret) |
8563 | return ret; | |
45def77e | 8564 | |
8764ed55 SC |
8565 | /* |
8566 | * Workaround userspace that relies on old KVM behavior of %rip being | |
8567 | * incremented prior to exiting to userspace to handle "OUT 0x7e". | |
8568 | */ | |
8569 | if (port == 0x7e && | |
8570 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { | |
8571 | vcpu->arch.complete_userspace_io = | |
8572 | complete_fast_pio_out_port_0x7e; | |
8573 | kvm_skip_emulated_instruction(vcpu); | |
8574 | } else { | |
45def77e SC |
8575 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
8576 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; | |
8577 | } | |
8764ed55 | 8578 | return 0; |
de7d789a | 8579 | } |
de7d789a | 8580 | |
8370c3d0 TL |
8581 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
8582 | { | |
8583 | unsigned long val; | |
8584 | ||
8585 | /* We should only ever be called with arch.pio.count equal to 1 */ | |
8586 | BUG_ON(vcpu->arch.pio.count != 1); | |
8587 | ||
45def77e SC |
8588 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
8589 | vcpu->arch.pio.count = 0; | |
8590 | return 1; | |
8591 | } | |
8592 | ||
8370c3d0 | 8593 | /* For size less than 4 we merge, else we zero extend */ |
de3cd117 | 8594 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 TL |
8595 | |
8596 | /* | |
2e3bb4d8 | 8597 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
8370c3d0 TL |
8598 | * the copy and tracing |
8599 | */ | |
2e3bb4d8 | 8600 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
de3cd117 | 8601 | kvm_rax_write(vcpu, val); |
8370c3d0 | 8602 | |
45def77e | 8603 | return kvm_skip_emulated_instruction(vcpu); |
8370c3d0 TL |
8604 | } |
8605 | ||
dca7f128 SC |
8606 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
8607 | unsigned short port) | |
8370c3d0 TL |
8608 | { |
8609 | unsigned long val; | |
8610 | int ret; | |
8611 | ||
8612 | /* For size less than 4 we merge, else we zero extend */ | |
de3cd117 | 8613 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 | 8614 | |
2e3bb4d8 | 8615 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
8370c3d0 | 8616 | if (ret) { |
de3cd117 | 8617 | kvm_rax_write(vcpu, val); |
8370c3d0 TL |
8618 | return ret; |
8619 | } | |
8620 | ||
45def77e | 8621 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
8370c3d0 TL |
8622 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
8623 | ||
8624 | return 0; | |
8625 | } | |
dca7f128 SC |
8626 | |
8627 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) | |
8628 | { | |
45def77e | 8629 | int ret; |
dca7f128 | 8630 | |
dca7f128 | 8631 | if (in) |
45def77e | 8632 | ret = kvm_fast_pio_in(vcpu, size, port); |
dca7f128 | 8633 | else |
45def77e SC |
8634 | ret = kvm_fast_pio_out(vcpu, size, port); |
8635 | return ret && kvm_skip_emulated_instruction(vcpu); | |
dca7f128 SC |
8636 | } |
8637 | EXPORT_SYMBOL_GPL(kvm_fast_pio); | |
8370c3d0 | 8638 | |
251a5fd6 | 8639 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
8cfdc000 | 8640 | { |
0a3aee0d | 8641 | __this_cpu_write(cpu_tsc_khz, 0); |
251a5fd6 | 8642 | return 0; |
8cfdc000 ZA |
8643 | } |
8644 | ||
8645 | static void tsc_khz_changed(void *data) | |
c8076604 | 8646 | { |
8cfdc000 ZA |
8647 | struct cpufreq_freqs *freq = data; |
8648 | unsigned long khz = 0; | |
8649 | ||
8650 | if (data) | |
8651 | khz = freq->new; | |
8652 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) | |
8653 | khz = cpufreq_quick_get(raw_smp_processor_id()); | |
8654 | if (!khz) | |
8655 | khz = tsc_khz; | |
0a3aee0d | 8656 | __this_cpu_write(cpu_tsc_khz, khz); |
c8076604 GH |
8657 | } |
8658 | ||
5fa4ec9c | 8659 | #ifdef CONFIG_X86_64 |
0092e434 VK |
8660 | static void kvm_hyperv_tsc_notifier(void) |
8661 | { | |
0092e434 | 8662 | struct kvm *kvm; |
0092e434 VK |
8663 | int cpu; |
8664 | ||
0d9ce162 | 8665 | mutex_lock(&kvm_lock); |
0092e434 VK |
8666 | list_for_each_entry(kvm, &vm_list, vm_list) |
8667 | kvm_make_mclock_inprogress_request(kvm); | |
8668 | ||
6b6fcd28 | 8669 | /* no guest entries from this point */ |
0092e434 VK |
8670 | hyperv_stop_tsc_emulation(); |
8671 | ||
8672 | /* TSC frequency always matches when on Hyper-V */ | |
8673 | for_each_present_cpu(cpu) | |
8674 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | |
8675 | kvm_max_guest_tsc_khz = tsc_khz; | |
8676 | ||
8677 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
869b4421 | 8678 | __kvm_start_pvclock_update(kvm); |
0092e434 | 8679 | pvclock_update_vm_gtod_copy(kvm); |
6b6fcd28 | 8680 | kvm_end_pvclock_update(kvm); |
0092e434 | 8681 | } |
6b6fcd28 | 8682 | |
0d9ce162 | 8683 | mutex_unlock(&kvm_lock); |
0092e434 | 8684 | } |
5fa4ec9c | 8685 | #endif |
0092e434 | 8686 | |
df24014a | 8687 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
c8076604 | 8688 | { |
c8076604 GH |
8689 | struct kvm *kvm; |
8690 | struct kvm_vcpu *vcpu; | |
46808a4c MZ |
8691 | int send_ipi = 0; |
8692 | unsigned long i; | |
c8076604 | 8693 | |
8cfdc000 ZA |
8694 | /* |
8695 | * We allow guests to temporarily run on slowing clocks, | |
8696 | * provided we notify them after, or to run on accelerating | |
8697 | * clocks, provided we notify them before. Thus time never | |
8698 | * goes backwards. | |
8699 | * | |
8700 | * However, we have a problem. We can't atomically update | |
8701 | * the frequency of a given CPU from this function; it is | |
8702 | * merely a notifier, which can be called from any CPU. | |
8703 | * Changing the TSC frequency at arbitrary points in time | |
8704 | * requires a recomputation of local variables related to | |
8705 | * the TSC for each VCPU. We must flag these local variables | |
8706 | * to be updated and be sure the update takes place with the | |
8707 | * new frequency before any guests proceed. | |
8708 | * | |
8709 | * Unfortunately, the combination of hotplug CPU and frequency | |
8710 | * change creates an intractable locking scenario; the order | |
8711 | * of when these callouts happen is undefined with respect to | |
8712 | * CPU hotplug, and they can race with each other. As such, | |
8713 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is | |
8714 | * undefined; you can actually have a CPU frequency change take | |
8715 | * place in between the computation of X and the setting of the | |
8716 | * variable. To protect against this problem, all updates of | |
8717 | * the per_cpu tsc_khz variable are done in an interrupt | |
8718 | * protected IPI, and all callers wishing to update the value | |
8719 | * must wait for a synchronous IPI to complete (which is trivial | |
8720 | * if the caller is on the CPU already). This establishes the | |
8721 | * necessary total order on variable updates. | |
8722 | * | |
8723 | * Note that because a guest time update may take place | |
8724 | * anytime after the setting of the VCPU's request bit, the | |
8725 | * correct TSC value must be set before the request. However, | |
8726 | * to ensure the update actually makes it to any guest which | |
8727 | * starts running in hardware virtualization between the set | |
8728 | * and the acquisition of the spinlock, we must also ping the | |
8729 | * CPU after setting the request bit. | |
8730 | * | |
8731 | */ | |
8732 | ||
df24014a | 8733 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 8734 | |
0d9ce162 | 8735 | mutex_lock(&kvm_lock); |
c8076604 | 8736 | list_for_each_entry(kvm, &vm_list, vm_list) { |
988a2cae | 8737 | kvm_for_each_vcpu(i, vcpu, kvm) { |
df24014a | 8738 | if (vcpu->cpu != cpu) |
c8076604 | 8739 | continue; |
c285545f | 8740 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0d9ce162 | 8741 | if (vcpu->cpu != raw_smp_processor_id()) |
8cfdc000 | 8742 | send_ipi = 1; |
c8076604 GH |
8743 | } |
8744 | } | |
0d9ce162 | 8745 | mutex_unlock(&kvm_lock); |
c8076604 GH |
8746 | |
8747 | if (freq->old < freq->new && send_ipi) { | |
8748 | /* | |
8749 | * We upscale the frequency. Must make the guest | |
8750 | * doesn't see old kvmclock values while running with | |
8751 | * the new frequency, otherwise we risk the guest sees | |
8752 | * time go backwards. | |
8753 | * | |
8754 | * In case we update the frequency for another cpu | |
8755 | * (which might be in guest context) send an interrupt | |
8756 | * to kick the cpu out of guest context. Next time | |
8757 | * guest context is entered kvmclock will be updated, | |
8758 | * so the guest will not see stale values. | |
8759 | */ | |
df24014a | 8760 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 8761 | } |
df24014a VK |
8762 | } |
8763 | ||
8764 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
8765 | void *data) | |
8766 | { | |
8767 | struct cpufreq_freqs *freq = data; | |
8768 | int cpu; | |
8769 | ||
8770 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | |
8771 | return 0; | |
8772 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | |
8773 | return 0; | |
8774 | ||
8775 | for_each_cpu(cpu, freq->policy->cpus) | |
8776 | __kvmclock_cpufreq_notifier(freq, cpu); | |
8777 | ||
c8076604 GH |
8778 | return 0; |
8779 | } | |
8780 | ||
8781 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | |
8cfdc000 ZA |
8782 | .notifier_call = kvmclock_cpufreq_notifier |
8783 | }; | |
8784 | ||
251a5fd6 | 8785 | static int kvmclock_cpu_online(unsigned int cpu) |
8cfdc000 | 8786 | { |
251a5fd6 SAS |
8787 | tsc_khz_changed(NULL); |
8788 | return 0; | |
8cfdc000 ZA |
8789 | } |
8790 | ||
b820cc0c ZA |
8791 | static void kvm_timer_init(void) |
8792 | { | |
c285545f | 8793 | max_tsc_khz = tsc_khz; |
460dd42e | 8794 | |
b820cc0c | 8795 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
c285545f | 8796 | #ifdef CONFIG_CPU_FREQ |
aaec7c03 | 8797 | struct cpufreq_policy *policy; |
758f588d BP |
8798 | int cpu; |
8799 | ||
3e26f230 | 8800 | cpu = get_cpu(); |
aaec7c03 | 8801 | policy = cpufreq_cpu_get(cpu); |
9a11997e WL |
8802 | if (policy) { |
8803 | if (policy->cpuinfo.max_freq) | |
8804 | max_tsc_khz = policy->cpuinfo.max_freq; | |
8805 | cpufreq_cpu_put(policy); | |
8806 | } | |
3e26f230 | 8807 | put_cpu(); |
c285545f | 8808 | #endif |
b820cc0c ZA |
8809 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
8810 | CPUFREQ_TRANSITION_NOTIFIER); | |
8811 | } | |
460dd42e | 8812 | |
73c1b41e | 8813 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
251a5fd6 | 8814 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
b820cc0c ZA |
8815 | } |
8816 | ||
16e8d74d MT |
8817 | #ifdef CONFIG_X86_64 |
8818 | static void pvclock_gtod_update_fn(struct work_struct *work) | |
8819 | { | |
d828199e | 8820 | struct kvm *kvm; |
d828199e | 8821 | struct kvm_vcpu *vcpu; |
46808a4c | 8822 | unsigned long i; |
d828199e | 8823 | |
0d9ce162 | 8824 | mutex_lock(&kvm_lock); |
d828199e MT |
8825 | list_for_each_entry(kvm, &vm_list, vm_list) |
8826 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 8827 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
d828199e | 8828 | atomic_set(&kvm_guest_has_master_clock, 0); |
0d9ce162 | 8829 | mutex_unlock(&kvm_lock); |
16e8d74d MT |
8830 | } |
8831 | ||
8832 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); | |
8833 | ||
3f804f6d TG |
8834 | /* |
8835 | * Indirection to move queue_work() out of the tk_core.seq write held | |
8836 | * region to prevent possible deadlocks against time accessors which | |
8837 | * are invoked with work related locks held. | |
8838 | */ | |
8839 | static void pvclock_irq_work_fn(struct irq_work *w) | |
8840 | { | |
8841 | queue_work(system_long_wq, &pvclock_gtod_work); | |
8842 | } | |
8843 | ||
8844 | static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn); | |
8845 | ||
16e8d74d MT |
8846 | /* |
8847 | * Notification about pvclock gtod data update. | |
8848 | */ | |
8849 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, | |
8850 | void *priv) | |
8851 | { | |
8852 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
8853 | struct timekeeper *tk = priv; | |
8854 | ||
8855 | update_pvclock_gtod(tk); | |
8856 | ||
3f804f6d TG |
8857 | /* |
8858 | * Disable master clock if host does not trust, or does not use, | |
8859 | * TSC based clocksource. Delegate queue_work() to irq_work as | |
8860 | * this is invoked with tk_core.seq write held. | |
16e8d74d | 8861 | */ |
b0c39dc6 | 8862 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
16e8d74d | 8863 | atomic_read(&kvm_guest_has_master_clock) != 0) |
3f804f6d | 8864 | irq_work_queue(&pvclock_irq_work); |
16e8d74d MT |
8865 | return 0; |
8866 | } | |
8867 | ||
8868 | static struct notifier_block pvclock_gtod_notifier = { | |
8869 | .notifier_call = pvclock_gtod_notify, | |
8870 | }; | |
8871 | #endif | |
8872 | ||
f8c16bba | 8873 | int kvm_arch_init(void *opaque) |
043405e1 | 8874 | { |
d008dfdb | 8875 | struct kvm_x86_init_ops *ops = opaque; |
b820cc0c | 8876 | int r; |
f8c16bba | 8877 | |
afaf0b2f | 8878 | if (kvm_x86_ops.hardware_enable) { |
9dadfc4a | 8879 | pr_err("kvm: already loaded vendor module '%s'\n", kvm_x86_ops.name); |
56c6d28a ZX |
8880 | r = -EEXIST; |
8881 | goto out; | |
f8c16bba ZX |
8882 | } |
8883 | ||
8884 | if (!ops->cpu_has_kvm_support()) { | |
9dadfc4a SC |
8885 | pr_err_ratelimited("kvm: no hardware support for '%s'\n", |
8886 | ops->runtime_ops->name); | |
56c6d28a ZX |
8887 | r = -EOPNOTSUPP; |
8888 | goto out; | |
f8c16bba ZX |
8889 | } |
8890 | if (ops->disabled_by_bios()) { | |
9dadfc4a SC |
8891 | pr_err_ratelimited("kvm: support for '%s' disabled by bios\n", |
8892 | ops->runtime_ops->name); | |
56c6d28a ZX |
8893 | r = -EOPNOTSUPP; |
8894 | goto out; | |
f8c16bba ZX |
8895 | } |
8896 | ||
b666a4b6 MO |
8897 | /* |
8898 | * KVM explicitly assumes that the guest has an FPU and | |
8899 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the | |
8900 | * vCPU's FPU state as a fxregs_state struct. | |
8901 | */ | |
8902 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { | |
8903 | printk(KERN_ERR "kvm: inadequate fpu\n"); | |
8904 | r = -EOPNOTSUPP; | |
5e17b2ee TG |
8905 | goto out; |
8906 | } | |
8907 | ||
8908 | if (IS_ENABLED(CONFIG_PREEMPT_RT) && !boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { | |
8909 | pr_err("RT requires X86_FEATURE_CONSTANT_TSC\n"); | |
8910 | r = -EOPNOTSUPP; | |
b666a4b6 MO |
8911 | goto out; |
8912 | } | |
8913 | ||
013f6a5d | 8914 | r = -ENOMEM; |
b666a4b6 | 8915 | |
c9b8b07c SC |
8916 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
8917 | if (!x86_emulator_cache) { | |
8918 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); | |
d69c1382 | 8919 | goto out; |
c9b8b07c SC |
8920 | } |
8921 | ||
7e34fbd0 SC |
8922 | user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); |
8923 | if (!user_return_msrs) { | |
8924 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); | |
c9b8b07c | 8925 | goto out_free_x86_emulator_cache; |
013f6a5d | 8926 | } |
e5fda4bb | 8927 | kvm_nr_uret_msrs = 0; |
013f6a5d | 8928 | |
97db56ce AK |
8929 | r = kvm_mmu_module_init(); |
8930 | if (r) | |
013f6a5d | 8931 | goto out_free_percpu; |
97db56ce | 8932 | |
b820cc0c | 8933 | kvm_timer_init(); |
c8076604 | 8934 | |
cfc48181 | 8935 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
2acf923e | 8936 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
cfc48181 SC |
8937 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
8938 | } | |
2acf923e | 8939 | |
0c5f81da WL |
8940 | if (pi_inject_timer == -1) |
8941 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); | |
16e8d74d MT |
8942 | #ifdef CONFIG_X86_64 |
8943 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); | |
0092e434 | 8944 | |
5fa4ec9c | 8945 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 | 8946 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
16e8d74d MT |
8947 | #endif |
8948 | ||
f8c16bba | 8949 | return 0; |
56c6d28a | 8950 | |
013f6a5d | 8951 | out_free_percpu: |
7e34fbd0 | 8952 | free_percpu(user_return_msrs); |
c9b8b07c SC |
8953 | out_free_x86_emulator_cache: |
8954 | kmem_cache_destroy(x86_emulator_cache); | |
56c6d28a | 8955 | out: |
56c6d28a | 8956 | return r; |
043405e1 | 8957 | } |
8776e519 | 8958 | |
f8c16bba ZX |
8959 | void kvm_arch_exit(void) |
8960 | { | |
0092e434 | 8961 | #ifdef CONFIG_X86_64 |
5fa4ec9c | 8962 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 VK |
8963 | clear_hv_tscchange_cb(); |
8964 | #endif | |
cef84c30 | 8965 | kvm_lapic_exit(); |
ff9d07a0 | 8966 | |
888d256e JK |
8967 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
8968 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, | |
8969 | CPUFREQ_TRANSITION_NOTIFIER); | |
251a5fd6 | 8970 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
16e8d74d MT |
8971 | #ifdef CONFIG_X86_64 |
8972 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); | |
3f804f6d | 8973 | irq_work_sync(&pvclock_irq_work); |
594b27e6 | 8974 | cancel_work_sync(&pvclock_gtod_work); |
16e8d74d | 8975 | #endif |
afaf0b2f | 8976 | kvm_x86_ops.hardware_enable = NULL; |
56c6d28a | 8977 | kvm_mmu_module_exit(); |
7e34fbd0 | 8978 | free_percpu(user_return_msrs); |
dfdc0a71 | 8979 | kmem_cache_destroy(x86_emulator_cache); |
b59b153d | 8980 | #ifdef CONFIG_KVM_XEN |
c462f859 | 8981 | static_key_deferred_flush(&kvm_xen_enabled); |
7d6bbebb | 8982 | WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key)); |
b59b153d | 8983 | #endif |
56c6d28a | 8984 | } |
f8c16bba | 8985 | |
1460179d | 8986 | static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason) |
8776e519 | 8987 | { |
91b99ea7 SC |
8988 | /* |
8989 | * The vCPU has halted, e.g. executed HLT. Update the run state if the | |
8990 | * local APIC is in-kernel, the run loop will detect the non-runnable | |
8991 | * state and halt the vCPU. Exit to userspace if the local APIC is | |
8992 | * managed by userspace, in which case userspace is responsible for | |
8993 | * handling wake events. | |
8994 | */ | |
8776e519 | 8995 | ++vcpu->stat.halt_exits; |
35754c98 | 8996 | if (lapic_in_kernel(vcpu)) { |
647daca2 | 8997 | vcpu->arch.mp_state = state; |
8776e519 HB |
8998 | return 1; |
8999 | } else { | |
647daca2 | 9000 | vcpu->run->exit_reason = reason; |
8776e519 HB |
9001 | return 0; |
9002 | } | |
9003 | } | |
647daca2 | 9004 | |
1460179d | 9005 | int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu) |
647daca2 | 9006 | { |
1460179d | 9007 | return __kvm_emulate_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT); |
647daca2 | 9008 | } |
1460179d | 9009 | EXPORT_SYMBOL_GPL(kvm_emulate_halt_noskip); |
5cb56059 JS |
9010 | |
9011 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
9012 | { | |
6affcbed KH |
9013 | int ret = kvm_skip_emulated_instruction(vcpu); |
9014 | /* | |
9015 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered | |
9016 | * KVM_EXIT_DEBUG here. | |
9017 | */ | |
1460179d | 9018 | return kvm_emulate_halt_noskip(vcpu) && ret; |
5cb56059 | 9019 | } |
8776e519 HB |
9020 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
9021 | ||
647daca2 TL |
9022 | int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu) |
9023 | { | |
9024 | int ret = kvm_skip_emulated_instruction(vcpu); | |
9025 | ||
1460179d SC |
9026 | return __kvm_emulate_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, |
9027 | KVM_EXIT_AP_RESET_HOLD) && ret; | |
647daca2 TL |
9028 | } |
9029 | EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold); | |
9030 | ||
8ef81a9a | 9031 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
9032 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
9033 | unsigned long clock_type) | |
9034 | { | |
9035 | struct kvm_clock_pairing clock_pairing; | |
899a31f5 | 9036 | struct timespec64 ts; |
80fbd89c | 9037 | u64 cycle; |
55dd00a7 MT |
9038 | int ret; |
9039 | ||
9040 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) | |
9041 | return -KVM_EOPNOTSUPP; | |
9042 | ||
3a55f729 AR |
9043 | /* |
9044 | * When tsc is in permanent catchup mode guests won't be able to use | |
9045 | * pvclock_read_retry loop to get consistent view of pvclock | |
9046 | */ | |
9047 | if (vcpu->arch.tsc_always_catchup) | |
9048 | return -KVM_EOPNOTSUPP; | |
9049 | ||
7ca7f3b9 | 9050 | if (!kvm_get_walltime_and_clockread(&ts, &cycle)) |
55dd00a7 MT |
9051 | return -KVM_EOPNOTSUPP; |
9052 | ||
9053 | clock_pairing.sec = ts.tv_sec; | |
9054 | clock_pairing.nsec = ts.tv_nsec; | |
9055 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); | |
9056 | clock_pairing.flags = 0; | |
bcbfbd8e | 9057 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
55dd00a7 MT |
9058 | |
9059 | ret = 0; | |
9060 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, | |
9061 | sizeof(struct kvm_clock_pairing))) | |
9062 | ret = -KVM_EFAULT; | |
9063 | ||
9064 | return ret; | |
9065 | } | |
8ef81a9a | 9066 | #endif |
55dd00a7 | 9067 | |
6aef266c SV |
9068 | /* |
9069 | * kvm_pv_kick_cpu_op: Kick a vcpu. | |
9070 | * | |
9071 | * @apicid - apicid of vcpu to be kicked. | |
9072 | */ | |
9d68c6f6 | 9073 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid) |
6aef266c | 9074 | { |
24d2166b | 9075 | struct kvm_lapic_irq lapic_irq; |
6aef266c | 9076 | |
150a84fe | 9077 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
c96001c5 | 9078 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
ebd28fcb | 9079 | lapic_irq.level = 0; |
24d2166b | 9080 | lapic_irq.dest_id = apicid; |
93bbf0b8 | 9081 | lapic_irq.msi_redir_hint = false; |
6aef266c | 9082 | |
24d2166b | 9083 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
795a149e | 9084 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
6aef266c SV |
9085 | } |
9086 | ||
4e19c36f SS |
9087 | bool kvm_apicv_activated(struct kvm *kvm) |
9088 | { | |
9089 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); | |
9090 | } | |
9091 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); | |
9092 | ||
4f4c4a3e SC |
9093 | |
9094 | static void set_or_clear_apicv_inhibit(unsigned long *inhibits, | |
9095 | enum kvm_apicv_inhibit reason, bool set) | |
9096 | { | |
9097 | if (set) | |
9098 | __set_bit(reason, inhibits); | |
9099 | else | |
9100 | __clear_bit(reason, inhibits); | |
9101 | ||
9102 | trace_kvm_apicv_inhibit_changed(reason, set, *inhibits); | |
9103 | } | |
9104 | ||
4651fc56 | 9105 | static void kvm_apicv_init(struct kvm *kvm) |
4e19c36f | 9106 | { |
4f4c4a3e SC |
9107 | unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons; |
9108 | ||
187c8833 | 9109 | init_rwsem(&kvm->arch.apicv_update_lock); |
b0a1637f | 9110 | |
4f4c4a3e SC |
9111 | set_or_clear_apicv_inhibit(inhibits, APICV_INHIBIT_REASON_ABSENT, true); |
9112 | ||
ef8b4b72 | 9113 | if (!enable_apicv) |
4f4c4a3e SC |
9114 | set_or_clear_apicv_inhibit(inhibits, |
9115 | APICV_INHIBIT_REASON_ABSENT, true); | |
4e19c36f | 9116 | } |
4e19c36f | 9117 | |
4a7132ef | 9118 | static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) |
71506297 WL |
9119 | { |
9120 | struct kvm_vcpu *target = NULL; | |
9121 | struct kvm_apic_map *map; | |
9122 | ||
4a7132ef WL |
9123 | vcpu->stat.directed_yield_attempted++; |
9124 | ||
72b268a8 WL |
9125 | if (single_task_running()) |
9126 | goto no_yield; | |
9127 | ||
71506297 | 9128 | rcu_read_lock(); |
4a7132ef | 9129 | map = rcu_dereference(vcpu->kvm->arch.apic_map); |
71506297 WL |
9130 | |
9131 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) | |
9132 | target = map->phys_map[dest_id]->vcpu; | |
9133 | ||
9134 | rcu_read_unlock(); | |
9135 | ||
4a7132ef WL |
9136 | if (!target || !READ_ONCE(target->ready)) |
9137 | goto no_yield; | |
9138 | ||
a1fa4cbd WL |
9139 | /* Ignore requests to yield to self */ |
9140 | if (vcpu == target) | |
9141 | goto no_yield; | |
9142 | ||
4a7132ef WL |
9143 | if (kvm_vcpu_yield_to(target) <= 0) |
9144 | goto no_yield; | |
9145 | ||
9146 | vcpu->stat.directed_yield_successful++; | |
9147 | ||
9148 | no_yield: | |
9149 | return; | |
71506297 WL |
9150 | } |
9151 | ||
0dbb1123 AK |
9152 | static int complete_hypercall_exit(struct kvm_vcpu *vcpu) |
9153 | { | |
9154 | u64 ret = vcpu->run->hypercall.ret; | |
9155 | ||
9156 | if (!is_64_bit_mode(vcpu)) | |
9157 | ret = (u32)ret; | |
9158 | kvm_rax_write(vcpu, ret); | |
9159 | ++vcpu->stat.hypercalls; | |
9160 | return kvm_skip_emulated_instruction(vcpu); | |
9161 | } | |
9162 | ||
8776e519 HB |
9163 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
9164 | { | |
9165 | unsigned long nr, a0, a1, a2, a3, ret; | |
6356ee0c | 9166 | int op_64_bit; |
8776e519 | 9167 | |
23200b7a JM |
9168 | if (kvm_xen_hypercall_enabled(vcpu->kvm)) |
9169 | return kvm_xen_hypercall(vcpu); | |
9170 | ||
8f014550 | 9171 | if (kvm_hv_hypercall_enabled(vcpu)) |
696ca779 | 9172 | return kvm_hv_hypercall(vcpu); |
55cd8e5a | 9173 | |
de3cd117 SC |
9174 | nr = kvm_rax_read(vcpu); |
9175 | a0 = kvm_rbx_read(vcpu); | |
9176 | a1 = kvm_rcx_read(vcpu); | |
9177 | a2 = kvm_rdx_read(vcpu); | |
9178 | a3 = kvm_rsi_read(vcpu); | |
8776e519 | 9179 | |
229456fc | 9180 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
2714d1d3 | 9181 | |
b5aead00 | 9182 | op_64_bit = is_64_bit_hypercall(vcpu); |
a449c7aa | 9183 | if (!op_64_bit) { |
8776e519 HB |
9184 | nr &= 0xFFFFFFFF; |
9185 | a0 &= 0xFFFFFFFF; | |
9186 | a1 &= 0xFFFFFFFF; | |
9187 | a2 &= 0xFFFFFFFF; | |
9188 | a3 &= 0xFFFFFFFF; | |
9189 | } | |
9190 | ||
b3646477 | 9191 | if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { |
07708c4a | 9192 | ret = -KVM_EPERM; |
696ca779 | 9193 | goto out; |
07708c4a JK |
9194 | } |
9195 | ||
66570e96 OU |
9196 | ret = -KVM_ENOSYS; |
9197 | ||
8776e519 | 9198 | switch (nr) { |
b93463aa AK |
9199 | case KVM_HC_VAPIC_POLL_IRQ: |
9200 | ret = 0; | |
9201 | break; | |
6aef266c | 9202 | case KVM_HC_KICK_CPU: |
66570e96 OU |
9203 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT)) |
9204 | break; | |
9205 | ||
9d68c6f6 | 9206 | kvm_pv_kick_cpu_op(vcpu->kvm, a1); |
4a7132ef | 9207 | kvm_sched_yield(vcpu, a1); |
6aef266c SV |
9208 | ret = 0; |
9209 | break; | |
8ef81a9a | 9210 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
9211 | case KVM_HC_CLOCK_PAIRING: |
9212 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); | |
9213 | break; | |
1ed199a4 | 9214 | #endif |
4180bf1b | 9215 | case KVM_HC_SEND_IPI: |
66570e96 OU |
9216 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI)) |
9217 | break; | |
9218 | ||
4180bf1b WL |
9219 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); |
9220 | break; | |
71506297 | 9221 | case KVM_HC_SCHED_YIELD: |
66570e96 OU |
9222 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD)) |
9223 | break; | |
9224 | ||
4a7132ef | 9225 | kvm_sched_yield(vcpu, a0); |
71506297 WL |
9226 | ret = 0; |
9227 | break; | |
0dbb1123 AK |
9228 | case KVM_HC_MAP_GPA_RANGE: { |
9229 | u64 gpa = a0, npages = a1, attrs = a2; | |
9230 | ||
9231 | ret = -KVM_ENOSYS; | |
9232 | if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) | |
9233 | break; | |
9234 | ||
9235 | if (!PAGE_ALIGNED(gpa) || !npages || | |
9236 | gpa_to_gfn(gpa) + npages <= gpa_to_gfn(gpa)) { | |
9237 | ret = -KVM_EINVAL; | |
9238 | break; | |
9239 | } | |
9240 | ||
9241 | vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; | |
9242 | vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; | |
9243 | vcpu->run->hypercall.args[0] = gpa; | |
9244 | vcpu->run->hypercall.args[1] = npages; | |
9245 | vcpu->run->hypercall.args[2] = attrs; | |
9246 | vcpu->run->hypercall.longmode = op_64_bit; | |
9247 | vcpu->arch.complete_userspace_io = complete_hypercall_exit; | |
9248 | return 0; | |
9249 | } | |
8776e519 HB |
9250 | default: |
9251 | ret = -KVM_ENOSYS; | |
9252 | break; | |
9253 | } | |
696ca779 | 9254 | out: |
a449c7aa NA |
9255 | if (!op_64_bit) |
9256 | ret = (u32)ret; | |
de3cd117 | 9257 | kvm_rax_write(vcpu, ret); |
6356ee0c | 9258 | |
f11c3a8d | 9259 | ++vcpu->stat.hypercalls; |
6356ee0c | 9260 | return kvm_skip_emulated_instruction(vcpu); |
8776e519 HB |
9261 | } |
9262 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
9263 | ||
b6785def | 9264 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
8776e519 | 9265 | { |
d6aa1000 | 9266 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
8776e519 | 9267 | char instruction[3]; |
5fdbf976 | 9268 | unsigned long rip = kvm_rip_read(vcpu); |
8776e519 | 9269 | |
f1a9761f OU |
9270 | /* |
9271 | * If the quirk is disabled, synthesize a #UD and let the guest pick up | |
9272 | * the pieces. | |
9273 | */ | |
9274 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { | |
9275 | ctxt->exception.error_code_valid = false; | |
9276 | ctxt->exception.vector = UD_VECTOR; | |
9277 | ctxt->have_exception = true; | |
9278 | return X86EMUL_PROPAGATE_FAULT; | |
9279 | } | |
9280 | ||
b3646477 | 9281 | static_call(kvm_x86_patch_hypercall)(vcpu, instruction); |
8776e519 | 9282 | |
ce2e852e DV |
9283 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
9284 | &ctxt->exception); | |
8776e519 HB |
9285 | } |
9286 | ||
851ba692 | 9287 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
b6c7a5dc | 9288 | { |
782d422b MG |
9289 | return vcpu->run->request_interrupt_window && |
9290 | likely(!pic_in_kernel(vcpu->kvm)); | |
b6c7a5dc HB |
9291 | } |
9292 | ||
8d25b7be | 9293 | /* Called within kvm->srcu read side. */ |
851ba692 | 9294 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
b6c7a5dc | 9295 | { |
851ba692 AK |
9296 | struct kvm_run *kvm_run = vcpu->run; |
9297 | ||
c5063551 | 9298 | kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu); |
2d3ad1f4 | 9299 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc | 9300 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
f3d1436d | 9301 | |
127a457a MG |
9302 | kvm_run->ready_for_interrupt_injection = |
9303 | pic_in_kernel(vcpu->kvm) || | |
782d422b | 9304 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
15aad3be CQ |
9305 | |
9306 | if (is_smm(vcpu)) | |
9307 | kvm_run->flags |= KVM_RUN_X86_SMM; | |
b6c7a5dc HB |
9308 | } |
9309 | ||
95ba8273 GN |
9310 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
9311 | { | |
9312 | int max_irr, tpr; | |
9313 | ||
afaf0b2f | 9314 | if (!kvm_x86_ops.update_cr8_intercept) |
95ba8273 GN |
9315 | return; |
9316 | ||
bce87cce | 9317 | if (!lapic_in_kernel(vcpu)) |
88c808fd AK |
9318 | return; |
9319 | ||
d62caabb AS |
9320 | if (vcpu->arch.apicv_active) |
9321 | return; | |
9322 | ||
8db3baa2 GN |
9323 | if (!vcpu->arch.apic->vapic_addr) |
9324 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
9325 | else | |
9326 | max_irr = -1; | |
95ba8273 GN |
9327 | |
9328 | if (max_irr != -1) | |
9329 | max_irr >>= 4; | |
9330 | ||
9331 | tpr = kvm_lapic_get_cr8(vcpu); | |
9332 | ||
b3646477 | 9333 | static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr); |
95ba8273 GN |
9334 | } |
9335 | ||
b97f0745 | 9336 | |
cb6a32c2 SC |
9337 | int kvm_check_nested_events(struct kvm_vcpu *vcpu) |
9338 | { | |
cb6a32c2 SC |
9339 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
9340 | kvm_x86_ops.nested_ops->triple_fault(vcpu); | |
9341 | return 1; | |
9342 | } | |
9343 | ||
9344 | return kvm_x86_ops.nested_ops->check_events(vcpu); | |
9345 | } | |
9346 | ||
b97f0745 ML |
9347 | static void kvm_inject_exception(struct kvm_vcpu *vcpu) |
9348 | { | |
9349 | if (vcpu->arch.exception.error_code && !is_protmode(vcpu)) | |
9350 | vcpu->arch.exception.error_code = false; | |
9351 | static_call(kvm_x86_queue_exception)(vcpu); | |
9352 | } | |
9353 | ||
a5f6909a | 9354 | static int inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) |
95ba8273 | 9355 | { |
b6b8a145 | 9356 | int r; |
c6b22f59 | 9357 | bool can_inject = true; |
b6b8a145 | 9358 | |
95ba8273 | 9359 | /* try to reinject previous events if any */ |
664f8e26 | 9360 | |
c6b22f59 | 9361 | if (vcpu->arch.exception.injected) { |
b97f0745 | 9362 | kvm_inject_exception(vcpu); |
c6b22f59 PB |
9363 | can_inject = false; |
9364 | } | |
664f8e26 | 9365 | /* |
a042c26f LA |
9366 | * Do not inject an NMI or interrupt if there is a pending |
9367 | * exception. Exceptions and interrupts are recognized at | |
9368 | * instruction boundaries, i.e. the start of an instruction. | |
9369 | * Trap-like exceptions, e.g. #DB, have higher priority than | |
9370 | * NMIs and interrupts, i.e. traps are recognized before an | |
9371 | * NMI/interrupt that's pending on the same instruction. | |
9372 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest | |
9373 | * priority, but are only generated (pended) during instruction | |
9374 | * execution, i.e. a pending fault-like exception means the | |
9375 | * fault occurred on the *previous* instruction and must be | |
9376 | * serviced prior to recognizing any new events in order to | |
9377 | * fully complete the previous instruction. | |
664f8e26 | 9378 | */ |
1a680e35 | 9379 | else if (!vcpu->arch.exception.pending) { |
c6b22f59 | 9380 | if (vcpu->arch.nmi_injected) { |
e27bc044 | 9381 | static_call(kvm_x86_inject_nmi)(vcpu); |
c6b22f59 PB |
9382 | can_inject = false; |
9383 | } else if (vcpu->arch.interrupt.injected) { | |
e27bc044 | 9384 | static_call(kvm_x86_inject_irq)(vcpu); |
c6b22f59 PB |
9385 | can_inject = false; |
9386 | } | |
664f8e26 WL |
9387 | } |
9388 | ||
3b82b8d7 SC |
9389 | WARN_ON_ONCE(vcpu->arch.exception.injected && |
9390 | vcpu->arch.exception.pending); | |
9391 | ||
1a680e35 LA |
9392 | /* |
9393 | * Call check_nested_events() even if we reinjected a previous event | |
9394 | * in order for caller to determine if it should require immediate-exit | |
9395 | * from L2 to L1 due to pending L1 events which require exit | |
9396 | * from L2 to L1. | |
9397 | */ | |
56083bdf | 9398 | if (is_guest_mode(vcpu)) { |
cb6a32c2 | 9399 | r = kvm_check_nested_events(vcpu); |
c9d40913 | 9400 | if (r < 0) |
a5f6909a | 9401 | goto out; |
664f8e26 WL |
9402 | } |
9403 | ||
9404 | /* try to inject new event if pending */ | |
b59bb7bd | 9405 | if (vcpu->arch.exception.pending) { |
5c1c85d0 AK |
9406 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
9407 | vcpu->arch.exception.has_error_code, | |
9408 | vcpu->arch.exception.error_code); | |
d6e8c854 | 9409 | |
664f8e26 WL |
9410 | vcpu->arch.exception.pending = false; |
9411 | vcpu->arch.exception.injected = true; | |
9412 | ||
d6e8c854 NA |
9413 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
9414 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | | |
9415 | X86_EFLAGS_RF); | |
9416 | ||
f10c729f | 9417 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
f10c729f JM |
9418 | kvm_deliver_exception_payload(vcpu); |
9419 | if (vcpu->arch.dr7 & DR7_GD) { | |
9420 | vcpu->arch.dr7 &= ~DR7_GD; | |
9421 | kvm_update_dr7(vcpu); | |
9422 | } | |
6bdf0662 NA |
9423 | } |
9424 | ||
b97f0745 | 9425 | kvm_inject_exception(vcpu); |
c6b22f59 | 9426 | can_inject = false; |
1a680e35 LA |
9427 | } |
9428 | ||
61e5f69e ML |
9429 | /* Don't inject interrupts if the user asked to avoid doing so */ |
9430 | if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) | |
9431 | return 0; | |
9432 | ||
c9d40913 PB |
9433 | /* |
9434 | * Finally, inject interrupt events. If an event cannot be injected | |
9435 | * due to architectural conditions (e.g. IF=0) a window-open exit | |
9436 | * will re-request KVM_REQ_EVENT. Sometimes however an event is pending | |
9437 | * and can architecturally be injected, but we cannot do it right now: | |
9438 | * an interrupt could have arrived just now and we have to inject it | |
9439 | * as a vmexit, or there could already an event in the queue, which is | |
9440 | * indicated by can_inject. In that case we request an immediate exit | |
9441 | * in order to make progress and get back here for another iteration. | |
9442 | * The kvm_x86_ops hooks communicate this by returning -EBUSY. | |
9443 | */ | |
9444 | if (vcpu->arch.smi_pending) { | |
b3646477 | 9445 | r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; |
c9d40913 | 9446 | if (r < 0) |
a5f6909a | 9447 | goto out; |
c9d40913 PB |
9448 | if (r) { |
9449 | vcpu->arch.smi_pending = false; | |
9450 | ++vcpu->arch.smi_count; | |
9451 | enter_smm(vcpu); | |
9452 | can_inject = false; | |
9453 | } else | |
b3646477 | 9454 | static_call(kvm_x86_enable_smi_window)(vcpu); |
c9d40913 PB |
9455 | } |
9456 | ||
9457 | if (vcpu->arch.nmi_pending) { | |
b3646477 | 9458 | r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; |
c9d40913 | 9459 | if (r < 0) |
a5f6909a | 9460 | goto out; |
c9d40913 PB |
9461 | if (r) { |
9462 | --vcpu->arch.nmi_pending; | |
9463 | vcpu->arch.nmi_injected = true; | |
e27bc044 | 9464 | static_call(kvm_x86_inject_nmi)(vcpu); |
c9d40913 | 9465 | can_inject = false; |
b3646477 | 9466 | WARN_ON(static_call(kvm_x86_nmi_allowed)(vcpu, true) < 0); |
c9d40913 PB |
9467 | } |
9468 | if (vcpu->arch.nmi_pending) | |
b3646477 | 9469 | static_call(kvm_x86_enable_nmi_window)(vcpu); |
c9d40913 | 9470 | } |
1a680e35 | 9471 | |
c9d40913 | 9472 | if (kvm_cpu_has_injectable_intr(vcpu)) { |
b3646477 | 9473 | r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; |
c9d40913 | 9474 | if (r < 0) |
a5f6909a | 9475 | goto out; |
c9d40913 PB |
9476 | if (r) { |
9477 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); | |
e27bc044 | 9478 | static_call(kvm_x86_inject_irq)(vcpu); |
b3646477 | 9479 | WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); |
c9d40913 PB |
9480 | } |
9481 | if (kvm_cpu_has_injectable_intr(vcpu)) | |
b3646477 | 9482 | static_call(kvm_x86_enable_irq_window)(vcpu); |
95ba8273 | 9483 | } |
ee2cd4b7 | 9484 | |
c9d40913 PB |
9485 | if (is_guest_mode(vcpu) && |
9486 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
9487 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
9488 | *req_immediate_exit = true; | |
9489 | ||
9490 | WARN_ON(vcpu->arch.exception.pending); | |
a5f6909a | 9491 | return 0; |
c9d40913 | 9492 | |
a5f6909a JM |
9493 | out: |
9494 | if (r == -EBUSY) { | |
9495 | *req_immediate_exit = true; | |
9496 | r = 0; | |
9497 | } | |
9498 | return r; | |
95ba8273 GN |
9499 | } |
9500 | ||
7460fb4a AK |
9501 | static void process_nmi(struct kvm_vcpu *vcpu) |
9502 | { | |
9503 | unsigned limit = 2; | |
9504 | ||
9505 | /* | |
9506 | * x86 is limited to one NMI running, and one NMI pending after it. | |
9507 | * If an NMI is already in progress, limit further NMIs to just one. | |
9508 | * Otherwise, allow two (and we'll inject the first one immediately). | |
9509 | */ | |
b3646477 | 9510 | if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) |
7460fb4a AK |
9511 | limit = 1; |
9512 | ||
9513 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); | |
9514 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); | |
9515 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9516 | } | |
9517 | ||
ee2cd4b7 | 9518 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
660a5d51 PB |
9519 | { |
9520 | u32 flags = 0; | |
9521 | flags |= seg->g << 23; | |
9522 | flags |= seg->db << 22; | |
9523 | flags |= seg->l << 21; | |
9524 | flags |= seg->avl << 20; | |
9525 | flags |= seg->present << 15; | |
9526 | flags |= seg->dpl << 13; | |
9527 | flags |= seg->s << 12; | |
9528 | flags |= seg->type << 8; | |
9529 | return flags; | |
9530 | } | |
9531 | ||
ee2cd4b7 | 9532 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
9533 | { |
9534 | struct kvm_segment seg; | |
9535 | int offset; | |
9536 | ||
9537 | kvm_get_segment(vcpu, &seg, n); | |
9538 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); | |
9539 | ||
9540 | if (n < 3) | |
9541 | offset = 0x7f84 + n * 12; | |
9542 | else | |
9543 | offset = 0x7f2c + (n - 3) * 12; | |
9544 | ||
9545 | put_smstate(u32, buf, offset + 8, seg.base); | |
9546 | put_smstate(u32, buf, offset + 4, seg.limit); | |
ee2cd4b7 | 9547 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
9548 | } |
9549 | ||
efbb288a | 9550 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 9551 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
9552 | { |
9553 | struct kvm_segment seg; | |
9554 | int offset; | |
9555 | u16 flags; | |
9556 | ||
9557 | kvm_get_segment(vcpu, &seg, n); | |
9558 | offset = 0x7e00 + n * 16; | |
9559 | ||
ee2cd4b7 | 9560 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
660a5d51 PB |
9561 | put_smstate(u16, buf, offset, seg.selector); |
9562 | put_smstate(u16, buf, offset + 2, flags); | |
9563 | put_smstate(u32, buf, offset + 4, seg.limit); | |
9564 | put_smstate(u64, buf, offset + 8, seg.base); | |
9565 | } | |
efbb288a | 9566 | #endif |
660a5d51 | 9567 | |
ee2cd4b7 | 9568 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 PB |
9569 | { |
9570 | struct desc_ptr dt; | |
9571 | struct kvm_segment seg; | |
9572 | unsigned long val; | |
9573 | int i; | |
9574 | ||
9575 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); | |
9576 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); | |
9577 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); | |
9578 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); | |
9579 | ||
9580 | for (i = 0; i < 8; i++) | |
27b4a9c4 | 9581 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read_raw(vcpu, i)); |
660a5d51 PB |
9582 | |
9583 | kvm_get_dr(vcpu, 6, &val); | |
9584 | put_smstate(u32, buf, 0x7fcc, (u32)val); | |
9585 | kvm_get_dr(vcpu, 7, &val); | |
9586 | put_smstate(u32, buf, 0x7fc8, (u32)val); | |
9587 | ||
9588 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
9589 | put_smstate(u32, buf, 0x7fc4, seg.selector); | |
9590 | put_smstate(u32, buf, 0x7f64, seg.base); | |
9591 | put_smstate(u32, buf, 0x7f60, seg.limit); | |
ee2cd4b7 | 9592 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
9593 | |
9594 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
9595 | put_smstate(u32, buf, 0x7fc0, seg.selector); | |
9596 | put_smstate(u32, buf, 0x7f80, seg.base); | |
9597 | put_smstate(u32, buf, 0x7f7c, seg.limit); | |
ee2cd4b7 | 9598 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
660a5d51 | 9599 | |
b3646477 | 9600 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
660a5d51 PB |
9601 | put_smstate(u32, buf, 0x7f74, dt.address); |
9602 | put_smstate(u32, buf, 0x7f70, dt.size); | |
9603 | ||
b3646477 | 9604 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
660a5d51 PB |
9605 | put_smstate(u32, buf, 0x7f58, dt.address); |
9606 | put_smstate(u32, buf, 0x7f54, dt.size); | |
9607 | ||
9608 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 9609 | enter_smm_save_seg_32(vcpu, buf, i); |
660a5d51 PB |
9610 | |
9611 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); | |
9612 | ||
9613 | /* revision id */ | |
9614 | put_smstate(u32, buf, 0x7efc, 0x00020000); | |
9615 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); | |
9616 | } | |
9617 | ||
b68f3cc7 | 9618 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 9619 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 | 9620 | { |
660a5d51 PB |
9621 | struct desc_ptr dt; |
9622 | struct kvm_segment seg; | |
9623 | unsigned long val; | |
9624 | int i; | |
9625 | ||
9626 | for (i = 0; i < 16; i++) | |
27b4a9c4 | 9627 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read_raw(vcpu, i)); |
660a5d51 PB |
9628 | |
9629 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); | |
9630 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); | |
9631 | ||
9632 | kvm_get_dr(vcpu, 6, &val); | |
9633 | put_smstate(u64, buf, 0x7f68, val); | |
9634 | kvm_get_dr(vcpu, 7, &val); | |
9635 | put_smstate(u64, buf, 0x7f60, val); | |
9636 | ||
9637 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); | |
9638 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); | |
9639 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); | |
9640 | ||
9641 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); | |
9642 | ||
9643 | /* revision id */ | |
9644 | put_smstate(u32, buf, 0x7efc, 0x00020064); | |
9645 | ||
9646 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); | |
9647 | ||
9648 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
9649 | put_smstate(u16, buf, 0x7e90, seg.selector); | |
ee2cd4b7 | 9650 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
9651 | put_smstate(u32, buf, 0x7e94, seg.limit); |
9652 | put_smstate(u64, buf, 0x7e98, seg.base); | |
9653 | ||
b3646477 | 9654 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
660a5d51 PB |
9655 | put_smstate(u32, buf, 0x7e84, dt.size); |
9656 | put_smstate(u64, buf, 0x7e88, dt.address); | |
9657 | ||
9658 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
9659 | put_smstate(u16, buf, 0x7e70, seg.selector); | |
ee2cd4b7 | 9660 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
9661 | put_smstate(u32, buf, 0x7e74, seg.limit); |
9662 | put_smstate(u64, buf, 0x7e78, seg.base); | |
9663 | ||
b3646477 | 9664 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
660a5d51 PB |
9665 | put_smstate(u32, buf, 0x7e64, dt.size); |
9666 | put_smstate(u64, buf, 0x7e68, dt.address); | |
9667 | ||
9668 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 9669 | enter_smm_save_seg_64(vcpu, buf, i); |
660a5d51 | 9670 | } |
b68f3cc7 | 9671 | #endif |
660a5d51 | 9672 | |
ee2cd4b7 | 9673 | static void enter_smm(struct kvm_vcpu *vcpu) |
64d60670 | 9674 | { |
660a5d51 | 9675 | struct kvm_segment cs, ds; |
18c3626e | 9676 | struct desc_ptr dt; |
dbc4739b | 9677 | unsigned long cr0; |
660a5d51 | 9678 | char buf[512]; |
660a5d51 | 9679 | |
660a5d51 | 9680 | memset(buf, 0, 512); |
b68f3cc7 | 9681 | #ifdef CONFIG_X86_64 |
d6321d49 | 9682 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
ee2cd4b7 | 9683 | enter_smm_save_state_64(vcpu, buf); |
660a5d51 | 9684 | else |
b68f3cc7 | 9685 | #endif |
ee2cd4b7 | 9686 | enter_smm_save_state_32(vcpu, buf); |
660a5d51 | 9687 | |
0234bf88 | 9688 | /* |
ecc513e5 SC |
9689 | * Give enter_smm() a chance to make ISA-specific changes to the vCPU |
9690 | * state (e.g. leave guest mode) after we've saved the state into the | |
9691 | * SMM state-save area. | |
0234bf88 | 9692 | */ |
ecc513e5 | 9693 | static_call(kvm_x86_enter_smm)(vcpu, buf); |
0234bf88 | 9694 | |
dc87275f | 9695 | kvm_smm_changed(vcpu, true); |
54bf36aa | 9696 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
660a5d51 | 9697 | |
b3646477 | 9698 | if (static_call(kvm_x86_get_nmi_mask)(vcpu)) |
660a5d51 PB |
9699 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
9700 | else | |
b3646477 | 9701 | static_call(kvm_x86_set_nmi_mask)(vcpu, true); |
660a5d51 PB |
9702 | |
9703 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); | |
9704 | kvm_rip_write(vcpu, 0x8000); | |
9705 | ||
9706 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); | |
b3646477 | 9707 | static_call(kvm_x86_set_cr0)(vcpu, cr0); |
660a5d51 PB |
9708 | vcpu->arch.cr0 = cr0; |
9709 | ||
b3646477 | 9710 | static_call(kvm_x86_set_cr4)(vcpu, 0); |
660a5d51 | 9711 | |
18c3626e PB |
9712 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
9713 | dt.address = dt.size = 0; | |
b3646477 | 9714 | static_call(kvm_x86_set_idt)(vcpu, &dt); |
18c3626e | 9715 | |
996ff542 | 9716 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
660a5d51 PB |
9717 | |
9718 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; | |
9719 | cs.base = vcpu->arch.smbase; | |
9720 | ||
9721 | ds.selector = 0; | |
9722 | ds.base = 0; | |
9723 | ||
9724 | cs.limit = ds.limit = 0xffffffff; | |
9725 | cs.type = ds.type = 0x3; | |
9726 | cs.dpl = ds.dpl = 0; | |
9727 | cs.db = ds.db = 0; | |
9728 | cs.s = ds.s = 1; | |
9729 | cs.l = ds.l = 0; | |
9730 | cs.g = ds.g = 1; | |
9731 | cs.avl = ds.avl = 0; | |
9732 | cs.present = ds.present = 1; | |
9733 | cs.unusable = ds.unusable = 0; | |
9734 | cs.padding = ds.padding = 0; | |
9735 | ||
9736 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
9737 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); | |
9738 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); | |
9739 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); | |
9740 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); | |
9741 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); | |
9742 | ||
b68f3cc7 | 9743 | #ifdef CONFIG_X86_64 |
d6321d49 | 9744 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
b3646477 | 9745 | static_call(kvm_x86_set_efer)(vcpu, 0); |
b68f3cc7 | 9746 | #endif |
660a5d51 | 9747 | |
aedbaf4f | 9748 | kvm_update_cpuid_runtime(vcpu); |
660a5d51 | 9749 | kvm_mmu_reset_context(vcpu); |
64d60670 PB |
9750 | } |
9751 | ||
ee2cd4b7 | 9752 | static void process_smi(struct kvm_vcpu *vcpu) |
c43203ca PB |
9753 | { |
9754 | vcpu->arch.smi_pending = true; | |
9755 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9756 | } | |
9757 | ||
7ee30bc1 NNL |
9758 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
9759 | unsigned long *vcpu_bitmap) | |
9760 | { | |
620b2438 | 9761 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, vcpu_bitmap); |
7ee30bc1 NNL |
9762 | } |
9763 | ||
2860c4b1 PB |
9764 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
9765 | { | |
9766 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); | |
9767 | } | |
9768 | ||
8df14af4 SS |
9769 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
9770 | { | |
06ef8134 ML |
9771 | bool activate; |
9772 | ||
8df14af4 SS |
9773 | if (!lapic_in_kernel(vcpu)) |
9774 | return; | |
9775 | ||
187c8833 | 9776 | down_read(&vcpu->kvm->arch.apicv_update_lock); |
b0a1637f | 9777 | |
06ef8134 ML |
9778 | activate = kvm_apicv_activated(vcpu->kvm); |
9779 | if (vcpu->arch.apicv_active == activate) | |
9780 | goto out; | |
9781 | ||
9782 | vcpu->arch.apicv_active = activate; | |
8df14af4 | 9783 | kvm_apic_update_apicv(vcpu); |
b3646477 | 9784 | static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu); |
bca66dbc VK |
9785 | |
9786 | /* | |
9787 | * When APICv gets disabled, we may still have injected interrupts | |
9788 | * pending. At the same time, KVM_REQ_EVENT may not be set as APICv was | |
9789 | * still active when the interrupt got accepted. Make sure | |
9790 | * inject_pending_event() is called to check for that. | |
9791 | */ | |
9792 | if (!vcpu->arch.apicv_active) | |
9793 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
b0a1637f | 9794 | |
06ef8134 | 9795 | out: |
187c8833 | 9796 | up_read(&vcpu->kvm->arch.apicv_update_lock); |
8df14af4 SS |
9797 | } |
9798 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); | |
9799 | ||
320af55a SC |
9800 | void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm, |
9801 | enum kvm_apicv_inhibit reason, bool set) | |
8df14af4 | 9802 | { |
b0a1637f | 9803 | unsigned long old, new; |
8e205a6b | 9804 | |
187c8833 SC |
9805 | lockdep_assert_held_write(&kvm->arch.apicv_update_lock); |
9806 | ||
7491b7b2 | 9807 | if (!static_call(kvm_x86_check_apicv_inhibit_reasons)(reason)) |
ef8efd7a SS |
9808 | return; |
9809 | ||
b0a1637f ML |
9810 | old = new = kvm->arch.apicv_inhibit_reasons; |
9811 | ||
4f4c4a3e | 9812 | set_or_clear_apicv_inhibit(&new, reason, set); |
8e205a6b | 9813 | |
36222b11 | 9814 | if (!!old != !!new) { |
ee49a893 SC |
9815 | /* |
9816 | * Kick all vCPUs before setting apicv_inhibit_reasons to avoid | |
9817 | * false positives in the sanity check WARN in svm_vcpu_run(). | |
9818 | * This task will wait for all vCPUs to ack the kick IRQ before | |
9819 | * updating apicv_inhibit_reasons, and all other vCPUs will | |
9820 | * block on acquiring apicv_update_lock so that vCPUs can't | |
9821 | * redo svm_vcpu_run() without seeing the new inhibit state. | |
9822 | * | |
9823 | * Note, holding apicv_update_lock and taking it in the read | |
9824 | * side (handling the request) also prevents other vCPUs from | |
9825 | * servicing the request with a stale apicv_inhibit_reasons. | |
9826 | */ | |
36222b11 | 9827 | kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE); |
b0a1637f | 9828 | kvm->arch.apicv_inhibit_reasons = new; |
36222b11 ML |
9829 | if (new) { |
9830 | unsigned long gfn = gpa_to_gfn(APIC_DEFAULT_PHYS_BASE); | |
36222b11 ML |
9831 | kvm_zap_gfn_range(kvm, gfn, gfn+1); |
9832 | } | |
7491b7b2 | 9833 | } else { |
b0a1637f | 9834 | kvm->arch.apicv_inhibit_reasons = new; |
7491b7b2 | 9835 | } |
b0a1637f | 9836 | } |
7d611233 | 9837 | |
320af55a SC |
9838 | void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm, |
9839 | enum kvm_apicv_inhibit reason, bool set) | |
b0a1637f | 9840 | { |
f1575642 SC |
9841 | if (!enable_apicv) |
9842 | return; | |
9843 | ||
187c8833 | 9844 | down_write(&kvm->arch.apicv_update_lock); |
320af55a | 9845 | __kvm_set_or_clear_apicv_inhibit(kvm, reason, set); |
187c8833 | 9846 | up_write(&kvm->arch.apicv_update_lock); |
8df14af4 | 9847 | } |
320af55a | 9848 | EXPORT_SYMBOL_GPL(kvm_set_or_clear_apicv_inhibit); |
8df14af4 | 9849 | |
3d81bc7e | 9850 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
c7c9c56c | 9851 | { |
dcbd3e49 | 9852 | if (!kvm_apic_present(vcpu)) |
3d81bc7e | 9853 | return; |
c7c9c56c | 9854 | |
6308630b | 9855 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
c7c9c56c | 9856 | |
b053b2ae | 9857 | if (irqchip_split(vcpu->kvm)) |
6308630b | 9858 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
db2bdcbb | 9859 | else { |
37c4dbf3 | 9860 | static_call_cond(kvm_x86_sync_pir_to_irr)(vcpu); |
e97f852f WL |
9861 | if (ioapic_in_kernel(vcpu->kvm)) |
9862 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); | |
db2bdcbb | 9863 | } |
e40ff1d6 LA |
9864 | |
9865 | if (is_guest_mode(vcpu)) | |
9866 | vcpu->arch.load_eoi_exitmap_pending = true; | |
9867 | else | |
9868 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); | |
9869 | } | |
9870 | ||
9871 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) | |
9872 | { | |
9873 | u64 eoi_exit_bitmap[4]; | |
9874 | ||
9875 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) | |
9876 | return; | |
9877 | ||
c5adbb3a | 9878 | if (to_hv_vcpu(vcpu)) { |
f2bc14b6 VK |
9879 | bitmap_or((ulong *)eoi_exit_bitmap, |
9880 | vcpu->arch.ioapic_handled_vectors, | |
9881 | to_hv_synic(vcpu)->vec_bitmap, 256); | |
abb6d479 | 9882 | static_call_cond(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap); |
c5adbb3a | 9883 | return; |
9884 | } | |
f2bc14b6 | 9885 | |
abb6d479 | 9886 | static_call_cond(kvm_x86_load_eoi_exitmap)( |
c5adbb3a | 9887 | vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); |
c7c9c56c YZ |
9888 | } |
9889 | ||
e649b3f0 ET |
9890 | void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
9891 | unsigned long start, unsigned long end) | |
b1394e74 RK |
9892 | { |
9893 | unsigned long apic_address; | |
9894 | ||
9895 | /* | |
9896 | * The physical address of apic access page is stored in the VMCS. | |
9897 | * Update it when it becomes invalid. | |
9898 | */ | |
9899 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); | |
9900 | if (start <= apic_address && apic_address < end) | |
9901 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); | |
9902 | } | |
9903 | ||
d081a343 | 9904 | static void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
4256f43f | 9905 | { |
35754c98 | 9906 | if (!lapic_in_kernel(vcpu)) |
f439ed27 PB |
9907 | return; |
9908 | ||
2a890614 | 9909 | static_call_cond(kvm_x86_set_apic_access_page_addr)(vcpu); |
4256f43f | 9910 | } |
4256f43f | 9911 | |
d264ee0c SC |
9912 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
9913 | { | |
9914 | smp_send_reschedule(vcpu->cpu); | |
9915 | } | |
9916 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); | |
9917 | ||
9357d939 | 9918 | /* |
8d25b7be | 9919 | * Called within kvm->srcu read side. |
362c698f | 9920 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
9357d939 TY |
9921 | * exiting to the userspace. Otherwise, the value will be returned to the |
9922 | * userspace. | |
9923 | */ | |
851ba692 | 9924 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
b6c7a5dc HB |
9925 | { |
9926 | int r; | |
62a193ed MG |
9927 | bool req_int_win = |
9928 | dm_request_for_irq_injection(vcpu) && | |
9929 | kvm_cpu_accept_dm_intr(vcpu); | |
404d5d7b | 9930 | fastpath_t exit_fastpath; |
62a193ed | 9931 | |
730dca42 | 9932 | bool req_immediate_exit = false; |
b6c7a5dc | 9933 | |
fb04a1ed PX |
9934 | /* Forbid vmenter if vcpu dirty ring is soft-full */ |
9935 | if (unlikely(vcpu->kvm->dirty_ring_size && | |
9936 | kvm_dirty_ring_soft_full(&vcpu->dirty_ring))) { | |
9937 | vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL; | |
9938 | trace_kvm_dirty_ring_exit(vcpu); | |
9939 | r = 0; | |
9940 | goto out; | |
9941 | } | |
9942 | ||
2fa6e1e1 | 9943 | if (kvm_request_pending(vcpu)) { |
f4d31653 | 9944 | if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) { |
67369273 SC |
9945 | r = -EIO; |
9946 | goto out; | |
9947 | } | |
729c15c2 | 9948 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
9a78e158 | 9949 | if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { |
671ddc70 JM |
9950 | r = 0; |
9951 | goto out; | |
9952 | } | |
9953 | } | |
527d5cd7 SC |
9954 | if (kvm_check_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu)) |
9955 | kvm_mmu_free_obsolete_roots(vcpu); | |
a8eeb04a | 9956 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
2f599714 | 9957 | __kvm_migrate_timers(vcpu); |
d828199e | 9958 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
6b6fcd28 | 9959 | kvm_update_masterclock(vcpu->kvm); |
0061d53d MT |
9960 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
9961 | kvm_gen_kvmclock_update(vcpu); | |
34c238a1 ZA |
9962 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
9963 | r = kvm_guest_time_update(vcpu); | |
8cfdc000 ZA |
9964 | if (unlikely(r)) |
9965 | goto out; | |
9966 | } | |
a8eeb04a | 9967 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
4731d4c7 | 9968 | kvm_mmu_sync_roots(vcpu); |
727a7e27 PB |
9969 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
9970 | kvm_mmu_load_pgd(vcpu); | |
eeeb4f67 | 9971 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
7780938c | 9972 | kvm_vcpu_flush_tlb_all(vcpu); |
eeeb4f67 SC |
9973 | |
9974 | /* Flushing all ASIDs flushes the current ASID... */ | |
9975 | kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
9976 | } | |
40e5f908 | 9977 | kvm_service_local_tlb_flush_requests(vcpu); |
eeeb4f67 | 9978 | |
a8eeb04a | 9979 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
851ba692 | 9980 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
b93463aa AK |
9981 | r = 0; |
9982 | goto out; | |
9983 | } | |
a8eeb04a | 9984 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
cb6a32c2 SC |
9985 | if (is_guest_mode(vcpu)) { |
9986 | kvm_x86_ops.nested_ops->triple_fault(vcpu); | |
9987 | } else { | |
9988 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; | |
9989 | vcpu->mmio_needed = 0; | |
9990 | r = 0; | |
9991 | goto out; | |
9992 | } | |
71c4dfaf | 9993 | } |
af585b92 GN |
9994 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
9995 | /* Page is swapped out. Do synthetic halt */ | |
9996 | vcpu->arch.apf.halted = true; | |
9997 | r = 1; | |
9998 | goto out; | |
9999 | } | |
c9aaa895 GC |
10000 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
10001 | record_steal_time(vcpu); | |
64d60670 PB |
10002 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
10003 | process_smi(vcpu); | |
7460fb4a AK |
10004 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
10005 | process_nmi(vcpu); | |
f5132b01 | 10006 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
c6702c9d | 10007 | kvm_pmu_handle_event(vcpu); |
f5132b01 | 10008 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
c6702c9d | 10009 | kvm_pmu_deliver_pmi(vcpu); |
7543a635 SR |
10010 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
10011 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); | |
10012 | if (test_bit(vcpu->arch.pending_ioapic_eoi, | |
6308630b | 10013 | vcpu->arch.ioapic_handled_vectors)) { |
7543a635 SR |
10014 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
10015 | vcpu->run->eoi.vector = | |
10016 | vcpu->arch.pending_ioapic_eoi; | |
10017 | r = 0; | |
10018 | goto out; | |
10019 | } | |
10020 | } | |
3d81bc7e YZ |
10021 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
10022 | vcpu_scan_ioapic(vcpu); | |
e40ff1d6 LA |
10023 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
10024 | vcpu_load_eoi_exitmap(vcpu); | |
4256f43f TC |
10025 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
10026 | kvm_vcpu_reload_apic_access_page(vcpu); | |
2ce79189 AS |
10027 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
10028 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
10029 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; | |
10030 | r = 0; | |
10031 | goto out; | |
10032 | } | |
e516cebb AS |
10033 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
10034 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
10035 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; | |
10036 | r = 0; | |
10037 | goto out; | |
10038 | } | |
db397571 | 10039 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
9ff5e030 VK |
10040 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
10041 | ||
db397571 | 10042 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
9ff5e030 | 10043 | vcpu->run->hyperv = hv_vcpu->exit; |
db397571 AS |
10044 | r = 0; |
10045 | goto out; | |
10046 | } | |
f3b138c5 AS |
10047 | |
10048 | /* | |
10049 | * KVM_REQ_HV_STIMER has to be processed after | |
10050 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers | |
10051 | * depend on the guest clock being up-to-date | |
10052 | */ | |
1f4b34f8 AS |
10053 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
10054 | kvm_hv_process_stimers(vcpu); | |
8df14af4 SS |
10055 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
10056 | kvm_vcpu_update_apicv(vcpu); | |
557a961a VK |
10057 | if (kvm_check_request(KVM_REQ_APF_READY, vcpu)) |
10058 | kvm_check_async_pf_completion(vcpu); | |
1a155254 | 10059 | if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu)) |
b3646477 | 10060 | static_call(kvm_x86_msr_filter_changed)(vcpu); |
a85863c2 MS |
10061 | |
10062 | if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) | |
10063 | static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); | |
2f52d58c | 10064 | } |
b93463aa | 10065 | |
40da8ccd DW |
10066 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || |
10067 | kvm_xen_has_interrupt(vcpu)) { | |
0f1e261e | 10068 | ++vcpu->stat.req_event; |
4fe09bcf JM |
10069 | r = kvm_apic_accept_events(vcpu); |
10070 | if (r < 0) { | |
10071 | r = 0; | |
10072 | goto out; | |
10073 | } | |
66450a21 JK |
10074 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { |
10075 | r = 1; | |
10076 | goto out; | |
10077 | } | |
10078 | ||
a5f6909a JM |
10079 | r = inject_pending_event(vcpu, &req_immediate_exit); |
10080 | if (r < 0) { | |
10081 | r = 0; | |
10082 | goto out; | |
10083 | } | |
c9d40913 | 10084 | if (req_int_win) |
b3646477 | 10085 | static_call(kvm_x86_enable_irq_window)(vcpu); |
b463a6f7 AK |
10086 | |
10087 | if (kvm_lapic_enabled(vcpu)) { | |
10088 | update_cr8_intercept(vcpu); | |
10089 | kvm_lapic_sync_to_vapic(vcpu); | |
10090 | } | |
10091 | } | |
10092 | ||
d8368af8 AK |
10093 | r = kvm_mmu_reload(vcpu); |
10094 | if (unlikely(r)) { | |
d905c069 | 10095 | goto cancel_injection; |
d8368af8 AK |
10096 | } |
10097 | ||
b6c7a5dc HB |
10098 | preempt_disable(); |
10099 | ||
e27bc044 | 10100 | static_call(kvm_x86_prepare_switch_to_guest)(vcpu); |
b95234c8 PB |
10101 | |
10102 | /* | |
10103 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt | |
10104 | * IPI are then delayed after guest entry, which ensures that they | |
10105 | * result in virtual interrupt delivery. | |
10106 | */ | |
10107 | local_irq_disable(); | |
66fa226c ML |
10108 | |
10109 | /* Store vcpu->apicv_active before vcpu->mode. */ | |
10110 | smp_store_release(&vcpu->mode, IN_GUEST_MODE); | |
6b7e2d09 | 10111 | |
01b71917 MT |
10112 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
10113 | ||
0f127d12 | 10114 | /* |
b95234c8 | 10115 | * 1) We should set ->mode before checking ->requests. Please see |
cde9af6e | 10116 | * the comment in kvm_vcpu_exiting_guest_mode(). |
b95234c8 | 10117 | * |
81b01667 | 10118 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
b95234c8 PB |
10119 | * pairs with the memory barrier implicit in pi_test_and_set_on |
10120 | * (see vmx_deliver_posted_interrupt). | |
10121 | * | |
10122 | * 3) This also orders the write to mode from any reads to the page | |
10123 | * tables done while the VCPU is running. Please see the comment | |
10124 | * in kvm_flush_remote_tlbs. | |
6b7e2d09 | 10125 | */ |
01b71917 | 10126 | smp_mb__after_srcu_read_unlock(); |
b6c7a5dc | 10127 | |
b95234c8 | 10128 | /* |
0f65a9d3 SC |
10129 | * Process pending posted interrupts to handle the case where the |
10130 | * notification IRQ arrived in the host, or was never sent (because the | |
10131 | * target vCPU wasn't running). Do this regardless of the vCPU's APICv | |
10132 | * status, KVM doesn't update assigned devices when APICv is inhibited, | |
10133 | * i.e. they can post interrupts even if APICv is temporarily disabled. | |
b95234c8 | 10134 | */ |
37c4dbf3 PB |
10135 | if (kvm_lapic_enabled(vcpu)) |
10136 | static_call_cond(kvm_x86_sync_pir_to_irr)(vcpu); | |
32f88400 | 10137 | |
5a9f5443 | 10138 | if (kvm_vcpu_exit_request(vcpu)) { |
6b7e2d09 | 10139 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 10140 | smp_wmb(); |
6c142801 AK |
10141 | local_irq_enable(); |
10142 | preempt_enable(); | |
01b71917 | 10143 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
6c142801 | 10144 | r = 1; |
d905c069 | 10145 | goto cancel_injection; |
6c142801 AK |
10146 | } |
10147 | ||
c43203ca PB |
10148 | if (req_immediate_exit) { |
10149 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
b3646477 | 10150 | static_call(kvm_x86_request_immediate_exit)(vcpu); |
c43203ca | 10151 | } |
d6185f20 | 10152 | |
2620fe26 SC |
10153 | fpregs_assert_state_consistent(); |
10154 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
10155 | switch_fpu_return(); | |
5f409e20 | 10156 | |
ec5be88a JL |
10157 | if (vcpu->arch.guest_fpu.xfd_err) |
10158 | wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); | |
10159 | ||
42dbaa5a | 10160 | if (unlikely(vcpu->arch.switch_db_regs)) { |
42dbaa5a JK |
10161 | set_debugreg(0, 7); |
10162 | set_debugreg(vcpu->arch.eff_db[0], 0); | |
10163 | set_debugreg(vcpu->arch.eff_db[1], 1); | |
10164 | set_debugreg(vcpu->arch.eff_db[2], 2); | |
10165 | set_debugreg(vcpu->arch.eff_db[3], 3); | |
f85d4016 LJ |
10166 | } else if (unlikely(hw_breakpoint_active())) { |
10167 | set_debugreg(0, 7); | |
42dbaa5a | 10168 | } |
b6c7a5dc | 10169 | |
b2d2af7e MR |
10170 | guest_timing_enter_irqoff(); |
10171 | ||
d89d04ab | 10172 | for (;;) { |
ee49a893 SC |
10173 | /* |
10174 | * Assert that vCPU vs. VM APICv state is consistent. An APICv | |
10175 | * update must kick and wait for all vCPUs before toggling the | |
10176 | * per-VM state, and responsing vCPUs must wait for the update | |
10177 | * to complete before servicing KVM_REQ_APICV_UPDATE. | |
10178 | */ | |
10179 | WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu)); | |
10180 | ||
e27bc044 | 10181 | exit_fastpath = static_call(kvm_x86_vcpu_run)(vcpu); |
d89d04ab PB |
10182 | if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST)) |
10183 | break; | |
10184 | ||
37c4dbf3 PB |
10185 | if (kvm_lapic_enabled(vcpu)) |
10186 | static_call_cond(kvm_x86_sync_pir_to_irr)(vcpu); | |
de7cd3f6 PB |
10187 | |
10188 | if (unlikely(kvm_vcpu_exit_request(vcpu))) { | |
d89d04ab PB |
10189 | exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED; |
10190 | break; | |
10191 | } | |
de7cd3f6 | 10192 | } |
b6c7a5dc | 10193 | |
c77fb5fe PB |
10194 | /* |
10195 | * Do this here before restoring debug registers on the host. And | |
10196 | * since we do this before handling the vmexit, a DR access vmexit | |
10197 | * can (a) read the correct value of the debug registers, (b) set | |
10198 | * KVM_DEBUGREG_WONT_EXIT again. | |
10199 | */ | |
10200 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { | |
c77fb5fe | 10201 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
b3646477 | 10202 | static_call(kvm_x86_sync_dirty_debug_regs)(vcpu); |
70e4da7a | 10203 | kvm_update_dr0123(vcpu); |
70e4da7a | 10204 | kvm_update_dr7(vcpu); |
c77fb5fe PB |
10205 | } |
10206 | ||
24f1e32c FW |
10207 | /* |
10208 | * If the guest has used debug registers, at least dr7 | |
10209 | * will be disabled while returning to the host. | |
10210 | * If we don't have active breakpoints in the host, we don't | |
10211 | * care about the messed up debug address registers. But if | |
10212 | * we have some of them active, restore the old state. | |
10213 | */ | |
59d8eb53 | 10214 | if (hw_breakpoint_active()) |
24f1e32c | 10215 | hw_breakpoint_restore(); |
42dbaa5a | 10216 | |
c967118d | 10217 | vcpu->arch.last_vmentry_cpu = vcpu->cpu; |
4ba76538 | 10218 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
1d5f066e | 10219 | |
6b7e2d09 | 10220 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 10221 | smp_wmb(); |
a547c6db | 10222 | |
b5274b1b KT |
10223 | /* |
10224 | * Sync xfd before calling handle_exit_irqoff() which may | |
10225 | * rely on the fact that guest_fpu::xfd is up-to-date (e.g. | |
10226 | * in #NM irqoff handler). | |
10227 | */ | |
10228 | if (vcpu->arch.xfd_no_write_intercept) | |
10229 | fpu_sync_guest_vmexit_xfd_state(); | |
10230 | ||
b3646477 | 10231 | static_call(kvm_x86_handle_exit_irqoff)(vcpu); |
b6c7a5dc | 10232 | |
ec5be88a JL |
10233 | if (vcpu->arch.guest_fpu.xfd_err) |
10234 | wrmsrl(MSR_IA32_XFD_ERR, 0); | |
10235 | ||
d7a08882 SC |
10236 | /* |
10237 | * Consume any pending interrupts, including the possible source of | |
10238 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. | |
10239 | * An instruction is required after local_irq_enable() to fully unblock | |
10240 | * interrupts on processors that implement an interrupt shadow, the | |
10241 | * stat.exits increment will do nicely. | |
10242 | */ | |
db215756 | 10243 | kvm_before_interrupt(vcpu, KVM_HANDLING_IRQ); |
d7a08882 | 10244 | local_irq_enable(); |
b6c7a5dc | 10245 | ++vcpu->stat.exits; |
d7a08882 SC |
10246 | local_irq_disable(); |
10247 | kvm_after_interrupt(vcpu); | |
b6c7a5dc | 10248 | |
16045714 WL |
10249 | /* |
10250 | * Wait until after servicing IRQs to account guest time so that any | |
10251 | * ticks that occurred while running the guest are properly accounted | |
10252 | * to the guest. Waiting until IRQs are enabled degrades the accuracy | |
10253 | * of accounting via context tracking, but the loss of accuracy is | |
10254 | * acceptable for all known use cases. | |
10255 | */ | |
b2d2af7e | 10256 | guest_timing_exit_irqoff(); |
16045714 | 10257 | |
ec0671d5 WL |
10258 | if (lapic_in_kernel(vcpu)) { |
10259 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; | |
10260 | if (delta != S64_MIN) { | |
10261 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); | |
10262 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; | |
10263 | } | |
10264 | } | |
b6c7a5dc | 10265 | |
f2485b3e | 10266 | local_irq_enable(); |
b6c7a5dc HB |
10267 | preempt_enable(); |
10268 | ||
f656ce01 | 10269 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
3200f405 | 10270 | |
b6c7a5dc HB |
10271 | /* |
10272 | * Profile KVM exit RIPs: | |
10273 | */ | |
10274 | if (unlikely(prof_on == KVM_PROFILING)) { | |
5fdbf976 MT |
10275 | unsigned long rip = kvm_rip_read(vcpu); |
10276 | profile_hit(KVM_PROFILING, (void *)rip); | |
b6c7a5dc HB |
10277 | } |
10278 | ||
cc578287 ZA |
10279 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
10280 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
298101da | 10281 | |
5cfb1d5a MT |
10282 | if (vcpu->arch.apic_attention) |
10283 | kvm_lapic_sync_from_vapic(vcpu); | |
b93463aa | 10284 | |
b3646477 | 10285 | r = static_call(kvm_x86_handle_exit)(vcpu, exit_fastpath); |
d905c069 MT |
10286 | return r; |
10287 | ||
10288 | cancel_injection: | |
8081ad06 SC |
10289 | if (req_immediate_exit) |
10290 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
b3646477 | 10291 | static_call(kvm_x86_cancel_injection)(vcpu); |
ae7a2a3f MT |
10292 | if (unlikely(vcpu->arch.apic_attention)) |
10293 | kvm_lapic_sync_from_vapic(vcpu); | |
d7690175 MT |
10294 | out: |
10295 | return r; | |
10296 | } | |
b6c7a5dc | 10297 | |
8d25b7be | 10298 | /* Called within kvm->srcu read side. */ |
362c698f PB |
10299 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
10300 | { | |
98c25ead SC |
10301 | bool hv_timer; |
10302 | ||
c3e8abf0 | 10303 | if (!kvm_arch_vcpu_runnable(vcpu)) { |
98c25ead SC |
10304 | /* |
10305 | * Switch to the software timer before halt-polling/blocking as | |
10306 | * the guest's timer may be a break event for the vCPU, and the | |
10307 | * hypervisor timer runs only when the CPU is in guest mode. | |
10308 | * Switch before halt-polling so that KVM recognizes an expired | |
10309 | * timer before blocking. | |
10310 | */ | |
10311 | hv_timer = kvm_lapic_hv_timer_in_use(vcpu); | |
10312 | if (hv_timer) | |
10313 | kvm_lapic_switch_to_sw_timer(vcpu); | |
10314 | ||
9c8fd1ba | 10315 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
cdafece4 SC |
10316 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
10317 | kvm_vcpu_halt(vcpu); | |
10318 | else | |
10319 | kvm_vcpu_block(vcpu); | |
9c8fd1ba | 10320 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
bf9f6ac8 | 10321 | |
98c25ead SC |
10322 | if (hv_timer) |
10323 | kvm_lapic_switch_to_hv_timer(vcpu); | |
10324 | ||
9c8fd1ba PB |
10325 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
10326 | return 1; | |
10327 | } | |
362c698f | 10328 | |
4fe09bcf JM |
10329 | if (kvm_apic_accept_events(vcpu) < 0) |
10330 | return 0; | |
362c698f PB |
10331 | switch(vcpu->arch.mp_state) { |
10332 | case KVM_MP_STATE_HALTED: | |
647daca2 | 10333 | case KVM_MP_STATE_AP_RESET_HOLD: |
362c698f PB |
10334 | vcpu->arch.pv.pv_unhalted = false; |
10335 | vcpu->arch.mp_state = | |
10336 | KVM_MP_STATE_RUNNABLE; | |
df561f66 | 10337 | fallthrough; |
362c698f PB |
10338 | case KVM_MP_STATE_RUNNABLE: |
10339 | vcpu->arch.apf.halted = false; | |
10340 | break; | |
10341 | case KVM_MP_STATE_INIT_RECEIVED: | |
10342 | break; | |
10343 | default: | |
10344 | return -EINTR; | |
362c698f PB |
10345 | } |
10346 | return 1; | |
10347 | } | |
09cec754 | 10348 | |
5d9bc648 PB |
10349 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
10350 | { | |
56083bdf | 10351 | if (is_guest_mode(vcpu)) |
cb6a32c2 | 10352 | kvm_check_nested_events(vcpu); |
0ad3bed6 | 10353 | |
5d9bc648 PB |
10354 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
10355 | !vcpu->arch.apf.halted); | |
10356 | } | |
10357 | ||
8d25b7be | 10358 | /* Called within kvm->srcu read side. */ |
362c698f | 10359 | static int vcpu_run(struct kvm_vcpu *vcpu) |
d7690175 MT |
10360 | { |
10361 | int r; | |
f656ce01 | 10362 | struct kvm *kvm = vcpu->kvm; |
d7690175 | 10363 | |
c595ceee | 10364 | vcpu->arch.l1tf_flush_l1d = true; |
d7690175 | 10365 | |
362c698f | 10366 | for (;;) { |
58f800d5 | 10367 | if (kvm_vcpu_running(vcpu)) { |
851ba692 | 10368 | r = vcpu_enter_guest(vcpu); |
bf9f6ac8 | 10369 | } else { |
362c698f | 10370 | r = vcpu_block(kvm, vcpu); |
bf9f6ac8 FW |
10371 | } |
10372 | ||
09cec754 GN |
10373 | if (r <= 0) |
10374 | break; | |
10375 | ||
084071d5 | 10376 | kvm_clear_request(KVM_REQ_UNBLOCK, vcpu); |
7caf9571 DW |
10377 | if (kvm_xen_has_pending_events(vcpu)) |
10378 | kvm_xen_inject_pending_events(vcpu); | |
10379 | ||
09cec754 GN |
10380 | if (kvm_cpu_has_pending_timer(vcpu)) |
10381 | kvm_inject_pending_timer_irqs(vcpu); | |
10382 | ||
782d422b MG |
10383 | if (dm_request_for_irq_injection(vcpu) && |
10384 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { | |
4ca7dd8c PB |
10385 | r = 0; |
10386 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; | |
09cec754 | 10387 | ++vcpu->stat.request_irq_exits; |
362c698f | 10388 | break; |
09cec754 | 10389 | } |
af585b92 | 10390 | |
f3020b88 | 10391 | if (__xfer_to_guest_mode_work_pending()) { |
f656ce01 | 10392 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
72c3c0fe | 10393 | r = xfer_to_guest_mode_handle_work(vcpu); |
8d25b7be | 10394 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
72c3c0fe TG |
10395 | if (r) |
10396 | return r; | |
d7690175 | 10397 | } |
b6c7a5dc HB |
10398 | } |
10399 | ||
b6c7a5dc HB |
10400 | return r; |
10401 | } | |
10402 | ||
716d51ab GN |
10403 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
10404 | { | |
10405 | int r; | |
60fc3d02 | 10406 | |
716d51ab | 10407 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
0ce97a2b | 10408 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
716d51ab | 10409 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
60fc3d02 | 10410 | return r; |
716d51ab GN |
10411 | } |
10412 | ||
10413 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) | |
10414 | { | |
10415 | BUG_ON(!vcpu->arch.pio.count); | |
10416 | ||
10417 | return complete_emulated_io(vcpu); | |
10418 | } | |
10419 | ||
f78146b0 AK |
10420 | /* |
10421 | * Implements the following, as a state machine: | |
10422 | * | |
10423 | * read: | |
10424 | * for each fragment | |
87da7e66 XG |
10425 | * for each mmio piece in the fragment |
10426 | * write gpa, len | |
10427 | * exit | |
10428 | * copy data | |
f78146b0 AK |
10429 | * execute insn |
10430 | * | |
10431 | * write: | |
10432 | * for each fragment | |
87da7e66 XG |
10433 | * for each mmio piece in the fragment |
10434 | * write gpa, len | |
10435 | * copy data | |
10436 | * exit | |
f78146b0 | 10437 | */ |
716d51ab | 10438 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
5287f194 AK |
10439 | { |
10440 | struct kvm_run *run = vcpu->run; | |
f78146b0 | 10441 | struct kvm_mmio_fragment *frag; |
87da7e66 | 10442 | unsigned len; |
5287f194 | 10443 | |
716d51ab | 10444 | BUG_ON(!vcpu->mmio_needed); |
5287f194 | 10445 | |
716d51ab | 10446 | /* Complete previous fragment */ |
87da7e66 XG |
10447 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
10448 | len = min(8u, frag->len); | |
716d51ab | 10449 | if (!vcpu->mmio_is_write) |
87da7e66 XG |
10450 | memcpy(frag->data, run->mmio.data, len); |
10451 | ||
10452 | if (frag->len <= 8) { | |
10453 | /* Switch to the next fragment. */ | |
10454 | frag++; | |
10455 | vcpu->mmio_cur_fragment++; | |
10456 | } else { | |
10457 | /* Go forward to the next mmio piece. */ | |
10458 | frag->data += len; | |
10459 | frag->gpa += len; | |
10460 | frag->len -= len; | |
10461 | } | |
10462 | ||
a08d3b3b | 10463 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
716d51ab | 10464 | vcpu->mmio_needed = 0; |
0912c977 PB |
10465 | |
10466 | /* FIXME: return into emulator if single-stepping. */ | |
cef4dea0 | 10467 | if (vcpu->mmio_is_write) |
716d51ab GN |
10468 | return 1; |
10469 | vcpu->mmio_read_completed = 1; | |
10470 | return complete_emulated_io(vcpu); | |
10471 | } | |
87da7e66 | 10472 | |
716d51ab GN |
10473 | run->exit_reason = KVM_EXIT_MMIO; |
10474 | run->mmio.phys_addr = frag->gpa; | |
10475 | if (vcpu->mmio_is_write) | |
87da7e66 XG |
10476 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
10477 | run->mmio.len = min(8u, frag->len); | |
716d51ab GN |
10478 | run->mmio.is_write = vcpu->mmio_is_write; |
10479 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; | |
10480 | return 0; | |
5287f194 AK |
10481 | } |
10482 | ||
822f312d SAS |
10483 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
10484 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
10485 | { | |
e27bc044 | 10486 | /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ |
d69c1382 | 10487 | fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); |
822f312d SAS |
10488 | trace_kvm_fpu(1); |
10489 | } | |
10490 | ||
10491 | /* When vcpu_run ends, restore user space FPU context. */ | |
10492 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
10493 | { | |
d69c1382 | 10494 | fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); |
822f312d SAS |
10495 | ++vcpu->stat.fpu_reload; |
10496 | trace_kvm_fpu(0); | |
10497 | } | |
10498 | ||
1b94f6f8 | 10499 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
b6c7a5dc | 10500 | { |
1b94f6f8 | 10501 | struct kvm_run *kvm_run = vcpu->run; |
8d25b7be | 10502 | struct kvm *kvm = vcpu->kvm; |
b6c7a5dc | 10503 | int r; |
b6c7a5dc | 10504 | |
accb757d | 10505 | vcpu_load(vcpu); |
20b7035c | 10506 | kvm_sigset_activate(vcpu); |
15aad3be | 10507 | kvm_run->flags = 0; |
5663d8f9 PX |
10508 | kvm_load_guest_fpu(vcpu); |
10509 | ||
8d25b7be | 10510 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
a4535290 | 10511 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
2f173d26 JS |
10512 | if (kvm_run->immediate_exit) { |
10513 | r = -EINTR; | |
10514 | goto out; | |
10515 | } | |
98c25ead SC |
10516 | /* |
10517 | * It should be impossible for the hypervisor timer to be in | |
10518 | * use before KVM has ever run the vCPU. | |
10519 | */ | |
10520 | WARN_ON_ONCE(kvm_lapic_hv_timer_in_use(vcpu)); | |
8d25b7be PB |
10521 | |
10522 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); | |
c91d4497 | 10523 | kvm_vcpu_block(vcpu); |
8d25b7be PB |
10524 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
10525 | ||
4fe09bcf JM |
10526 | if (kvm_apic_accept_events(vcpu) < 0) { |
10527 | r = 0; | |
10528 | goto out; | |
10529 | } | |
72875d8a | 10530 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
ac9f6dc0 | 10531 | r = -EAGAIN; |
a0595000 JS |
10532 | if (signal_pending(current)) { |
10533 | r = -EINTR; | |
1b94f6f8 | 10534 | kvm_run->exit_reason = KVM_EXIT_INTR; |
a0595000 JS |
10535 | ++vcpu->stat.signal_exits; |
10536 | } | |
ac9f6dc0 | 10537 | goto out; |
b6c7a5dc HB |
10538 | } |
10539 | ||
e489a4a6 SC |
10540 | if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || |
10541 | (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { | |
01643c51 KH |
10542 | r = -EINVAL; |
10543 | goto out; | |
10544 | } | |
10545 | ||
1b94f6f8 | 10546 | if (kvm_run->kvm_dirty_regs) { |
01643c51 KH |
10547 | r = sync_regs(vcpu); |
10548 | if (r != 0) | |
10549 | goto out; | |
10550 | } | |
10551 | ||
b6c7a5dc | 10552 | /* re-sync apic's tpr */ |
35754c98 | 10553 | if (!lapic_in_kernel(vcpu)) { |
eea1cff9 AP |
10554 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
10555 | r = -EINVAL; | |
10556 | goto out; | |
10557 | } | |
10558 | } | |
b6c7a5dc | 10559 | |
716d51ab GN |
10560 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
10561 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; | |
10562 | vcpu->arch.complete_userspace_io = NULL; | |
10563 | r = cui(vcpu); | |
10564 | if (r <= 0) | |
5663d8f9 | 10565 | goto out; |
716d51ab GN |
10566 | } else |
10567 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); | |
5287f194 | 10568 | |
fc4fad79 | 10569 | if (kvm_run->immediate_exit) { |
460df4c1 | 10570 | r = -EINTR; |
fc4fad79 SC |
10571 | goto out; |
10572 | } | |
10573 | ||
10574 | r = static_call(kvm_x86_vcpu_pre_run)(vcpu); | |
10575 | if (r <= 0) | |
10576 | goto out; | |
10577 | ||
10578 | r = vcpu_run(vcpu); | |
b6c7a5dc HB |
10579 | |
10580 | out: | |
5663d8f9 | 10581 | kvm_put_guest_fpu(vcpu); |
1b94f6f8 | 10582 | if (kvm_run->kvm_valid_regs) |
01643c51 | 10583 | store_regs(vcpu); |
f1d86e46 | 10584 | post_kvm_run_save(vcpu); |
8d25b7be | 10585 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
b6c7a5dc | 10586 | |
8d25b7be | 10587 | kvm_sigset_deactivate(vcpu); |
accb757d | 10588 | vcpu_put(vcpu); |
b6c7a5dc HB |
10589 | return r; |
10590 | } | |
10591 | ||
01643c51 | 10592 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 10593 | { |
7ae441ea GN |
10594 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
10595 | /* | |
10596 | * We are here if userspace calls get_regs() in the middle of | |
10597 | * instruction emulation. Registers state needs to be copied | |
4a969980 | 10598 | * back from emulation context to vcpu. Userspace shouldn't do |
7ae441ea GN |
10599 | * that usually, but some bad designed PV devices (vmware |
10600 | * backdoor interface) need this to work | |
10601 | */ | |
c9b8b07c | 10602 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
7ae441ea GN |
10603 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
10604 | } | |
de3cd117 SC |
10605 | regs->rax = kvm_rax_read(vcpu); |
10606 | regs->rbx = kvm_rbx_read(vcpu); | |
10607 | regs->rcx = kvm_rcx_read(vcpu); | |
10608 | regs->rdx = kvm_rdx_read(vcpu); | |
10609 | regs->rsi = kvm_rsi_read(vcpu); | |
10610 | regs->rdi = kvm_rdi_read(vcpu); | |
e9c16c78 | 10611 | regs->rsp = kvm_rsp_read(vcpu); |
de3cd117 | 10612 | regs->rbp = kvm_rbp_read(vcpu); |
b6c7a5dc | 10613 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
10614 | regs->r8 = kvm_r8_read(vcpu); |
10615 | regs->r9 = kvm_r9_read(vcpu); | |
10616 | regs->r10 = kvm_r10_read(vcpu); | |
10617 | regs->r11 = kvm_r11_read(vcpu); | |
10618 | regs->r12 = kvm_r12_read(vcpu); | |
10619 | regs->r13 = kvm_r13_read(vcpu); | |
10620 | regs->r14 = kvm_r14_read(vcpu); | |
10621 | regs->r15 = kvm_r15_read(vcpu); | |
b6c7a5dc HB |
10622 | #endif |
10623 | ||
5fdbf976 | 10624 | regs->rip = kvm_rip_read(vcpu); |
91586a3b | 10625 | regs->rflags = kvm_get_rflags(vcpu); |
01643c51 | 10626 | } |
b6c7a5dc | 10627 | |
01643c51 KH |
10628 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
10629 | { | |
10630 | vcpu_load(vcpu); | |
10631 | __get_regs(vcpu, regs); | |
1fc9b76b | 10632 | vcpu_put(vcpu); |
b6c7a5dc HB |
10633 | return 0; |
10634 | } | |
10635 | ||
01643c51 | 10636 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 10637 | { |
7ae441ea GN |
10638 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
10639 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; | |
10640 | ||
de3cd117 SC |
10641 | kvm_rax_write(vcpu, regs->rax); |
10642 | kvm_rbx_write(vcpu, regs->rbx); | |
10643 | kvm_rcx_write(vcpu, regs->rcx); | |
10644 | kvm_rdx_write(vcpu, regs->rdx); | |
10645 | kvm_rsi_write(vcpu, regs->rsi); | |
10646 | kvm_rdi_write(vcpu, regs->rdi); | |
e9c16c78 | 10647 | kvm_rsp_write(vcpu, regs->rsp); |
de3cd117 | 10648 | kvm_rbp_write(vcpu, regs->rbp); |
b6c7a5dc | 10649 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
10650 | kvm_r8_write(vcpu, regs->r8); |
10651 | kvm_r9_write(vcpu, regs->r9); | |
10652 | kvm_r10_write(vcpu, regs->r10); | |
10653 | kvm_r11_write(vcpu, regs->r11); | |
10654 | kvm_r12_write(vcpu, regs->r12); | |
10655 | kvm_r13_write(vcpu, regs->r13); | |
10656 | kvm_r14_write(vcpu, regs->r14); | |
10657 | kvm_r15_write(vcpu, regs->r15); | |
b6c7a5dc HB |
10658 | #endif |
10659 | ||
5fdbf976 | 10660 | kvm_rip_write(vcpu, regs->rip); |
d73235d1 | 10661 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
b6c7a5dc | 10662 | |
b4f14abd JK |
10663 | vcpu->arch.exception.pending = false; |
10664 | ||
3842d135 | 10665 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
01643c51 | 10666 | } |
3842d135 | 10667 | |
01643c51 KH |
10668 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
10669 | { | |
10670 | vcpu_load(vcpu); | |
10671 | __set_regs(vcpu, regs); | |
875656fe | 10672 | vcpu_put(vcpu); |
b6c7a5dc HB |
10673 | return 0; |
10674 | } | |
10675 | ||
6dba9403 | 10676 | static void __get_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 10677 | { |
89a27f4d | 10678 | struct desc_ptr dt; |
b6c7a5dc | 10679 | |
5265713a TL |
10680 | if (vcpu->arch.guest_state_protected) |
10681 | goto skip_protected_regs; | |
10682 | ||
3e6e0aab GT |
10683 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
10684 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
10685 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
10686 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
10687 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
10688 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 10689 | |
3e6e0aab GT |
10690 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
10691 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 10692 | |
b3646477 | 10693 | static_call(kvm_x86_get_idt)(vcpu, &dt); |
89a27f4d GN |
10694 | sregs->idt.limit = dt.size; |
10695 | sregs->idt.base = dt.address; | |
b3646477 | 10696 | static_call(kvm_x86_get_gdt)(vcpu, &dt); |
89a27f4d GN |
10697 | sregs->gdt.limit = dt.size; |
10698 | sregs->gdt.base = dt.address; | |
b6c7a5dc | 10699 | |
ad312c7c | 10700 | sregs->cr2 = vcpu->arch.cr2; |
9f8fe504 | 10701 | sregs->cr3 = kvm_read_cr3(vcpu); |
5265713a TL |
10702 | |
10703 | skip_protected_regs: | |
10704 | sregs->cr0 = kvm_read_cr0(vcpu); | |
fc78f519 | 10705 | sregs->cr4 = kvm_read_cr4(vcpu); |
2d3ad1f4 | 10706 | sregs->cr8 = kvm_get_cr8(vcpu); |
f6801dff | 10707 | sregs->efer = vcpu->arch.efer; |
b6c7a5dc | 10708 | sregs->apic_base = kvm_get_apic_base(vcpu); |
6dba9403 | 10709 | } |
b6c7a5dc | 10710 | |
6dba9403 ML |
10711 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
10712 | { | |
10713 | __get_sregs_common(vcpu, sregs); | |
10714 | ||
10715 | if (vcpu->arch.guest_state_protected) | |
10716 | return; | |
b6c7a5dc | 10717 | |
04140b41 | 10718 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
14d0bc1f GN |
10719 | set_bit(vcpu->arch.interrupt.nr, |
10720 | (unsigned long *)sregs->interrupt_bitmap); | |
01643c51 | 10721 | } |
16d7a191 | 10722 | |
6dba9403 ML |
10723 | static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2) |
10724 | { | |
10725 | int i; | |
10726 | ||
10727 | __get_sregs_common(vcpu, (struct kvm_sregs *)sregs2); | |
10728 | ||
10729 | if (vcpu->arch.guest_state_protected) | |
10730 | return; | |
10731 | ||
10732 | if (is_pae_paging(vcpu)) { | |
10733 | for (i = 0 ; i < 4 ; i++) | |
10734 | sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); | |
10735 | sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; | |
10736 | } | |
10737 | } | |
10738 | ||
01643c51 KH |
10739 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
10740 | struct kvm_sregs *sregs) | |
10741 | { | |
10742 | vcpu_load(vcpu); | |
10743 | __get_sregs(vcpu, sregs); | |
bcdec41c | 10744 | vcpu_put(vcpu); |
b6c7a5dc HB |
10745 | return 0; |
10746 | } | |
10747 | ||
62d9f0db MT |
10748 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
10749 | struct kvm_mp_state *mp_state) | |
10750 | { | |
4fe09bcf JM |
10751 | int r; |
10752 | ||
fd232561 | 10753 | vcpu_load(vcpu); |
f958bd23 SC |
10754 | if (kvm_mpx_supported()) |
10755 | kvm_load_guest_fpu(vcpu); | |
fd232561 | 10756 | |
4fe09bcf JM |
10757 | r = kvm_apic_accept_events(vcpu); |
10758 | if (r < 0) | |
10759 | goto out; | |
10760 | r = 0; | |
10761 | ||
647daca2 TL |
10762 | if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || |
10763 | vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && | |
10764 | vcpu->arch.pv.pv_unhalted) | |
6aef266c SV |
10765 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; |
10766 | else | |
10767 | mp_state->mp_state = vcpu->arch.mp_state; | |
10768 | ||
4fe09bcf | 10769 | out: |
f958bd23 SC |
10770 | if (kvm_mpx_supported()) |
10771 | kvm_put_guest_fpu(vcpu); | |
fd232561 | 10772 | vcpu_put(vcpu); |
4fe09bcf | 10773 | return r; |
62d9f0db MT |
10774 | } |
10775 | ||
10776 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
10777 | struct kvm_mp_state *mp_state) | |
10778 | { | |
e83dff5e CD |
10779 | int ret = -EINVAL; |
10780 | ||
10781 | vcpu_load(vcpu); | |
10782 | ||
bce87cce | 10783 | if (!lapic_in_kernel(vcpu) && |
66450a21 | 10784 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
e83dff5e | 10785 | goto out; |
66450a21 | 10786 | |
27cbe7d6 LA |
10787 | /* |
10788 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in | |
10789 | * INIT state; latched init should be reported using | |
10790 | * KVM_SET_VCPU_EVENTS, so reject it here. | |
10791 | */ | |
10792 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && | |
28bf2888 DH |
10793 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
10794 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) | |
e83dff5e | 10795 | goto out; |
28bf2888 | 10796 | |
66450a21 JK |
10797 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
10798 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; | |
10799 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); | |
10800 | } else | |
10801 | vcpu->arch.mp_state = mp_state->mp_state; | |
3842d135 | 10802 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
e83dff5e CD |
10803 | |
10804 | ret = 0; | |
10805 | out: | |
10806 | vcpu_put(vcpu); | |
10807 | return ret; | |
62d9f0db MT |
10808 | } |
10809 | ||
7f3d35fd KW |
10810 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
10811 | int reason, bool has_error_code, u32 error_code) | |
b6c7a5dc | 10812 | { |
c9b8b07c | 10813 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d | 10814 | int ret; |
e01c2426 | 10815 | |
8ec4722d | 10816 | init_emulate_ctxt(vcpu); |
c697518a | 10817 | |
7f3d35fd | 10818 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
9d74191a | 10819 | has_error_code, error_code); |
1051778f SC |
10820 | if (ret) { |
10821 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
10822 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
10823 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 10824 | return 0; |
1051778f | 10825 | } |
37817f29 | 10826 | |
9d74191a TY |
10827 | kvm_rip_write(vcpu, ctxt->eip); |
10828 | kvm_set_rflags(vcpu, ctxt->eflags); | |
60fc3d02 | 10829 | return 1; |
37817f29 IE |
10830 | } |
10831 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
10832 | ||
ee69c92b | 10833 | static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
f2981033 | 10834 | { |
37b95951 | 10835 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
f2981033 LT |
10836 | /* |
10837 | * When EFER.LME and CR0.PG are set, the processor is in | |
10838 | * 64-bit mode (though maybe in a 32-bit code segment). | |
10839 | * CR4.PAE and EFER.LMA must be set. | |
10840 | */ | |
ee69c92b SC |
10841 | if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) |
10842 | return false; | |
ca29e145 | 10843 | if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3)) |
c1c35cf7 | 10844 | return false; |
f2981033 LT |
10845 | } else { |
10846 | /* | |
10847 | * Not in 64-bit mode: EFER.LMA is clear and the code | |
10848 | * segment cannot be 64-bit. | |
10849 | */ | |
10850 | if (sregs->efer & EFER_LMA || sregs->cs.l) | |
ee69c92b | 10851 | return false; |
f2981033 LT |
10852 | } |
10853 | ||
ee69c92b | 10854 | return kvm_is_valid_cr4(vcpu, sregs->cr4); |
f2981033 LT |
10855 | } |
10856 | ||
6dba9403 ML |
10857 | static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, |
10858 | int *mmu_reset_needed, bool update_pdptrs) | |
b6c7a5dc | 10859 | { |
58cb628d | 10860 | struct msr_data apic_base_msr; |
6dba9403 | 10861 | int idx; |
89a27f4d | 10862 | struct desc_ptr dt; |
b4ef9d4e | 10863 | |
ee69c92b | 10864 | if (!kvm_is_valid_sregs(vcpu, sregs)) |
6dba9403 | 10865 | return -EINVAL; |
f2981033 | 10866 | |
d3802286 JM |
10867 | apic_base_msr.data = sregs->apic_base; |
10868 | apic_base_msr.host_initiated = true; | |
10869 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) | |
6dba9403 | 10870 | return -EINVAL; |
6d1068b3 | 10871 | |
5265713a | 10872 | if (vcpu->arch.guest_state_protected) |
6dba9403 | 10873 | return 0; |
5265713a | 10874 | |
89a27f4d GN |
10875 | dt.size = sregs->idt.limit; |
10876 | dt.address = sregs->idt.base; | |
b3646477 | 10877 | static_call(kvm_x86_set_idt)(vcpu, &dt); |
89a27f4d GN |
10878 | dt.size = sregs->gdt.limit; |
10879 | dt.address = sregs->gdt.base; | |
b3646477 | 10880 | static_call(kvm_x86_set_gdt)(vcpu, &dt); |
b6c7a5dc | 10881 | |
ad312c7c | 10882 | vcpu->arch.cr2 = sregs->cr2; |
6dba9403 | 10883 | *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
dc7e795e | 10884 | vcpu->arch.cr3 = sregs->cr3; |
3883bc9d | 10885 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); |
405329fc | 10886 | static_call_cond(kvm_x86_post_set_cr3)(vcpu, sregs->cr3); |
b6c7a5dc | 10887 | |
2d3ad1f4 | 10888 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 10889 | |
6dba9403 | 10890 | *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
b3646477 | 10891 | static_call(kvm_x86_set_efer)(vcpu, sregs->efer); |
b6c7a5dc | 10892 | |
6dba9403 | 10893 | *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
b3646477 | 10894 | static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0); |
d7306163 | 10895 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 10896 | |
6dba9403 | 10897 | *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
b3646477 | 10898 | static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4); |
63f42e02 | 10899 | |
6dba9403 ML |
10900 | if (update_pdptrs) { |
10901 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
10902 | if (is_pae_paging(vcpu)) { | |
2df4a5eb | 10903 | load_pdptrs(vcpu, kvm_read_cr3(vcpu)); |
6dba9403 ML |
10904 | *mmu_reset_needed = 1; |
10905 | } | |
10906 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
7c93be44 | 10907 | } |
b6c7a5dc | 10908 | |
3e6e0aab GT |
10909 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
10910 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
10911 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
10912 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
10913 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
10914 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 10915 | |
3e6e0aab GT |
10916 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
10917 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 10918 | |
5f0269f5 ME |
10919 | update_cr8_intercept(vcpu); |
10920 | ||
9c3e4aab | 10921 | /* Older userspace won't unhalt the vcpu on reset. */ |
c5af89b6 | 10922 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
9c3e4aab | 10923 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
3eeb3288 | 10924 | !is_protmode(vcpu)) |
9c3e4aab MT |
10925 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
10926 | ||
6dba9403 ML |
10927 | return 0; |
10928 | } | |
10929 | ||
10930 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) | |
10931 | { | |
10932 | int pending_vec, max_bits; | |
10933 | int mmu_reset_needed = 0; | |
10934 | int ret = __set_sregs_common(vcpu, sregs, &mmu_reset_needed, true); | |
10935 | ||
10936 | if (ret) | |
10937 | return ret; | |
10938 | ||
10939 | if (mmu_reset_needed) | |
10940 | kvm_mmu_reset_context(vcpu); | |
10941 | ||
5265713a TL |
10942 | max_bits = KVM_NR_INTERRUPTS; |
10943 | pending_vec = find_first_bit( | |
10944 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); | |
6dba9403 | 10945 | |
5265713a TL |
10946 | if (pending_vec < max_bits) { |
10947 | kvm_queue_interrupt(vcpu, pending_vec, false); | |
10948 | pr_debug("Set back pending irq %d\n", pending_vec); | |
6dba9403 | 10949 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
5265713a | 10950 | } |
6dba9403 ML |
10951 | return 0; |
10952 | } | |
5265713a | 10953 | |
6dba9403 ML |
10954 | static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2) |
10955 | { | |
10956 | int mmu_reset_needed = 0; | |
10957 | bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; | |
10958 | bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && | |
10959 | !(sregs2->efer & EFER_LMA); | |
10960 | int i, ret; | |
3842d135 | 10961 | |
6dba9403 ML |
10962 | if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) |
10963 | return -EINVAL; | |
10964 | ||
10965 | if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) | |
10966 | return -EINVAL; | |
10967 | ||
10968 | ret = __set_sregs_common(vcpu, (struct kvm_sregs *)sregs2, | |
10969 | &mmu_reset_needed, !valid_pdptrs); | |
10970 | if (ret) | |
10971 | return ret; | |
10972 | ||
10973 | if (valid_pdptrs) { | |
10974 | for (i = 0; i < 4 ; i++) | |
10975 | kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); | |
10976 | ||
10977 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); | |
10978 | mmu_reset_needed = 1; | |
158a48ec | 10979 | vcpu->arch.pdptrs_from_userspace = true; |
6dba9403 ML |
10980 | } |
10981 | if (mmu_reset_needed) | |
10982 | kvm_mmu_reset_context(vcpu); | |
10983 | return 0; | |
01643c51 KH |
10984 | } |
10985 | ||
10986 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
10987 | struct kvm_sregs *sregs) | |
10988 | { | |
10989 | int ret; | |
10990 | ||
10991 | vcpu_load(vcpu); | |
10992 | ret = __set_sregs(vcpu, sregs); | |
b4ef9d4e CD |
10993 | vcpu_put(vcpu); |
10994 | return ret; | |
b6c7a5dc HB |
10995 | } |
10996 | ||
cae72dcc ML |
10997 | static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm) |
10998 | { | |
320af55a | 10999 | bool set = false; |
cae72dcc | 11000 | struct kvm_vcpu *vcpu; |
46808a4c | 11001 | unsigned long i; |
cae72dcc ML |
11002 | |
11003 | down_write(&kvm->arch.apicv_update_lock); | |
11004 | ||
11005 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
11006 | if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { | |
320af55a | 11007 | set = true; |
cae72dcc ML |
11008 | break; |
11009 | } | |
11010 | } | |
320af55a | 11011 | __kvm_set_or_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_BLOCKIRQ, set); |
cae72dcc ML |
11012 | up_write(&kvm->arch.apicv_update_lock); |
11013 | } | |
11014 | ||
d0bfb940 JK |
11015 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
11016 | struct kvm_guest_debug *dbg) | |
b6c7a5dc | 11017 | { |
355be0b9 | 11018 | unsigned long rflags; |
ae675ef0 | 11019 | int i, r; |
b6c7a5dc | 11020 | |
8d4846b9 TL |
11021 | if (vcpu->arch.guest_state_protected) |
11022 | return -EINVAL; | |
11023 | ||
66b56562 CD |
11024 | vcpu_load(vcpu); |
11025 | ||
4f926bf2 JK |
11026 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
11027 | r = -EBUSY; | |
11028 | if (vcpu->arch.exception.pending) | |
2122ff5e | 11029 | goto out; |
4f926bf2 JK |
11030 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
11031 | kvm_queue_exception(vcpu, DB_VECTOR); | |
11032 | else | |
11033 | kvm_queue_exception(vcpu, BP_VECTOR); | |
11034 | } | |
11035 | ||
91586a3b JK |
11036 | /* |
11037 | * Read rflags as long as potentially injected trace flags are still | |
11038 | * filtered out. | |
11039 | */ | |
11040 | rflags = kvm_get_rflags(vcpu); | |
355be0b9 JK |
11041 | |
11042 | vcpu->guest_debug = dbg->control; | |
11043 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) | |
11044 | vcpu->guest_debug = 0; | |
11045 | ||
11046 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
ae675ef0 JK |
11047 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
11048 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | |
c8639010 | 11049 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
ae675ef0 JK |
11050 | } else { |
11051 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
11052 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
ae675ef0 | 11053 | } |
c8639010 | 11054 | kvm_update_dr7(vcpu); |
ae675ef0 | 11055 | |
f92653ee | 11056 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
e87e46d5 | 11057 | vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); |
94fe45da | 11058 | |
91586a3b JK |
11059 | /* |
11060 | * Trigger an rflags update that will inject or remove the trace | |
11061 | * flags. | |
11062 | */ | |
11063 | kvm_set_rflags(vcpu, rflags); | |
b6c7a5dc | 11064 | |
b3646477 | 11065 | static_call(kvm_x86_update_exception_bitmap)(vcpu); |
b6c7a5dc | 11066 | |
cae72dcc ML |
11067 | kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); |
11068 | ||
4f926bf2 | 11069 | r = 0; |
d0bfb940 | 11070 | |
2122ff5e | 11071 | out: |
66b56562 | 11072 | vcpu_put(vcpu); |
b6c7a5dc HB |
11073 | return r; |
11074 | } | |
11075 | ||
8b006791 ZX |
11076 | /* |
11077 | * Translate a guest virtual address to a guest physical address. | |
11078 | */ | |
11079 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
11080 | struct kvm_translation *tr) | |
11081 | { | |
11082 | unsigned long vaddr = tr->linear_address; | |
11083 | gpa_t gpa; | |
f656ce01 | 11084 | int idx; |
8b006791 | 11085 | |
1da5b61d CD |
11086 | vcpu_load(vcpu); |
11087 | ||
f656ce01 | 11088 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
1871c602 | 11089 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
f656ce01 | 11090 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8b006791 ZX |
11091 | tr->physical_address = gpa; |
11092 | tr->valid = gpa != UNMAPPED_GVA; | |
11093 | tr->writeable = 1; | |
11094 | tr->usermode = 0; | |
8b006791 | 11095 | |
1da5b61d | 11096 | vcpu_put(vcpu); |
8b006791 ZX |
11097 | return 0; |
11098 | } | |
11099 | ||
d0752060 HB |
11100 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
11101 | { | |
1393123e | 11102 | struct fxregs_state *fxsave; |
d0752060 | 11103 | |
d69c1382 | 11104 | if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) |
ed02b213 TL |
11105 | return 0; |
11106 | ||
1393123e | 11107 | vcpu_load(vcpu); |
d0752060 | 11108 | |
d69c1382 | 11109 | fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; |
d0752060 HB |
11110 | memcpy(fpu->fpr, fxsave->st_space, 128); |
11111 | fpu->fcw = fxsave->cwd; | |
11112 | fpu->fsw = fxsave->swd; | |
11113 | fpu->ftwx = fxsave->twd; | |
11114 | fpu->last_opcode = fxsave->fop; | |
11115 | fpu->last_ip = fxsave->rip; | |
11116 | fpu->last_dp = fxsave->rdp; | |
0e96f31e | 11117 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
d0752060 | 11118 | |
1393123e | 11119 | vcpu_put(vcpu); |
d0752060 HB |
11120 | return 0; |
11121 | } | |
11122 | ||
11123 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
11124 | { | |
6a96bc7f CD |
11125 | struct fxregs_state *fxsave; |
11126 | ||
d69c1382 | 11127 | if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) |
ed02b213 TL |
11128 | return 0; |
11129 | ||
6a96bc7f CD |
11130 | vcpu_load(vcpu); |
11131 | ||
d69c1382 | 11132 | fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; |
d0752060 | 11133 | |
d0752060 HB |
11134 | memcpy(fxsave->st_space, fpu->fpr, 128); |
11135 | fxsave->cwd = fpu->fcw; | |
11136 | fxsave->swd = fpu->fsw; | |
11137 | fxsave->twd = fpu->ftwx; | |
11138 | fxsave->fop = fpu->last_opcode; | |
11139 | fxsave->rip = fpu->last_ip; | |
11140 | fxsave->rdp = fpu->last_dp; | |
0e96f31e | 11141 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
d0752060 | 11142 | |
6a96bc7f | 11143 | vcpu_put(vcpu); |
d0752060 HB |
11144 | return 0; |
11145 | } | |
11146 | ||
01643c51 KH |
11147 | static void store_regs(struct kvm_vcpu *vcpu) |
11148 | { | |
11149 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); | |
11150 | ||
11151 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) | |
11152 | __get_regs(vcpu, &vcpu->run->s.regs.regs); | |
11153 | ||
11154 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) | |
11155 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); | |
11156 | ||
11157 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) | |
11158 | kvm_vcpu_ioctl_x86_get_vcpu_events( | |
11159 | vcpu, &vcpu->run->s.regs.events); | |
11160 | } | |
11161 | ||
11162 | static int sync_regs(struct kvm_vcpu *vcpu) | |
11163 | { | |
01643c51 KH |
11164 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { |
11165 | __set_regs(vcpu, &vcpu->run->s.regs.regs); | |
11166 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; | |
11167 | } | |
11168 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { | |
11169 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) | |
11170 | return -EINVAL; | |
11171 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; | |
11172 | } | |
11173 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { | |
11174 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( | |
11175 | vcpu, &vcpu->run->s.regs.events)) | |
11176 | return -EINVAL; | |
11177 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; | |
11178 | } | |
11179 | ||
11180 | return 0; | |
11181 | } | |
11182 | ||
897cc38e | 11183 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
e9b11c17 | 11184 | { |
897cc38e SC |
11185 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
11186 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " | |
11187 | "guest TSC will not be reliable\n"); | |
7f1ea208 | 11188 | |
897cc38e | 11189 | return 0; |
e9b11c17 ZX |
11190 | } |
11191 | ||
e529ef66 | 11192 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
e9b11c17 | 11193 | { |
95a0d01e SC |
11194 | struct page *page; |
11195 | int r; | |
c447e76b | 11196 | |
63f5a190 | 11197 | vcpu->arch.last_vmentry_cpu = -1; |
7117003f SC |
11198 | vcpu->arch.regs_avail = ~0; |
11199 | vcpu->arch.regs_dirty = ~0; | |
63f5a190 | 11200 | |
95a0d01e SC |
11201 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
11202 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
11203 | else | |
11204 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; | |
c447e76b | 11205 | |
95a0d01e SC |
11206 | r = kvm_mmu_create(vcpu); |
11207 | if (r < 0) | |
11208 | return r; | |
11209 | ||
11210 | if (irqchip_in_kernel(vcpu->kvm)) { | |
95a0d01e SC |
11211 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
11212 | if (r < 0) | |
11213 | goto fail_mmu_destroy; | |
4e19c36f SS |
11214 | if (kvm_apicv_activated(vcpu->kvm)) |
11215 | vcpu->arch.apicv_active = true; | |
95a0d01e | 11216 | } else |
6e4e3b4d | 11217 | static_branch_inc(&kvm_has_noapic_vcpu); |
95a0d01e SC |
11218 | |
11219 | r = -ENOMEM; | |
11220 | ||
93bb59ca | 11221 | page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
95a0d01e SC |
11222 | if (!page) |
11223 | goto fail_free_lapic; | |
11224 | vcpu->arch.pio_data = page_address(page); | |
11225 | ||
11226 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, | |
11227 | GFP_KERNEL_ACCOUNT); | |
11228 | if (!vcpu->arch.mce_banks) | |
11229 | goto fail_free_pio_data; | |
11230 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; | |
11231 | ||
11232 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, | |
11233 | GFP_KERNEL_ACCOUNT)) | |
11234 | goto fail_free_mce_banks; | |
11235 | ||
c9b8b07c SC |
11236 | if (!alloc_emulate_ctxt(vcpu)) |
11237 | goto free_wbinvd_dirty_mask; | |
11238 | ||
d69c1382 | 11239 | if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { |
95a0d01e | 11240 | pr_err("kvm: failed to allocate vcpu's fpu\n"); |
c9b8b07c | 11241 | goto free_emulate_ctxt; |
95a0d01e SC |
11242 | } |
11243 | ||
95a0d01e | 11244 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
a8ac864a | 11245 | vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); |
95a0d01e SC |
11246 | |
11247 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; | |
11248 | ||
11249 | kvm_async_pf_hash_reset(vcpu); | |
11250 | kvm_pmu_init(vcpu); | |
11251 | ||
11252 | vcpu->arch.pending_external_vector = -1; | |
11253 | vcpu->arch.preempted_in_kernel = false; | |
11254 | ||
3c86c0d3 VP |
11255 | #if IS_ENABLED(CONFIG_HYPERV) |
11256 | vcpu->arch.hv_root_tdp = INVALID_PAGE; | |
11257 | #endif | |
11258 | ||
b3646477 | 11259 | r = static_call(kvm_x86_vcpu_create)(vcpu); |
95a0d01e SC |
11260 | if (r) |
11261 | goto free_guest_fpu; | |
e9b11c17 | 11262 | |
0cf9135b | 11263 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
e53d88af | 11264 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
942c2490 | 11265 | kvm_xen_init_vcpu(vcpu); |
19efffa2 | 11266 | kvm_vcpu_mtrr_init(vcpu); |
ec7660cc | 11267 | vcpu_load(vcpu); |
1ab9287a | 11268 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
d28bc9dd | 11269 | kvm_vcpu_reset(vcpu, false); |
c9060662 | 11270 | kvm_init_mmu(vcpu); |
e9b11c17 | 11271 | vcpu_put(vcpu); |
ec7660cc | 11272 | return 0; |
95a0d01e SC |
11273 | |
11274 | free_guest_fpu: | |
d69c1382 | 11275 | fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); |
c9b8b07c SC |
11276 | free_emulate_ctxt: |
11277 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); | |
95a0d01e SC |
11278 | free_wbinvd_dirty_mask: |
11279 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); | |
11280 | fail_free_mce_banks: | |
11281 | kfree(vcpu->arch.mce_banks); | |
11282 | fail_free_pio_data: | |
11283 | free_page((unsigned long)vcpu->arch.pio_data); | |
11284 | fail_free_lapic: | |
11285 | kvm_free_lapic(vcpu); | |
11286 | fail_mmu_destroy: | |
11287 | kvm_mmu_destroy(vcpu); | |
11288 | return r; | |
e9b11c17 ZX |
11289 | } |
11290 | ||
31928aa5 | 11291 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
42897d86 | 11292 | { |
332967a3 | 11293 | struct kvm *kvm = vcpu->kvm; |
42897d86 | 11294 | |
ec7660cc | 11295 | if (mutex_lock_killable(&vcpu->mutex)) |
31928aa5 | 11296 | return; |
ec7660cc | 11297 | vcpu_load(vcpu); |
0c899c25 | 11298 | kvm_synchronize_tsc(vcpu, 0); |
42897d86 | 11299 | vcpu_put(vcpu); |
2d5ba19b MT |
11300 | |
11301 | /* poll control enabled by default */ | |
11302 | vcpu->arch.msr_kvm_poll_control = 1; | |
11303 | ||
ec7660cc | 11304 | mutex_unlock(&vcpu->mutex); |
42897d86 | 11305 | |
b34de572 WL |
11306 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
11307 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
11308 | KVMCLOCK_SYNC_PERIOD); | |
42897d86 MT |
11309 | } |
11310 | ||
d40ccc62 | 11311 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 | 11312 | { |
95a0d01e | 11313 | int idx; |
344d9588 | 11314 | |
50b143e1 | 11315 | kvmclock_reset(vcpu); |
e9b11c17 | 11316 | |
b3646477 | 11317 | static_call(kvm_x86_vcpu_free)(vcpu); |
50b143e1 | 11318 | |
c9b8b07c | 11319 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
50b143e1 | 11320 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
d69c1382 | 11321 | fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); |
95a0d01e | 11322 | |
a795cd43 | 11323 | kvm_xen_destroy_vcpu(vcpu); |
95a0d01e SC |
11324 | kvm_hv_vcpu_uninit(vcpu); |
11325 | kvm_pmu_destroy(vcpu); | |
11326 | kfree(vcpu->arch.mce_banks); | |
11327 | kvm_free_lapic(vcpu); | |
11328 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
11329 | kvm_mmu_destroy(vcpu); | |
11330 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
11331 | free_page((unsigned long)vcpu->arch.pio_data); | |
255cbecf | 11332 | kvfree(vcpu->arch.cpuid_entries); |
95a0d01e | 11333 | if (!lapic_in_kernel(vcpu)) |
6e4e3b4d | 11334 | static_branch_dec(&kvm_has_noapic_vcpu); |
e9b11c17 ZX |
11335 | } |
11336 | ||
d28bc9dd | 11337 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e9b11c17 | 11338 | { |
25b97845 | 11339 | struct kvm_cpuid_entry2 *cpuid_0x1; |
0aa18375 | 11340 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
4c72ab5a | 11341 | unsigned long new_cr0; |
0aa18375 | 11342 | |
62dd57dd SC |
11343 | /* |
11344 | * Several of the "set" flows, e.g. ->set_cr0(), read other registers | |
11345 | * to handle side effects. RESET emulation hits those flows and relies | |
11346 | * on emulated/virtualized registers, including those that are loaded | |
11347 | * into hardware, to be zeroed at vCPU creation. Use CRs as a sentinel | |
11348 | * to detect improper or missing initialization. | |
11349 | */ | |
11350 | WARN_ON_ONCE(!init_event && | |
11351 | (old_cr0 || kvm_read_cr3(vcpu) || kvm_read_cr4(vcpu))); | |
0aa18375 | 11352 | |
b7e31be3 RK |
11353 | kvm_lapic_reset(vcpu, init_event); |
11354 | ||
e69fab5d PB |
11355 | vcpu->arch.hflags = 0; |
11356 | ||
c43203ca | 11357 | vcpu->arch.smi_pending = 0; |
52797bf9 | 11358 | vcpu->arch.smi_count = 0; |
7460fb4a AK |
11359 | atomic_set(&vcpu->arch.nmi_queued, 0); |
11360 | vcpu->arch.nmi_pending = 0; | |
448fa4a9 | 11361 | vcpu->arch.nmi_injected = false; |
5f7552d4 NA |
11362 | kvm_clear_interrupt_queue(vcpu); |
11363 | kvm_clear_exception_queue(vcpu); | |
448fa4a9 | 11364 | |
42dbaa5a | 11365 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
ae561ede | 11366 | kvm_update_dr0123(vcpu); |
9a3ecd5e | 11367 | vcpu->arch.dr6 = DR6_ACTIVE_LOW; |
42dbaa5a | 11368 | vcpu->arch.dr7 = DR7_FIXED_1; |
c8639010 | 11369 | kvm_update_dr7(vcpu); |
42dbaa5a | 11370 | |
1119022c NA |
11371 | vcpu->arch.cr2 = 0; |
11372 | ||
3842d135 | 11373 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
2635b5c4 VK |
11374 | vcpu->arch.apf.msr_en_val = 0; |
11375 | vcpu->arch.apf.msr_int_val = 0; | |
c9aaa895 | 11376 | vcpu->arch.st.msr_val = 0; |
3842d135 | 11377 | |
12f9a48f GC |
11378 | kvmclock_reset(vcpu); |
11379 | ||
af585b92 GN |
11380 | kvm_clear_async_pf_completion_queue(vcpu); |
11381 | kvm_async_pf_hash_reset(vcpu); | |
11382 | vcpu->arch.apf.halted = false; | |
3842d135 | 11383 | |
d69c1382 TG |
11384 | if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { |
11385 | struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; | |
a554d207 WL |
11386 | |
11387 | /* | |
11388 | * To avoid have the INIT path from kvm_apic_has_events() that be | |
11389 | * called with loaded FPU and does not let userspace fix the state. | |
11390 | */ | |
f775b13e RR |
11391 | if (init_event) |
11392 | kvm_put_guest_fpu(vcpu); | |
087df48c TG |
11393 | |
11394 | fpstate_clear_xstate_component(fpstate, XFEATURE_BNDREGS); | |
11395 | fpstate_clear_xstate_component(fpstate, XFEATURE_BNDCSR); | |
11396 | ||
f775b13e RR |
11397 | if (init_event) |
11398 | kvm_load_guest_fpu(vcpu); | |
a554d207 WL |
11399 | } |
11400 | ||
64d60670 | 11401 | if (!init_event) { |
d28bc9dd | 11402 | kvm_pmu_reset(vcpu); |
64d60670 | 11403 | vcpu->arch.smbase = 0x30000; |
db2336a8 | 11404 | |
db2336a8 | 11405 | vcpu->arch.msr_misc_features_enables = 0; |
a554d207 | 11406 | |
05a9e065 LX |
11407 | __kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP); |
11408 | __kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true); | |
64d60670 | 11409 | } |
f5132b01 | 11410 | |
ff8828c8 | 11411 | /* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */ |
66f7b72e | 11412 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
ff8828c8 | 11413 | kvm_register_mark_dirty(vcpu, VCPU_REGS_RSP); |
66f7b72e | 11414 | |
49d8665c SC |
11415 | /* |
11416 | * Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon) | |
11417 | * if no CPUID match is found. Note, it's impossible to get a match at | |
11418 | * RESET since KVM emulates RESET before exposing the vCPU to userspace, | |
25b97845 SC |
11419 | * i.e. it's impossible for kvm_find_cpuid_entry() to find a valid entry |
11420 | * on RESET. But, go through the motions in case that's ever remedied. | |
49d8665c | 11421 | */ |
25b97845 SC |
11422 | cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1, 0); |
11423 | kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); | |
49d8665c | 11424 | |
b3646477 | 11425 | static_call(kvm_x86_vcpu_reset)(vcpu, init_event); |
0aa18375 | 11426 | |
f39e805e SC |
11427 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
11428 | kvm_rip_write(vcpu, 0xfff0); | |
11429 | ||
03a6e840 SC |
11430 | vcpu->arch.cr3 = 0; |
11431 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); | |
11432 | ||
4c72ab5a SC |
11433 | /* |
11434 | * CR0.CD/NW are set on RESET, preserved on INIT. Note, some versions | |
11435 | * of Intel's SDM list CD/NW as being set on INIT, but they contradict | |
11436 | * (or qualify) that with a footnote stating that CD/NW are preserved. | |
11437 | */ | |
11438 | new_cr0 = X86_CR0_ET; | |
11439 | if (init_event) | |
11440 | new_cr0 |= (old_cr0 & (X86_CR0_NW | X86_CR0_CD)); | |
11441 | else | |
11442 | new_cr0 |= X86_CR0_NW | X86_CR0_CD; | |
11443 | ||
11444 | static_call(kvm_x86_set_cr0)(vcpu, new_cr0); | |
f39e805e SC |
11445 | static_call(kvm_x86_set_cr4)(vcpu, 0); |
11446 | static_call(kvm_x86_set_efer)(vcpu, 0); | |
11447 | static_call(kvm_x86_update_exception_bitmap)(vcpu); | |
11448 | ||
0aa18375 | 11449 | /* |
b5f61c03 PB |
11450 | * On the standard CR0/CR4/EFER modification paths, there are several |
11451 | * complex conditions determining whether the MMU has to be reset and/or | |
11452 | * which PCIDs have to be flushed. However, CR0.WP and the paging-related | |
11453 | * bits in CR4 and EFER are irrelevant if CR0.PG was '0'; and a reset+flush | |
11454 | * is needed anyway if CR0.PG was '1' (which can only happen for INIT, as | |
11455 | * CR0 will be '0' prior to RESET). So we only need to check CR0.PG here. | |
0aa18375 | 11456 | */ |
b5f61c03 PB |
11457 | if (old_cr0 & X86_CR0_PG) { |
11458 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); | |
0aa18375 | 11459 | kvm_mmu_reset_context(vcpu); |
b5f61c03 | 11460 | } |
df37ed38 SC |
11461 | |
11462 | /* | |
11463 | * Intel's SDM states that all TLB entries are flushed on INIT. AMD's | |
11464 | * APM states the TLBs are untouched by INIT, but it also states that | |
11465 | * the TLBs are flushed on "External initialization of the processor." | |
11466 | * Flush the guest TLB regardless of vendor, there is no meaningful | |
11467 | * benefit in relying on the guest to flush the TLB immediately after | |
11468 | * INIT. A spurious TLB flush is benign and likely negligible from a | |
11469 | * performance perspective. | |
11470 | */ | |
11471 | if (init_event) | |
11472 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); | |
e9b11c17 | 11473 | } |
265e4353 | 11474 | EXPORT_SYMBOL_GPL(kvm_vcpu_reset); |
e9b11c17 | 11475 | |
2b4a273b | 11476 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
66450a21 JK |
11477 | { |
11478 | struct kvm_segment cs; | |
11479 | ||
11480 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
11481 | cs.selector = vector << 8; | |
11482 | cs.base = vector << 12; | |
11483 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
11484 | kvm_rip_write(vcpu, 0); | |
e9b11c17 | 11485 | } |
647daca2 | 11486 | EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); |
e9b11c17 | 11487 | |
13a34e06 | 11488 | int kvm_arch_hardware_enable(void) |
e9b11c17 | 11489 | { |
ca84d1a2 ZA |
11490 | struct kvm *kvm; |
11491 | struct kvm_vcpu *vcpu; | |
46808a4c | 11492 | unsigned long i; |
0dd6a6ed ZA |
11493 | int ret; |
11494 | u64 local_tsc; | |
11495 | u64 max_tsc = 0; | |
11496 | bool stable, backwards_tsc = false; | |
18863bdd | 11497 | |
7e34fbd0 | 11498 | kvm_user_return_msr_cpu_online(); |
b3646477 | 11499 | ret = static_call(kvm_x86_hardware_enable)(); |
0dd6a6ed ZA |
11500 | if (ret != 0) |
11501 | return ret; | |
11502 | ||
4ea1636b | 11503 | local_tsc = rdtsc(); |
b0c39dc6 | 11504 | stable = !kvm_check_tsc_unstable(); |
0dd6a6ed ZA |
11505 | list_for_each_entry(kvm, &vm_list, vm_list) { |
11506 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
11507 | if (!stable && vcpu->cpu == smp_processor_id()) | |
105b21bb | 11508 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
11509 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
11510 | backwards_tsc = true; | |
11511 | if (vcpu->arch.last_host_tsc > max_tsc) | |
11512 | max_tsc = vcpu->arch.last_host_tsc; | |
11513 | } | |
11514 | } | |
11515 | } | |
11516 | ||
11517 | /* | |
11518 | * Sometimes, even reliable TSCs go backwards. This happens on | |
11519 | * platforms that reset TSC during suspend or hibernate actions, but | |
11520 | * maintain synchronization. We must compensate. Fortunately, we can | |
11521 | * detect that condition here, which happens early in CPU bringup, | |
11522 | * before any KVM threads can be running. Unfortunately, we can't | |
11523 | * bring the TSCs fully up to date with real time, as we aren't yet far | |
11524 | * enough into CPU bringup that we know how much real time has actually | |
9285ec4c | 11525 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
0dd6a6ed ZA |
11526 | * variables that haven't been updated yet. |
11527 | * | |
11528 | * So we simply find the maximum observed TSC above, then record the | |
11529 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, | |
11530 | * the adjustment will be applied. Note that we accumulate | |
11531 | * adjustments, in case multiple suspend cycles happen before some VCPU | |
11532 | * gets a chance to run again. In the event that no KVM threads get a | |
11533 | * chance to run, we will miss the entire elapsed period, as we'll have | |
11534 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may | |
11535 | * loose cycle time. This isn't too big a deal, since the loss will be | |
11536 | * uniform across all VCPUs (not to mention the scenario is extremely | |
11537 | * unlikely). It is possible that a second hibernate recovery happens | |
11538 | * much faster than a first, causing the observed TSC here to be | |
11539 | * smaller; this would require additional padding adjustment, which is | |
11540 | * why we set last_host_tsc to the local tsc observed here. | |
11541 | * | |
11542 | * N.B. - this code below runs only on platforms with reliable TSC, | |
11543 | * as that is the only way backwards_tsc is set above. Also note | |
11544 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should | |
11545 | * have the same delta_cyc adjustment applied if backwards_tsc | |
11546 | * is detected. Note further, this adjustment is only done once, | |
11547 | * as we reset last_host_tsc on all VCPUs to stop this from being | |
11548 | * called multiple times (one for each physical CPU bringup). | |
11549 | * | |
4a969980 | 11550 | * Platforms with unreliable TSCs don't have to deal with this, they |
0dd6a6ed ZA |
11551 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
11552 | * catchup mode. This will catchup all VCPUs to real time, but cannot | |
11553 | * guarantee that they stay in perfect synchronization. | |
11554 | */ | |
11555 | if (backwards_tsc) { | |
11556 | u64 delta_cyc = max_tsc - local_tsc; | |
11557 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
a826faf1 | 11558 | kvm->arch.backwards_tsc_observed = true; |
0dd6a6ed ZA |
11559 | kvm_for_each_vcpu(i, vcpu, kvm) { |
11560 | vcpu->arch.tsc_offset_adjustment += delta_cyc; | |
11561 | vcpu->arch.last_host_tsc = local_tsc; | |
105b21bb | 11562 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
11563 | } |
11564 | ||
11565 | /* | |
11566 | * We have to disable TSC offset matching.. if you were | |
11567 | * booting a VM while issuing an S4 host suspend.... | |
11568 | * you may have some problem. Solving this issue is | |
11569 | * left as an exercise to the reader. | |
11570 | */ | |
11571 | kvm->arch.last_tsc_nsec = 0; | |
11572 | kvm->arch.last_tsc_write = 0; | |
11573 | } | |
11574 | ||
11575 | } | |
11576 | return 0; | |
e9b11c17 ZX |
11577 | } |
11578 | ||
13a34e06 | 11579 | void kvm_arch_hardware_disable(void) |
e9b11c17 | 11580 | { |
b3646477 | 11581 | static_call(kvm_x86_hardware_disable)(); |
13a34e06 | 11582 | drop_user_return_notifiers(); |
e9b11c17 ZX |
11583 | } |
11584 | ||
b9904085 | 11585 | int kvm_arch_hardware_setup(void *opaque) |
e9b11c17 | 11586 | { |
d008dfdb | 11587 | struct kvm_x86_init_ops *ops = opaque; |
9e9c3fe4 NA |
11588 | int r; |
11589 | ||
91661989 SC |
11590 | rdmsrl_safe(MSR_EFER, &host_efer); |
11591 | ||
408e9a31 PB |
11592 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
11593 | rdmsrl(MSR_IA32_XSS, host_xss); | |
11594 | ||
d008dfdb | 11595 | r = ops->hardware_setup(); |
9e9c3fe4 NA |
11596 | if (r != 0) |
11597 | return r; | |
11598 | ||
afaf0b2f | 11599 | memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); |
b3646477 | 11600 | kvm_ops_static_call_update(); |
69c6f69a | 11601 | |
33271a9e | 11602 | kvm_register_perf_callbacks(ops->handle_intel_pt_intr); |
5c7df80e | 11603 | |
408e9a31 PB |
11604 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
11605 | supported_xss = 0; | |
11606 | ||
139f7425 PB |
11607 | #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) |
11608 | cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); | |
11609 | #undef __kvm_cpu_cap_has | |
b11306b5 | 11610 | |
35181e86 HZ |
11611 | if (kvm_has_tsc_control) { |
11612 | /* | |
11613 | * Make sure the user can only configure tsc_khz values that | |
11614 | * fit into a signed integer. | |
273ba457 | 11615 | * A min value is not calculated because it will always |
35181e86 HZ |
11616 | * be 1 on all machines. |
11617 | */ | |
11618 | u64 max = min(0x7fffffffULL, | |
11619 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); | |
11620 | kvm_max_guest_tsc_khz = max; | |
35181e86 | 11621 | } |
88099313 | 11622 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
9e9c3fe4 NA |
11623 | kvm_init_msr_list(); |
11624 | return 0; | |
e9b11c17 ZX |
11625 | } |
11626 | ||
11627 | void kvm_arch_hardware_unsetup(void) | |
11628 | { | |
e1bfc245 | 11629 | kvm_unregister_perf_callbacks(); |
5c7df80e | 11630 | |
b3646477 | 11631 | static_call(kvm_x86_hardware_unsetup)(); |
e9b11c17 ZX |
11632 | } |
11633 | ||
b9904085 | 11634 | int kvm_arch_check_processor_compat(void *opaque) |
e9b11c17 | 11635 | { |
f1cdecf5 | 11636 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
d008dfdb | 11637 | struct kvm_x86_init_ops *ops = opaque; |
f1cdecf5 SC |
11638 | |
11639 | WARN_ON(!irqs_disabled()); | |
11640 | ||
139f7425 PB |
11641 | if (__cr4_reserved_bits(cpu_has, c) != |
11642 | __cr4_reserved_bits(cpu_has, &boot_cpu_data)) | |
f1cdecf5 SC |
11643 | return -EIO; |
11644 | ||
d008dfdb | 11645 | return ops->check_processor_compatibility(); |
d71ba788 PB |
11646 | } |
11647 | ||
11648 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) | |
11649 | { | |
11650 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; | |
11651 | } | |
11652 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); | |
11653 | ||
11654 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) | |
11655 | { | |
11656 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; | |
e9b11c17 ZX |
11657 | } |
11658 | ||
6e4e3b4d CL |
11659 | __read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu); |
11660 | EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu); | |
54e9818f | 11661 | |
e790d9ef RK |
11662 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
11663 | { | |
b35e5548 LX |
11664 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
11665 | ||
c595ceee | 11666 | vcpu->arch.l1tf_flush_l1d = true; |
b35e5548 LX |
11667 | if (pmu->version && unlikely(pmu->event_count)) { |
11668 | pmu->need_cleanup = true; | |
11669 | kvm_make_request(KVM_REQ_PMU, vcpu); | |
11670 | } | |
b3646477 | 11671 | static_call(kvm_x86_sched_in)(vcpu, cpu); |
e790d9ef RK |
11672 | } |
11673 | ||
562b6b08 SC |
11674 | void kvm_arch_free_vm(struct kvm *kvm) |
11675 | { | |
05f04ae4 | 11676 | kfree(to_kvm_hv(kvm)->hv_pa_pg); |
78b497f2 | 11677 | __kvm_arch_free_vm(kvm); |
e790d9ef RK |
11678 | } |
11679 | ||
562b6b08 | 11680 | |
e08b9637 | 11681 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
d19a9cd2 | 11682 | { |
eb7511bf | 11683 | int ret; |
869b4421 | 11684 | unsigned long flags; |
eb7511bf | 11685 | |
e08b9637 CO |
11686 | if (type) |
11687 | return -EINVAL; | |
11688 | ||
eb7511bf HZ |
11689 | ret = kvm_page_track_init(kvm); |
11690 | if (ret) | |
a1a39128 PB |
11691 | goto out; |
11692 | ||
11693 | ret = kvm_mmu_init_vm(kvm); | |
11694 | if (ret) | |
11695 | goto out_page_track; | |
eb7511bf | 11696 | |
6ef768fa | 11697 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
4d5c5d0f | 11698 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
e0f0bbc5 | 11699 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
d19a9cd2 | 11700 | |
5550af4d SY |
11701 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
11702 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | |
7a84428a AW |
11703 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
11704 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
11705 | &kvm->arch.irq_sources_bitmap); | |
5550af4d | 11706 | |
038f8c11 | 11707 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
1e08ec4a | 11708 | mutex_init(&kvm->arch.apic_map_lock); |
869b4421 | 11709 | seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); |
8171cd68 | 11710 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
869b4421 PB |
11711 | |
11712 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); | |
d828199e | 11713 | pvclock_update_vm_gtod_copy(kvm); |
869b4421 | 11714 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
53f658b3 | 11715 | |
6fbbde9a | 11716 | kvm->arch.guest_can_read_msr_platform_info = true; |
ba7bb663 | 11717 | kvm->arch.enable_pmu = enable_pmu; |
6fbbde9a | 11718 | |
3c86c0d3 VP |
11719 | #if IS_ENABLED(CONFIG_HYPERV) |
11720 | spin_lock_init(&kvm->arch.hv_root_tdp_lock); | |
11721 | kvm->arch.hv_root_tdp = INVALID_PAGE; | |
11722 | #endif | |
11723 | ||
7e44e449 | 11724 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
332967a3 | 11725 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
7e44e449 | 11726 | |
4651fc56 | 11727 | kvm_apicv_init(kvm); |
cbc0236a | 11728 | kvm_hv_init_vm(kvm); |
319afe68 | 11729 | kvm_xen_init_vm(kvm); |
0eb05bf2 | 11730 | |
b3646477 | 11731 | return static_call(kvm_x86_vm_init)(kvm); |
a1a39128 PB |
11732 | |
11733 | out_page_track: | |
11734 | kvm_page_track_cleanup(kvm); | |
11735 | out: | |
11736 | return ret; | |
d19a9cd2 ZX |
11737 | } |
11738 | ||
1aa9b957 JS |
11739 | int kvm_arch_post_init_vm(struct kvm *kvm) |
11740 | { | |
11741 | return kvm_mmu_post_init_vm(kvm); | |
11742 | } | |
11743 | ||
d19a9cd2 ZX |
11744 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
11745 | { | |
ec7660cc | 11746 | vcpu_load(vcpu); |
d19a9cd2 ZX |
11747 | kvm_mmu_unload(vcpu); |
11748 | vcpu_put(vcpu); | |
11749 | } | |
11750 | ||
11751 | static void kvm_free_vcpus(struct kvm *kvm) | |
11752 | { | |
46808a4c | 11753 | unsigned long i; |
988a2cae | 11754 | struct kvm_vcpu *vcpu; |
d19a9cd2 ZX |
11755 | |
11756 | /* | |
11757 | * Unpin any mmu pages first. | |
11758 | */ | |
af585b92 GN |
11759 | kvm_for_each_vcpu(i, vcpu, kvm) { |
11760 | kvm_clear_async_pf_completion_queue(vcpu); | |
988a2cae | 11761 | kvm_unload_vcpu_mmu(vcpu); |
af585b92 | 11762 | } |
d19a9cd2 | 11763 | |
27592ae8 | 11764 | kvm_destroy_vcpus(kvm); |
d19a9cd2 ZX |
11765 | } |
11766 | ||
ad8ba2cd SY |
11767 | void kvm_arch_sync_events(struct kvm *kvm) |
11768 | { | |
332967a3 | 11769 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
7e44e449 | 11770 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
aea924f6 | 11771 | kvm_free_pit(kvm); |
ad8ba2cd SY |
11772 | } |
11773 | ||
ff5a983c PX |
11774 | /** |
11775 | * __x86_set_memory_region: Setup KVM internal memory slot | |
11776 | * | |
11777 | * @kvm: the kvm pointer to the VM. | |
11778 | * @id: the slot ID to setup. | |
11779 | * @gpa: the GPA to install the slot (unused when @size == 0). | |
11780 | * @size: the size of the slot. Set to zero to uninstall a slot. | |
11781 | * | |
11782 | * This function helps to setup a KVM internal memory slot. Specify | |
11783 | * @size > 0 to install a new slot, while @size == 0 to uninstall a | |
11784 | * slot. The return code can be one of the following: | |
11785 | * | |
11786 | * HVA: on success (uninstall will return a bogus HVA) | |
11787 | * -errno: on error | |
11788 | * | |
11789 | * The caller should always use IS_ERR() to check the return value | |
11790 | * before use. Note, the KVM internal memory slots are guaranteed to | |
11791 | * remain valid and unchanged until the VM is destroyed, i.e., the | |
11792 | * GPA->HVA translation will not change. However, the HVA is a user | |
11793 | * address, i.e. its accessibility is not guaranteed, and must be | |
11794 | * accessed via __copy_{to,from}_user(). | |
11795 | */ | |
11796 | void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, | |
11797 | u32 size) | |
9da0e4d5 PB |
11798 | { |
11799 | int i, r; | |
3f649ab7 | 11800 | unsigned long hva, old_npages; |
f0d648bd | 11801 | struct kvm_memslots *slots = kvm_memslots(kvm); |
0577d1ab | 11802 | struct kvm_memory_slot *slot; |
9da0e4d5 PB |
11803 | |
11804 | /* Called with kvm->slots_lock held. */ | |
1d8007bd | 11805 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
ff5a983c | 11806 | return ERR_PTR_USR(-EINVAL); |
9da0e4d5 | 11807 | |
f0d648bd PB |
11808 | slot = id_to_memslot(slots, id); |
11809 | if (size) { | |
0577d1ab | 11810 | if (slot && slot->npages) |
ff5a983c | 11811 | return ERR_PTR_USR(-EEXIST); |
f0d648bd PB |
11812 | |
11813 | /* | |
11814 | * MAP_SHARED to prevent internal slot pages from being moved | |
11815 | * by fork()/COW. | |
11816 | */ | |
11817 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, | |
11818 | MAP_SHARED | MAP_ANONYMOUS, 0); | |
11819 | if (IS_ERR((void *)hva)) | |
ff5a983c | 11820 | return (void __user *)hva; |
f0d648bd | 11821 | } else { |
0577d1ab | 11822 | if (!slot || !slot->npages) |
46914534 | 11823 | return NULL; |
f0d648bd | 11824 | |
0577d1ab | 11825 | old_npages = slot->npages; |
b66f9bab | 11826 | hva = slot->userspace_addr; |
f0d648bd PB |
11827 | } |
11828 | ||
9da0e4d5 | 11829 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
1d8007bd | 11830 | struct kvm_userspace_memory_region m; |
9da0e4d5 | 11831 | |
1d8007bd PB |
11832 | m.slot = id | (i << 16); |
11833 | m.flags = 0; | |
11834 | m.guest_phys_addr = gpa; | |
f0d648bd | 11835 | m.userspace_addr = hva; |
1d8007bd | 11836 | m.memory_size = size; |
9da0e4d5 PB |
11837 | r = __kvm_set_memory_region(kvm, &m); |
11838 | if (r < 0) | |
ff5a983c | 11839 | return ERR_PTR_USR(r); |
9da0e4d5 PB |
11840 | } |
11841 | ||
103c763c | 11842 | if (!size) |
0577d1ab | 11843 | vm_munmap(hva, old_npages * PAGE_SIZE); |
f0d648bd | 11844 | |
ff5a983c | 11845 | return (void __user *)hva; |
9da0e4d5 PB |
11846 | } |
11847 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); | |
11848 | ||
1aa9b957 JS |
11849 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
11850 | { | |
11851 | kvm_mmu_pre_destroy_vm(kvm); | |
11852 | } | |
11853 | ||
d19a9cd2 ZX |
11854 | void kvm_arch_destroy_vm(struct kvm *kvm) |
11855 | { | |
27469d29 AH |
11856 | if (current->mm == kvm->mm) { |
11857 | /* | |
11858 | * Free memory regions allocated on behalf of userspace, | |
11859 | * unless the the memory map has changed due to process exit | |
11860 | * or fd copying. | |
11861 | */ | |
6a3c623b PX |
11862 | mutex_lock(&kvm->slots_lock); |
11863 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, | |
11864 | 0, 0); | |
11865 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, | |
11866 | 0, 0); | |
11867 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); | |
11868 | mutex_unlock(&kvm->slots_lock); | |
27469d29 | 11869 | } |
b3646477 | 11870 | static_call_cond(kvm_x86_vm_destroy)(kvm); |
b318e8de | 11871 | kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); |
c761159c PX |
11872 | kvm_pic_destroy(kvm); |
11873 | kvm_ioapic_destroy(kvm); | |
d19a9cd2 | 11874 | kvm_free_vcpus(kvm); |
af1bae54 | 11875 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
66bb8a06 | 11876 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
13d268ca | 11877 | kvm_mmu_uninit_vm(kvm); |
2beb6dad | 11878 | kvm_page_track_cleanup(kvm); |
7d6bbebb | 11879 | kvm_xen_destroy_vm(kvm); |
cbc0236a | 11880 | kvm_hv_destroy_vm(kvm); |
d19a9cd2 | 11881 | } |
0de10343 | 11882 | |
c9b929b3 | 11883 | static void memslot_rmap_free(struct kvm_memory_slot *slot) |
db3fe4eb TY |
11884 | { |
11885 | int i; | |
11886 | ||
d89cc617 | 11887 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
e96c81ee SC |
11888 | kvfree(slot->arch.rmap[i]); |
11889 | slot->arch.rmap[i] = NULL; | |
c9b929b3 BG |
11890 | } |
11891 | } | |
e96c81ee | 11892 | |
c9b929b3 BG |
11893 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
11894 | { | |
11895 | int i; | |
11896 | ||
11897 | memslot_rmap_free(slot); | |
d89cc617 | 11898 | |
c9b929b3 | 11899 | for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { |
e96c81ee SC |
11900 | kvfree(slot->arch.lpage_info[i - 1]); |
11901 | slot->arch.lpage_info[i - 1] = NULL; | |
db3fe4eb | 11902 | } |
21ebbeda | 11903 | |
e96c81ee | 11904 | kvm_page_track_free_memslot(slot); |
db3fe4eb TY |
11905 | } |
11906 | ||
1e76a3ce | 11907 | int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages) |
56dd1019 BG |
11908 | { |
11909 | const int sz = sizeof(*slot->arch.rmap[0]); | |
11910 | int i; | |
11911 | ||
11912 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { | |
11913 | int level = i + 1; | |
4139b197 | 11914 | int lpages = __kvm_mmu_slot_lpages(slot, npages, level); |
56dd1019 | 11915 | |
fa13843d PB |
11916 | if (slot->arch.rmap[i]) |
11917 | continue; | |
d501f747 | 11918 | |
37b2a651 | 11919 | slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); |
56dd1019 BG |
11920 | if (!slot->arch.rmap[i]) { |
11921 | memslot_rmap_free(slot); | |
11922 | return -ENOMEM; | |
11923 | } | |
11924 | } | |
11925 | ||
11926 | return 0; | |
11927 | } | |
11928 | ||
a2557408 | 11929 | static int kvm_alloc_memslot_metadata(struct kvm *kvm, |
9d7d18ee | 11930 | struct kvm_memory_slot *slot) |
db3fe4eb | 11931 | { |
9d7d18ee | 11932 | unsigned long npages = slot->npages; |
56dd1019 | 11933 | int i, r; |
db3fe4eb | 11934 | |
edd4fa37 SC |
11935 | /* |
11936 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The | |
11937 | * old arrays will be freed by __kvm_set_memory_region() if installing | |
11938 | * the new memslot is successful. | |
11939 | */ | |
11940 | memset(&slot->arch, 0, sizeof(slot->arch)); | |
11941 | ||
e2209710 | 11942 | if (kvm_memslots_have_rmaps(kvm)) { |
a2557408 BG |
11943 | r = memslot_rmap_alloc(slot, npages); |
11944 | if (r) | |
11945 | return r; | |
11946 | } | |
56dd1019 BG |
11947 | |
11948 | for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { | |
92f94f1e | 11949 | struct kvm_lpage_info *linfo; |
db3fe4eb TY |
11950 | unsigned long ugfn; |
11951 | int lpages; | |
d89cc617 | 11952 | int level = i + 1; |
db3fe4eb | 11953 | |
4139b197 | 11954 | lpages = __kvm_mmu_slot_lpages(slot, npages, level); |
db3fe4eb | 11955 | |
37b2a651 | 11956 | linfo = __vcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
92f94f1e | 11957 | if (!linfo) |
db3fe4eb TY |
11958 | goto out_free; |
11959 | ||
92f94f1e XG |
11960 | slot->arch.lpage_info[i - 1] = linfo; |
11961 | ||
db3fe4eb | 11962 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 11963 | linfo[0].disallow_lpage = 1; |
db3fe4eb | 11964 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 11965 | linfo[lpages - 1].disallow_lpage = 1; |
db3fe4eb TY |
11966 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
11967 | /* | |
11968 | * If the gfn and userspace address are not aligned wrt each | |
600087b6 | 11969 | * other, disable large page support for this slot. |
db3fe4eb | 11970 | */ |
600087b6 | 11971 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
db3fe4eb TY |
11972 | unsigned long j; |
11973 | ||
11974 | for (j = 0; j < lpages; ++j) | |
92f94f1e | 11975 | linfo[j].disallow_lpage = 1; |
db3fe4eb TY |
11976 | } |
11977 | } | |
11978 | ||
deae4a10 | 11979 | if (kvm_page_track_create_memslot(kvm, slot, npages)) |
21ebbeda XG |
11980 | goto out_free; |
11981 | ||
db3fe4eb TY |
11982 | return 0; |
11983 | ||
11984 | out_free: | |
c9b929b3 | 11985 | memslot_rmap_free(slot); |
d89cc617 | 11986 | |
c9b929b3 | 11987 | for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { |
548ef284 | 11988 | kvfree(slot->arch.lpage_info[i - 1]); |
d89cc617 | 11989 | slot->arch.lpage_info[i - 1] = NULL; |
db3fe4eb TY |
11990 | } |
11991 | return -ENOMEM; | |
11992 | } | |
11993 | ||
15248258 | 11994 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
e59dbe09 | 11995 | { |
91724814 | 11996 | struct kvm_vcpu *vcpu; |
46808a4c | 11997 | unsigned long i; |
91724814 | 11998 | |
e6dff7d1 TY |
11999 | /* |
12000 | * memslots->generation has been incremented. | |
12001 | * mmio generation may have reached its maximum value. | |
12002 | */ | |
15248258 | 12003 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
91724814 BO |
12004 | |
12005 | /* Force re-initialization of steal_time cache */ | |
12006 | kvm_for_each_vcpu(i, vcpu, kvm) | |
12007 | kvm_vcpu_kick(vcpu); | |
e59dbe09 TY |
12008 | } |
12009 | ||
f7784b8e | 12010 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
537a17b3 SC |
12011 | const struct kvm_memory_slot *old, |
12012 | struct kvm_memory_slot *new, | |
12013 | enum kvm_mr_change change) | |
0de10343 | 12014 | { |
0dab98b7 | 12015 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
9d7d18ee | 12016 | return kvm_alloc_memslot_metadata(kvm, new); |
537a17b3 SC |
12017 | |
12018 | if (change == KVM_MR_FLAGS_ONLY) | |
12019 | memcpy(&new->arch, &old->arch, sizeof(old->arch)); | |
12020 | else if (WARN_ON_ONCE(change != KVM_MR_DELETE)) | |
12021 | return -EIO; | |
12022 | ||
f7784b8e MT |
12023 | return 0; |
12024 | } | |
12025 | ||
a85863c2 MS |
12026 | |
12027 | static void kvm_mmu_update_cpu_dirty_logging(struct kvm *kvm, bool enable) | |
12028 | { | |
12029 | struct kvm_arch *ka = &kvm->arch; | |
12030 | ||
12031 | if (!kvm_x86_ops.cpu_dirty_log_size) | |
12032 | return; | |
12033 | ||
12034 | if ((enable && ++ka->cpu_dirty_logging_count == 1) || | |
12035 | (!enable && --ka->cpu_dirty_logging_count == 0)) | |
12036 | kvm_make_all_cpus_request(kvm, KVM_REQ_UPDATE_CPU_DIRTY_LOGGING); | |
12037 | ||
12038 | WARN_ON_ONCE(ka->cpu_dirty_logging_count < 0); | |
12039 | } | |
12040 | ||
88178fd4 | 12041 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
3741679b | 12042 | struct kvm_memory_slot *old, |
269e9552 | 12043 | const struct kvm_memory_slot *new, |
3741679b | 12044 | enum kvm_mr_change change) |
88178fd4 | 12045 | { |
77aedf26 SC |
12046 | u32 old_flags = old ? old->flags : 0; |
12047 | u32 new_flags = new ? new->flags : 0; | |
12048 | bool log_dirty_pages = new_flags & KVM_MEM_LOG_DIRTY_PAGES; | |
a85863c2 | 12049 | |
3741679b | 12050 | /* |
a85863c2 MS |
12051 | * Update CPU dirty logging if dirty logging is being toggled. This |
12052 | * applies to all operations. | |
3741679b | 12053 | */ |
77aedf26 | 12054 | if ((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES) |
a85863c2 | 12055 | kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages); |
88178fd4 KH |
12056 | |
12057 | /* | |
a85863c2 | 12058 | * Nothing more to do for RO slots (which can't be dirtied and can't be |
b6e16ae5 | 12059 | * made writable) or CREATE/MOVE/DELETE of a slot. |
88178fd4 | 12060 | * |
b6e16ae5 | 12061 | * For a memslot with dirty logging disabled: |
3741679b AY |
12062 | * CREATE: No dirty mappings will already exist. |
12063 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
12064 | * kvm_arch_flush_shadow_memslot() | |
b6e16ae5 SC |
12065 | * |
12066 | * For a memslot with dirty logging enabled: | |
12067 | * CREATE: No shadow pages exist, thus nothing to write-protect | |
12068 | * and no dirty bits to clear. | |
12069 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
12070 | * kvm_arch_flush_shadow_memslot(). | |
3741679b | 12071 | */ |
77aedf26 | 12072 | if ((change != KVM_MR_FLAGS_ONLY) || (new_flags & KVM_MEM_READONLY)) |
88178fd4 | 12073 | return; |
3741679b AY |
12074 | |
12075 | /* | |
52f46079 SC |
12076 | * READONLY and non-flags changes were filtered out above, and the only |
12077 | * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty | |
12078 | * logging isn't being toggled on or off. | |
88178fd4 | 12079 | */ |
77aedf26 | 12080 | if (WARN_ON_ONCE(!((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES))) |
52f46079 SC |
12081 | return; |
12082 | ||
b6e16ae5 SC |
12083 | if (!log_dirty_pages) { |
12084 | /* | |
12085 | * Dirty logging tracks sptes in 4k granularity, meaning that | |
12086 | * large sptes have to be split. If live migration succeeds, | |
12087 | * the guest in the source machine will be destroyed and large | |
12088 | * sptes will be created in the destination. However, if the | |
12089 | * guest continues to run in the source machine (for example if | |
12090 | * live migration fails), small sptes will remain around and | |
12091 | * cause bad performance. | |
12092 | * | |
12093 | * Scan sptes if dirty logging has been stopped, dropping those | |
12094 | * which can be collapsed into a single large-page spte. Later | |
12095 | * page faults will create the large-page sptes. | |
12096 | */ | |
3741679b | 12097 | kvm_mmu_zap_collapsible_sptes(kvm, new); |
b6e16ae5 | 12098 | } else { |
89212919 KZ |
12099 | /* |
12100 | * Initially-all-set does not require write protecting any page, | |
12101 | * because they're all assumed to be dirty. | |
12102 | */ | |
12103 | if (kvm_dirty_log_manual_protect_and_init_set(kvm)) | |
12104 | return; | |
a1419f8b | 12105 | |
a3fe5dbd DM |
12106 | if (READ_ONCE(eager_page_split)) |
12107 | kvm_mmu_slot_try_split_huge_pages(kvm, new, PG_LEVEL_4K); | |
12108 | ||
a018eba5 | 12109 | if (kvm_x86_ops.cpu_dirty_log_size) { |
89212919 KZ |
12110 | kvm_mmu_slot_leaf_clear_dirty(kvm, new); |
12111 | kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_2M); | |
12112 | } else { | |
12113 | kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K); | |
3c9bd400 | 12114 | } |
88178fd4 KH |
12115 | } |
12116 | } | |
12117 | ||
f7784b8e | 12118 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
9d4c197c | 12119 | struct kvm_memory_slot *old, |
f36f3f28 | 12120 | const struct kvm_memory_slot *new, |
8482644a | 12121 | enum kvm_mr_change change) |
f7784b8e | 12122 | { |
e0c2b633 | 12123 | if (!kvm->arch.n_requested_mmu_pages && |
f5756029 MS |
12124 | (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { |
12125 | unsigned long nr_mmu_pages; | |
12126 | ||
12127 | nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; | |
12128 | nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); | |
12129 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
12130 | } | |
1c91cad4 | 12131 | |
269e9552 | 12132 | kvm_mmu_slot_apply_flags(kvm, old, new, change); |
21198846 SC |
12133 | |
12134 | /* Free the arrays associated with the old memslot. */ | |
12135 | if (change == KVM_MR_MOVE) | |
e96c81ee | 12136 | kvm_arch_free_memslot(kvm, old); |
0de10343 | 12137 | } |
1d737c8a | 12138 | |
2df72e9b | 12139 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
34d4cb8f | 12140 | { |
7390de1e | 12141 | kvm_mmu_zap_all(kvm); |
34d4cb8f MT |
12142 | } |
12143 | ||
2df72e9b MT |
12144 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
12145 | struct kvm_memory_slot *slot) | |
12146 | { | |
ae7cd873 | 12147 | kvm_page_track_flush_slot(kvm, slot); |
2df72e9b MT |
12148 | } |
12149 | ||
e6c67d8c LA |
12150 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
12151 | { | |
12152 | return (is_guest_mode(vcpu) && | |
5be2226f | 12153 | static_call(kvm_x86_guest_apic_has_interrupt)(vcpu)); |
e6c67d8c LA |
12154 | } |
12155 | ||
5d9bc648 PB |
12156 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
12157 | { | |
12158 | if (!list_empty_careful(&vcpu->async_pf.done)) | |
12159 | return true; | |
12160 | ||
12161 | if (kvm_apic_has_events(vcpu)) | |
12162 | return true; | |
12163 | ||
12164 | if (vcpu->arch.pv.pv_unhalted) | |
12165 | return true; | |
12166 | ||
a5f01f8e WL |
12167 | if (vcpu->arch.exception.pending) |
12168 | return true; | |
12169 | ||
47a66eed Z |
12170 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
12171 | (vcpu->arch.nmi_pending && | |
b3646477 | 12172 | static_call(kvm_x86_nmi_allowed)(vcpu, false))) |
5d9bc648 PB |
12173 | return true; |
12174 | ||
47a66eed | 12175 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
a9fa7cb6 | 12176 | (vcpu->arch.smi_pending && |
b3646477 | 12177 | static_call(kvm_x86_smi_allowed)(vcpu, false))) |
73917739 PB |
12178 | return true; |
12179 | ||
5d9bc648 | 12180 | if (kvm_arch_interrupt_allowed(vcpu) && |
e6c67d8c LA |
12181 | (kvm_cpu_has_interrupt(vcpu) || |
12182 | kvm_guest_apic_has_interrupt(vcpu))) | |
5d9bc648 PB |
12183 | return true; |
12184 | ||
1f4b34f8 AS |
12185 | if (kvm_hv_has_stimer_pending(vcpu)) |
12186 | return true; | |
12187 | ||
d2060bd4 SC |
12188 | if (is_guest_mode(vcpu) && |
12189 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
12190 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
12191 | return true; | |
12192 | ||
7caf9571 DW |
12193 | if (kvm_xen_has_pending_events(vcpu)) |
12194 | return true; | |
12195 | ||
5d9bc648 PB |
12196 | return false; |
12197 | } | |
12198 | ||
1d737c8a ZX |
12199 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
12200 | { | |
5d9bc648 | 12201 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
1d737c8a | 12202 | } |
5736199a | 12203 | |
10dbdf98 | 12204 | bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) |
17e433b5 | 12205 | { |
b3646477 | 12206 | if (vcpu->arch.apicv_active && static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu)) |
52acd22f WL |
12207 | return true; |
12208 | ||
12209 | return false; | |
12210 | } | |
12211 | ||
17e433b5 WL |
12212 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
12213 | { | |
12214 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) | |
12215 | return true; | |
12216 | ||
12217 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || | |
12218 | kvm_test_request(KVM_REQ_SMI, vcpu) || | |
12219 | kvm_test_request(KVM_REQ_EVENT, vcpu)) | |
12220 | return true; | |
12221 | ||
10dbdf98 | 12222 | return kvm_arch_dy_has_pending_interrupt(vcpu); |
17e433b5 WL |
12223 | } |
12224 | ||
199b5763 LM |
12225 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
12226 | { | |
b86bb11e WL |
12227 | if (vcpu->arch.guest_state_protected) |
12228 | return true; | |
12229 | ||
de63ad4c | 12230 | return vcpu->arch.preempted_in_kernel; |
199b5763 LM |
12231 | } |
12232 | ||
e1bfc245 SC |
12233 | unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) |
12234 | { | |
12235 | return kvm_rip_read(vcpu); | |
12236 | } | |
12237 | ||
b6d33834 | 12238 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
5736199a | 12239 | { |
b6d33834 | 12240 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
5736199a | 12241 | } |
78646121 GN |
12242 | |
12243 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) | |
12244 | { | |
b3646477 | 12245 | return static_call(kvm_x86_interrupt_allowed)(vcpu, false); |
78646121 | 12246 | } |
229456fc | 12247 | |
82b32774 | 12248 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
f92653ee | 12249 | { |
7ed9abfe TL |
12250 | /* Can't read the RIP when guest state is protected, just return 0 */ |
12251 | if (vcpu->arch.guest_state_protected) | |
12252 | return 0; | |
12253 | ||
82b32774 NA |
12254 | if (is_64_bit_mode(vcpu)) |
12255 | return kvm_rip_read(vcpu); | |
12256 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + | |
12257 | kvm_rip_read(vcpu)); | |
12258 | } | |
12259 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); | |
f92653ee | 12260 | |
82b32774 NA |
12261 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
12262 | { | |
12263 | return kvm_get_linear_rip(vcpu) == linear_rip; | |
f92653ee JK |
12264 | } |
12265 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); | |
12266 | ||
94fe45da JK |
12267 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
12268 | { | |
12269 | unsigned long rflags; | |
12270 | ||
b3646477 | 12271 | rflags = static_call(kvm_x86_get_rflags)(vcpu); |
94fe45da | 12272 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
c310bac5 | 12273 | rflags &= ~X86_EFLAGS_TF; |
94fe45da JK |
12274 | return rflags; |
12275 | } | |
12276 | EXPORT_SYMBOL_GPL(kvm_get_rflags); | |
12277 | ||
6addfc42 | 12278 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
94fe45da JK |
12279 | { |
12280 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && | |
f92653ee | 12281 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
c310bac5 | 12282 | rflags |= X86_EFLAGS_TF; |
b3646477 | 12283 | static_call(kvm_x86_set_rflags)(vcpu, rflags); |
6addfc42 PB |
12284 | } |
12285 | ||
12286 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
12287 | { | |
12288 | __kvm_set_rflags(vcpu, rflags); | |
3842d135 | 12289 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
94fe45da JK |
12290 | } |
12291 | EXPORT_SYMBOL_GPL(kvm_set_rflags); | |
12292 | ||
56028d08 GN |
12293 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
12294 | { | |
12295 | int r; | |
12296 | ||
44dd3ffa | 12297 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
f2e10669 | 12298 | work->wakeup_all) |
56028d08 GN |
12299 | return; |
12300 | ||
12301 | r = kvm_mmu_reload(vcpu); | |
12302 | if (unlikely(r)) | |
12303 | return; | |
12304 | ||
44dd3ffa | 12305 | if (!vcpu->arch.mmu->direct_map && |
d8dd54e0 | 12306 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
fb67e14f XG |
12307 | return; |
12308 | ||
7a02674d | 12309 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
56028d08 GN |
12310 | } |
12311 | ||
af585b92 GN |
12312 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
12313 | { | |
dd03bcaa PX |
12314 | BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU)); |
12315 | ||
af585b92 GN |
12316 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
12317 | } | |
12318 | ||
12319 | static inline u32 kvm_async_pf_next_probe(u32 key) | |
12320 | { | |
dd03bcaa | 12321 | return (key + 1) & (ASYNC_PF_PER_VCPU - 1); |
af585b92 GN |
12322 | } |
12323 | ||
12324 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
12325 | { | |
12326 | u32 key = kvm_async_pf_hash_fn(gfn); | |
12327 | ||
12328 | while (vcpu->arch.apf.gfns[key] != ~0) | |
12329 | key = kvm_async_pf_next_probe(key); | |
12330 | ||
12331 | vcpu->arch.apf.gfns[key] = gfn; | |
12332 | } | |
12333 | ||
12334 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) | |
12335 | { | |
12336 | int i; | |
12337 | u32 key = kvm_async_pf_hash_fn(gfn); | |
12338 | ||
dd03bcaa | 12339 | for (i = 0; i < ASYNC_PF_PER_VCPU && |
c7d28c24 XG |
12340 | (vcpu->arch.apf.gfns[key] != gfn && |
12341 | vcpu->arch.apf.gfns[key] != ~0); i++) | |
af585b92 GN |
12342 | key = kvm_async_pf_next_probe(key); |
12343 | ||
12344 | return key; | |
12345 | } | |
12346 | ||
12347 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
12348 | { | |
12349 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; | |
12350 | } | |
12351 | ||
12352 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
12353 | { | |
12354 | u32 i, j, k; | |
12355 | ||
12356 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); | |
0fd46044 PX |
12357 | |
12358 | if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) | |
12359 | return; | |
12360 | ||
af585b92 GN |
12361 | while (true) { |
12362 | vcpu->arch.apf.gfns[i] = ~0; | |
12363 | do { | |
12364 | j = kvm_async_pf_next_probe(j); | |
12365 | if (vcpu->arch.apf.gfns[j] == ~0) | |
12366 | return; | |
12367 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); | |
12368 | /* | |
12369 | * k lies cyclically in ]i,j] | |
12370 | * | i.k.j | | |
12371 | * |....j i.k.| or |.k..j i...| | |
12372 | */ | |
12373 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); | |
12374 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; | |
12375 | i = j; | |
12376 | } | |
12377 | } | |
12378 | ||
68fd66f1 | 12379 | static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu) |
7c90705b | 12380 | { |
68fd66f1 VK |
12381 | u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT; |
12382 | ||
12383 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, | |
12384 | sizeof(reason)); | |
12385 | } | |
12386 | ||
12387 | static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token) | |
12388 | { | |
2635b5c4 | 12389 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
4e335d9e | 12390 | |
2635b5c4 VK |
12391 | return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
12392 | &token, offset, sizeof(token)); | |
12393 | } | |
12394 | ||
12395 | static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu) | |
12396 | { | |
12397 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); | |
12398 | u32 val; | |
12399 | ||
12400 | if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, | |
12401 | &val, offset, sizeof(val))) | |
12402 | return false; | |
12403 | ||
12404 | return !val; | |
7c90705b GN |
12405 | } |
12406 | ||
1dfdb45e PB |
12407 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
12408 | { | |
57cb3bb0 PB |
12409 | |
12410 | if (!kvm_pv_async_pf_enabled(vcpu)) | |
1dfdb45e PB |
12411 | return false; |
12412 | ||
57cb3bb0 PB |
12413 | if (vcpu->arch.apf.send_user_only && |
12414 | static_call(kvm_x86_get_cpl)(vcpu) == 0) | |
1dfdb45e PB |
12415 | return false; |
12416 | ||
57cb3bb0 PB |
12417 | if (is_guest_mode(vcpu)) { |
12418 | /* | |
12419 | * L1 needs to opt into the special #PF vmexits that are | |
12420 | * used to deliver async page faults. | |
12421 | */ | |
12422 | return vcpu->arch.apf.delivery_as_pf_vmexit; | |
12423 | } else { | |
12424 | /* | |
12425 | * Play it safe in case the guest temporarily disables paging. | |
12426 | * The real mode IDT in particular is unlikely to have a #PF | |
12427 | * exception setup. | |
12428 | */ | |
12429 | return is_paging(vcpu); | |
12430 | } | |
1dfdb45e PB |
12431 | } |
12432 | ||
12433 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) | |
12434 | { | |
12435 | if (unlikely(!lapic_in_kernel(vcpu) || | |
12436 | kvm_event_needs_reinjection(vcpu) || | |
12437 | vcpu->arch.exception.pending)) | |
12438 | return false; | |
12439 | ||
12440 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) | |
12441 | return false; | |
12442 | ||
12443 | /* | |
12444 | * If interrupts are off we cannot even use an artificial | |
12445 | * halt state. | |
12446 | */ | |
c300ab9f | 12447 | return kvm_arch_interrupt_allowed(vcpu); |
1dfdb45e PB |
12448 | } |
12449 | ||
2a18b7e7 | 12450 | bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
af585b92 GN |
12451 | struct kvm_async_pf *work) |
12452 | { | |
6389ee94 AK |
12453 | struct x86_exception fault; |
12454 | ||
736c291c | 12455 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
af585b92 | 12456 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
7c90705b | 12457 | |
1dfdb45e | 12458 | if (kvm_can_deliver_async_pf(vcpu) && |
68fd66f1 | 12459 | !apf_put_user_notpresent(vcpu)) { |
6389ee94 AK |
12460 | fault.vector = PF_VECTOR; |
12461 | fault.error_code_valid = true; | |
12462 | fault.error_code = 0; | |
12463 | fault.nested_page_fault = false; | |
12464 | fault.address = work->arch.token; | |
adfe20fb | 12465 | fault.async_page_fault = true; |
6389ee94 | 12466 | kvm_inject_page_fault(vcpu, &fault); |
2a18b7e7 | 12467 | return true; |
1dfdb45e PB |
12468 | } else { |
12469 | /* | |
12470 | * It is not possible to deliver a paravirtualized asynchronous | |
12471 | * page fault, but putting the guest in an artificial halt state | |
12472 | * can be beneficial nevertheless: if an interrupt arrives, we | |
12473 | * can deliver it timely and perhaps the guest will schedule | |
12474 | * another process. When the instruction that triggered a page | |
12475 | * fault is retried, hopefully the page will be ready in the host. | |
12476 | */ | |
12477 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
2a18b7e7 | 12478 | return false; |
7c90705b | 12479 | } |
af585b92 GN |
12480 | } |
12481 | ||
12482 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, | |
12483 | struct kvm_async_pf *work) | |
12484 | { | |
2635b5c4 VK |
12485 | struct kvm_lapic_irq irq = { |
12486 | .delivery_mode = APIC_DM_FIXED, | |
12487 | .vector = vcpu->arch.apf.vec | |
12488 | }; | |
6389ee94 | 12489 | |
f2e10669 | 12490 | if (work->wakeup_all) |
7c90705b GN |
12491 | work->arch.token = ~0; /* broadcast wakeup */ |
12492 | else | |
12493 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); | |
736c291c | 12494 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
7c90705b | 12495 | |
2a18b7e7 VK |
12496 | if ((work->wakeup_all || work->notpresent_injected) && |
12497 | kvm_pv_async_pf_enabled(vcpu) && | |
557a961a VK |
12498 | !apf_put_user_ready(vcpu, work->arch.token)) { |
12499 | vcpu->arch.apf.pageready_pending = true; | |
2635b5c4 | 12500 | kvm_apic_set_irq(vcpu, &irq, NULL); |
557a961a | 12501 | } |
2635b5c4 | 12502 | |
e6d53e3b | 12503 | vcpu->arch.apf.halted = false; |
a4fa1635 | 12504 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
7c90705b GN |
12505 | } |
12506 | ||
557a961a VK |
12507 | void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) |
12508 | { | |
12509 | kvm_make_request(KVM_REQ_APF_READY, vcpu); | |
12510 | if (!vcpu->arch.apf.pageready_pending) | |
12511 | kvm_vcpu_kick(vcpu); | |
12512 | } | |
12513 | ||
7c0ade6c | 12514 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
7c90705b | 12515 | { |
2635b5c4 | 12516 | if (!kvm_pv_async_pf_enabled(vcpu)) |
7c90705b GN |
12517 | return true; |
12518 | else | |
2f15d027 | 12519 | return kvm_lapic_enabled(vcpu) && apf_pageready_slot_free(vcpu); |
af585b92 GN |
12520 | } |
12521 | ||
5544eb9b PB |
12522 | void kvm_arch_start_assignment(struct kvm *kvm) |
12523 | { | |
57ab8794 | 12524 | if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) |
e27bc044 | 12525 | static_call_cond(kvm_x86_pi_start_assignment)(kvm); |
5544eb9b PB |
12526 | } |
12527 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); | |
12528 | ||
12529 | void kvm_arch_end_assignment(struct kvm *kvm) | |
12530 | { | |
12531 | atomic_dec(&kvm->arch.assigned_device_count); | |
12532 | } | |
12533 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); | |
12534 | ||
12535 | bool kvm_arch_has_assigned_device(struct kvm *kvm) | |
12536 | { | |
12537 | return atomic_read(&kvm->arch.assigned_device_count); | |
12538 | } | |
12539 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); | |
12540 | ||
e0f0bbc5 AW |
12541 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
12542 | { | |
12543 | atomic_inc(&kvm->arch.noncoherent_dma_count); | |
12544 | } | |
12545 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); | |
12546 | ||
12547 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) | |
12548 | { | |
12549 | atomic_dec(&kvm->arch.noncoherent_dma_count); | |
12550 | } | |
12551 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); | |
12552 | ||
12553 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) | |
12554 | { | |
12555 | return atomic_read(&kvm->arch.noncoherent_dma_count); | |
12556 | } | |
12557 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); | |
12558 | ||
14717e20 AW |
12559 | bool kvm_arch_has_irq_bypass(void) |
12560 | { | |
92735b1b | 12561 | return true; |
14717e20 AW |
12562 | } |
12563 | ||
87276880 FW |
12564 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
12565 | struct irq_bypass_producer *prod) | |
12566 | { | |
12567 | struct kvm_kernel_irqfd *irqfd = | |
12568 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
2edd9cb7 | 12569 | int ret; |
87276880 | 12570 | |
14717e20 | 12571 | irqfd->producer = prod; |
2edd9cb7 | 12572 | kvm_arch_start_assignment(irqfd->kvm); |
e27bc044 | 12573 | ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, |
2edd9cb7 ZL |
12574 | prod->irq, irqfd->gsi, 1); |
12575 | ||
12576 | if (ret) | |
12577 | kvm_arch_end_assignment(irqfd->kvm); | |
87276880 | 12578 | |
2edd9cb7 | 12579 | return ret; |
87276880 FW |
12580 | } |
12581 | ||
12582 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, | |
12583 | struct irq_bypass_producer *prod) | |
12584 | { | |
12585 | int ret; | |
12586 | struct kvm_kernel_irqfd *irqfd = | |
12587 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
12588 | ||
87276880 FW |
12589 | WARN_ON(irqfd->producer != prod); |
12590 | irqfd->producer = NULL; | |
12591 | ||
12592 | /* | |
12593 | * When producer of consumer is unregistered, we change back to | |
12594 | * remapped mode, so we can re-use the current implementation | |
bb3541f1 | 12595 | * when the irq is masked/disabled or the consumer side (KVM |
87276880 FW |
12596 | * int this case doesn't want to receive the interrupts. |
12597 | */ | |
e27bc044 | 12598 | ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
87276880 FW |
12599 | if (ret) |
12600 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" | |
12601 | " fails: %d\n", irqfd->consumer.token, ret); | |
2edd9cb7 ZL |
12602 | |
12603 | kvm_arch_end_assignment(irqfd->kvm); | |
87276880 FW |
12604 | } |
12605 | ||
12606 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, | |
12607 | uint32_t guest_irq, bool set) | |
12608 | { | |
e27bc044 | 12609 | return static_call(kvm_x86_pi_update_irte)(kvm, host_irq, guest_irq, set); |
87276880 FW |
12610 | } |
12611 | ||
515a0c79 LM |
12612 | bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, |
12613 | struct kvm_kernel_irq_routing_entry *new) | |
12614 | { | |
12615 | if (new->type != KVM_IRQ_ROUTING_MSI) | |
12616 | return true; | |
12617 | ||
12618 | return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); | |
12619 | } | |
12620 | ||
52004014 FW |
12621 | bool kvm_vector_hashing_enabled(void) |
12622 | { | |
12623 | return vector_hashing; | |
12624 | } | |
52004014 | 12625 | |
2d5ba19b MT |
12626 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
12627 | { | |
12628 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; | |
12629 | } | |
12630 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); | |
12631 | ||
841c2be0 ML |
12632 | |
12633 | int kvm_spec_ctrl_test_value(u64 value) | |
6441fa61 | 12634 | { |
841c2be0 ML |
12635 | /* |
12636 | * test that setting IA32_SPEC_CTRL to given value | |
12637 | * is allowed by the host processor | |
12638 | */ | |
6441fa61 | 12639 | |
841c2be0 ML |
12640 | u64 saved_value; |
12641 | unsigned long flags; | |
12642 | int ret = 0; | |
6441fa61 | 12643 | |
841c2be0 | 12644 | local_irq_save(flags); |
6441fa61 | 12645 | |
841c2be0 ML |
12646 | if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value)) |
12647 | ret = 1; | |
12648 | else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value)) | |
12649 | ret = 1; | |
12650 | else | |
12651 | wrmsrl(MSR_IA32_SPEC_CTRL, saved_value); | |
6441fa61 | 12652 | |
841c2be0 | 12653 | local_irq_restore(flags); |
6441fa61 | 12654 | |
841c2be0 | 12655 | return ret; |
6441fa61 | 12656 | } |
841c2be0 | 12657 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value); |
2d5ba19b | 12658 | |
89786147 MG |
12659 | void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code) |
12660 | { | |
1f5a21ee | 12661 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
89786147 | 12662 | struct x86_exception fault; |
5b22bbe7 | 12663 | u64 access = error_code & |
19cf4b7e | 12664 | (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK); |
89786147 MG |
12665 | |
12666 | if (!(error_code & PFERR_PRESENT_MASK) || | |
1f5a21ee | 12667 | mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != UNMAPPED_GVA) { |
89786147 MG |
12668 | /* |
12669 | * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page | |
12670 | * tables probably do not match the TLB. Just proceed | |
12671 | * with the error code that the processor gave. | |
12672 | */ | |
12673 | fault.vector = PF_VECTOR; | |
12674 | fault.error_code_valid = true; | |
12675 | fault.error_code = error_code; | |
12676 | fault.nested_page_fault = false; | |
12677 | fault.address = gva; | |
12678 | } | |
12679 | vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); | |
6441fa61 | 12680 | } |
89786147 | 12681 | EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error); |
2d5ba19b | 12682 | |
3f3393b3 BM |
12683 | /* |
12684 | * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns | |
12685 | * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value | |
12686 | * indicates whether exit to userspace is needed. | |
12687 | */ | |
12688 | int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r, | |
12689 | struct x86_exception *e) | |
12690 | { | |
12691 | if (r == X86EMUL_PROPAGATE_FAULT) { | |
12692 | kvm_inject_emulated_page_fault(vcpu, e); | |
12693 | return 1; | |
12694 | } | |
12695 | ||
12696 | /* | |
12697 | * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED | |
12698 | * while handling a VMX instruction KVM could've handled the request | |
12699 | * correctly by exiting to userspace and performing I/O but there | |
12700 | * doesn't seem to be a real use-case behind such requests, just return | |
12701 | * KVM_EXIT_INTERNAL_ERROR for now. | |
12702 | */ | |
e615e355 | 12703 | kvm_prepare_emulation_failure_exit(vcpu); |
3f3393b3 BM |
12704 | |
12705 | return 0; | |
12706 | } | |
12707 | EXPORT_SYMBOL_GPL(kvm_handle_memory_failure); | |
12708 | ||
9715092f BM |
12709 | int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva) |
12710 | { | |
12711 | bool pcid_enabled; | |
12712 | struct x86_exception e; | |
9715092f BM |
12713 | struct { |
12714 | u64 pcid; | |
12715 | u64 gla; | |
12716 | } operand; | |
12717 | int r; | |
12718 | ||
12719 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); | |
12720 | if (r != X86EMUL_CONTINUE) | |
12721 | return kvm_handle_memory_failure(vcpu, r, &e); | |
12722 | ||
12723 | if (operand.pcid >> 12 != 0) { | |
12724 | kvm_inject_gp(vcpu, 0); | |
12725 | return 1; | |
12726 | } | |
12727 | ||
12728 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); | |
12729 | ||
12730 | switch (type) { | |
12731 | case INVPCID_TYPE_INDIV_ADDR: | |
12732 | if ((!pcid_enabled && (operand.pcid != 0)) || | |
12733 | is_noncanonical_address(operand.gla, vcpu)) { | |
12734 | kvm_inject_gp(vcpu, 0); | |
12735 | return 1; | |
12736 | } | |
12737 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); | |
12738 | return kvm_skip_emulated_instruction(vcpu); | |
12739 | ||
12740 | case INVPCID_TYPE_SINGLE_CTXT: | |
12741 | if (!pcid_enabled && (operand.pcid != 0)) { | |
12742 | kvm_inject_gp(vcpu, 0); | |
12743 | return 1; | |
12744 | } | |
12745 | ||
21823fbd | 12746 | kvm_invalidate_pcid(vcpu, operand.pcid); |
9715092f BM |
12747 | return kvm_skip_emulated_instruction(vcpu); |
12748 | ||
12749 | case INVPCID_TYPE_ALL_NON_GLOBAL: | |
12750 | /* | |
12751 | * Currently, KVM doesn't mark global entries in the shadow | |
12752 | * page tables, so a non-global flush just degenerates to a | |
12753 | * global flush. If needed, we could optimize this later by | |
12754 | * keeping track of global entries in shadow page tables. | |
12755 | */ | |
12756 | ||
12757 | fallthrough; | |
12758 | case INVPCID_TYPE_ALL_INCL_GLOBAL: | |
28f28d45 | 12759 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
9715092f BM |
12760 | return kvm_skip_emulated_instruction(vcpu); |
12761 | ||
12762 | default: | |
796c83c5 VS |
12763 | kvm_inject_gp(vcpu, 0); |
12764 | return 1; | |
9715092f BM |
12765 | } |
12766 | } | |
12767 | EXPORT_SYMBOL_GPL(kvm_handle_invpcid); | |
12768 | ||
8f423a80 TL |
12769 | static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu) |
12770 | { | |
12771 | struct kvm_run *run = vcpu->run; | |
12772 | struct kvm_mmio_fragment *frag; | |
12773 | unsigned int len; | |
12774 | ||
12775 | BUG_ON(!vcpu->mmio_needed); | |
12776 | ||
12777 | /* Complete previous fragment */ | |
12778 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; | |
12779 | len = min(8u, frag->len); | |
12780 | if (!vcpu->mmio_is_write) | |
12781 | memcpy(frag->data, run->mmio.data, len); | |
12782 | ||
12783 | if (frag->len <= 8) { | |
12784 | /* Switch to the next fragment. */ | |
12785 | frag++; | |
12786 | vcpu->mmio_cur_fragment++; | |
12787 | } else { | |
12788 | /* Go forward to the next mmio piece. */ | |
12789 | frag->data += len; | |
12790 | frag->gpa += len; | |
12791 | frag->len -= len; | |
12792 | } | |
12793 | ||
12794 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { | |
12795 | vcpu->mmio_needed = 0; | |
12796 | ||
12797 | // VMG change, at this point, we're always done | |
12798 | // RIP has already been advanced | |
12799 | return 1; | |
12800 | } | |
12801 | ||
12802 | // More MMIO is needed | |
12803 | run->mmio.phys_addr = frag->gpa; | |
12804 | run->mmio.len = min(8u, frag->len); | |
12805 | run->mmio.is_write = vcpu->mmio_is_write; | |
12806 | if (run->mmio.is_write) | |
12807 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); | |
12808 | run->exit_reason = KVM_EXIT_MMIO; | |
12809 | ||
12810 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
12811 | ||
12812 | return 0; | |
12813 | } | |
12814 | ||
12815 | int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, | |
12816 | void *data) | |
12817 | { | |
12818 | int handled; | |
12819 | struct kvm_mmio_fragment *frag; | |
12820 | ||
12821 | if (!data) | |
12822 | return -EINVAL; | |
12823 | ||
12824 | handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data); | |
12825 | if (handled == bytes) | |
12826 | return 1; | |
12827 | ||
12828 | bytes -= handled; | |
12829 | gpa += handled; | |
12830 | data += handled; | |
12831 | ||
12832 | /*TODO: Check if need to increment number of frags */ | |
12833 | frag = vcpu->mmio_fragments; | |
12834 | vcpu->mmio_nr_fragments = 1; | |
12835 | frag->len = bytes; | |
12836 | frag->gpa = gpa; | |
12837 | frag->data = data; | |
12838 | ||
12839 | vcpu->mmio_needed = 1; | |
12840 | vcpu->mmio_cur_fragment = 0; | |
12841 | ||
12842 | vcpu->run->mmio.phys_addr = gpa; | |
12843 | vcpu->run->mmio.len = min(8u, frag->len); | |
12844 | vcpu->run->mmio.is_write = 1; | |
12845 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); | |
12846 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
12847 | ||
12848 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
12849 | ||
12850 | return 0; | |
12851 | } | |
12852 | EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_write); | |
12853 | ||
12854 | int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, | |
12855 | void *data) | |
12856 | { | |
12857 | int handled; | |
12858 | struct kvm_mmio_fragment *frag; | |
12859 | ||
12860 | if (!data) | |
12861 | return -EINVAL; | |
12862 | ||
12863 | handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data); | |
12864 | if (handled == bytes) | |
12865 | return 1; | |
12866 | ||
12867 | bytes -= handled; | |
12868 | gpa += handled; | |
12869 | data += handled; | |
12870 | ||
12871 | /*TODO: Check if need to increment number of frags */ | |
12872 | frag = vcpu->mmio_fragments; | |
12873 | vcpu->mmio_nr_fragments = 1; | |
12874 | frag->len = bytes; | |
12875 | frag->gpa = gpa; | |
12876 | frag->data = data; | |
12877 | ||
12878 | vcpu->mmio_needed = 1; | |
12879 | vcpu->mmio_cur_fragment = 0; | |
12880 | ||
12881 | vcpu->run->mmio.phys_addr = gpa; | |
12882 | vcpu->run->mmio.len = min(8u, frag->len); | |
12883 | vcpu->run->mmio.is_write = 0; | |
12884 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
12885 | ||
12886 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; | |
12887 | ||
12888 | return 0; | |
12889 | } | |
12890 | EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read); | |
12891 | ||
7ed9abfe | 12892 | static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size, |
95e16b47 PB |
12893 | unsigned int port); |
12894 | ||
12895 | static int complete_sev_es_emulated_outs(struct kvm_vcpu *vcpu) | |
7ed9abfe | 12896 | { |
95e16b47 PB |
12897 | int size = vcpu->arch.pio.size; |
12898 | int port = vcpu->arch.pio.port; | |
12899 | ||
12900 | vcpu->arch.pio.count = 0; | |
12901 | if (vcpu->arch.sev_pio_count) | |
12902 | return kvm_sev_es_outs(vcpu, size, port); | |
12903 | return 1; | |
12904 | } | |
12905 | ||
12906 | static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size, | |
12907 | unsigned int port) | |
12908 | { | |
12909 | for (;;) { | |
12910 | unsigned int count = | |
12911 | min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); | |
12912 | int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); | |
12913 | ||
12914 | /* memcpy done already by emulator_pio_out. */ | |
12915 | vcpu->arch.sev_pio_count -= count; | |
12916 | vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size; | |
12917 | if (!ret) | |
12918 | break; | |
7ed9abfe | 12919 | |
ea724ea4 | 12920 | /* Emulation done by the kernel. */ |
95e16b47 PB |
12921 | if (!vcpu->arch.sev_pio_count) |
12922 | return 1; | |
ea724ea4 | 12923 | } |
7ed9abfe | 12924 | |
95e16b47 | 12925 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; |
7ed9abfe TL |
12926 | return 0; |
12927 | } | |
12928 | ||
95e16b47 PB |
12929 | static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, |
12930 | unsigned int port); | |
12931 | ||
12932 | static void advance_sev_es_emulated_ins(struct kvm_vcpu *vcpu) | |
12933 | { | |
12934 | unsigned count = vcpu->arch.pio.count; | |
12935 | complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); | |
12936 | vcpu->arch.sev_pio_count -= count; | |
12937 | vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size; | |
12938 | } | |
12939 | ||
4fa4b38d PB |
12940 | static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) |
12941 | { | |
95e16b47 PB |
12942 | int size = vcpu->arch.pio.size; |
12943 | int port = vcpu->arch.pio.port; | |
4fa4b38d | 12944 | |
95e16b47 PB |
12945 | advance_sev_es_emulated_ins(vcpu); |
12946 | if (vcpu->arch.sev_pio_count) | |
12947 | return kvm_sev_es_ins(vcpu, size, port); | |
4fa4b38d PB |
12948 | return 1; |
12949 | } | |
12950 | ||
7ed9abfe | 12951 | static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, |
95e16b47 | 12952 | unsigned int port) |
7ed9abfe | 12953 | { |
95e16b47 PB |
12954 | for (;;) { |
12955 | unsigned int count = | |
12956 | min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); | |
12957 | if (!__emulator_pio_in(vcpu, size, port, count)) | |
12958 | break; | |
7ed9abfe | 12959 | |
ea724ea4 | 12960 | /* Emulation done by the kernel. */ |
95e16b47 PB |
12961 | advance_sev_es_emulated_ins(vcpu); |
12962 | if (!vcpu->arch.sev_pio_count) | |
12963 | return 1; | |
7ed9abfe TL |
12964 | } |
12965 | ||
ea724ea4 | 12966 | vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; |
7ed9abfe TL |
12967 | return 0; |
12968 | } | |
12969 | ||
12970 | int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, | |
12971 | unsigned int port, void *data, unsigned int count, | |
12972 | int in) | |
12973 | { | |
ea724ea4 | 12974 | vcpu->arch.sev_pio_data = data; |
95e16b47 PB |
12975 | vcpu->arch.sev_pio_count = count; |
12976 | return in ? kvm_sev_es_ins(vcpu, size, port) | |
12977 | : kvm_sev_es_outs(vcpu, size, port); | |
7ed9abfe TL |
12978 | } |
12979 | EXPORT_SYMBOL_GPL(kvm_sev_es_string_io); | |
12980 | ||
d95df951 | 12981 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry); |
229456fc | 12982 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
931c33b1 | 12983 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
229456fc MT |
12984 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
12985 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); | |
12986 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); | |
12987 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); | |
0ac406de | 12988 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
d8cabddf | 12989 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
17897f36 | 12990 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
236649de | 12991 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
5497b955 | 12992 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
ec1ff790 | 12993 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
532a46b9 | 12994 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
2e554e8d | 12995 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
489223ed | 12996 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
4f75bcc3 | 12997 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
843e4330 | 12998 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
efc64404 | 12999 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
18f40c53 SS |
13000 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
13001 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); | |
ab56f8e6 | 13002 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
8e819d75 | 13003 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_accept_irq); |
d523ab6b TL |
13004 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); |
13005 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); | |
59e38b58 TL |
13006 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); |
13007 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); |