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