xref: /openbmc/qemu/hw/i386/xen/xen-hvm.c (revision 9269b9d1888c2b17d2e9948abf9f6310b3afbac3)
193d43e7eSAnthony Xu /*
293d43e7eSAnthony Xu  * Copyright (C) 2010       Citrix Ltd.
393d43e7eSAnthony Xu  *
493d43e7eSAnthony Xu  * This work is licensed under the terms of the GNU GPL, version 2.  See
593d43e7eSAnthony Xu  * the COPYING file in the top-level directory.
693d43e7eSAnthony Xu  *
793d43e7eSAnthony Xu  * Contributions after 2012-01-13 are licensed under the terms of the
893d43e7eSAnthony Xu  * GNU GPL, version 2 or (at your option) any later version.
993d43e7eSAnthony Xu  */
1093d43e7eSAnthony Xu 
1193d43e7eSAnthony Xu #include "qemu/osdep.h"
12039a93b0SPhilippe Mathieu-Daudé #include "qemu/units.h"
1393d43e7eSAnthony Xu 
1493d43e7eSAnthony Xu #include "cpu.h"
1593d43e7eSAnthony Xu #include "hw/pci/pci.h"
16dfb6578dSPaul Durrant #include "hw/pci/pci_host.h"
1793d43e7eSAnthony Xu #include "hw/i386/pc.h"
1864552b6bSMarkus Armbruster #include "hw/irq.h"
19650d103dSMarkus Armbruster #include "hw/hw.h"
2093d43e7eSAnthony Xu #include "hw/i386/apic-msidef.h"
21e2abfe5eSDavid Woodhouse #include "hw/xen/xen_native.h"
222d0ed5e6SPaul Durrant #include "hw/xen/xen-legacy-backend.h"
23108f7bbaSPaul Durrant #include "hw/xen/xen-bus.h"
24c834596fSPhilippe Mathieu-Daudé #include "hw/xen/xen-x86.h"
25e688df6bSMarkus Armbruster #include "qapi/error.h"
2628af9ba2SPhilippe Mathieu-Daudé #include "qapi/qapi-commands-migration.h"
2793d43e7eSAnthony Xu #include "qemu/error-report.h"
28db725815SMarkus Armbruster #include "qemu/main-loop.h"
2993d43e7eSAnthony Xu #include "qemu/range.h"
3054d31236SMarkus Armbruster #include "sysemu/runstate.h"
3146517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
32da278d58SPhilippe Mathieu-Daudé #include "sysemu/xen.h"
3393d43e7eSAnthony Xu #include "sysemu/xen-mapcache.h"
3493d43e7eSAnthony Xu #include "trace.h"
3593d43e7eSAnthony Xu 
3693d43e7eSAnthony Xu #include <xen/hvm/ioreq.h>
3793d43e7eSAnthony Xu #include <xen/hvm/e820.h>
3893d43e7eSAnthony Xu 
3993d43e7eSAnthony Xu //#define DEBUG_XEN_HVM
4093d43e7eSAnthony Xu 
4193d43e7eSAnthony Xu #ifdef DEBUG_XEN_HVM
4293d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \
4393d43e7eSAnthony Xu     do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
4493d43e7eSAnthony Xu #else
4593d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \
4693d43e7eSAnthony Xu     do { } while (0)
4793d43e7eSAnthony Xu #endif
4893d43e7eSAnthony Xu 
4993d43e7eSAnthony Xu static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
5093d43e7eSAnthony Xu static MemoryRegion *framebuffer;
5193d43e7eSAnthony Xu static bool xen_in_migration;
5293d43e7eSAnthony Xu 
5393d43e7eSAnthony Xu /* Compatibility with older version */
5493d43e7eSAnthony Xu 
55e2abfe5eSDavid Woodhouse /*
56e2abfe5eSDavid Woodhouse  * This allows QEMU to build on a system that has Xen 4.5 or earlier installed.
57e2abfe5eSDavid Woodhouse  * This is here (not in hw/xen/xen_native.h) because xen/hvm/ioreq.h needs to
58e2abfe5eSDavid Woodhouse  * be included before this block and hw/xen/xen_native.h needs to be included
59e2abfe5eSDavid Woodhouse  * before xen/hvm/ioreq.h
6093d43e7eSAnthony Xu  */
6193d43e7eSAnthony Xu #ifndef IOREQ_TYPE_VMWARE_PORT
6293d43e7eSAnthony Xu #define IOREQ_TYPE_VMWARE_PORT  3
6393d43e7eSAnthony Xu struct vmware_regs {
6493d43e7eSAnthony Xu     uint32_t esi;
6593d43e7eSAnthony Xu     uint32_t edi;
6693d43e7eSAnthony Xu     uint32_t ebx;
6793d43e7eSAnthony Xu     uint32_t ecx;
6893d43e7eSAnthony Xu     uint32_t edx;
6993d43e7eSAnthony Xu };
7093d43e7eSAnthony Xu typedef struct vmware_regs vmware_regs_t;
7193d43e7eSAnthony Xu 
7293d43e7eSAnthony Xu struct shared_vmport_iopage {
7393d43e7eSAnthony Xu     struct vmware_regs vcpu_vmport_regs[1];
7493d43e7eSAnthony Xu };
7593d43e7eSAnthony Xu typedef struct shared_vmport_iopage shared_vmport_iopage_t;
7693d43e7eSAnthony Xu #endif
77*9269b9d1SStefano Stabellini static shared_vmport_iopage_t *shared_vmport_page;
7893d43e7eSAnthony Xu 
7993d43e7eSAnthony Xu static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
8093d43e7eSAnthony Xu {
8193d43e7eSAnthony Xu     return shared_page->vcpu_ioreq[i].vp_eport;
8293d43e7eSAnthony Xu }
8393d43e7eSAnthony Xu static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
8493d43e7eSAnthony Xu {
8593d43e7eSAnthony Xu     return &shared_page->vcpu_ioreq[vcpu];
8693d43e7eSAnthony Xu }
8793d43e7eSAnthony Xu 
8893d43e7eSAnthony Xu #define BUFFER_IO_MAX_DELAY  100
8993d43e7eSAnthony Xu 
9093d43e7eSAnthony Xu typedef struct XenPhysmap {
9193d43e7eSAnthony Xu     hwaddr start_addr;
9293d43e7eSAnthony Xu     ram_addr_t size;
9393d43e7eSAnthony Xu     const char *name;
9493d43e7eSAnthony Xu     hwaddr phys_offset;
9593d43e7eSAnthony Xu 
9693d43e7eSAnthony Xu     QLIST_ENTRY(XenPhysmap) list;
9793d43e7eSAnthony Xu } XenPhysmap;
9893d43e7eSAnthony Xu 
9904a8f72eSIgor Druzhinin static QLIST_HEAD(, XenPhysmap) xen_physmap;
100*9269b9d1SStefano Stabellini static const XenPhysmap *log_for_dirtybit;
101*9269b9d1SStefano Stabellini /* Buffer used by xen_sync_dirty_bitmap */
102*9269b9d1SStefano Stabellini static unsigned long *dirty_bitmap;
103*9269b9d1SStefano Stabellini static Notifier suspend;
104*9269b9d1SStefano Stabellini static Notifier wakeup;
10504a8f72eSIgor Druzhinin 
106dfb6578dSPaul Durrant typedef struct XenPciDevice {
107dfb6578dSPaul Durrant     PCIDevice *pci_dev;
108dfb6578dSPaul Durrant     uint32_t sbdf;
109dfb6578dSPaul Durrant     QLIST_ENTRY(XenPciDevice) entry;
110dfb6578dSPaul Durrant } XenPciDevice;
111dfb6578dSPaul Durrant 
11293d43e7eSAnthony Xu typedef struct XenIOState {
11393d43e7eSAnthony Xu     ioservid_t ioservid;
11493d43e7eSAnthony Xu     shared_iopage_t *shared_page;
11593d43e7eSAnthony Xu     buffered_iopage_t *buffered_io_page;
116f1e43b60SAnthony PERARD     xenforeignmemory_resource_handle *fres;
11793d43e7eSAnthony Xu     QEMUTimer *buffered_io_timer;
11893d43e7eSAnthony Xu     CPUState **cpu_by_vcpu_id;
11993d43e7eSAnthony Xu     /* the evtchn port for polling the notification, */
12093d43e7eSAnthony Xu     evtchn_port_t *ioreq_local_port;
12171cec1edSPaul Durrant     /* evtchn remote and local ports for buffered io */
12271cec1edSPaul Durrant     evtchn_port_t bufioreq_remote_port;
12393d43e7eSAnthony Xu     evtchn_port_t bufioreq_local_port;
12493d43e7eSAnthony Xu     /* the evtchn fd for polling */
12593d43e7eSAnthony Xu     xenevtchn_handle *xce_handle;
12693d43e7eSAnthony Xu     /* which vcpu we are serving */
12793d43e7eSAnthony Xu     int send_vcpu;
12893d43e7eSAnthony Xu 
12993d43e7eSAnthony Xu     struct xs_handle *xenstore;
13093d43e7eSAnthony Xu     MemoryListener memory_listener;
13193d43e7eSAnthony Xu     MemoryListener io_listener;
132dfb6578dSPaul Durrant     QLIST_HEAD(, XenPciDevice) dev_list;
13393d43e7eSAnthony Xu     DeviceListener device_listener;
13493d43e7eSAnthony Xu 
13593d43e7eSAnthony Xu     Notifier exit;
13693d43e7eSAnthony Xu } XenIOState;
13793d43e7eSAnthony Xu 
13893d43e7eSAnthony Xu /* Xen specific function for piix pci */
13993d43e7eSAnthony Xu 
14093d43e7eSAnthony Xu int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
14193d43e7eSAnthony Xu {
1428d40def6SPhilippe Mathieu-Daudé     return irq_num + (PCI_SLOT(pci_dev->devfn) << 2);
14393d43e7eSAnthony Xu }
14493d43e7eSAnthony Xu 
14527047bd2SBernhard Beschow void xen_intx_set_irq(void *opaque, int irq_num, int level)
14693d43e7eSAnthony Xu {
14793d43e7eSAnthony Xu     xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
14893d43e7eSAnthony Xu                            irq_num & 3, level);
14993d43e7eSAnthony Xu }
15093d43e7eSAnthony Xu 
15121d87050SBernhard Beschow int xen_set_pci_link_route(uint8_t link, uint8_t irq)
15221d87050SBernhard Beschow {
15321d87050SBernhard Beschow     return xendevicemodel_set_pci_link_route(xen_dmod, xen_domid, link, irq);
15421d87050SBernhard Beschow }
15521d87050SBernhard Beschow 
15693d43e7eSAnthony Xu int xen_is_pirq_msi(uint32_t msi_data)
15793d43e7eSAnthony Xu {
15893d43e7eSAnthony Xu     /* If vector is 0, the msi is remapped into a pirq, passed as
15993d43e7eSAnthony Xu      * dest_id.
16093d43e7eSAnthony Xu      */
16193d43e7eSAnthony Xu     return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
16293d43e7eSAnthony Xu }
16393d43e7eSAnthony Xu 
16493d43e7eSAnthony Xu void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
16593d43e7eSAnthony Xu {
16693d43e7eSAnthony Xu     xen_inject_msi(xen_domid, addr, data);
16793d43e7eSAnthony Xu }
16893d43e7eSAnthony Xu 
16993d43e7eSAnthony Xu static void xen_suspend_notifier(Notifier *notifier, void *data)
17093d43e7eSAnthony Xu {
17193d43e7eSAnthony Xu     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
17293d43e7eSAnthony Xu }
17393d43e7eSAnthony Xu 
17493d43e7eSAnthony Xu /* Xen Interrupt Controller */
17593d43e7eSAnthony Xu 
17693d43e7eSAnthony Xu static void xen_set_irq(void *opaque, int irq, int level)
17793d43e7eSAnthony Xu {
17893d43e7eSAnthony Xu     xen_set_isa_irq_level(xen_domid, irq, level);
17993d43e7eSAnthony Xu }
18093d43e7eSAnthony Xu 
18193d43e7eSAnthony Xu qemu_irq *xen_interrupt_controller_init(void)
18293d43e7eSAnthony Xu {
18393d43e7eSAnthony Xu     return qemu_allocate_irqs(xen_set_irq, NULL, 16);
18493d43e7eSAnthony Xu }
18593d43e7eSAnthony Xu 
18693d43e7eSAnthony Xu /* Memory Ops */
18793d43e7eSAnthony Xu 
18893d43e7eSAnthony Xu static void xen_ram_init(PCMachineState *pcms,
18993d43e7eSAnthony Xu                          ram_addr_t ram_size, MemoryRegion **ram_memory_p)
19093d43e7eSAnthony Xu {
191f0bb276bSPaolo Bonzini     X86MachineState *x86ms = X86_MACHINE(pcms);
19293d43e7eSAnthony Xu     MemoryRegion *sysmem = get_system_memory();
19393d43e7eSAnthony Xu     ram_addr_t block_len;
194f0bb276bSPaolo Bonzini     uint64_t user_lowmem =
195f0bb276bSPaolo Bonzini         object_property_get_uint(qdev_get_machine(),
1969a45729dSGerd Hoffmann                                  PC_MACHINE_MAX_RAM_BELOW_4G,
19793d43e7eSAnthony Xu                                  &error_abort);
19893d43e7eSAnthony Xu 
19993d43e7eSAnthony Xu     /* Handle the machine opt max-ram-below-4g.  It is basically doing
20093d43e7eSAnthony Xu      * min(xen limit, user limit).
20193d43e7eSAnthony Xu      */
20293d43e7eSAnthony Xu     if (!user_lowmem) {
20393d43e7eSAnthony Xu         user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
20493d43e7eSAnthony Xu     }
20593d43e7eSAnthony Xu     if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
20693d43e7eSAnthony Xu         user_lowmem = HVM_BELOW_4G_RAM_END;
20793d43e7eSAnthony Xu     }
20893d43e7eSAnthony Xu 
20993d43e7eSAnthony Xu     if (ram_size >= user_lowmem) {
210f0bb276bSPaolo Bonzini         x86ms->above_4g_mem_size = ram_size - user_lowmem;
211f0bb276bSPaolo Bonzini         x86ms->below_4g_mem_size = user_lowmem;
21293d43e7eSAnthony Xu     } else {
213f0bb276bSPaolo Bonzini         x86ms->above_4g_mem_size = 0;
214f0bb276bSPaolo Bonzini         x86ms->below_4g_mem_size = ram_size;
21593d43e7eSAnthony Xu     }
216f0bb276bSPaolo Bonzini     if (!x86ms->above_4g_mem_size) {
21793d43e7eSAnthony Xu         block_len = ram_size;
21893d43e7eSAnthony Xu     } else {
21993d43e7eSAnthony Xu         /*
22093d43e7eSAnthony Xu          * Xen does not allocate the memory continuously, it keeps a
22193d43e7eSAnthony Xu          * hole of the size computed above or passed in.
22293d43e7eSAnthony Xu          */
223039a93b0SPhilippe Mathieu-Daudé         block_len = (4 * GiB) + x86ms->above_4g_mem_size;
22493d43e7eSAnthony Xu     }
22598a99ce0SPeter Maydell     memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
22693d43e7eSAnthony Xu                            &error_fatal);
22793d43e7eSAnthony Xu     *ram_memory_p = &ram_memory;
22893d43e7eSAnthony Xu 
22993d43e7eSAnthony Xu     memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
23093d43e7eSAnthony Xu                              &ram_memory, 0, 0xa0000);
23193d43e7eSAnthony Xu     memory_region_add_subregion(sysmem, 0, &ram_640k);
23293d43e7eSAnthony Xu     /* Skip of the VGA IO memory space, it will be registered later by the VGA
23393d43e7eSAnthony Xu      * emulated device.
23493d43e7eSAnthony Xu      *
23593d43e7eSAnthony Xu      * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
23693d43e7eSAnthony Xu      * the Options ROM, so it is registered here as RAM.
23793d43e7eSAnthony Xu      */
23893d43e7eSAnthony Xu     memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
23993d43e7eSAnthony Xu                              &ram_memory, 0xc0000,
240f0bb276bSPaolo Bonzini                              x86ms->below_4g_mem_size - 0xc0000);
24193d43e7eSAnthony Xu     memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
242f0bb276bSPaolo Bonzini     if (x86ms->above_4g_mem_size > 0) {
24393d43e7eSAnthony Xu         memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
24493d43e7eSAnthony Xu                                  &ram_memory, 0x100000000ULL,
245f0bb276bSPaolo Bonzini                                  x86ms->above_4g_mem_size);
24693d43e7eSAnthony Xu         memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
24793d43e7eSAnthony Xu     }
24893d43e7eSAnthony Xu }
24993d43e7eSAnthony Xu 
25093d43e7eSAnthony Xu void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
25193d43e7eSAnthony Xu                    Error **errp)
25293d43e7eSAnthony Xu {
25393d43e7eSAnthony Xu     unsigned long nr_pfn;
25493d43e7eSAnthony Xu     xen_pfn_t *pfn_list;
25593d43e7eSAnthony Xu     int i;
25693d43e7eSAnthony Xu 
25793d43e7eSAnthony Xu     if (runstate_check(RUN_STATE_INMIGRATE)) {
25893d43e7eSAnthony Xu         /* RAM already populated in Xen */
25993d43e7eSAnthony Xu         fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
26093d43e7eSAnthony Xu                 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
26193d43e7eSAnthony Xu                 __func__, size, ram_addr);
26293d43e7eSAnthony Xu         return;
26393d43e7eSAnthony Xu     }
26493d43e7eSAnthony Xu 
26593d43e7eSAnthony Xu     if (mr == &ram_memory) {
26693d43e7eSAnthony Xu         return;
26793d43e7eSAnthony Xu     }
26893d43e7eSAnthony Xu 
26993d43e7eSAnthony Xu     trace_xen_ram_alloc(ram_addr, size);
27093d43e7eSAnthony Xu 
27193d43e7eSAnthony Xu     nr_pfn = size >> TARGET_PAGE_BITS;
27293d43e7eSAnthony Xu     pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
27393d43e7eSAnthony Xu 
27493d43e7eSAnthony Xu     for (i = 0; i < nr_pfn; i++) {
27593d43e7eSAnthony Xu         pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
27693d43e7eSAnthony Xu     }
27793d43e7eSAnthony Xu 
27893d43e7eSAnthony Xu     if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
27993d43e7eSAnthony Xu         error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
28093d43e7eSAnthony Xu                    ram_addr);
28193d43e7eSAnthony Xu     }
28293d43e7eSAnthony Xu 
28393d43e7eSAnthony Xu     g_free(pfn_list);
28493d43e7eSAnthony Xu }
28593d43e7eSAnthony Xu 
28604a8f72eSIgor Druzhinin static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size)
28793d43e7eSAnthony Xu {
28893d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
28993d43e7eSAnthony Xu 
29093d43e7eSAnthony Xu     start_addr &= TARGET_PAGE_MASK;
29193d43e7eSAnthony Xu 
29204a8f72eSIgor Druzhinin     QLIST_FOREACH(physmap, &xen_physmap, list) {
29393d43e7eSAnthony Xu         if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
29493d43e7eSAnthony Xu             return physmap;
29593d43e7eSAnthony Xu         }
29693d43e7eSAnthony Xu     }
29793d43e7eSAnthony Xu     return NULL;
29893d43e7eSAnthony Xu }
29993d43e7eSAnthony Xu 
30004a8f72eSIgor Druzhinin static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size)
30193d43e7eSAnthony Xu {
30204a8f72eSIgor Druzhinin     hwaddr addr = phys_offset & TARGET_PAGE_MASK;
30393d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
30493d43e7eSAnthony Xu 
30504a8f72eSIgor Druzhinin     QLIST_FOREACH(physmap, &xen_physmap, list) {
30693d43e7eSAnthony Xu         if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
30704a8f72eSIgor Druzhinin             return physmap->start_addr + (phys_offset - physmap->phys_offset);
30893d43e7eSAnthony Xu         }
30993d43e7eSAnthony Xu     }
31093d43e7eSAnthony Xu 
31104a8f72eSIgor Druzhinin     return phys_offset;
31293d43e7eSAnthony Xu }
31393d43e7eSAnthony Xu 
31404a8f72eSIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
315697b66d0SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
316697b66d0SIgor Druzhinin {
317697b66d0SIgor Druzhinin     char path[80], value[17];
318697b66d0SIgor Druzhinin 
319697b66d0SIgor Druzhinin     snprintf(path, sizeof(path),
320697b66d0SIgor Druzhinin             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
321697b66d0SIgor Druzhinin             xen_domid, (uint64_t)physmap->phys_offset);
322697b66d0SIgor Druzhinin     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
323697b66d0SIgor Druzhinin     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
324697b66d0SIgor Druzhinin         return -1;
325697b66d0SIgor Druzhinin     }
326697b66d0SIgor Druzhinin     snprintf(path, sizeof(path),
327697b66d0SIgor Druzhinin             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
328697b66d0SIgor Druzhinin             xen_domid, (uint64_t)physmap->phys_offset);
329697b66d0SIgor Druzhinin     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
330697b66d0SIgor Druzhinin     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
331697b66d0SIgor Druzhinin         return -1;
332697b66d0SIgor Druzhinin     }
333697b66d0SIgor Druzhinin     if (physmap->name) {
334697b66d0SIgor Druzhinin         snprintf(path, sizeof(path),
335697b66d0SIgor Druzhinin                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
336697b66d0SIgor Druzhinin                 xen_domid, (uint64_t)physmap->phys_offset);
337697b66d0SIgor Druzhinin         if (!xs_write(state->xenstore, 0, path,
338697b66d0SIgor Druzhinin                       physmap->name, strlen(physmap->name))) {
339697b66d0SIgor Druzhinin             return -1;
340697b66d0SIgor Druzhinin         }
341697b66d0SIgor Druzhinin     }
342697b66d0SIgor Druzhinin     return 0;
343697b66d0SIgor Druzhinin }
344331b5189SIgor Druzhinin #else
345331b5189SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
346331b5189SIgor Druzhinin {
347331b5189SIgor Druzhinin     return 0;
348331b5189SIgor Druzhinin }
349331b5189SIgor Druzhinin #endif
350697b66d0SIgor Druzhinin 
35193d43e7eSAnthony Xu static int xen_add_to_physmap(XenIOState *state,
35293d43e7eSAnthony Xu                               hwaddr start_addr,
35393d43e7eSAnthony Xu                               ram_addr_t size,
35493d43e7eSAnthony Xu                               MemoryRegion *mr,
35593d43e7eSAnthony Xu                               hwaddr offset_within_region)
35693d43e7eSAnthony Xu {
3572cbf8903SRoss Lagerwall     unsigned long nr_pages;
35893d43e7eSAnthony Xu     int rc = 0;
35993d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
36093d43e7eSAnthony Xu     hwaddr pfn, start_gpfn;
36193d43e7eSAnthony Xu     hwaddr phys_offset = memory_region_get_ram_addr(mr);
36293d43e7eSAnthony Xu     const char *mr_name;
36393d43e7eSAnthony Xu 
36404a8f72eSIgor Druzhinin     if (get_physmapping(start_addr, size)) {
36593d43e7eSAnthony Xu         return 0;
36693d43e7eSAnthony Xu     }
36793d43e7eSAnthony Xu     if (size <= 0) {
36893d43e7eSAnthony Xu         return -1;
36993d43e7eSAnthony Xu     }
37093d43e7eSAnthony Xu 
37193d43e7eSAnthony Xu     /* Xen can only handle a single dirty log region for now and we want
37293d43e7eSAnthony Xu      * the linear framebuffer to be that region.
37393d43e7eSAnthony Xu      * Avoid tracking any regions that is not videoram and avoid tracking
37493d43e7eSAnthony Xu      * the legacy vga region. */
37593d43e7eSAnthony Xu     if (mr == framebuffer && start_addr > 0xbffff) {
37693d43e7eSAnthony Xu         goto go_physmap;
37793d43e7eSAnthony Xu     }
37893d43e7eSAnthony Xu     return -1;
37993d43e7eSAnthony Xu 
38093d43e7eSAnthony Xu go_physmap:
38193d43e7eSAnthony Xu     DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
38293d43e7eSAnthony Xu             start_addr, start_addr + size);
38393d43e7eSAnthony Xu 
384331b5189SIgor Druzhinin     mr_name = memory_region_name(mr);
385331b5189SIgor Druzhinin 
386b21e2380SMarkus Armbruster     physmap = g_new(XenPhysmap, 1);
387331b5189SIgor Druzhinin 
388331b5189SIgor Druzhinin     physmap->start_addr = start_addr;
389331b5189SIgor Druzhinin     physmap->size = size;
390331b5189SIgor Druzhinin     physmap->name = mr_name;
391331b5189SIgor Druzhinin     physmap->phys_offset = phys_offset;
392331b5189SIgor Druzhinin 
39304a8f72eSIgor Druzhinin     QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
394331b5189SIgor Druzhinin 
395331b5189SIgor Druzhinin     if (runstate_check(RUN_STATE_INMIGRATE)) {
396331b5189SIgor Druzhinin         /* Now when we have a physmap entry we can replace a dummy mapping with
397331b5189SIgor Druzhinin          * a real one of guest foreign memory. */
398331b5189SIgor Druzhinin         uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
399331b5189SIgor Druzhinin         assert(p && p == memory_region_get_ram_ptr(mr));
400331b5189SIgor Druzhinin 
401331b5189SIgor Druzhinin         return 0;
402331b5189SIgor Druzhinin     }
403331b5189SIgor Druzhinin 
40493d43e7eSAnthony Xu     pfn = phys_offset >> TARGET_PAGE_BITS;
40593d43e7eSAnthony Xu     start_gpfn = start_addr >> TARGET_PAGE_BITS;
4062cbf8903SRoss Lagerwall     nr_pages = size >> TARGET_PAGE_BITS;
4072cbf8903SRoss Lagerwall     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
4082cbf8903SRoss Lagerwall                                         start_gpfn);
40993d43e7eSAnthony Xu     if (rc) {
4102cbf8903SRoss Lagerwall         int saved_errno = errno;
4112cbf8903SRoss Lagerwall 
4122cbf8903SRoss Lagerwall         error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
4132cbf8903SRoss Lagerwall                      " to GFN %"HWADDR_PRIx" failed: %s",
4142cbf8903SRoss Lagerwall                      nr_pages, pfn, start_gpfn, strerror(saved_errno));
4152cbf8903SRoss Lagerwall         errno = saved_errno;
4162cbf8903SRoss Lagerwall         return -1;
41793d43e7eSAnthony Xu     }
41893d43e7eSAnthony Xu 
4192cbf8903SRoss Lagerwall     rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
42093d43e7eSAnthony Xu                                    start_addr >> TARGET_PAGE_BITS,
42193d43e7eSAnthony Xu                                    (start_addr + size - 1) >> TARGET_PAGE_BITS,
42293d43e7eSAnthony Xu                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
4232cbf8903SRoss Lagerwall     if (rc) {
4242cbf8903SRoss Lagerwall         error_report("pin_memory_cacheattr failed: %s", strerror(errno));
4252cbf8903SRoss Lagerwall     }
426697b66d0SIgor Druzhinin     return xen_save_physmap(state, physmap);
42793d43e7eSAnthony Xu }
42893d43e7eSAnthony Xu 
42993d43e7eSAnthony Xu static int xen_remove_from_physmap(XenIOState *state,
43093d43e7eSAnthony Xu                                    hwaddr start_addr,
43193d43e7eSAnthony Xu                                    ram_addr_t size)
43293d43e7eSAnthony Xu {
43393d43e7eSAnthony Xu     int rc = 0;
43493d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
43593d43e7eSAnthony Xu     hwaddr phys_offset = 0;
43693d43e7eSAnthony Xu 
43704a8f72eSIgor Druzhinin     physmap = get_physmapping(start_addr, size);
43893d43e7eSAnthony Xu     if (physmap == NULL) {
43993d43e7eSAnthony Xu         return -1;
44093d43e7eSAnthony Xu     }
44193d43e7eSAnthony Xu 
44293d43e7eSAnthony Xu     phys_offset = physmap->phys_offset;
44393d43e7eSAnthony Xu     size = physmap->size;
44493d43e7eSAnthony Xu 
44593d43e7eSAnthony Xu     DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
44693d43e7eSAnthony Xu             "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
44793d43e7eSAnthony Xu 
44893d43e7eSAnthony Xu     size >>= TARGET_PAGE_BITS;
44993d43e7eSAnthony Xu     start_addr >>= TARGET_PAGE_BITS;
45093d43e7eSAnthony Xu     phys_offset >>= TARGET_PAGE_BITS;
4512cbf8903SRoss Lagerwall     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
4522cbf8903SRoss Lagerwall                                         phys_offset);
45393d43e7eSAnthony Xu     if (rc) {
4542cbf8903SRoss Lagerwall         int saved_errno = errno;
4552cbf8903SRoss Lagerwall 
4562cbf8903SRoss Lagerwall         error_report("relocate_memory "RAM_ADDR_FMT" pages"
4572cbf8903SRoss Lagerwall                      " from GFN %"HWADDR_PRIx
4582cbf8903SRoss Lagerwall                      " to GFN %"HWADDR_PRIx" failed: %s",
4592cbf8903SRoss Lagerwall                      size, start_addr, phys_offset, strerror(saved_errno));
4602cbf8903SRoss Lagerwall         errno = saved_errno;
4612cbf8903SRoss Lagerwall         return -1;
46293d43e7eSAnthony Xu     }
46393d43e7eSAnthony Xu 
46493d43e7eSAnthony Xu     QLIST_REMOVE(physmap, list);
465*9269b9d1SStefano Stabellini     if (log_for_dirtybit == physmap) {
466*9269b9d1SStefano Stabellini         log_for_dirtybit = NULL;
467*9269b9d1SStefano Stabellini         g_free(dirty_bitmap);
468*9269b9d1SStefano Stabellini         dirty_bitmap = NULL;
46993d43e7eSAnthony Xu     }
47093d43e7eSAnthony Xu     g_free(physmap);
47193d43e7eSAnthony Xu 
47293d43e7eSAnthony Xu     return 0;
47393d43e7eSAnthony Xu }
47493d43e7eSAnthony Xu 
47593d43e7eSAnthony Xu static void xen_set_memory(struct MemoryListener *listener,
47693d43e7eSAnthony Xu                            MemoryRegionSection *section,
47793d43e7eSAnthony Xu                            bool add)
47893d43e7eSAnthony Xu {
47993d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
48093d43e7eSAnthony Xu     hwaddr start_addr = section->offset_within_address_space;
48193d43e7eSAnthony Xu     ram_addr_t size = int128_get64(section->size);
48293d43e7eSAnthony Xu     bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
48393d43e7eSAnthony Xu     hvmmem_type_t mem_type;
48493d43e7eSAnthony Xu 
48593d43e7eSAnthony Xu     if (section->mr == &ram_memory) {
48693d43e7eSAnthony Xu         return;
48793d43e7eSAnthony Xu     } else {
48893d43e7eSAnthony Xu         if (add) {
48993d43e7eSAnthony Xu             xen_map_memory_section(xen_domid, state->ioservid,
49093d43e7eSAnthony Xu                                    section);
49193d43e7eSAnthony Xu         } else {
49293d43e7eSAnthony Xu             xen_unmap_memory_section(xen_domid, state->ioservid,
49393d43e7eSAnthony Xu                                      section);
49493d43e7eSAnthony Xu         }
49593d43e7eSAnthony Xu     }
49693d43e7eSAnthony Xu 
49793d43e7eSAnthony Xu     if (!memory_region_is_ram(section->mr)) {
49893d43e7eSAnthony Xu         return;
49993d43e7eSAnthony Xu     }
50093d43e7eSAnthony Xu 
50193d43e7eSAnthony Xu     if (log_dirty != add) {
50293d43e7eSAnthony Xu         return;
50393d43e7eSAnthony Xu     }
50493d43e7eSAnthony Xu 
50593d43e7eSAnthony Xu     trace_xen_client_set_memory(start_addr, size, log_dirty);
50693d43e7eSAnthony Xu 
50793d43e7eSAnthony Xu     start_addr &= TARGET_PAGE_MASK;
50893d43e7eSAnthony Xu     size = TARGET_PAGE_ALIGN(size);
50993d43e7eSAnthony Xu 
51093d43e7eSAnthony Xu     if (add) {
51193d43e7eSAnthony Xu         if (!memory_region_is_rom(section->mr)) {
51293d43e7eSAnthony Xu             xen_add_to_physmap(state, start_addr, size,
51393d43e7eSAnthony Xu                                section->mr, section->offset_within_region);
51493d43e7eSAnthony Xu         } else {
51593d43e7eSAnthony Xu             mem_type = HVMMEM_ram_ro;
51693d43e7eSAnthony Xu             if (xen_set_mem_type(xen_domid, mem_type,
51793d43e7eSAnthony Xu                                  start_addr >> TARGET_PAGE_BITS,
51893d43e7eSAnthony Xu                                  size >> TARGET_PAGE_BITS)) {
519883f2c59SPhilippe Mathieu-Daudé                 DPRINTF("xen_set_mem_type error, addr: "HWADDR_FMT_plx"\n",
52093d43e7eSAnthony Xu                         start_addr);
52193d43e7eSAnthony Xu             }
52293d43e7eSAnthony Xu         }
52393d43e7eSAnthony Xu     } else {
52493d43e7eSAnthony Xu         if (xen_remove_from_physmap(state, start_addr, size) < 0) {
525883f2c59SPhilippe Mathieu-Daudé             DPRINTF("physmapping does not exist at "HWADDR_FMT_plx"\n", start_addr);
52693d43e7eSAnthony Xu         }
52793d43e7eSAnthony Xu     }
52893d43e7eSAnthony Xu }
52993d43e7eSAnthony Xu 
53093d43e7eSAnthony Xu static void xen_region_add(MemoryListener *listener,
53193d43e7eSAnthony Xu                            MemoryRegionSection *section)
53293d43e7eSAnthony Xu {
53393d43e7eSAnthony Xu     memory_region_ref(section->mr);
53493d43e7eSAnthony Xu     xen_set_memory(listener, section, true);
53593d43e7eSAnthony Xu }
53693d43e7eSAnthony Xu 
53793d43e7eSAnthony Xu static void xen_region_del(MemoryListener *listener,
53893d43e7eSAnthony Xu                            MemoryRegionSection *section)
53993d43e7eSAnthony Xu {
54093d43e7eSAnthony Xu     xen_set_memory(listener, section, false);
54193d43e7eSAnthony Xu     memory_region_unref(section->mr);
54293d43e7eSAnthony Xu }
54393d43e7eSAnthony Xu 
54493d43e7eSAnthony Xu static void xen_io_add(MemoryListener *listener,
54593d43e7eSAnthony Xu                        MemoryRegionSection *section)
54693d43e7eSAnthony Xu {
54793d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, io_listener);
54893d43e7eSAnthony Xu     MemoryRegion *mr = section->mr;
54993d43e7eSAnthony Xu 
55093d43e7eSAnthony Xu     if (mr->ops == &unassigned_io_ops) {
55193d43e7eSAnthony Xu         return;
55293d43e7eSAnthony Xu     }
55393d43e7eSAnthony Xu 
55493d43e7eSAnthony Xu     memory_region_ref(mr);
55593d43e7eSAnthony Xu 
55693d43e7eSAnthony Xu     xen_map_io_section(xen_domid, state->ioservid, section);
55793d43e7eSAnthony Xu }
55893d43e7eSAnthony Xu 
55993d43e7eSAnthony Xu static void xen_io_del(MemoryListener *listener,
56093d43e7eSAnthony Xu                        MemoryRegionSection *section)
56193d43e7eSAnthony Xu {
56293d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, io_listener);
56393d43e7eSAnthony Xu     MemoryRegion *mr = section->mr;
56493d43e7eSAnthony Xu 
56593d43e7eSAnthony Xu     if (mr->ops == &unassigned_io_ops) {
56693d43e7eSAnthony Xu         return;
56793d43e7eSAnthony Xu     }
56893d43e7eSAnthony Xu 
56993d43e7eSAnthony Xu     xen_unmap_io_section(xen_domid, state->ioservid, section);
57093d43e7eSAnthony Xu 
57193d43e7eSAnthony Xu     memory_region_unref(mr);
57293d43e7eSAnthony Xu }
57393d43e7eSAnthony Xu 
57493d43e7eSAnthony Xu static void xen_device_realize(DeviceListener *listener,
57593d43e7eSAnthony Xu                                DeviceState *dev)
57693d43e7eSAnthony Xu {
57793d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, device_listener);
57893d43e7eSAnthony Xu 
57993d43e7eSAnthony Xu     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
58093d43e7eSAnthony Xu         PCIDevice *pci_dev = PCI_DEVICE(dev);
581dfb6578dSPaul Durrant         XenPciDevice *xendev = g_new(XenPciDevice, 1);
582dfb6578dSPaul Durrant 
583dfb6578dSPaul Durrant         xendev->pci_dev = pci_dev;
584dfb6578dSPaul Durrant         xendev->sbdf = PCI_BUILD_BDF(pci_dev_bus_num(pci_dev),
585dfb6578dSPaul Durrant                                      pci_dev->devfn);
586dfb6578dSPaul Durrant         QLIST_INSERT_HEAD(&state->dev_list, xendev, entry);
58793d43e7eSAnthony Xu 
58893d43e7eSAnthony Xu         xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
58993d43e7eSAnthony Xu     }
59093d43e7eSAnthony Xu }
59193d43e7eSAnthony Xu 
59293d43e7eSAnthony Xu static void xen_device_unrealize(DeviceListener *listener,
59393d43e7eSAnthony Xu                                  DeviceState *dev)
59493d43e7eSAnthony Xu {
59593d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, device_listener);
59693d43e7eSAnthony Xu 
59793d43e7eSAnthony Xu     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
59893d43e7eSAnthony Xu         PCIDevice *pci_dev = PCI_DEVICE(dev);
599dfb6578dSPaul Durrant         XenPciDevice *xendev, *next;
60093d43e7eSAnthony Xu 
60193d43e7eSAnthony Xu         xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
602dfb6578dSPaul Durrant 
603dfb6578dSPaul Durrant         QLIST_FOREACH_SAFE(xendev, &state->dev_list, entry, next) {
604dfb6578dSPaul Durrant             if (xendev->pci_dev == pci_dev) {
605dfb6578dSPaul Durrant                 QLIST_REMOVE(xendev, entry);
606dfb6578dSPaul Durrant                 g_free(xendev);
607dfb6578dSPaul Durrant                 break;
608dfb6578dSPaul Durrant             }
609dfb6578dSPaul Durrant         }
61093d43e7eSAnthony Xu     }
61193d43e7eSAnthony Xu }
61293d43e7eSAnthony Xu 
61393d43e7eSAnthony Xu static void xen_sync_dirty_bitmap(XenIOState *state,
61493d43e7eSAnthony Xu                                   hwaddr start_addr,
61593d43e7eSAnthony Xu                                   ram_addr_t size)
61693d43e7eSAnthony Xu {
61793d43e7eSAnthony Xu     hwaddr npages = size >> TARGET_PAGE_BITS;
61893d43e7eSAnthony Xu     const int width = sizeof(unsigned long) * 8;
61934fbbc16SAnthony PERARD     size_t bitmap_size = DIV_ROUND_UP(npages, width);
62093d43e7eSAnthony Xu     int rc, i, j;
62193d43e7eSAnthony Xu     const XenPhysmap *physmap = NULL;
62293d43e7eSAnthony Xu 
62304a8f72eSIgor Druzhinin     physmap = get_physmapping(start_addr, size);
62493d43e7eSAnthony Xu     if (physmap == NULL) {
62593d43e7eSAnthony Xu         /* not handled */
62693d43e7eSAnthony Xu         return;
62793d43e7eSAnthony Xu     }
62893d43e7eSAnthony Xu 
629*9269b9d1SStefano Stabellini     if (log_for_dirtybit == NULL) {
630*9269b9d1SStefano Stabellini         log_for_dirtybit = physmap;
631*9269b9d1SStefano Stabellini         dirty_bitmap = g_new(unsigned long, bitmap_size);
632*9269b9d1SStefano Stabellini     } else if (log_for_dirtybit != physmap) {
63393d43e7eSAnthony Xu         /* Only one range for dirty bitmap can be tracked. */
63493d43e7eSAnthony Xu         return;
63593d43e7eSAnthony Xu     }
63693d43e7eSAnthony Xu 
63793d43e7eSAnthony Xu     rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
638*9269b9d1SStefano Stabellini                               npages, dirty_bitmap);
63993d43e7eSAnthony Xu     if (rc < 0) {
64093d43e7eSAnthony Xu #ifndef ENODATA
64193d43e7eSAnthony Xu #define ENODATA  ENOENT
64293d43e7eSAnthony Xu #endif
64393d43e7eSAnthony Xu         if (errno == ENODATA) {
64493d43e7eSAnthony Xu             memory_region_set_dirty(framebuffer, 0, size);
645883f2c59SPhilippe Mathieu-Daudé             DPRINTF("xen: track_dirty_vram failed (0x" HWADDR_FMT_plx
646883f2c59SPhilippe Mathieu-Daudé                     ", 0x" HWADDR_FMT_plx "): %s\n",
64793d43e7eSAnthony Xu                     start_addr, start_addr + size, strerror(errno));
64893d43e7eSAnthony Xu         }
64993d43e7eSAnthony Xu         return;
65093d43e7eSAnthony Xu     }
65193d43e7eSAnthony Xu 
65234fbbc16SAnthony PERARD     for (i = 0; i < bitmap_size; i++) {
653*9269b9d1SStefano Stabellini         unsigned long map = dirty_bitmap[i];
65493d43e7eSAnthony Xu         while (map != 0) {
65593d43e7eSAnthony Xu             j = ctzl(map);
65693d43e7eSAnthony Xu             map &= ~(1ul << j);
65793d43e7eSAnthony Xu             memory_region_set_dirty(framebuffer,
65893d43e7eSAnthony Xu                                     (i * width + j) * TARGET_PAGE_SIZE,
65993d43e7eSAnthony Xu                                     TARGET_PAGE_SIZE);
66093d43e7eSAnthony Xu         };
66193d43e7eSAnthony Xu     }
66293d43e7eSAnthony Xu }
66393d43e7eSAnthony Xu 
66493d43e7eSAnthony Xu static void xen_log_start(MemoryListener *listener,
66593d43e7eSAnthony Xu                           MemoryRegionSection *section,
66693d43e7eSAnthony Xu                           int old, int new)
66793d43e7eSAnthony Xu {
66893d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
66993d43e7eSAnthony Xu 
67093d43e7eSAnthony Xu     if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
67193d43e7eSAnthony Xu         xen_sync_dirty_bitmap(state, section->offset_within_address_space,
67293d43e7eSAnthony Xu                               int128_get64(section->size));
67393d43e7eSAnthony Xu     }
67493d43e7eSAnthony Xu }
67593d43e7eSAnthony Xu 
67693d43e7eSAnthony Xu static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
67793d43e7eSAnthony Xu                          int old, int new)
67893d43e7eSAnthony Xu {
67993d43e7eSAnthony Xu     if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
680*9269b9d1SStefano Stabellini         log_for_dirtybit = NULL;
681*9269b9d1SStefano Stabellini         g_free(dirty_bitmap);
682*9269b9d1SStefano Stabellini         dirty_bitmap = NULL;
68393d43e7eSAnthony Xu         /* Disable dirty bit tracking */
68493d43e7eSAnthony Xu         xen_track_dirty_vram(xen_domid, 0, 0, NULL);
68593d43e7eSAnthony Xu     }
68693d43e7eSAnthony Xu }
68793d43e7eSAnthony Xu 
68893d43e7eSAnthony Xu static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
68993d43e7eSAnthony Xu {
69093d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
69193d43e7eSAnthony Xu 
69293d43e7eSAnthony Xu     xen_sync_dirty_bitmap(state, section->offset_within_address_space,
69393d43e7eSAnthony Xu                           int128_get64(section->size));
69493d43e7eSAnthony Xu }
69593d43e7eSAnthony Xu 
69693d43e7eSAnthony Xu static void xen_log_global_start(MemoryListener *listener)
69793d43e7eSAnthony Xu {
69893d43e7eSAnthony Xu     if (xen_enabled()) {
69993d43e7eSAnthony Xu         xen_in_migration = true;
70093d43e7eSAnthony Xu     }
70193d43e7eSAnthony Xu }
70293d43e7eSAnthony Xu 
70393d43e7eSAnthony Xu static void xen_log_global_stop(MemoryListener *listener)
70493d43e7eSAnthony Xu {
70593d43e7eSAnthony Xu     xen_in_migration = false;
70693d43e7eSAnthony Xu }
70793d43e7eSAnthony Xu 
70893d43e7eSAnthony Xu static MemoryListener xen_memory_listener = {
709142518bdSPeter Xu     .name = "xen-memory",
71093d43e7eSAnthony Xu     .region_add = xen_region_add,
71193d43e7eSAnthony Xu     .region_del = xen_region_del,
71293d43e7eSAnthony Xu     .log_start = xen_log_start,
71393d43e7eSAnthony Xu     .log_stop = xen_log_stop,
71493d43e7eSAnthony Xu     .log_sync = xen_log_sync,
71593d43e7eSAnthony Xu     .log_global_start = xen_log_global_start,
71693d43e7eSAnthony Xu     .log_global_stop = xen_log_global_stop,
71793d43e7eSAnthony Xu     .priority = 10,
71893d43e7eSAnthony Xu };
71993d43e7eSAnthony Xu 
72093d43e7eSAnthony Xu static MemoryListener xen_io_listener = {
721142518bdSPeter Xu     .name = "xen-io",
72293d43e7eSAnthony Xu     .region_add = xen_io_add,
72393d43e7eSAnthony Xu     .region_del = xen_io_del,
72493d43e7eSAnthony Xu     .priority = 10,
72593d43e7eSAnthony Xu };
72693d43e7eSAnthony Xu 
72793d43e7eSAnthony Xu static DeviceListener xen_device_listener = {
72893d43e7eSAnthony Xu     .realize = xen_device_realize,
72993d43e7eSAnthony Xu     .unrealize = xen_device_unrealize,
73093d43e7eSAnthony Xu };
73193d43e7eSAnthony Xu 
73293d43e7eSAnthony Xu /* get the ioreq packets from share mem */
73393d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
73493d43e7eSAnthony Xu {
73593d43e7eSAnthony Xu     ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
73693d43e7eSAnthony Xu 
73793d43e7eSAnthony Xu     if (req->state != STATE_IOREQ_READY) {
73893d43e7eSAnthony Xu         DPRINTF("I/O request not ready: "
73993d43e7eSAnthony Xu                 "%x, ptr: %x, port: %"PRIx64", "
74093d43e7eSAnthony Xu                 "data: %"PRIx64", count: %u, size: %u\n",
74193d43e7eSAnthony Xu                 req->state, req->data_is_ptr, req->addr,
74293d43e7eSAnthony Xu                 req->data, req->count, req->size);
74393d43e7eSAnthony Xu         return NULL;
74493d43e7eSAnthony Xu     }
74593d43e7eSAnthony Xu 
74693d43e7eSAnthony Xu     xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
74793d43e7eSAnthony Xu 
74893d43e7eSAnthony Xu     req->state = STATE_IOREQ_INPROCESS;
74993d43e7eSAnthony Xu     return req;
75093d43e7eSAnthony Xu }
75193d43e7eSAnthony Xu 
75293d43e7eSAnthony Xu /* use poll to get the port notification */
75393d43e7eSAnthony Xu /* ioreq_vec--out,the */
75493d43e7eSAnthony Xu /* retval--the number of ioreq packet */
75593d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq(XenIOState *state)
75693d43e7eSAnthony Xu {
7570e11fc69SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7580e11fc69SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
75993d43e7eSAnthony Xu     int i;
76093d43e7eSAnthony Xu     evtchn_port_t port;
76193d43e7eSAnthony Xu 
762b6cacfeaSDavid Woodhouse     port = qemu_xen_evtchn_pending(state->xce_handle);
76393d43e7eSAnthony Xu     if (port == state->bufioreq_local_port) {
76493d43e7eSAnthony Xu         timer_mod(state->buffered_io_timer,
76593d43e7eSAnthony Xu                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
76693d43e7eSAnthony Xu         return NULL;
76793d43e7eSAnthony Xu     }
76893d43e7eSAnthony Xu 
76993d43e7eSAnthony Xu     if (port != -1) {
77093d43e7eSAnthony Xu         for (i = 0; i < max_cpus; i++) {
77193d43e7eSAnthony Xu             if (state->ioreq_local_port[i] == port) {
77293d43e7eSAnthony Xu                 break;
77393d43e7eSAnthony Xu             }
77493d43e7eSAnthony Xu         }
77593d43e7eSAnthony Xu 
77693d43e7eSAnthony Xu         if (i == max_cpus) {
77793d43e7eSAnthony Xu             hw_error("Fatal error while trying to get io event!\n");
77893d43e7eSAnthony Xu         }
77993d43e7eSAnthony Xu 
78093d43e7eSAnthony Xu         /* unmask the wanted port again */
781b6cacfeaSDavid Woodhouse         qemu_xen_evtchn_unmask(state->xce_handle, port);
78293d43e7eSAnthony Xu 
78393d43e7eSAnthony Xu         /* get the io packet from shared memory */
78493d43e7eSAnthony Xu         state->send_vcpu = i;
78593d43e7eSAnthony Xu         return cpu_get_ioreq_from_shared_memory(state, i);
78693d43e7eSAnthony Xu     }
78793d43e7eSAnthony Xu 
78893d43e7eSAnthony Xu     /* read error or read nothing */
78993d43e7eSAnthony Xu     return NULL;
79093d43e7eSAnthony Xu }
79193d43e7eSAnthony Xu 
79293d43e7eSAnthony Xu static uint32_t do_inp(uint32_t addr, unsigned long size)
79393d43e7eSAnthony Xu {
79493d43e7eSAnthony Xu     switch (size) {
79593d43e7eSAnthony Xu         case 1:
79693d43e7eSAnthony Xu             return cpu_inb(addr);
79793d43e7eSAnthony Xu         case 2:
79893d43e7eSAnthony Xu             return cpu_inw(addr);
79993d43e7eSAnthony Xu         case 4:
80093d43e7eSAnthony Xu             return cpu_inl(addr);
80193d43e7eSAnthony Xu         default:
80293d43e7eSAnthony Xu             hw_error("inp: bad size: %04x %lx", addr, size);
80393d43e7eSAnthony Xu     }
80493d43e7eSAnthony Xu }
80593d43e7eSAnthony Xu 
80693d43e7eSAnthony Xu static void do_outp(uint32_t addr,
80793d43e7eSAnthony Xu         unsigned long size, uint32_t val)
80893d43e7eSAnthony Xu {
80993d43e7eSAnthony Xu     switch (size) {
81093d43e7eSAnthony Xu         case 1:
81193d43e7eSAnthony Xu             return cpu_outb(addr, val);
81293d43e7eSAnthony Xu         case 2:
81393d43e7eSAnthony Xu             return cpu_outw(addr, val);
81493d43e7eSAnthony Xu         case 4:
81593d43e7eSAnthony Xu             return cpu_outl(addr, val);
81693d43e7eSAnthony Xu         default:
81793d43e7eSAnthony Xu             hw_error("outp: bad size: %04x %lx", addr, size);
81893d43e7eSAnthony Xu     }
81993d43e7eSAnthony Xu }
82093d43e7eSAnthony Xu 
82193d43e7eSAnthony Xu /*
82293d43e7eSAnthony Xu  * Helper functions which read/write an object from/to physical guest
82393d43e7eSAnthony Xu  * memory, as part of the implementation of an ioreq.
82493d43e7eSAnthony Xu  *
82593d43e7eSAnthony Xu  * Equivalent to
82693d43e7eSAnthony Xu  *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
82793d43e7eSAnthony Xu  *                          val, req->size, 0/1)
82893d43e7eSAnthony Xu  * except without the integer overflow problems.
82993d43e7eSAnthony Xu  */
83093d43e7eSAnthony Xu static void rw_phys_req_item(hwaddr addr,
83193d43e7eSAnthony Xu                              ioreq_t *req, uint32_t i, void *val, int rw)
83293d43e7eSAnthony Xu {
83393d43e7eSAnthony Xu     /* Do everything unsigned so overflow just results in a truncated result
83493d43e7eSAnthony Xu      * and accesses to undesired parts of guest memory, which is up
83593d43e7eSAnthony Xu      * to the guest */
83693d43e7eSAnthony Xu     hwaddr offset = (hwaddr)req->size * i;
83793d43e7eSAnthony Xu     if (req->df) {
83893d43e7eSAnthony Xu         addr -= offset;
83993d43e7eSAnthony Xu     } else {
84093d43e7eSAnthony Xu         addr += offset;
84193d43e7eSAnthony Xu     }
84293d43e7eSAnthony Xu     cpu_physical_memory_rw(addr, val, req->size, rw);
84393d43e7eSAnthony Xu }
84493d43e7eSAnthony Xu 
84593d43e7eSAnthony Xu static inline void read_phys_req_item(hwaddr addr,
84693d43e7eSAnthony Xu                                       ioreq_t *req, uint32_t i, void *val)
84793d43e7eSAnthony Xu {
84893d43e7eSAnthony Xu     rw_phys_req_item(addr, req, i, val, 0);
84993d43e7eSAnthony Xu }
85093d43e7eSAnthony Xu static inline void write_phys_req_item(hwaddr addr,
85193d43e7eSAnthony Xu                                        ioreq_t *req, uint32_t i, void *val)
85293d43e7eSAnthony Xu {
85393d43e7eSAnthony Xu     rw_phys_req_item(addr, req, i, val, 1);
85493d43e7eSAnthony Xu }
85593d43e7eSAnthony Xu 
85693d43e7eSAnthony Xu 
85793d43e7eSAnthony Xu static void cpu_ioreq_pio(ioreq_t *req)
85893d43e7eSAnthony Xu {
85993d43e7eSAnthony Xu     uint32_t i;
86093d43e7eSAnthony Xu 
86193d43e7eSAnthony Xu     trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
86293d43e7eSAnthony Xu                          req->data, req->count, req->size);
86393d43e7eSAnthony Xu 
86493d43e7eSAnthony Xu     if (req->size > sizeof(uint32_t)) {
86593d43e7eSAnthony Xu         hw_error("PIO: bad size (%u)", req->size);
86693d43e7eSAnthony Xu     }
86793d43e7eSAnthony Xu 
86893d43e7eSAnthony Xu     if (req->dir == IOREQ_READ) {
86993d43e7eSAnthony Xu         if (!req->data_is_ptr) {
87093d43e7eSAnthony Xu             req->data = do_inp(req->addr, req->size);
87193d43e7eSAnthony Xu             trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
87293d43e7eSAnthony Xu                                          req->size);
87393d43e7eSAnthony Xu         } else {
87493d43e7eSAnthony Xu             uint32_t tmp;
87593d43e7eSAnthony Xu 
87693d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
87793d43e7eSAnthony Xu                 tmp = do_inp(req->addr, req->size);
87893d43e7eSAnthony Xu                 write_phys_req_item(req->data, req, i, &tmp);
87993d43e7eSAnthony Xu             }
88093d43e7eSAnthony Xu         }
88193d43e7eSAnthony Xu     } else if (req->dir == IOREQ_WRITE) {
88293d43e7eSAnthony Xu         if (!req->data_is_ptr) {
88393d43e7eSAnthony Xu             trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
88493d43e7eSAnthony Xu                                           req->size);
88593d43e7eSAnthony Xu             do_outp(req->addr, req->size, req->data);
88693d43e7eSAnthony Xu         } else {
88793d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
88893d43e7eSAnthony Xu                 uint32_t tmp = 0;
88993d43e7eSAnthony Xu 
89093d43e7eSAnthony Xu                 read_phys_req_item(req->data, req, i, &tmp);
89193d43e7eSAnthony Xu                 do_outp(req->addr, req->size, tmp);
89293d43e7eSAnthony Xu             }
89393d43e7eSAnthony Xu         }
89493d43e7eSAnthony Xu     }
89593d43e7eSAnthony Xu }
89693d43e7eSAnthony Xu 
89793d43e7eSAnthony Xu static void cpu_ioreq_move(ioreq_t *req)
89893d43e7eSAnthony Xu {
89993d43e7eSAnthony Xu     uint32_t i;
90093d43e7eSAnthony Xu 
90193d43e7eSAnthony Xu     trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
90293d43e7eSAnthony Xu                          req->data, req->count, req->size);
90393d43e7eSAnthony Xu 
90493d43e7eSAnthony Xu     if (req->size > sizeof(req->data)) {
90593d43e7eSAnthony Xu         hw_error("MMIO: bad size (%u)", req->size);
90693d43e7eSAnthony Xu     }
90793d43e7eSAnthony Xu 
90893d43e7eSAnthony Xu     if (!req->data_is_ptr) {
90993d43e7eSAnthony Xu         if (req->dir == IOREQ_READ) {
91093d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
91193d43e7eSAnthony Xu                 read_phys_req_item(req->addr, req, i, &req->data);
91293d43e7eSAnthony Xu             }
91393d43e7eSAnthony Xu         } else if (req->dir == IOREQ_WRITE) {
91493d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
91593d43e7eSAnthony Xu                 write_phys_req_item(req->addr, req, i, &req->data);
91693d43e7eSAnthony Xu             }
91793d43e7eSAnthony Xu         }
91893d43e7eSAnthony Xu     } else {
91993d43e7eSAnthony Xu         uint64_t tmp;
92093d43e7eSAnthony Xu 
92193d43e7eSAnthony Xu         if (req->dir == IOREQ_READ) {
92293d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
92393d43e7eSAnthony Xu                 read_phys_req_item(req->addr, req, i, &tmp);
92493d43e7eSAnthony Xu                 write_phys_req_item(req->data, req, i, &tmp);
92593d43e7eSAnthony Xu             }
92693d43e7eSAnthony Xu         } else if (req->dir == IOREQ_WRITE) {
92793d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
92893d43e7eSAnthony Xu                 read_phys_req_item(req->data, req, i, &tmp);
92993d43e7eSAnthony Xu                 write_phys_req_item(req->addr, req, i, &tmp);
93093d43e7eSAnthony Xu             }
93193d43e7eSAnthony Xu         }
93293d43e7eSAnthony Xu     }
93393d43e7eSAnthony Xu }
93493d43e7eSAnthony Xu 
935dfb6578dSPaul Durrant static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
936dfb6578dSPaul Durrant {
937dfb6578dSPaul Durrant     uint32_t sbdf = req->addr >> 32;
938dfb6578dSPaul Durrant     uint32_t reg = req->addr;
939dfb6578dSPaul Durrant     XenPciDevice *xendev;
940dfb6578dSPaul Durrant 
941dfb6578dSPaul Durrant     if (req->size != sizeof(uint8_t) && req->size != sizeof(uint16_t) &&
942dfb6578dSPaul Durrant         req->size != sizeof(uint32_t)) {
943dfb6578dSPaul Durrant         hw_error("PCI config access: bad size (%u)", req->size);
944dfb6578dSPaul Durrant     }
945dfb6578dSPaul Durrant 
946dfb6578dSPaul Durrant     if (req->count != 1) {
947dfb6578dSPaul Durrant         hw_error("PCI config access: bad count (%u)", req->count);
948dfb6578dSPaul Durrant     }
949dfb6578dSPaul Durrant 
950dfb6578dSPaul Durrant     QLIST_FOREACH(xendev, &state->dev_list, entry) {
951dfb6578dSPaul Durrant         if (xendev->sbdf != sbdf) {
952dfb6578dSPaul Durrant             continue;
953dfb6578dSPaul Durrant         }
954dfb6578dSPaul Durrant 
955dfb6578dSPaul Durrant         if (!req->data_is_ptr) {
956dfb6578dSPaul Durrant             if (req->dir == IOREQ_READ) {
957dfb6578dSPaul Durrant                 req->data = pci_host_config_read_common(
958dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
959dfb6578dSPaul Durrant                     req->size);
960dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
961dfb6578dSPaul Durrant                                             req->size, req->data);
962dfb6578dSPaul Durrant             } else if (req->dir == IOREQ_WRITE) {
963dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
964dfb6578dSPaul Durrant                                              req->size, req->data);
965dfb6578dSPaul Durrant                 pci_host_config_write_common(
966dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
967dfb6578dSPaul Durrant                     req->data, req->size);
968dfb6578dSPaul Durrant             }
969dfb6578dSPaul Durrant         } else {
970dfb6578dSPaul Durrant             uint32_t tmp;
971dfb6578dSPaul Durrant 
972dfb6578dSPaul Durrant             if (req->dir == IOREQ_READ) {
973dfb6578dSPaul Durrant                 tmp = pci_host_config_read_common(
974dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
975dfb6578dSPaul Durrant                     req->size);
976dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
977dfb6578dSPaul Durrant                                             req->size, tmp);
978dfb6578dSPaul Durrant                 write_phys_req_item(req->data, req, 0, &tmp);
979dfb6578dSPaul Durrant             } else if (req->dir == IOREQ_WRITE) {
980dfb6578dSPaul Durrant                 read_phys_req_item(req->data, req, 0, &tmp);
981dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
982dfb6578dSPaul Durrant                                              req->size, tmp);
983dfb6578dSPaul Durrant                 pci_host_config_write_common(
984dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
985dfb6578dSPaul Durrant                     tmp, req->size);
986dfb6578dSPaul Durrant             }
987dfb6578dSPaul Durrant         }
988dfb6578dSPaul Durrant     }
989dfb6578dSPaul Durrant }
990dfb6578dSPaul Durrant 
99193d43e7eSAnthony Xu static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
99293d43e7eSAnthony Xu {
99393d43e7eSAnthony Xu     X86CPU *cpu;
99493d43e7eSAnthony Xu     CPUX86State *env;
99593d43e7eSAnthony Xu 
99693d43e7eSAnthony Xu     cpu = X86_CPU(current_cpu);
99793d43e7eSAnthony Xu     env = &cpu->env;
99893d43e7eSAnthony Xu     env->regs[R_EAX] = req->data;
99993d43e7eSAnthony Xu     env->regs[R_EBX] = vmport_regs->ebx;
100093d43e7eSAnthony Xu     env->regs[R_ECX] = vmport_regs->ecx;
100193d43e7eSAnthony Xu     env->regs[R_EDX] = vmport_regs->edx;
100293d43e7eSAnthony Xu     env->regs[R_ESI] = vmport_regs->esi;
100393d43e7eSAnthony Xu     env->regs[R_EDI] = vmport_regs->edi;
100493d43e7eSAnthony Xu }
100593d43e7eSAnthony Xu 
100693d43e7eSAnthony Xu static void regs_from_cpu(vmware_regs_t *vmport_regs)
100793d43e7eSAnthony Xu {
100893d43e7eSAnthony Xu     X86CPU *cpu = X86_CPU(current_cpu);
100993d43e7eSAnthony Xu     CPUX86State *env = &cpu->env;
101093d43e7eSAnthony Xu 
101193d43e7eSAnthony Xu     vmport_regs->ebx = env->regs[R_EBX];
101293d43e7eSAnthony Xu     vmport_regs->ecx = env->regs[R_ECX];
101393d43e7eSAnthony Xu     vmport_regs->edx = env->regs[R_EDX];
101493d43e7eSAnthony Xu     vmport_regs->esi = env->regs[R_ESI];
101593d43e7eSAnthony Xu     vmport_regs->edi = env->regs[R_EDI];
101693d43e7eSAnthony Xu }
101793d43e7eSAnthony Xu 
101893d43e7eSAnthony Xu static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
101993d43e7eSAnthony Xu {
102093d43e7eSAnthony Xu     vmware_regs_t *vmport_regs;
102193d43e7eSAnthony Xu 
1022*9269b9d1SStefano Stabellini     assert(shared_vmport_page);
102393d43e7eSAnthony Xu     vmport_regs =
1024*9269b9d1SStefano Stabellini         &shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
102593d43e7eSAnthony Xu     QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
102693d43e7eSAnthony Xu 
102793d43e7eSAnthony Xu     current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
102893d43e7eSAnthony Xu     regs_to_cpu(vmport_regs, req);
102993d43e7eSAnthony Xu     cpu_ioreq_pio(req);
103093d43e7eSAnthony Xu     regs_from_cpu(vmport_regs);
103193d43e7eSAnthony Xu     current_cpu = NULL;
103293d43e7eSAnthony Xu }
103393d43e7eSAnthony Xu 
103493d43e7eSAnthony Xu static void handle_ioreq(XenIOState *state, ioreq_t *req)
103593d43e7eSAnthony Xu {
103693d43e7eSAnthony Xu     trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
103793d43e7eSAnthony Xu                        req->addr, req->data, req->count, req->size);
103893d43e7eSAnthony Xu 
103993d43e7eSAnthony Xu     if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
104093d43e7eSAnthony Xu             (req->size < sizeof (target_ulong))) {
104193d43e7eSAnthony Xu         req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
104293d43e7eSAnthony Xu     }
104393d43e7eSAnthony Xu 
104493d43e7eSAnthony Xu     if (req->dir == IOREQ_WRITE)
104593d43e7eSAnthony Xu         trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
104693d43e7eSAnthony Xu                                  req->addr, req->data, req->count, req->size);
104793d43e7eSAnthony Xu 
104893d43e7eSAnthony Xu     switch (req->type) {
104993d43e7eSAnthony Xu         case IOREQ_TYPE_PIO:
105093d43e7eSAnthony Xu             cpu_ioreq_pio(req);
105193d43e7eSAnthony Xu             break;
105293d43e7eSAnthony Xu         case IOREQ_TYPE_COPY:
105393d43e7eSAnthony Xu             cpu_ioreq_move(req);
105493d43e7eSAnthony Xu             break;
105593d43e7eSAnthony Xu         case IOREQ_TYPE_VMWARE_PORT:
105693d43e7eSAnthony Xu             handle_vmport_ioreq(state, req);
105793d43e7eSAnthony Xu             break;
105893d43e7eSAnthony Xu         case IOREQ_TYPE_TIMEOFFSET:
105993d43e7eSAnthony Xu             break;
106093d43e7eSAnthony Xu         case IOREQ_TYPE_INVALIDATE:
106193d43e7eSAnthony Xu             xen_invalidate_map_cache();
106293d43e7eSAnthony Xu             break;
1063dfb6578dSPaul Durrant         case IOREQ_TYPE_PCI_CONFIG:
1064dfb6578dSPaul Durrant             cpu_ioreq_config(state, req);
106593d43e7eSAnthony Xu             break;
106693d43e7eSAnthony Xu         default:
106793d43e7eSAnthony Xu             hw_error("Invalid ioreq type 0x%x\n", req->type);
106893d43e7eSAnthony Xu     }
106993d43e7eSAnthony Xu     if (req->dir == IOREQ_READ) {
107093d43e7eSAnthony Xu         trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
107193d43e7eSAnthony Xu                                 req->addr, req->data, req->count, req->size);
107293d43e7eSAnthony Xu     }
107393d43e7eSAnthony Xu }
107493d43e7eSAnthony Xu 
10759288e803SJason Andryuk static bool handle_buffered_iopage(XenIOState *state)
107693d43e7eSAnthony Xu {
107793d43e7eSAnthony Xu     buffered_iopage_t *buf_page = state->buffered_io_page;
107893d43e7eSAnthony Xu     buf_ioreq_t *buf_req = NULL;
10799288e803SJason Andryuk     bool handled_ioreq = false;
108093d43e7eSAnthony Xu     ioreq_t req;
108193d43e7eSAnthony Xu     int qw;
108293d43e7eSAnthony Xu 
108393d43e7eSAnthony Xu     if (!buf_page) {
108493d43e7eSAnthony Xu         return 0;
108593d43e7eSAnthony Xu     }
108693d43e7eSAnthony Xu 
108793d43e7eSAnthony Xu     memset(&req, 0x00, sizeof(req));
108893d43e7eSAnthony Xu     req.state = STATE_IOREQ_READY;
108993d43e7eSAnthony Xu     req.count = 1;
109093d43e7eSAnthony Xu     req.dir = IOREQ_WRITE;
109193d43e7eSAnthony Xu 
109293d43e7eSAnthony Xu     for (;;) {
109393d43e7eSAnthony Xu         uint32_t rdptr = buf_page->read_pointer, wrptr;
109493d43e7eSAnthony Xu 
109593d43e7eSAnthony Xu         xen_rmb();
109693d43e7eSAnthony Xu         wrptr = buf_page->write_pointer;
109793d43e7eSAnthony Xu         xen_rmb();
109893d43e7eSAnthony Xu         if (rdptr != buf_page->read_pointer) {
109993d43e7eSAnthony Xu             continue;
110093d43e7eSAnthony Xu         }
110193d43e7eSAnthony Xu         if (rdptr == wrptr) {
110293d43e7eSAnthony Xu             break;
110393d43e7eSAnthony Xu         }
110493d43e7eSAnthony Xu         buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
110593d43e7eSAnthony Xu         req.size = 1U << buf_req->size;
110693d43e7eSAnthony Xu         req.addr = buf_req->addr;
110793d43e7eSAnthony Xu         req.data = buf_req->data;
110893d43e7eSAnthony Xu         req.type = buf_req->type;
110993d43e7eSAnthony Xu         xen_rmb();
111093d43e7eSAnthony Xu         qw = (req.size == 8);
111193d43e7eSAnthony Xu         if (qw) {
111293d43e7eSAnthony Xu             if (rdptr + 1 == wrptr) {
111393d43e7eSAnthony Xu                 hw_error("Incomplete quad word buffered ioreq");
111493d43e7eSAnthony Xu             }
111593d43e7eSAnthony Xu             buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
111693d43e7eSAnthony Xu                                            IOREQ_BUFFER_SLOT_NUM];
111793d43e7eSAnthony Xu             req.data |= ((uint64_t)buf_req->data) << 32;
111893d43e7eSAnthony Xu             xen_rmb();
111993d43e7eSAnthony Xu         }
112093d43e7eSAnthony Xu 
112193d43e7eSAnthony Xu         handle_ioreq(state, &req);
112293d43e7eSAnthony Xu 
112393d43e7eSAnthony Xu         /* Only req.data may get updated by handle_ioreq(), albeit even that
112493d43e7eSAnthony Xu          * should not happen as such data would never make it to the guest (we
112593d43e7eSAnthony Xu          * can only usefully see writes here after all).
112693d43e7eSAnthony Xu          */
112793d43e7eSAnthony Xu         assert(req.state == STATE_IOREQ_READY);
112893d43e7eSAnthony Xu         assert(req.count == 1);
112993d43e7eSAnthony Xu         assert(req.dir == IOREQ_WRITE);
113093d43e7eSAnthony Xu         assert(!req.data_is_ptr);
113193d43e7eSAnthony Xu 
1132d73415a3SStefan Hajnoczi         qatomic_add(&buf_page->read_pointer, qw + 1);
11339288e803SJason Andryuk         handled_ioreq = true;
113493d43e7eSAnthony Xu     }
113593d43e7eSAnthony Xu 
11369288e803SJason Andryuk     return handled_ioreq;
113793d43e7eSAnthony Xu }
113893d43e7eSAnthony Xu 
113993d43e7eSAnthony Xu static void handle_buffered_io(void *opaque)
114093d43e7eSAnthony Xu {
114193d43e7eSAnthony Xu     XenIOState *state = opaque;
114293d43e7eSAnthony Xu 
114393d43e7eSAnthony Xu     if (handle_buffered_iopage(state)) {
114493d43e7eSAnthony Xu         timer_mod(state->buffered_io_timer,
114593d43e7eSAnthony Xu                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
114693d43e7eSAnthony Xu     } else {
114793d43e7eSAnthony Xu         timer_del(state->buffered_io_timer);
1148b6cacfeaSDavid Woodhouse         qemu_xen_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
114993d43e7eSAnthony Xu     }
115093d43e7eSAnthony Xu }
115193d43e7eSAnthony Xu 
115293d43e7eSAnthony Xu static void cpu_handle_ioreq(void *opaque)
115393d43e7eSAnthony Xu {
115493d43e7eSAnthony Xu     XenIOState *state = opaque;
115593d43e7eSAnthony Xu     ioreq_t *req = cpu_get_ioreq(state);
115693d43e7eSAnthony Xu 
115793d43e7eSAnthony Xu     handle_buffered_iopage(state);
115893d43e7eSAnthony Xu     if (req) {
115993d43e7eSAnthony Xu         ioreq_t copy = *req;
116093d43e7eSAnthony Xu 
116193d43e7eSAnthony Xu         xen_rmb();
116293d43e7eSAnthony Xu         handle_ioreq(state, &copy);
116393d43e7eSAnthony Xu         req->data = copy.data;
116493d43e7eSAnthony Xu 
116593d43e7eSAnthony Xu         if (req->state != STATE_IOREQ_INPROCESS) {
116693d43e7eSAnthony Xu             fprintf(stderr, "Badness in I/O request ... not in service?!: "
116793d43e7eSAnthony Xu                     "%x, ptr: %x, port: %"PRIx64", "
116893d43e7eSAnthony Xu                     "data: %"PRIx64", count: %u, size: %u, type: %u\n",
116993d43e7eSAnthony Xu                     req->state, req->data_is_ptr, req->addr,
117093d43e7eSAnthony Xu                     req->data, req->count, req->size, req->type);
117193d43e7eSAnthony Xu             destroy_hvm_domain(false);
117293d43e7eSAnthony Xu             return;
117393d43e7eSAnthony Xu         }
117493d43e7eSAnthony Xu 
117593d43e7eSAnthony Xu         xen_wmb(); /* Update ioreq contents /then/ update state. */
117693d43e7eSAnthony Xu 
117793d43e7eSAnthony Xu         /*
117893d43e7eSAnthony Xu          * We do this before we send the response so that the tools
117993d43e7eSAnthony Xu          * have the opportunity to pick up on the reset before the
118093d43e7eSAnthony Xu          * guest resumes and does a hlt with interrupts disabled which
118193d43e7eSAnthony Xu          * causes Xen to powerdown the domain.
118293d43e7eSAnthony Xu          */
118393d43e7eSAnthony Xu         if (runstate_is_running()) {
1184aedbe192SEric Blake             ShutdownCause request;
1185aedbe192SEric Blake 
118693d43e7eSAnthony Xu             if (qemu_shutdown_requested_get()) {
118793d43e7eSAnthony Xu                 destroy_hvm_domain(false);
118893d43e7eSAnthony Xu             }
1189aedbe192SEric Blake             request = qemu_reset_requested_get();
1190aedbe192SEric Blake             if (request) {
1191aedbe192SEric Blake                 qemu_system_reset(request);
119293d43e7eSAnthony Xu                 destroy_hvm_domain(true);
119393d43e7eSAnthony Xu             }
119493d43e7eSAnthony Xu         }
119593d43e7eSAnthony Xu 
119693d43e7eSAnthony Xu         req->state = STATE_IORESP_READY;
1197b6cacfeaSDavid Woodhouse         qemu_xen_evtchn_notify(state->xce_handle,
119893d43e7eSAnthony Xu                                state->ioreq_local_port[state->send_vcpu]);
119993d43e7eSAnthony Xu     }
120093d43e7eSAnthony Xu }
120193d43e7eSAnthony Xu 
120293d43e7eSAnthony Xu static void xen_main_loop_prepare(XenIOState *state)
120393d43e7eSAnthony Xu {
120493d43e7eSAnthony Xu     int evtchn_fd = -1;
120593d43e7eSAnthony Xu 
120693d43e7eSAnthony Xu     if (state->xce_handle != NULL) {
1207b6cacfeaSDavid Woodhouse         evtchn_fd = qemu_xen_evtchn_fd(state->xce_handle);
120893d43e7eSAnthony Xu     }
120993d43e7eSAnthony Xu 
121093d43e7eSAnthony Xu     state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
121193d43e7eSAnthony Xu                                                  state);
121293d43e7eSAnthony Xu 
121393d43e7eSAnthony Xu     if (evtchn_fd != -1) {
121493d43e7eSAnthony Xu         CPUState *cpu_state;
121593d43e7eSAnthony Xu 
121693d43e7eSAnthony Xu         DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
121793d43e7eSAnthony Xu         CPU_FOREACH(cpu_state) {
121893d43e7eSAnthony Xu             DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
121993d43e7eSAnthony Xu                     __func__, cpu_state->cpu_index, cpu_state);
122093d43e7eSAnthony Xu             state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
122193d43e7eSAnthony Xu         }
122293d43e7eSAnthony Xu         qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
122393d43e7eSAnthony Xu     }
122493d43e7eSAnthony Xu }
122593d43e7eSAnthony Xu 
122693d43e7eSAnthony Xu 
1227538f0497SPhilippe Mathieu-Daudé static void xen_hvm_change_state_handler(void *opaque, bool running,
122893d43e7eSAnthony Xu                                          RunState rstate)
122993d43e7eSAnthony Xu {
123093d43e7eSAnthony Xu     XenIOState *state = opaque;
123193d43e7eSAnthony Xu 
123293d43e7eSAnthony Xu     if (running) {
123393d43e7eSAnthony Xu         xen_main_loop_prepare(state);
123493d43e7eSAnthony Xu     }
123593d43e7eSAnthony Xu 
123693d43e7eSAnthony Xu     xen_set_ioreq_server_state(xen_domid,
123793d43e7eSAnthony Xu                                state->ioservid,
123893d43e7eSAnthony Xu                                (rstate == RUN_STATE_RUNNING));
123993d43e7eSAnthony Xu }
124093d43e7eSAnthony Xu 
124193d43e7eSAnthony Xu static void xen_exit_notifier(Notifier *n, void *data)
124293d43e7eSAnthony Xu {
124393d43e7eSAnthony Xu     XenIOState *state = container_of(n, XenIOState, exit);
124493d43e7eSAnthony Xu 
1245ba7fdd64SIgor Druzhinin     xen_destroy_ioreq_server(xen_domid, state->ioservid);
1246f1e43b60SAnthony PERARD     if (state->fres != NULL) {
1247f1e43b60SAnthony PERARD         xenforeignmemory_unmap_resource(xen_fmem, state->fres);
1248f1e43b60SAnthony PERARD     }
1249ba7fdd64SIgor Druzhinin 
1250b6cacfeaSDavid Woodhouse     qemu_xen_evtchn_close(state->xce_handle);
125193d43e7eSAnthony Xu     xs_daemon_close(state->xenstore);
125293d43e7eSAnthony Xu }
125393d43e7eSAnthony Xu 
1254331b5189SIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
125593d43e7eSAnthony Xu static void xen_read_physmap(XenIOState *state)
125693d43e7eSAnthony Xu {
125793d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
125893d43e7eSAnthony Xu     unsigned int len, num, i;
125993d43e7eSAnthony Xu     char path[80], *value = NULL;
126093d43e7eSAnthony Xu     char **entries = NULL;
126193d43e7eSAnthony Xu 
126293d43e7eSAnthony Xu     snprintf(path, sizeof(path),
126393d43e7eSAnthony Xu             "/local/domain/0/device-model/%d/physmap", xen_domid);
126493d43e7eSAnthony Xu     entries = xs_directory(state->xenstore, 0, path, &num);
126593d43e7eSAnthony Xu     if (entries == NULL)
126693d43e7eSAnthony Xu         return;
126793d43e7eSAnthony Xu 
126893d43e7eSAnthony Xu     for (i = 0; i < num; i++) {
1269b21e2380SMarkus Armbruster         physmap = g_new(XenPhysmap, 1);
127093d43e7eSAnthony Xu         physmap->phys_offset = strtoull(entries[i], NULL, 16);
127193d43e7eSAnthony Xu         snprintf(path, sizeof(path),
127293d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
127393d43e7eSAnthony Xu                 xen_domid, entries[i]);
127493d43e7eSAnthony Xu         value = xs_read(state->xenstore, 0, path, &len);
127593d43e7eSAnthony Xu         if (value == NULL) {
127693d43e7eSAnthony Xu             g_free(physmap);
127793d43e7eSAnthony Xu             continue;
127893d43e7eSAnthony Xu         }
127993d43e7eSAnthony Xu         physmap->start_addr = strtoull(value, NULL, 16);
128093d43e7eSAnthony Xu         free(value);
128193d43e7eSAnthony Xu 
128293d43e7eSAnthony Xu         snprintf(path, sizeof(path),
128393d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/size",
128493d43e7eSAnthony Xu                 xen_domid, entries[i]);
128593d43e7eSAnthony Xu         value = xs_read(state->xenstore, 0, path, &len);
128693d43e7eSAnthony Xu         if (value == NULL) {
128793d43e7eSAnthony Xu             g_free(physmap);
128893d43e7eSAnthony Xu             continue;
128993d43e7eSAnthony Xu         }
129093d43e7eSAnthony Xu         physmap->size = strtoull(value, NULL, 16);
129193d43e7eSAnthony Xu         free(value);
129293d43e7eSAnthony Xu 
129393d43e7eSAnthony Xu         snprintf(path, sizeof(path),
129493d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/name",
129593d43e7eSAnthony Xu                 xen_domid, entries[i]);
129693d43e7eSAnthony Xu         physmap->name = xs_read(state->xenstore, 0, path, &len);
129793d43e7eSAnthony Xu 
129804a8f72eSIgor Druzhinin         QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
129993d43e7eSAnthony Xu     }
130093d43e7eSAnthony Xu     free(entries);
130193d43e7eSAnthony Xu }
1302331b5189SIgor Druzhinin #else
1303331b5189SIgor Druzhinin static void xen_read_physmap(XenIOState *state)
1304331b5189SIgor Druzhinin {
1305331b5189SIgor Druzhinin }
1306331b5189SIgor Druzhinin #endif
130793d43e7eSAnthony Xu 
130893d43e7eSAnthony Xu static void xen_wakeup_notifier(Notifier *notifier, void *data)
130993d43e7eSAnthony Xu {
131093d43e7eSAnthony Xu     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
131193d43e7eSAnthony Xu }
131293d43e7eSAnthony Xu 
131371cec1edSPaul Durrant static int xen_map_ioreq_server(XenIOState *state)
131471cec1edSPaul Durrant {
1315d3c49ebbSPaul Durrant     void *addr = NULL;
131671cec1edSPaul Durrant     xen_pfn_t ioreq_pfn;
131771cec1edSPaul Durrant     xen_pfn_t bufioreq_pfn;
131871cec1edSPaul Durrant     evtchn_port_t bufioreq_evtchn;
131971cec1edSPaul Durrant     int rc;
132071cec1edSPaul Durrant 
1321d3c49ebbSPaul Durrant     /*
1322d3c49ebbSPaul Durrant      * Attempt to map using the resource API and fall back to normal
1323d3c49ebbSPaul Durrant      * foreign mapping if this is not supported.
1324d3c49ebbSPaul Durrant      */
1325d3c49ebbSPaul Durrant     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq != 0);
1326d3c49ebbSPaul Durrant     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1327f1e43b60SAnthony PERARD     state->fres = xenforeignmemory_map_resource(xen_fmem, xen_domid,
1328d3c49ebbSPaul Durrant                                          XENMEM_resource_ioreq_server,
1329d3c49ebbSPaul Durrant                                          state->ioservid, 0, 2,
1330d3c49ebbSPaul Durrant                                          &addr,
1331d3c49ebbSPaul Durrant                                          PROT_READ | PROT_WRITE, 0);
1332f1e43b60SAnthony PERARD     if (state->fres != NULL) {
1333d3c49ebbSPaul Durrant         trace_xen_map_resource_ioreq(state->ioservid, addr);
1334d3c49ebbSPaul Durrant         state->buffered_io_page = addr;
1335d3c49ebbSPaul Durrant         state->shared_page = addr + TARGET_PAGE_SIZE;
1336d3c49ebbSPaul Durrant     } else if (errno != EOPNOTSUPP) {
1337d3c49ebbSPaul Durrant         error_report("failed to map ioreq server resources: error %d handle=%p",
1338d3c49ebbSPaul Durrant                      errno, xen_xc);
1339d3c49ebbSPaul Durrant         return -1;
1340d3c49ebbSPaul Durrant     }
1341d3c49ebbSPaul Durrant 
134271cec1edSPaul Durrant     rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1343d3c49ebbSPaul Durrant                                    (state->shared_page == NULL) ?
1344d3c49ebbSPaul Durrant                                    &ioreq_pfn : NULL,
1345d3c49ebbSPaul Durrant                                    (state->buffered_io_page == NULL) ?
1346d3c49ebbSPaul Durrant                                    &bufioreq_pfn : NULL,
134771cec1edSPaul Durrant                                    &bufioreq_evtchn);
134871cec1edSPaul Durrant     if (rc < 0) {
134971cec1edSPaul Durrant         error_report("failed to get ioreq server info: error %d handle=%p",
135071cec1edSPaul Durrant                      errno, xen_xc);
135171cec1edSPaul Durrant         return rc;
135271cec1edSPaul Durrant     }
135371cec1edSPaul Durrant 
1354d3c49ebbSPaul Durrant     if (state->shared_page == NULL) {
135571cec1edSPaul Durrant         DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
135671cec1edSPaul Durrant 
135771cec1edSPaul Durrant         state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
135871cec1edSPaul Durrant                                                   PROT_READ | PROT_WRITE,
135971cec1edSPaul Durrant                                                   1, &ioreq_pfn, NULL);
136071cec1edSPaul Durrant         if (state->shared_page == NULL) {
136171cec1edSPaul Durrant             error_report("map shared IO page returned error %d handle=%p",
136271cec1edSPaul Durrant                          errno, xen_xc);
136371cec1edSPaul Durrant         }
1364d3c49ebbSPaul Durrant     }
1365d3c49ebbSPaul Durrant 
1366d3c49ebbSPaul Durrant     if (state->buffered_io_page == NULL) {
1367d3c49ebbSPaul Durrant         DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
136871cec1edSPaul Durrant 
136971cec1edSPaul Durrant         state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
137071cec1edSPaul Durrant                                                        PROT_READ | PROT_WRITE,
1371d3c49ebbSPaul Durrant                                                        1, &bufioreq_pfn,
1372d3c49ebbSPaul Durrant                                                        NULL);
137371cec1edSPaul Durrant         if (state->buffered_io_page == NULL) {
137471cec1edSPaul Durrant             error_report("map buffered IO page returned error %d", errno);
137571cec1edSPaul Durrant             return -1;
137671cec1edSPaul Durrant         }
1377d3c49ebbSPaul Durrant     }
1378d3c49ebbSPaul Durrant 
1379d3c49ebbSPaul Durrant     if (state->shared_page == NULL || state->buffered_io_page == NULL) {
1380d3c49ebbSPaul Durrant         return -1;
1381d3c49ebbSPaul Durrant     }
1382d3c49ebbSPaul Durrant 
1383d3c49ebbSPaul Durrant     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
138471cec1edSPaul Durrant 
138571cec1edSPaul Durrant     state->bufioreq_remote_port = bufioreq_evtchn;
138671cec1edSPaul Durrant 
138771cec1edSPaul Durrant     return 0;
138871cec1edSPaul Durrant }
138971cec1edSPaul Durrant 
13905650ac00SPhilippe Mathieu-Daudé void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
139193d43e7eSAnthony Xu {
13920e11fc69SLike Xu     MachineState *ms = MACHINE(pcms);
13930e11fc69SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
139493d43e7eSAnthony Xu     int i, rc;
139593d43e7eSAnthony Xu     xen_pfn_t ioreq_pfn;
139693d43e7eSAnthony Xu     XenIOState *state;
139793d43e7eSAnthony Xu 
1398b6cacfeaSDavid Woodhouse     setup_xen_backend_ops();
1399b6cacfeaSDavid Woodhouse 
1400b21e2380SMarkus Armbruster     state = g_new0(XenIOState, 1);
140193d43e7eSAnthony Xu 
1402b6cacfeaSDavid Woodhouse     state->xce_handle = qemu_xen_evtchn_open();
140393d43e7eSAnthony Xu     if (state->xce_handle == NULL) {
140493d43e7eSAnthony Xu         perror("xen: event channel open");
140593d43e7eSAnthony Xu         goto err;
140693d43e7eSAnthony Xu     }
140793d43e7eSAnthony Xu 
140893d43e7eSAnthony Xu     state->xenstore = xs_daemon_open();
140993d43e7eSAnthony Xu     if (state->xenstore == NULL) {
141093d43e7eSAnthony Xu         perror("xen: xenstore open");
141193d43e7eSAnthony Xu         goto err;
141293d43e7eSAnthony Xu     }
141393d43e7eSAnthony Xu 
141493d43e7eSAnthony Xu     xen_create_ioreq_server(xen_domid, &state->ioservid);
141593d43e7eSAnthony Xu 
141693d43e7eSAnthony Xu     state->exit.notify = xen_exit_notifier;
141793d43e7eSAnthony Xu     qemu_add_exit_notifier(&state->exit);
141893d43e7eSAnthony Xu 
141946ea94caSDaniel Henrique Barboza     /*
142046ea94caSDaniel Henrique Barboza      * Register wake-up support in QMP query-current-machine API
142146ea94caSDaniel Henrique Barboza      */
142246ea94caSDaniel Henrique Barboza     qemu_register_wakeup_support();
142346ea94caSDaniel Henrique Barboza 
142471cec1edSPaul Durrant     rc = xen_map_ioreq_server(state);
142593d43e7eSAnthony Xu     if (rc < 0) {
142693d43e7eSAnthony Xu         goto err;
142793d43e7eSAnthony Xu     }
142893d43e7eSAnthony Xu 
142993d43e7eSAnthony Xu     /* Note: cpus is empty at this point in init */
1430b21e2380SMarkus Armbruster     state->cpu_by_vcpu_id = g_new0(CPUState *, max_cpus);
143193d43e7eSAnthony Xu 
143293d43e7eSAnthony Xu     rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
143393d43e7eSAnthony Xu     if (rc < 0) {
143493d43e7eSAnthony Xu         error_report("failed to enable ioreq server info: error %d handle=%p",
143593d43e7eSAnthony Xu                      errno, xen_xc);
143693d43e7eSAnthony Xu         goto err;
143793d43e7eSAnthony Xu     }
143893d43e7eSAnthony Xu 
1439b21e2380SMarkus Armbruster     state->ioreq_local_port = g_new0(evtchn_port_t, max_cpus);
144093d43e7eSAnthony Xu 
144193d43e7eSAnthony Xu     /* FIXME: how about if we overflow the page here? */
144293d43e7eSAnthony Xu     for (i = 0; i < max_cpus; i++) {
1443b6cacfeaSDavid Woodhouse         rc = qemu_xen_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1444b6cacfeaSDavid Woodhouse                                               xen_vcpu_eport(state->shared_page,
1445b6cacfeaSDavid Woodhouse                                                              i));
144693d43e7eSAnthony Xu         if (rc == -1) {
144793d43e7eSAnthony Xu             error_report("shared evtchn %d bind error %d", i, errno);
144893d43e7eSAnthony Xu             goto err;
144993d43e7eSAnthony Xu         }
145093d43e7eSAnthony Xu         state->ioreq_local_port[i] = rc;
145193d43e7eSAnthony Xu     }
145293d43e7eSAnthony Xu 
1453b6cacfeaSDavid Woodhouse     rc = qemu_xen_evtchn_bind_interdomain(state->xce_handle, xen_domid,
145471cec1edSPaul Durrant                                           state->bufioreq_remote_port);
145593d43e7eSAnthony Xu     if (rc == -1) {
145693d43e7eSAnthony Xu         error_report("buffered evtchn bind error %d", errno);
145793d43e7eSAnthony Xu         goto err;
145893d43e7eSAnthony Xu     }
145993d43e7eSAnthony Xu     state->bufioreq_local_port = rc;
146093d43e7eSAnthony Xu 
146193d43e7eSAnthony Xu     /* Init RAM management */
1462331b5189SIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
146393d43e7eSAnthony Xu     xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1464331b5189SIgor Druzhinin #else
1465331b5189SIgor Druzhinin     xen_map_cache_init(NULL, state);
1466331b5189SIgor Druzhinin #endif
146793d43e7eSAnthony Xu 
146893d43e7eSAnthony Xu     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
146993d43e7eSAnthony Xu 
147093d43e7eSAnthony Xu     state->memory_listener = xen_memory_listener;
147193d43e7eSAnthony Xu     memory_listener_register(&state->memory_listener, &address_space_memory);
147293d43e7eSAnthony Xu 
147393d43e7eSAnthony Xu     state->io_listener = xen_io_listener;
147493d43e7eSAnthony Xu     memory_listener_register(&state->io_listener, &address_space_io);
147593d43e7eSAnthony Xu 
147693d43e7eSAnthony Xu     state->device_listener = xen_device_listener;
1477dfb6578dSPaul Durrant     QLIST_INIT(&state->dev_list);
147893d43e7eSAnthony Xu     device_listener_register(&state->device_listener);
147993d43e7eSAnthony Xu 
1480108f7bbaSPaul Durrant     xen_bus_init();
148179807f3eSDavid Woodhouse     xen_be_init();
148204a8f72eSIgor Druzhinin 
148304a8f72eSIgor Druzhinin     QLIST_INIT(&xen_physmap);
148493d43e7eSAnthony Xu     xen_read_physmap(state);
148593d43e7eSAnthony Xu 
1486*9269b9d1SStefano Stabellini     suspend.notify = xen_suspend_notifier;
1487*9269b9d1SStefano Stabellini     qemu_register_suspend_notifier(&suspend);
148833087aacSVikram Garhwal 
1489*9269b9d1SStefano Stabellini     wakeup.notify = xen_wakeup_notifier;
1490*9269b9d1SStefano Stabellini     qemu_register_wakeup_notifier(&wakeup);
149133087aacSVikram Garhwal 
149233087aacSVikram Garhwal     rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
149333087aacSVikram Garhwal     if (!rc) {
149433087aacSVikram Garhwal         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1495*9269b9d1SStefano Stabellini         shared_vmport_page =
149633087aacSVikram Garhwal             xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
149733087aacSVikram Garhwal                                  1, &ioreq_pfn, NULL);
1498*9269b9d1SStefano Stabellini         if (shared_vmport_page == NULL) {
149933087aacSVikram Garhwal             error_report("map shared vmport IO page returned error %d handle=%p",
150033087aacSVikram Garhwal                          errno, xen_xc);
150133087aacSVikram Garhwal             goto err;
150233087aacSVikram Garhwal         }
150333087aacSVikram Garhwal     } else if (rc != -ENOSYS) {
150433087aacSVikram Garhwal         error_report("get vmport regs pfn returned error %d, rc=%d",
150533087aacSVikram Garhwal                      errno, rc);
150633087aacSVikram Garhwal         goto err;
150733087aacSVikram Garhwal     }
150833087aacSVikram Garhwal 
150933087aacSVikram Garhwal     xen_ram_init(pcms, ms->ram_size, ram_memory);
151033087aacSVikram Garhwal 
151193d43e7eSAnthony Xu     /* Disable ACPI build because Xen handles it */
151293d43e7eSAnthony Xu     pcms->acpi_build_enabled = false;
151393d43e7eSAnthony Xu 
151493d43e7eSAnthony Xu     return;
151593d43e7eSAnthony Xu 
151693d43e7eSAnthony Xu err:
151793d43e7eSAnthony Xu     error_report("xen hardware virtual machine initialisation failed");
151893d43e7eSAnthony Xu     exit(1);
151993d43e7eSAnthony Xu }
152093d43e7eSAnthony Xu 
152193d43e7eSAnthony Xu void destroy_hvm_domain(bool reboot)
152293d43e7eSAnthony Xu {
152393d43e7eSAnthony Xu     xc_interface *xc_handle;
152493d43e7eSAnthony Xu     int sts;
15256b47c2aaSIan Jackson     int rc;
152693d43e7eSAnthony Xu 
152774aae6bfSIan Jackson     unsigned int reason = reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff;
152874aae6bfSIan Jackson 
15296b47c2aaSIan Jackson     if (xen_dmod) {
15306b47c2aaSIan Jackson         rc = xendevicemodel_shutdown(xen_dmod, xen_domid, reason);
15316b47c2aaSIan Jackson         if (!rc) {
15326b47c2aaSIan Jackson             return;
15336b47c2aaSIan Jackson         }
15346b47c2aaSIan Jackson         if (errno != ENOTTY /* old Xen */) {
15356b47c2aaSIan Jackson             perror("xendevicemodel_shutdown failed");
15366b47c2aaSIan Jackson         }
15376b47c2aaSIan Jackson         /* well, try the old thing then */
15386b47c2aaSIan Jackson     }
15396b47c2aaSIan Jackson 
154093d43e7eSAnthony Xu     xc_handle = xc_interface_open(0, 0, 0);
154193d43e7eSAnthony Xu     if (xc_handle == NULL) {
154293d43e7eSAnthony Xu         fprintf(stderr, "Cannot acquire xenctrl handle\n");
154393d43e7eSAnthony Xu     } else {
154474aae6bfSIan Jackson         sts = xc_domain_shutdown(xc_handle, xen_domid, reason);
154593d43e7eSAnthony Xu         if (sts != 0) {
154693d43e7eSAnthony Xu             fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
154793d43e7eSAnthony Xu                     "sts %d, %s\n", reboot ? "reboot" : "poweroff",
154893d43e7eSAnthony Xu                     sts, strerror(errno));
154993d43e7eSAnthony Xu         } else {
155093d43e7eSAnthony Xu             fprintf(stderr, "Issued domain %d %s\n", xen_domid,
155193d43e7eSAnthony Xu                     reboot ? "reboot" : "poweroff");
155293d43e7eSAnthony Xu         }
155393d43e7eSAnthony Xu         xc_interface_close(xc_handle);
155493d43e7eSAnthony Xu     }
155593d43e7eSAnthony Xu }
155693d43e7eSAnthony Xu 
155793d43e7eSAnthony Xu void xen_register_framebuffer(MemoryRegion *mr)
155893d43e7eSAnthony Xu {
155993d43e7eSAnthony Xu     framebuffer = mr;
156093d43e7eSAnthony Xu }
156193d43e7eSAnthony Xu 
156293d43e7eSAnthony Xu void xen_shutdown_fatal_error(const char *fmt, ...)
156393d43e7eSAnthony Xu {
156493d43e7eSAnthony Xu     va_list ap;
156593d43e7eSAnthony Xu 
156693d43e7eSAnthony Xu     va_start(ap, fmt);
156793d43e7eSAnthony Xu     vfprintf(stderr, fmt, ap);
156893d43e7eSAnthony Xu     va_end(ap);
156993d43e7eSAnthony Xu     fprintf(stderr, "Will destroy the domain.\n");
157093d43e7eSAnthony Xu     /* destroy the domain */
1571cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
157293d43e7eSAnthony Xu }
157393d43e7eSAnthony Xu 
157493d43e7eSAnthony Xu void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
157593d43e7eSAnthony Xu {
157693d43e7eSAnthony Xu     if (unlikely(xen_in_migration)) {
157793d43e7eSAnthony Xu         int rc;
157893d43e7eSAnthony Xu         ram_addr_t start_pfn, nb_pages;
157993d43e7eSAnthony Xu 
158004a8f72eSIgor Druzhinin         start = xen_phys_offset_to_gaddr(start, length);
158104a8f72eSIgor Druzhinin 
158293d43e7eSAnthony Xu         if (length == 0) {
158393d43e7eSAnthony Xu             length = TARGET_PAGE_SIZE;
158493d43e7eSAnthony Xu         }
158593d43e7eSAnthony Xu         start_pfn = start >> TARGET_PAGE_BITS;
158693d43e7eSAnthony Xu         nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
158793d43e7eSAnthony Xu             - start_pfn;
158893d43e7eSAnthony Xu         rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
158993d43e7eSAnthony Xu         if (rc) {
159093d43e7eSAnthony Xu             fprintf(stderr,
159193d43e7eSAnthony Xu                     "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
15927cdcca72SRoss Lagerwall                     __func__, start, nb_pages, errno, strerror(errno));
159393d43e7eSAnthony Xu         }
159493d43e7eSAnthony Xu     }
159593d43e7eSAnthony Xu }
159693d43e7eSAnthony Xu 
159793d43e7eSAnthony Xu void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
159893d43e7eSAnthony Xu {
159993d43e7eSAnthony Xu     if (enable) {
160063b41db4SHyman Huang(黄勇)         memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION);
160193d43e7eSAnthony Xu     } else {
160263b41db4SHyman Huang(黄勇)         memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION);
160393d43e7eSAnthony Xu     }
160493d43e7eSAnthony Xu }
1605