KVM: x86/xen: Add event channel interrupt vector upcall
[linux-2.6-microblaze.git] / arch / x86 / kvm / xen.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
4  * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5  *
6  * KVM Xen emulation
7  */
8
9 #include "x86.h"
10 #include "xen.h"
11 #include "hyperv.h"
12
13 #include <linux/kvm_host.h>
14
15 #include <trace/events/kvm.h>
16 #include <xen/interface/xen.h>
17
18 #include "trace.h"
19
20 DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
21
22 static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn)
23 {
24         gpa_t gpa = gfn_to_gpa(gfn);
25         int wc_ofs, sec_hi_ofs;
26         int ret;
27         int idx = srcu_read_lock(&kvm->srcu);
28
29         ret = kvm_gfn_to_hva_cache_init(kvm, &kvm->arch.xen.shinfo_cache,
30                                         gpa, PAGE_SIZE);
31         if (ret)
32                 goto out;
33
34         kvm->arch.xen.shinfo_set = true;
35
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);
40
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);
44
45 #ifdef CONFIG_X86_64
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);
49
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);
53         }
54 #endif
55
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);
58
59 out:
60         srcu_read_unlock(&kvm->srcu, idx);
61         return ret;
62 }
63
64 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
65 {
66         u8 rc = 0;
67
68         /*
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.
71          */
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);
75
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));
83
84         /*
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.
88          */
89         if (likely(slots->generation == ghc->generation &&
90                    !kvm_is_error_hva(ghc->hva) && ghc->memslot)) {
91                 /* Fast path */
92                 __get_user(rc, (u8 __user *)ghc->hva + offset);
93         } else {
94                 /* Slow path */
95                 kvm_read_guest_offset_cached(v->kvm, ghc, &rc, offset,
96                                              sizeof(rc));
97         }
98
99         return rc;
100 }
101
102 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
103 {
104         int r = -ENOENT;
105
106         mutex_lock(&kvm->lock);
107
108         mutex_unlock(&kvm->lock);
109
110         switch (data->type) {
111         case KVM_XEN_ATTR_TYPE_LONG_MODE:
112                 if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
113                         r = -EINVAL;
114                 } else {
115                         kvm->arch.xen.long_mode = !!data->u.long_mode;
116                         r = 0;
117                 }
118                 break;
119
120         case KVM_XEN_ATTR_TYPE_SHARED_INFO:
121                 r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
122                 break;
123
124
125         case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
126                 if (data->u.vector < 0x10)
127                         r = -EINVAL;
128                 else {
129                         kvm->arch.xen.upcall_vector = data->u.vector;
130                         r = 0;
131                 }
132                 break;
133
134         default:
135                 break;
136         }
137
138         mutex_unlock(&kvm->lock);
139         return r;
140 }
141
142 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
143 {
144         int r = -ENOENT;
145
146         mutex_lock(&kvm->lock);
147
148         switch (data->type) {
149         case KVM_XEN_ATTR_TYPE_LONG_MODE:
150                 data->u.long_mode = kvm->arch.xen.long_mode;
151                 r = 0;
152                 break;
153
154         case KVM_XEN_ATTR_TYPE_SHARED_INFO:
155                 if (kvm->arch.xen.shinfo_set) {
156                         data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_cache.gpa);
157                         r = 0;
158                 }
159                 break;
160
161         case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
162                 data->u.vector = kvm->arch.xen.upcall_vector;
163                 r = 0;
164                 break;
165
166         default:
167                 break;
168         }
169
170         mutex_unlock(&kvm->lock);
171         return r;
172 }
173
174 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
175 {
176         int idx, r = -ENOENT;
177
178         mutex_lock(&vcpu->kvm->lock);
179         idx = srcu_read_lock(&vcpu->kvm->srcu);
180
181         switch (data->type) {
182         case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
183                 /* No compat necessary here. */
184                 BUILD_BUG_ON(sizeof(struct vcpu_info) !=
185                              sizeof(struct compat_vcpu_info));
186
187                 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
188                                               &vcpu->arch.xen.vcpu_info_cache,
189                                               data->u.gpa,
190                                               sizeof(struct vcpu_info));
191                 if (!r) {
192                         vcpu->arch.xen.vcpu_info_set = true;
193                         kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
194                 }
195                 break;
196
197         case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
198                 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
199                                               &vcpu->arch.xen.vcpu_time_info_cache,
200                                               data->u.gpa,
201                                               sizeof(struct pvclock_vcpu_time_info));
202                 if (!r) {
203                         vcpu->arch.xen.vcpu_time_info_set = true;
204                         kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
205                 }
206                 break;
207
208         default:
209                 break;
210         }
211
212         srcu_read_unlock(&vcpu->kvm->srcu, idx);
213         mutex_unlock(&vcpu->kvm->lock);
214         return r;
215 }
216
217 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
218 {
219         int r = -ENOENT;
220
221         mutex_lock(&vcpu->kvm->lock);
222
223         switch (data->type) {
224         case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
225                 if (vcpu->arch.xen.vcpu_info_set) {
226                         data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
227                         r = 0;
228                 }
229                 break;
230
231         case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
232                 if (vcpu->arch.xen.vcpu_time_info_set) {
233                         data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
234                         r = 0;
235                 }
236                 break;
237
238         default:
239                 break;
240         }
241
242         mutex_unlock(&vcpu->kvm->lock);
243         return r;
244 }
245
246 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
247 {
248         struct kvm *kvm = vcpu->kvm;
249         u32 page_num = data & ~PAGE_MASK;
250         u64 page_addr = data & PAGE_MASK;
251         bool lm = is_long_mode(vcpu);
252
253         /* Latch long_mode for shared_info pages etc. */
254         vcpu->kvm->arch.xen.long_mode = lm;
255
256         /*
257          * If Xen hypercall intercept is enabled, fill the hypercall
258          * page with VMCALL/VMMCALL instructions since that's what
259          * we catch. Else the VMM has provided the hypercall pages
260          * with instructions of its own choosing, so use those.
261          */
262         if (kvm_xen_hypercall_enabled(kvm)) {
263                 u8 instructions[32];
264                 int i;
265
266                 if (page_num)
267                         return 1;
268
269                 /* mov imm32, %eax */
270                 instructions[0] = 0xb8;
271
272                 /* vmcall / vmmcall */
273                 kvm_x86_ops.patch_hypercall(vcpu, instructions + 5);
274
275                 /* ret */
276                 instructions[8] = 0xc3;
277
278                 /* int3 to pad */
279                 memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
280
281                 for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
282                         *(u32 *)&instructions[1] = i;
283                         if (kvm_vcpu_write_guest(vcpu,
284                                                  page_addr + (i * sizeof(instructions)),
285                                                  instructions, sizeof(instructions)))
286                                 return 1;
287                 }
288         } else {
289                 u64 blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
290                                    : kvm->arch.xen_hvm_config.blob_addr_32;
291                 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
292                                   : kvm->arch.xen_hvm_config.blob_size_32;
293                 u8 *page;
294
295                 if (page_num >= blob_size)
296                         return 1;
297
298                 blob_addr += page_num * PAGE_SIZE;
299
300                 page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
301                 if (IS_ERR(page))
302                         return PTR_ERR(page);
303
304                 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
305                         kfree(page);
306                         return 1;
307                 }
308         }
309         return 0;
310 }
311
312 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
313 {
314         if (xhc->flags & ~KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL)
315                 return -EINVAL;
316
317         /*
318          * With hypercall interception the kernel generates its own
319          * hypercall page so it must not be provided.
320          */
321         if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
322             (xhc->blob_addr_32 || xhc->blob_addr_64 ||
323              xhc->blob_size_32 || xhc->blob_size_64))
324                 return -EINVAL;
325
326         mutex_lock(&kvm->lock);
327
328         if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
329                 static_branch_inc(&kvm_xen_enabled.key);
330         else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
331                 static_branch_slow_dec_deferred(&kvm_xen_enabled);
332
333         memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
334
335         mutex_unlock(&kvm->lock);
336         return 0;
337 }
338
339 void kvm_xen_destroy_vm(struct kvm *kvm)
340 {
341         if (kvm->arch.xen_hvm_config.msr)
342                 static_branch_slow_dec_deferred(&kvm_xen_enabled);
343 }
344
345 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
346 {
347         kvm_rax_write(vcpu, result);
348         return kvm_skip_emulated_instruction(vcpu);
349 }
350
351 static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
352 {
353         struct kvm_run *run = vcpu->run;
354
355         if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
356                 return 1;
357
358         return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
359 }
360
361 int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
362 {
363         bool longmode;
364         u64 input, params[6];
365
366         input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
367
368         /* Hyper-V hypercalls get bit 31 set in EAX */
369         if ((input & 0x80000000) &&
370             kvm_hv_hypercall_enabled(vcpu->kvm))
371                 return kvm_hv_hypercall(vcpu);
372
373         longmode = is_64_bit_mode(vcpu);
374         if (!longmode) {
375                 params[0] = (u32)kvm_rbx_read(vcpu);
376                 params[1] = (u32)kvm_rcx_read(vcpu);
377                 params[2] = (u32)kvm_rdx_read(vcpu);
378                 params[3] = (u32)kvm_rsi_read(vcpu);
379                 params[4] = (u32)kvm_rdi_read(vcpu);
380                 params[5] = (u32)kvm_rbp_read(vcpu);
381         }
382 #ifdef CONFIG_X86_64
383         else {
384                 params[0] = (u64)kvm_rdi_read(vcpu);
385                 params[1] = (u64)kvm_rsi_read(vcpu);
386                 params[2] = (u64)kvm_rdx_read(vcpu);
387                 params[3] = (u64)kvm_r10_read(vcpu);
388                 params[4] = (u64)kvm_r8_read(vcpu);
389                 params[5] = (u64)kvm_r9_read(vcpu);
390         }
391 #endif
392         trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
393                                 params[3], params[4], params[5]);
394
395         vcpu->run->exit_reason = KVM_EXIT_XEN;
396         vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
397         vcpu->run->xen.u.hcall.longmode = longmode;
398         vcpu->run->xen.u.hcall.cpl = kvm_x86_ops.get_cpl(vcpu);
399         vcpu->run->xen.u.hcall.input = input;
400         vcpu->run->xen.u.hcall.params[0] = params[0];
401         vcpu->run->xen.u.hcall.params[1] = params[1];
402         vcpu->run->xen.u.hcall.params[2] = params[2];
403         vcpu->run->xen.u.hcall.params[3] = params[3];
404         vcpu->run->xen.u.hcall.params[4] = params[4];
405         vcpu->run->xen.u.hcall.params[5] = params[5];
406         vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
407         vcpu->arch.complete_userspace_io =
408                 kvm_xen_hypercall_complete_userspace;
409
410         return 0;
411 }