xref: /openbmc/qemu/hw/i386/xen/xen-hvm.c (revision 0a553c58)
1 /*
2  * Copyright (C) 2010       Citrix Ltd.
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2.  See
5  * the COPYING file in the top-level directory.
6  *
7  * Contributions after 2012-01-13 are licensed under the terms of the
8  * GNU GPL, version 2 or (at your option) any later version.
9  */
10 
11 #include "qemu/osdep.h"
12 
13 #include "cpu.h"
14 #include "hw/pci/pci.h"
15 #include "hw/pci/pci_host.h"
16 #include "hw/i386/pc.h"
17 #include "hw/i386/apic-msidef.h"
18 #include "hw/xen/xen_common.h"
19 #include "hw/xen/xen-legacy-backend.h"
20 #include "hw/xen/xen-bus.h"
21 #include "qapi/error.h"
22 #include "qapi/qapi-commands-misc.h"
23 #include "qemu/error-report.h"
24 #include "qemu/range.h"
25 #include "sysemu/xen-mapcache.h"
26 #include "trace.h"
27 #include "exec/address-spaces.h"
28 
29 #include <xen/hvm/ioreq.h>
30 #include <xen/hvm/params.h>
31 #include <xen/hvm/e820.h>
32 
33 //#define DEBUG_XEN_HVM
34 
35 #ifdef DEBUG_XEN_HVM
36 #define DPRINTF(fmt, ...) \
37     do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
38 #else
39 #define DPRINTF(fmt, ...) \
40     do { } while (0)
41 #endif
42 
43 static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
44 static MemoryRegion *framebuffer;
45 static bool xen_in_migration;
46 
47 /* Compatibility with older version */
48 
49 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
50  * installed.  This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
51  * needs to be included before this block and hw/xen/xen_common.h needs to
52  * be included before xen/hvm/ioreq.h
53  */
54 #ifndef IOREQ_TYPE_VMWARE_PORT
55 #define IOREQ_TYPE_VMWARE_PORT  3
56 struct vmware_regs {
57     uint32_t esi;
58     uint32_t edi;
59     uint32_t ebx;
60     uint32_t ecx;
61     uint32_t edx;
62 };
63 typedef struct vmware_regs vmware_regs_t;
64 
65 struct shared_vmport_iopage {
66     struct vmware_regs vcpu_vmport_regs[1];
67 };
68 typedef struct shared_vmport_iopage shared_vmport_iopage_t;
69 #endif
70 
71 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
72 {
73     return shared_page->vcpu_ioreq[i].vp_eport;
74 }
75 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
76 {
77     return &shared_page->vcpu_ioreq[vcpu];
78 }
79 
80 #define BUFFER_IO_MAX_DELAY  100
81 
82 typedef struct XenPhysmap {
83     hwaddr start_addr;
84     ram_addr_t size;
85     const char *name;
86     hwaddr phys_offset;
87 
88     QLIST_ENTRY(XenPhysmap) list;
89 } XenPhysmap;
90 
91 static QLIST_HEAD(, XenPhysmap) xen_physmap;
92 
93 typedef struct XenPciDevice {
94     PCIDevice *pci_dev;
95     uint32_t sbdf;
96     QLIST_ENTRY(XenPciDevice) entry;
97 } XenPciDevice;
98 
99 typedef struct XenIOState {
100     ioservid_t ioservid;
101     shared_iopage_t *shared_page;
102     shared_vmport_iopage_t *shared_vmport_page;
103     buffered_iopage_t *buffered_io_page;
104     QEMUTimer *buffered_io_timer;
105     CPUState **cpu_by_vcpu_id;
106     /* the evtchn port for polling the notification, */
107     evtchn_port_t *ioreq_local_port;
108     /* evtchn remote and local ports for buffered io */
109     evtchn_port_t bufioreq_remote_port;
110     evtchn_port_t bufioreq_local_port;
111     /* the evtchn fd for polling */
112     xenevtchn_handle *xce_handle;
113     /* which vcpu we are serving */
114     int send_vcpu;
115 
116     struct xs_handle *xenstore;
117     MemoryListener memory_listener;
118     MemoryListener io_listener;
119     QLIST_HEAD(, XenPciDevice) dev_list;
120     DeviceListener device_listener;
121     hwaddr free_phys_offset;
122     const XenPhysmap *log_for_dirtybit;
123 
124     Notifier exit;
125     Notifier suspend;
126     Notifier wakeup;
127 } XenIOState;
128 
129 /* Xen specific function for piix pci */
130 
131 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
132 {
133     return irq_num + ((pci_dev->devfn >> 3) << 2);
134 }
135 
136 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
137 {
138     xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
139                            irq_num & 3, level);
140 }
141 
142 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
143 {
144     int i;
145 
146     /* Scan for updates to PCI link routes (0x60-0x63). */
147     for (i = 0; i < len; i++) {
148         uint8_t v = (val >> (8 * i)) & 0xff;
149         if (v & 0x80) {
150             v = 0;
151         }
152         v &= 0xf;
153         if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
154             xen_set_pci_link_route(xen_domid, address + i - 0x60, v);
155         }
156     }
157 }
158 
159 int xen_is_pirq_msi(uint32_t msi_data)
160 {
161     /* If vector is 0, the msi is remapped into a pirq, passed as
162      * dest_id.
163      */
164     return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
165 }
166 
167 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
168 {
169     xen_inject_msi(xen_domid, addr, data);
170 }
171 
172 static void xen_suspend_notifier(Notifier *notifier, void *data)
173 {
174     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
175 }
176 
177 /* Xen Interrupt Controller */
178 
179 static void xen_set_irq(void *opaque, int irq, int level)
180 {
181     xen_set_isa_irq_level(xen_domid, irq, level);
182 }
183 
184 qemu_irq *xen_interrupt_controller_init(void)
185 {
186     return qemu_allocate_irqs(xen_set_irq, NULL, 16);
187 }
188 
189 /* Memory Ops */
190 
191 static void xen_ram_init(PCMachineState *pcms,
192                          ram_addr_t ram_size, MemoryRegion **ram_memory_p)
193 {
194     MemoryRegion *sysmem = get_system_memory();
195     ram_addr_t block_len;
196     uint64_t user_lowmem = object_property_get_uint(qdev_get_machine(),
197                                                     PC_MACHINE_MAX_RAM_BELOW_4G,
198                                                     &error_abort);
199 
200     /* Handle the machine opt max-ram-below-4g.  It is basically doing
201      * min(xen limit, user limit).
202      */
203     if (!user_lowmem) {
204         user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
205     }
206     if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
207         user_lowmem = HVM_BELOW_4G_RAM_END;
208     }
209 
210     if (ram_size >= user_lowmem) {
211         pcms->above_4g_mem_size = ram_size - user_lowmem;
212         pcms->below_4g_mem_size = user_lowmem;
213     } else {
214         pcms->above_4g_mem_size = 0;
215         pcms->below_4g_mem_size = ram_size;
216     }
217     if (!pcms->above_4g_mem_size) {
218         block_len = ram_size;
219     } else {
220         /*
221          * Xen does not allocate the memory continuously, it keeps a
222          * hole of the size computed above or passed in.
223          */
224         block_len = (1ULL << 32) + pcms->above_4g_mem_size;
225     }
226     memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
227                            &error_fatal);
228     *ram_memory_p = &ram_memory;
229 
230     memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
231                              &ram_memory, 0, 0xa0000);
232     memory_region_add_subregion(sysmem, 0, &ram_640k);
233     /* Skip of the VGA IO memory space, it will be registered later by the VGA
234      * emulated device.
235      *
236      * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
237      * the Options ROM, so it is registered here as RAM.
238      */
239     memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
240                              &ram_memory, 0xc0000,
241                              pcms->below_4g_mem_size - 0xc0000);
242     memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
243     if (pcms->above_4g_mem_size > 0) {
244         memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
245                                  &ram_memory, 0x100000000ULL,
246                                  pcms->above_4g_mem_size);
247         memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
248     }
249 }
250 
251 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
252                    Error **errp)
253 {
254     unsigned long nr_pfn;
255     xen_pfn_t *pfn_list;
256     int i;
257 
258     if (runstate_check(RUN_STATE_INMIGRATE)) {
259         /* RAM already populated in Xen */
260         fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
261                 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
262                 __func__, size, ram_addr);
263         return;
264     }
265 
266     if (mr == &ram_memory) {
267         return;
268     }
269 
270     trace_xen_ram_alloc(ram_addr, size);
271 
272     nr_pfn = size >> TARGET_PAGE_BITS;
273     pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
274 
275     for (i = 0; i < nr_pfn; i++) {
276         pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
277     }
278 
279     if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
280         error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
281                    ram_addr);
282     }
283 
284     g_free(pfn_list);
285 }
286 
287 static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size)
288 {
289     XenPhysmap *physmap = NULL;
290 
291     start_addr &= TARGET_PAGE_MASK;
292 
293     QLIST_FOREACH(physmap, &xen_physmap, list) {
294         if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
295             return physmap;
296         }
297     }
298     return NULL;
299 }
300 
301 static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size)
302 {
303     hwaddr addr = phys_offset & TARGET_PAGE_MASK;
304     XenPhysmap *physmap = NULL;
305 
306     QLIST_FOREACH(physmap, &xen_physmap, list) {
307         if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
308             return physmap->start_addr + (phys_offset - physmap->phys_offset);
309         }
310     }
311 
312     return phys_offset;
313 }
314 
315 #ifdef XEN_COMPAT_PHYSMAP
316 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
317 {
318     char path[80], value[17];
319 
320     snprintf(path, sizeof(path),
321             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
322             xen_domid, (uint64_t)physmap->phys_offset);
323     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
324     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
325         return -1;
326     }
327     snprintf(path, sizeof(path),
328             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
329             xen_domid, (uint64_t)physmap->phys_offset);
330     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
331     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
332         return -1;
333     }
334     if (physmap->name) {
335         snprintf(path, sizeof(path),
336                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
337                 xen_domid, (uint64_t)physmap->phys_offset);
338         if (!xs_write(state->xenstore, 0, path,
339                       physmap->name, strlen(physmap->name))) {
340             return -1;
341         }
342     }
343     return 0;
344 }
345 #else
346 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
347 {
348     return 0;
349 }
350 #endif
351 
352 static int xen_add_to_physmap(XenIOState *state,
353                               hwaddr start_addr,
354                               ram_addr_t size,
355                               MemoryRegion *mr,
356                               hwaddr offset_within_region)
357 {
358     unsigned long nr_pages;
359     int rc = 0;
360     XenPhysmap *physmap = NULL;
361     hwaddr pfn, start_gpfn;
362     hwaddr phys_offset = memory_region_get_ram_addr(mr);
363     const char *mr_name;
364 
365     if (get_physmapping(start_addr, size)) {
366         return 0;
367     }
368     if (size <= 0) {
369         return -1;
370     }
371 
372     /* Xen can only handle a single dirty log region for now and we want
373      * the linear framebuffer to be that region.
374      * Avoid tracking any regions that is not videoram and avoid tracking
375      * the legacy vga region. */
376     if (mr == framebuffer && start_addr > 0xbffff) {
377         goto go_physmap;
378     }
379     return -1;
380 
381 go_physmap:
382     DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
383             start_addr, start_addr + size);
384 
385     mr_name = memory_region_name(mr);
386 
387     physmap = g_malloc(sizeof(XenPhysmap));
388 
389     physmap->start_addr = start_addr;
390     physmap->size = size;
391     physmap->name = mr_name;
392     physmap->phys_offset = phys_offset;
393 
394     QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
395 
396     if (runstate_check(RUN_STATE_INMIGRATE)) {
397         /* Now when we have a physmap entry we can replace a dummy mapping with
398          * a real one of guest foreign memory. */
399         uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
400         assert(p && p == memory_region_get_ram_ptr(mr));
401 
402         return 0;
403     }
404 
405     pfn = phys_offset >> TARGET_PAGE_BITS;
406     start_gpfn = start_addr >> TARGET_PAGE_BITS;
407     nr_pages = size >> TARGET_PAGE_BITS;
408     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
409                                         start_gpfn);
410     if (rc) {
411         int saved_errno = errno;
412 
413         error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
414                      " to GFN %"HWADDR_PRIx" failed: %s",
415                      nr_pages, pfn, start_gpfn, strerror(saved_errno));
416         errno = saved_errno;
417         return -1;
418     }
419 
420     rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
421                                    start_addr >> TARGET_PAGE_BITS,
422                                    (start_addr + size - 1) >> TARGET_PAGE_BITS,
423                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
424     if (rc) {
425         error_report("pin_memory_cacheattr failed: %s", strerror(errno));
426     }
427     return xen_save_physmap(state, physmap);
428 }
429 
430 static int xen_remove_from_physmap(XenIOState *state,
431                                    hwaddr start_addr,
432                                    ram_addr_t size)
433 {
434     int rc = 0;
435     XenPhysmap *physmap = NULL;
436     hwaddr phys_offset = 0;
437 
438     physmap = get_physmapping(start_addr, size);
439     if (physmap == NULL) {
440         return -1;
441     }
442 
443     phys_offset = physmap->phys_offset;
444     size = physmap->size;
445 
446     DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
447             "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
448 
449     size >>= TARGET_PAGE_BITS;
450     start_addr >>= TARGET_PAGE_BITS;
451     phys_offset >>= TARGET_PAGE_BITS;
452     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
453                                         phys_offset);
454     if (rc) {
455         int saved_errno = errno;
456 
457         error_report("relocate_memory "RAM_ADDR_FMT" pages"
458                      " from GFN %"HWADDR_PRIx
459                      " to GFN %"HWADDR_PRIx" failed: %s",
460                      size, start_addr, phys_offset, strerror(saved_errno));
461         errno = saved_errno;
462         return -1;
463     }
464 
465     QLIST_REMOVE(physmap, list);
466     if (state->log_for_dirtybit == physmap) {
467         state->log_for_dirtybit = NULL;
468     }
469     g_free(physmap);
470 
471     return 0;
472 }
473 
474 static void xen_set_memory(struct MemoryListener *listener,
475                            MemoryRegionSection *section,
476                            bool add)
477 {
478     XenIOState *state = container_of(listener, XenIOState, memory_listener);
479     hwaddr start_addr = section->offset_within_address_space;
480     ram_addr_t size = int128_get64(section->size);
481     bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
482     hvmmem_type_t mem_type;
483 
484     if (section->mr == &ram_memory) {
485         return;
486     } else {
487         if (add) {
488             xen_map_memory_section(xen_domid, state->ioservid,
489                                    section);
490         } else {
491             xen_unmap_memory_section(xen_domid, state->ioservid,
492                                      section);
493         }
494     }
495 
496     if (!memory_region_is_ram(section->mr)) {
497         return;
498     }
499 
500     if (log_dirty != add) {
501         return;
502     }
503 
504     trace_xen_client_set_memory(start_addr, size, log_dirty);
505 
506     start_addr &= TARGET_PAGE_MASK;
507     size = TARGET_PAGE_ALIGN(size);
508 
509     if (add) {
510         if (!memory_region_is_rom(section->mr)) {
511             xen_add_to_physmap(state, start_addr, size,
512                                section->mr, section->offset_within_region);
513         } else {
514             mem_type = HVMMEM_ram_ro;
515             if (xen_set_mem_type(xen_domid, mem_type,
516                                  start_addr >> TARGET_PAGE_BITS,
517                                  size >> TARGET_PAGE_BITS)) {
518                 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx"\n",
519                         start_addr);
520             }
521         }
522     } else {
523         if (xen_remove_from_physmap(state, start_addr, size) < 0) {
524             DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
525         }
526     }
527 }
528 
529 static void xen_region_add(MemoryListener *listener,
530                            MemoryRegionSection *section)
531 {
532     memory_region_ref(section->mr);
533     xen_set_memory(listener, section, true);
534 }
535 
536 static void xen_region_del(MemoryListener *listener,
537                            MemoryRegionSection *section)
538 {
539     xen_set_memory(listener, section, false);
540     memory_region_unref(section->mr);
541 }
542 
543 static void xen_io_add(MemoryListener *listener,
544                        MemoryRegionSection *section)
545 {
546     XenIOState *state = container_of(listener, XenIOState, io_listener);
547     MemoryRegion *mr = section->mr;
548 
549     if (mr->ops == &unassigned_io_ops) {
550         return;
551     }
552 
553     memory_region_ref(mr);
554 
555     xen_map_io_section(xen_domid, state->ioservid, section);
556 }
557 
558 static void xen_io_del(MemoryListener *listener,
559                        MemoryRegionSection *section)
560 {
561     XenIOState *state = container_of(listener, XenIOState, io_listener);
562     MemoryRegion *mr = section->mr;
563 
564     if (mr->ops == &unassigned_io_ops) {
565         return;
566     }
567 
568     xen_unmap_io_section(xen_domid, state->ioservid, section);
569 
570     memory_region_unref(mr);
571 }
572 
573 static void xen_device_realize(DeviceListener *listener,
574                                DeviceState *dev)
575 {
576     XenIOState *state = container_of(listener, XenIOState, device_listener);
577 
578     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
579         PCIDevice *pci_dev = PCI_DEVICE(dev);
580         XenPciDevice *xendev = g_new(XenPciDevice, 1);
581 
582         xendev->pci_dev = pci_dev;
583         xendev->sbdf = PCI_BUILD_BDF(pci_dev_bus_num(pci_dev),
584                                      pci_dev->devfn);
585         QLIST_INSERT_HEAD(&state->dev_list, xendev, entry);
586 
587         xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
588     }
589 }
590 
591 static void xen_device_unrealize(DeviceListener *listener,
592                                  DeviceState *dev)
593 {
594     XenIOState *state = container_of(listener, XenIOState, device_listener);
595 
596     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
597         PCIDevice *pci_dev = PCI_DEVICE(dev);
598         XenPciDevice *xendev, *next;
599 
600         xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
601 
602         QLIST_FOREACH_SAFE(xendev, &state->dev_list, entry, next) {
603             if (xendev->pci_dev == pci_dev) {
604                 QLIST_REMOVE(xendev, entry);
605                 g_free(xendev);
606                 break;
607             }
608         }
609     }
610 }
611 
612 static void xen_sync_dirty_bitmap(XenIOState *state,
613                                   hwaddr start_addr,
614                                   ram_addr_t size)
615 {
616     hwaddr npages = size >> TARGET_PAGE_BITS;
617     const int width = sizeof(unsigned long) * 8;
618     unsigned long bitmap[DIV_ROUND_UP(npages, width)];
619     int rc, i, j;
620     const XenPhysmap *physmap = NULL;
621 
622     physmap = get_physmapping(start_addr, size);
623     if (physmap == NULL) {
624         /* not handled */
625         return;
626     }
627 
628     if (state->log_for_dirtybit == NULL) {
629         state->log_for_dirtybit = physmap;
630     } else if (state->log_for_dirtybit != physmap) {
631         /* Only one range for dirty bitmap can be tracked. */
632         return;
633     }
634 
635     rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
636                               npages, bitmap);
637     if (rc < 0) {
638 #ifndef ENODATA
639 #define ENODATA  ENOENT
640 #endif
641         if (errno == ENODATA) {
642             memory_region_set_dirty(framebuffer, 0, size);
643             DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
644                     ", 0x" TARGET_FMT_plx "): %s\n",
645                     start_addr, start_addr + size, strerror(errno));
646         }
647         return;
648     }
649 
650     for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
651         unsigned long map = bitmap[i];
652         while (map != 0) {
653             j = ctzl(map);
654             map &= ~(1ul << j);
655             memory_region_set_dirty(framebuffer,
656                                     (i * width + j) * TARGET_PAGE_SIZE,
657                                     TARGET_PAGE_SIZE);
658         };
659     }
660 }
661 
662 static void xen_log_start(MemoryListener *listener,
663                           MemoryRegionSection *section,
664                           int old, int new)
665 {
666     XenIOState *state = container_of(listener, XenIOState, memory_listener);
667 
668     if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
669         xen_sync_dirty_bitmap(state, section->offset_within_address_space,
670                               int128_get64(section->size));
671     }
672 }
673 
674 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
675                          int old, int new)
676 {
677     XenIOState *state = container_of(listener, XenIOState, memory_listener);
678 
679     if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
680         state->log_for_dirtybit = NULL;
681         /* Disable dirty bit tracking */
682         xen_track_dirty_vram(xen_domid, 0, 0, NULL);
683     }
684 }
685 
686 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
687 {
688     XenIOState *state = container_of(listener, XenIOState, memory_listener);
689 
690     xen_sync_dirty_bitmap(state, section->offset_within_address_space,
691                           int128_get64(section->size));
692 }
693 
694 static void xen_log_global_start(MemoryListener *listener)
695 {
696     if (xen_enabled()) {
697         xen_in_migration = true;
698     }
699 }
700 
701 static void xen_log_global_stop(MemoryListener *listener)
702 {
703     xen_in_migration = false;
704 }
705 
706 static MemoryListener xen_memory_listener = {
707     .region_add = xen_region_add,
708     .region_del = xen_region_del,
709     .log_start = xen_log_start,
710     .log_stop = xen_log_stop,
711     .log_sync = xen_log_sync,
712     .log_global_start = xen_log_global_start,
713     .log_global_stop = xen_log_global_stop,
714     .priority = 10,
715 };
716 
717 static MemoryListener xen_io_listener = {
718     .region_add = xen_io_add,
719     .region_del = xen_io_del,
720     .priority = 10,
721 };
722 
723 static DeviceListener xen_device_listener = {
724     .realize = xen_device_realize,
725     .unrealize = xen_device_unrealize,
726 };
727 
728 /* get the ioreq packets from share mem */
729 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
730 {
731     ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
732 
733     if (req->state != STATE_IOREQ_READY) {
734         DPRINTF("I/O request not ready: "
735                 "%x, ptr: %x, port: %"PRIx64", "
736                 "data: %"PRIx64", count: %u, size: %u\n",
737                 req->state, req->data_is_ptr, req->addr,
738                 req->data, req->count, req->size);
739         return NULL;
740     }
741 
742     xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
743 
744     req->state = STATE_IOREQ_INPROCESS;
745     return req;
746 }
747 
748 /* use poll to get the port notification */
749 /* ioreq_vec--out,the */
750 /* retval--the number of ioreq packet */
751 static ioreq_t *cpu_get_ioreq(XenIOState *state)
752 {
753     int i;
754     evtchn_port_t port;
755 
756     port = xenevtchn_pending(state->xce_handle);
757     if (port == state->bufioreq_local_port) {
758         timer_mod(state->buffered_io_timer,
759                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
760         return NULL;
761     }
762 
763     if (port != -1) {
764         for (i = 0; i < max_cpus; i++) {
765             if (state->ioreq_local_port[i] == port) {
766                 break;
767             }
768         }
769 
770         if (i == max_cpus) {
771             hw_error("Fatal error while trying to get io event!\n");
772         }
773 
774         /* unmask the wanted port again */
775         xenevtchn_unmask(state->xce_handle, port);
776 
777         /* get the io packet from shared memory */
778         state->send_vcpu = i;
779         return cpu_get_ioreq_from_shared_memory(state, i);
780     }
781 
782     /* read error or read nothing */
783     return NULL;
784 }
785 
786 static uint32_t do_inp(uint32_t addr, unsigned long size)
787 {
788     switch (size) {
789         case 1:
790             return cpu_inb(addr);
791         case 2:
792             return cpu_inw(addr);
793         case 4:
794             return cpu_inl(addr);
795         default:
796             hw_error("inp: bad size: %04x %lx", addr, size);
797     }
798 }
799 
800 static void do_outp(uint32_t addr,
801         unsigned long size, uint32_t val)
802 {
803     switch (size) {
804         case 1:
805             return cpu_outb(addr, val);
806         case 2:
807             return cpu_outw(addr, val);
808         case 4:
809             return cpu_outl(addr, val);
810         default:
811             hw_error("outp: bad size: %04x %lx", addr, size);
812     }
813 }
814 
815 /*
816  * Helper functions which read/write an object from/to physical guest
817  * memory, as part of the implementation of an ioreq.
818  *
819  * Equivalent to
820  *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
821  *                          val, req->size, 0/1)
822  * except without the integer overflow problems.
823  */
824 static void rw_phys_req_item(hwaddr addr,
825                              ioreq_t *req, uint32_t i, void *val, int rw)
826 {
827     /* Do everything unsigned so overflow just results in a truncated result
828      * and accesses to undesired parts of guest memory, which is up
829      * to the guest */
830     hwaddr offset = (hwaddr)req->size * i;
831     if (req->df) {
832         addr -= offset;
833     } else {
834         addr += offset;
835     }
836     cpu_physical_memory_rw(addr, val, req->size, rw);
837 }
838 
839 static inline void read_phys_req_item(hwaddr addr,
840                                       ioreq_t *req, uint32_t i, void *val)
841 {
842     rw_phys_req_item(addr, req, i, val, 0);
843 }
844 static inline void write_phys_req_item(hwaddr addr,
845                                        ioreq_t *req, uint32_t i, void *val)
846 {
847     rw_phys_req_item(addr, req, i, val, 1);
848 }
849 
850 
851 static void cpu_ioreq_pio(ioreq_t *req)
852 {
853     uint32_t i;
854 
855     trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
856                          req->data, req->count, req->size);
857 
858     if (req->size > sizeof(uint32_t)) {
859         hw_error("PIO: bad size (%u)", req->size);
860     }
861 
862     if (req->dir == IOREQ_READ) {
863         if (!req->data_is_ptr) {
864             req->data = do_inp(req->addr, req->size);
865             trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
866                                          req->size);
867         } else {
868             uint32_t tmp;
869 
870             for (i = 0; i < req->count; i++) {
871                 tmp = do_inp(req->addr, req->size);
872                 write_phys_req_item(req->data, req, i, &tmp);
873             }
874         }
875     } else if (req->dir == IOREQ_WRITE) {
876         if (!req->data_is_ptr) {
877             trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
878                                           req->size);
879             do_outp(req->addr, req->size, req->data);
880         } else {
881             for (i = 0; i < req->count; i++) {
882                 uint32_t tmp = 0;
883 
884                 read_phys_req_item(req->data, req, i, &tmp);
885                 do_outp(req->addr, req->size, tmp);
886             }
887         }
888     }
889 }
890 
891 static void cpu_ioreq_move(ioreq_t *req)
892 {
893     uint32_t i;
894 
895     trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
896                          req->data, req->count, req->size);
897 
898     if (req->size > sizeof(req->data)) {
899         hw_error("MMIO: bad size (%u)", req->size);
900     }
901 
902     if (!req->data_is_ptr) {
903         if (req->dir == IOREQ_READ) {
904             for (i = 0; i < req->count; i++) {
905                 read_phys_req_item(req->addr, req, i, &req->data);
906             }
907         } else if (req->dir == IOREQ_WRITE) {
908             for (i = 0; i < req->count; i++) {
909                 write_phys_req_item(req->addr, req, i, &req->data);
910             }
911         }
912     } else {
913         uint64_t tmp;
914 
915         if (req->dir == IOREQ_READ) {
916             for (i = 0; i < req->count; i++) {
917                 read_phys_req_item(req->addr, req, i, &tmp);
918                 write_phys_req_item(req->data, req, i, &tmp);
919             }
920         } else if (req->dir == IOREQ_WRITE) {
921             for (i = 0; i < req->count; i++) {
922                 read_phys_req_item(req->data, req, i, &tmp);
923                 write_phys_req_item(req->addr, req, i, &tmp);
924             }
925         }
926     }
927 }
928 
929 static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
930 {
931     uint32_t sbdf = req->addr >> 32;
932     uint32_t reg = req->addr;
933     XenPciDevice *xendev;
934 
935     if (req->size != sizeof(uint8_t) && req->size != sizeof(uint16_t) &&
936         req->size != sizeof(uint32_t)) {
937         hw_error("PCI config access: bad size (%u)", req->size);
938     }
939 
940     if (req->count != 1) {
941         hw_error("PCI config access: bad count (%u)", req->count);
942     }
943 
944     QLIST_FOREACH(xendev, &state->dev_list, entry) {
945         if (xendev->sbdf != sbdf) {
946             continue;
947         }
948 
949         if (!req->data_is_ptr) {
950             if (req->dir == IOREQ_READ) {
951                 req->data = pci_host_config_read_common(
952                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
953                     req->size);
954                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
955                                             req->size, req->data);
956             } else if (req->dir == IOREQ_WRITE) {
957                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
958                                              req->size, req->data);
959                 pci_host_config_write_common(
960                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
961                     req->data, req->size);
962             }
963         } else {
964             uint32_t tmp;
965 
966             if (req->dir == IOREQ_READ) {
967                 tmp = pci_host_config_read_common(
968                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
969                     req->size);
970                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
971                                             req->size, tmp);
972                 write_phys_req_item(req->data, req, 0, &tmp);
973             } else if (req->dir == IOREQ_WRITE) {
974                 read_phys_req_item(req->data, req, 0, &tmp);
975                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
976                                              req->size, tmp);
977                 pci_host_config_write_common(
978                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
979                     tmp, req->size);
980             }
981         }
982     }
983 }
984 
985 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
986 {
987     X86CPU *cpu;
988     CPUX86State *env;
989 
990     cpu = X86_CPU(current_cpu);
991     env = &cpu->env;
992     env->regs[R_EAX] = req->data;
993     env->regs[R_EBX] = vmport_regs->ebx;
994     env->regs[R_ECX] = vmport_regs->ecx;
995     env->regs[R_EDX] = vmport_regs->edx;
996     env->regs[R_ESI] = vmport_regs->esi;
997     env->regs[R_EDI] = vmport_regs->edi;
998 }
999 
1000 static void regs_from_cpu(vmware_regs_t *vmport_regs)
1001 {
1002     X86CPU *cpu = X86_CPU(current_cpu);
1003     CPUX86State *env = &cpu->env;
1004 
1005     vmport_regs->ebx = env->regs[R_EBX];
1006     vmport_regs->ecx = env->regs[R_ECX];
1007     vmport_regs->edx = env->regs[R_EDX];
1008     vmport_regs->esi = env->regs[R_ESI];
1009     vmport_regs->edi = env->regs[R_EDI];
1010 }
1011 
1012 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
1013 {
1014     vmware_regs_t *vmport_regs;
1015 
1016     assert(state->shared_vmport_page);
1017     vmport_regs =
1018         &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
1019     QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
1020 
1021     current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
1022     regs_to_cpu(vmport_regs, req);
1023     cpu_ioreq_pio(req);
1024     regs_from_cpu(vmport_regs);
1025     current_cpu = NULL;
1026 }
1027 
1028 static void handle_ioreq(XenIOState *state, ioreq_t *req)
1029 {
1030     trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
1031                        req->addr, req->data, req->count, req->size);
1032 
1033     if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
1034             (req->size < sizeof (target_ulong))) {
1035         req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
1036     }
1037 
1038     if (req->dir == IOREQ_WRITE)
1039         trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
1040                                  req->addr, req->data, req->count, req->size);
1041 
1042     switch (req->type) {
1043         case IOREQ_TYPE_PIO:
1044             cpu_ioreq_pio(req);
1045             break;
1046         case IOREQ_TYPE_COPY:
1047             cpu_ioreq_move(req);
1048             break;
1049         case IOREQ_TYPE_VMWARE_PORT:
1050             handle_vmport_ioreq(state, req);
1051             break;
1052         case IOREQ_TYPE_TIMEOFFSET:
1053             break;
1054         case IOREQ_TYPE_INVALIDATE:
1055             xen_invalidate_map_cache();
1056             break;
1057         case IOREQ_TYPE_PCI_CONFIG:
1058             cpu_ioreq_config(state, req);
1059             break;
1060         default:
1061             hw_error("Invalid ioreq type 0x%x\n", req->type);
1062     }
1063     if (req->dir == IOREQ_READ) {
1064         trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
1065                                 req->addr, req->data, req->count, req->size);
1066     }
1067 }
1068 
1069 static int handle_buffered_iopage(XenIOState *state)
1070 {
1071     buffered_iopage_t *buf_page = state->buffered_io_page;
1072     buf_ioreq_t *buf_req = NULL;
1073     ioreq_t req;
1074     int qw;
1075 
1076     if (!buf_page) {
1077         return 0;
1078     }
1079 
1080     memset(&req, 0x00, sizeof(req));
1081     req.state = STATE_IOREQ_READY;
1082     req.count = 1;
1083     req.dir = IOREQ_WRITE;
1084 
1085     for (;;) {
1086         uint32_t rdptr = buf_page->read_pointer, wrptr;
1087 
1088         xen_rmb();
1089         wrptr = buf_page->write_pointer;
1090         xen_rmb();
1091         if (rdptr != buf_page->read_pointer) {
1092             continue;
1093         }
1094         if (rdptr == wrptr) {
1095             break;
1096         }
1097         buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1098         req.size = 1U << buf_req->size;
1099         req.addr = buf_req->addr;
1100         req.data = buf_req->data;
1101         req.type = buf_req->type;
1102         xen_rmb();
1103         qw = (req.size == 8);
1104         if (qw) {
1105             if (rdptr + 1 == wrptr) {
1106                 hw_error("Incomplete quad word buffered ioreq");
1107             }
1108             buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1109                                            IOREQ_BUFFER_SLOT_NUM];
1110             req.data |= ((uint64_t)buf_req->data) << 32;
1111             xen_rmb();
1112         }
1113 
1114         handle_ioreq(state, &req);
1115 
1116         /* Only req.data may get updated by handle_ioreq(), albeit even that
1117          * should not happen as such data would never make it to the guest (we
1118          * can only usefully see writes here after all).
1119          */
1120         assert(req.state == STATE_IOREQ_READY);
1121         assert(req.count == 1);
1122         assert(req.dir == IOREQ_WRITE);
1123         assert(!req.data_is_ptr);
1124 
1125         atomic_add(&buf_page->read_pointer, qw + 1);
1126     }
1127 
1128     return req.count;
1129 }
1130 
1131 static void handle_buffered_io(void *opaque)
1132 {
1133     XenIOState *state = opaque;
1134 
1135     if (handle_buffered_iopage(state)) {
1136         timer_mod(state->buffered_io_timer,
1137                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1138     } else {
1139         timer_del(state->buffered_io_timer);
1140         xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1141     }
1142 }
1143 
1144 static void cpu_handle_ioreq(void *opaque)
1145 {
1146     XenIOState *state = opaque;
1147     ioreq_t *req = cpu_get_ioreq(state);
1148 
1149     handle_buffered_iopage(state);
1150     if (req) {
1151         ioreq_t copy = *req;
1152 
1153         xen_rmb();
1154         handle_ioreq(state, &copy);
1155         req->data = copy.data;
1156 
1157         if (req->state != STATE_IOREQ_INPROCESS) {
1158             fprintf(stderr, "Badness in I/O request ... not in service?!: "
1159                     "%x, ptr: %x, port: %"PRIx64", "
1160                     "data: %"PRIx64", count: %u, size: %u, type: %u\n",
1161                     req->state, req->data_is_ptr, req->addr,
1162                     req->data, req->count, req->size, req->type);
1163             destroy_hvm_domain(false);
1164             return;
1165         }
1166 
1167         xen_wmb(); /* Update ioreq contents /then/ update state. */
1168 
1169         /*
1170          * We do this before we send the response so that the tools
1171          * have the opportunity to pick up on the reset before the
1172          * guest resumes and does a hlt with interrupts disabled which
1173          * causes Xen to powerdown the domain.
1174          */
1175         if (runstate_is_running()) {
1176             ShutdownCause request;
1177 
1178             if (qemu_shutdown_requested_get()) {
1179                 destroy_hvm_domain(false);
1180             }
1181             request = qemu_reset_requested_get();
1182             if (request) {
1183                 qemu_system_reset(request);
1184                 destroy_hvm_domain(true);
1185             }
1186         }
1187 
1188         req->state = STATE_IORESP_READY;
1189         xenevtchn_notify(state->xce_handle,
1190                          state->ioreq_local_port[state->send_vcpu]);
1191     }
1192 }
1193 
1194 static void xen_main_loop_prepare(XenIOState *state)
1195 {
1196     int evtchn_fd = -1;
1197 
1198     if (state->xce_handle != NULL) {
1199         evtchn_fd = xenevtchn_fd(state->xce_handle);
1200     }
1201 
1202     state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1203                                                  state);
1204 
1205     if (evtchn_fd != -1) {
1206         CPUState *cpu_state;
1207 
1208         DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1209         CPU_FOREACH(cpu_state) {
1210             DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1211                     __func__, cpu_state->cpu_index, cpu_state);
1212             state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1213         }
1214         qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1215     }
1216 }
1217 
1218 
1219 static void xen_hvm_change_state_handler(void *opaque, int running,
1220                                          RunState rstate)
1221 {
1222     XenIOState *state = opaque;
1223 
1224     if (running) {
1225         xen_main_loop_prepare(state);
1226     }
1227 
1228     xen_set_ioreq_server_state(xen_domid,
1229                                state->ioservid,
1230                                (rstate == RUN_STATE_RUNNING));
1231 }
1232 
1233 static void xen_exit_notifier(Notifier *n, void *data)
1234 {
1235     XenIOState *state = container_of(n, XenIOState, exit);
1236 
1237     xenevtchn_close(state->xce_handle);
1238     xs_daemon_close(state->xenstore);
1239 }
1240 
1241 #ifdef XEN_COMPAT_PHYSMAP
1242 static void xen_read_physmap(XenIOState *state)
1243 {
1244     XenPhysmap *physmap = NULL;
1245     unsigned int len, num, i;
1246     char path[80], *value = NULL;
1247     char **entries = NULL;
1248 
1249     snprintf(path, sizeof(path),
1250             "/local/domain/0/device-model/%d/physmap", xen_domid);
1251     entries = xs_directory(state->xenstore, 0, path, &num);
1252     if (entries == NULL)
1253         return;
1254 
1255     for (i = 0; i < num; i++) {
1256         physmap = g_malloc(sizeof (XenPhysmap));
1257         physmap->phys_offset = strtoull(entries[i], NULL, 16);
1258         snprintf(path, sizeof(path),
1259                 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1260                 xen_domid, entries[i]);
1261         value = xs_read(state->xenstore, 0, path, &len);
1262         if (value == NULL) {
1263             g_free(physmap);
1264             continue;
1265         }
1266         physmap->start_addr = strtoull(value, NULL, 16);
1267         free(value);
1268 
1269         snprintf(path, sizeof(path),
1270                 "/local/domain/0/device-model/%d/physmap/%s/size",
1271                 xen_domid, entries[i]);
1272         value = xs_read(state->xenstore, 0, path, &len);
1273         if (value == NULL) {
1274             g_free(physmap);
1275             continue;
1276         }
1277         physmap->size = strtoull(value, NULL, 16);
1278         free(value);
1279 
1280         snprintf(path, sizeof(path),
1281                 "/local/domain/0/device-model/%d/physmap/%s/name",
1282                 xen_domid, entries[i]);
1283         physmap->name = xs_read(state->xenstore, 0, path, &len);
1284 
1285         QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
1286     }
1287     free(entries);
1288 }
1289 #else
1290 static void xen_read_physmap(XenIOState *state)
1291 {
1292 }
1293 #endif
1294 
1295 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1296 {
1297     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1298 }
1299 
1300 static int xen_map_ioreq_server(XenIOState *state)
1301 {
1302     void *addr = NULL;
1303     xenforeignmemory_resource_handle *fres;
1304     xen_pfn_t ioreq_pfn;
1305     xen_pfn_t bufioreq_pfn;
1306     evtchn_port_t bufioreq_evtchn;
1307     int rc;
1308 
1309     /*
1310      * Attempt to map using the resource API and fall back to normal
1311      * foreign mapping if this is not supported.
1312      */
1313     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq != 0);
1314     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1315     fres = xenforeignmemory_map_resource(xen_fmem, xen_domid,
1316                                          XENMEM_resource_ioreq_server,
1317                                          state->ioservid, 0, 2,
1318                                          &addr,
1319                                          PROT_READ | PROT_WRITE, 0);
1320     if (fres != NULL) {
1321         trace_xen_map_resource_ioreq(state->ioservid, addr);
1322         state->buffered_io_page = addr;
1323         state->shared_page = addr + TARGET_PAGE_SIZE;
1324     } else if (errno != EOPNOTSUPP) {
1325         error_report("failed to map ioreq server resources: error %d handle=%p",
1326                      errno, xen_xc);
1327         return -1;
1328     }
1329 
1330     rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1331                                    (state->shared_page == NULL) ?
1332                                    &ioreq_pfn : NULL,
1333                                    (state->buffered_io_page == NULL) ?
1334                                    &bufioreq_pfn : NULL,
1335                                    &bufioreq_evtchn);
1336     if (rc < 0) {
1337         error_report("failed to get ioreq server info: error %d handle=%p",
1338                      errno, xen_xc);
1339         return rc;
1340     }
1341 
1342     if (state->shared_page == NULL) {
1343         DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1344 
1345         state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1346                                                   PROT_READ | PROT_WRITE,
1347                                                   1, &ioreq_pfn, NULL);
1348         if (state->shared_page == NULL) {
1349             error_report("map shared IO page returned error %d handle=%p",
1350                          errno, xen_xc);
1351         }
1352     }
1353 
1354     if (state->buffered_io_page == NULL) {
1355         DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1356 
1357         state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1358                                                        PROT_READ | PROT_WRITE,
1359                                                        1, &bufioreq_pfn,
1360                                                        NULL);
1361         if (state->buffered_io_page == NULL) {
1362             error_report("map buffered IO page returned error %d", errno);
1363             return -1;
1364         }
1365     }
1366 
1367     if (state->shared_page == NULL || state->buffered_io_page == NULL) {
1368         return -1;
1369     }
1370 
1371     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1372 
1373     state->bufioreq_remote_port = bufioreq_evtchn;
1374 
1375     return 0;
1376 }
1377 
1378 void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory)
1379 {
1380     int i, rc;
1381     xen_pfn_t ioreq_pfn;
1382     XenIOState *state;
1383 
1384     state = g_malloc0(sizeof (XenIOState));
1385 
1386     state->xce_handle = xenevtchn_open(NULL, 0);
1387     if (state->xce_handle == NULL) {
1388         perror("xen: event channel open");
1389         goto err;
1390     }
1391 
1392     state->xenstore = xs_daemon_open();
1393     if (state->xenstore == NULL) {
1394         perror("xen: xenstore open");
1395         goto err;
1396     }
1397 
1398     xen_create_ioreq_server(xen_domid, &state->ioservid);
1399 
1400     state->exit.notify = xen_exit_notifier;
1401     qemu_add_exit_notifier(&state->exit);
1402 
1403     state->suspend.notify = xen_suspend_notifier;
1404     qemu_register_suspend_notifier(&state->suspend);
1405 
1406     state->wakeup.notify = xen_wakeup_notifier;
1407     qemu_register_wakeup_notifier(&state->wakeup);
1408 
1409     /*
1410      * Register wake-up support in QMP query-current-machine API
1411      */
1412     qemu_register_wakeup_support();
1413 
1414     rc = xen_map_ioreq_server(state);
1415     if (rc < 0) {
1416         goto err;
1417     }
1418 
1419     rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1420     if (!rc) {
1421         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1422         state->shared_vmport_page =
1423             xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1424                                  1, &ioreq_pfn, NULL);
1425         if (state->shared_vmport_page == NULL) {
1426             error_report("map shared vmport IO page returned error %d handle=%p",
1427                          errno, xen_xc);
1428             goto err;
1429         }
1430     } else if (rc != -ENOSYS) {
1431         error_report("get vmport regs pfn returned error %d, rc=%d",
1432                      errno, rc);
1433         goto err;
1434     }
1435 
1436     /* Note: cpus is empty at this point in init */
1437     state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1438 
1439     rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
1440     if (rc < 0) {
1441         error_report("failed to enable ioreq server info: error %d handle=%p",
1442                      errno, xen_xc);
1443         goto err;
1444     }
1445 
1446     state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1447 
1448     /* FIXME: how about if we overflow the page here? */
1449     for (i = 0; i < max_cpus; i++) {
1450         rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1451                                         xen_vcpu_eport(state->shared_page, i));
1452         if (rc == -1) {
1453             error_report("shared evtchn %d bind error %d", i, errno);
1454             goto err;
1455         }
1456         state->ioreq_local_port[i] = rc;
1457     }
1458 
1459     rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1460                                     state->bufioreq_remote_port);
1461     if (rc == -1) {
1462         error_report("buffered evtchn bind error %d", errno);
1463         goto err;
1464     }
1465     state->bufioreq_local_port = rc;
1466 
1467     /* Init RAM management */
1468 #ifdef XEN_COMPAT_PHYSMAP
1469     xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1470 #else
1471     xen_map_cache_init(NULL, state);
1472 #endif
1473     xen_ram_init(pcms, ram_size, ram_memory);
1474 
1475     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1476 
1477     state->memory_listener = xen_memory_listener;
1478     memory_listener_register(&state->memory_listener, &address_space_memory);
1479     state->log_for_dirtybit = NULL;
1480 
1481     state->io_listener = xen_io_listener;
1482     memory_listener_register(&state->io_listener, &address_space_io);
1483 
1484     state->device_listener = xen_device_listener;
1485     QLIST_INIT(&state->dev_list);
1486     device_listener_register(&state->device_listener);
1487 
1488     xen_bus_init();
1489 
1490     /* Initialize backend core & drivers */
1491     if (xen_be_init() != 0) {
1492         error_report("xen backend core setup failed");
1493         goto err;
1494     }
1495     xen_be_register_common();
1496 
1497     QLIST_INIT(&xen_physmap);
1498     xen_read_physmap(state);
1499 
1500     /* Disable ACPI build because Xen handles it */
1501     pcms->acpi_build_enabled = false;
1502 
1503     return;
1504 
1505 err:
1506     error_report("xen hardware virtual machine initialisation failed");
1507     exit(1);
1508 }
1509 
1510 void destroy_hvm_domain(bool reboot)
1511 {
1512     xc_interface *xc_handle;
1513     int sts;
1514     int rc;
1515 
1516     unsigned int reason = reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff;
1517 
1518     if (xen_dmod) {
1519         rc = xendevicemodel_shutdown(xen_dmod, xen_domid, reason);
1520         if (!rc) {
1521             return;
1522         }
1523         if (errno != ENOTTY /* old Xen */) {
1524             perror("xendevicemodel_shutdown failed");
1525         }
1526         /* well, try the old thing then */
1527     }
1528 
1529     xc_handle = xc_interface_open(0, 0, 0);
1530     if (xc_handle == NULL) {
1531         fprintf(stderr, "Cannot acquire xenctrl handle\n");
1532     } else {
1533         sts = xc_domain_shutdown(xc_handle, xen_domid, reason);
1534         if (sts != 0) {
1535             fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1536                     "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1537                     sts, strerror(errno));
1538         } else {
1539             fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1540                     reboot ? "reboot" : "poweroff");
1541         }
1542         xc_interface_close(xc_handle);
1543     }
1544 }
1545 
1546 void xen_register_framebuffer(MemoryRegion *mr)
1547 {
1548     framebuffer = mr;
1549 }
1550 
1551 void xen_shutdown_fatal_error(const char *fmt, ...)
1552 {
1553     va_list ap;
1554 
1555     va_start(ap, fmt);
1556     vfprintf(stderr, fmt, ap);
1557     va_end(ap);
1558     fprintf(stderr, "Will destroy the domain.\n");
1559     /* destroy the domain */
1560     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
1561 }
1562 
1563 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
1564 {
1565     if (unlikely(xen_in_migration)) {
1566         int rc;
1567         ram_addr_t start_pfn, nb_pages;
1568 
1569         start = xen_phys_offset_to_gaddr(start, length);
1570 
1571         if (length == 0) {
1572             length = TARGET_PAGE_SIZE;
1573         }
1574         start_pfn = start >> TARGET_PAGE_BITS;
1575         nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1576             - start_pfn;
1577         rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
1578         if (rc) {
1579             fprintf(stderr,
1580                     "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1581                     __func__, start, nb_pages, errno, strerror(errno));
1582         }
1583     }
1584 }
1585 
1586 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1587 {
1588     if (enable) {
1589         memory_global_dirty_log_start();
1590     } else {
1591         memory_global_dirty_log_stop();
1592     }
1593 }
1594