xref: /openbmc/qemu/hw/i386/xen/xen-hvm.c (revision 21d87050af66f50c8d394a160e89560ceaa6bd5d)
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"
184b19de14SPhilippe Mathieu-Daudé #include "hw/southbridge/piix.h"
1964552b6bSMarkus Armbruster #include "hw/irq.h"
20650d103dSMarkus Armbruster #include "hw/hw.h"
2193d43e7eSAnthony Xu #include "hw/i386/apic-msidef.h"
2293d43e7eSAnthony Xu #include "hw/xen/xen_common.h"
232d0ed5e6SPaul Durrant #include "hw/xen/xen-legacy-backend.h"
24108f7bbaSPaul Durrant #include "hw/xen/xen-bus.h"
25c834596fSPhilippe Mathieu-Daudé #include "hw/xen/xen-x86.h"
26e688df6bSMarkus Armbruster #include "qapi/error.h"
2728af9ba2SPhilippe Mathieu-Daudé #include "qapi/qapi-commands-migration.h"
2893d43e7eSAnthony Xu #include "qemu/error-report.h"
29db725815SMarkus Armbruster #include "qemu/main-loop.h"
3093d43e7eSAnthony Xu #include "qemu/range.h"
3154d31236SMarkus Armbruster #include "sysemu/runstate.h"
3246517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
33da278d58SPhilippe Mathieu-Daudé #include "sysemu/xen.h"
3493d43e7eSAnthony Xu #include "sysemu/xen-mapcache.h"
3593d43e7eSAnthony Xu #include "trace.h"
3693d43e7eSAnthony Xu 
3793d43e7eSAnthony Xu #include <xen/hvm/ioreq.h>
3893d43e7eSAnthony Xu #include <xen/hvm/e820.h>
3993d43e7eSAnthony Xu 
4093d43e7eSAnthony Xu //#define DEBUG_XEN_HVM
4193d43e7eSAnthony Xu 
4293d43e7eSAnthony Xu #ifdef DEBUG_XEN_HVM
4393d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \
4493d43e7eSAnthony Xu     do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
4593d43e7eSAnthony Xu #else
4693d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \
4793d43e7eSAnthony Xu     do { } while (0)
4893d43e7eSAnthony Xu #endif
4993d43e7eSAnthony Xu 
5093d43e7eSAnthony Xu static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
5193d43e7eSAnthony Xu static MemoryRegion *framebuffer;
5293d43e7eSAnthony Xu static bool xen_in_migration;
5393d43e7eSAnthony Xu 
5493d43e7eSAnthony Xu /* Compatibility with older version */
5593d43e7eSAnthony Xu 
5693d43e7eSAnthony Xu /* This allows QEMU to build on a system that has Xen 4.5 or earlier
5793d43e7eSAnthony Xu  * installed.  This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
5893d43e7eSAnthony Xu  * needs to be included before this block and hw/xen/xen_common.h needs to
5993d43e7eSAnthony Xu  * be included 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
7793d43e7eSAnthony Xu 
7893d43e7eSAnthony Xu static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
7993d43e7eSAnthony Xu {
8093d43e7eSAnthony Xu     return shared_page->vcpu_ioreq[i].vp_eport;
8193d43e7eSAnthony Xu }
8293d43e7eSAnthony Xu static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
8393d43e7eSAnthony Xu {
8493d43e7eSAnthony Xu     return &shared_page->vcpu_ioreq[vcpu];
8593d43e7eSAnthony Xu }
8693d43e7eSAnthony Xu 
8793d43e7eSAnthony Xu #define BUFFER_IO_MAX_DELAY  100
8893d43e7eSAnthony Xu 
8993d43e7eSAnthony Xu typedef struct XenPhysmap {
9093d43e7eSAnthony Xu     hwaddr start_addr;
9193d43e7eSAnthony Xu     ram_addr_t size;
9293d43e7eSAnthony Xu     const char *name;
9393d43e7eSAnthony Xu     hwaddr phys_offset;
9493d43e7eSAnthony Xu 
9593d43e7eSAnthony Xu     QLIST_ENTRY(XenPhysmap) list;
9693d43e7eSAnthony Xu } XenPhysmap;
9793d43e7eSAnthony Xu 
9804a8f72eSIgor Druzhinin static QLIST_HEAD(, XenPhysmap) xen_physmap;
9904a8f72eSIgor Druzhinin 
100dfb6578dSPaul Durrant typedef struct XenPciDevice {
101dfb6578dSPaul Durrant     PCIDevice *pci_dev;
102dfb6578dSPaul Durrant     uint32_t sbdf;
103dfb6578dSPaul Durrant     QLIST_ENTRY(XenPciDevice) entry;
104dfb6578dSPaul Durrant } XenPciDevice;
105dfb6578dSPaul Durrant 
10693d43e7eSAnthony Xu typedef struct XenIOState {
10793d43e7eSAnthony Xu     ioservid_t ioservid;
10893d43e7eSAnthony Xu     shared_iopage_t *shared_page;
10993d43e7eSAnthony Xu     shared_vmport_iopage_t *shared_vmport_page;
11093d43e7eSAnthony Xu     buffered_iopage_t *buffered_io_page;
111f1e43b60SAnthony PERARD     xenforeignmemory_resource_handle *fres;
11293d43e7eSAnthony Xu     QEMUTimer *buffered_io_timer;
11393d43e7eSAnthony Xu     CPUState **cpu_by_vcpu_id;
11493d43e7eSAnthony Xu     /* the evtchn port for polling the notification, */
11593d43e7eSAnthony Xu     evtchn_port_t *ioreq_local_port;
11671cec1edSPaul Durrant     /* evtchn remote and local ports for buffered io */
11771cec1edSPaul Durrant     evtchn_port_t bufioreq_remote_port;
11893d43e7eSAnthony Xu     evtchn_port_t bufioreq_local_port;
11993d43e7eSAnthony Xu     /* the evtchn fd for polling */
12093d43e7eSAnthony Xu     xenevtchn_handle *xce_handle;
12193d43e7eSAnthony Xu     /* which vcpu we are serving */
12293d43e7eSAnthony Xu     int send_vcpu;
12393d43e7eSAnthony Xu 
12493d43e7eSAnthony Xu     struct xs_handle *xenstore;
12593d43e7eSAnthony Xu     MemoryListener memory_listener;
12693d43e7eSAnthony Xu     MemoryListener io_listener;
127dfb6578dSPaul Durrant     QLIST_HEAD(, XenPciDevice) dev_list;
12893d43e7eSAnthony Xu     DeviceListener device_listener;
12993d43e7eSAnthony Xu     hwaddr free_phys_offset;
13093d43e7eSAnthony Xu     const XenPhysmap *log_for_dirtybit;
13134fbbc16SAnthony PERARD     /* Buffer used by xen_sync_dirty_bitmap */
13234fbbc16SAnthony PERARD     unsigned long *dirty_bitmap;
13393d43e7eSAnthony Xu 
13493d43e7eSAnthony Xu     Notifier exit;
13593d43e7eSAnthony Xu     Notifier suspend;
13693d43e7eSAnthony Xu     Notifier wakeup;
13793d43e7eSAnthony Xu } XenIOState;
13893d43e7eSAnthony Xu 
13993d43e7eSAnthony Xu /* Xen specific function for piix pci */
14093d43e7eSAnthony Xu 
14193d43e7eSAnthony Xu int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
14293d43e7eSAnthony Xu {
1438d40def6SPhilippe Mathieu-Daudé     return irq_num + (PCI_SLOT(pci_dev->devfn) << 2);
14493d43e7eSAnthony Xu }
14593d43e7eSAnthony Xu 
14693d43e7eSAnthony Xu void xen_piix3_set_irq(void *opaque, int irq_num, int level)
14793d43e7eSAnthony Xu {
14893d43e7eSAnthony Xu     xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
14993d43e7eSAnthony Xu                            irq_num & 3, level);
15093d43e7eSAnthony Xu }
15193d43e7eSAnthony Xu 
15293d43e7eSAnthony Xu void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
15393d43e7eSAnthony Xu {
15493d43e7eSAnthony Xu     int i;
15593d43e7eSAnthony Xu 
15693d43e7eSAnthony Xu     /* Scan for updates to PCI link routes (0x60-0x63). */
15793d43e7eSAnthony Xu     for (i = 0; i < len; i++) {
15893d43e7eSAnthony Xu         uint8_t v = (val >> (8 * i)) & 0xff;
15993d43e7eSAnthony Xu         if (v & 0x80) {
16093d43e7eSAnthony Xu             v = 0;
16193d43e7eSAnthony Xu         }
16293d43e7eSAnthony Xu         v &= 0xf;
1634b19de14SPhilippe Mathieu-Daudé         if (((address + i) >= PIIX_PIRQCA) && ((address + i) <= PIIX_PIRQCD)) {
164*21d87050SBernhard Beschow             xen_set_pci_link_route(address + i - PIIX_PIRQCA, v);
16593d43e7eSAnthony Xu         }
16693d43e7eSAnthony Xu     }
16793d43e7eSAnthony Xu }
16893d43e7eSAnthony Xu 
169*21d87050SBernhard Beschow int xen_set_pci_link_route(uint8_t link, uint8_t irq)
170*21d87050SBernhard Beschow {
171*21d87050SBernhard Beschow     return xendevicemodel_set_pci_link_route(xen_dmod, xen_domid, link, irq);
172*21d87050SBernhard Beschow }
173*21d87050SBernhard Beschow 
17493d43e7eSAnthony Xu int xen_is_pirq_msi(uint32_t msi_data)
17593d43e7eSAnthony Xu {
17693d43e7eSAnthony Xu     /* If vector is 0, the msi is remapped into a pirq, passed as
17793d43e7eSAnthony Xu      * dest_id.
17893d43e7eSAnthony Xu      */
17993d43e7eSAnthony Xu     return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
18093d43e7eSAnthony Xu }
18193d43e7eSAnthony Xu 
18293d43e7eSAnthony Xu void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
18393d43e7eSAnthony Xu {
18493d43e7eSAnthony Xu     xen_inject_msi(xen_domid, addr, data);
18593d43e7eSAnthony Xu }
18693d43e7eSAnthony Xu 
18793d43e7eSAnthony Xu static void xen_suspend_notifier(Notifier *notifier, void *data)
18893d43e7eSAnthony Xu {
18993d43e7eSAnthony Xu     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
19093d43e7eSAnthony Xu }
19193d43e7eSAnthony Xu 
19293d43e7eSAnthony Xu /* Xen Interrupt Controller */
19393d43e7eSAnthony Xu 
19493d43e7eSAnthony Xu static void xen_set_irq(void *opaque, int irq, int level)
19593d43e7eSAnthony Xu {
19693d43e7eSAnthony Xu     xen_set_isa_irq_level(xen_domid, irq, level);
19793d43e7eSAnthony Xu }
19893d43e7eSAnthony Xu 
19993d43e7eSAnthony Xu qemu_irq *xen_interrupt_controller_init(void)
20093d43e7eSAnthony Xu {
20193d43e7eSAnthony Xu     return qemu_allocate_irqs(xen_set_irq, NULL, 16);
20293d43e7eSAnthony Xu }
20393d43e7eSAnthony Xu 
20493d43e7eSAnthony Xu /* Memory Ops */
20593d43e7eSAnthony Xu 
20693d43e7eSAnthony Xu static void xen_ram_init(PCMachineState *pcms,
20793d43e7eSAnthony Xu                          ram_addr_t ram_size, MemoryRegion **ram_memory_p)
20893d43e7eSAnthony Xu {
209f0bb276bSPaolo Bonzini     X86MachineState *x86ms = X86_MACHINE(pcms);
21093d43e7eSAnthony Xu     MemoryRegion *sysmem = get_system_memory();
21193d43e7eSAnthony Xu     ram_addr_t block_len;
212f0bb276bSPaolo Bonzini     uint64_t user_lowmem =
213f0bb276bSPaolo Bonzini         object_property_get_uint(qdev_get_machine(),
2149a45729dSGerd Hoffmann                                  PC_MACHINE_MAX_RAM_BELOW_4G,
21593d43e7eSAnthony Xu                                  &error_abort);
21693d43e7eSAnthony Xu 
21793d43e7eSAnthony Xu     /* Handle the machine opt max-ram-below-4g.  It is basically doing
21893d43e7eSAnthony Xu      * min(xen limit, user limit).
21993d43e7eSAnthony Xu      */
22093d43e7eSAnthony Xu     if (!user_lowmem) {
22193d43e7eSAnthony Xu         user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
22293d43e7eSAnthony Xu     }
22393d43e7eSAnthony Xu     if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
22493d43e7eSAnthony Xu         user_lowmem = HVM_BELOW_4G_RAM_END;
22593d43e7eSAnthony Xu     }
22693d43e7eSAnthony Xu 
22793d43e7eSAnthony Xu     if (ram_size >= user_lowmem) {
228f0bb276bSPaolo Bonzini         x86ms->above_4g_mem_size = ram_size - user_lowmem;
229f0bb276bSPaolo Bonzini         x86ms->below_4g_mem_size = user_lowmem;
23093d43e7eSAnthony Xu     } else {
231f0bb276bSPaolo Bonzini         x86ms->above_4g_mem_size = 0;
232f0bb276bSPaolo Bonzini         x86ms->below_4g_mem_size = ram_size;
23393d43e7eSAnthony Xu     }
234f0bb276bSPaolo Bonzini     if (!x86ms->above_4g_mem_size) {
23593d43e7eSAnthony Xu         block_len = ram_size;
23693d43e7eSAnthony Xu     } else {
23793d43e7eSAnthony Xu         /*
23893d43e7eSAnthony Xu          * Xen does not allocate the memory continuously, it keeps a
23993d43e7eSAnthony Xu          * hole of the size computed above or passed in.
24093d43e7eSAnthony Xu          */
241039a93b0SPhilippe Mathieu-Daudé         block_len = (4 * GiB) + x86ms->above_4g_mem_size;
24293d43e7eSAnthony Xu     }
24398a99ce0SPeter Maydell     memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
24493d43e7eSAnthony Xu                            &error_fatal);
24593d43e7eSAnthony Xu     *ram_memory_p = &ram_memory;
24693d43e7eSAnthony Xu 
24793d43e7eSAnthony Xu     memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
24893d43e7eSAnthony Xu                              &ram_memory, 0, 0xa0000);
24993d43e7eSAnthony Xu     memory_region_add_subregion(sysmem, 0, &ram_640k);
25093d43e7eSAnthony Xu     /* Skip of the VGA IO memory space, it will be registered later by the VGA
25193d43e7eSAnthony Xu      * emulated device.
25293d43e7eSAnthony Xu      *
25393d43e7eSAnthony Xu      * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
25493d43e7eSAnthony Xu      * the Options ROM, so it is registered here as RAM.
25593d43e7eSAnthony Xu      */
25693d43e7eSAnthony Xu     memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
25793d43e7eSAnthony Xu                              &ram_memory, 0xc0000,
258f0bb276bSPaolo Bonzini                              x86ms->below_4g_mem_size - 0xc0000);
25993d43e7eSAnthony Xu     memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
260f0bb276bSPaolo Bonzini     if (x86ms->above_4g_mem_size > 0) {
26193d43e7eSAnthony Xu         memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
26293d43e7eSAnthony Xu                                  &ram_memory, 0x100000000ULL,
263f0bb276bSPaolo Bonzini                                  x86ms->above_4g_mem_size);
26493d43e7eSAnthony Xu         memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
26593d43e7eSAnthony Xu     }
26693d43e7eSAnthony Xu }
26793d43e7eSAnthony Xu 
26893d43e7eSAnthony Xu void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
26993d43e7eSAnthony Xu                    Error **errp)
27093d43e7eSAnthony Xu {
27193d43e7eSAnthony Xu     unsigned long nr_pfn;
27293d43e7eSAnthony Xu     xen_pfn_t *pfn_list;
27393d43e7eSAnthony Xu     int i;
27493d43e7eSAnthony Xu 
27593d43e7eSAnthony Xu     if (runstate_check(RUN_STATE_INMIGRATE)) {
27693d43e7eSAnthony Xu         /* RAM already populated in Xen */
27793d43e7eSAnthony Xu         fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
27893d43e7eSAnthony Xu                 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
27993d43e7eSAnthony Xu                 __func__, size, ram_addr);
28093d43e7eSAnthony Xu         return;
28193d43e7eSAnthony Xu     }
28293d43e7eSAnthony Xu 
28393d43e7eSAnthony Xu     if (mr == &ram_memory) {
28493d43e7eSAnthony Xu         return;
28593d43e7eSAnthony Xu     }
28693d43e7eSAnthony Xu 
28793d43e7eSAnthony Xu     trace_xen_ram_alloc(ram_addr, size);
28893d43e7eSAnthony Xu 
28993d43e7eSAnthony Xu     nr_pfn = size >> TARGET_PAGE_BITS;
29093d43e7eSAnthony Xu     pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
29193d43e7eSAnthony Xu 
29293d43e7eSAnthony Xu     for (i = 0; i < nr_pfn; i++) {
29393d43e7eSAnthony Xu         pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
29493d43e7eSAnthony Xu     }
29593d43e7eSAnthony Xu 
29693d43e7eSAnthony Xu     if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
29793d43e7eSAnthony Xu         error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
29893d43e7eSAnthony Xu                    ram_addr);
29993d43e7eSAnthony Xu     }
30093d43e7eSAnthony Xu 
30193d43e7eSAnthony Xu     g_free(pfn_list);
30293d43e7eSAnthony Xu }
30393d43e7eSAnthony Xu 
30404a8f72eSIgor Druzhinin static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size)
30593d43e7eSAnthony Xu {
30693d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
30793d43e7eSAnthony Xu 
30893d43e7eSAnthony Xu     start_addr &= TARGET_PAGE_MASK;
30993d43e7eSAnthony Xu 
31004a8f72eSIgor Druzhinin     QLIST_FOREACH(physmap, &xen_physmap, list) {
31193d43e7eSAnthony Xu         if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
31293d43e7eSAnthony Xu             return physmap;
31393d43e7eSAnthony Xu         }
31493d43e7eSAnthony Xu     }
31593d43e7eSAnthony Xu     return NULL;
31693d43e7eSAnthony Xu }
31793d43e7eSAnthony Xu 
31804a8f72eSIgor Druzhinin static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size)
31993d43e7eSAnthony Xu {
32004a8f72eSIgor Druzhinin     hwaddr addr = phys_offset & TARGET_PAGE_MASK;
32193d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
32293d43e7eSAnthony Xu 
32304a8f72eSIgor Druzhinin     QLIST_FOREACH(physmap, &xen_physmap, list) {
32493d43e7eSAnthony Xu         if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
32504a8f72eSIgor Druzhinin             return physmap->start_addr + (phys_offset - physmap->phys_offset);
32693d43e7eSAnthony Xu         }
32793d43e7eSAnthony Xu     }
32893d43e7eSAnthony Xu 
32904a8f72eSIgor Druzhinin     return phys_offset;
33093d43e7eSAnthony Xu }
33193d43e7eSAnthony Xu 
33204a8f72eSIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
333697b66d0SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
334697b66d0SIgor Druzhinin {
335697b66d0SIgor Druzhinin     char path[80], value[17];
336697b66d0SIgor Druzhinin 
337697b66d0SIgor Druzhinin     snprintf(path, sizeof(path),
338697b66d0SIgor Druzhinin             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
339697b66d0SIgor Druzhinin             xen_domid, (uint64_t)physmap->phys_offset);
340697b66d0SIgor Druzhinin     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
341697b66d0SIgor Druzhinin     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
342697b66d0SIgor Druzhinin         return -1;
343697b66d0SIgor Druzhinin     }
344697b66d0SIgor Druzhinin     snprintf(path, sizeof(path),
345697b66d0SIgor Druzhinin             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
346697b66d0SIgor Druzhinin             xen_domid, (uint64_t)physmap->phys_offset);
347697b66d0SIgor Druzhinin     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
348697b66d0SIgor Druzhinin     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
349697b66d0SIgor Druzhinin         return -1;
350697b66d0SIgor Druzhinin     }
351697b66d0SIgor Druzhinin     if (physmap->name) {
352697b66d0SIgor Druzhinin         snprintf(path, sizeof(path),
353697b66d0SIgor Druzhinin                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
354697b66d0SIgor Druzhinin                 xen_domid, (uint64_t)physmap->phys_offset);
355697b66d0SIgor Druzhinin         if (!xs_write(state->xenstore, 0, path,
356697b66d0SIgor Druzhinin                       physmap->name, strlen(physmap->name))) {
357697b66d0SIgor Druzhinin             return -1;
358697b66d0SIgor Druzhinin         }
359697b66d0SIgor Druzhinin     }
360697b66d0SIgor Druzhinin     return 0;
361697b66d0SIgor Druzhinin }
362331b5189SIgor Druzhinin #else
363331b5189SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
364331b5189SIgor Druzhinin {
365331b5189SIgor Druzhinin     return 0;
366331b5189SIgor Druzhinin }
367331b5189SIgor Druzhinin #endif
368697b66d0SIgor Druzhinin 
36993d43e7eSAnthony Xu static int xen_add_to_physmap(XenIOState *state,
37093d43e7eSAnthony Xu                               hwaddr start_addr,
37193d43e7eSAnthony Xu                               ram_addr_t size,
37293d43e7eSAnthony Xu                               MemoryRegion *mr,
37393d43e7eSAnthony Xu                               hwaddr offset_within_region)
37493d43e7eSAnthony Xu {
3752cbf8903SRoss Lagerwall     unsigned long nr_pages;
37693d43e7eSAnthony Xu     int rc = 0;
37793d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
37893d43e7eSAnthony Xu     hwaddr pfn, start_gpfn;
37993d43e7eSAnthony Xu     hwaddr phys_offset = memory_region_get_ram_addr(mr);
38093d43e7eSAnthony Xu     const char *mr_name;
38193d43e7eSAnthony Xu 
38204a8f72eSIgor Druzhinin     if (get_physmapping(start_addr, size)) {
38393d43e7eSAnthony Xu         return 0;
38493d43e7eSAnthony Xu     }
38593d43e7eSAnthony Xu     if (size <= 0) {
38693d43e7eSAnthony Xu         return -1;
38793d43e7eSAnthony Xu     }
38893d43e7eSAnthony Xu 
38993d43e7eSAnthony Xu     /* Xen can only handle a single dirty log region for now and we want
39093d43e7eSAnthony Xu      * the linear framebuffer to be that region.
39193d43e7eSAnthony Xu      * Avoid tracking any regions that is not videoram and avoid tracking
39293d43e7eSAnthony Xu      * the legacy vga region. */
39393d43e7eSAnthony Xu     if (mr == framebuffer && start_addr > 0xbffff) {
39493d43e7eSAnthony Xu         goto go_physmap;
39593d43e7eSAnthony Xu     }
39693d43e7eSAnthony Xu     return -1;
39793d43e7eSAnthony Xu 
39893d43e7eSAnthony Xu go_physmap:
39993d43e7eSAnthony Xu     DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
40093d43e7eSAnthony Xu             start_addr, start_addr + size);
40193d43e7eSAnthony Xu 
402331b5189SIgor Druzhinin     mr_name = memory_region_name(mr);
403331b5189SIgor Druzhinin 
404b21e2380SMarkus Armbruster     physmap = g_new(XenPhysmap, 1);
405331b5189SIgor Druzhinin 
406331b5189SIgor Druzhinin     physmap->start_addr = start_addr;
407331b5189SIgor Druzhinin     physmap->size = size;
408331b5189SIgor Druzhinin     physmap->name = mr_name;
409331b5189SIgor Druzhinin     physmap->phys_offset = phys_offset;
410331b5189SIgor Druzhinin 
41104a8f72eSIgor Druzhinin     QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
412331b5189SIgor Druzhinin 
413331b5189SIgor Druzhinin     if (runstate_check(RUN_STATE_INMIGRATE)) {
414331b5189SIgor Druzhinin         /* Now when we have a physmap entry we can replace a dummy mapping with
415331b5189SIgor Druzhinin          * a real one of guest foreign memory. */
416331b5189SIgor Druzhinin         uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
417331b5189SIgor Druzhinin         assert(p && p == memory_region_get_ram_ptr(mr));
418331b5189SIgor Druzhinin 
419331b5189SIgor Druzhinin         return 0;
420331b5189SIgor Druzhinin     }
421331b5189SIgor Druzhinin 
42293d43e7eSAnthony Xu     pfn = phys_offset >> TARGET_PAGE_BITS;
42393d43e7eSAnthony Xu     start_gpfn = start_addr >> TARGET_PAGE_BITS;
4242cbf8903SRoss Lagerwall     nr_pages = size >> TARGET_PAGE_BITS;
4252cbf8903SRoss Lagerwall     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
4262cbf8903SRoss Lagerwall                                         start_gpfn);
42793d43e7eSAnthony Xu     if (rc) {
4282cbf8903SRoss Lagerwall         int saved_errno = errno;
4292cbf8903SRoss Lagerwall 
4302cbf8903SRoss Lagerwall         error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
4312cbf8903SRoss Lagerwall                      " to GFN %"HWADDR_PRIx" failed: %s",
4322cbf8903SRoss Lagerwall                      nr_pages, pfn, start_gpfn, strerror(saved_errno));
4332cbf8903SRoss Lagerwall         errno = saved_errno;
4342cbf8903SRoss Lagerwall         return -1;
43593d43e7eSAnthony Xu     }
43693d43e7eSAnthony Xu 
4372cbf8903SRoss Lagerwall     rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
43893d43e7eSAnthony Xu                                    start_addr >> TARGET_PAGE_BITS,
43993d43e7eSAnthony Xu                                    (start_addr + size - 1) >> TARGET_PAGE_BITS,
44093d43e7eSAnthony Xu                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
4412cbf8903SRoss Lagerwall     if (rc) {
4422cbf8903SRoss Lagerwall         error_report("pin_memory_cacheattr failed: %s", strerror(errno));
4432cbf8903SRoss Lagerwall     }
444697b66d0SIgor Druzhinin     return xen_save_physmap(state, physmap);
44593d43e7eSAnthony Xu }
44693d43e7eSAnthony Xu 
44793d43e7eSAnthony Xu static int xen_remove_from_physmap(XenIOState *state,
44893d43e7eSAnthony Xu                                    hwaddr start_addr,
44993d43e7eSAnthony Xu                                    ram_addr_t size)
45093d43e7eSAnthony Xu {
45193d43e7eSAnthony Xu     int rc = 0;
45293d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
45393d43e7eSAnthony Xu     hwaddr phys_offset = 0;
45493d43e7eSAnthony Xu 
45504a8f72eSIgor Druzhinin     physmap = get_physmapping(start_addr, size);
45693d43e7eSAnthony Xu     if (physmap == NULL) {
45793d43e7eSAnthony Xu         return -1;
45893d43e7eSAnthony Xu     }
45993d43e7eSAnthony Xu 
46093d43e7eSAnthony Xu     phys_offset = physmap->phys_offset;
46193d43e7eSAnthony Xu     size = physmap->size;
46293d43e7eSAnthony Xu 
46393d43e7eSAnthony Xu     DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
46493d43e7eSAnthony Xu             "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
46593d43e7eSAnthony Xu 
46693d43e7eSAnthony Xu     size >>= TARGET_PAGE_BITS;
46793d43e7eSAnthony Xu     start_addr >>= TARGET_PAGE_BITS;
46893d43e7eSAnthony Xu     phys_offset >>= TARGET_PAGE_BITS;
4692cbf8903SRoss Lagerwall     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
4702cbf8903SRoss Lagerwall                                         phys_offset);
47193d43e7eSAnthony Xu     if (rc) {
4722cbf8903SRoss Lagerwall         int saved_errno = errno;
4732cbf8903SRoss Lagerwall 
4742cbf8903SRoss Lagerwall         error_report("relocate_memory "RAM_ADDR_FMT" pages"
4752cbf8903SRoss Lagerwall                      " from GFN %"HWADDR_PRIx
4762cbf8903SRoss Lagerwall                      " to GFN %"HWADDR_PRIx" failed: %s",
4772cbf8903SRoss Lagerwall                      size, start_addr, phys_offset, strerror(saved_errno));
4782cbf8903SRoss Lagerwall         errno = saved_errno;
4792cbf8903SRoss Lagerwall         return -1;
48093d43e7eSAnthony Xu     }
48193d43e7eSAnthony Xu 
48293d43e7eSAnthony Xu     QLIST_REMOVE(physmap, list);
48393d43e7eSAnthony Xu     if (state->log_for_dirtybit == physmap) {
48493d43e7eSAnthony Xu         state->log_for_dirtybit = NULL;
48534fbbc16SAnthony PERARD         g_free(state->dirty_bitmap);
48634fbbc16SAnthony PERARD         state->dirty_bitmap = NULL;
48793d43e7eSAnthony Xu     }
48893d43e7eSAnthony Xu     g_free(physmap);
48993d43e7eSAnthony Xu 
49093d43e7eSAnthony Xu     return 0;
49193d43e7eSAnthony Xu }
49293d43e7eSAnthony Xu 
49393d43e7eSAnthony Xu static void xen_set_memory(struct MemoryListener *listener,
49493d43e7eSAnthony Xu                            MemoryRegionSection *section,
49593d43e7eSAnthony Xu                            bool add)
49693d43e7eSAnthony Xu {
49793d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
49893d43e7eSAnthony Xu     hwaddr start_addr = section->offset_within_address_space;
49993d43e7eSAnthony Xu     ram_addr_t size = int128_get64(section->size);
50093d43e7eSAnthony Xu     bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
50193d43e7eSAnthony Xu     hvmmem_type_t mem_type;
50293d43e7eSAnthony Xu 
50393d43e7eSAnthony Xu     if (section->mr == &ram_memory) {
50493d43e7eSAnthony Xu         return;
50593d43e7eSAnthony Xu     } else {
50693d43e7eSAnthony Xu         if (add) {
50793d43e7eSAnthony Xu             xen_map_memory_section(xen_domid, state->ioservid,
50893d43e7eSAnthony Xu                                    section);
50993d43e7eSAnthony Xu         } else {
51093d43e7eSAnthony Xu             xen_unmap_memory_section(xen_domid, state->ioservid,
51193d43e7eSAnthony Xu                                      section);
51293d43e7eSAnthony Xu         }
51393d43e7eSAnthony Xu     }
51493d43e7eSAnthony Xu 
51593d43e7eSAnthony Xu     if (!memory_region_is_ram(section->mr)) {
51693d43e7eSAnthony Xu         return;
51793d43e7eSAnthony Xu     }
51893d43e7eSAnthony Xu 
51993d43e7eSAnthony Xu     if (log_dirty != add) {
52093d43e7eSAnthony Xu         return;
52193d43e7eSAnthony Xu     }
52293d43e7eSAnthony Xu 
52393d43e7eSAnthony Xu     trace_xen_client_set_memory(start_addr, size, log_dirty);
52493d43e7eSAnthony Xu 
52593d43e7eSAnthony Xu     start_addr &= TARGET_PAGE_MASK;
52693d43e7eSAnthony Xu     size = TARGET_PAGE_ALIGN(size);
52793d43e7eSAnthony Xu 
52893d43e7eSAnthony Xu     if (add) {
52993d43e7eSAnthony Xu         if (!memory_region_is_rom(section->mr)) {
53093d43e7eSAnthony Xu             xen_add_to_physmap(state, start_addr, size,
53193d43e7eSAnthony Xu                                section->mr, section->offset_within_region);
53293d43e7eSAnthony Xu         } else {
53393d43e7eSAnthony Xu             mem_type = HVMMEM_ram_ro;
53493d43e7eSAnthony Xu             if (xen_set_mem_type(xen_domid, mem_type,
53593d43e7eSAnthony Xu                                  start_addr >> TARGET_PAGE_BITS,
53693d43e7eSAnthony Xu                                  size >> TARGET_PAGE_BITS)) {
53793d43e7eSAnthony Xu                 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx"\n",
53893d43e7eSAnthony Xu                         start_addr);
53993d43e7eSAnthony Xu             }
54093d43e7eSAnthony Xu         }
54193d43e7eSAnthony Xu     } else {
54293d43e7eSAnthony Xu         if (xen_remove_from_physmap(state, start_addr, size) < 0) {
54393d43e7eSAnthony Xu             DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
54493d43e7eSAnthony Xu         }
54593d43e7eSAnthony Xu     }
54693d43e7eSAnthony Xu }
54793d43e7eSAnthony Xu 
54893d43e7eSAnthony Xu static void xen_region_add(MemoryListener *listener,
54993d43e7eSAnthony Xu                            MemoryRegionSection *section)
55093d43e7eSAnthony Xu {
55193d43e7eSAnthony Xu     memory_region_ref(section->mr);
55293d43e7eSAnthony Xu     xen_set_memory(listener, section, true);
55393d43e7eSAnthony Xu }
55493d43e7eSAnthony Xu 
55593d43e7eSAnthony Xu static void xen_region_del(MemoryListener *listener,
55693d43e7eSAnthony Xu                            MemoryRegionSection *section)
55793d43e7eSAnthony Xu {
55893d43e7eSAnthony Xu     xen_set_memory(listener, section, false);
55993d43e7eSAnthony Xu     memory_region_unref(section->mr);
56093d43e7eSAnthony Xu }
56193d43e7eSAnthony Xu 
56293d43e7eSAnthony Xu static void xen_io_add(MemoryListener *listener,
56393d43e7eSAnthony Xu                        MemoryRegionSection *section)
56493d43e7eSAnthony Xu {
56593d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, io_listener);
56693d43e7eSAnthony Xu     MemoryRegion *mr = section->mr;
56793d43e7eSAnthony Xu 
56893d43e7eSAnthony Xu     if (mr->ops == &unassigned_io_ops) {
56993d43e7eSAnthony Xu         return;
57093d43e7eSAnthony Xu     }
57193d43e7eSAnthony Xu 
57293d43e7eSAnthony Xu     memory_region_ref(mr);
57393d43e7eSAnthony Xu 
57493d43e7eSAnthony Xu     xen_map_io_section(xen_domid, state->ioservid, section);
57593d43e7eSAnthony Xu }
57693d43e7eSAnthony Xu 
57793d43e7eSAnthony Xu static void xen_io_del(MemoryListener *listener,
57893d43e7eSAnthony Xu                        MemoryRegionSection *section)
57993d43e7eSAnthony Xu {
58093d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, io_listener);
58193d43e7eSAnthony Xu     MemoryRegion *mr = section->mr;
58293d43e7eSAnthony Xu 
58393d43e7eSAnthony Xu     if (mr->ops == &unassigned_io_ops) {
58493d43e7eSAnthony Xu         return;
58593d43e7eSAnthony Xu     }
58693d43e7eSAnthony Xu 
58793d43e7eSAnthony Xu     xen_unmap_io_section(xen_domid, state->ioservid, section);
58893d43e7eSAnthony Xu 
58993d43e7eSAnthony Xu     memory_region_unref(mr);
59093d43e7eSAnthony Xu }
59193d43e7eSAnthony Xu 
59293d43e7eSAnthony Xu static void xen_device_realize(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 = g_new(XenPciDevice, 1);
600dfb6578dSPaul Durrant 
601dfb6578dSPaul Durrant         xendev->pci_dev = pci_dev;
602dfb6578dSPaul Durrant         xendev->sbdf = PCI_BUILD_BDF(pci_dev_bus_num(pci_dev),
603dfb6578dSPaul Durrant                                      pci_dev->devfn);
604dfb6578dSPaul Durrant         QLIST_INSERT_HEAD(&state->dev_list, xendev, entry);
60593d43e7eSAnthony Xu 
60693d43e7eSAnthony Xu         xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
60793d43e7eSAnthony Xu     }
60893d43e7eSAnthony Xu }
60993d43e7eSAnthony Xu 
61093d43e7eSAnthony Xu static void xen_device_unrealize(DeviceListener *listener,
61193d43e7eSAnthony Xu                                  DeviceState *dev)
61293d43e7eSAnthony Xu {
61393d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, device_listener);
61493d43e7eSAnthony Xu 
61593d43e7eSAnthony Xu     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
61693d43e7eSAnthony Xu         PCIDevice *pci_dev = PCI_DEVICE(dev);
617dfb6578dSPaul Durrant         XenPciDevice *xendev, *next;
61893d43e7eSAnthony Xu 
61993d43e7eSAnthony Xu         xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
620dfb6578dSPaul Durrant 
621dfb6578dSPaul Durrant         QLIST_FOREACH_SAFE(xendev, &state->dev_list, entry, next) {
622dfb6578dSPaul Durrant             if (xendev->pci_dev == pci_dev) {
623dfb6578dSPaul Durrant                 QLIST_REMOVE(xendev, entry);
624dfb6578dSPaul Durrant                 g_free(xendev);
625dfb6578dSPaul Durrant                 break;
626dfb6578dSPaul Durrant             }
627dfb6578dSPaul Durrant         }
62893d43e7eSAnthony Xu     }
62993d43e7eSAnthony Xu }
63093d43e7eSAnthony Xu 
63193d43e7eSAnthony Xu static void xen_sync_dirty_bitmap(XenIOState *state,
63293d43e7eSAnthony Xu                                   hwaddr start_addr,
63393d43e7eSAnthony Xu                                   ram_addr_t size)
63493d43e7eSAnthony Xu {
63593d43e7eSAnthony Xu     hwaddr npages = size >> TARGET_PAGE_BITS;
63693d43e7eSAnthony Xu     const int width = sizeof(unsigned long) * 8;
63734fbbc16SAnthony PERARD     size_t bitmap_size = DIV_ROUND_UP(npages, width);
63893d43e7eSAnthony Xu     int rc, i, j;
63993d43e7eSAnthony Xu     const XenPhysmap *physmap = NULL;
64093d43e7eSAnthony Xu 
64104a8f72eSIgor Druzhinin     physmap = get_physmapping(start_addr, size);
64293d43e7eSAnthony Xu     if (physmap == NULL) {
64393d43e7eSAnthony Xu         /* not handled */
64493d43e7eSAnthony Xu         return;
64593d43e7eSAnthony Xu     }
64693d43e7eSAnthony Xu 
64793d43e7eSAnthony Xu     if (state->log_for_dirtybit == NULL) {
64893d43e7eSAnthony Xu         state->log_for_dirtybit = physmap;
64934fbbc16SAnthony PERARD         state->dirty_bitmap = g_new(unsigned long, bitmap_size);
65093d43e7eSAnthony Xu     } else if (state->log_for_dirtybit != physmap) {
65193d43e7eSAnthony Xu         /* Only one range for dirty bitmap can be tracked. */
65293d43e7eSAnthony Xu         return;
65393d43e7eSAnthony Xu     }
65493d43e7eSAnthony Xu 
65593d43e7eSAnthony Xu     rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
65634fbbc16SAnthony PERARD                               npages, state->dirty_bitmap);
65793d43e7eSAnthony Xu     if (rc < 0) {
65893d43e7eSAnthony Xu #ifndef ENODATA
65993d43e7eSAnthony Xu #define ENODATA  ENOENT
66093d43e7eSAnthony Xu #endif
66193d43e7eSAnthony Xu         if (errno == ENODATA) {
66293d43e7eSAnthony Xu             memory_region_set_dirty(framebuffer, 0, size);
66393d43e7eSAnthony Xu             DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
66493d43e7eSAnthony Xu                     ", 0x" TARGET_FMT_plx "): %s\n",
66593d43e7eSAnthony Xu                     start_addr, start_addr + size, strerror(errno));
66693d43e7eSAnthony Xu         }
66793d43e7eSAnthony Xu         return;
66893d43e7eSAnthony Xu     }
66993d43e7eSAnthony Xu 
67034fbbc16SAnthony PERARD     for (i = 0; i < bitmap_size; i++) {
67134fbbc16SAnthony PERARD         unsigned long map = state->dirty_bitmap[i];
67293d43e7eSAnthony Xu         while (map != 0) {
67393d43e7eSAnthony Xu             j = ctzl(map);
67493d43e7eSAnthony Xu             map &= ~(1ul << j);
67593d43e7eSAnthony Xu             memory_region_set_dirty(framebuffer,
67693d43e7eSAnthony Xu                                     (i * width + j) * TARGET_PAGE_SIZE,
67793d43e7eSAnthony Xu                                     TARGET_PAGE_SIZE);
67893d43e7eSAnthony Xu         };
67993d43e7eSAnthony Xu     }
68093d43e7eSAnthony Xu }
68193d43e7eSAnthony Xu 
68293d43e7eSAnthony Xu static void xen_log_start(MemoryListener *listener,
68393d43e7eSAnthony Xu                           MemoryRegionSection *section,
68493d43e7eSAnthony Xu                           int old, int new)
68593d43e7eSAnthony Xu {
68693d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
68793d43e7eSAnthony Xu 
68893d43e7eSAnthony Xu     if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
68993d43e7eSAnthony Xu         xen_sync_dirty_bitmap(state, section->offset_within_address_space,
69093d43e7eSAnthony Xu                               int128_get64(section->size));
69193d43e7eSAnthony Xu     }
69293d43e7eSAnthony Xu }
69393d43e7eSAnthony Xu 
69493d43e7eSAnthony Xu static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
69593d43e7eSAnthony Xu                          int old, int new)
69693d43e7eSAnthony Xu {
69793d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
69893d43e7eSAnthony Xu 
69993d43e7eSAnthony Xu     if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
70093d43e7eSAnthony Xu         state->log_for_dirtybit = NULL;
70134fbbc16SAnthony PERARD         g_free(state->dirty_bitmap);
70234fbbc16SAnthony PERARD         state->dirty_bitmap = NULL;
70393d43e7eSAnthony Xu         /* Disable dirty bit tracking */
70493d43e7eSAnthony Xu         xen_track_dirty_vram(xen_domid, 0, 0, NULL);
70593d43e7eSAnthony Xu     }
70693d43e7eSAnthony Xu }
70793d43e7eSAnthony Xu 
70893d43e7eSAnthony Xu static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
70993d43e7eSAnthony Xu {
71093d43e7eSAnthony Xu     XenIOState *state = container_of(listener, XenIOState, memory_listener);
71193d43e7eSAnthony Xu 
71293d43e7eSAnthony Xu     xen_sync_dirty_bitmap(state, section->offset_within_address_space,
71393d43e7eSAnthony Xu                           int128_get64(section->size));
71493d43e7eSAnthony Xu }
71593d43e7eSAnthony Xu 
71693d43e7eSAnthony Xu static void xen_log_global_start(MemoryListener *listener)
71793d43e7eSAnthony Xu {
71893d43e7eSAnthony Xu     if (xen_enabled()) {
71993d43e7eSAnthony Xu         xen_in_migration = true;
72093d43e7eSAnthony Xu     }
72193d43e7eSAnthony Xu }
72293d43e7eSAnthony Xu 
72393d43e7eSAnthony Xu static void xen_log_global_stop(MemoryListener *listener)
72493d43e7eSAnthony Xu {
72593d43e7eSAnthony Xu     xen_in_migration = false;
72693d43e7eSAnthony Xu }
72793d43e7eSAnthony Xu 
72893d43e7eSAnthony Xu static MemoryListener xen_memory_listener = {
729142518bdSPeter Xu     .name = "xen-memory",
73093d43e7eSAnthony Xu     .region_add = xen_region_add,
73193d43e7eSAnthony Xu     .region_del = xen_region_del,
73293d43e7eSAnthony Xu     .log_start = xen_log_start,
73393d43e7eSAnthony Xu     .log_stop = xen_log_stop,
73493d43e7eSAnthony Xu     .log_sync = xen_log_sync,
73593d43e7eSAnthony Xu     .log_global_start = xen_log_global_start,
73693d43e7eSAnthony Xu     .log_global_stop = xen_log_global_stop,
73793d43e7eSAnthony Xu     .priority = 10,
73893d43e7eSAnthony Xu };
73993d43e7eSAnthony Xu 
74093d43e7eSAnthony Xu static MemoryListener xen_io_listener = {
741142518bdSPeter Xu     .name = "xen-io",
74293d43e7eSAnthony Xu     .region_add = xen_io_add,
74393d43e7eSAnthony Xu     .region_del = xen_io_del,
74493d43e7eSAnthony Xu     .priority = 10,
74593d43e7eSAnthony Xu };
74693d43e7eSAnthony Xu 
74793d43e7eSAnthony Xu static DeviceListener xen_device_listener = {
74893d43e7eSAnthony Xu     .realize = xen_device_realize,
74993d43e7eSAnthony Xu     .unrealize = xen_device_unrealize,
75093d43e7eSAnthony Xu };
75193d43e7eSAnthony Xu 
75293d43e7eSAnthony Xu /* get the ioreq packets from share mem */
75393d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
75493d43e7eSAnthony Xu {
75593d43e7eSAnthony Xu     ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
75693d43e7eSAnthony Xu 
75793d43e7eSAnthony Xu     if (req->state != STATE_IOREQ_READY) {
75893d43e7eSAnthony Xu         DPRINTF("I/O request not ready: "
75993d43e7eSAnthony Xu                 "%x, ptr: %x, port: %"PRIx64", "
76093d43e7eSAnthony Xu                 "data: %"PRIx64", count: %u, size: %u\n",
76193d43e7eSAnthony Xu                 req->state, req->data_is_ptr, req->addr,
76293d43e7eSAnthony Xu                 req->data, req->count, req->size);
76393d43e7eSAnthony Xu         return NULL;
76493d43e7eSAnthony Xu     }
76593d43e7eSAnthony Xu 
76693d43e7eSAnthony Xu     xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
76793d43e7eSAnthony Xu 
76893d43e7eSAnthony Xu     req->state = STATE_IOREQ_INPROCESS;
76993d43e7eSAnthony Xu     return req;
77093d43e7eSAnthony Xu }
77193d43e7eSAnthony Xu 
77293d43e7eSAnthony Xu /* use poll to get the port notification */
77393d43e7eSAnthony Xu /* ioreq_vec--out,the */
77493d43e7eSAnthony Xu /* retval--the number of ioreq packet */
77593d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq(XenIOState *state)
77693d43e7eSAnthony Xu {
7770e11fc69SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7780e11fc69SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
77993d43e7eSAnthony Xu     int i;
78093d43e7eSAnthony Xu     evtchn_port_t port;
78193d43e7eSAnthony Xu 
78293d43e7eSAnthony Xu     port = xenevtchn_pending(state->xce_handle);
78393d43e7eSAnthony Xu     if (port == state->bufioreq_local_port) {
78493d43e7eSAnthony Xu         timer_mod(state->buffered_io_timer,
78593d43e7eSAnthony Xu                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
78693d43e7eSAnthony Xu         return NULL;
78793d43e7eSAnthony Xu     }
78893d43e7eSAnthony Xu 
78993d43e7eSAnthony Xu     if (port != -1) {
79093d43e7eSAnthony Xu         for (i = 0; i < max_cpus; i++) {
79193d43e7eSAnthony Xu             if (state->ioreq_local_port[i] == port) {
79293d43e7eSAnthony Xu                 break;
79393d43e7eSAnthony Xu             }
79493d43e7eSAnthony Xu         }
79593d43e7eSAnthony Xu 
79693d43e7eSAnthony Xu         if (i == max_cpus) {
79793d43e7eSAnthony Xu             hw_error("Fatal error while trying to get io event!\n");
79893d43e7eSAnthony Xu         }
79993d43e7eSAnthony Xu 
80093d43e7eSAnthony Xu         /* unmask the wanted port again */
80193d43e7eSAnthony Xu         xenevtchn_unmask(state->xce_handle, port);
80293d43e7eSAnthony Xu 
80393d43e7eSAnthony Xu         /* get the io packet from shared memory */
80493d43e7eSAnthony Xu         state->send_vcpu = i;
80593d43e7eSAnthony Xu         return cpu_get_ioreq_from_shared_memory(state, i);
80693d43e7eSAnthony Xu     }
80793d43e7eSAnthony Xu 
80893d43e7eSAnthony Xu     /* read error or read nothing */
80993d43e7eSAnthony Xu     return NULL;
81093d43e7eSAnthony Xu }
81193d43e7eSAnthony Xu 
81293d43e7eSAnthony Xu static uint32_t do_inp(uint32_t addr, unsigned long size)
81393d43e7eSAnthony Xu {
81493d43e7eSAnthony Xu     switch (size) {
81593d43e7eSAnthony Xu         case 1:
81693d43e7eSAnthony Xu             return cpu_inb(addr);
81793d43e7eSAnthony Xu         case 2:
81893d43e7eSAnthony Xu             return cpu_inw(addr);
81993d43e7eSAnthony Xu         case 4:
82093d43e7eSAnthony Xu             return cpu_inl(addr);
82193d43e7eSAnthony Xu         default:
82293d43e7eSAnthony Xu             hw_error("inp: bad size: %04x %lx", addr, size);
82393d43e7eSAnthony Xu     }
82493d43e7eSAnthony Xu }
82593d43e7eSAnthony Xu 
82693d43e7eSAnthony Xu static void do_outp(uint32_t addr,
82793d43e7eSAnthony Xu         unsigned long size, uint32_t val)
82893d43e7eSAnthony Xu {
82993d43e7eSAnthony Xu     switch (size) {
83093d43e7eSAnthony Xu         case 1:
83193d43e7eSAnthony Xu             return cpu_outb(addr, val);
83293d43e7eSAnthony Xu         case 2:
83393d43e7eSAnthony Xu             return cpu_outw(addr, val);
83493d43e7eSAnthony Xu         case 4:
83593d43e7eSAnthony Xu             return cpu_outl(addr, val);
83693d43e7eSAnthony Xu         default:
83793d43e7eSAnthony Xu             hw_error("outp: bad size: %04x %lx", addr, size);
83893d43e7eSAnthony Xu     }
83993d43e7eSAnthony Xu }
84093d43e7eSAnthony Xu 
84193d43e7eSAnthony Xu /*
84293d43e7eSAnthony Xu  * Helper functions which read/write an object from/to physical guest
84393d43e7eSAnthony Xu  * memory, as part of the implementation of an ioreq.
84493d43e7eSAnthony Xu  *
84593d43e7eSAnthony Xu  * Equivalent to
84693d43e7eSAnthony Xu  *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
84793d43e7eSAnthony Xu  *                          val, req->size, 0/1)
84893d43e7eSAnthony Xu  * except without the integer overflow problems.
84993d43e7eSAnthony Xu  */
85093d43e7eSAnthony Xu static void rw_phys_req_item(hwaddr addr,
85193d43e7eSAnthony Xu                              ioreq_t *req, uint32_t i, void *val, int rw)
85293d43e7eSAnthony Xu {
85393d43e7eSAnthony Xu     /* Do everything unsigned so overflow just results in a truncated result
85493d43e7eSAnthony Xu      * and accesses to undesired parts of guest memory, which is up
85593d43e7eSAnthony Xu      * to the guest */
85693d43e7eSAnthony Xu     hwaddr offset = (hwaddr)req->size * i;
85793d43e7eSAnthony Xu     if (req->df) {
85893d43e7eSAnthony Xu         addr -= offset;
85993d43e7eSAnthony Xu     } else {
86093d43e7eSAnthony Xu         addr += offset;
86193d43e7eSAnthony Xu     }
86293d43e7eSAnthony Xu     cpu_physical_memory_rw(addr, val, req->size, rw);
86393d43e7eSAnthony Xu }
86493d43e7eSAnthony Xu 
86593d43e7eSAnthony Xu static inline void read_phys_req_item(hwaddr addr,
86693d43e7eSAnthony Xu                                       ioreq_t *req, uint32_t i, void *val)
86793d43e7eSAnthony Xu {
86893d43e7eSAnthony Xu     rw_phys_req_item(addr, req, i, val, 0);
86993d43e7eSAnthony Xu }
87093d43e7eSAnthony Xu static inline void write_phys_req_item(hwaddr addr,
87193d43e7eSAnthony Xu                                        ioreq_t *req, uint32_t i, void *val)
87293d43e7eSAnthony Xu {
87393d43e7eSAnthony Xu     rw_phys_req_item(addr, req, i, val, 1);
87493d43e7eSAnthony Xu }
87593d43e7eSAnthony Xu 
87693d43e7eSAnthony Xu 
87793d43e7eSAnthony Xu static void cpu_ioreq_pio(ioreq_t *req)
87893d43e7eSAnthony Xu {
87993d43e7eSAnthony Xu     uint32_t i;
88093d43e7eSAnthony Xu 
88193d43e7eSAnthony Xu     trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
88293d43e7eSAnthony Xu                          req->data, req->count, req->size);
88393d43e7eSAnthony Xu 
88493d43e7eSAnthony Xu     if (req->size > sizeof(uint32_t)) {
88593d43e7eSAnthony Xu         hw_error("PIO: bad size (%u)", req->size);
88693d43e7eSAnthony Xu     }
88793d43e7eSAnthony Xu 
88893d43e7eSAnthony Xu     if (req->dir == IOREQ_READ) {
88993d43e7eSAnthony Xu         if (!req->data_is_ptr) {
89093d43e7eSAnthony Xu             req->data = do_inp(req->addr, req->size);
89193d43e7eSAnthony Xu             trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
89293d43e7eSAnthony Xu                                          req->size);
89393d43e7eSAnthony Xu         } else {
89493d43e7eSAnthony Xu             uint32_t tmp;
89593d43e7eSAnthony Xu 
89693d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
89793d43e7eSAnthony Xu                 tmp = do_inp(req->addr, req->size);
89893d43e7eSAnthony Xu                 write_phys_req_item(req->data, req, i, &tmp);
89993d43e7eSAnthony Xu             }
90093d43e7eSAnthony Xu         }
90193d43e7eSAnthony Xu     } else if (req->dir == IOREQ_WRITE) {
90293d43e7eSAnthony Xu         if (!req->data_is_ptr) {
90393d43e7eSAnthony Xu             trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
90493d43e7eSAnthony Xu                                           req->size);
90593d43e7eSAnthony Xu             do_outp(req->addr, req->size, req->data);
90693d43e7eSAnthony Xu         } else {
90793d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
90893d43e7eSAnthony Xu                 uint32_t tmp = 0;
90993d43e7eSAnthony Xu 
91093d43e7eSAnthony Xu                 read_phys_req_item(req->data, req, i, &tmp);
91193d43e7eSAnthony Xu                 do_outp(req->addr, req->size, tmp);
91293d43e7eSAnthony Xu             }
91393d43e7eSAnthony Xu         }
91493d43e7eSAnthony Xu     }
91593d43e7eSAnthony Xu }
91693d43e7eSAnthony Xu 
91793d43e7eSAnthony Xu static void cpu_ioreq_move(ioreq_t *req)
91893d43e7eSAnthony Xu {
91993d43e7eSAnthony Xu     uint32_t i;
92093d43e7eSAnthony Xu 
92193d43e7eSAnthony Xu     trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
92293d43e7eSAnthony Xu                          req->data, req->count, req->size);
92393d43e7eSAnthony Xu 
92493d43e7eSAnthony Xu     if (req->size > sizeof(req->data)) {
92593d43e7eSAnthony Xu         hw_error("MMIO: bad size (%u)", req->size);
92693d43e7eSAnthony Xu     }
92793d43e7eSAnthony Xu 
92893d43e7eSAnthony Xu     if (!req->data_is_ptr) {
92993d43e7eSAnthony Xu         if (req->dir == IOREQ_READ) {
93093d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
93193d43e7eSAnthony Xu                 read_phys_req_item(req->addr, req, i, &req->data);
93293d43e7eSAnthony Xu             }
93393d43e7eSAnthony Xu         } else if (req->dir == IOREQ_WRITE) {
93493d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
93593d43e7eSAnthony Xu                 write_phys_req_item(req->addr, req, i, &req->data);
93693d43e7eSAnthony Xu             }
93793d43e7eSAnthony Xu         }
93893d43e7eSAnthony Xu     } else {
93993d43e7eSAnthony Xu         uint64_t tmp;
94093d43e7eSAnthony Xu 
94193d43e7eSAnthony Xu         if (req->dir == IOREQ_READ) {
94293d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
94393d43e7eSAnthony Xu                 read_phys_req_item(req->addr, req, i, &tmp);
94493d43e7eSAnthony Xu                 write_phys_req_item(req->data, req, i, &tmp);
94593d43e7eSAnthony Xu             }
94693d43e7eSAnthony Xu         } else if (req->dir == IOREQ_WRITE) {
94793d43e7eSAnthony Xu             for (i = 0; i < req->count; i++) {
94893d43e7eSAnthony Xu                 read_phys_req_item(req->data, req, i, &tmp);
94993d43e7eSAnthony Xu                 write_phys_req_item(req->addr, req, i, &tmp);
95093d43e7eSAnthony Xu             }
95193d43e7eSAnthony Xu         }
95293d43e7eSAnthony Xu     }
95393d43e7eSAnthony Xu }
95493d43e7eSAnthony Xu 
955dfb6578dSPaul Durrant static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
956dfb6578dSPaul Durrant {
957dfb6578dSPaul Durrant     uint32_t sbdf = req->addr >> 32;
958dfb6578dSPaul Durrant     uint32_t reg = req->addr;
959dfb6578dSPaul Durrant     XenPciDevice *xendev;
960dfb6578dSPaul Durrant 
961dfb6578dSPaul Durrant     if (req->size != sizeof(uint8_t) && req->size != sizeof(uint16_t) &&
962dfb6578dSPaul Durrant         req->size != sizeof(uint32_t)) {
963dfb6578dSPaul Durrant         hw_error("PCI config access: bad size (%u)", req->size);
964dfb6578dSPaul Durrant     }
965dfb6578dSPaul Durrant 
966dfb6578dSPaul Durrant     if (req->count != 1) {
967dfb6578dSPaul Durrant         hw_error("PCI config access: bad count (%u)", req->count);
968dfb6578dSPaul Durrant     }
969dfb6578dSPaul Durrant 
970dfb6578dSPaul Durrant     QLIST_FOREACH(xendev, &state->dev_list, entry) {
971dfb6578dSPaul Durrant         if (xendev->sbdf != sbdf) {
972dfb6578dSPaul Durrant             continue;
973dfb6578dSPaul Durrant         }
974dfb6578dSPaul Durrant 
975dfb6578dSPaul Durrant         if (!req->data_is_ptr) {
976dfb6578dSPaul Durrant             if (req->dir == IOREQ_READ) {
977dfb6578dSPaul Durrant                 req->data = pci_host_config_read_common(
978dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
979dfb6578dSPaul Durrant                     req->size);
980dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
981dfb6578dSPaul Durrant                                             req->size, req->data);
982dfb6578dSPaul Durrant             } else if (req->dir == IOREQ_WRITE) {
983dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
984dfb6578dSPaul Durrant                                              req->size, req->data);
985dfb6578dSPaul Durrant                 pci_host_config_write_common(
986dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
987dfb6578dSPaul Durrant                     req->data, req->size);
988dfb6578dSPaul Durrant             }
989dfb6578dSPaul Durrant         } else {
990dfb6578dSPaul Durrant             uint32_t tmp;
991dfb6578dSPaul Durrant 
992dfb6578dSPaul Durrant             if (req->dir == IOREQ_READ) {
993dfb6578dSPaul Durrant                 tmp = pci_host_config_read_common(
994dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
995dfb6578dSPaul Durrant                     req->size);
996dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
997dfb6578dSPaul Durrant                                             req->size, tmp);
998dfb6578dSPaul Durrant                 write_phys_req_item(req->data, req, 0, &tmp);
999dfb6578dSPaul Durrant             } else if (req->dir == IOREQ_WRITE) {
1000dfb6578dSPaul Durrant                 read_phys_req_item(req->data, req, 0, &tmp);
1001dfb6578dSPaul Durrant                 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
1002dfb6578dSPaul Durrant                                              req->size, tmp);
1003dfb6578dSPaul Durrant                 pci_host_config_write_common(
1004dfb6578dSPaul Durrant                     xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
1005dfb6578dSPaul Durrant                     tmp, req->size);
1006dfb6578dSPaul Durrant             }
1007dfb6578dSPaul Durrant         }
1008dfb6578dSPaul Durrant     }
1009dfb6578dSPaul Durrant }
1010dfb6578dSPaul Durrant 
101193d43e7eSAnthony Xu static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
101293d43e7eSAnthony Xu {
101393d43e7eSAnthony Xu     X86CPU *cpu;
101493d43e7eSAnthony Xu     CPUX86State *env;
101593d43e7eSAnthony Xu 
101693d43e7eSAnthony Xu     cpu = X86_CPU(current_cpu);
101793d43e7eSAnthony Xu     env = &cpu->env;
101893d43e7eSAnthony Xu     env->regs[R_EAX] = req->data;
101993d43e7eSAnthony Xu     env->regs[R_EBX] = vmport_regs->ebx;
102093d43e7eSAnthony Xu     env->regs[R_ECX] = vmport_regs->ecx;
102193d43e7eSAnthony Xu     env->regs[R_EDX] = vmport_regs->edx;
102293d43e7eSAnthony Xu     env->regs[R_ESI] = vmport_regs->esi;
102393d43e7eSAnthony Xu     env->regs[R_EDI] = vmport_regs->edi;
102493d43e7eSAnthony Xu }
102593d43e7eSAnthony Xu 
102693d43e7eSAnthony Xu static void regs_from_cpu(vmware_regs_t *vmport_regs)
102793d43e7eSAnthony Xu {
102893d43e7eSAnthony Xu     X86CPU *cpu = X86_CPU(current_cpu);
102993d43e7eSAnthony Xu     CPUX86State *env = &cpu->env;
103093d43e7eSAnthony Xu 
103193d43e7eSAnthony Xu     vmport_regs->ebx = env->regs[R_EBX];
103293d43e7eSAnthony Xu     vmport_regs->ecx = env->regs[R_ECX];
103393d43e7eSAnthony Xu     vmport_regs->edx = env->regs[R_EDX];
103493d43e7eSAnthony Xu     vmport_regs->esi = env->regs[R_ESI];
103593d43e7eSAnthony Xu     vmport_regs->edi = env->regs[R_EDI];
103693d43e7eSAnthony Xu }
103793d43e7eSAnthony Xu 
103893d43e7eSAnthony Xu static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
103993d43e7eSAnthony Xu {
104093d43e7eSAnthony Xu     vmware_regs_t *vmport_regs;
104193d43e7eSAnthony Xu 
104293d43e7eSAnthony Xu     assert(state->shared_vmport_page);
104393d43e7eSAnthony Xu     vmport_regs =
104493d43e7eSAnthony Xu         &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
104593d43e7eSAnthony Xu     QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
104693d43e7eSAnthony Xu 
104793d43e7eSAnthony Xu     current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
104893d43e7eSAnthony Xu     regs_to_cpu(vmport_regs, req);
104993d43e7eSAnthony Xu     cpu_ioreq_pio(req);
105093d43e7eSAnthony Xu     regs_from_cpu(vmport_regs);
105193d43e7eSAnthony Xu     current_cpu = NULL;
105293d43e7eSAnthony Xu }
105393d43e7eSAnthony Xu 
105493d43e7eSAnthony Xu static void handle_ioreq(XenIOState *state, ioreq_t *req)
105593d43e7eSAnthony Xu {
105693d43e7eSAnthony Xu     trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
105793d43e7eSAnthony Xu                        req->addr, req->data, req->count, req->size);
105893d43e7eSAnthony Xu 
105993d43e7eSAnthony Xu     if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
106093d43e7eSAnthony Xu             (req->size < sizeof (target_ulong))) {
106193d43e7eSAnthony Xu         req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
106293d43e7eSAnthony Xu     }
106393d43e7eSAnthony Xu 
106493d43e7eSAnthony Xu     if (req->dir == IOREQ_WRITE)
106593d43e7eSAnthony Xu         trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
106693d43e7eSAnthony Xu                                  req->addr, req->data, req->count, req->size);
106793d43e7eSAnthony Xu 
106893d43e7eSAnthony Xu     switch (req->type) {
106993d43e7eSAnthony Xu         case IOREQ_TYPE_PIO:
107093d43e7eSAnthony Xu             cpu_ioreq_pio(req);
107193d43e7eSAnthony Xu             break;
107293d43e7eSAnthony Xu         case IOREQ_TYPE_COPY:
107393d43e7eSAnthony Xu             cpu_ioreq_move(req);
107493d43e7eSAnthony Xu             break;
107593d43e7eSAnthony Xu         case IOREQ_TYPE_VMWARE_PORT:
107693d43e7eSAnthony Xu             handle_vmport_ioreq(state, req);
107793d43e7eSAnthony Xu             break;
107893d43e7eSAnthony Xu         case IOREQ_TYPE_TIMEOFFSET:
107993d43e7eSAnthony Xu             break;
108093d43e7eSAnthony Xu         case IOREQ_TYPE_INVALIDATE:
108193d43e7eSAnthony Xu             xen_invalidate_map_cache();
108293d43e7eSAnthony Xu             break;
1083dfb6578dSPaul Durrant         case IOREQ_TYPE_PCI_CONFIG:
1084dfb6578dSPaul Durrant             cpu_ioreq_config(state, req);
108593d43e7eSAnthony Xu             break;
108693d43e7eSAnthony Xu         default:
108793d43e7eSAnthony Xu             hw_error("Invalid ioreq type 0x%x\n", req->type);
108893d43e7eSAnthony Xu     }
108993d43e7eSAnthony Xu     if (req->dir == IOREQ_READ) {
109093d43e7eSAnthony Xu         trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
109193d43e7eSAnthony Xu                                 req->addr, req->data, req->count, req->size);
109293d43e7eSAnthony Xu     }
109393d43e7eSAnthony Xu }
109493d43e7eSAnthony Xu 
10959288e803SJason Andryuk static bool handle_buffered_iopage(XenIOState *state)
109693d43e7eSAnthony Xu {
109793d43e7eSAnthony Xu     buffered_iopage_t *buf_page = state->buffered_io_page;
109893d43e7eSAnthony Xu     buf_ioreq_t *buf_req = NULL;
10999288e803SJason Andryuk     bool handled_ioreq = false;
110093d43e7eSAnthony Xu     ioreq_t req;
110193d43e7eSAnthony Xu     int qw;
110293d43e7eSAnthony Xu 
110393d43e7eSAnthony Xu     if (!buf_page) {
110493d43e7eSAnthony Xu         return 0;
110593d43e7eSAnthony Xu     }
110693d43e7eSAnthony Xu 
110793d43e7eSAnthony Xu     memset(&req, 0x00, sizeof(req));
110893d43e7eSAnthony Xu     req.state = STATE_IOREQ_READY;
110993d43e7eSAnthony Xu     req.count = 1;
111093d43e7eSAnthony Xu     req.dir = IOREQ_WRITE;
111193d43e7eSAnthony Xu 
111293d43e7eSAnthony Xu     for (;;) {
111393d43e7eSAnthony Xu         uint32_t rdptr = buf_page->read_pointer, wrptr;
111493d43e7eSAnthony Xu 
111593d43e7eSAnthony Xu         xen_rmb();
111693d43e7eSAnthony Xu         wrptr = buf_page->write_pointer;
111793d43e7eSAnthony Xu         xen_rmb();
111893d43e7eSAnthony Xu         if (rdptr != buf_page->read_pointer) {
111993d43e7eSAnthony Xu             continue;
112093d43e7eSAnthony Xu         }
112193d43e7eSAnthony Xu         if (rdptr == wrptr) {
112293d43e7eSAnthony Xu             break;
112393d43e7eSAnthony Xu         }
112493d43e7eSAnthony Xu         buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
112593d43e7eSAnthony Xu         req.size = 1U << buf_req->size;
112693d43e7eSAnthony Xu         req.addr = buf_req->addr;
112793d43e7eSAnthony Xu         req.data = buf_req->data;
112893d43e7eSAnthony Xu         req.type = buf_req->type;
112993d43e7eSAnthony Xu         xen_rmb();
113093d43e7eSAnthony Xu         qw = (req.size == 8);
113193d43e7eSAnthony Xu         if (qw) {
113293d43e7eSAnthony Xu             if (rdptr + 1 == wrptr) {
113393d43e7eSAnthony Xu                 hw_error("Incomplete quad word buffered ioreq");
113493d43e7eSAnthony Xu             }
113593d43e7eSAnthony Xu             buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
113693d43e7eSAnthony Xu                                            IOREQ_BUFFER_SLOT_NUM];
113793d43e7eSAnthony Xu             req.data |= ((uint64_t)buf_req->data) << 32;
113893d43e7eSAnthony Xu             xen_rmb();
113993d43e7eSAnthony Xu         }
114093d43e7eSAnthony Xu 
114193d43e7eSAnthony Xu         handle_ioreq(state, &req);
114293d43e7eSAnthony Xu 
114393d43e7eSAnthony Xu         /* Only req.data may get updated by handle_ioreq(), albeit even that
114493d43e7eSAnthony Xu          * should not happen as such data would never make it to the guest (we
114593d43e7eSAnthony Xu          * can only usefully see writes here after all).
114693d43e7eSAnthony Xu          */
114793d43e7eSAnthony Xu         assert(req.state == STATE_IOREQ_READY);
114893d43e7eSAnthony Xu         assert(req.count == 1);
114993d43e7eSAnthony Xu         assert(req.dir == IOREQ_WRITE);
115093d43e7eSAnthony Xu         assert(!req.data_is_ptr);
115193d43e7eSAnthony Xu 
1152d73415a3SStefan Hajnoczi         qatomic_add(&buf_page->read_pointer, qw + 1);
11539288e803SJason Andryuk         handled_ioreq = true;
115493d43e7eSAnthony Xu     }
115593d43e7eSAnthony Xu 
11569288e803SJason Andryuk     return handled_ioreq;
115793d43e7eSAnthony Xu }
115893d43e7eSAnthony Xu 
115993d43e7eSAnthony Xu static void handle_buffered_io(void *opaque)
116093d43e7eSAnthony Xu {
116193d43e7eSAnthony Xu     XenIOState *state = opaque;
116293d43e7eSAnthony Xu 
116393d43e7eSAnthony Xu     if (handle_buffered_iopage(state)) {
116493d43e7eSAnthony Xu         timer_mod(state->buffered_io_timer,
116593d43e7eSAnthony Xu                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
116693d43e7eSAnthony Xu     } else {
116793d43e7eSAnthony Xu         timer_del(state->buffered_io_timer);
116893d43e7eSAnthony Xu         xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
116993d43e7eSAnthony Xu     }
117093d43e7eSAnthony Xu }
117193d43e7eSAnthony Xu 
117293d43e7eSAnthony Xu static void cpu_handle_ioreq(void *opaque)
117393d43e7eSAnthony Xu {
117493d43e7eSAnthony Xu     XenIOState *state = opaque;
117593d43e7eSAnthony Xu     ioreq_t *req = cpu_get_ioreq(state);
117693d43e7eSAnthony Xu 
117793d43e7eSAnthony Xu     handle_buffered_iopage(state);
117893d43e7eSAnthony Xu     if (req) {
117993d43e7eSAnthony Xu         ioreq_t copy = *req;
118093d43e7eSAnthony Xu 
118193d43e7eSAnthony Xu         xen_rmb();
118293d43e7eSAnthony Xu         handle_ioreq(state, &copy);
118393d43e7eSAnthony Xu         req->data = copy.data;
118493d43e7eSAnthony Xu 
118593d43e7eSAnthony Xu         if (req->state != STATE_IOREQ_INPROCESS) {
118693d43e7eSAnthony Xu             fprintf(stderr, "Badness in I/O request ... not in service?!: "
118793d43e7eSAnthony Xu                     "%x, ptr: %x, port: %"PRIx64", "
118893d43e7eSAnthony Xu                     "data: %"PRIx64", count: %u, size: %u, type: %u\n",
118993d43e7eSAnthony Xu                     req->state, req->data_is_ptr, req->addr,
119093d43e7eSAnthony Xu                     req->data, req->count, req->size, req->type);
119193d43e7eSAnthony Xu             destroy_hvm_domain(false);
119293d43e7eSAnthony Xu             return;
119393d43e7eSAnthony Xu         }
119493d43e7eSAnthony Xu 
119593d43e7eSAnthony Xu         xen_wmb(); /* Update ioreq contents /then/ update state. */
119693d43e7eSAnthony Xu 
119793d43e7eSAnthony Xu         /*
119893d43e7eSAnthony Xu          * We do this before we send the response so that the tools
119993d43e7eSAnthony Xu          * have the opportunity to pick up on the reset before the
120093d43e7eSAnthony Xu          * guest resumes and does a hlt with interrupts disabled which
120193d43e7eSAnthony Xu          * causes Xen to powerdown the domain.
120293d43e7eSAnthony Xu          */
120393d43e7eSAnthony Xu         if (runstate_is_running()) {
1204aedbe192SEric Blake             ShutdownCause request;
1205aedbe192SEric Blake 
120693d43e7eSAnthony Xu             if (qemu_shutdown_requested_get()) {
120793d43e7eSAnthony Xu                 destroy_hvm_domain(false);
120893d43e7eSAnthony Xu             }
1209aedbe192SEric Blake             request = qemu_reset_requested_get();
1210aedbe192SEric Blake             if (request) {
1211aedbe192SEric Blake                 qemu_system_reset(request);
121293d43e7eSAnthony Xu                 destroy_hvm_domain(true);
121393d43e7eSAnthony Xu             }
121493d43e7eSAnthony Xu         }
121593d43e7eSAnthony Xu 
121693d43e7eSAnthony Xu         req->state = STATE_IORESP_READY;
121793d43e7eSAnthony Xu         xenevtchn_notify(state->xce_handle,
121893d43e7eSAnthony Xu                          state->ioreq_local_port[state->send_vcpu]);
121993d43e7eSAnthony Xu     }
122093d43e7eSAnthony Xu }
122193d43e7eSAnthony Xu 
122293d43e7eSAnthony Xu static void xen_main_loop_prepare(XenIOState *state)
122393d43e7eSAnthony Xu {
122493d43e7eSAnthony Xu     int evtchn_fd = -1;
122593d43e7eSAnthony Xu 
122693d43e7eSAnthony Xu     if (state->xce_handle != NULL) {
122793d43e7eSAnthony Xu         evtchn_fd = xenevtchn_fd(state->xce_handle);
122893d43e7eSAnthony Xu     }
122993d43e7eSAnthony Xu 
123093d43e7eSAnthony Xu     state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
123193d43e7eSAnthony Xu                                                  state);
123293d43e7eSAnthony Xu 
123393d43e7eSAnthony Xu     if (evtchn_fd != -1) {
123493d43e7eSAnthony Xu         CPUState *cpu_state;
123593d43e7eSAnthony Xu 
123693d43e7eSAnthony Xu         DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
123793d43e7eSAnthony Xu         CPU_FOREACH(cpu_state) {
123893d43e7eSAnthony Xu             DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
123993d43e7eSAnthony Xu                     __func__, cpu_state->cpu_index, cpu_state);
124093d43e7eSAnthony Xu             state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
124193d43e7eSAnthony Xu         }
124293d43e7eSAnthony Xu         qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
124393d43e7eSAnthony Xu     }
124493d43e7eSAnthony Xu }
124593d43e7eSAnthony Xu 
124693d43e7eSAnthony Xu 
1247538f0497SPhilippe Mathieu-Daudé static void xen_hvm_change_state_handler(void *opaque, bool running,
124893d43e7eSAnthony Xu                                          RunState rstate)
124993d43e7eSAnthony Xu {
125093d43e7eSAnthony Xu     XenIOState *state = opaque;
125193d43e7eSAnthony Xu 
125293d43e7eSAnthony Xu     if (running) {
125393d43e7eSAnthony Xu         xen_main_loop_prepare(state);
125493d43e7eSAnthony Xu     }
125593d43e7eSAnthony Xu 
125693d43e7eSAnthony Xu     xen_set_ioreq_server_state(xen_domid,
125793d43e7eSAnthony Xu                                state->ioservid,
125893d43e7eSAnthony Xu                                (rstate == RUN_STATE_RUNNING));
125993d43e7eSAnthony Xu }
126093d43e7eSAnthony Xu 
126193d43e7eSAnthony Xu static void xen_exit_notifier(Notifier *n, void *data)
126293d43e7eSAnthony Xu {
126393d43e7eSAnthony Xu     XenIOState *state = container_of(n, XenIOState, exit);
126493d43e7eSAnthony Xu 
1265ba7fdd64SIgor Druzhinin     xen_destroy_ioreq_server(xen_domid, state->ioservid);
1266f1e43b60SAnthony PERARD     if (state->fres != NULL) {
1267f1e43b60SAnthony PERARD         xenforeignmemory_unmap_resource(xen_fmem, state->fres);
1268f1e43b60SAnthony PERARD     }
1269ba7fdd64SIgor Druzhinin 
127093d43e7eSAnthony Xu     xenevtchn_close(state->xce_handle);
127193d43e7eSAnthony Xu     xs_daemon_close(state->xenstore);
127293d43e7eSAnthony Xu }
127393d43e7eSAnthony Xu 
1274331b5189SIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
127593d43e7eSAnthony Xu static void xen_read_physmap(XenIOState *state)
127693d43e7eSAnthony Xu {
127793d43e7eSAnthony Xu     XenPhysmap *physmap = NULL;
127893d43e7eSAnthony Xu     unsigned int len, num, i;
127993d43e7eSAnthony Xu     char path[80], *value = NULL;
128093d43e7eSAnthony Xu     char **entries = NULL;
128193d43e7eSAnthony Xu 
128293d43e7eSAnthony Xu     snprintf(path, sizeof(path),
128393d43e7eSAnthony Xu             "/local/domain/0/device-model/%d/physmap", xen_domid);
128493d43e7eSAnthony Xu     entries = xs_directory(state->xenstore, 0, path, &num);
128593d43e7eSAnthony Xu     if (entries == NULL)
128693d43e7eSAnthony Xu         return;
128793d43e7eSAnthony Xu 
128893d43e7eSAnthony Xu     for (i = 0; i < num; i++) {
1289b21e2380SMarkus Armbruster         physmap = g_new(XenPhysmap, 1);
129093d43e7eSAnthony Xu         physmap->phys_offset = strtoull(entries[i], NULL, 16);
129193d43e7eSAnthony Xu         snprintf(path, sizeof(path),
129293d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
129393d43e7eSAnthony Xu                 xen_domid, entries[i]);
129493d43e7eSAnthony Xu         value = xs_read(state->xenstore, 0, path, &len);
129593d43e7eSAnthony Xu         if (value == NULL) {
129693d43e7eSAnthony Xu             g_free(physmap);
129793d43e7eSAnthony Xu             continue;
129893d43e7eSAnthony Xu         }
129993d43e7eSAnthony Xu         physmap->start_addr = strtoull(value, NULL, 16);
130093d43e7eSAnthony Xu         free(value);
130193d43e7eSAnthony Xu 
130293d43e7eSAnthony Xu         snprintf(path, sizeof(path),
130393d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/size",
130493d43e7eSAnthony Xu                 xen_domid, entries[i]);
130593d43e7eSAnthony Xu         value = xs_read(state->xenstore, 0, path, &len);
130693d43e7eSAnthony Xu         if (value == NULL) {
130793d43e7eSAnthony Xu             g_free(physmap);
130893d43e7eSAnthony Xu             continue;
130993d43e7eSAnthony Xu         }
131093d43e7eSAnthony Xu         physmap->size = strtoull(value, NULL, 16);
131193d43e7eSAnthony Xu         free(value);
131293d43e7eSAnthony Xu 
131393d43e7eSAnthony Xu         snprintf(path, sizeof(path),
131493d43e7eSAnthony Xu                 "/local/domain/0/device-model/%d/physmap/%s/name",
131593d43e7eSAnthony Xu                 xen_domid, entries[i]);
131693d43e7eSAnthony Xu         physmap->name = xs_read(state->xenstore, 0, path, &len);
131793d43e7eSAnthony Xu 
131804a8f72eSIgor Druzhinin         QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
131993d43e7eSAnthony Xu     }
132093d43e7eSAnthony Xu     free(entries);
132193d43e7eSAnthony Xu }
1322331b5189SIgor Druzhinin #else
1323331b5189SIgor Druzhinin static void xen_read_physmap(XenIOState *state)
1324331b5189SIgor Druzhinin {
1325331b5189SIgor Druzhinin }
1326331b5189SIgor Druzhinin #endif
132793d43e7eSAnthony Xu 
132893d43e7eSAnthony Xu static void xen_wakeup_notifier(Notifier *notifier, void *data)
132993d43e7eSAnthony Xu {
133093d43e7eSAnthony Xu     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
133193d43e7eSAnthony Xu }
133293d43e7eSAnthony Xu 
133371cec1edSPaul Durrant static int xen_map_ioreq_server(XenIOState *state)
133471cec1edSPaul Durrant {
1335d3c49ebbSPaul Durrant     void *addr = NULL;
133671cec1edSPaul Durrant     xen_pfn_t ioreq_pfn;
133771cec1edSPaul Durrant     xen_pfn_t bufioreq_pfn;
133871cec1edSPaul Durrant     evtchn_port_t bufioreq_evtchn;
133971cec1edSPaul Durrant     int rc;
134071cec1edSPaul Durrant 
1341d3c49ebbSPaul Durrant     /*
1342d3c49ebbSPaul Durrant      * Attempt to map using the resource API and fall back to normal
1343d3c49ebbSPaul Durrant      * foreign mapping if this is not supported.
1344d3c49ebbSPaul Durrant      */
1345d3c49ebbSPaul Durrant     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq != 0);
1346d3c49ebbSPaul Durrant     QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1347f1e43b60SAnthony PERARD     state->fres = xenforeignmemory_map_resource(xen_fmem, xen_domid,
1348d3c49ebbSPaul Durrant                                          XENMEM_resource_ioreq_server,
1349d3c49ebbSPaul Durrant                                          state->ioservid, 0, 2,
1350d3c49ebbSPaul Durrant                                          &addr,
1351d3c49ebbSPaul Durrant                                          PROT_READ | PROT_WRITE, 0);
1352f1e43b60SAnthony PERARD     if (state->fres != NULL) {
1353d3c49ebbSPaul Durrant         trace_xen_map_resource_ioreq(state->ioservid, addr);
1354d3c49ebbSPaul Durrant         state->buffered_io_page = addr;
1355d3c49ebbSPaul Durrant         state->shared_page = addr + TARGET_PAGE_SIZE;
1356d3c49ebbSPaul Durrant     } else if (errno != EOPNOTSUPP) {
1357d3c49ebbSPaul Durrant         error_report("failed to map ioreq server resources: error %d handle=%p",
1358d3c49ebbSPaul Durrant                      errno, xen_xc);
1359d3c49ebbSPaul Durrant         return -1;
1360d3c49ebbSPaul Durrant     }
1361d3c49ebbSPaul Durrant 
136271cec1edSPaul Durrant     rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1363d3c49ebbSPaul Durrant                                    (state->shared_page == NULL) ?
1364d3c49ebbSPaul Durrant                                    &ioreq_pfn : NULL,
1365d3c49ebbSPaul Durrant                                    (state->buffered_io_page == NULL) ?
1366d3c49ebbSPaul Durrant                                    &bufioreq_pfn : NULL,
136771cec1edSPaul Durrant                                    &bufioreq_evtchn);
136871cec1edSPaul Durrant     if (rc < 0) {
136971cec1edSPaul Durrant         error_report("failed to get ioreq server info: error %d handle=%p",
137071cec1edSPaul Durrant                      errno, xen_xc);
137171cec1edSPaul Durrant         return rc;
137271cec1edSPaul Durrant     }
137371cec1edSPaul Durrant 
1374d3c49ebbSPaul Durrant     if (state->shared_page == NULL) {
137571cec1edSPaul Durrant         DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
137671cec1edSPaul Durrant 
137771cec1edSPaul Durrant         state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
137871cec1edSPaul Durrant                                                   PROT_READ | PROT_WRITE,
137971cec1edSPaul Durrant                                                   1, &ioreq_pfn, NULL);
138071cec1edSPaul Durrant         if (state->shared_page == NULL) {
138171cec1edSPaul Durrant             error_report("map shared IO page returned error %d handle=%p",
138271cec1edSPaul Durrant                          errno, xen_xc);
138371cec1edSPaul Durrant         }
1384d3c49ebbSPaul Durrant     }
1385d3c49ebbSPaul Durrant 
1386d3c49ebbSPaul Durrant     if (state->buffered_io_page == NULL) {
1387d3c49ebbSPaul Durrant         DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
138871cec1edSPaul Durrant 
138971cec1edSPaul Durrant         state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
139071cec1edSPaul Durrant                                                        PROT_READ | PROT_WRITE,
1391d3c49ebbSPaul Durrant                                                        1, &bufioreq_pfn,
1392d3c49ebbSPaul Durrant                                                        NULL);
139371cec1edSPaul Durrant         if (state->buffered_io_page == NULL) {
139471cec1edSPaul Durrant             error_report("map buffered IO page returned error %d", errno);
139571cec1edSPaul Durrant             return -1;
139671cec1edSPaul Durrant         }
1397d3c49ebbSPaul Durrant     }
1398d3c49ebbSPaul Durrant 
1399d3c49ebbSPaul Durrant     if (state->shared_page == NULL || state->buffered_io_page == NULL) {
1400d3c49ebbSPaul Durrant         return -1;
1401d3c49ebbSPaul Durrant     }
1402d3c49ebbSPaul Durrant 
1403d3c49ebbSPaul Durrant     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
140471cec1edSPaul Durrant 
140571cec1edSPaul Durrant     state->bufioreq_remote_port = bufioreq_evtchn;
140671cec1edSPaul Durrant 
140771cec1edSPaul Durrant     return 0;
140871cec1edSPaul Durrant }
140971cec1edSPaul Durrant 
14105650ac00SPhilippe Mathieu-Daudé void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
141193d43e7eSAnthony Xu {
14120e11fc69SLike Xu     MachineState *ms = MACHINE(pcms);
14130e11fc69SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
141493d43e7eSAnthony Xu     int i, rc;
141593d43e7eSAnthony Xu     xen_pfn_t ioreq_pfn;
141693d43e7eSAnthony Xu     XenIOState *state;
141793d43e7eSAnthony Xu 
1418b21e2380SMarkus Armbruster     state = g_new0(XenIOState, 1);
141993d43e7eSAnthony Xu 
142093d43e7eSAnthony Xu     state->xce_handle = xenevtchn_open(NULL, 0);
142193d43e7eSAnthony Xu     if (state->xce_handle == NULL) {
142293d43e7eSAnthony Xu         perror("xen: event channel open");
142393d43e7eSAnthony Xu         goto err;
142493d43e7eSAnthony Xu     }
142593d43e7eSAnthony Xu 
142693d43e7eSAnthony Xu     state->xenstore = xs_daemon_open();
142793d43e7eSAnthony Xu     if (state->xenstore == NULL) {
142893d43e7eSAnthony Xu         perror("xen: xenstore open");
142993d43e7eSAnthony Xu         goto err;
143093d43e7eSAnthony Xu     }
143193d43e7eSAnthony Xu 
143293d43e7eSAnthony Xu     xen_create_ioreq_server(xen_domid, &state->ioservid);
143393d43e7eSAnthony Xu 
143493d43e7eSAnthony Xu     state->exit.notify = xen_exit_notifier;
143593d43e7eSAnthony Xu     qemu_add_exit_notifier(&state->exit);
143693d43e7eSAnthony Xu 
143793d43e7eSAnthony Xu     state->suspend.notify = xen_suspend_notifier;
143893d43e7eSAnthony Xu     qemu_register_suspend_notifier(&state->suspend);
143993d43e7eSAnthony Xu 
144093d43e7eSAnthony Xu     state->wakeup.notify = xen_wakeup_notifier;
144193d43e7eSAnthony Xu     qemu_register_wakeup_notifier(&state->wakeup);
144293d43e7eSAnthony Xu 
144346ea94caSDaniel Henrique Barboza     /*
144446ea94caSDaniel Henrique Barboza      * Register wake-up support in QMP query-current-machine API
144546ea94caSDaniel Henrique Barboza      */
144646ea94caSDaniel Henrique Barboza     qemu_register_wakeup_support();
144746ea94caSDaniel Henrique Barboza 
144871cec1edSPaul Durrant     rc = xen_map_ioreq_server(state);
144993d43e7eSAnthony Xu     if (rc < 0) {
145093d43e7eSAnthony Xu         goto err;
145193d43e7eSAnthony Xu     }
145293d43e7eSAnthony Xu 
145393d43e7eSAnthony Xu     rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
145493d43e7eSAnthony Xu     if (!rc) {
145593d43e7eSAnthony Xu         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
145693d43e7eSAnthony Xu         state->shared_vmport_page =
145793d43e7eSAnthony Xu             xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
145893d43e7eSAnthony Xu                                  1, &ioreq_pfn, NULL);
145993d43e7eSAnthony Xu         if (state->shared_vmport_page == NULL) {
146093d43e7eSAnthony Xu             error_report("map shared vmport IO page returned error %d handle=%p",
146193d43e7eSAnthony Xu                          errno, xen_xc);
146293d43e7eSAnthony Xu             goto err;
146393d43e7eSAnthony Xu         }
146493d43e7eSAnthony Xu     } else if (rc != -ENOSYS) {
146593d43e7eSAnthony Xu         error_report("get vmport regs pfn returned error %d, rc=%d",
146693d43e7eSAnthony Xu                      errno, rc);
146793d43e7eSAnthony Xu         goto err;
146893d43e7eSAnthony Xu     }
146993d43e7eSAnthony Xu 
147093d43e7eSAnthony Xu     /* Note: cpus is empty at this point in init */
1471b21e2380SMarkus Armbruster     state->cpu_by_vcpu_id = g_new0(CPUState *, max_cpus);
147293d43e7eSAnthony Xu 
147393d43e7eSAnthony Xu     rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
147493d43e7eSAnthony Xu     if (rc < 0) {
147593d43e7eSAnthony Xu         error_report("failed to enable ioreq server info: error %d handle=%p",
147693d43e7eSAnthony Xu                      errno, xen_xc);
147793d43e7eSAnthony Xu         goto err;
147893d43e7eSAnthony Xu     }
147993d43e7eSAnthony Xu 
1480b21e2380SMarkus Armbruster     state->ioreq_local_port = g_new0(evtchn_port_t, max_cpus);
148193d43e7eSAnthony Xu 
148293d43e7eSAnthony Xu     /* FIXME: how about if we overflow the page here? */
148393d43e7eSAnthony Xu     for (i = 0; i < max_cpus; i++) {
148493d43e7eSAnthony Xu         rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
148593d43e7eSAnthony Xu                                         xen_vcpu_eport(state->shared_page, i));
148693d43e7eSAnthony Xu         if (rc == -1) {
148793d43e7eSAnthony Xu             error_report("shared evtchn %d bind error %d", i, errno);
148893d43e7eSAnthony Xu             goto err;
148993d43e7eSAnthony Xu         }
149093d43e7eSAnthony Xu         state->ioreq_local_port[i] = rc;
149193d43e7eSAnthony Xu     }
149293d43e7eSAnthony Xu 
149393d43e7eSAnthony Xu     rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
149471cec1edSPaul Durrant                                     state->bufioreq_remote_port);
149593d43e7eSAnthony Xu     if (rc == -1) {
149693d43e7eSAnthony Xu         error_report("buffered evtchn bind error %d", errno);
149793d43e7eSAnthony Xu         goto err;
149893d43e7eSAnthony Xu     }
149993d43e7eSAnthony Xu     state->bufioreq_local_port = rc;
150093d43e7eSAnthony Xu 
150193d43e7eSAnthony Xu     /* Init RAM management */
1502331b5189SIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
150393d43e7eSAnthony Xu     xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1504331b5189SIgor Druzhinin #else
1505331b5189SIgor Druzhinin     xen_map_cache_init(NULL, state);
1506331b5189SIgor Druzhinin #endif
150786378b29SPaolo Bonzini     xen_ram_init(pcms, ms->ram_size, ram_memory);
150893d43e7eSAnthony Xu 
150993d43e7eSAnthony Xu     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
151093d43e7eSAnthony Xu 
151193d43e7eSAnthony Xu     state->memory_listener = xen_memory_listener;
151293d43e7eSAnthony Xu     memory_listener_register(&state->memory_listener, &address_space_memory);
151393d43e7eSAnthony Xu     state->log_for_dirtybit = NULL;
151493d43e7eSAnthony Xu 
151593d43e7eSAnthony Xu     state->io_listener = xen_io_listener;
151693d43e7eSAnthony Xu     memory_listener_register(&state->io_listener, &address_space_io);
151793d43e7eSAnthony Xu 
151893d43e7eSAnthony Xu     state->device_listener = xen_device_listener;
1519dfb6578dSPaul Durrant     QLIST_INIT(&state->dev_list);
152093d43e7eSAnthony Xu     device_listener_register(&state->device_listener);
152193d43e7eSAnthony Xu 
1522108f7bbaSPaul Durrant     xen_bus_init();
1523108f7bbaSPaul Durrant 
152493d43e7eSAnthony Xu     /* Initialize backend core & drivers */
152593d43e7eSAnthony Xu     if (xen_be_init() != 0) {
152693d43e7eSAnthony Xu         error_report("xen backend core setup failed");
152793d43e7eSAnthony Xu         goto err;
152893d43e7eSAnthony Xu     }
152993d43e7eSAnthony Xu     xen_be_register_common();
153004a8f72eSIgor Druzhinin 
153104a8f72eSIgor Druzhinin     QLIST_INIT(&xen_physmap);
153293d43e7eSAnthony Xu     xen_read_physmap(state);
153393d43e7eSAnthony Xu 
153493d43e7eSAnthony Xu     /* Disable ACPI build because Xen handles it */
153593d43e7eSAnthony Xu     pcms->acpi_build_enabled = false;
153693d43e7eSAnthony Xu 
153793d43e7eSAnthony Xu     return;
153893d43e7eSAnthony Xu 
153993d43e7eSAnthony Xu err:
154093d43e7eSAnthony Xu     error_report("xen hardware virtual machine initialisation failed");
154193d43e7eSAnthony Xu     exit(1);
154293d43e7eSAnthony Xu }
154393d43e7eSAnthony Xu 
154493d43e7eSAnthony Xu void destroy_hvm_domain(bool reboot)
154593d43e7eSAnthony Xu {
154693d43e7eSAnthony Xu     xc_interface *xc_handle;
154793d43e7eSAnthony Xu     int sts;
15486b47c2aaSIan Jackson     int rc;
154993d43e7eSAnthony Xu 
155074aae6bfSIan Jackson     unsigned int reason = reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff;
155174aae6bfSIan Jackson 
15526b47c2aaSIan Jackson     if (xen_dmod) {
15536b47c2aaSIan Jackson         rc = xendevicemodel_shutdown(xen_dmod, xen_domid, reason);
15546b47c2aaSIan Jackson         if (!rc) {
15556b47c2aaSIan Jackson             return;
15566b47c2aaSIan Jackson         }
15576b47c2aaSIan Jackson         if (errno != ENOTTY /* old Xen */) {
15586b47c2aaSIan Jackson             perror("xendevicemodel_shutdown failed");
15596b47c2aaSIan Jackson         }
15606b47c2aaSIan Jackson         /* well, try the old thing then */
15616b47c2aaSIan Jackson     }
15626b47c2aaSIan Jackson 
156393d43e7eSAnthony Xu     xc_handle = xc_interface_open(0, 0, 0);
156493d43e7eSAnthony Xu     if (xc_handle == NULL) {
156593d43e7eSAnthony Xu         fprintf(stderr, "Cannot acquire xenctrl handle\n");
156693d43e7eSAnthony Xu     } else {
156774aae6bfSIan Jackson         sts = xc_domain_shutdown(xc_handle, xen_domid, reason);
156893d43e7eSAnthony Xu         if (sts != 0) {
156993d43e7eSAnthony Xu             fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
157093d43e7eSAnthony Xu                     "sts %d, %s\n", reboot ? "reboot" : "poweroff",
157193d43e7eSAnthony Xu                     sts, strerror(errno));
157293d43e7eSAnthony Xu         } else {
157393d43e7eSAnthony Xu             fprintf(stderr, "Issued domain %d %s\n", xen_domid,
157493d43e7eSAnthony Xu                     reboot ? "reboot" : "poweroff");
157593d43e7eSAnthony Xu         }
157693d43e7eSAnthony Xu         xc_interface_close(xc_handle);
157793d43e7eSAnthony Xu     }
157893d43e7eSAnthony Xu }
157993d43e7eSAnthony Xu 
158093d43e7eSAnthony Xu void xen_register_framebuffer(MemoryRegion *mr)
158193d43e7eSAnthony Xu {
158293d43e7eSAnthony Xu     framebuffer = mr;
158393d43e7eSAnthony Xu }
158493d43e7eSAnthony Xu 
158593d43e7eSAnthony Xu void xen_shutdown_fatal_error(const char *fmt, ...)
158693d43e7eSAnthony Xu {
158793d43e7eSAnthony Xu     va_list ap;
158893d43e7eSAnthony Xu 
158993d43e7eSAnthony Xu     va_start(ap, fmt);
159093d43e7eSAnthony Xu     vfprintf(stderr, fmt, ap);
159193d43e7eSAnthony Xu     va_end(ap);
159293d43e7eSAnthony Xu     fprintf(stderr, "Will destroy the domain.\n");
159393d43e7eSAnthony Xu     /* destroy the domain */
1594cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
159593d43e7eSAnthony Xu }
159693d43e7eSAnthony Xu 
159793d43e7eSAnthony Xu void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
159893d43e7eSAnthony Xu {
159993d43e7eSAnthony Xu     if (unlikely(xen_in_migration)) {
160093d43e7eSAnthony Xu         int rc;
160193d43e7eSAnthony Xu         ram_addr_t start_pfn, nb_pages;
160293d43e7eSAnthony Xu 
160304a8f72eSIgor Druzhinin         start = xen_phys_offset_to_gaddr(start, length);
160404a8f72eSIgor Druzhinin 
160593d43e7eSAnthony Xu         if (length == 0) {
160693d43e7eSAnthony Xu             length = TARGET_PAGE_SIZE;
160793d43e7eSAnthony Xu         }
160893d43e7eSAnthony Xu         start_pfn = start >> TARGET_PAGE_BITS;
160993d43e7eSAnthony Xu         nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
161093d43e7eSAnthony Xu             - start_pfn;
161193d43e7eSAnthony Xu         rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
161293d43e7eSAnthony Xu         if (rc) {
161393d43e7eSAnthony Xu             fprintf(stderr,
161493d43e7eSAnthony Xu                     "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
16157cdcca72SRoss Lagerwall                     __func__, start, nb_pages, errno, strerror(errno));
161693d43e7eSAnthony Xu         }
161793d43e7eSAnthony Xu     }
161893d43e7eSAnthony Xu }
161993d43e7eSAnthony Xu 
162093d43e7eSAnthony Xu void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
162193d43e7eSAnthony Xu {
162293d43e7eSAnthony Xu     if (enable) {
162363b41db4SHyman Huang(黄勇)         memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION);
162493d43e7eSAnthony Xu     } else {
162563b41db4SHyman Huang(黄勇)         memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION);
162693d43e7eSAnthony Xu     }
162793d43e7eSAnthony Xu }
1628