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