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" 1293d43e7eSAnthony Xu 1393d43e7eSAnthony Xu #include "cpu.h" 1493d43e7eSAnthony Xu #include "hw/pci/pci.h" 1593d43e7eSAnthony Xu #include "hw/i386/pc.h" 1693d43e7eSAnthony Xu #include "hw/i386/apic-msidef.h" 1793d43e7eSAnthony Xu #include "hw/xen/xen_common.h" 1893d43e7eSAnthony Xu #include "hw/xen/xen_backend.h" 1993d43e7eSAnthony Xu #include "qmp-commands.h" 2093d43e7eSAnthony Xu 2193d43e7eSAnthony Xu #include "qemu/error-report.h" 2293d43e7eSAnthony Xu #include "qemu/range.h" 2393d43e7eSAnthony Xu #include "sysemu/xen-mapcache.h" 2493d43e7eSAnthony Xu #include "trace.h" 2593d43e7eSAnthony Xu #include "exec/address-spaces.h" 2693d43e7eSAnthony Xu 2793d43e7eSAnthony Xu #include <xen/hvm/ioreq.h> 2893d43e7eSAnthony Xu #include <xen/hvm/params.h> 2993d43e7eSAnthony Xu #include <xen/hvm/e820.h> 3093d43e7eSAnthony Xu 3193d43e7eSAnthony Xu //#define DEBUG_XEN_HVM 3293d43e7eSAnthony Xu 3393d43e7eSAnthony Xu #ifdef DEBUG_XEN_HVM 3493d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \ 3593d43e7eSAnthony Xu do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0) 3693d43e7eSAnthony Xu #else 3793d43e7eSAnthony Xu #define DPRINTF(fmt, ...) \ 3893d43e7eSAnthony Xu do { } while (0) 3993d43e7eSAnthony Xu #endif 4093d43e7eSAnthony Xu 4193d43e7eSAnthony Xu static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi; 4293d43e7eSAnthony Xu static MemoryRegion *framebuffer; 4393d43e7eSAnthony Xu static bool xen_in_migration; 4493d43e7eSAnthony Xu 4593d43e7eSAnthony Xu /* Compatibility with older version */ 4693d43e7eSAnthony Xu 4793d43e7eSAnthony Xu /* This allows QEMU to build on a system that has Xen 4.5 or earlier 4893d43e7eSAnthony Xu * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h 4993d43e7eSAnthony Xu * needs to be included before this block and hw/xen/xen_common.h needs to 5093d43e7eSAnthony Xu * be included before xen/hvm/ioreq.h 5193d43e7eSAnthony Xu */ 5293d43e7eSAnthony Xu #ifndef IOREQ_TYPE_VMWARE_PORT 5393d43e7eSAnthony Xu #define IOREQ_TYPE_VMWARE_PORT 3 5493d43e7eSAnthony Xu struct vmware_regs { 5593d43e7eSAnthony Xu uint32_t esi; 5693d43e7eSAnthony Xu uint32_t edi; 5793d43e7eSAnthony Xu uint32_t ebx; 5893d43e7eSAnthony Xu uint32_t ecx; 5993d43e7eSAnthony Xu uint32_t edx; 6093d43e7eSAnthony Xu }; 6193d43e7eSAnthony Xu typedef struct vmware_regs vmware_regs_t; 6293d43e7eSAnthony Xu 6393d43e7eSAnthony Xu struct shared_vmport_iopage { 6493d43e7eSAnthony Xu struct vmware_regs vcpu_vmport_regs[1]; 6593d43e7eSAnthony Xu }; 6693d43e7eSAnthony Xu typedef struct shared_vmport_iopage shared_vmport_iopage_t; 6793d43e7eSAnthony Xu #endif 6893d43e7eSAnthony Xu 6993d43e7eSAnthony Xu static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i) 7093d43e7eSAnthony Xu { 7193d43e7eSAnthony Xu return shared_page->vcpu_ioreq[i].vp_eport; 7293d43e7eSAnthony Xu } 7393d43e7eSAnthony Xu static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu) 7493d43e7eSAnthony Xu { 7593d43e7eSAnthony Xu return &shared_page->vcpu_ioreq[vcpu]; 7693d43e7eSAnthony Xu } 7793d43e7eSAnthony Xu 7893d43e7eSAnthony Xu #define BUFFER_IO_MAX_DELAY 100 7993d43e7eSAnthony Xu 8093d43e7eSAnthony Xu typedef struct XenPhysmap { 8193d43e7eSAnthony Xu hwaddr start_addr; 8293d43e7eSAnthony Xu ram_addr_t size; 8393d43e7eSAnthony Xu const char *name; 8493d43e7eSAnthony Xu hwaddr phys_offset; 8593d43e7eSAnthony Xu 8693d43e7eSAnthony Xu QLIST_ENTRY(XenPhysmap) list; 8793d43e7eSAnthony Xu } XenPhysmap; 8893d43e7eSAnthony Xu 8993d43e7eSAnthony Xu typedef struct XenIOState { 9093d43e7eSAnthony Xu ioservid_t ioservid; 9193d43e7eSAnthony Xu shared_iopage_t *shared_page; 9293d43e7eSAnthony Xu shared_vmport_iopage_t *shared_vmport_page; 9393d43e7eSAnthony Xu buffered_iopage_t *buffered_io_page; 9493d43e7eSAnthony Xu QEMUTimer *buffered_io_timer; 9593d43e7eSAnthony Xu CPUState **cpu_by_vcpu_id; 9693d43e7eSAnthony Xu /* the evtchn port for polling the notification, */ 9793d43e7eSAnthony Xu evtchn_port_t *ioreq_local_port; 9893d43e7eSAnthony Xu /* evtchn local port for buffered io */ 9993d43e7eSAnthony Xu evtchn_port_t bufioreq_local_port; 10093d43e7eSAnthony Xu /* the evtchn fd for polling */ 10193d43e7eSAnthony Xu xenevtchn_handle *xce_handle; 10293d43e7eSAnthony Xu /* which vcpu we are serving */ 10393d43e7eSAnthony Xu int send_vcpu; 10493d43e7eSAnthony Xu 10593d43e7eSAnthony Xu struct xs_handle *xenstore; 10693d43e7eSAnthony Xu MemoryListener memory_listener; 10793d43e7eSAnthony Xu MemoryListener io_listener; 10893d43e7eSAnthony Xu DeviceListener device_listener; 10993d43e7eSAnthony Xu QLIST_HEAD(, XenPhysmap) physmap; 11093d43e7eSAnthony Xu hwaddr free_phys_offset; 11193d43e7eSAnthony Xu const XenPhysmap *log_for_dirtybit; 11293d43e7eSAnthony Xu 11393d43e7eSAnthony Xu Notifier exit; 11493d43e7eSAnthony Xu Notifier suspend; 11593d43e7eSAnthony Xu Notifier wakeup; 11693d43e7eSAnthony Xu } XenIOState; 11793d43e7eSAnthony Xu 11893d43e7eSAnthony Xu /* Xen specific function for piix pci */ 11993d43e7eSAnthony Xu 12093d43e7eSAnthony Xu int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num) 12193d43e7eSAnthony Xu { 12293d43e7eSAnthony Xu return irq_num + ((pci_dev->devfn >> 3) << 2); 12393d43e7eSAnthony Xu } 12493d43e7eSAnthony Xu 12593d43e7eSAnthony Xu void xen_piix3_set_irq(void *opaque, int irq_num, int level) 12693d43e7eSAnthony Xu { 12793d43e7eSAnthony Xu xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2, 12893d43e7eSAnthony Xu irq_num & 3, level); 12993d43e7eSAnthony Xu } 13093d43e7eSAnthony Xu 13193d43e7eSAnthony Xu void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len) 13293d43e7eSAnthony Xu { 13393d43e7eSAnthony Xu int i; 13493d43e7eSAnthony Xu 13593d43e7eSAnthony Xu /* Scan for updates to PCI link routes (0x60-0x63). */ 13693d43e7eSAnthony Xu for (i = 0; i < len; i++) { 13793d43e7eSAnthony Xu uint8_t v = (val >> (8 * i)) & 0xff; 13893d43e7eSAnthony Xu if (v & 0x80) { 13993d43e7eSAnthony Xu v = 0; 14093d43e7eSAnthony Xu } 14193d43e7eSAnthony Xu v &= 0xf; 14293d43e7eSAnthony Xu if (((address + i) >= 0x60) && ((address + i) <= 0x63)) { 14393d43e7eSAnthony Xu xen_set_pci_link_route(xen_domid, address + i - 0x60, v); 14493d43e7eSAnthony Xu } 14593d43e7eSAnthony Xu } 14693d43e7eSAnthony Xu } 14793d43e7eSAnthony Xu 14893d43e7eSAnthony Xu int xen_is_pirq_msi(uint32_t msi_data) 14993d43e7eSAnthony Xu { 15093d43e7eSAnthony Xu /* If vector is 0, the msi is remapped into a pirq, passed as 15193d43e7eSAnthony Xu * dest_id. 15293d43e7eSAnthony Xu */ 15393d43e7eSAnthony Xu return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0; 15493d43e7eSAnthony Xu } 15593d43e7eSAnthony Xu 15693d43e7eSAnthony Xu void xen_hvm_inject_msi(uint64_t addr, uint32_t data) 15793d43e7eSAnthony Xu { 15893d43e7eSAnthony Xu xen_inject_msi(xen_domid, addr, data); 15993d43e7eSAnthony Xu } 16093d43e7eSAnthony Xu 16193d43e7eSAnthony Xu static void xen_suspend_notifier(Notifier *notifier, void *data) 16293d43e7eSAnthony Xu { 16393d43e7eSAnthony Xu xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3); 16493d43e7eSAnthony Xu } 16593d43e7eSAnthony Xu 16693d43e7eSAnthony Xu /* Xen Interrupt Controller */ 16793d43e7eSAnthony Xu 16893d43e7eSAnthony Xu static void xen_set_irq(void *opaque, int irq, int level) 16993d43e7eSAnthony Xu { 17093d43e7eSAnthony Xu xen_set_isa_irq_level(xen_domid, irq, level); 17193d43e7eSAnthony Xu } 17293d43e7eSAnthony Xu 17393d43e7eSAnthony Xu qemu_irq *xen_interrupt_controller_init(void) 17493d43e7eSAnthony Xu { 17593d43e7eSAnthony Xu return qemu_allocate_irqs(xen_set_irq, NULL, 16); 17693d43e7eSAnthony Xu } 17793d43e7eSAnthony Xu 17893d43e7eSAnthony Xu /* Memory Ops */ 17993d43e7eSAnthony Xu 18093d43e7eSAnthony Xu static void xen_ram_init(PCMachineState *pcms, 18193d43e7eSAnthony Xu ram_addr_t ram_size, MemoryRegion **ram_memory_p) 18293d43e7eSAnthony Xu { 18393d43e7eSAnthony Xu MemoryRegion *sysmem = get_system_memory(); 18493d43e7eSAnthony Xu ram_addr_t block_len; 1854ccd89d2SMarc-André Lureau uint64_t user_lowmem = object_property_get_uint(qdev_get_machine(), 18693d43e7eSAnthony Xu PC_MACHINE_MAX_RAM_BELOW_4G, 18793d43e7eSAnthony Xu &error_abort); 18893d43e7eSAnthony Xu 18993d43e7eSAnthony Xu /* Handle the machine opt max-ram-below-4g. It is basically doing 19093d43e7eSAnthony Xu * min(xen limit, user limit). 19193d43e7eSAnthony Xu */ 19293d43e7eSAnthony Xu if (!user_lowmem) { 19393d43e7eSAnthony Xu user_lowmem = HVM_BELOW_4G_RAM_END; /* default */ 19493d43e7eSAnthony Xu } 19593d43e7eSAnthony Xu if (HVM_BELOW_4G_RAM_END <= user_lowmem) { 19693d43e7eSAnthony Xu user_lowmem = HVM_BELOW_4G_RAM_END; 19793d43e7eSAnthony Xu } 19893d43e7eSAnthony Xu 19993d43e7eSAnthony Xu if (ram_size >= user_lowmem) { 20093d43e7eSAnthony Xu pcms->above_4g_mem_size = ram_size - user_lowmem; 20193d43e7eSAnthony Xu pcms->below_4g_mem_size = user_lowmem; 20293d43e7eSAnthony Xu } else { 20393d43e7eSAnthony Xu pcms->above_4g_mem_size = 0; 20493d43e7eSAnthony Xu pcms->below_4g_mem_size = ram_size; 20593d43e7eSAnthony Xu } 20693d43e7eSAnthony Xu if (!pcms->above_4g_mem_size) { 20793d43e7eSAnthony Xu block_len = ram_size; 20893d43e7eSAnthony Xu } else { 20993d43e7eSAnthony Xu /* 21093d43e7eSAnthony Xu * Xen does not allocate the memory continuously, it keeps a 21193d43e7eSAnthony Xu * hole of the size computed above or passed in. 21293d43e7eSAnthony Xu */ 21393d43e7eSAnthony Xu block_len = (1ULL << 32) + pcms->above_4g_mem_size; 21493d43e7eSAnthony Xu } 21598a99ce0SPeter Maydell memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len, 21693d43e7eSAnthony Xu &error_fatal); 21793d43e7eSAnthony Xu *ram_memory_p = &ram_memory; 21893d43e7eSAnthony Xu 21993d43e7eSAnthony Xu memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k", 22093d43e7eSAnthony Xu &ram_memory, 0, 0xa0000); 22193d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0, &ram_640k); 22293d43e7eSAnthony Xu /* Skip of the VGA IO memory space, it will be registered later by the VGA 22393d43e7eSAnthony Xu * emulated device. 22493d43e7eSAnthony Xu * 22593d43e7eSAnthony Xu * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load 22693d43e7eSAnthony Xu * the Options ROM, so it is registered here as RAM. 22793d43e7eSAnthony Xu */ 22893d43e7eSAnthony Xu memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", 22993d43e7eSAnthony Xu &ram_memory, 0xc0000, 23093d43e7eSAnthony Xu pcms->below_4g_mem_size - 0xc0000); 23193d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0xc0000, &ram_lo); 23293d43e7eSAnthony Xu if (pcms->above_4g_mem_size > 0) { 23393d43e7eSAnthony Xu memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", 23493d43e7eSAnthony Xu &ram_memory, 0x100000000ULL, 23593d43e7eSAnthony Xu pcms->above_4g_mem_size); 23693d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi); 23793d43e7eSAnthony Xu } 23893d43e7eSAnthony Xu } 23993d43e7eSAnthony Xu 24093d43e7eSAnthony Xu void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr, 24193d43e7eSAnthony Xu Error **errp) 24293d43e7eSAnthony Xu { 24393d43e7eSAnthony Xu unsigned long nr_pfn; 24493d43e7eSAnthony Xu xen_pfn_t *pfn_list; 24593d43e7eSAnthony Xu int i; 24693d43e7eSAnthony Xu 24793d43e7eSAnthony Xu if (runstate_check(RUN_STATE_INMIGRATE)) { 24893d43e7eSAnthony Xu /* RAM already populated in Xen */ 24993d43e7eSAnthony Xu fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT 25093d43e7eSAnthony Xu " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n", 25193d43e7eSAnthony Xu __func__, size, ram_addr); 25293d43e7eSAnthony Xu return; 25393d43e7eSAnthony Xu } 25493d43e7eSAnthony Xu 25593d43e7eSAnthony Xu if (mr == &ram_memory) { 25693d43e7eSAnthony Xu return; 25793d43e7eSAnthony Xu } 25893d43e7eSAnthony Xu 25993d43e7eSAnthony Xu trace_xen_ram_alloc(ram_addr, size); 26093d43e7eSAnthony Xu 26193d43e7eSAnthony Xu nr_pfn = size >> TARGET_PAGE_BITS; 26293d43e7eSAnthony Xu pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn); 26393d43e7eSAnthony Xu 26493d43e7eSAnthony Xu for (i = 0; i < nr_pfn; i++) { 26593d43e7eSAnthony Xu pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i; 26693d43e7eSAnthony Xu } 26793d43e7eSAnthony Xu 26893d43e7eSAnthony Xu if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) { 26993d43e7eSAnthony Xu error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT, 27093d43e7eSAnthony Xu ram_addr); 27193d43e7eSAnthony Xu } 27293d43e7eSAnthony Xu 27393d43e7eSAnthony Xu g_free(pfn_list); 27493d43e7eSAnthony Xu } 27593d43e7eSAnthony Xu 27693d43e7eSAnthony Xu static XenPhysmap *get_physmapping(XenIOState *state, 27793d43e7eSAnthony Xu hwaddr start_addr, ram_addr_t size) 27893d43e7eSAnthony Xu { 27993d43e7eSAnthony Xu XenPhysmap *physmap = NULL; 28093d43e7eSAnthony Xu 28193d43e7eSAnthony Xu start_addr &= TARGET_PAGE_MASK; 28293d43e7eSAnthony Xu 28393d43e7eSAnthony Xu QLIST_FOREACH(physmap, &state->physmap, list) { 28493d43e7eSAnthony Xu if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) { 28593d43e7eSAnthony Xu return physmap; 28693d43e7eSAnthony Xu } 28793d43e7eSAnthony Xu } 28893d43e7eSAnthony Xu return NULL; 28993d43e7eSAnthony Xu } 29093d43e7eSAnthony Xu 29193d43e7eSAnthony Xu static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr, 29293d43e7eSAnthony Xu ram_addr_t size, void *opaque) 29393d43e7eSAnthony Xu { 29493d43e7eSAnthony Xu hwaddr addr = start_addr & TARGET_PAGE_MASK; 29593d43e7eSAnthony Xu XenIOState *xen_io_state = opaque; 29693d43e7eSAnthony Xu XenPhysmap *physmap = NULL; 29793d43e7eSAnthony Xu 29893d43e7eSAnthony Xu QLIST_FOREACH(physmap, &xen_io_state->physmap, list) { 29993d43e7eSAnthony Xu if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) { 30093d43e7eSAnthony Xu return physmap->start_addr; 30193d43e7eSAnthony Xu } 30293d43e7eSAnthony Xu } 30393d43e7eSAnthony Xu 30493d43e7eSAnthony Xu return start_addr; 30593d43e7eSAnthony Xu } 30693d43e7eSAnthony Xu 307*697b66d0SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap) 308*697b66d0SIgor Druzhinin { 309*697b66d0SIgor Druzhinin char path[80], value[17]; 310*697b66d0SIgor Druzhinin 311*697b66d0SIgor Druzhinin snprintf(path, sizeof(path), 312*697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr", 313*697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset); 314*697b66d0SIgor Druzhinin snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr); 315*697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path, value, strlen(value))) { 316*697b66d0SIgor Druzhinin return -1; 317*697b66d0SIgor Druzhinin } 318*697b66d0SIgor Druzhinin snprintf(path, sizeof(path), 319*697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size", 320*697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset); 321*697b66d0SIgor Druzhinin snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size); 322*697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path, value, strlen(value))) { 323*697b66d0SIgor Druzhinin return -1; 324*697b66d0SIgor Druzhinin } 325*697b66d0SIgor Druzhinin if (physmap->name) { 326*697b66d0SIgor Druzhinin snprintf(path, sizeof(path), 327*697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name", 328*697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset); 329*697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path, 330*697b66d0SIgor Druzhinin physmap->name, strlen(physmap->name))) { 331*697b66d0SIgor Druzhinin return -1; 332*697b66d0SIgor Druzhinin } 333*697b66d0SIgor Druzhinin } 334*697b66d0SIgor Druzhinin return 0; 335*697b66d0SIgor Druzhinin } 336*697b66d0SIgor Druzhinin 33793d43e7eSAnthony Xu static int xen_add_to_physmap(XenIOState *state, 33893d43e7eSAnthony Xu hwaddr start_addr, 33993d43e7eSAnthony Xu ram_addr_t size, 34093d43e7eSAnthony Xu MemoryRegion *mr, 34193d43e7eSAnthony Xu hwaddr offset_within_region) 34293d43e7eSAnthony Xu { 34393d43e7eSAnthony Xu unsigned long i = 0; 34493d43e7eSAnthony Xu int rc = 0; 34593d43e7eSAnthony Xu XenPhysmap *physmap = NULL; 34693d43e7eSAnthony Xu hwaddr pfn, start_gpfn; 34793d43e7eSAnthony Xu hwaddr phys_offset = memory_region_get_ram_addr(mr); 34893d43e7eSAnthony Xu const char *mr_name; 34993d43e7eSAnthony Xu 35093d43e7eSAnthony Xu if (get_physmapping(state, start_addr, size)) { 35193d43e7eSAnthony Xu return 0; 35293d43e7eSAnthony Xu } 35393d43e7eSAnthony Xu if (size <= 0) { 35493d43e7eSAnthony Xu return -1; 35593d43e7eSAnthony Xu } 35693d43e7eSAnthony Xu 35793d43e7eSAnthony Xu /* Xen can only handle a single dirty log region for now and we want 35893d43e7eSAnthony Xu * the linear framebuffer to be that region. 35993d43e7eSAnthony Xu * Avoid tracking any regions that is not videoram and avoid tracking 36093d43e7eSAnthony Xu * the legacy vga region. */ 36193d43e7eSAnthony Xu if (mr == framebuffer && start_addr > 0xbffff) { 36293d43e7eSAnthony Xu goto go_physmap; 36393d43e7eSAnthony Xu } 36493d43e7eSAnthony Xu return -1; 36593d43e7eSAnthony Xu 36693d43e7eSAnthony Xu go_physmap: 36793d43e7eSAnthony Xu DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n", 36893d43e7eSAnthony Xu start_addr, start_addr + size); 36993d43e7eSAnthony Xu 37093d43e7eSAnthony Xu pfn = phys_offset >> TARGET_PAGE_BITS; 37193d43e7eSAnthony Xu start_gpfn = start_addr >> TARGET_PAGE_BITS; 37293d43e7eSAnthony Xu for (i = 0; i < size >> TARGET_PAGE_BITS; i++) { 37393d43e7eSAnthony Xu unsigned long idx = pfn + i; 37493d43e7eSAnthony Xu xen_pfn_t gpfn = start_gpfn + i; 37593d43e7eSAnthony Xu 37693d43e7eSAnthony Xu rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn); 37793d43e7eSAnthony Xu if (rc) { 37893d43e7eSAnthony Xu DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %" 37993d43e7eSAnthony Xu PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno); 38093d43e7eSAnthony Xu return -rc; 38193d43e7eSAnthony Xu } 38293d43e7eSAnthony Xu } 38393d43e7eSAnthony Xu 38493d43e7eSAnthony Xu mr_name = memory_region_name(mr); 38593d43e7eSAnthony Xu 38693d43e7eSAnthony Xu physmap = g_malloc(sizeof (XenPhysmap)); 38793d43e7eSAnthony Xu 38893d43e7eSAnthony Xu physmap->start_addr = start_addr; 38993d43e7eSAnthony Xu physmap->size = size; 39093d43e7eSAnthony Xu physmap->name = mr_name; 39193d43e7eSAnthony Xu physmap->phys_offset = phys_offset; 39293d43e7eSAnthony Xu 39393d43e7eSAnthony Xu QLIST_INSERT_HEAD(&state->physmap, physmap, list); 39493d43e7eSAnthony Xu 39593d43e7eSAnthony Xu xc_domain_pin_memory_cacheattr(xen_xc, xen_domid, 39693d43e7eSAnthony Xu start_addr >> TARGET_PAGE_BITS, 39793d43e7eSAnthony Xu (start_addr + size - 1) >> TARGET_PAGE_BITS, 39893d43e7eSAnthony Xu XEN_DOMCTL_MEM_CACHEATTR_WB); 399*697b66d0SIgor Druzhinin return xen_save_physmap(state, physmap); 40093d43e7eSAnthony Xu } 40193d43e7eSAnthony Xu 40293d43e7eSAnthony Xu static int xen_remove_from_physmap(XenIOState *state, 40393d43e7eSAnthony Xu hwaddr start_addr, 40493d43e7eSAnthony Xu ram_addr_t size) 40593d43e7eSAnthony Xu { 40693d43e7eSAnthony Xu unsigned long i = 0; 40793d43e7eSAnthony Xu int rc = 0; 40893d43e7eSAnthony Xu XenPhysmap *physmap = NULL; 40993d43e7eSAnthony Xu hwaddr phys_offset = 0; 41093d43e7eSAnthony Xu 41193d43e7eSAnthony Xu physmap = get_physmapping(state, start_addr, size); 41293d43e7eSAnthony Xu if (physmap == NULL) { 41393d43e7eSAnthony Xu return -1; 41493d43e7eSAnthony Xu } 41593d43e7eSAnthony Xu 41693d43e7eSAnthony Xu phys_offset = physmap->phys_offset; 41793d43e7eSAnthony Xu size = physmap->size; 41893d43e7eSAnthony Xu 41993d43e7eSAnthony Xu DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at " 42093d43e7eSAnthony Xu "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset); 42193d43e7eSAnthony Xu 42293d43e7eSAnthony Xu size >>= TARGET_PAGE_BITS; 42393d43e7eSAnthony Xu start_addr >>= TARGET_PAGE_BITS; 42493d43e7eSAnthony Xu phys_offset >>= TARGET_PAGE_BITS; 42593d43e7eSAnthony Xu for (i = 0; i < size; i++) { 42693d43e7eSAnthony Xu xen_pfn_t idx = start_addr + i; 42793d43e7eSAnthony Xu xen_pfn_t gpfn = phys_offset + i; 42893d43e7eSAnthony Xu 42993d43e7eSAnthony Xu rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn); 43093d43e7eSAnthony Xu if (rc) { 43193d43e7eSAnthony Xu fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %" 43293d43e7eSAnthony Xu PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno); 43393d43e7eSAnthony Xu return -rc; 43493d43e7eSAnthony Xu } 43593d43e7eSAnthony Xu } 43693d43e7eSAnthony Xu 43793d43e7eSAnthony Xu QLIST_REMOVE(physmap, list); 43893d43e7eSAnthony Xu if (state->log_for_dirtybit == physmap) { 43993d43e7eSAnthony Xu state->log_for_dirtybit = NULL; 44093d43e7eSAnthony Xu } 44193d43e7eSAnthony Xu g_free(physmap); 44293d43e7eSAnthony Xu 44393d43e7eSAnthony Xu return 0; 44493d43e7eSAnthony Xu } 44593d43e7eSAnthony Xu 44693d43e7eSAnthony Xu static void xen_set_memory(struct MemoryListener *listener, 44793d43e7eSAnthony Xu MemoryRegionSection *section, 44893d43e7eSAnthony Xu bool add) 44993d43e7eSAnthony Xu { 45093d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener); 45193d43e7eSAnthony Xu hwaddr start_addr = section->offset_within_address_space; 45293d43e7eSAnthony Xu ram_addr_t size = int128_get64(section->size); 45393d43e7eSAnthony Xu bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA); 45493d43e7eSAnthony Xu hvmmem_type_t mem_type; 45593d43e7eSAnthony Xu 45693d43e7eSAnthony Xu if (section->mr == &ram_memory) { 45793d43e7eSAnthony Xu return; 45893d43e7eSAnthony Xu } else { 45993d43e7eSAnthony Xu if (add) { 46093d43e7eSAnthony Xu xen_map_memory_section(xen_domid, state->ioservid, 46193d43e7eSAnthony Xu section); 46293d43e7eSAnthony Xu } else { 46393d43e7eSAnthony Xu xen_unmap_memory_section(xen_domid, state->ioservid, 46493d43e7eSAnthony Xu section); 46593d43e7eSAnthony Xu } 46693d43e7eSAnthony Xu } 46793d43e7eSAnthony Xu 46893d43e7eSAnthony Xu if (!memory_region_is_ram(section->mr)) { 46993d43e7eSAnthony Xu return; 47093d43e7eSAnthony Xu } 47193d43e7eSAnthony Xu 47293d43e7eSAnthony Xu if (log_dirty != add) { 47393d43e7eSAnthony Xu return; 47493d43e7eSAnthony Xu } 47593d43e7eSAnthony Xu 47693d43e7eSAnthony Xu trace_xen_client_set_memory(start_addr, size, log_dirty); 47793d43e7eSAnthony Xu 47893d43e7eSAnthony Xu start_addr &= TARGET_PAGE_MASK; 47993d43e7eSAnthony Xu size = TARGET_PAGE_ALIGN(size); 48093d43e7eSAnthony Xu 48193d43e7eSAnthony Xu if (add) { 48293d43e7eSAnthony Xu if (!memory_region_is_rom(section->mr)) { 48393d43e7eSAnthony Xu xen_add_to_physmap(state, start_addr, size, 48493d43e7eSAnthony Xu section->mr, section->offset_within_region); 48593d43e7eSAnthony Xu } else { 48693d43e7eSAnthony Xu mem_type = HVMMEM_ram_ro; 48793d43e7eSAnthony Xu if (xen_set_mem_type(xen_domid, mem_type, 48893d43e7eSAnthony Xu start_addr >> TARGET_PAGE_BITS, 48993d43e7eSAnthony Xu size >> TARGET_PAGE_BITS)) { 49093d43e7eSAnthony Xu DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx"\n", 49193d43e7eSAnthony Xu start_addr); 49293d43e7eSAnthony Xu } 49393d43e7eSAnthony Xu } 49493d43e7eSAnthony Xu } else { 49593d43e7eSAnthony Xu if (xen_remove_from_physmap(state, start_addr, size) < 0) { 49693d43e7eSAnthony Xu DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr); 49793d43e7eSAnthony Xu } 49893d43e7eSAnthony Xu } 49993d43e7eSAnthony Xu } 50093d43e7eSAnthony Xu 50193d43e7eSAnthony Xu static void xen_region_add(MemoryListener *listener, 50293d43e7eSAnthony Xu MemoryRegionSection *section) 50393d43e7eSAnthony Xu { 50493d43e7eSAnthony Xu memory_region_ref(section->mr); 50593d43e7eSAnthony Xu xen_set_memory(listener, section, true); 50693d43e7eSAnthony Xu } 50793d43e7eSAnthony Xu 50893d43e7eSAnthony Xu static void xen_region_del(MemoryListener *listener, 50993d43e7eSAnthony Xu MemoryRegionSection *section) 51093d43e7eSAnthony Xu { 51193d43e7eSAnthony Xu xen_set_memory(listener, section, false); 51293d43e7eSAnthony Xu memory_region_unref(section->mr); 51393d43e7eSAnthony Xu } 51493d43e7eSAnthony Xu 51593d43e7eSAnthony Xu static void xen_io_add(MemoryListener *listener, 51693d43e7eSAnthony Xu MemoryRegionSection *section) 51793d43e7eSAnthony Xu { 51893d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, io_listener); 51993d43e7eSAnthony Xu MemoryRegion *mr = section->mr; 52093d43e7eSAnthony Xu 52193d43e7eSAnthony Xu if (mr->ops == &unassigned_io_ops) { 52293d43e7eSAnthony Xu return; 52393d43e7eSAnthony Xu } 52493d43e7eSAnthony Xu 52593d43e7eSAnthony Xu memory_region_ref(mr); 52693d43e7eSAnthony Xu 52793d43e7eSAnthony Xu xen_map_io_section(xen_domid, state->ioservid, section); 52893d43e7eSAnthony Xu } 52993d43e7eSAnthony Xu 53093d43e7eSAnthony Xu static void xen_io_del(MemoryListener *listener, 53193d43e7eSAnthony Xu MemoryRegionSection *section) 53293d43e7eSAnthony Xu { 53393d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, io_listener); 53493d43e7eSAnthony Xu MemoryRegion *mr = section->mr; 53593d43e7eSAnthony Xu 53693d43e7eSAnthony Xu if (mr->ops == &unassigned_io_ops) { 53793d43e7eSAnthony Xu return; 53893d43e7eSAnthony Xu } 53993d43e7eSAnthony Xu 54093d43e7eSAnthony Xu xen_unmap_io_section(xen_domid, state->ioservid, section); 54193d43e7eSAnthony Xu 54293d43e7eSAnthony Xu memory_region_unref(mr); 54393d43e7eSAnthony Xu } 54493d43e7eSAnthony Xu 54593d43e7eSAnthony Xu static void xen_device_realize(DeviceListener *listener, 54693d43e7eSAnthony Xu DeviceState *dev) 54793d43e7eSAnthony Xu { 54893d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, device_listener); 54993d43e7eSAnthony Xu 55093d43e7eSAnthony Xu if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) { 55193d43e7eSAnthony Xu PCIDevice *pci_dev = PCI_DEVICE(dev); 55293d43e7eSAnthony Xu 55393d43e7eSAnthony Xu xen_map_pcidev(xen_domid, state->ioservid, pci_dev); 55493d43e7eSAnthony Xu } 55593d43e7eSAnthony Xu } 55693d43e7eSAnthony Xu 55793d43e7eSAnthony Xu static void xen_device_unrealize(DeviceListener *listener, 55893d43e7eSAnthony Xu DeviceState *dev) 55993d43e7eSAnthony Xu { 56093d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, device_listener); 56193d43e7eSAnthony Xu 56293d43e7eSAnthony Xu if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) { 56393d43e7eSAnthony Xu PCIDevice *pci_dev = PCI_DEVICE(dev); 56493d43e7eSAnthony Xu 56593d43e7eSAnthony Xu xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev); 56693d43e7eSAnthony Xu } 56793d43e7eSAnthony Xu } 56893d43e7eSAnthony Xu 56993d43e7eSAnthony Xu static void xen_sync_dirty_bitmap(XenIOState *state, 57093d43e7eSAnthony Xu hwaddr start_addr, 57193d43e7eSAnthony Xu ram_addr_t size) 57293d43e7eSAnthony Xu { 57393d43e7eSAnthony Xu hwaddr npages = size >> TARGET_PAGE_BITS; 57493d43e7eSAnthony Xu const int width = sizeof(unsigned long) * 8; 57593d43e7eSAnthony Xu unsigned long bitmap[DIV_ROUND_UP(npages, width)]; 57693d43e7eSAnthony Xu int rc, i, j; 57793d43e7eSAnthony Xu const XenPhysmap *physmap = NULL; 57893d43e7eSAnthony Xu 57993d43e7eSAnthony Xu physmap = get_physmapping(state, start_addr, size); 58093d43e7eSAnthony Xu if (physmap == NULL) { 58193d43e7eSAnthony Xu /* not handled */ 58293d43e7eSAnthony Xu return; 58393d43e7eSAnthony Xu } 58493d43e7eSAnthony Xu 58593d43e7eSAnthony Xu if (state->log_for_dirtybit == NULL) { 58693d43e7eSAnthony Xu state->log_for_dirtybit = physmap; 58793d43e7eSAnthony Xu } else if (state->log_for_dirtybit != physmap) { 58893d43e7eSAnthony Xu /* Only one range for dirty bitmap can be tracked. */ 58993d43e7eSAnthony Xu return; 59093d43e7eSAnthony Xu } 59193d43e7eSAnthony Xu 59293d43e7eSAnthony Xu rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS, 59393d43e7eSAnthony Xu npages, bitmap); 59493d43e7eSAnthony Xu if (rc < 0) { 59593d43e7eSAnthony Xu #ifndef ENODATA 59693d43e7eSAnthony Xu #define ENODATA ENOENT 59793d43e7eSAnthony Xu #endif 59893d43e7eSAnthony Xu if (errno == ENODATA) { 59993d43e7eSAnthony Xu memory_region_set_dirty(framebuffer, 0, size); 60093d43e7eSAnthony Xu DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx 60193d43e7eSAnthony Xu ", 0x" TARGET_FMT_plx "): %s\n", 60293d43e7eSAnthony Xu start_addr, start_addr + size, strerror(errno)); 60393d43e7eSAnthony Xu } 60493d43e7eSAnthony Xu return; 60593d43e7eSAnthony Xu } 60693d43e7eSAnthony Xu 60793d43e7eSAnthony Xu for (i = 0; i < ARRAY_SIZE(bitmap); i++) { 60893d43e7eSAnthony Xu unsigned long map = bitmap[i]; 60993d43e7eSAnthony Xu while (map != 0) { 61093d43e7eSAnthony Xu j = ctzl(map); 61193d43e7eSAnthony Xu map &= ~(1ul << j); 61293d43e7eSAnthony Xu memory_region_set_dirty(framebuffer, 61393d43e7eSAnthony Xu (i * width + j) * TARGET_PAGE_SIZE, 61493d43e7eSAnthony Xu TARGET_PAGE_SIZE); 61593d43e7eSAnthony Xu }; 61693d43e7eSAnthony Xu } 61793d43e7eSAnthony Xu } 61893d43e7eSAnthony Xu 61993d43e7eSAnthony Xu static void xen_log_start(MemoryListener *listener, 62093d43e7eSAnthony Xu MemoryRegionSection *section, 62193d43e7eSAnthony Xu int old, int new) 62293d43e7eSAnthony Xu { 62393d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener); 62493d43e7eSAnthony Xu 62593d43e7eSAnthony Xu if (new & ~old & (1 << DIRTY_MEMORY_VGA)) { 62693d43e7eSAnthony Xu xen_sync_dirty_bitmap(state, section->offset_within_address_space, 62793d43e7eSAnthony Xu int128_get64(section->size)); 62893d43e7eSAnthony Xu } 62993d43e7eSAnthony Xu } 63093d43e7eSAnthony Xu 63193d43e7eSAnthony Xu static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section, 63293d43e7eSAnthony Xu int old, int new) 63393d43e7eSAnthony Xu { 63493d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener); 63593d43e7eSAnthony Xu 63693d43e7eSAnthony Xu if (old & ~new & (1 << DIRTY_MEMORY_VGA)) { 63793d43e7eSAnthony Xu state->log_for_dirtybit = NULL; 63893d43e7eSAnthony Xu /* Disable dirty bit tracking */ 63993d43e7eSAnthony Xu xen_track_dirty_vram(xen_domid, 0, 0, NULL); 64093d43e7eSAnthony Xu } 64193d43e7eSAnthony Xu } 64293d43e7eSAnthony Xu 64393d43e7eSAnthony Xu static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section) 64493d43e7eSAnthony Xu { 64593d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener); 64693d43e7eSAnthony Xu 64793d43e7eSAnthony Xu xen_sync_dirty_bitmap(state, section->offset_within_address_space, 64893d43e7eSAnthony Xu int128_get64(section->size)); 64993d43e7eSAnthony Xu } 65093d43e7eSAnthony Xu 65193d43e7eSAnthony Xu static void xen_log_global_start(MemoryListener *listener) 65293d43e7eSAnthony Xu { 65393d43e7eSAnthony Xu if (xen_enabled()) { 65493d43e7eSAnthony Xu xen_in_migration = true; 65593d43e7eSAnthony Xu } 65693d43e7eSAnthony Xu } 65793d43e7eSAnthony Xu 65893d43e7eSAnthony Xu static void xen_log_global_stop(MemoryListener *listener) 65993d43e7eSAnthony Xu { 66093d43e7eSAnthony Xu xen_in_migration = false; 66193d43e7eSAnthony Xu } 66293d43e7eSAnthony Xu 66393d43e7eSAnthony Xu static MemoryListener xen_memory_listener = { 66493d43e7eSAnthony Xu .region_add = xen_region_add, 66593d43e7eSAnthony Xu .region_del = xen_region_del, 66693d43e7eSAnthony Xu .log_start = xen_log_start, 66793d43e7eSAnthony Xu .log_stop = xen_log_stop, 66893d43e7eSAnthony Xu .log_sync = xen_log_sync, 66993d43e7eSAnthony Xu .log_global_start = xen_log_global_start, 67093d43e7eSAnthony Xu .log_global_stop = xen_log_global_stop, 67193d43e7eSAnthony Xu .priority = 10, 67293d43e7eSAnthony Xu }; 67393d43e7eSAnthony Xu 67493d43e7eSAnthony Xu static MemoryListener xen_io_listener = { 67593d43e7eSAnthony Xu .region_add = xen_io_add, 67693d43e7eSAnthony Xu .region_del = xen_io_del, 67793d43e7eSAnthony Xu .priority = 10, 67893d43e7eSAnthony Xu }; 67993d43e7eSAnthony Xu 68093d43e7eSAnthony Xu static DeviceListener xen_device_listener = { 68193d43e7eSAnthony Xu .realize = xen_device_realize, 68293d43e7eSAnthony Xu .unrealize = xen_device_unrealize, 68393d43e7eSAnthony Xu }; 68493d43e7eSAnthony Xu 68593d43e7eSAnthony Xu /* get the ioreq packets from share mem */ 68693d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) 68793d43e7eSAnthony Xu { 68893d43e7eSAnthony Xu ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu); 68993d43e7eSAnthony Xu 69093d43e7eSAnthony Xu if (req->state != STATE_IOREQ_READY) { 69193d43e7eSAnthony Xu DPRINTF("I/O request not ready: " 69293d43e7eSAnthony Xu "%x, ptr: %x, port: %"PRIx64", " 69393d43e7eSAnthony Xu "data: %"PRIx64", count: %u, size: %u\n", 69493d43e7eSAnthony Xu req->state, req->data_is_ptr, req->addr, 69593d43e7eSAnthony Xu req->data, req->count, req->size); 69693d43e7eSAnthony Xu return NULL; 69793d43e7eSAnthony Xu } 69893d43e7eSAnthony Xu 69993d43e7eSAnthony Xu xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */ 70093d43e7eSAnthony Xu 70193d43e7eSAnthony Xu req->state = STATE_IOREQ_INPROCESS; 70293d43e7eSAnthony Xu return req; 70393d43e7eSAnthony Xu } 70493d43e7eSAnthony Xu 70593d43e7eSAnthony Xu /* use poll to get the port notification */ 70693d43e7eSAnthony Xu /* ioreq_vec--out,the */ 70793d43e7eSAnthony Xu /* retval--the number of ioreq packet */ 70893d43e7eSAnthony Xu static ioreq_t *cpu_get_ioreq(XenIOState *state) 70993d43e7eSAnthony Xu { 71093d43e7eSAnthony Xu int i; 71193d43e7eSAnthony Xu evtchn_port_t port; 71293d43e7eSAnthony Xu 71393d43e7eSAnthony Xu port = xenevtchn_pending(state->xce_handle); 71493d43e7eSAnthony Xu if (port == state->bufioreq_local_port) { 71593d43e7eSAnthony Xu timer_mod(state->buffered_io_timer, 71693d43e7eSAnthony Xu BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); 71793d43e7eSAnthony Xu return NULL; 71893d43e7eSAnthony Xu } 71993d43e7eSAnthony Xu 72093d43e7eSAnthony Xu if (port != -1) { 72193d43e7eSAnthony Xu for (i = 0; i < max_cpus; i++) { 72293d43e7eSAnthony Xu if (state->ioreq_local_port[i] == port) { 72393d43e7eSAnthony Xu break; 72493d43e7eSAnthony Xu } 72593d43e7eSAnthony Xu } 72693d43e7eSAnthony Xu 72793d43e7eSAnthony Xu if (i == max_cpus) { 72893d43e7eSAnthony Xu hw_error("Fatal error while trying to get io event!\n"); 72993d43e7eSAnthony Xu } 73093d43e7eSAnthony Xu 73193d43e7eSAnthony Xu /* unmask the wanted port again */ 73293d43e7eSAnthony Xu xenevtchn_unmask(state->xce_handle, port); 73393d43e7eSAnthony Xu 73493d43e7eSAnthony Xu /* get the io packet from shared memory */ 73593d43e7eSAnthony Xu state->send_vcpu = i; 73693d43e7eSAnthony Xu return cpu_get_ioreq_from_shared_memory(state, i); 73793d43e7eSAnthony Xu } 73893d43e7eSAnthony Xu 73993d43e7eSAnthony Xu /* read error or read nothing */ 74093d43e7eSAnthony Xu return NULL; 74193d43e7eSAnthony Xu } 74293d43e7eSAnthony Xu 74393d43e7eSAnthony Xu static uint32_t do_inp(uint32_t addr, unsigned long size) 74493d43e7eSAnthony Xu { 74593d43e7eSAnthony Xu switch (size) { 74693d43e7eSAnthony Xu case 1: 74793d43e7eSAnthony Xu return cpu_inb(addr); 74893d43e7eSAnthony Xu case 2: 74993d43e7eSAnthony Xu return cpu_inw(addr); 75093d43e7eSAnthony Xu case 4: 75193d43e7eSAnthony Xu return cpu_inl(addr); 75293d43e7eSAnthony Xu default: 75393d43e7eSAnthony Xu hw_error("inp: bad size: %04x %lx", addr, size); 75493d43e7eSAnthony Xu } 75593d43e7eSAnthony Xu } 75693d43e7eSAnthony Xu 75793d43e7eSAnthony Xu static void do_outp(uint32_t addr, 75893d43e7eSAnthony Xu unsigned long size, uint32_t val) 75993d43e7eSAnthony Xu { 76093d43e7eSAnthony Xu switch (size) { 76193d43e7eSAnthony Xu case 1: 76293d43e7eSAnthony Xu return cpu_outb(addr, val); 76393d43e7eSAnthony Xu case 2: 76493d43e7eSAnthony Xu return cpu_outw(addr, val); 76593d43e7eSAnthony Xu case 4: 76693d43e7eSAnthony Xu return cpu_outl(addr, val); 76793d43e7eSAnthony Xu default: 76893d43e7eSAnthony Xu hw_error("outp: bad size: %04x %lx", addr, size); 76993d43e7eSAnthony Xu } 77093d43e7eSAnthony Xu } 77193d43e7eSAnthony Xu 77293d43e7eSAnthony Xu /* 77393d43e7eSAnthony Xu * Helper functions which read/write an object from/to physical guest 77493d43e7eSAnthony Xu * memory, as part of the implementation of an ioreq. 77593d43e7eSAnthony Xu * 77693d43e7eSAnthony Xu * Equivalent to 77793d43e7eSAnthony Xu * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i, 77893d43e7eSAnthony Xu * val, req->size, 0/1) 77993d43e7eSAnthony Xu * except without the integer overflow problems. 78093d43e7eSAnthony Xu */ 78193d43e7eSAnthony Xu static void rw_phys_req_item(hwaddr addr, 78293d43e7eSAnthony Xu ioreq_t *req, uint32_t i, void *val, int rw) 78393d43e7eSAnthony Xu { 78493d43e7eSAnthony Xu /* Do everything unsigned so overflow just results in a truncated result 78593d43e7eSAnthony Xu * and accesses to undesired parts of guest memory, which is up 78693d43e7eSAnthony Xu * to the guest */ 78793d43e7eSAnthony Xu hwaddr offset = (hwaddr)req->size * i; 78893d43e7eSAnthony Xu if (req->df) { 78993d43e7eSAnthony Xu addr -= offset; 79093d43e7eSAnthony Xu } else { 79193d43e7eSAnthony Xu addr += offset; 79293d43e7eSAnthony Xu } 79393d43e7eSAnthony Xu cpu_physical_memory_rw(addr, val, req->size, rw); 79493d43e7eSAnthony Xu } 79593d43e7eSAnthony Xu 79693d43e7eSAnthony Xu static inline void read_phys_req_item(hwaddr addr, 79793d43e7eSAnthony Xu ioreq_t *req, uint32_t i, void *val) 79893d43e7eSAnthony Xu { 79993d43e7eSAnthony Xu rw_phys_req_item(addr, req, i, val, 0); 80093d43e7eSAnthony Xu } 80193d43e7eSAnthony Xu static inline void write_phys_req_item(hwaddr addr, 80293d43e7eSAnthony Xu ioreq_t *req, uint32_t i, void *val) 80393d43e7eSAnthony Xu { 80493d43e7eSAnthony Xu rw_phys_req_item(addr, req, i, val, 1); 80593d43e7eSAnthony Xu } 80693d43e7eSAnthony Xu 80793d43e7eSAnthony Xu 80893d43e7eSAnthony Xu static void cpu_ioreq_pio(ioreq_t *req) 80993d43e7eSAnthony Xu { 81093d43e7eSAnthony Xu uint32_t i; 81193d43e7eSAnthony Xu 81293d43e7eSAnthony Xu trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr, 81393d43e7eSAnthony Xu req->data, req->count, req->size); 81493d43e7eSAnthony Xu 81593d43e7eSAnthony Xu if (req->size > sizeof(uint32_t)) { 81693d43e7eSAnthony Xu hw_error("PIO: bad size (%u)", req->size); 81793d43e7eSAnthony Xu } 81893d43e7eSAnthony Xu 81993d43e7eSAnthony Xu if (req->dir == IOREQ_READ) { 82093d43e7eSAnthony Xu if (!req->data_is_ptr) { 82193d43e7eSAnthony Xu req->data = do_inp(req->addr, req->size); 82293d43e7eSAnthony Xu trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr, 82393d43e7eSAnthony Xu req->size); 82493d43e7eSAnthony Xu } else { 82593d43e7eSAnthony Xu uint32_t tmp; 82693d43e7eSAnthony Xu 82793d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 82893d43e7eSAnthony Xu tmp = do_inp(req->addr, req->size); 82993d43e7eSAnthony Xu write_phys_req_item(req->data, req, i, &tmp); 83093d43e7eSAnthony Xu } 83193d43e7eSAnthony Xu } 83293d43e7eSAnthony Xu } else if (req->dir == IOREQ_WRITE) { 83393d43e7eSAnthony Xu if (!req->data_is_ptr) { 83493d43e7eSAnthony Xu trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr, 83593d43e7eSAnthony Xu req->size); 83693d43e7eSAnthony Xu do_outp(req->addr, req->size, req->data); 83793d43e7eSAnthony Xu } else { 83893d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 83993d43e7eSAnthony Xu uint32_t tmp = 0; 84093d43e7eSAnthony Xu 84193d43e7eSAnthony Xu read_phys_req_item(req->data, req, i, &tmp); 84293d43e7eSAnthony Xu do_outp(req->addr, req->size, tmp); 84393d43e7eSAnthony Xu } 84493d43e7eSAnthony Xu } 84593d43e7eSAnthony Xu } 84693d43e7eSAnthony Xu } 84793d43e7eSAnthony Xu 84893d43e7eSAnthony Xu static void cpu_ioreq_move(ioreq_t *req) 84993d43e7eSAnthony Xu { 85093d43e7eSAnthony Xu uint32_t i; 85193d43e7eSAnthony Xu 85293d43e7eSAnthony Xu trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr, 85393d43e7eSAnthony Xu req->data, req->count, req->size); 85493d43e7eSAnthony Xu 85593d43e7eSAnthony Xu if (req->size > sizeof(req->data)) { 85693d43e7eSAnthony Xu hw_error("MMIO: bad size (%u)", req->size); 85793d43e7eSAnthony Xu } 85893d43e7eSAnthony Xu 85993d43e7eSAnthony Xu if (!req->data_is_ptr) { 86093d43e7eSAnthony Xu if (req->dir == IOREQ_READ) { 86193d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 86293d43e7eSAnthony Xu read_phys_req_item(req->addr, req, i, &req->data); 86393d43e7eSAnthony Xu } 86493d43e7eSAnthony Xu } else if (req->dir == IOREQ_WRITE) { 86593d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 86693d43e7eSAnthony Xu write_phys_req_item(req->addr, req, i, &req->data); 86793d43e7eSAnthony Xu } 86893d43e7eSAnthony Xu } 86993d43e7eSAnthony Xu } else { 87093d43e7eSAnthony Xu uint64_t tmp; 87193d43e7eSAnthony Xu 87293d43e7eSAnthony Xu if (req->dir == IOREQ_READ) { 87393d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 87493d43e7eSAnthony Xu read_phys_req_item(req->addr, req, i, &tmp); 87593d43e7eSAnthony Xu write_phys_req_item(req->data, req, i, &tmp); 87693d43e7eSAnthony Xu } 87793d43e7eSAnthony Xu } else if (req->dir == IOREQ_WRITE) { 87893d43e7eSAnthony Xu for (i = 0; i < req->count; i++) { 87993d43e7eSAnthony Xu read_phys_req_item(req->data, req, i, &tmp); 88093d43e7eSAnthony Xu write_phys_req_item(req->addr, req, i, &tmp); 88193d43e7eSAnthony Xu } 88293d43e7eSAnthony Xu } 88393d43e7eSAnthony Xu } 88493d43e7eSAnthony Xu } 88593d43e7eSAnthony Xu 88693d43e7eSAnthony Xu static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req) 88793d43e7eSAnthony Xu { 88893d43e7eSAnthony Xu X86CPU *cpu; 88993d43e7eSAnthony Xu CPUX86State *env; 89093d43e7eSAnthony Xu 89193d43e7eSAnthony Xu cpu = X86_CPU(current_cpu); 89293d43e7eSAnthony Xu env = &cpu->env; 89393d43e7eSAnthony Xu env->regs[R_EAX] = req->data; 89493d43e7eSAnthony Xu env->regs[R_EBX] = vmport_regs->ebx; 89593d43e7eSAnthony Xu env->regs[R_ECX] = vmport_regs->ecx; 89693d43e7eSAnthony Xu env->regs[R_EDX] = vmport_regs->edx; 89793d43e7eSAnthony Xu env->regs[R_ESI] = vmport_regs->esi; 89893d43e7eSAnthony Xu env->regs[R_EDI] = vmport_regs->edi; 89993d43e7eSAnthony Xu } 90093d43e7eSAnthony Xu 90193d43e7eSAnthony Xu static void regs_from_cpu(vmware_regs_t *vmport_regs) 90293d43e7eSAnthony Xu { 90393d43e7eSAnthony Xu X86CPU *cpu = X86_CPU(current_cpu); 90493d43e7eSAnthony Xu CPUX86State *env = &cpu->env; 90593d43e7eSAnthony Xu 90693d43e7eSAnthony Xu vmport_regs->ebx = env->regs[R_EBX]; 90793d43e7eSAnthony Xu vmport_regs->ecx = env->regs[R_ECX]; 90893d43e7eSAnthony Xu vmport_regs->edx = env->regs[R_EDX]; 90993d43e7eSAnthony Xu vmport_regs->esi = env->regs[R_ESI]; 91093d43e7eSAnthony Xu vmport_regs->edi = env->regs[R_EDI]; 91193d43e7eSAnthony Xu } 91293d43e7eSAnthony Xu 91393d43e7eSAnthony Xu static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req) 91493d43e7eSAnthony Xu { 91593d43e7eSAnthony Xu vmware_regs_t *vmport_regs; 91693d43e7eSAnthony Xu 91793d43e7eSAnthony Xu assert(state->shared_vmport_page); 91893d43e7eSAnthony Xu vmport_regs = 91993d43e7eSAnthony Xu &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu]; 92093d43e7eSAnthony Xu QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs)); 92193d43e7eSAnthony Xu 92293d43e7eSAnthony Xu current_cpu = state->cpu_by_vcpu_id[state->send_vcpu]; 92393d43e7eSAnthony Xu regs_to_cpu(vmport_regs, req); 92493d43e7eSAnthony Xu cpu_ioreq_pio(req); 92593d43e7eSAnthony Xu regs_from_cpu(vmport_regs); 92693d43e7eSAnthony Xu current_cpu = NULL; 92793d43e7eSAnthony Xu } 92893d43e7eSAnthony Xu 92993d43e7eSAnthony Xu static void handle_ioreq(XenIOState *state, ioreq_t *req) 93093d43e7eSAnthony Xu { 93193d43e7eSAnthony Xu trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr, 93293d43e7eSAnthony Xu req->addr, req->data, req->count, req->size); 93393d43e7eSAnthony Xu 93493d43e7eSAnthony Xu if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) && 93593d43e7eSAnthony Xu (req->size < sizeof (target_ulong))) { 93693d43e7eSAnthony Xu req->data &= ((target_ulong) 1 << (8 * req->size)) - 1; 93793d43e7eSAnthony Xu } 93893d43e7eSAnthony Xu 93993d43e7eSAnthony Xu if (req->dir == IOREQ_WRITE) 94093d43e7eSAnthony Xu trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr, 94193d43e7eSAnthony Xu req->addr, req->data, req->count, req->size); 94293d43e7eSAnthony Xu 94393d43e7eSAnthony Xu switch (req->type) { 94493d43e7eSAnthony Xu case IOREQ_TYPE_PIO: 94593d43e7eSAnthony Xu cpu_ioreq_pio(req); 94693d43e7eSAnthony Xu break; 94793d43e7eSAnthony Xu case IOREQ_TYPE_COPY: 94893d43e7eSAnthony Xu cpu_ioreq_move(req); 94993d43e7eSAnthony Xu break; 95093d43e7eSAnthony Xu case IOREQ_TYPE_VMWARE_PORT: 95193d43e7eSAnthony Xu handle_vmport_ioreq(state, req); 95293d43e7eSAnthony Xu break; 95393d43e7eSAnthony Xu case IOREQ_TYPE_TIMEOFFSET: 95493d43e7eSAnthony Xu break; 95593d43e7eSAnthony Xu case IOREQ_TYPE_INVALIDATE: 95693d43e7eSAnthony Xu xen_invalidate_map_cache(); 95793d43e7eSAnthony Xu break; 95893d43e7eSAnthony Xu case IOREQ_TYPE_PCI_CONFIG: { 95993d43e7eSAnthony Xu uint32_t sbdf = req->addr >> 32; 96093d43e7eSAnthony Xu uint32_t val; 96193d43e7eSAnthony Xu 96293d43e7eSAnthony Xu /* Fake a write to port 0xCF8 so that 96393d43e7eSAnthony Xu * the config space access will target the 96493d43e7eSAnthony Xu * correct device model. 96593d43e7eSAnthony Xu */ 96693d43e7eSAnthony Xu val = (1u << 31) | 96793d43e7eSAnthony Xu ((req->addr & 0x0f00) << 16) | 96893d43e7eSAnthony Xu ((sbdf & 0xffff) << 8) | 96993d43e7eSAnthony Xu (req->addr & 0xfc); 97093d43e7eSAnthony Xu do_outp(0xcf8, 4, val); 97193d43e7eSAnthony Xu 97293d43e7eSAnthony Xu /* Now issue the config space access via 97393d43e7eSAnthony Xu * port 0xCFC 97493d43e7eSAnthony Xu */ 97593d43e7eSAnthony Xu req->addr = 0xcfc | (req->addr & 0x03); 97693d43e7eSAnthony Xu cpu_ioreq_pio(req); 97793d43e7eSAnthony Xu break; 97893d43e7eSAnthony Xu } 97993d43e7eSAnthony Xu default: 98093d43e7eSAnthony Xu hw_error("Invalid ioreq type 0x%x\n", req->type); 98193d43e7eSAnthony Xu } 98293d43e7eSAnthony Xu if (req->dir == IOREQ_READ) { 98393d43e7eSAnthony Xu trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr, 98493d43e7eSAnthony Xu req->addr, req->data, req->count, req->size); 98593d43e7eSAnthony Xu } 98693d43e7eSAnthony Xu } 98793d43e7eSAnthony Xu 98893d43e7eSAnthony Xu static int handle_buffered_iopage(XenIOState *state) 98993d43e7eSAnthony Xu { 99093d43e7eSAnthony Xu buffered_iopage_t *buf_page = state->buffered_io_page; 99193d43e7eSAnthony Xu buf_ioreq_t *buf_req = NULL; 99293d43e7eSAnthony Xu ioreq_t req; 99393d43e7eSAnthony Xu int qw; 99493d43e7eSAnthony Xu 99593d43e7eSAnthony Xu if (!buf_page) { 99693d43e7eSAnthony Xu return 0; 99793d43e7eSAnthony Xu } 99893d43e7eSAnthony Xu 99993d43e7eSAnthony Xu memset(&req, 0x00, sizeof(req)); 100093d43e7eSAnthony Xu req.state = STATE_IOREQ_READY; 100193d43e7eSAnthony Xu req.count = 1; 100293d43e7eSAnthony Xu req.dir = IOREQ_WRITE; 100393d43e7eSAnthony Xu 100493d43e7eSAnthony Xu for (;;) { 100593d43e7eSAnthony Xu uint32_t rdptr = buf_page->read_pointer, wrptr; 100693d43e7eSAnthony Xu 100793d43e7eSAnthony Xu xen_rmb(); 100893d43e7eSAnthony Xu wrptr = buf_page->write_pointer; 100993d43e7eSAnthony Xu xen_rmb(); 101093d43e7eSAnthony Xu if (rdptr != buf_page->read_pointer) { 101193d43e7eSAnthony Xu continue; 101293d43e7eSAnthony Xu } 101393d43e7eSAnthony Xu if (rdptr == wrptr) { 101493d43e7eSAnthony Xu break; 101593d43e7eSAnthony Xu } 101693d43e7eSAnthony Xu buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM]; 101793d43e7eSAnthony Xu req.size = 1U << buf_req->size; 101893d43e7eSAnthony Xu req.addr = buf_req->addr; 101993d43e7eSAnthony Xu req.data = buf_req->data; 102093d43e7eSAnthony Xu req.type = buf_req->type; 102193d43e7eSAnthony Xu xen_rmb(); 102293d43e7eSAnthony Xu qw = (req.size == 8); 102393d43e7eSAnthony Xu if (qw) { 102493d43e7eSAnthony Xu if (rdptr + 1 == wrptr) { 102593d43e7eSAnthony Xu hw_error("Incomplete quad word buffered ioreq"); 102693d43e7eSAnthony Xu } 102793d43e7eSAnthony Xu buf_req = &buf_page->buf_ioreq[(rdptr + 1) % 102893d43e7eSAnthony Xu IOREQ_BUFFER_SLOT_NUM]; 102993d43e7eSAnthony Xu req.data |= ((uint64_t)buf_req->data) << 32; 103093d43e7eSAnthony Xu xen_rmb(); 103193d43e7eSAnthony Xu } 103293d43e7eSAnthony Xu 103393d43e7eSAnthony Xu handle_ioreq(state, &req); 103493d43e7eSAnthony Xu 103593d43e7eSAnthony Xu /* Only req.data may get updated by handle_ioreq(), albeit even that 103693d43e7eSAnthony Xu * should not happen as such data would never make it to the guest (we 103793d43e7eSAnthony Xu * can only usefully see writes here after all). 103893d43e7eSAnthony Xu */ 103993d43e7eSAnthony Xu assert(req.state == STATE_IOREQ_READY); 104093d43e7eSAnthony Xu assert(req.count == 1); 104193d43e7eSAnthony Xu assert(req.dir == IOREQ_WRITE); 104293d43e7eSAnthony Xu assert(!req.data_is_ptr); 104393d43e7eSAnthony Xu 104493d43e7eSAnthony Xu atomic_add(&buf_page->read_pointer, qw + 1); 104593d43e7eSAnthony Xu } 104693d43e7eSAnthony Xu 104793d43e7eSAnthony Xu return req.count; 104893d43e7eSAnthony Xu } 104993d43e7eSAnthony Xu 105093d43e7eSAnthony Xu static void handle_buffered_io(void *opaque) 105193d43e7eSAnthony Xu { 105293d43e7eSAnthony Xu XenIOState *state = opaque; 105393d43e7eSAnthony Xu 105493d43e7eSAnthony Xu if (handle_buffered_iopage(state)) { 105593d43e7eSAnthony Xu timer_mod(state->buffered_io_timer, 105693d43e7eSAnthony Xu BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); 105793d43e7eSAnthony Xu } else { 105893d43e7eSAnthony Xu timer_del(state->buffered_io_timer); 105993d43e7eSAnthony Xu xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port); 106093d43e7eSAnthony Xu } 106193d43e7eSAnthony Xu } 106293d43e7eSAnthony Xu 106393d43e7eSAnthony Xu static void cpu_handle_ioreq(void *opaque) 106493d43e7eSAnthony Xu { 106593d43e7eSAnthony Xu XenIOState *state = opaque; 106693d43e7eSAnthony Xu ioreq_t *req = cpu_get_ioreq(state); 106793d43e7eSAnthony Xu 106893d43e7eSAnthony Xu handle_buffered_iopage(state); 106993d43e7eSAnthony Xu if (req) { 107093d43e7eSAnthony Xu ioreq_t copy = *req; 107193d43e7eSAnthony Xu 107293d43e7eSAnthony Xu xen_rmb(); 107393d43e7eSAnthony Xu handle_ioreq(state, ©); 107493d43e7eSAnthony Xu req->data = copy.data; 107593d43e7eSAnthony Xu 107693d43e7eSAnthony Xu if (req->state != STATE_IOREQ_INPROCESS) { 107793d43e7eSAnthony Xu fprintf(stderr, "Badness in I/O request ... not in service?!: " 107893d43e7eSAnthony Xu "%x, ptr: %x, port: %"PRIx64", " 107993d43e7eSAnthony Xu "data: %"PRIx64", count: %u, size: %u, type: %u\n", 108093d43e7eSAnthony Xu req->state, req->data_is_ptr, req->addr, 108193d43e7eSAnthony Xu req->data, req->count, req->size, req->type); 108293d43e7eSAnthony Xu destroy_hvm_domain(false); 108393d43e7eSAnthony Xu return; 108493d43e7eSAnthony Xu } 108593d43e7eSAnthony Xu 108693d43e7eSAnthony Xu xen_wmb(); /* Update ioreq contents /then/ update state. */ 108793d43e7eSAnthony Xu 108893d43e7eSAnthony Xu /* 108993d43e7eSAnthony Xu * We do this before we send the response so that the tools 109093d43e7eSAnthony Xu * have the opportunity to pick up on the reset before the 109193d43e7eSAnthony Xu * guest resumes and does a hlt with interrupts disabled which 109293d43e7eSAnthony Xu * causes Xen to powerdown the domain. 109393d43e7eSAnthony Xu */ 109493d43e7eSAnthony Xu if (runstate_is_running()) { 1095aedbe192SEric Blake ShutdownCause request; 1096aedbe192SEric Blake 109793d43e7eSAnthony Xu if (qemu_shutdown_requested_get()) { 109893d43e7eSAnthony Xu destroy_hvm_domain(false); 109993d43e7eSAnthony Xu } 1100aedbe192SEric Blake request = qemu_reset_requested_get(); 1101aedbe192SEric Blake if (request) { 1102aedbe192SEric Blake qemu_system_reset(request); 110393d43e7eSAnthony Xu destroy_hvm_domain(true); 110493d43e7eSAnthony Xu } 110593d43e7eSAnthony Xu } 110693d43e7eSAnthony Xu 110793d43e7eSAnthony Xu req->state = STATE_IORESP_READY; 110893d43e7eSAnthony Xu xenevtchn_notify(state->xce_handle, 110993d43e7eSAnthony Xu state->ioreq_local_port[state->send_vcpu]); 111093d43e7eSAnthony Xu } 111193d43e7eSAnthony Xu } 111293d43e7eSAnthony Xu 111393d43e7eSAnthony Xu static void xen_main_loop_prepare(XenIOState *state) 111493d43e7eSAnthony Xu { 111593d43e7eSAnthony Xu int evtchn_fd = -1; 111693d43e7eSAnthony Xu 111793d43e7eSAnthony Xu if (state->xce_handle != NULL) { 111893d43e7eSAnthony Xu evtchn_fd = xenevtchn_fd(state->xce_handle); 111993d43e7eSAnthony Xu } 112093d43e7eSAnthony Xu 112193d43e7eSAnthony Xu state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io, 112293d43e7eSAnthony Xu state); 112393d43e7eSAnthony Xu 112493d43e7eSAnthony Xu if (evtchn_fd != -1) { 112593d43e7eSAnthony Xu CPUState *cpu_state; 112693d43e7eSAnthony Xu 112793d43e7eSAnthony Xu DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__); 112893d43e7eSAnthony Xu CPU_FOREACH(cpu_state) { 112993d43e7eSAnthony Xu DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n", 113093d43e7eSAnthony Xu __func__, cpu_state->cpu_index, cpu_state); 113193d43e7eSAnthony Xu state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state; 113293d43e7eSAnthony Xu } 113393d43e7eSAnthony Xu qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state); 113493d43e7eSAnthony Xu } 113593d43e7eSAnthony Xu } 113693d43e7eSAnthony Xu 113793d43e7eSAnthony Xu 113893d43e7eSAnthony Xu static void xen_hvm_change_state_handler(void *opaque, int running, 113993d43e7eSAnthony Xu RunState rstate) 114093d43e7eSAnthony Xu { 114193d43e7eSAnthony Xu XenIOState *state = opaque; 114293d43e7eSAnthony Xu 114393d43e7eSAnthony Xu if (running) { 114493d43e7eSAnthony Xu xen_main_loop_prepare(state); 114593d43e7eSAnthony Xu } 114693d43e7eSAnthony Xu 114793d43e7eSAnthony Xu xen_set_ioreq_server_state(xen_domid, 114893d43e7eSAnthony Xu state->ioservid, 114993d43e7eSAnthony Xu (rstate == RUN_STATE_RUNNING)); 115093d43e7eSAnthony Xu } 115193d43e7eSAnthony Xu 115293d43e7eSAnthony Xu static void xen_exit_notifier(Notifier *n, void *data) 115393d43e7eSAnthony Xu { 115493d43e7eSAnthony Xu XenIOState *state = container_of(n, XenIOState, exit); 115593d43e7eSAnthony Xu 115693d43e7eSAnthony Xu xenevtchn_close(state->xce_handle); 115793d43e7eSAnthony Xu xs_daemon_close(state->xenstore); 115893d43e7eSAnthony Xu } 115993d43e7eSAnthony Xu 116093d43e7eSAnthony Xu static void xen_read_physmap(XenIOState *state) 116193d43e7eSAnthony Xu { 116293d43e7eSAnthony Xu XenPhysmap *physmap = NULL; 116393d43e7eSAnthony Xu unsigned int len, num, i; 116493d43e7eSAnthony Xu char path[80], *value = NULL; 116593d43e7eSAnthony Xu char **entries = NULL; 116693d43e7eSAnthony Xu 116793d43e7eSAnthony Xu snprintf(path, sizeof(path), 116893d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap", xen_domid); 116993d43e7eSAnthony Xu entries = xs_directory(state->xenstore, 0, path, &num); 117093d43e7eSAnthony Xu if (entries == NULL) 117193d43e7eSAnthony Xu return; 117293d43e7eSAnthony Xu 117393d43e7eSAnthony Xu for (i = 0; i < num; i++) { 117493d43e7eSAnthony Xu physmap = g_malloc(sizeof (XenPhysmap)); 117593d43e7eSAnthony Xu physmap->phys_offset = strtoull(entries[i], NULL, 16); 117693d43e7eSAnthony Xu snprintf(path, sizeof(path), 117793d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/start_addr", 117893d43e7eSAnthony Xu xen_domid, entries[i]); 117993d43e7eSAnthony Xu value = xs_read(state->xenstore, 0, path, &len); 118093d43e7eSAnthony Xu if (value == NULL) { 118193d43e7eSAnthony Xu g_free(physmap); 118293d43e7eSAnthony Xu continue; 118393d43e7eSAnthony Xu } 118493d43e7eSAnthony Xu physmap->start_addr = strtoull(value, NULL, 16); 118593d43e7eSAnthony Xu free(value); 118693d43e7eSAnthony Xu 118793d43e7eSAnthony Xu snprintf(path, sizeof(path), 118893d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/size", 118993d43e7eSAnthony Xu xen_domid, entries[i]); 119093d43e7eSAnthony Xu value = xs_read(state->xenstore, 0, path, &len); 119193d43e7eSAnthony Xu if (value == NULL) { 119293d43e7eSAnthony Xu g_free(physmap); 119393d43e7eSAnthony Xu continue; 119493d43e7eSAnthony Xu } 119593d43e7eSAnthony Xu physmap->size = strtoull(value, NULL, 16); 119693d43e7eSAnthony Xu free(value); 119793d43e7eSAnthony Xu 119893d43e7eSAnthony Xu snprintf(path, sizeof(path), 119993d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/name", 120093d43e7eSAnthony Xu xen_domid, entries[i]); 120193d43e7eSAnthony Xu physmap->name = xs_read(state->xenstore, 0, path, &len); 120293d43e7eSAnthony Xu 120393d43e7eSAnthony Xu QLIST_INSERT_HEAD(&state->physmap, physmap, list); 120493d43e7eSAnthony Xu } 120593d43e7eSAnthony Xu free(entries); 120693d43e7eSAnthony Xu } 120793d43e7eSAnthony Xu 120893d43e7eSAnthony Xu static void xen_wakeup_notifier(Notifier *notifier, void *data) 120993d43e7eSAnthony Xu { 121093d43e7eSAnthony Xu xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0); 121193d43e7eSAnthony Xu } 121293d43e7eSAnthony Xu 121393d43e7eSAnthony Xu void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory) 121493d43e7eSAnthony Xu { 121593d43e7eSAnthony Xu int i, rc; 121693d43e7eSAnthony Xu xen_pfn_t ioreq_pfn; 121793d43e7eSAnthony Xu xen_pfn_t bufioreq_pfn; 121893d43e7eSAnthony Xu evtchn_port_t bufioreq_evtchn; 121993d43e7eSAnthony Xu XenIOState *state; 122093d43e7eSAnthony Xu 122193d43e7eSAnthony Xu state = g_malloc0(sizeof (XenIOState)); 122293d43e7eSAnthony Xu 122393d43e7eSAnthony Xu state->xce_handle = xenevtchn_open(NULL, 0); 122493d43e7eSAnthony Xu if (state->xce_handle == NULL) { 122593d43e7eSAnthony Xu perror("xen: event channel open"); 122693d43e7eSAnthony Xu goto err; 122793d43e7eSAnthony Xu } 122893d43e7eSAnthony Xu 122993d43e7eSAnthony Xu state->xenstore = xs_daemon_open(); 123093d43e7eSAnthony Xu if (state->xenstore == NULL) { 123193d43e7eSAnthony Xu perror("xen: xenstore open"); 123293d43e7eSAnthony Xu goto err; 123393d43e7eSAnthony Xu } 123493d43e7eSAnthony Xu 123593d43e7eSAnthony Xu if (xen_domid_restrict) { 123693d43e7eSAnthony Xu rc = xen_restrict(xen_domid); 123793d43e7eSAnthony Xu if (rc < 0) { 123893d43e7eSAnthony Xu error_report("failed to restrict: error %d", errno); 123993d43e7eSAnthony Xu goto err; 124093d43e7eSAnthony Xu } 124193d43e7eSAnthony Xu } 124293d43e7eSAnthony Xu 124393d43e7eSAnthony Xu xen_create_ioreq_server(xen_domid, &state->ioservid); 124493d43e7eSAnthony Xu 124593d43e7eSAnthony Xu state->exit.notify = xen_exit_notifier; 124693d43e7eSAnthony Xu qemu_add_exit_notifier(&state->exit); 124793d43e7eSAnthony Xu 124893d43e7eSAnthony Xu state->suspend.notify = xen_suspend_notifier; 124993d43e7eSAnthony Xu qemu_register_suspend_notifier(&state->suspend); 125093d43e7eSAnthony Xu 125193d43e7eSAnthony Xu state->wakeup.notify = xen_wakeup_notifier; 125293d43e7eSAnthony Xu qemu_register_wakeup_notifier(&state->wakeup); 125393d43e7eSAnthony Xu 125493d43e7eSAnthony Xu rc = xen_get_ioreq_server_info(xen_domid, state->ioservid, 125593d43e7eSAnthony Xu &ioreq_pfn, &bufioreq_pfn, 125693d43e7eSAnthony Xu &bufioreq_evtchn); 125793d43e7eSAnthony Xu if (rc < 0) { 125893d43e7eSAnthony Xu error_report("failed to get ioreq server info: error %d handle=%p", 125993d43e7eSAnthony Xu errno, xen_xc); 126093d43e7eSAnthony Xu goto err; 126193d43e7eSAnthony Xu } 126293d43e7eSAnthony Xu 126393d43e7eSAnthony Xu DPRINTF("shared page at pfn %lx\n", ioreq_pfn); 126493d43e7eSAnthony Xu DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn); 126593d43e7eSAnthony Xu DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn); 126693d43e7eSAnthony Xu 126793d43e7eSAnthony Xu state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid, 126893d43e7eSAnthony Xu PROT_READ|PROT_WRITE, 126993d43e7eSAnthony Xu 1, &ioreq_pfn, NULL); 127093d43e7eSAnthony Xu if (state->shared_page == NULL) { 127193d43e7eSAnthony Xu error_report("map shared IO page returned error %d handle=%p", 127293d43e7eSAnthony Xu errno, xen_xc); 127393d43e7eSAnthony Xu goto err; 127493d43e7eSAnthony Xu } 127593d43e7eSAnthony Xu 127693d43e7eSAnthony Xu rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn); 127793d43e7eSAnthony Xu if (!rc) { 127893d43e7eSAnthony Xu DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn); 127993d43e7eSAnthony Xu state->shared_vmport_page = 128093d43e7eSAnthony Xu xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE, 128193d43e7eSAnthony Xu 1, &ioreq_pfn, NULL); 128293d43e7eSAnthony Xu if (state->shared_vmport_page == NULL) { 128393d43e7eSAnthony Xu error_report("map shared vmport IO page returned error %d handle=%p", 128493d43e7eSAnthony Xu errno, xen_xc); 128593d43e7eSAnthony Xu goto err; 128693d43e7eSAnthony Xu } 128793d43e7eSAnthony Xu } else if (rc != -ENOSYS) { 128893d43e7eSAnthony Xu error_report("get vmport regs pfn returned error %d, rc=%d", 128993d43e7eSAnthony Xu errno, rc); 129093d43e7eSAnthony Xu goto err; 129193d43e7eSAnthony Xu } 129293d43e7eSAnthony Xu 129393d43e7eSAnthony Xu state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid, 129493d43e7eSAnthony Xu PROT_READ|PROT_WRITE, 129593d43e7eSAnthony Xu 1, &bufioreq_pfn, NULL); 129693d43e7eSAnthony Xu if (state->buffered_io_page == NULL) { 129793d43e7eSAnthony Xu error_report("map buffered IO page returned error %d", errno); 129893d43e7eSAnthony Xu goto err; 129993d43e7eSAnthony Xu } 130093d43e7eSAnthony Xu 130193d43e7eSAnthony Xu /* Note: cpus is empty at this point in init */ 130293d43e7eSAnthony Xu state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *)); 130393d43e7eSAnthony Xu 130493d43e7eSAnthony Xu rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true); 130593d43e7eSAnthony Xu if (rc < 0) { 130693d43e7eSAnthony Xu error_report("failed to enable ioreq server info: error %d handle=%p", 130793d43e7eSAnthony Xu errno, xen_xc); 130893d43e7eSAnthony Xu goto err; 130993d43e7eSAnthony Xu } 131093d43e7eSAnthony Xu 131193d43e7eSAnthony Xu state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t)); 131293d43e7eSAnthony Xu 131393d43e7eSAnthony Xu /* FIXME: how about if we overflow the page here? */ 131493d43e7eSAnthony Xu for (i = 0; i < max_cpus; i++) { 131593d43e7eSAnthony Xu rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid, 131693d43e7eSAnthony Xu xen_vcpu_eport(state->shared_page, i)); 131793d43e7eSAnthony Xu if (rc == -1) { 131893d43e7eSAnthony Xu error_report("shared evtchn %d bind error %d", i, errno); 131993d43e7eSAnthony Xu goto err; 132093d43e7eSAnthony Xu } 132193d43e7eSAnthony Xu state->ioreq_local_port[i] = rc; 132293d43e7eSAnthony Xu } 132393d43e7eSAnthony Xu 132493d43e7eSAnthony Xu rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid, 132593d43e7eSAnthony Xu bufioreq_evtchn); 132693d43e7eSAnthony Xu if (rc == -1) { 132793d43e7eSAnthony Xu error_report("buffered evtchn bind error %d", errno); 132893d43e7eSAnthony Xu goto err; 132993d43e7eSAnthony Xu } 133093d43e7eSAnthony Xu state->bufioreq_local_port = rc; 133193d43e7eSAnthony Xu 133293d43e7eSAnthony Xu /* Init RAM management */ 133393d43e7eSAnthony Xu xen_map_cache_init(xen_phys_offset_to_gaddr, state); 133493d43e7eSAnthony Xu xen_ram_init(pcms, ram_size, ram_memory); 133593d43e7eSAnthony Xu 133693d43e7eSAnthony Xu qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state); 133793d43e7eSAnthony Xu 133893d43e7eSAnthony Xu state->memory_listener = xen_memory_listener; 133993d43e7eSAnthony Xu QLIST_INIT(&state->physmap); 134093d43e7eSAnthony Xu memory_listener_register(&state->memory_listener, &address_space_memory); 134193d43e7eSAnthony Xu state->log_for_dirtybit = NULL; 134293d43e7eSAnthony Xu 134393d43e7eSAnthony Xu state->io_listener = xen_io_listener; 134493d43e7eSAnthony Xu memory_listener_register(&state->io_listener, &address_space_io); 134593d43e7eSAnthony Xu 134693d43e7eSAnthony Xu state->device_listener = xen_device_listener; 134793d43e7eSAnthony Xu device_listener_register(&state->device_listener); 134893d43e7eSAnthony Xu 134993d43e7eSAnthony Xu /* Initialize backend core & drivers */ 135093d43e7eSAnthony Xu if (xen_be_init() != 0) { 135193d43e7eSAnthony Xu error_report("xen backend core setup failed"); 135293d43e7eSAnthony Xu goto err; 135393d43e7eSAnthony Xu } 135493d43e7eSAnthony Xu xen_be_register_common(); 135593d43e7eSAnthony Xu xen_read_physmap(state); 135693d43e7eSAnthony Xu 135793d43e7eSAnthony Xu /* Disable ACPI build because Xen handles it */ 135893d43e7eSAnthony Xu pcms->acpi_build_enabled = false; 135993d43e7eSAnthony Xu 136093d43e7eSAnthony Xu return; 136193d43e7eSAnthony Xu 136293d43e7eSAnthony Xu err: 136393d43e7eSAnthony Xu error_report("xen hardware virtual machine initialisation failed"); 136493d43e7eSAnthony Xu exit(1); 136593d43e7eSAnthony Xu } 136693d43e7eSAnthony Xu 136793d43e7eSAnthony Xu void destroy_hvm_domain(bool reboot) 136893d43e7eSAnthony Xu { 136993d43e7eSAnthony Xu xc_interface *xc_handle; 137093d43e7eSAnthony Xu int sts; 137193d43e7eSAnthony Xu 137293d43e7eSAnthony Xu xc_handle = xc_interface_open(0, 0, 0); 137393d43e7eSAnthony Xu if (xc_handle == NULL) { 137493d43e7eSAnthony Xu fprintf(stderr, "Cannot acquire xenctrl handle\n"); 137593d43e7eSAnthony Xu } else { 137693d43e7eSAnthony Xu sts = xc_domain_shutdown(xc_handle, xen_domid, 137793d43e7eSAnthony Xu reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff); 137893d43e7eSAnthony Xu if (sts != 0) { 137993d43e7eSAnthony Xu fprintf(stderr, "xc_domain_shutdown failed to issue %s, " 138093d43e7eSAnthony Xu "sts %d, %s\n", reboot ? "reboot" : "poweroff", 138193d43e7eSAnthony Xu sts, strerror(errno)); 138293d43e7eSAnthony Xu } else { 138393d43e7eSAnthony Xu fprintf(stderr, "Issued domain %d %s\n", xen_domid, 138493d43e7eSAnthony Xu reboot ? "reboot" : "poweroff"); 138593d43e7eSAnthony Xu } 138693d43e7eSAnthony Xu xc_interface_close(xc_handle); 138793d43e7eSAnthony Xu } 138893d43e7eSAnthony Xu } 138993d43e7eSAnthony Xu 139093d43e7eSAnthony Xu void xen_register_framebuffer(MemoryRegion *mr) 139193d43e7eSAnthony Xu { 139293d43e7eSAnthony Xu framebuffer = mr; 139393d43e7eSAnthony Xu } 139493d43e7eSAnthony Xu 139593d43e7eSAnthony Xu void xen_shutdown_fatal_error(const char *fmt, ...) 139693d43e7eSAnthony Xu { 139793d43e7eSAnthony Xu va_list ap; 139893d43e7eSAnthony Xu 139993d43e7eSAnthony Xu va_start(ap, fmt); 140093d43e7eSAnthony Xu vfprintf(stderr, fmt, ap); 140193d43e7eSAnthony Xu va_end(ap); 140293d43e7eSAnthony Xu fprintf(stderr, "Will destroy the domain.\n"); 140393d43e7eSAnthony Xu /* destroy the domain */ 1404cf83f140SEric Blake qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR); 140593d43e7eSAnthony Xu } 140693d43e7eSAnthony Xu 140793d43e7eSAnthony Xu void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length) 140893d43e7eSAnthony Xu { 140993d43e7eSAnthony Xu if (unlikely(xen_in_migration)) { 141093d43e7eSAnthony Xu int rc; 141193d43e7eSAnthony Xu ram_addr_t start_pfn, nb_pages; 141293d43e7eSAnthony Xu 141393d43e7eSAnthony Xu if (length == 0) { 141493d43e7eSAnthony Xu length = TARGET_PAGE_SIZE; 141593d43e7eSAnthony Xu } 141693d43e7eSAnthony Xu start_pfn = start >> TARGET_PAGE_BITS; 141793d43e7eSAnthony Xu nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS) 141893d43e7eSAnthony Xu - start_pfn; 141993d43e7eSAnthony Xu rc = xen_modified_memory(xen_domid, start_pfn, nb_pages); 142093d43e7eSAnthony Xu if (rc) { 142193d43e7eSAnthony Xu fprintf(stderr, 142293d43e7eSAnthony Xu "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n", 142393d43e7eSAnthony Xu __func__, start, nb_pages, rc, strerror(-rc)); 142493d43e7eSAnthony Xu } 142593d43e7eSAnthony Xu } 142693d43e7eSAnthony Xu } 142793d43e7eSAnthony Xu 142893d43e7eSAnthony Xu void qmp_xen_set_global_dirty_log(bool enable, Error **errp) 142993d43e7eSAnthony Xu { 143093d43e7eSAnthony Xu if (enable) { 143193d43e7eSAnthony Xu memory_global_dirty_log_start(); 143293d43e7eSAnthony Xu } else { 143393d43e7eSAnthony Xu memory_global_dirty_log_stop(); 143493d43e7eSAnthony Xu } 143593d43e7eSAnthony Xu } 1436