OSDN Git Service

b8b439dd311aec5531ddb0dd82cff3eda937298e
[qmiga/qemu.git] / target / i386 / kvm / xen-emu.c
1 /*
2  * Xen HVM emulation support in KVM
3  *
4  * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
5  * Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  *
10  */
11
12 #include "qemu/osdep.h"
13 #include "qemu/log.h"
14 #include "qemu/main-loop.h"
15 #include "hw/xen/xen.h"
16 #include "sysemu/kvm_int.h"
17 #include "sysemu/kvm_xen.h"
18 #include "kvm/kvm_i386.h"
19 #include "exec/address-spaces.h"
20 #include "xen-emu.h"
21 #include "trace.h"
22 #include "sysemu/runstate.h"
23
24 #include "hw/pci/msi.h"
25 #include "hw/i386/apic-msidef.h"
26 #include "hw/i386/kvm/xen_overlay.h"
27 #include "hw/i386/kvm/xen_evtchn.h"
28
29 #include "hw/xen/interface/version.h"
30 #include "hw/xen/interface/sched.h"
31 #include "hw/xen/interface/memory.h"
32 #include "hw/xen/interface/hvm/hvm_op.h"
33 #include "hw/xen/interface/hvm/params.h"
34 #include "hw/xen/interface/vcpu.h"
35 #include "hw/xen/interface/event_channel.h"
36
37 #include "xen-compat.h"
38
39 #ifdef TARGET_X86_64
40 #define hypercall_compat32(longmode) (!(longmode))
41 #else
42 #define hypercall_compat32(longmode) (false)
43 #endif
44
45 static bool kvm_gva_to_gpa(CPUState *cs, uint64_t gva, uint64_t *gpa,
46                            size_t *len, bool is_write)
47 {
48         struct kvm_translation tr = {
49             .linear_address = gva,
50         };
51
52         if (len) {
53             *len = TARGET_PAGE_SIZE - (gva & ~TARGET_PAGE_MASK);
54         }
55
56         if (kvm_vcpu_ioctl(cs, KVM_TRANSLATE, &tr) || !tr.valid ||
57             (is_write && !tr.writeable)) {
58             return false;
59         }
60         *gpa = tr.physical_address;
61         return true;
62 }
63
64 static int kvm_gva_rw(CPUState *cs, uint64_t gva, void *_buf, size_t sz,
65                       bool is_write)
66 {
67     uint8_t *buf = (uint8_t *)_buf;
68     uint64_t gpa;
69     size_t len;
70
71     while (sz) {
72         if (!kvm_gva_to_gpa(cs, gva, &gpa, &len, is_write)) {
73             return -EFAULT;
74         }
75         if (len > sz) {
76             len = sz;
77         }
78
79         cpu_physical_memory_rw(gpa, buf, len, is_write);
80
81         buf += len;
82         sz -= len;
83         gva += len;
84     }
85
86     return 0;
87 }
88
89 static inline int kvm_copy_from_gva(CPUState *cs, uint64_t gva, void *buf,
90                                     size_t sz)
91 {
92     return kvm_gva_rw(cs, gva, buf, sz, false);
93 }
94
95 static inline int kvm_copy_to_gva(CPUState *cs, uint64_t gva, void *buf,
96                                   size_t sz)
97 {
98     return kvm_gva_rw(cs, gva, buf, sz, true);
99 }
100
101 int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
102 {
103     const int required_caps = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
104         KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL | KVM_XEN_HVM_CONFIG_SHARED_INFO;
105     struct kvm_xen_hvm_config cfg = {
106         .msr = hypercall_msr,
107         .flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
108     };
109     int xen_caps, ret;
110
111     xen_caps = kvm_check_extension(s, KVM_CAP_XEN_HVM);
112     if (required_caps & ~xen_caps) {
113         error_report("kvm: Xen HVM guest support not present or insufficient");
114         return -ENOSYS;
115     }
116
117     if (xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND) {
118         struct kvm_xen_hvm_attr ha = {
119             .type = KVM_XEN_ATTR_TYPE_XEN_VERSION,
120             .u.xen_version = s->xen_version,
121         };
122         (void)kvm_vm_ioctl(s, KVM_XEN_HVM_SET_ATTR, &ha);
123
124         cfg.flags |= KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
125     }
126
127     ret = kvm_vm_ioctl(s, KVM_XEN_HVM_CONFIG, &cfg);
128     if (ret < 0) {
129         error_report("kvm: Failed to enable Xen HVM support: %s",
130                      strerror(-ret));
131         return ret;
132     }
133
134     s->xen_caps = xen_caps;
135     return 0;
136 }
137
138 int kvm_xen_init_vcpu(CPUState *cs)
139 {
140     X86CPU *cpu = X86_CPU(cs);
141     CPUX86State *env = &cpu->env;
142     int err;
143
144     /*
145      * The kernel needs to know the Xen/ACPI vCPU ID because that's
146      * what the guest uses in hypercalls such as timers. It doesn't
147      * match the APIC ID which is generally used for talking to the
148      * kernel about vCPUs. And if vCPU threads race with creating
149      * their KVM vCPUs out of order, it doesn't necessarily match
150      * with the kernel's internal vCPU indices either.
151      */
152     if (kvm_xen_has_cap(EVTCHN_SEND)) {
153         struct kvm_xen_vcpu_attr va = {
154             .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID,
155             .u.vcpu_id = cs->cpu_index,
156         };
157         err = kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &va);
158         if (err) {
159             error_report("kvm: Failed to set Xen vCPU ID attribute: %s",
160                          strerror(-err));
161             return err;
162         }
163     }
164
165     env->xen_vcpu_info_gpa = INVALID_GPA;
166     env->xen_vcpu_info_default_gpa = INVALID_GPA;
167     env->xen_vcpu_time_info_gpa = INVALID_GPA;
168     env->xen_vcpu_runstate_gpa = INVALID_GPA;
169
170     return 0;
171 }
172
173 uint32_t kvm_xen_get_caps(void)
174 {
175     return kvm_state->xen_caps;
176 }
177
178 static bool kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu,
179                                      int cmd, uint64_t arg)
180 {
181     int err = 0;
182
183     switch (cmd) {
184     case XENVER_get_features: {
185         struct xen_feature_info fi;
186
187         /* No need for 32/64 compat handling */
188         qemu_build_assert(sizeof(fi) == 8);
189
190         err = kvm_copy_from_gva(CPU(cpu), arg, &fi, sizeof(fi));
191         if (err) {
192             break;
193         }
194
195         fi.submap = 0;
196         if (fi.submap_idx == 0) {
197             fi.submap |= 1 << XENFEAT_writable_page_tables |
198                          1 << XENFEAT_writable_descriptor_tables |
199                          1 << XENFEAT_auto_translated_physmap |
200                          1 << XENFEAT_supervisor_mode_kernel |
201                          1 << XENFEAT_hvm_callback_vector;
202         }
203
204         err = kvm_copy_to_gva(CPU(cpu), arg, &fi, sizeof(fi));
205         break;
206     }
207
208     default:
209         return false;
210     }
211
212     exit->u.hcall.result = err;
213     return true;
214 }
215
216 static int kvm_xen_set_vcpu_attr(CPUState *cs, uint16_t type, uint64_t gpa)
217 {
218     struct kvm_xen_vcpu_attr xhsi;
219
220     xhsi.type = type;
221     xhsi.u.gpa = gpa;
222
223     trace_kvm_xen_set_vcpu_attr(cs->cpu_index, type, gpa);
224
225     return kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &xhsi);
226 }
227
228 static int kvm_xen_set_vcpu_callback_vector(CPUState *cs)
229 {
230     uint8_t vector = X86_CPU(cs)->env.xen_vcpu_callback_vector;
231     struct kvm_xen_vcpu_attr xva;
232
233     xva.type = KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR;
234     xva.u.vector = vector;
235
236     trace_kvm_xen_set_vcpu_callback(cs->cpu_index, vector);
237
238     return kvm_vcpu_ioctl(cs, KVM_XEN_HVM_SET_ATTR, &xva);
239 }
240
241 static void do_set_vcpu_callback_vector(CPUState *cs, run_on_cpu_data data)
242 {
243     X86CPU *cpu = X86_CPU(cs);
244     CPUX86State *env = &cpu->env;
245
246     env->xen_vcpu_callback_vector = data.host_int;
247
248     if (kvm_xen_has_cap(EVTCHN_SEND)) {
249         kvm_xen_set_vcpu_callback_vector(cs);
250     }
251 }
252
253 static int set_vcpu_info(CPUState *cs, uint64_t gpa)
254 {
255     X86CPU *cpu = X86_CPU(cs);
256     CPUX86State *env = &cpu->env;
257     MemoryRegionSection mrs = { .mr = NULL };
258     void *vcpu_info_hva = NULL;
259     int ret;
260
261     ret = kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, gpa);
262     if (ret || gpa == INVALID_GPA) {
263         goto out;
264     }
265
266     mrs = memory_region_find(get_system_memory(), gpa,
267                              sizeof(struct vcpu_info));
268     if (mrs.mr && mrs.mr->ram_block &&
269         !int128_lt(mrs.size, int128_make64(sizeof(struct vcpu_info)))) {
270         vcpu_info_hva = qemu_map_ram_ptr(mrs.mr->ram_block,
271                                          mrs.offset_within_region);
272     }
273     if (!vcpu_info_hva) {
274         if (mrs.mr) {
275             memory_region_unref(mrs.mr);
276             mrs.mr = NULL;
277         }
278         ret = -EINVAL;
279     }
280
281  out:
282     if (env->xen_vcpu_info_mr) {
283         memory_region_unref(env->xen_vcpu_info_mr);
284     }
285     env->xen_vcpu_info_hva = vcpu_info_hva;
286     env->xen_vcpu_info_mr = mrs.mr;
287     return ret;
288 }
289
290 static void do_set_vcpu_info_default_gpa(CPUState *cs, run_on_cpu_data data)
291 {
292     X86CPU *cpu = X86_CPU(cs);
293     CPUX86State *env = &cpu->env;
294
295     env->xen_vcpu_info_default_gpa = data.host_ulong;
296
297     /* Changing the default does nothing if a vcpu_info was explicitly set. */
298     if (env->xen_vcpu_info_gpa == INVALID_GPA) {
299         set_vcpu_info(cs, env->xen_vcpu_info_default_gpa);
300     }
301 }
302
303 static void do_set_vcpu_info_gpa(CPUState *cs, run_on_cpu_data data)
304 {
305     X86CPU *cpu = X86_CPU(cs);
306     CPUX86State *env = &cpu->env;
307
308     env->xen_vcpu_info_gpa = data.host_ulong;
309
310     set_vcpu_info(cs, env->xen_vcpu_info_gpa);
311 }
312
313 void *kvm_xen_get_vcpu_info_hva(uint32_t vcpu_id)
314 {
315     CPUState *cs = qemu_get_cpu(vcpu_id);
316     if (!cs) {
317         return NULL;
318     }
319
320     return X86_CPU(cs)->env.xen_vcpu_info_hva;
321 }
322
323 void kvm_xen_inject_vcpu_callback_vector(uint32_t vcpu_id, int type)
324 {
325     CPUState *cs = qemu_get_cpu(vcpu_id);
326     uint8_t vector;
327
328     if (!cs) {
329         return;
330     }
331
332     vector = X86_CPU(cs)->env.xen_vcpu_callback_vector;
333     if (vector) {
334         /*
335          * The per-vCPU callback vector injected via lapic. Just
336          * deliver it as an MSI.
337          */
338         MSIMessage msg = {
339             .address = APIC_DEFAULT_ADDRESS | X86_CPU(cs)->apic_id,
340             .data = vector | (1UL << MSI_DATA_LEVEL_SHIFT),
341         };
342         kvm_irqchip_send_msi(kvm_state, msg);
343         return;
344     }
345
346     switch (type) {
347     case HVM_PARAM_CALLBACK_TYPE_VECTOR:
348         /*
349          * If the evtchn_upcall_pending field in the vcpu_info is set, then
350          * KVM will automatically deliver the vector on entering the vCPU
351          * so all we have to do is kick it out.
352          */
353         qemu_cpu_kick(cs);
354         break;
355     }
356 }
357
358 static int kvm_xen_set_vcpu_timer(CPUState *cs)
359 {
360     X86CPU *cpu = X86_CPU(cs);
361     CPUX86State *env = &cpu->env;
362
363     struct kvm_xen_vcpu_attr va = {
364         .type = KVM_XEN_VCPU_ATTR_TYPE_TIMER,
365         .u.timer.port = env->xen_virq[VIRQ_TIMER],
366         .u.timer.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL,
367         .u.timer.expires_ns = env->xen_singleshot_timer_ns,
368     };
369
370     return kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &va);
371 }
372
373 static void do_set_vcpu_timer_virq(CPUState *cs, run_on_cpu_data data)
374 {
375     kvm_xen_set_vcpu_timer(cs);
376 }
377
378 int kvm_xen_set_vcpu_virq(uint32_t vcpu_id, uint16_t virq, uint16_t port)
379 {
380     CPUState *cs = qemu_get_cpu(vcpu_id);
381
382     if (!cs) {
383         return -ENOENT;
384     }
385
386     /* cpu.h doesn't include the actual Xen header. */
387     qemu_build_assert(NR_VIRQS == XEN_NR_VIRQS);
388
389     if (virq >= NR_VIRQS) {
390         return -EINVAL;
391     }
392
393     if (port && X86_CPU(cs)->env.xen_virq[virq]) {
394         return -EEXIST;
395     }
396
397     X86_CPU(cs)->env.xen_virq[virq] = port;
398     if (virq == VIRQ_TIMER && kvm_xen_has_cap(EVTCHN_SEND)) {
399         async_run_on_cpu(cs, do_set_vcpu_timer_virq,
400                          RUN_ON_CPU_HOST_INT(port));
401     }
402     return 0;
403 }
404
405 static void do_set_vcpu_time_info_gpa(CPUState *cs, run_on_cpu_data data)
406 {
407     X86CPU *cpu = X86_CPU(cs);
408     CPUX86State *env = &cpu->env;
409
410     env->xen_vcpu_time_info_gpa = data.host_ulong;
411
412     kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
413                           env->xen_vcpu_time_info_gpa);
414 }
415
416 static void do_set_vcpu_runstate_gpa(CPUState *cs, run_on_cpu_data data)
417 {
418     X86CPU *cpu = X86_CPU(cs);
419     CPUX86State *env = &cpu->env;
420
421     env->xen_vcpu_runstate_gpa = data.host_ulong;
422
423     kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
424                           env->xen_vcpu_runstate_gpa);
425 }
426
427 static void do_vcpu_soft_reset(CPUState *cs, run_on_cpu_data data)
428 {
429     X86CPU *cpu = X86_CPU(cs);
430     CPUX86State *env = &cpu->env;
431
432     env->xen_vcpu_info_gpa = INVALID_GPA;
433     env->xen_vcpu_info_default_gpa = INVALID_GPA;
434     env->xen_vcpu_time_info_gpa = INVALID_GPA;
435     env->xen_vcpu_runstate_gpa = INVALID_GPA;
436     env->xen_vcpu_callback_vector = 0;
437     env->xen_singleshot_timer_ns = 0;
438     memset(env->xen_virq, 0, sizeof(env->xen_virq));
439
440     set_vcpu_info(cs, INVALID_GPA);
441     kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
442                           INVALID_GPA);
443     kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
444                           INVALID_GPA);
445     if (kvm_xen_has_cap(EVTCHN_SEND)) {
446         kvm_xen_set_vcpu_callback_vector(cs);
447         kvm_xen_set_vcpu_timer(cs);
448     }
449
450 }
451
452 static int xen_set_shared_info(uint64_t gfn)
453 {
454     uint64_t gpa = gfn << TARGET_PAGE_BITS;
455     int i, err;
456
457     QEMU_IOTHREAD_LOCK_GUARD();
458
459     /*
460      * The xen_overlay device tells KVM about it too, since it had to
461      * do that on migration load anyway (unless we're going to jump
462      * through lots of hoops to maintain the fiction that this isn't
463      * KVM-specific.
464      */
465     err = xen_overlay_map_shinfo_page(gpa);
466     if (err) {
467             return err;
468     }
469
470     trace_kvm_xen_set_shared_info(gfn);
471
472     for (i = 0; i < XEN_LEGACY_MAX_VCPUS; i++) {
473         CPUState *cpu = qemu_get_cpu(i);
474         if (cpu) {
475             async_run_on_cpu(cpu, do_set_vcpu_info_default_gpa,
476                              RUN_ON_CPU_HOST_ULONG(gpa));
477         }
478         gpa += sizeof(vcpu_info_t);
479     }
480
481     return err;
482 }
483
484 static int add_to_physmap_one(uint32_t space, uint64_t idx, uint64_t gfn)
485 {
486     switch (space) {
487     case XENMAPSPACE_shared_info:
488         if (idx > 0) {
489             return -EINVAL;
490         }
491         return xen_set_shared_info(gfn);
492
493     case XENMAPSPACE_grant_table:
494     case XENMAPSPACE_gmfn:
495     case XENMAPSPACE_gmfn_range:
496         return -ENOTSUP;
497
498     case XENMAPSPACE_gmfn_foreign:
499     case XENMAPSPACE_dev_mmio:
500         return -EPERM;
501
502     default:
503         return -EINVAL;
504     }
505 }
506
507 static int do_add_to_physmap(struct kvm_xen_exit *exit, X86CPU *cpu,
508                              uint64_t arg)
509 {
510     struct xen_add_to_physmap xatp;
511     CPUState *cs = CPU(cpu);
512
513     if (hypercall_compat32(exit->u.hcall.longmode)) {
514         struct compat_xen_add_to_physmap xatp32;
515
516         qemu_build_assert(sizeof(struct compat_xen_add_to_physmap) == 16);
517         if (kvm_copy_from_gva(cs, arg, &xatp32, sizeof(xatp32))) {
518             return -EFAULT;
519         }
520         xatp.domid = xatp32.domid;
521         xatp.size = xatp32.size;
522         xatp.space = xatp32.space;
523         xatp.idx = xatp32.idx;
524         xatp.gpfn = xatp32.gpfn;
525     } else {
526         if (kvm_copy_from_gva(cs, arg, &xatp, sizeof(xatp))) {
527             return -EFAULT;
528         }
529     }
530
531     if (xatp.domid != DOMID_SELF && xatp.domid != xen_domid) {
532         return -ESRCH;
533     }
534
535     return add_to_physmap_one(xatp.space, xatp.idx, xatp.gpfn);
536 }
537
538 static int do_add_to_physmap_batch(struct kvm_xen_exit *exit, X86CPU *cpu,
539                                    uint64_t arg)
540 {
541     struct xen_add_to_physmap_batch xatpb;
542     unsigned long idxs_gva, gpfns_gva, errs_gva;
543     CPUState *cs = CPU(cpu);
544     size_t op_sz;
545
546     if (hypercall_compat32(exit->u.hcall.longmode)) {
547         struct compat_xen_add_to_physmap_batch xatpb32;
548
549         qemu_build_assert(sizeof(struct compat_xen_add_to_physmap_batch) == 20);
550         if (kvm_copy_from_gva(cs, arg, &xatpb32, sizeof(xatpb32))) {
551             return -EFAULT;
552         }
553         xatpb.domid = xatpb32.domid;
554         xatpb.space = xatpb32.space;
555         xatpb.size = xatpb32.size;
556
557         idxs_gva = xatpb32.idxs.c;
558         gpfns_gva = xatpb32.gpfns.c;
559         errs_gva = xatpb32.errs.c;
560         op_sz = sizeof(uint32_t);
561     } else {
562         if (kvm_copy_from_gva(cs, arg, &xatpb, sizeof(xatpb))) {
563             return -EFAULT;
564         }
565         op_sz = sizeof(unsigned long);
566         idxs_gva = (unsigned long)xatpb.idxs.p;
567         gpfns_gva = (unsigned long)xatpb.gpfns.p;
568         errs_gva = (unsigned long)xatpb.errs.p;
569     }
570
571     if (xatpb.domid != DOMID_SELF && xatpb.domid != xen_domid) {
572         return -ESRCH;
573     }
574
575     /* Explicitly invalid for the batch op. Not that we implement it anyway. */
576     if (xatpb.space == XENMAPSPACE_gmfn_range) {
577         return -EINVAL;
578     }
579
580     while (xatpb.size--) {
581         unsigned long idx = 0;
582         unsigned long gpfn = 0;
583         int err;
584
585         /* For 32-bit compat this only copies the low 32 bits of each */
586         if (kvm_copy_from_gva(cs, idxs_gva, &idx, op_sz) ||
587             kvm_copy_from_gva(cs, gpfns_gva, &gpfn, op_sz)) {
588             return -EFAULT;
589         }
590         idxs_gva += op_sz;
591         gpfns_gva += op_sz;
592
593         err = add_to_physmap_one(xatpb.space, idx, gpfn);
594
595         if (kvm_copy_to_gva(cs, errs_gva, &err, sizeof(err))) {
596             return -EFAULT;
597         }
598         errs_gva += sizeof(err);
599     }
600     return 0;
601 }
602
603 static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, X86CPU *cpu,
604                                    int cmd, uint64_t arg)
605 {
606     int err;
607
608     switch (cmd) {
609     case XENMEM_add_to_physmap:
610         err = do_add_to_physmap(exit, cpu, arg);
611         break;
612
613     case XENMEM_add_to_physmap_batch:
614         err = do_add_to_physmap_batch(exit, cpu, arg);
615         break;
616
617     default:
618         return false;
619     }
620
621     exit->u.hcall.result = err;
622     return true;
623 }
624
625 static bool handle_set_param(struct kvm_xen_exit *exit, X86CPU *cpu,
626                              uint64_t arg)
627 {
628     CPUState *cs = CPU(cpu);
629     struct xen_hvm_param hp;
630     int err = 0;
631
632     /* No need for 32/64 compat handling */
633     qemu_build_assert(sizeof(hp) == 16);
634
635     if (kvm_copy_from_gva(cs, arg, &hp, sizeof(hp))) {
636         err = -EFAULT;
637         goto out;
638     }
639
640     if (hp.domid != DOMID_SELF && hp.domid != xen_domid) {
641         err = -ESRCH;
642         goto out;
643     }
644
645     switch (hp.index) {
646     case HVM_PARAM_CALLBACK_IRQ:
647         err = xen_evtchn_set_callback_param(hp.value);
648         xen_set_long_mode(exit->u.hcall.longmode);
649         break;
650     default:
651         return false;
652     }
653
654 out:
655     exit->u.hcall.result = err;
656     return true;
657 }
658
659 static int kvm_xen_hcall_evtchn_upcall_vector(struct kvm_xen_exit *exit,
660                                               X86CPU *cpu, uint64_t arg)
661 {
662     struct xen_hvm_evtchn_upcall_vector up;
663     CPUState *target_cs;
664
665     /* No need for 32/64 compat handling */
666     qemu_build_assert(sizeof(up) == 8);
667
668     if (kvm_copy_from_gva(CPU(cpu), arg, &up, sizeof(up))) {
669         return -EFAULT;
670     }
671
672     if (up.vector < 0x10) {
673         return -EINVAL;
674     }
675
676     target_cs = qemu_get_cpu(up.vcpu);
677     if (!target_cs) {
678         return -EINVAL;
679     }
680
681     async_run_on_cpu(target_cs, do_set_vcpu_callback_vector,
682                      RUN_ON_CPU_HOST_INT(up.vector));
683     return 0;
684 }
685
686 static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu,
687                                  int cmd, uint64_t arg)
688 {
689     int ret = -ENOSYS;
690     switch (cmd) {
691     case HVMOP_set_evtchn_upcall_vector:
692         ret = kvm_xen_hcall_evtchn_upcall_vector(exit, cpu,
693                                                  exit->u.hcall.params[0]);
694         break;
695
696     case HVMOP_pagetable_dying:
697         ret = -ENOSYS;
698         break;
699
700     case HVMOP_set_param:
701         return handle_set_param(exit, cpu, arg);
702
703     default:
704         return false;
705     }
706
707     exit->u.hcall.result = ret;
708     return true;
709 }
710
711 static int vcpuop_register_vcpu_info(CPUState *cs, CPUState *target,
712                                      uint64_t arg)
713 {
714     struct vcpu_register_vcpu_info rvi;
715     uint64_t gpa;
716
717     /* No need for 32/64 compat handling */
718     qemu_build_assert(sizeof(rvi) == 16);
719     qemu_build_assert(sizeof(struct vcpu_info) == 64);
720
721     if (!target) {
722         return -ENOENT;
723     }
724
725     if (kvm_copy_from_gva(cs, arg, &rvi, sizeof(rvi))) {
726         return -EFAULT;
727     }
728
729     if (rvi.offset > TARGET_PAGE_SIZE - sizeof(struct vcpu_info)) {
730         return -EINVAL;
731     }
732
733     gpa = ((rvi.mfn << TARGET_PAGE_BITS) + rvi.offset);
734     async_run_on_cpu(target, do_set_vcpu_info_gpa, RUN_ON_CPU_HOST_ULONG(gpa));
735     return 0;
736 }
737
738 static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target,
739                                           uint64_t arg)
740 {
741     struct vcpu_register_time_memory_area tma;
742     uint64_t gpa;
743     size_t len;
744
745     /* No need for 32/64 compat handling */
746     qemu_build_assert(sizeof(tma) == 8);
747     qemu_build_assert(sizeof(struct vcpu_time_info) == 32);
748
749     if (!target) {
750         return -ENOENT;
751     }
752
753     if (kvm_copy_from_gva(cs, arg, &tma, sizeof(tma))) {
754         return -EFAULT;
755     }
756
757     /*
758      * Xen actually uses the GVA and does the translation through the guest
759      * page tables each time. But Linux/KVM uses the GPA, on the assumption
760      * that guests only ever use *global* addresses (kernel virtual addresses)
761      * for it. If Linux is changed to redo the GVA→GPA translation each time,
762      * it will offer a new vCPU attribute for that, and we'll use it instead.
763      */
764     if (!kvm_gva_to_gpa(cs, tma.addr.p, &gpa, &len, false) ||
765         len < sizeof(struct vcpu_time_info)) {
766         return -EFAULT;
767     }
768
769     async_run_on_cpu(target, do_set_vcpu_time_info_gpa,
770                      RUN_ON_CPU_HOST_ULONG(gpa));
771     return 0;
772 }
773
774 static int vcpuop_register_runstate_info(CPUState *cs, CPUState *target,
775                                          uint64_t arg)
776 {
777     struct vcpu_register_runstate_memory_area rma;
778     uint64_t gpa;
779     size_t len;
780
781     /* No need for 32/64 compat handling */
782     qemu_build_assert(sizeof(rma) == 8);
783     /* The runstate area actually does change size, but Linux copes. */
784
785     if (!target) {
786         return -ENOENT;
787     }
788
789     if (kvm_copy_from_gva(cs, arg, &rma, sizeof(rma))) {
790         return -EFAULT;
791     }
792
793     /* As with vcpu_time_info, Xen actually uses the GVA but KVM doesn't. */
794     if (!kvm_gva_to_gpa(cs, rma.addr.p, &gpa, &len, false)) {
795         return -EFAULT;
796     }
797
798     async_run_on_cpu(target, do_set_vcpu_runstate_gpa,
799                      RUN_ON_CPU_HOST_ULONG(gpa));
800     return 0;
801 }
802
803 static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
804                                   int cmd, int vcpu_id, uint64_t arg)
805 {
806     CPUState *dest = qemu_get_cpu(vcpu_id);
807     CPUState *cs = CPU(cpu);
808     int err;
809
810     switch (cmd) {
811     case VCPUOP_register_runstate_memory_area:
812         err = vcpuop_register_runstate_info(cs, dest, arg);
813         break;
814     case VCPUOP_register_vcpu_time_memory_area:
815         err = vcpuop_register_vcpu_time_info(cs, dest, arg);
816         break;
817     case VCPUOP_register_vcpu_info:
818         err = vcpuop_register_vcpu_info(cs, dest, arg);
819         break;
820
821     default:
822         return false;
823     }
824
825     exit->u.hcall.result = err;
826     return true;
827 }
828
829 static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu,
830                                     int cmd, uint64_t arg)
831 {
832     CPUState *cs = CPU(cpu);
833     int err = -ENOSYS;
834
835     switch (cmd) {
836     case EVTCHNOP_init_control:
837     case EVTCHNOP_expand_array:
838     case EVTCHNOP_set_priority:
839         /* We do not support FIFO channels at this point */
840         err = -ENOSYS;
841         break;
842
843     case EVTCHNOP_status: {
844         struct evtchn_status status;
845
846         qemu_build_assert(sizeof(status) == 24);
847         if (kvm_copy_from_gva(cs, arg, &status, sizeof(status))) {
848             err = -EFAULT;
849             break;
850         }
851
852         err = xen_evtchn_status_op(&status);
853         if (!err && kvm_copy_to_gva(cs, arg, &status, sizeof(status))) {
854             err = -EFAULT;
855         }
856         break;
857     }
858     case EVTCHNOP_close: {
859         struct evtchn_close close;
860
861         qemu_build_assert(sizeof(close) == 4);
862         if (kvm_copy_from_gva(cs, arg, &close, sizeof(close))) {
863             err = -EFAULT;
864             break;
865         }
866
867         err = xen_evtchn_close_op(&close);
868         break;
869     }
870     case EVTCHNOP_unmask: {
871         struct evtchn_unmask unmask;
872
873         qemu_build_assert(sizeof(unmask) == 4);
874         if (kvm_copy_from_gva(cs, arg, &unmask, sizeof(unmask))) {
875             err = -EFAULT;
876             break;
877         }
878
879         err = xen_evtchn_unmask_op(&unmask);
880         break;
881     }
882     case EVTCHNOP_bind_virq: {
883         struct evtchn_bind_virq virq;
884
885         qemu_build_assert(sizeof(virq) == 12);
886         if (kvm_copy_from_gva(cs, arg, &virq, sizeof(virq))) {
887             err = -EFAULT;
888             break;
889         }
890
891         err = xen_evtchn_bind_virq_op(&virq);
892         if (!err && kvm_copy_to_gva(cs, arg, &virq, sizeof(virq))) {
893             err = -EFAULT;
894         }
895         break;
896     }
897     case EVTCHNOP_bind_ipi: {
898         struct evtchn_bind_ipi ipi;
899
900         qemu_build_assert(sizeof(ipi) == 8);
901         if (kvm_copy_from_gva(cs, arg, &ipi, sizeof(ipi))) {
902             err = -EFAULT;
903             break;
904         }
905
906         err = xen_evtchn_bind_ipi_op(&ipi);
907         if (!err && kvm_copy_to_gva(cs, arg, &ipi, sizeof(ipi))) {
908             err = -EFAULT;
909         }
910         break;
911     }
912     default:
913         return false;
914     }
915
916     exit->u.hcall.result = err;
917     return true;
918 }
919
920 int kvm_xen_soft_reset(void)
921 {
922     CPUState *cpu;
923     int err;
924
925     assert(qemu_mutex_iothread_locked());
926
927     trace_kvm_xen_soft_reset();
928
929     /*
930      * Zero is the reset/startup state for HVM_PARAM_CALLBACK_IRQ. Strictly,
931      * it maps to HVM_PARAM_CALLBACK_TYPE_GSI with GSI#0, but Xen refuses to
932      * to deliver to the timer interrupt and treats that as 'disabled'.
933      */
934     err = xen_evtchn_set_callback_param(0);
935     if (err) {
936         return err;
937     }
938
939     CPU_FOREACH(cpu) {
940         async_run_on_cpu(cpu, do_vcpu_soft_reset, RUN_ON_CPU_NULL);
941     }
942
943     err = xen_overlay_map_shinfo_page(INVALID_GFN);
944     if (err) {
945         return err;
946     }
947
948     return 0;
949 }
950
951 static int schedop_shutdown(CPUState *cs, uint64_t arg)
952 {
953     struct sched_shutdown shutdown;
954     int ret = 0;
955
956     /* No need for 32/64 compat handling */
957     qemu_build_assert(sizeof(shutdown) == 4);
958
959     if (kvm_copy_from_gva(cs, arg, &shutdown, sizeof(shutdown))) {
960         return -EFAULT;
961     }
962
963     switch (shutdown.reason) {
964     case SHUTDOWN_crash:
965         cpu_dump_state(cs, stderr, CPU_DUMP_CODE);
966         qemu_system_guest_panicked(NULL);
967         break;
968
969     case SHUTDOWN_reboot:
970         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
971         break;
972
973     case SHUTDOWN_poweroff:
974         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
975         break;
976
977     case SHUTDOWN_soft_reset:
978         qemu_mutex_lock_iothread();
979         ret = kvm_xen_soft_reset();
980         qemu_mutex_unlock_iothread();
981         break;
982
983     default:
984         ret = -EINVAL;
985         break;
986     }
987
988     return ret;
989 }
990
991 static bool kvm_xen_hcall_sched_op(struct kvm_xen_exit *exit, X86CPU *cpu,
992                                    int cmd, uint64_t arg)
993 {
994     CPUState *cs = CPU(cpu);
995     int err = -ENOSYS;
996
997     switch (cmd) {
998     case SCHEDOP_shutdown:
999         err = schedop_shutdown(cs, arg);
1000         break;
1001
1002     case SCHEDOP_poll:
1003         /*
1004          * Linux will panic if this doesn't work. Just yield; it's not
1005          * worth overthinking it because with event channel handling
1006          * in KVM, the kernel will intercept this and it will never
1007          * reach QEMU anyway. The semantics of the hypercall explicltly
1008          * permit spurious wakeups.
1009          */
1010     case SCHEDOP_yield:
1011         sched_yield();
1012         err = 0;
1013         break;
1014
1015     default:
1016         return false;
1017     }
1018
1019     exit->u.hcall.result = err;
1020     return true;
1021 }
1022
1023 static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
1024 {
1025     uint16_t code = exit->u.hcall.input;
1026
1027     if (exit->u.hcall.cpl > 0) {
1028         exit->u.hcall.result = -EPERM;
1029         return true;
1030     }
1031
1032     switch (code) {
1033     case __HYPERVISOR_sched_op:
1034         return kvm_xen_hcall_sched_op(exit, cpu, exit->u.hcall.params[0],
1035                                       exit->u.hcall.params[1]);
1036     case __HYPERVISOR_event_channel_op:
1037         return kvm_xen_hcall_evtchn_op(exit, cpu, exit->u.hcall.params[0],
1038                                        exit->u.hcall.params[1]);
1039     case __HYPERVISOR_vcpu_op:
1040         return kvm_xen_hcall_vcpu_op(exit, cpu,
1041                                      exit->u.hcall.params[0],
1042                                      exit->u.hcall.params[1],
1043                                      exit->u.hcall.params[2]);
1044     case __HYPERVISOR_hvm_op:
1045         return kvm_xen_hcall_hvm_op(exit, cpu, exit->u.hcall.params[0],
1046                                     exit->u.hcall.params[1]);
1047     case __HYPERVISOR_memory_op:
1048         return kvm_xen_hcall_memory_op(exit, cpu, exit->u.hcall.params[0],
1049                                        exit->u.hcall.params[1]);
1050     case __HYPERVISOR_xen_version:
1051         return kvm_xen_hcall_xen_version(exit, cpu, exit->u.hcall.params[0],
1052                                          exit->u.hcall.params[1]);
1053     default:
1054         return false;
1055     }
1056 }
1057
1058 int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
1059 {
1060     if (exit->type != KVM_EXIT_XEN_HCALL) {
1061         return -1;
1062     }
1063
1064     /*
1065      * The kernel latches the guest 32/64 mode when the MSR is used to fill
1066      * the hypercall page. So if we see a hypercall in a mode that doesn't
1067      * match our own idea of the guest mode, fetch the kernel's idea of the
1068      * "long mode" to remain in sync.
1069      */
1070     if (exit->u.hcall.longmode != xen_is_long_mode()) {
1071         xen_sync_long_mode();
1072     }
1073
1074     if (!do_kvm_xen_handle_exit(cpu, exit)) {
1075         /*
1076          * Some hypercalls will be deliberately "implemented" by returning
1077          * -ENOSYS. This case is for hypercalls which are unexpected.
1078          */
1079         exit->u.hcall.result = -ENOSYS;
1080         qemu_log_mask(LOG_UNIMP, "Unimplemented Xen hypercall %"
1081                       PRId64 " (0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 ")\n",
1082                       (uint64_t)exit->u.hcall.input,
1083                       (uint64_t)exit->u.hcall.params[0],
1084                       (uint64_t)exit->u.hcall.params[1],
1085                       (uint64_t)exit->u.hcall.params[2]);
1086     }
1087
1088     trace_kvm_xen_hypercall(CPU(cpu)->cpu_index, exit->u.hcall.cpl,
1089                             exit->u.hcall.input, exit->u.hcall.params[0],
1090                             exit->u.hcall.params[1], exit->u.hcall.params[2],
1091                             exit->u.hcall.result);
1092     return 0;
1093 }
1094
1095 int kvm_put_xen_state(CPUState *cs)
1096 {
1097     X86CPU *cpu = X86_CPU(cs);
1098     CPUX86State *env = &cpu->env;
1099     uint64_t gpa;
1100     int ret;
1101
1102     gpa = env->xen_vcpu_info_gpa;
1103     if (gpa == INVALID_GPA) {
1104         gpa = env->xen_vcpu_info_default_gpa;
1105     }
1106
1107     if (gpa != INVALID_GPA) {
1108         ret = set_vcpu_info(cs, gpa);
1109         if (ret < 0) {
1110             return ret;
1111         }
1112     }
1113
1114     gpa = env->xen_vcpu_time_info_gpa;
1115     if (gpa != INVALID_GPA) {
1116         ret = kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
1117                                     gpa);
1118         if (ret < 0) {
1119             return ret;
1120         }
1121     }
1122
1123     gpa = env->xen_vcpu_runstate_gpa;
1124     if (gpa != INVALID_GPA) {
1125         ret = kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
1126                                     gpa);
1127         if (ret < 0) {
1128             return ret;
1129         }
1130     }
1131
1132     if (!kvm_xen_has_cap(EVTCHN_SEND)) {
1133         return 0;
1134     }
1135
1136     if (env->xen_vcpu_callback_vector) {
1137         ret = kvm_xen_set_vcpu_callback_vector(cs);
1138         if (ret < 0) {
1139             return ret;
1140         }
1141     }
1142
1143     if (env->xen_virq[VIRQ_TIMER]) {
1144         ret = kvm_xen_set_vcpu_timer(cs);
1145         if (ret < 0) {
1146             return ret;
1147         }
1148     }
1149     return 0;
1150 }
1151
1152 int kvm_get_xen_state(CPUState *cs)
1153 {
1154     X86CPU *cpu = X86_CPU(cs);
1155     CPUX86State *env = &cpu->env;
1156     uint64_t gpa;
1157     int ret;
1158
1159     /*
1160      * The kernel does not mark vcpu_info as dirty when it delivers interrupts
1161      * to it. It's up to userspace to *assume* that any page shared thus is
1162      * always considered dirty. The shared_info page is different since it's
1163      * an overlay and migrated separately anyway.
1164      */
1165     gpa = env->xen_vcpu_info_gpa;
1166     if (gpa == INVALID_GPA) {
1167         gpa = env->xen_vcpu_info_default_gpa;
1168     }
1169     if (gpa != INVALID_GPA) {
1170         MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1171                                                      gpa,
1172                                                      sizeof(struct vcpu_info));
1173         if (mrs.mr &&
1174             !int128_lt(mrs.size, int128_make64(sizeof(struct vcpu_info)))) {
1175             memory_region_set_dirty(mrs.mr, mrs.offset_within_region,
1176                                     sizeof(struct vcpu_info));
1177         }
1178     }
1179
1180     if (!kvm_xen_has_cap(EVTCHN_SEND)) {
1181         return 0;
1182     }
1183
1184     /*
1185      * If the kernel is accelerating timers, read out the current value of the
1186      * singleshot timer deadline.
1187      */
1188     if (env->xen_virq[VIRQ_TIMER]) {
1189         struct kvm_xen_vcpu_attr va = {
1190             .type = KVM_XEN_VCPU_ATTR_TYPE_TIMER,
1191         };
1192         ret = kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_GET_ATTR, &va);
1193         if (ret < 0) {
1194             return ret;
1195         }
1196         env->xen_singleshot_timer_ns = va.u.timer.expires_ns;
1197     }
1198
1199     return 0;
1200 }