1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
4 * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
13 #include <linux/kvm_host.h>
15 #include <trace/events/kvm.h>
16 #include <xen/interface/xen.h>
20 DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
22 static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn)
24 gpa_t gpa = gfn_to_gpa(gfn);
25 int wc_ofs, sec_hi_ofs;
27 int idx = srcu_read_lock(&kvm->srcu);
29 ret = kvm_gfn_to_hva_cache_init(kvm, &kvm->arch.xen.shinfo_cache,
34 kvm->arch.xen.shinfo_set = true;
36 /* Paranoia checks on the 32-bit struct layout */
37 BUILD_BUG_ON(offsetof(struct compat_shared_info, wc) != 0x900);
38 BUILD_BUG_ON(offsetof(struct compat_shared_info, arch.wc_sec_hi) != 0x924);
39 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
41 /* 32-bit location by default */
42 wc_ofs = offsetof(struct compat_shared_info, wc);
43 sec_hi_ofs = offsetof(struct compat_shared_info, arch.wc_sec_hi);
46 /* Paranoia checks on the 64-bit struct layout */
47 BUILD_BUG_ON(offsetof(struct shared_info, wc) != 0xc00);
48 BUILD_BUG_ON(offsetof(struct shared_info, wc_sec_hi) != 0xc0c);
50 if (kvm->arch.xen.long_mode) {
51 wc_ofs = offsetof(struct shared_info, wc);
52 sec_hi_ofs = offsetof(struct shared_info, wc_sec_hi);
56 kvm_write_wall_clock(kvm, gpa + wc_ofs, sec_hi_ofs - wc_ofs);
57 kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE);
60 srcu_read_unlock(&kvm->srcu, idx);
64 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
69 * If the global upcall vector (HVMIRQ_callback_vector) is set and
70 * the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
72 struct gfn_to_hva_cache *ghc = &v->arch.xen.vcpu_info_cache;
73 struct kvm_memslots *slots = kvm_memslots(v->kvm);
74 unsigned int offset = offsetof(struct vcpu_info, evtchn_upcall_pending);
76 /* No need for compat handling here */
77 BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
78 offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
79 BUILD_BUG_ON(sizeof(rc) !=
80 sizeof(((struct vcpu_info *)0)->evtchn_upcall_pending));
81 BUILD_BUG_ON(sizeof(rc) !=
82 sizeof(((struct compat_vcpu_info *)0)->evtchn_upcall_pending));
85 * For efficiency, this mirrors the checks for using the valid
86 * cache in kvm_read_guest_offset_cached(), but just uses
87 * __get_user() instead. And falls back to the slow path.
89 if (likely(slots->generation == ghc->generation &&
90 !kvm_is_error_hva(ghc->hva) && ghc->memslot)) {
92 __get_user(rc, (u8 __user *)ghc->hva + offset);
95 kvm_read_guest_offset_cached(v->kvm, ghc, &rc, offset,
102 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
106 mutex_lock(&kvm->lock);
108 switch (data->type) {
109 case KVM_XEN_ATTR_TYPE_LONG_MODE:
110 if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
113 kvm->arch.xen.long_mode = !!data->u.long_mode;
118 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
119 r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
123 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
124 if (data->u.vector < 0x10)
127 kvm->arch.xen.upcall_vector = data->u.vector;
136 mutex_unlock(&kvm->lock);
140 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
144 mutex_lock(&kvm->lock);
146 switch (data->type) {
147 case KVM_XEN_ATTR_TYPE_LONG_MODE:
148 data->u.long_mode = kvm->arch.xen.long_mode;
152 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
153 if (kvm->arch.xen.shinfo_set) {
154 data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_cache.gpa);
159 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
160 data->u.vector = kvm->arch.xen.upcall_vector;
168 mutex_unlock(&kvm->lock);
172 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
174 int idx, r = -ENOENT;
176 mutex_lock(&vcpu->kvm->lock);
177 idx = srcu_read_lock(&vcpu->kvm->srcu);
179 switch (data->type) {
180 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
181 /* No compat necessary here. */
182 BUILD_BUG_ON(sizeof(struct vcpu_info) !=
183 sizeof(struct compat_vcpu_info));
185 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
186 &vcpu->arch.xen.vcpu_info_cache,
188 sizeof(struct vcpu_info));
190 vcpu->arch.xen.vcpu_info_set = true;
191 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
195 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
196 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
197 &vcpu->arch.xen.vcpu_time_info_cache,
199 sizeof(struct pvclock_vcpu_time_info));
201 vcpu->arch.xen.vcpu_time_info_set = true;
202 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
210 srcu_read_unlock(&vcpu->kvm->srcu, idx);
211 mutex_unlock(&vcpu->kvm->lock);
215 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
219 mutex_lock(&vcpu->kvm->lock);
221 switch (data->type) {
222 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
223 if (vcpu->arch.xen.vcpu_info_set) {
224 data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
229 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
230 if (vcpu->arch.xen.vcpu_time_info_set) {
231 data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
240 mutex_unlock(&vcpu->kvm->lock);
244 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
246 struct kvm *kvm = vcpu->kvm;
247 u32 page_num = data & ~PAGE_MASK;
248 u64 page_addr = data & PAGE_MASK;
249 bool lm = is_long_mode(vcpu);
251 /* Latch long_mode for shared_info pages etc. */
252 vcpu->kvm->arch.xen.long_mode = lm;
255 * If Xen hypercall intercept is enabled, fill the hypercall
256 * page with VMCALL/VMMCALL instructions since that's what
257 * we catch. Else the VMM has provided the hypercall pages
258 * with instructions of its own choosing, so use those.
260 if (kvm_xen_hypercall_enabled(kvm)) {
267 /* mov imm32, %eax */
268 instructions[0] = 0xb8;
270 /* vmcall / vmmcall */
271 kvm_x86_ops.patch_hypercall(vcpu, instructions + 5);
274 instructions[8] = 0xc3;
277 memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
279 for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
280 *(u32 *)&instructions[1] = i;
281 if (kvm_vcpu_write_guest(vcpu,
282 page_addr + (i * sizeof(instructions)),
283 instructions, sizeof(instructions)))
288 * Note, truncation is a non-issue as 'lm' is guaranteed to be
289 * false for a 32-bit kernel, i.e. when hva_t is only 4 bytes.
291 hva_t blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
292 : kvm->arch.xen_hvm_config.blob_addr_32;
293 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
294 : kvm->arch.xen_hvm_config.blob_size_32;
297 if (page_num >= blob_size)
300 blob_addr += page_num * PAGE_SIZE;
302 page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
304 return PTR_ERR(page);
306 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
314 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
316 if (xhc->flags & ~KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL)
320 * With hypercall interception the kernel generates its own
321 * hypercall page so it must not be provided.
323 if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
324 (xhc->blob_addr_32 || xhc->blob_addr_64 ||
325 xhc->blob_size_32 || xhc->blob_size_64))
328 mutex_lock(&kvm->lock);
330 if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
331 static_branch_inc(&kvm_xen_enabled.key);
332 else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
333 static_branch_slow_dec_deferred(&kvm_xen_enabled);
335 memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
337 mutex_unlock(&kvm->lock);
341 void kvm_xen_destroy_vm(struct kvm *kvm)
343 if (kvm->arch.xen_hvm_config.msr)
344 static_branch_slow_dec_deferred(&kvm_xen_enabled);
347 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
349 kvm_rax_write(vcpu, result);
350 return kvm_skip_emulated_instruction(vcpu);
353 static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
355 struct kvm_run *run = vcpu->run;
357 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
360 return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
363 int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
366 u64 input, params[6];
368 input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
370 /* Hyper-V hypercalls get bit 31 set in EAX */
371 if ((input & 0x80000000) &&
372 kvm_hv_hypercall_enabled(vcpu))
373 return kvm_hv_hypercall(vcpu);
375 longmode = is_64_bit_mode(vcpu);
377 params[0] = (u32)kvm_rbx_read(vcpu);
378 params[1] = (u32)kvm_rcx_read(vcpu);
379 params[2] = (u32)kvm_rdx_read(vcpu);
380 params[3] = (u32)kvm_rsi_read(vcpu);
381 params[4] = (u32)kvm_rdi_read(vcpu);
382 params[5] = (u32)kvm_rbp_read(vcpu);
386 params[0] = (u64)kvm_rdi_read(vcpu);
387 params[1] = (u64)kvm_rsi_read(vcpu);
388 params[2] = (u64)kvm_rdx_read(vcpu);
389 params[3] = (u64)kvm_r10_read(vcpu);
390 params[4] = (u64)kvm_r8_read(vcpu);
391 params[5] = (u64)kvm_r9_read(vcpu);
394 trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
395 params[3], params[4], params[5]);
397 vcpu->run->exit_reason = KVM_EXIT_XEN;
398 vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
399 vcpu->run->xen.u.hcall.longmode = longmode;
400 vcpu->run->xen.u.hcall.cpl = kvm_x86_ops.get_cpl(vcpu);
401 vcpu->run->xen.u.hcall.input = input;
402 vcpu->run->xen.u.hcall.params[0] = params[0];
403 vcpu->run->xen.u.hcall.params[1] = params[1];
404 vcpu->run->xen.u.hcall.params[2] = params[2];
405 vcpu->run->xen.u.hcall.params[3] = params[3];
406 vcpu->run->xen.u.hcall.params[4] = params[4];
407 vcpu->run->xen.u.hcall.params[5] = params[5];
408 vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
409 vcpu->arch.complete_userspace_io =
410 kvm_xen_hypercall_complete_userspace;