2 * Copyright (C) 2010 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
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.
11 #include "qemu/osdep.h"
14 #include "hw/pci/pci.h"
15 #include "hw/i386/pc.h"
16 #include "hw/i386/apic-msidef.h"
17 #include "hw/xen/xen_common.h"
18 #include "hw/xen/xen_backend.h"
19 #include "qmp-commands.h"
21 #include "qemu/error-report.h"
22 #include "qemu/range.h"
23 #include "sysemu/xen-mapcache.h"
25 #include "exec/address-spaces.h"
27 #include <xen/hvm/ioreq.h>
28 #include <xen/hvm/params.h>
29 #include <xen/hvm/e820.h>
31 //#define DEBUG_XEN_HVM
34 #define DPRINTF(fmt, ...) \
35 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...) \
41 static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
42 static MemoryRegion *framebuffer;
43 static bool xen_in_migration;
45 /* Compatibility with older version */
47 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
48 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
49 * needs to be included before this block and hw/xen/xen_common.h needs to
50 * be included before xen/hvm/ioreq.h
52 #ifndef IOREQ_TYPE_VMWARE_PORT
53 #define IOREQ_TYPE_VMWARE_PORT 3
61 typedef struct vmware_regs vmware_regs_t;
63 struct shared_vmport_iopage {
64 struct vmware_regs vcpu_vmport_regs[1];
66 typedef struct shared_vmport_iopage shared_vmport_iopage_t;
69 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
71 return shared_page->vcpu_ioreq[i].vp_eport;
73 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
75 return &shared_page->vcpu_ioreq[vcpu];
78 #define BUFFER_IO_MAX_DELAY 100
80 typedef struct XenPhysmap {
86 QLIST_ENTRY(XenPhysmap) list;
89 typedef struct XenIOState {
91 shared_iopage_t *shared_page;
92 shared_vmport_iopage_t *shared_vmport_page;
93 buffered_iopage_t *buffered_io_page;
94 QEMUTimer *buffered_io_timer;
95 CPUState **cpu_by_vcpu_id;
96 /* the evtchn port for polling the notification, */
97 evtchn_port_t *ioreq_local_port;
98 /* evtchn local port for buffered io */
99 evtchn_port_t bufioreq_local_port;
100 /* the evtchn fd for polling */
101 xenevtchn_handle *xce_handle;
102 /* which vcpu we are serving */
105 struct xs_handle *xenstore;
106 MemoryListener memory_listener;
107 MemoryListener io_listener;
108 DeviceListener device_listener;
109 QLIST_HEAD(, XenPhysmap) physmap;
110 hwaddr free_phys_offset;
111 const XenPhysmap *log_for_dirtybit;
118 /* Xen specific function for piix pci */
120 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
122 return irq_num + ((pci_dev->devfn >> 3) << 2);
125 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
127 xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
131 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
135 /* Scan for updates to PCI link routes (0x60-0x63). */
136 for (i = 0; i < len; i++) {
137 uint8_t v = (val >> (8 * i)) & 0xff;
142 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
143 xen_set_pci_link_route(xen_domid, address + i - 0x60, v);
148 int xen_is_pirq_msi(uint32_t msi_data)
150 /* If vector is 0, the msi is remapped into a pirq, passed as
153 return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
156 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
158 xen_inject_msi(xen_domid, addr, data);
161 static void xen_suspend_notifier(Notifier *notifier, void *data)
163 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
166 /* Xen Interrupt Controller */
168 static void xen_set_irq(void *opaque, int irq, int level)
170 xen_set_isa_irq_level(xen_domid, irq, level);
173 qemu_irq *xen_interrupt_controller_init(void)
175 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
180 static void xen_ram_init(PCMachineState *pcms,
181 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
183 MemoryRegion *sysmem = get_system_memory();
184 ram_addr_t block_len;
185 uint64_t user_lowmem = object_property_get_uint(qdev_get_machine(),
186 PC_MACHINE_MAX_RAM_BELOW_4G,
189 /* Handle the machine opt max-ram-below-4g. It is basically doing
190 * min(xen limit, user limit).
193 user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
195 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
196 user_lowmem = HVM_BELOW_4G_RAM_END;
199 if (ram_size >= user_lowmem) {
200 pcms->above_4g_mem_size = ram_size - user_lowmem;
201 pcms->below_4g_mem_size = user_lowmem;
203 pcms->above_4g_mem_size = 0;
204 pcms->below_4g_mem_size = ram_size;
206 if (!pcms->above_4g_mem_size) {
207 block_len = ram_size;
210 * Xen does not allocate the memory continuously, it keeps a
211 * hole of the size computed above or passed in.
213 block_len = (1ULL << 32) + pcms->above_4g_mem_size;
215 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
217 *ram_memory_p = &ram_memory;
219 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
220 &ram_memory, 0, 0xa0000);
221 memory_region_add_subregion(sysmem, 0, &ram_640k);
222 /* Skip of the VGA IO memory space, it will be registered later by the VGA
225 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
226 * the Options ROM, so it is registered here as RAM.
228 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
229 &ram_memory, 0xc0000,
230 pcms->below_4g_mem_size - 0xc0000);
231 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
232 if (pcms->above_4g_mem_size > 0) {
233 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
234 &ram_memory, 0x100000000ULL,
235 pcms->above_4g_mem_size);
236 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
240 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
243 unsigned long nr_pfn;
247 if (runstate_check(RUN_STATE_INMIGRATE)) {
248 /* RAM already populated in Xen */
249 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
250 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
251 __func__, size, ram_addr);
255 if (mr == &ram_memory) {
259 trace_xen_ram_alloc(ram_addr, size);
261 nr_pfn = size >> TARGET_PAGE_BITS;
262 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
264 for (i = 0; i < nr_pfn; i++) {
265 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
268 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
269 error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
276 static XenPhysmap *get_physmapping(XenIOState *state,
277 hwaddr start_addr, ram_addr_t size)
279 XenPhysmap *physmap = NULL;
281 start_addr &= TARGET_PAGE_MASK;
283 QLIST_FOREACH(physmap, &state->physmap, list) {
284 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
291 static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
292 ram_addr_t size, void *opaque)
294 hwaddr addr = start_addr & TARGET_PAGE_MASK;
295 XenIOState *xen_io_state = opaque;
296 XenPhysmap *physmap = NULL;
298 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
299 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
300 return physmap->start_addr;
307 static int xen_add_to_physmap(XenIOState *state,
311 hwaddr offset_within_region)
315 XenPhysmap *physmap = NULL;
316 hwaddr pfn, start_gpfn;
317 hwaddr phys_offset = memory_region_get_ram_addr(mr);
318 char path[80], value[17];
321 if (get_physmapping(state, start_addr, size)) {
328 /* Xen can only handle a single dirty log region for now and we want
329 * the linear framebuffer to be that region.
330 * Avoid tracking any regions that is not videoram and avoid tracking
331 * the legacy vga region. */
332 if (mr == framebuffer && start_addr > 0xbffff) {
338 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
339 start_addr, start_addr + size);
341 pfn = phys_offset >> TARGET_PAGE_BITS;
342 start_gpfn = start_addr >> TARGET_PAGE_BITS;
343 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
344 unsigned long idx = pfn + i;
345 xen_pfn_t gpfn = start_gpfn + i;
347 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
349 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
350 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
355 mr_name = memory_region_name(mr);
357 physmap = g_malloc(sizeof (XenPhysmap));
359 physmap->start_addr = start_addr;
360 physmap->size = size;
361 physmap->name = mr_name;
362 physmap->phys_offset = phys_offset;
364 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
366 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
367 start_addr >> TARGET_PAGE_BITS,
368 (start_addr + size - 1) >> TARGET_PAGE_BITS,
369 XEN_DOMCTL_MEM_CACHEATTR_WB);
371 snprintf(path, sizeof(path),
372 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
373 xen_domid, (uint64_t)phys_offset);
374 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
375 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
378 snprintf(path, sizeof(path),
379 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
380 xen_domid, (uint64_t)phys_offset);
381 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
382 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
386 snprintf(path, sizeof(path),
387 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
388 xen_domid, (uint64_t)phys_offset);
389 if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
397 static int xen_remove_from_physmap(XenIOState *state,
403 XenPhysmap *physmap = NULL;
404 hwaddr phys_offset = 0;
406 physmap = get_physmapping(state, start_addr, size);
407 if (physmap == NULL) {
411 phys_offset = physmap->phys_offset;
412 size = physmap->size;
414 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
415 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
417 size >>= TARGET_PAGE_BITS;
418 start_addr >>= TARGET_PAGE_BITS;
419 phys_offset >>= TARGET_PAGE_BITS;
420 for (i = 0; i < size; i++) {
421 xen_pfn_t idx = start_addr + i;
422 xen_pfn_t gpfn = phys_offset + i;
424 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
426 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
427 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
432 QLIST_REMOVE(physmap, list);
433 if (state->log_for_dirtybit == physmap) {
434 state->log_for_dirtybit = NULL;
441 static void xen_set_memory(struct MemoryListener *listener,
442 MemoryRegionSection *section,
445 XenIOState *state = container_of(listener, XenIOState, memory_listener);
446 hwaddr start_addr = section->offset_within_address_space;
447 ram_addr_t size = int128_get64(section->size);
448 bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
449 hvmmem_type_t mem_type;
451 if (section->mr == &ram_memory) {
455 xen_map_memory_section(xen_domid, state->ioservid,
458 xen_unmap_memory_section(xen_domid, state->ioservid,
463 if (!memory_region_is_ram(section->mr)) {
467 if (log_dirty != add) {
471 trace_xen_client_set_memory(start_addr, size, log_dirty);
473 start_addr &= TARGET_PAGE_MASK;
474 size = TARGET_PAGE_ALIGN(size);
477 if (!memory_region_is_rom(section->mr)) {
478 xen_add_to_physmap(state, start_addr, size,
479 section->mr, section->offset_within_region);
481 mem_type = HVMMEM_ram_ro;
482 if (xen_set_mem_type(xen_domid, mem_type,
483 start_addr >> TARGET_PAGE_BITS,
484 size >> TARGET_PAGE_BITS)) {
485 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx"\n",
490 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
491 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
496 static void xen_region_add(MemoryListener *listener,
497 MemoryRegionSection *section)
499 memory_region_ref(section->mr);
500 xen_set_memory(listener, section, true);
503 static void xen_region_del(MemoryListener *listener,
504 MemoryRegionSection *section)
506 xen_set_memory(listener, section, false);
507 memory_region_unref(section->mr);
510 static void xen_io_add(MemoryListener *listener,
511 MemoryRegionSection *section)
513 XenIOState *state = container_of(listener, XenIOState, io_listener);
514 MemoryRegion *mr = section->mr;
516 if (mr->ops == &unassigned_io_ops) {
520 memory_region_ref(mr);
522 xen_map_io_section(xen_domid, state->ioservid, section);
525 static void xen_io_del(MemoryListener *listener,
526 MemoryRegionSection *section)
528 XenIOState *state = container_of(listener, XenIOState, io_listener);
529 MemoryRegion *mr = section->mr;
531 if (mr->ops == &unassigned_io_ops) {
535 xen_unmap_io_section(xen_domid, state->ioservid, section);
537 memory_region_unref(mr);
540 static void xen_device_realize(DeviceListener *listener,
543 XenIOState *state = container_of(listener, XenIOState, device_listener);
545 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
546 PCIDevice *pci_dev = PCI_DEVICE(dev);
548 xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
552 static void xen_device_unrealize(DeviceListener *listener,
555 XenIOState *state = container_of(listener, XenIOState, device_listener);
557 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
558 PCIDevice *pci_dev = PCI_DEVICE(dev);
560 xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
564 static void xen_sync_dirty_bitmap(XenIOState *state,
568 hwaddr npages = size >> TARGET_PAGE_BITS;
569 const int width = sizeof(unsigned long) * 8;
570 unsigned long bitmap[DIV_ROUND_UP(npages, width)];
572 const XenPhysmap *physmap = NULL;
574 physmap = get_physmapping(state, start_addr, size);
575 if (physmap == NULL) {
580 if (state->log_for_dirtybit == NULL) {
581 state->log_for_dirtybit = physmap;
582 } else if (state->log_for_dirtybit != physmap) {
583 /* Only one range for dirty bitmap can be tracked. */
587 rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
591 #define ENODATA ENOENT
593 if (errno == ENODATA) {
594 memory_region_set_dirty(framebuffer, 0, size);
595 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
596 ", 0x" TARGET_FMT_plx "): %s\n",
597 start_addr, start_addr + size, strerror(errno));
602 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
603 unsigned long map = bitmap[i];
607 memory_region_set_dirty(framebuffer,
608 (i * width + j) * TARGET_PAGE_SIZE,
614 static void xen_log_start(MemoryListener *listener,
615 MemoryRegionSection *section,
618 XenIOState *state = container_of(listener, XenIOState, memory_listener);
620 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
621 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
622 int128_get64(section->size));
626 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
629 XenIOState *state = container_of(listener, XenIOState, memory_listener);
631 if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
632 state->log_for_dirtybit = NULL;
633 /* Disable dirty bit tracking */
634 xen_track_dirty_vram(xen_domid, 0, 0, NULL);
638 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
640 XenIOState *state = container_of(listener, XenIOState, memory_listener);
642 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
643 int128_get64(section->size));
646 static void xen_log_global_start(MemoryListener *listener)
649 xen_in_migration = true;
653 static void xen_log_global_stop(MemoryListener *listener)
655 xen_in_migration = false;
658 static MemoryListener xen_memory_listener = {
659 .region_add = xen_region_add,
660 .region_del = xen_region_del,
661 .log_start = xen_log_start,
662 .log_stop = xen_log_stop,
663 .log_sync = xen_log_sync,
664 .log_global_start = xen_log_global_start,
665 .log_global_stop = xen_log_global_stop,
669 static MemoryListener xen_io_listener = {
670 .region_add = xen_io_add,
671 .region_del = xen_io_del,
675 static DeviceListener xen_device_listener = {
676 .realize = xen_device_realize,
677 .unrealize = xen_device_unrealize,
680 /* get the ioreq packets from share mem */
681 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
683 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
685 if (req->state != STATE_IOREQ_READY) {
686 DPRINTF("I/O request not ready: "
687 "%x, ptr: %x, port: %"PRIx64", "
688 "data: %"PRIx64", count: %u, size: %u\n",
689 req->state, req->data_is_ptr, req->addr,
690 req->data, req->count, req->size);
694 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
696 req->state = STATE_IOREQ_INPROCESS;
700 /* use poll to get the port notification */
701 /* ioreq_vec--out,the */
702 /* retval--the number of ioreq packet */
703 static ioreq_t *cpu_get_ioreq(XenIOState *state)
708 port = xenevtchn_pending(state->xce_handle);
709 if (port == state->bufioreq_local_port) {
710 timer_mod(state->buffered_io_timer,
711 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
716 for (i = 0; i < max_cpus; i++) {
717 if (state->ioreq_local_port[i] == port) {
723 hw_error("Fatal error while trying to get io event!\n");
726 /* unmask the wanted port again */
727 xenevtchn_unmask(state->xce_handle, port);
729 /* get the io packet from shared memory */
730 state->send_vcpu = i;
731 return cpu_get_ioreq_from_shared_memory(state, i);
734 /* read error or read nothing */
738 static uint32_t do_inp(uint32_t addr, unsigned long size)
742 return cpu_inb(addr);
744 return cpu_inw(addr);
746 return cpu_inl(addr);
748 hw_error("inp: bad size: %04x %lx", addr, size);
752 static void do_outp(uint32_t addr,
753 unsigned long size, uint32_t val)
757 return cpu_outb(addr, val);
759 return cpu_outw(addr, val);
761 return cpu_outl(addr, val);
763 hw_error("outp: bad size: %04x %lx", addr, size);
768 * Helper functions which read/write an object from/to physical guest
769 * memory, as part of the implementation of an ioreq.
772 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
773 * val, req->size, 0/1)
774 * except without the integer overflow problems.
776 static void rw_phys_req_item(hwaddr addr,
777 ioreq_t *req, uint32_t i, void *val, int rw)
779 /* Do everything unsigned so overflow just results in a truncated result
780 * and accesses to undesired parts of guest memory, which is up
782 hwaddr offset = (hwaddr)req->size * i;
788 cpu_physical_memory_rw(addr, val, req->size, rw);
791 static inline void read_phys_req_item(hwaddr addr,
792 ioreq_t *req, uint32_t i, void *val)
794 rw_phys_req_item(addr, req, i, val, 0);
796 static inline void write_phys_req_item(hwaddr addr,
797 ioreq_t *req, uint32_t i, void *val)
799 rw_phys_req_item(addr, req, i, val, 1);
803 static void cpu_ioreq_pio(ioreq_t *req)
807 trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
808 req->data, req->count, req->size);
810 if (req->size > sizeof(uint32_t)) {
811 hw_error("PIO: bad size (%u)", req->size);
814 if (req->dir == IOREQ_READ) {
815 if (!req->data_is_ptr) {
816 req->data = do_inp(req->addr, req->size);
817 trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
822 for (i = 0; i < req->count; i++) {
823 tmp = do_inp(req->addr, req->size);
824 write_phys_req_item(req->data, req, i, &tmp);
827 } else if (req->dir == IOREQ_WRITE) {
828 if (!req->data_is_ptr) {
829 trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
831 do_outp(req->addr, req->size, req->data);
833 for (i = 0; i < req->count; i++) {
836 read_phys_req_item(req->data, req, i, &tmp);
837 do_outp(req->addr, req->size, tmp);
843 static void cpu_ioreq_move(ioreq_t *req)
847 trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
848 req->data, req->count, req->size);
850 if (req->size > sizeof(req->data)) {
851 hw_error("MMIO: bad size (%u)", req->size);
854 if (!req->data_is_ptr) {
855 if (req->dir == IOREQ_READ) {
856 for (i = 0; i < req->count; i++) {
857 read_phys_req_item(req->addr, req, i, &req->data);
859 } else if (req->dir == IOREQ_WRITE) {
860 for (i = 0; i < req->count; i++) {
861 write_phys_req_item(req->addr, req, i, &req->data);
867 if (req->dir == IOREQ_READ) {
868 for (i = 0; i < req->count; i++) {
869 read_phys_req_item(req->addr, req, i, &tmp);
870 write_phys_req_item(req->data, req, i, &tmp);
872 } else if (req->dir == IOREQ_WRITE) {
873 for (i = 0; i < req->count; i++) {
874 read_phys_req_item(req->data, req, i, &tmp);
875 write_phys_req_item(req->addr, req, i, &tmp);
881 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
886 cpu = X86_CPU(current_cpu);
888 env->regs[R_EAX] = req->data;
889 env->regs[R_EBX] = vmport_regs->ebx;
890 env->regs[R_ECX] = vmport_regs->ecx;
891 env->regs[R_EDX] = vmport_regs->edx;
892 env->regs[R_ESI] = vmport_regs->esi;
893 env->regs[R_EDI] = vmport_regs->edi;
896 static void regs_from_cpu(vmware_regs_t *vmport_regs)
898 X86CPU *cpu = X86_CPU(current_cpu);
899 CPUX86State *env = &cpu->env;
901 vmport_regs->ebx = env->regs[R_EBX];
902 vmport_regs->ecx = env->regs[R_ECX];
903 vmport_regs->edx = env->regs[R_EDX];
904 vmport_regs->esi = env->regs[R_ESI];
905 vmport_regs->edi = env->regs[R_EDI];
908 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
910 vmware_regs_t *vmport_regs;
912 assert(state->shared_vmport_page);
914 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
915 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
917 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
918 regs_to_cpu(vmport_regs, req);
920 regs_from_cpu(vmport_regs);
924 static void handle_ioreq(XenIOState *state, ioreq_t *req)
926 trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
927 req->addr, req->data, req->count, req->size);
929 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
930 (req->size < sizeof (target_ulong))) {
931 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
934 if (req->dir == IOREQ_WRITE)
935 trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
936 req->addr, req->data, req->count, req->size);
942 case IOREQ_TYPE_COPY:
945 case IOREQ_TYPE_VMWARE_PORT:
946 handle_vmport_ioreq(state, req);
948 case IOREQ_TYPE_TIMEOFFSET:
950 case IOREQ_TYPE_INVALIDATE:
951 xen_invalidate_map_cache();
953 case IOREQ_TYPE_PCI_CONFIG: {
954 uint32_t sbdf = req->addr >> 32;
957 /* Fake a write to port 0xCF8 so that
958 * the config space access will target the
959 * correct device model.
962 ((req->addr & 0x0f00) << 16) |
963 ((sbdf & 0xffff) << 8) |
965 do_outp(0xcf8, 4, val);
967 /* Now issue the config space access via
970 req->addr = 0xcfc | (req->addr & 0x03);
975 hw_error("Invalid ioreq type 0x%x\n", req->type);
977 if (req->dir == IOREQ_READ) {
978 trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
979 req->addr, req->data, req->count, req->size);
983 static int handle_buffered_iopage(XenIOState *state)
985 buffered_iopage_t *buf_page = state->buffered_io_page;
986 buf_ioreq_t *buf_req = NULL;
994 memset(&req, 0x00, sizeof(req));
995 req.state = STATE_IOREQ_READY;
997 req.dir = IOREQ_WRITE;
1000 uint32_t rdptr = buf_page->read_pointer, wrptr;
1003 wrptr = buf_page->write_pointer;
1005 if (rdptr != buf_page->read_pointer) {
1008 if (rdptr == wrptr) {
1011 buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1012 req.size = 1U << buf_req->size;
1013 req.addr = buf_req->addr;
1014 req.data = buf_req->data;
1015 req.type = buf_req->type;
1017 qw = (req.size == 8);
1019 if (rdptr + 1 == wrptr) {
1020 hw_error("Incomplete quad word buffered ioreq");
1022 buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1023 IOREQ_BUFFER_SLOT_NUM];
1024 req.data |= ((uint64_t)buf_req->data) << 32;
1028 handle_ioreq(state, &req);
1030 /* Only req.data may get updated by handle_ioreq(), albeit even that
1031 * should not happen as such data would never make it to the guest (we
1032 * can only usefully see writes here after all).
1034 assert(req.state == STATE_IOREQ_READY);
1035 assert(req.count == 1);
1036 assert(req.dir == IOREQ_WRITE);
1037 assert(!req.data_is_ptr);
1039 atomic_add(&buf_page->read_pointer, qw + 1);
1045 static void handle_buffered_io(void *opaque)
1047 XenIOState *state = opaque;
1049 if (handle_buffered_iopage(state)) {
1050 timer_mod(state->buffered_io_timer,
1051 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1053 timer_del(state->buffered_io_timer);
1054 xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1058 static void cpu_handle_ioreq(void *opaque)
1060 XenIOState *state = opaque;
1061 ioreq_t *req = cpu_get_ioreq(state);
1063 handle_buffered_iopage(state);
1065 ioreq_t copy = *req;
1068 handle_ioreq(state, ©);
1069 req->data = copy.data;
1071 if (req->state != STATE_IOREQ_INPROCESS) {
1072 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1073 "%x, ptr: %x, port: %"PRIx64", "
1074 "data: %"PRIx64", count: %u, size: %u, type: %u\n",
1075 req->state, req->data_is_ptr, req->addr,
1076 req->data, req->count, req->size, req->type);
1077 destroy_hvm_domain(false);
1081 xen_wmb(); /* Update ioreq contents /then/ update state. */
1084 * We do this before we send the response so that the tools
1085 * have the opportunity to pick up on the reset before the
1086 * guest resumes and does a hlt with interrupts disabled which
1087 * causes Xen to powerdown the domain.
1089 if (runstate_is_running()) {
1090 ShutdownCause request;
1092 if (qemu_shutdown_requested_get()) {
1093 destroy_hvm_domain(false);
1095 request = qemu_reset_requested_get();
1097 qemu_system_reset(request);
1098 destroy_hvm_domain(true);
1102 req->state = STATE_IORESP_READY;
1103 xenevtchn_notify(state->xce_handle,
1104 state->ioreq_local_port[state->send_vcpu]);
1108 static void xen_main_loop_prepare(XenIOState *state)
1112 if (state->xce_handle != NULL) {
1113 evtchn_fd = xenevtchn_fd(state->xce_handle);
1116 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1119 if (evtchn_fd != -1) {
1120 CPUState *cpu_state;
1122 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1123 CPU_FOREACH(cpu_state) {
1124 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1125 __func__, cpu_state->cpu_index, cpu_state);
1126 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1128 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1133 static void xen_hvm_change_state_handler(void *opaque, int running,
1136 XenIOState *state = opaque;
1139 xen_main_loop_prepare(state);
1142 xen_set_ioreq_server_state(xen_domid,
1144 (rstate == RUN_STATE_RUNNING));
1147 static void xen_exit_notifier(Notifier *n, void *data)
1149 XenIOState *state = container_of(n, XenIOState, exit);
1151 xenevtchn_close(state->xce_handle);
1152 xs_daemon_close(state->xenstore);
1155 static void xen_read_physmap(XenIOState *state)
1157 XenPhysmap *physmap = NULL;
1158 unsigned int len, num, i;
1159 char path[80], *value = NULL;
1160 char **entries = NULL;
1162 snprintf(path, sizeof(path),
1163 "/local/domain/0/device-model/%d/physmap", xen_domid);
1164 entries = xs_directory(state->xenstore, 0, path, &num);
1165 if (entries == NULL)
1168 for (i = 0; i < num; i++) {
1169 physmap = g_malloc(sizeof (XenPhysmap));
1170 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1171 snprintf(path, sizeof(path),
1172 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1173 xen_domid, entries[i]);
1174 value = xs_read(state->xenstore, 0, path, &len);
1175 if (value == NULL) {
1179 physmap->start_addr = strtoull(value, NULL, 16);
1182 snprintf(path, sizeof(path),
1183 "/local/domain/0/device-model/%d/physmap/%s/size",
1184 xen_domid, entries[i]);
1185 value = xs_read(state->xenstore, 0, path, &len);
1186 if (value == NULL) {
1190 physmap->size = strtoull(value, NULL, 16);
1193 snprintf(path, sizeof(path),
1194 "/local/domain/0/device-model/%d/physmap/%s/name",
1195 xen_domid, entries[i]);
1196 physmap->name = xs_read(state->xenstore, 0, path, &len);
1198 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1203 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1205 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1208 void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory)
1211 xen_pfn_t ioreq_pfn;
1212 xen_pfn_t bufioreq_pfn;
1213 evtchn_port_t bufioreq_evtchn;
1216 state = g_malloc0(sizeof (XenIOState));
1218 state->xce_handle = xenevtchn_open(NULL, 0);
1219 if (state->xce_handle == NULL) {
1220 perror("xen: event channel open");
1224 state->xenstore = xs_daemon_open();
1225 if (state->xenstore == NULL) {
1226 perror("xen: xenstore open");
1230 if (xen_domid_restrict) {
1231 rc = xen_restrict(xen_domid);
1233 error_report("failed to restrict: error %d", errno);
1238 xen_create_ioreq_server(xen_domid, &state->ioservid);
1240 state->exit.notify = xen_exit_notifier;
1241 qemu_add_exit_notifier(&state->exit);
1243 state->suspend.notify = xen_suspend_notifier;
1244 qemu_register_suspend_notifier(&state->suspend);
1246 state->wakeup.notify = xen_wakeup_notifier;
1247 qemu_register_wakeup_notifier(&state->wakeup);
1249 rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1250 &ioreq_pfn, &bufioreq_pfn,
1253 error_report("failed to get ioreq server info: error %d handle=%p",
1258 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1259 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1260 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1262 state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1263 PROT_READ|PROT_WRITE,
1264 1, &ioreq_pfn, NULL);
1265 if (state->shared_page == NULL) {
1266 error_report("map shared IO page returned error %d handle=%p",
1271 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1273 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1274 state->shared_vmport_page =
1275 xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1276 1, &ioreq_pfn, NULL);
1277 if (state->shared_vmport_page == NULL) {
1278 error_report("map shared vmport IO page returned error %d handle=%p",
1282 } else if (rc != -ENOSYS) {
1283 error_report("get vmport regs pfn returned error %d, rc=%d",
1288 state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1289 PROT_READ|PROT_WRITE,
1290 1, &bufioreq_pfn, NULL);
1291 if (state->buffered_io_page == NULL) {
1292 error_report("map buffered IO page returned error %d", errno);
1296 /* Note: cpus is empty at this point in init */
1297 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1299 rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
1301 error_report("failed to enable ioreq server info: error %d handle=%p",
1306 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1308 /* FIXME: how about if we overflow the page here? */
1309 for (i = 0; i < max_cpus; i++) {
1310 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1311 xen_vcpu_eport(state->shared_page, i));
1313 error_report("shared evtchn %d bind error %d", i, errno);
1316 state->ioreq_local_port[i] = rc;
1319 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1322 error_report("buffered evtchn bind error %d", errno);
1325 state->bufioreq_local_port = rc;
1327 /* Init RAM management */
1328 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1329 xen_ram_init(pcms, ram_size, ram_memory);
1331 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1333 state->memory_listener = xen_memory_listener;
1334 QLIST_INIT(&state->physmap);
1335 memory_listener_register(&state->memory_listener, &address_space_memory);
1336 state->log_for_dirtybit = NULL;
1338 state->io_listener = xen_io_listener;
1339 memory_listener_register(&state->io_listener, &address_space_io);
1341 state->device_listener = xen_device_listener;
1342 device_listener_register(&state->device_listener);
1344 /* Initialize backend core & drivers */
1345 if (xen_be_init() != 0) {
1346 error_report("xen backend core setup failed");
1349 xen_be_register_common();
1350 xen_read_physmap(state);
1352 /* Disable ACPI build because Xen handles it */
1353 pcms->acpi_build_enabled = false;
1358 error_report("xen hardware virtual machine initialisation failed");
1362 void destroy_hvm_domain(bool reboot)
1364 xc_interface *xc_handle;
1367 xc_handle = xc_interface_open(0, 0, 0);
1368 if (xc_handle == NULL) {
1369 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1371 sts = xc_domain_shutdown(xc_handle, xen_domid,
1372 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
1374 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1375 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1376 sts, strerror(errno));
1378 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1379 reboot ? "reboot" : "poweroff");
1381 xc_interface_close(xc_handle);
1385 void xen_register_framebuffer(MemoryRegion *mr)
1390 void xen_shutdown_fatal_error(const char *fmt, ...)
1395 vfprintf(stderr, fmt, ap);
1397 fprintf(stderr, "Will destroy the domain.\n");
1398 /* destroy the domain */
1399 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
1402 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
1404 if (unlikely(xen_in_migration)) {
1406 ram_addr_t start_pfn, nb_pages;
1409 length = TARGET_PAGE_SIZE;
1411 start_pfn = start >> TARGET_PAGE_BITS;
1412 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1414 rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
1417 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1418 __func__, start, nb_pages, rc, strerror(-rc));
1423 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1426 memory_global_dirty_log_start();
1428 memory_global_dirty_log_stop();