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
kvm_xen_shared_info_init(struct kvm * kvm,gfn_t gfn)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
kvm_xen_update_runstate(struct kvm_vcpu * v,int state)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
kvm_xen_update_runstate_guest(struct kvm_vcpu * v,int state)93 void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
94 {
95 struct kvm_vcpu_xen *vx = &v->arch.xen;
96 struct gfn_to_hva_cache *ghc = &vx->runstate_cache;
97 struct kvm_memslots *slots = kvm_memslots(v->kvm);
98 bool atomic = (state == RUNSTATE_runnable);
99 uint64_t state_entry_time;
100 int __user *user_state;
101 uint64_t __user *user_times;
102
103 kvm_xen_update_runstate(v, state);
104
105 if (!vx->runstate_set)
106 return;
107
108 if (unlikely(slots->generation != ghc->generation || kvm_is_error_hva(ghc->hva)) &&
109 kvm_gfn_to_hva_cache_init(v->kvm, ghc, ghc->gpa, ghc->len))
110 return;
111
112 /* We made sure it fits in a single page */
113 BUG_ON(!ghc->memslot);
114
115 if (atomic)
116 pagefault_disable();
117
118 /*
119 * The only difference between 32-bit and 64-bit versions of the
120 * runstate struct us the alignment of uint64_t in 32-bit, which
121 * means that the 64-bit version has an additional 4 bytes of
122 * padding after the first field 'state'.
123 *
124 * So we use 'int __user *user_state' to point to the state field,
125 * and 'uint64_t __user *user_times' for runstate_entry_time. So
126 * the actual array of time[] in each state starts at user_times[1].
127 */
128 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) != 0);
129 BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state) != 0);
130 user_state = (int __user *)ghc->hva;
131
132 BUILD_BUG_ON(sizeof(struct compat_vcpu_runstate_info) != 0x2c);
133
134 user_times = (uint64_t __user *)(ghc->hva +
135 offsetof(struct compat_vcpu_runstate_info,
136 state_entry_time));
137 #ifdef CONFIG_X86_64
138 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
139 offsetof(struct compat_vcpu_runstate_info, state_entry_time) + 4);
140 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, time) !=
141 offsetof(struct compat_vcpu_runstate_info, time) + 4);
142
143 if (v->kvm->arch.xen.long_mode)
144 user_times = (uint64_t __user *)(ghc->hva +
145 offsetof(struct vcpu_runstate_info,
146 state_entry_time));
147 #endif
148 /*
149 * First write the updated state_entry_time at the appropriate
150 * location determined by 'offset'.
151 */
152 state_entry_time = vx->runstate_entry_time;
153 state_entry_time |= XEN_RUNSTATE_UPDATE;
154
155 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->state_entry_time) !=
156 sizeof(state_entry_time));
157 BUILD_BUG_ON(sizeof(((struct compat_vcpu_runstate_info *)0)->state_entry_time) !=
158 sizeof(state_entry_time));
159
160 if (__put_user(state_entry_time, user_times))
161 goto out;
162 smp_wmb();
163
164 /*
165 * Next, write the new runstate. This is in the *same* place
166 * for 32-bit and 64-bit guests, asserted here for paranoia.
167 */
168 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) !=
169 offsetof(struct compat_vcpu_runstate_info, state));
170 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->state) !=
171 sizeof(vx->current_runstate));
172 BUILD_BUG_ON(sizeof(((struct compat_vcpu_runstate_info *)0)->state) !=
173 sizeof(vx->current_runstate));
174
175 if (__put_user(vx->current_runstate, user_state))
176 goto out;
177
178 /*
179 * Write the actual runstate times immediately after the
180 * runstate_entry_time.
181 */
182 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
183 offsetof(struct vcpu_runstate_info, time) - sizeof(u64));
184 BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state_entry_time) !=
185 offsetof(struct compat_vcpu_runstate_info, time) - sizeof(u64));
186 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->time) !=
187 sizeof(((struct compat_vcpu_runstate_info *)0)->time));
188 BUILD_BUG_ON(sizeof(((struct vcpu_runstate_info *)0)->time) !=
189 sizeof(vx->runstate_times));
190
191 if (__copy_to_user(user_times + 1, vx->runstate_times, sizeof(vx->runstate_times)))
192 goto out;
193 smp_wmb();
194
195 /*
196 * Finally, clear the XEN_RUNSTATE_UPDATE bit in the guest's
197 * runstate_entry_time field.
198 */
199 state_entry_time &= ~XEN_RUNSTATE_UPDATE;
200 __put_user(state_entry_time, user_times);
201 smp_wmb();
202
203 out:
204 mark_page_dirty_in_slot(v->kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
205
206 if (atomic)
207 pagefault_enable();
208 }
209
__kvm_xen_has_interrupt(struct kvm_vcpu * v)210 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
211 {
212 int err;
213 u8 rc = 0;
214
215 /*
216 * If the global upcall vector (HVMIRQ_callback_vector) is set and
217 * the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
218 */
219 struct gfn_to_hva_cache *ghc = &v->arch.xen.vcpu_info_cache;
220 struct kvm_memslots *slots = kvm_memslots(v->kvm);
221 unsigned int offset = offsetof(struct vcpu_info, evtchn_upcall_pending);
222
223 /* No need for compat handling here */
224 BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
225 offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
226 BUILD_BUG_ON(sizeof(rc) !=
227 sizeof(((struct vcpu_info *)0)->evtchn_upcall_pending));
228 BUILD_BUG_ON(sizeof(rc) !=
229 sizeof(((struct compat_vcpu_info *)0)->evtchn_upcall_pending));
230
231 /*
232 * For efficiency, this mirrors the checks for using the valid
233 * cache in kvm_read_guest_offset_cached(), but just uses
234 * __get_user() instead. And falls back to the slow path.
235 */
236 if (likely(slots->generation == ghc->generation &&
237 !kvm_is_error_hva(ghc->hva) && ghc->memslot)) {
238 /* Fast path */
239 pagefault_disable();
240 err = __get_user(rc, (u8 __user *)ghc->hva + offset);
241 pagefault_enable();
242 if (!err)
243 return rc;
244 }
245
246 /* Slow path */
247
248 /*
249 * This function gets called from kvm_vcpu_block() after setting the
250 * task to TASK_INTERRUPTIBLE, to see if it needs to wake immediately
251 * from a HLT. So we really mustn't sleep. If the page ended up absent
252 * at that point, just return 1 in order to trigger an immediate wake,
253 * and we'll end up getting called again from a context where we *can*
254 * fault in the page and wait for it.
255 */
256 if (in_atomic() || !task_is_running(current))
257 return 1;
258
259 kvm_read_guest_offset_cached(v->kvm, ghc, &rc, offset,
260 sizeof(rc));
261
262 return rc;
263 }
264
kvm_xen_hvm_set_attr(struct kvm * kvm,struct kvm_xen_hvm_attr * data)265 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
266 {
267 int r = -ENOENT;
268
269 mutex_lock(&kvm->lock);
270
271 switch (data->type) {
272 case KVM_XEN_ATTR_TYPE_LONG_MODE:
273 if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
274 r = -EINVAL;
275 } else {
276 kvm->arch.xen.long_mode = !!data->u.long_mode;
277 r = 0;
278 }
279 break;
280
281 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
282 if (data->u.shared_info.gfn == GPA_INVALID) {
283 kvm->arch.xen.shinfo_gfn = GPA_INVALID;
284 r = 0;
285 break;
286 }
287 r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
288 break;
289
290
291 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
292 if (data->u.vector && data->u.vector < 0x10)
293 r = -EINVAL;
294 else {
295 kvm->arch.xen.upcall_vector = data->u.vector;
296 r = 0;
297 }
298 break;
299
300 default:
301 break;
302 }
303
304 mutex_unlock(&kvm->lock);
305 return r;
306 }
307
kvm_xen_hvm_get_attr(struct kvm * kvm,struct kvm_xen_hvm_attr * data)308 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
309 {
310 int r = -ENOENT;
311
312 mutex_lock(&kvm->lock);
313
314 switch (data->type) {
315 case KVM_XEN_ATTR_TYPE_LONG_MODE:
316 data->u.long_mode = kvm->arch.xen.long_mode;
317 r = 0;
318 break;
319
320 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
321 data->u.shared_info.gfn = kvm->arch.xen.shinfo_gfn;
322 r = 0;
323 break;
324
325 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
326 data->u.vector = kvm->arch.xen.upcall_vector;
327 r = 0;
328 break;
329
330 default:
331 break;
332 }
333
334 mutex_unlock(&kvm->lock);
335 return r;
336 }
337
kvm_xen_vcpu_set_attr(struct kvm_vcpu * vcpu,struct kvm_xen_vcpu_attr * data)338 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
339 {
340 int idx, r = -ENOENT;
341
342 mutex_lock(&vcpu->kvm->lock);
343 idx = srcu_read_lock(&vcpu->kvm->srcu);
344
345 switch (data->type) {
346 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
347 /* No compat necessary here. */
348 BUILD_BUG_ON(sizeof(struct vcpu_info) !=
349 sizeof(struct compat_vcpu_info));
350 BUILD_BUG_ON(offsetof(struct vcpu_info, time) !=
351 offsetof(struct compat_vcpu_info, time));
352
353 if (data->u.gpa == GPA_INVALID) {
354 vcpu->arch.xen.vcpu_info_set = false;
355 r = 0;
356 break;
357 }
358
359 /* It must fit within a single page */
360 if ((data->u.gpa & ~PAGE_MASK) + sizeof(struct vcpu_info) > PAGE_SIZE) {
361 r = -EINVAL;
362 break;
363 }
364
365 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
366 &vcpu->arch.xen.vcpu_info_cache,
367 data->u.gpa,
368 sizeof(struct vcpu_info));
369 if (!r) {
370 vcpu->arch.xen.vcpu_info_set = true;
371 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
372 }
373 break;
374
375 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
376 if (data->u.gpa == GPA_INVALID) {
377 vcpu->arch.xen.vcpu_time_info_set = false;
378 r = 0;
379 break;
380 }
381
382 /* It must fit within a single page */
383 if ((data->u.gpa & ~PAGE_MASK) + sizeof(struct pvclock_vcpu_time_info) > PAGE_SIZE) {
384 r = -EINVAL;
385 break;
386 }
387
388 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
389 &vcpu->arch.xen.vcpu_time_info_cache,
390 data->u.gpa,
391 sizeof(struct pvclock_vcpu_time_info));
392 if (!r) {
393 vcpu->arch.xen.vcpu_time_info_set = true;
394 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
395 }
396 break;
397
398 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
399 if (!sched_info_on()) {
400 r = -EOPNOTSUPP;
401 break;
402 }
403 if (data->u.gpa == GPA_INVALID) {
404 vcpu->arch.xen.runstate_set = false;
405 r = 0;
406 break;
407 }
408
409 /* It must fit within a single page */
410 if ((data->u.gpa & ~PAGE_MASK) + sizeof(struct vcpu_runstate_info) > PAGE_SIZE) {
411 r = -EINVAL;
412 break;
413 }
414
415 r = kvm_gfn_to_hva_cache_init(vcpu->kvm,
416 &vcpu->arch.xen.runstate_cache,
417 data->u.gpa,
418 sizeof(struct vcpu_runstate_info));
419 if (!r) {
420 vcpu->arch.xen.runstate_set = true;
421 }
422 break;
423
424 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
425 if (!sched_info_on()) {
426 r = -EOPNOTSUPP;
427 break;
428 }
429 if (data->u.runstate.state > RUNSTATE_offline) {
430 r = -EINVAL;
431 break;
432 }
433
434 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
435 r = 0;
436 break;
437
438 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
439 if (!sched_info_on()) {
440 r = -EOPNOTSUPP;
441 break;
442 }
443 if (data->u.runstate.state > RUNSTATE_offline) {
444 r = -EINVAL;
445 break;
446 }
447 if (data->u.runstate.state_entry_time !=
448 (data->u.runstate.time_running +
449 data->u.runstate.time_runnable +
450 data->u.runstate.time_blocked +
451 data->u.runstate.time_offline)) {
452 r = -EINVAL;
453 break;
454 }
455 if (get_kvmclock_ns(vcpu->kvm) <
456 data->u.runstate.state_entry_time) {
457 r = -EINVAL;
458 break;
459 }
460
461 vcpu->arch.xen.current_runstate = data->u.runstate.state;
462 vcpu->arch.xen.runstate_entry_time =
463 data->u.runstate.state_entry_time;
464 vcpu->arch.xen.runstate_times[RUNSTATE_running] =
465 data->u.runstate.time_running;
466 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] =
467 data->u.runstate.time_runnable;
468 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] =
469 data->u.runstate.time_blocked;
470 vcpu->arch.xen.runstate_times[RUNSTATE_offline] =
471 data->u.runstate.time_offline;
472 vcpu->arch.xen.last_steal = current->sched_info.run_delay;
473 r = 0;
474 break;
475
476 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
477 if (!sched_info_on()) {
478 r = -EOPNOTSUPP;
479 break;
480 }
481 if (data->u.runstate.state > RUNSTATE_offline &&
482 data->u.runstate.state != (u64)-1) {
483 r = -EINVAL;
484 break;
485 }
486 /* The adjustment must add up */
487 if (data->u.runstate.state_entry_time !=
488 (data->u.runstate.time_running +
489 data->u.runstate.time_runnable +
490 data->u.runstate.time_blocked +
491 data->u.runstate.time_offline)) {
492 r = -EINVAL;
493 break;
494 }
495
496 if (get_kvmclock_ns(vcpu->kvm) <
497 (vcpu->arch.xen.runstate_entry_time +
498 data->u.runstate.state_entry_time)) {
499 r = -EINVAL;
500 break;
501 }
502
503 vcpu->arch.xen.runstate_entry_time +=
504 data->u.runstate.state_entry_time;
505 vcpu->arch.xen.runstate_times[RUNSTATE_running] +=
506 data->u.runstate.time_running;
507 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] +=
508 data->u.runstate.time_runnable;
509 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] +=
510 data->u.runstate.time_blocked;
511 vcpu->arch.xen.runstate_times[RUNSTATE_offline] +=
512 data->u.runstate.time_offline;
513
514 if (data->u.runstate.state <= RUNSTATE_offline)
515 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
516 r = 0;
517 break;
518
519 default:
520 break;
521 }
522
523 srcu_read_unlock(&vcpu->kvm->srcu, idx);
524 mutex_unlock(&vcpu->kvm->lock);
525 return r;
526 }
527
kvm_xen_vcpu_get_attr(struct kvm_vcpu * vcpu,struct kvm_xen_vcpu_attr * data)528 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
529 {
530 int r = -ENOENT;
531
532 mutex_lock(&vcpu->kvm->lock);
533
534 switch (data->type) {
535 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
536 if (vcpu->arch.xen.vcpu_info_set)
537 data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
538 else
539 data->u.gpa = GPA_INVALID;
540 r = 0;
541 break;
542
543 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
544 if (vcpu->arch.xen.vcpu_time_info_set)
545 data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
546 else
547 data->u.gpa = GPA_INVALID;
548 r = 0;
549 break;
550
551 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
552 if (!sched_info_on()) {
553 r = -EOPNOTSUPP;
554 break;
555 }
556 if (vcpu->arch.xen.runstate_set) {
557 data->u.gpa = vcpu->arch.xen.runstate_cache.gpa;
558 r = 0;
559 }
560 break;
561
562 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
563 if (!sched_info_on()) {
564 r = -EOPNOTSUPP;
565 break;
566 }
567 data->u.runstate.state = vcpu->arch.xen.current_runstate;
568 r = 0;
569 break;
570
571 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
572 if (!sched_info_on()) {
573 r = -EOPNOTSUPP;
574 break;
575 }
576 data->u.runstate.state = vcpu->arch.xen.current_runstate;
577 data->u.runstate.state_entry_time =
578 vcpu->arch.xen.runstate_entry_time;
579 data->u.runstate.time_running =
580 vcpu->arch.xen.runstate_times[RUNSTATE_running];
581 data->u.runstate.time_runnable =
582 vcpu->arch.xen.runstate_times[RUNSTATE_runnable];
583 data->u.runstate.time_blocked =
584 vcpu->arch.xen.runstate_times[RUNSTATE_blocked];
585 data->u.runstate.time_offline =
586 vcpu->arch.xen.runstate_times[RUNSTATE_offline];
587 r = 0;
588 break;
589
590 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
591 r = -EINVAL;
592 break;
593
594 default:
595 break;
596 }
597
598 mutex_unlock(&vcpu->kvm->lock);
599 return r;
600 }
601
kvm_xen_write_hypercall_page(struct kvm_vcpu * vcpu,u64 data)602 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
603 {
604 struct kvm *kvm = vcpu->kvm;
605 u32 page_num = data & ~PAGE_MASK;
606 u64 page_addr = data & PAGE_MASK;
607 bool lm = is_long_mode(vcpu);
608
609 /* Latch long_mode for shared_info pages etc. */
610 vcpu->kvm->arch.xen.long_mode = lm;
611
612 /*
613 * If Xen hypercall intercept is enabled, fill the hypercall
614 * page with VMCALL/VMMCALL instructions since that's what
615 * we catch. Else the VMM has provided the hypercall pages
616 * with instructions of its own choosing, so use those.
617 */
618 if (kvm_xen_hypercall_enabled(kvm)) {
619 u8 instructions[32];
620 int i;
621
622 if (page_num)
623 return 1;
624
625 /* mov imm32, %eax */
626 instructions[0] = 0xb8;
627
628 /* vmcall / vmmcall */
629 kvm_x86_ops.patch_hypercall(vcpu, instructions + 5);
630
631 /* ret */
632 instructions[8] = 0xc3;
633
634 /* int3 to pad */
635 memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
636
637 for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
638 *(u32 *)&instructions[1] = i;
639 if (kvm_vcpu_write_guest(vcpu,
640 page_addr + (i * sizeof(instructions)),
641 instructions, sizeof(instructions)))
642 return 1;
643 }
644 } else {
645 /*
646 * Note, truncation is a non-issue as 'lm' is guaranteed to be
647 * false for a 32-bit kernel, i.e. when hva_t is only 4 bytes.
648 */
649 hva_t blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
650 : kvm->arch.xen_hvm_config.blob_addr_32;
651 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
652 : kvm->arch.xen_hvm_config.blob_size_32;
653 u8 *page;
654
655 if (page_num >= blob_size)
656 return 1;
657
658 blob_addr += page_num * PAGE_SIZE;
659
660 page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
661 if (IS_ERR(page))
662 return PTR_ERR(page);
663
664 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
665 kfree(page);
666 return 1;
667 }
668 }
669 return 0;
670 }
671
kvm_xen_hvm_config(struct kvm * kvm,struct kvm_xen_hvm_config * xhc)672 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
673 {
674 if (xhc->flags & ~KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL)
675 return -EINVAL;
676
677 /*
678 * With hypercall interception the kernel generates its own
679 * hypercall page so it must not be provided.
680 */
681 if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
682 (xhc->blob_addr_32 || xhc->blob_addr_64 ||
683 xhc->blob_size_32 || xhc->blob_size_64))
684 return -EINVAL;
685
686 mutex_lock(&kvm->lock);
687
688 if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
689 static_branch_inc(&kvm_xen_enabled.key);
690 else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
691 static_branch_slow_dec_deferred(&kvm_xen_enabled);
692
693 memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
694
695 mutex_unlock(&kvm->lock);
696 return 0;
697 }
698
kvm_xen_init_vm(struct kvm * kvm)699 void kvm_xen_init_vm(struct kvm *kvm)
700 {
701 kvm->arch.xen.shinfo_gfn = GPA_INVALID;
702 }
703
kvm_xen_destroy_vm(struct kvm * kvm)704 void kvm_xen_destroy_vm(struct kvm *kvm)
705 {
706 if (kvm->arch.xen_hvm_config.msr)
707 static_branch_slow_dec_deferred(&kvm_xen_enabled);
708 }
709
kvm_xen_hypercall_set_result(struct kvm_vcpu * vcpu,u64 result)710 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
711 {
712 kvm_rax_write(vcpu, result);
713 return kvm_skip_emulated_instruction(vcpu);
714 }
715
kvm_xen_hypercall_complete_userspace(struct kvm_vcpu * vcpu)716 static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
717 {
718 struct kvm_run *run = vcpu->run;
719
720 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
721 return 1;
722
723 return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
724 }
725
kvm_xen_hypercall(struct kvm_vcpu * vcpu)726 int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
727 {
728 bool longmode;
729 u64 input, params[6];
730
731 input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
732
733 /* Hyper-V hypercalls get bit 31 set in EAX */
734 if ((input & 0x80000000) &&
735 kvm_hv_hypercall_enabled(vcpu))
736 return kvm_hv_hypercall(vcpu);
737
738 longmode = is_64_bit_hypercall(vcpu);
739 if (!longmode) {
740 params[0] = (u32)kvm_rbx_read(vcpu);
741 params[1] = (u32)kvm_rcx_read(vcpu);
742 params[2] = (u32)kvm_rdx_read(vcpu);
743 params[3] = (u32)kvm_rsi_read(vcpu);
744 params[4] = (u32)kvm_rdi_read(vcpu);
745 params[5] = (u32)kvm_rbp_read(vcpu);
746 }
747 #ifdef CONFIG_X86_64
748 else {
749 params[0] = (u64)kvm_rdi_read(vcpu);
750 params[1] = (u64)kvm_rsi_read(vcpu);
751 params[2] = (u64)kvm_rdx_read(vcpu);
752 params[3] = (u64)kvm_r10_read(vcpu);
753 params[4] = (u64)kvm_r8_read(vcpu);
754 params[5] = (u64)kvm_r9_read(vcpu);
755 }
756 #endif
757 trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
758 params[3], params[4], params[5]);
759
760 vcpu->run->exit_reason = KVM_EXIT_XEN;
761 vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
762 vcpu->run->xen.u.hcall.longmode = longmode;
763 vcpu->run->xen.u.hcall.cpl = kvm_x86_ops.get_cpl(vcpu);
764 vcpu->run->xen.u.hcall.input = input;
765 vcpu->run->xen.u.hcall.params[0] = params[0];
766 vcpu->run->xen.u.hcall.params[1] = params[1];
767 vcpu->run->xen.u.hcall.params[2] = params[2];
768 vcpu->run->xen.u.hcall.params[3] = params[3];
769 vcpu->run->xen.u.hcall.params[4] = params[4];
770 vcpu->run->xen.u.hcall.params[5] = params[5];
771 vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
772 vcpu->arch.complete_userspace_io =
773 kvm_xen_hypercall_complete_userspace;
774
775 return 0;
776 }
777