]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/kvm/xen.c
KVM: xen: do not use struct gfn_to_hva_cache
[mirror_ubuntu-jammy-kernel.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 #include <linux/sched/stat.h>
15
16 #include <trace/events/kvm.h>
17 #include <xen/interface/xen.h>
18 #include <xen/interface/vcpu.h>
19
20 #include "trace.h"
21
22 DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
23
24 static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn)
25 {
26 gpa_t gpa = gfn_to_gpa(gfn);
27 int wc_ofs, sec_hi_ofs;
28 int ret = 0;
29 int idx = srcu_read_lock(&kvm->srcu);
30
31 if (kvm_is_error_hva(gfn_to_hva(kvm, gfn))) {
32 ret = -EFAULT;
33 goto out;
34 }
35 kvm->arch.xen.shinfo_gfn = gfn;
36
37 /* Paranoia checks on the 32-bit struct layout */
38 BUILD_BUG_ON(offsetof(struct compat_shared_info, wc) != 0x900);
39 BUILD_BUG_ON(offsetof(struct compat_shared_info, arch.wc_sec_hi) != 0x924);
40 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
41
42 /* 32-bit location by default */
43 wc_ofs = offsetof(struct compat_shared_info, wc);
44 sec_hi_ofs = offsetof(struct compat_shared_info, arch.wc_sec_hi);
45
46 #ifdef CONFIG_X86_64
47 /* Paranoia checks on the 64-bit struct layout */
48 BUILD_BUG_ON(offsetof(struct shared_info, wc) != 0xc00);
49 BUILD_BUG_ON(offsetof(struct shared_info, wc_sec_hi) != 0xc0c);
50
51 if (kvm->arch.xen.long_mode) {
52 wc_ofs = offsetof(struct shared_info, wc);
53 sec_hi_ofs = offsetof(struct shared_info, wc_sec_hi);
54 }
55 #endif
56
57 kvm_write_wall_clock(kvm, gpa + wc_ofs, sec_hi_ofs - wc_ofs);
58 kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE);
59
60 out:
61 srcu_read_unlock(&kvm->srcu, idx);
62 return ret;
63 }
64
65 static void kvm_xen_update_runstate(struct kvm_vcpu *v, int state)
66 {
67 struct kvm_vcpu_xen *vx = &v->arch.xen;
68 u64 now = get_kvmclock_ns(v->kvm);
69 u64 delta_ns = now - vx->runstate_entry_time;
70 u64 run_delay = current->sched_info.run_delay;
71
72 if (unlikely(!vx->runstate_entry_time))
73 vx->current_runstate = RUNSTATE_offline;
74
75 /*
76 * Time waiting for the scheduler isn't "stolen" if the
77 * vCPU wasn't running anyway.
78 */
79 if (vx->current_runstate == RUNSTATE_running) {
80 u64 steal_ns = run_delay - vx->last_steal;
81
82 delta_ns -= steal_ns;
83
84 vx->runstate_times[RUNSTATE_runnable] += steal_ns;
85 }
86 vx->last_steal = run_delay;
87
88 vx->runstate_times[vx->current_runstate] += delta_ns;
89 vx->current_runstate = state;
90 vx->runstate_entry_time = now;
91 }
92
93 void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
94 {
95 struct kvm_vcpu_xen *vx = &v->arch.xen;
96 uint64_t state_entry_time;
97 unsigned int offset;
98
99 kvm_xen_update_runstate(v, state);
100
101 if (!vx->runstate_set)
102 return;
103
104 BUILD_BUG_ON(sizeof(struct compat_vcpu_runstate_info) != 0x2c);
105
106 offset = offsetof(struct compat_vcpu_runstate_info, state_entry_time);
107 #ifdef CONFIG_X86_64
108 /*
109 * The only difference is alignment of uint64_t in 32-bit.
110 * So the first field 'state' is accessed directly using
111 * offsetof() (where its offset happens to be zero), while the
112 * remaining fields which are all uint64_t, start at 'offset'
113 * which we tweak here by adding 4.
114 */
115 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
116 offsetof(struct compat_vcpu_runstate_info, state_entry_time) + 4);
117 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, time) !=
118 offsetof(struct compat_vcpu_runstate_info, time) + 4);
119
120 if (v->kvm->arch.xen.long_mode)
121 offset = offsetof(struct vcpu_runstate_info, state_entry_time);
122 #endif
123 /*
124 * First write the updated state_entry_time at the appropriate
125 * location determined by 'offset'.
126 */
127 state_entry_time = vx->runstate_entry_time;
128 state_entry_time |= XEN_RUNSTATE_UPDATE;
129
130 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->state_entry_time) !=
131 sizeof(state_entry_time));
132 BUILD_BUG_ON(sizeof(((struct compat_vcpu_runstate_info *)0)->state_entry_time) !=
133 sizeof(state_entry_time));
134
135 if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
136 &state_entry_time, offset,
137 sizeof(state_entry_time)))
138 return;
139 smp_wmb();
140
141 /*
142 * Next, write the new runstate. This is in the *same* place
143 * for 32-bit and 64-bit guests, asserted here for paranoia.
144 */
145 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) !=
146 offsetof(struct compat_vcpu_runstate_info, state));
147 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->state) !=
148 sizeof(vx->current_runstate));
149 BUILD_BUG_ON(sizeof(((struct compat_vcpu_runstate_info *)0)->state) !=
150 sizeof(vx->current_runstate));
151
152 if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
153 &vx->current_runstate,
154 offsetof(struct vcpu_runstate_info, state),
155 sizeof(vx->current_runstate)))
156 return;
157
158 /*
159 * Write the actual runstate times immediately after the
160 * runstate_entry_time.
161 */
162 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
163 offsetof(struct vcpu_runstate_info, time) - sizeof(u64));
164 BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state_entry_time) !=
165 offsetof(struct compat_vcpu_runstate_info, time) - sizeof(u64));
166 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->time) !=
167 sizeof(((struct compat_vcpu_runstate_info *)0)->time));
168 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->time) !=
169 sizeof(vx->runstate_times));
170
171 if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
172 &vx->runstate_times[0],
173 offset + sizeof(u64),
174 sizeof(vx->runstate_times)))
175 return;
176
177 smp_wmb();
178
179 /*
180 * Finally, clear the XEN_RUNSTATE_UPDATE bit in the guest's
181 * runstate_entry_time field.
182 */
183
184 state_entry_time &= ~XEN_RUNSTATE_UPDATE;
185 if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
186 &state_entry_time, offset,
187 sizeof(state_entry_time)))
188 return;
189 }
190
191 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
192 {
193 u8 rc = 0;
194
195 /*
196 * If the global upcall vector (HVMIRQ_callback_vector) is set and
197 * the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
198 */
199 struct gfn_to_hva_cache *ghc = &v->arch.xen.vcpu_info_cache;
200 struct kvm_memslots *slots = kvm_memslots(v->kvm);
201 unsigned int offset = offsetof(struct vcpu_info, evtchn_upcall_pending);
202
203 /* No need for compat handling here */
204 BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
205 offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
206 BUILD_BUG_ON(sizeof(rc) !=
207 sizeof(((struct vcpu_info *)0)->evtchn_upcall_pending));
208 BUILD_BUG_ON(sizeof(rc) !=
209 sizeof(((struct compat_vcpu_info *)0)->evtchn_upcall_pending));
210
211 /*
212 * For efficiency, this mirrors the checks for using the valid
213 * cache in kvm_read_guest_offset_cached(), but just uses
214 * __get_user() instead. And falls back to the slow path.
215 */
216 if (likely(slots->generation == ghc->generation &&
217 !kvm_is_error_hva(ghc->hva) && ghc->memslot)) {
218 /* Fast path */
219 __get_user(rc, (u8 __user *)ghc->hva + offset);
220 } else {
221 /* Slow path */
222 kvm_read_guest_offset_cached(v->kvm, ghc, &rc, offset,
223 sizeof(rc));
224 }
225
226 return rc;
227 }
228
229 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
230 {
231 int r = -ENOENT;
232
233 mutex_lock(&kvm->lock);
234
235 switch (data->type) {
236 case KVM_XEN_ATTR_TYPE_LONG_MODE:
237 if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
238 r = -EINVAL;
239 } else {
240 kvm->arch.xen.long_mode = !!data->u.long_mode;
241 r = 0;
242 }
243 break;
244
245 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
246 if (data->u.shared_info.gfn == GPA_INVALID) {
247 kvm->arch.xen.shinfo_gfn = GPA_INVALID;
248 r = 0;
249 break;
250 }
251 r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
252 break;
253
254
255 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
256 if (data->u.vector && data->u.vector < 0x10)
257 r = -EINVAL;
258 else {
259 kvm->arch.xen.upcall_vector = data->u.vector;
260 r = 0;
261 }
262 break;
263
264 default:
265 break;
266 }
267
268 mutex_unlock(&kvm->lock);
269 return r;
270 }
271
272 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
273 {
274 int r = -ENOENT;
275
276 mutex_lock(&kvm->lock);
277
278 switch (data->type) {
279 case KVM_XEN_ATTR_TYPE_LONG_MODE:
280 data->u.long_mode = kvm->arch.xen.long_mode;
281 r = 0;
282 break;
283
284 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
285 data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_gfn);
286 r = 0;
287 break;
288
289 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
290 data->u.vector = kvm->arch.xen.upcall_vector;
291 r = 0;
292 break;
293
294 default:
295 break;
296 }
297
298 mutex_unlock(&kvm->lock);
299 return r;
300 }
301
302 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
303 {
304 int idx, r = -ENOENT;
305
306 mutex_lock(&vcpu->kvm->lock);
307 idx = srcu_read_lock(&vcpu->kvm->srcu);
308
309 switch (data->type) {
310 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
311 /* No compat necessary here. */
312 BUILD_BUG_ON(sizeof(struct vcpu_info) !=
313 sizeof(struct compat_vcpu_info));
314 BUILD_BUG_ON(offsetof(struct vcpu_info, time) !=
315 offsetof(struct compat_vcpu_info, time));
316
317 if (data->u.gpa == GPA_INVALID) {
318 vcpu->arch.xen.vcpu_info_set = false;
319 r = 0;
320 break;
321 }
322
323 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
324 &vcpu->arch.xen.vcpu_info_cache,
325 data->u.gpa,
326 sizeof(struct vcpu_info));
327 if (!r) {
328 vcpu->arch.xen.vcpu_info_set = true;
329 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
330 }
331 break;
332
333 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
334 if (data->u.gpa == GPA_INVALID) {
335 vcpu->arch.xen.vcpu_time_info_set = false;
336 r = 0;
337 break;
338 }
339
340 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
341 &vcpu->arch.xen.vcpu_time_info_cache,
342 data->u.gpa,
343 sizeof(struct pvclock_vcpu_time_info));
344 if (!r) {
345 vcpu->arch.xen.vcpu_time_info_set = true;
346 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
347 }
348 break;
349
350 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
351 if (!sched_info_on()) {
352 r = -EOPNOTSUPP;
353 break;
354 }
355 if (data->u.gpa == GPA_INVALID) {
356 vcpu->arch.xen.runstate_set = false;
357 r = 0;
358 break;
359 }
360
361 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
362 &vcpu->arch.xen.runstate_cache,
363 data->u.gpa,
364 sizeof(struct vcpu_runstate_info));
365 if (!r) {
366 vcpu->arch.xen.runstate_set = true;
367 }
368 break;
369
370 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
371 if (!sched_info_on()) {
372 r = -EOPNOTSUPP;
373 break;
374 }
375 if (data->u.runstate.state > RUNSTATE_offline) {
376 r = -EINVAL;
377 break;
378 }
379
380 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
381 r = 0;
382 break;
383
384 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
385 if (!sched_info_on()) {
386 r = -EOPNOTSUPP;
387 break;
388 }
389 if (data->u.runstate.state > RUNSTATE_offline) {
390 r = -EINVAL;
391 break;
392 }
393 if (data->u.runstate.state_entry_time !=
394 (data->u.runstate.time_running +
395 data->u.runstate.time_runnable +
396 data->u.runstate.time_blocked +
397 data->u.runstate.time_offline)) {
398 r = -EINVAL;
399 break;
400 }
401 if (get_kvmclock_ns(vcpu->kvm) <
402 data->u.runstate.state_entry_time) {
403 r = -EINVAL;
404 break;
405 }
406
407 vcpu->arch.xen.current_runstate = data->u.runstate.state;
408 vcpu->arch.xen.runstate_entry_time =
409 data->u.runstate.state_entry_time;
410 vcpu->arch.xen.runstate_times[RUNSTATE_running] =
411 data->u.runstate.time_running;
412 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] =
413 data->u.runstate.time_runnable;
414 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] =
415 data->u.runstate.time_blocked;
416 vcpu->arch.xen.runstate_times[RUNSTATE_offline] =
417 data->u.runstate.time_offline;
418 vcpu->arch.xen.last_steal = current->sched_info.run_delay;
419 r = 0;
420 break;
421
422 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
423 if (!sched_info_on()) {
424 r = -EOPNOTSUPP;
425 break;
426 }
427 if (data->u.runstate.state > RUNSTATE_offline &&
428 data->u.runstate.state != (u64)-1) {
429 r = -EINVAL;
430 break;
431 }
432 /* The adjustment must add up */
433 if (data->u.runstate.state_entry_time !=
434 (data->u.runstate.time_running +
435 data->u.runstate.time_runnable +
436 data->u.runstate.time_blocked +
437 data->u.runstate.time_offline)) {
438 r = -EINVAL;
439 break;
440 }
441
442 if (get_kvmclock_ns(vcpu->kvm) <
443 (vcpu->arch.xen.runstate_entry_time +
444 data->u.runstate.state_entry_time)) {
445 r = -EINVAL;
446 break;
447 }
448
449 vcpu->arch.xen.runstate_entry_time +=
450 data->u.runstate.state_entry_time;
451 vcpu->arch.xen.runstate_times[RUNSTATE_running] +=
452 data->u.runstate.time_running;
453 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] +=
454 data->u.runstate.time_runnable;
455 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] +=
456 data->u.runstate.time_blocked;
457 vcpu->arch.xen.runstate_times[RUNSTATE_offline] +=
458 data->u.runstate.time_offline;
459
460 if (data->u.runstate.state <= RUNSTATE_offline)
461 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
462 r = 0;
463 break;
464
465 default:
466 break;
467 }
468
469 srcu_read_unlock(&vcpu->kvm->srcu, idx);
470 mutex_unlock(&vcpu->kvm->lock);
471 return r;
472 }
473
474 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
475 {
476 int r = -ENOENT;
477
478 mutex_lock(&vcpu->kvm->lock);
479
480 switch (data->type) {
481 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
482 if (vcpu->arch.xen.vcpu_info_set)
483 data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
484 else
485 data->u.gpa = GPA_INVALID;
486 r = 0;
487 break;
488
489 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
490 if (vcpu->arch.xen.vcpu_time_info_set)
491 data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
492 else
493 data->u.gpa = GPA_INVALID;
494 r = 0;
495 break;
496
497 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
498 if (!sched_info_on()) {
499 r = -EOPNOTSUPP;
500 break;
501 }
502 if (vcpu->arch.xen.runstate_set) {
503 data->u.gpa = vcpu->arch.xen.runstate_cache.gpa;
504 r = 0;
505 }
506 break;
507
508 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
509 if (!sched_info_on()) {
510 r = -EOPNOTSUPP;
511 break;
512 }
513 data->u.runstate.state = vcpu->arch.xen.current_runstate;
514 r = 0;
515 break;
516
517 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
518 if (!sched_info_on()) {
519 r = -EOPNOTSUPP;
520 break;
521 }
522 data->u.runstate.state = vcpu->arch.xen.current_runstate;
523 data->u.runstate.state_entry_time =
524 vcpu->arch.xen.runstate_entry_time;
525 data->u.runstate.time_running =
526 vcpu->arch.xen.runstate_times[RUNSTATE_running];
527 data->u.runstate.time_runnable =
528 vcpu->arch.xen.runstate_times[RUNSTATE_runnable];
529 data->u.runstate.time_blocked =
530 vcpu->arch.xen.runstate_times[RUNSTATE_blocked];
531 data->u.runstate.time_offline =
532 vcpu->arch.xen.runstate_times[RUNSTATE_offline];
533 r = 0;
534 break;
535
536 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
537 r = -EINVAL;
538 break;
539
540 default:
541 break;
542 }
543
544 mutex_unlock(&vcpu->kvm->lock);
545 return r;
546 }
547
548 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
549 {
550 struct kvm *kvm = vcpu->kvm;
551 u32 page_num = data & ~PAGE_MASK;
552 u64 page_addr = data & PAGE_MASK;
553 bool lm = is_long_mode(vcpu);
554
555 /* Latch long_mode for shared_info pages etc. */
556 vcpu->kvm->arch.xen.long_mode = lm;
557
558 /*
559 * If Xen hypercall intercept is enabled, fill the hypercall
560 * page with VMCALL/VMMCALL instructions since that's what
561 * we catch. Else the VMM has provided the hypercall pages
562 * with instructions of its own choosing, so use those.
563 */
564 if (kvm_xen_hypercall_enabled(kvm)) {
565 u8 instructions[32];
566 int i;
567
568 if (page_num)
569 return 1;
570
571 /* mov imm32, %eax */
572 instructions[0] = 0xb8;
573
574 /* vmcall / vmmcall */
575 kvm_x86_ops.patch_hypercall(vcpu, instructions + 5);
576
577 /* ret */
578 instructions[8] = 0xc3;
579
580 /* int3 to pad */
581 memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
582
583 for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
584 *(u32 *)&instructions[1] = i;
585 if (kvm_vcpu_write_guest(vcpu,
586 page_addr + (i * sizeof(instructions)),
587 instructions, sizeof(instructions)))
588 return 1;
589 }
590 } else {
591 /*
592 * Note, truncation is a non-issue as 'lm' is guaranteed to be
593 * false for a 32-bit kernel, i.e. when hva_t is only 4 bytes.
594 */
595 hva_t blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
596 : kvm->arch.xen_hvm_config.blob_addr_32;
597 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
598 : kvm->arch.xen_hvm_config.blob_size_32;
599 u8 *page;
600
601 if (page_num >= blob_size)
602 return 1;
603
604 blob_addr += page_num * PAGE_SIZE;
605
606 page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
607 if (IS_ERR(page))
608 return PTR_ERR(page);
609
610 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
611 kfree(page);
612 return 1;
613 }
614 }
615 return 0;
616 }
617
618 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
619 {
620 if (xhc->flags & ~KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL)
621 return -EINVAL;
622
623 /*
624 * With hypercall interception the kernel generates its own
625 * hypercall page so it must not be provided.
626 */
627 if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
628 (xhc->blob_addr_32 || xhc->blob_addr_64 ||
629 xhc->blob_size_32 || xhc->blob_size_64))
630 return -EINVAL;
631
632 mutex_lock(&kvm->lock);
633
634 if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
635 static_branch_inc(&kvm_xen_enabled.key);
636 else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
637 static_branch_slow_dec_deferred(&kvm_xen_enabled);
638
639 memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
640
641 mutex_unlock(&kvm->lock);
642 return 0;
643 }
644
645 void kvm_xen_init_vm(struct kvm *kvm)
646 {
647 kvm->arch.xen.shinfo_gfn = GPA_INVALID;
648 }
649
650 void kvm_xen_destroy_vm(struct kvm *kvm)
651 {
652 if (kvm->arch.xen_hvm_config.msr)
653 static_branch_slow_dec_deferred(&kvm_xen_enabled);
654 }
655
656 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
657 {
658 kvm_rax_write(vcpu, result);
659 return kvm_skip_emulated_instruction(vcpu);
660 }
661
662 static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
663 {
664 struct kvm_run *run = vcpu->run;
665
666 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
667 return 1;
668
669 return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
670 }
671
672 int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
673 {
674 bool longmode;
675 u64 input, params[6];
676
677 input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
678
679 /* Hyper-V hypercalls get bit 31 set in EAX */
680 if ((input & 0x80000000) &&
681 kvm_hv_hypercall_enabled(vcpu))
682 return kvm_hv_hypercall(vcpu);
683
684 longmode = is_64_bit_mode(vcpu);
685 if (!longmode) {
686 params[0] = (u32)kvm_rbx_read(vcpu);
687 params[1] = (u32)kvm_rcx_read(vcpu);
688 params[2] = (u32)kvm_rdx_read(vcpu);
689 params[3] = (u32)kvm_rsi_read(vcpu);
690 params[4] = (u32)kvm_rdi_read(vcpu);
691 params[5] = (u32)kvm_rbp_read(vcpu);
692 }
693 #ifdef CONFIG_X86_64
694 else {
695 params[0] = (u64)kvm_rdi_read(vcpu);
696 params[1] = (u64)kvm_rsi_read(vcpu);
697 params[2] = (u64)kvm_rdx_read(vcpu);
698 params[3] = (u64)kvm_r10_read(vcpu);
699 params[4] = (u64)kvm_r8_read(vcpu);
700 params[5] = (u64)kvm_r9_read(vcpu);
701 }
702 #endif
703 trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
704 params[3], params[4], params[5]);
705
706 vcpu->run->exit_reason = KVM_EXIT_XEN;
707 vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
708 vcpu->run->xen.u.hcall.longmode = longmode;
709 vcpu->run->xen.u.hcall.cpl = kvm_x86_ops.get_cpl(vcpu);
710 vcpu->run->xen.u.hcall.input = input;
711 vcpu->run->xen.u.hcall.params[0] = params[0];
712 vcpu->run->xen.u.hcall.params[1] = params[1];
713 vcpu->run->xen.u.hcall.params[2] = params[2];
714 vcpu->run->xen.u.hcall.params[3] = params[3];
715 vcpu->run->xen.u.hcall.params[4] = params[4];
716 vcpu->run->xen.u.hcall.params[5] = params[5];
717 vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
718 vcpu->arch.complete_userspace_io =
719 kvm_xen_hypercall_complete_userspace;
720
721 return 0;
722 }