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 "qapi/error.h"
20 #include "qapi/qapi-commands-misc.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 remote and local ports for buffered io */
99 evtchn_port_t bufioreq_remote_port;
100 evtchn_port_t bufioreq_local_port;
101 /* the evtchn fd for polling */
102 xenevtchn_handle *xce_handle;
103 /* which vcpu we are serving */
106 struct xs_handle *xenstore;
107 MemoryListener memory_listener;
108 MemoryListener io_listener;
109 DeviceListener device_listener;
110 QLIST_HEAD(, XenPhysmap) physmap;
111 hwaddr free_phys_offset;
112 const XenPhysmap *log_for_dirtybit;
119 /* Xen specific function for piix pci */
121 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
123 return irq_num + ((pci_dev->devfn >> 3) << 2);
126 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
128 xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
132 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
136 /* Scan for updates to PCI link routes (0x60-0x63). */
137 for (i = 0; i < len; i++) {
138 uint8_t v = (val >> (8 * i)) & 0xff;
143 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
144 xen_set_pci_link_route(xen_domid, address + i - 0x60, v);
149 int xen_is_pirq_msi(uint32_t msi_data)
151 /* If vector is 0, the msi is remapped into a pirq, passed as
154 return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
157 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
159 xen_inject_msi(xen_domid, addr, data);
162 static void xen_suspend_notifier(Notifier *notifier, void *data)
164 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
167 /* Xen Interrupt Controller */
169 static void xen_set_irq(void *opaque, int irq, int level)
171 xen_set_isa_irq_level(xen_domid, irq, level);
174 qemu_irq *xen_interrupt_controller_init(void)
176 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
181 static void xen_ram_init(PCMachineState *pcms,
182 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
184 MemoryRegion *sysmem = get_system_memory();
185 ram_addr_t block_len;
186 uint64_t user_lowmem = object_property_get_uint(qdev_get_machine(),
187 PC_MACHINE_MAX_RAM_BELOW_4G,
190 /* Handle the machine opt max-ram-below-4g. It is basically doing
191 * min(xen limit, user limit).
194 user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
196 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
197 user_lowmem = HVM_BELOW_4G_RAM_END;
200 if (ram_size >= user_lowmem) {
201 pcms->above_4g_mem_size = ram_size - user_lowmem;
202 pcms->below_4g_mem_size = user_lowmem;
204 pcms->above_4g_mem_size = 0;
205 pcms->below_4g_mem_size = ram_size;
207 if (!pcms->above_4g_mem_size) {
208 block_len = ram_size;
211 * Xen does not allocate the memory continuously, it keeps a
212 * hole of the size computed above or passed in.
214 block_len = (1ULL << 32) + pcms->above_4g_mem_size;
216 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
218 *ram_memory_p = &ram_memory;
220 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
221 &ram_memory, 0, 0xa0000);
222 memory_region_add_subregion(sysmem, 0, &ram_640k);
223 /* Skip of the VGA IO memory space, it will be registered later by the VGA
226 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
227 * the Options ROM, so it is registered here as RAM.
229 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
230 &ram_memory, 0xc0000,
231 pcms->below_4g_mem_size - 0xc0000);
232 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
233 if (pcms->above_4g_mem_size > 0) {
234 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
235 &ram_memory, 0x100000000ULL,
236 pcms->above_4g_mem_size);
237 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
241 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
244 unsigned long nr_pfn;
248 if (runstate_check(RUN_STATE_INMIGRATE)) {
249 /* RAM already populated in Xen */
250 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
251 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
252 __func__, size, ram_addr);
256 if (mr == &ram_memory) {
260 trace_xen_ram_alloc(ram_addr, size);
262 nr_pfn = size >> TARGET_PAGE_BITS;
263 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
265 for (i = 0; i < nr_pfn; i++) {
266 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
269 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
270 error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
277 static XenPhysmap *get_physmapping(XenIOState *state,
278 hwaddr start_addr, ram_addr_t size)
280 XenPhysmap *physmap = NULL;
282 start_addr &= TARGET_PAGE_MASK;
284 QLIST_FOREACH(physmap, &state->physmap, list) {
285 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
292 #ifdef XEN_COMPAT_PHYSMAP
293 static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
294 ram_addr_t size, void *opaque)
296 hwaddr addr = start_addr & TARGET_PAGE_MASK;
297 XenIOState *xen_io_state = opaque;
298 XenPhysmap *physmap = NULL;
300 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
301 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
302 return physmap->start_addr;
309 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
311 char path[80], value[17];
313 snprintf(path, sizeof(path),
314 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
315 xen_domid, (uint64_t)physmap->phys_offset);
316 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
317 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
320 snprintf(path, sizeof(path),
321 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
322 xen_domid, (uint64_t)physmap->phys_offset);
323 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
324 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
328 snprintf(path, sizeof(path),
329 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
330 xen_domid, (uint64_t)physmap->phys_offset);
331 if (!xs_write(state->xenstore, 0, path,
332 physmap->name, strlen(physmap->name))) {
339 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
345 static int xen_add_to_physmap(XenIOState *state,
349 hwaddr offset_within_region)
351 unsigned long nr_pages;
353 XenPhysmap *physmap = NULL;
354 hwaddr pfn, start_gpfn;
355 hwaddr phys_offset = memory_region_get_ram_addr(mr);
358 if (get_physmapping(state, start_addr, size)) {
365 /* Xen can only handle a single dirty log region for now and we want
366 * the linear framebuffer to be that region.
367 * Avoid tracking any regions that is not videoram and avoid tracking
368 * the legacy vga region. */
369 if (mr == framebuffer && start_addr > 0xbffff) {
375 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
376 start_addr, start_addr + size);
378 mr_name = memory_region_name(mr);
380 physmap = g_malloc(sizeof(XenPhysmap));
382 physmap->start_addr = start_addr;
383 physmap->size = size;
384 physmap->name = mr_name;
385 physmap->phys_offset = phys_offset;
387 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
389 if (runstate_check(RUN_STATE_INMIGRATE)) {
390 /* Now when we have a physmap entry we can replace a dummy mapping with
391 * a real one of guest foreign memory. */
392 uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
393 assert(p && p == memory_region_get_ram_ptr(mr));
398 pfn = phys_offset >> TARGET_PAGE_BITS;
399 start_gpfn = start_addr >> TARGET_PAGE_BITS;
400 nr_pages = size >> TARGET_PAGE_BITS;
401 rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
404 int saved_errno = errno;
406 error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
407 " to GFN %"HWADDR_PRIx" failed: %s",
408 nr_pages, pfn, start_gpfn, strerror(saved_errno));
413 rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
414 start_addr >> TARGET_PAGE_BITS,
415 (start_addr + size - 1) >> TARGET_PAGE_BITS,
416 XEN_DOMCTL_MEM_CACHEATTR_WB);
418 error_report("pin_memory_cacheattr failed: %s", strerror(errno));
420 return xen_save_physmap(state, physmap);
423 static int xen_remove_from_physmap(XenIOState *state,
428 XenPhysmap *physmap = NULL;
429 hwaddr phys_offset = 0;
431 physmap = get_physmapping(state, start_addr, size);
432 if (physmap == NULL) {
436 phys_offset = physmap->phys_offset;
437 size = physmap->size;
439 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
440 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
442 size >>= TARGET_PAGE_BITS;
443 start_addr >>= TARGET_PAGE_BITS;
444 phys_offset >>= TARGET_PAGE_BITS;
445 rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
448 int saved_errno = errno;
450 error_report("relocate_memory "RAM_ADDR_FMT" pages"
451 " from GFN %"HWADDR_PRIx
452 " to GFN %"HWADDR_PRIx" failed: %s",
453 size, start_addr, phys_offset, strerror(saved_errno));
458 QLIST_REMOVE(physmap, list);
459 if (state->log_for_dirtybit == physmap) {
460 state->log_for_dirtybit = NULL;
467 static void xen_set_memory(struct MemoryListener *listener,
468 MemoryRegionSection *section,
471 XenIOState *state = container_of(listener, XenIOState, memory_listener);
472 hwaddr start_addr = section->offset_within_address_space;
473 ram_addr_t size = int128_get64(section->size);
474 bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
475 hvmmem_type_t mem_type;
477 if (section->mr == &ram_memory) {
481 xen_map_memory_section(xen_domid, state->ioservid,
484 xen_unmap_memory_section(xen_domid, state->ioservid,
489 if (!memory_region_is_ram(section->mr)) {
493 if (log_dirty != add) {
497 trace_xen_client_set_memory(start_addr, size, log_dirty);
499 start_addr &= TARGET_PAGE_MASK;
500 size = TARGET_PAGE_ALIGN(size);
503 if (!memory_region_is_rom(section->mr)) {
504 xen_add_to_physmap(state, start_addr, size,
505 section->mr, section->offset_within_region);
507 mem_type = HVMMEM_ram_ro;
508 if (xen_set_mem_type(xen_domid, mem_type,
509 start_addr >> TARGET_PAGE_BITS,
510 size >> TARGET_PAGE_BITS)) {
511 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx"\n",
516 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
517 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
522 static void xen_region_add(MemoryListener *listener,
523 MemoryRegionSection *section)
525 memory_region_ref(section->mr);
526 xen_set_memory(listener, section, true);
529 static void xen_region_del(MemoryListener *listener,
530 MemoryRegionSection *section)
532 xen_set_memory(listener, section, false);
533 memory_region_unref(section->mr);
536 static void xen_io_add(MemoryListener *listener,
537 MemoryRegionSection *section)
539 XenIOState *state = container_of(listener, XenIOState, io_listener);
540 MemoryRegion *mr = section->mr;
542 if (mr->ops == &unassigned_io_ops) {
546 memory_region_ref(mr);
548 xen_map_io_section(xen_domid, state->ioservid, section);
551 static void xen_io_del(MemoryListener *listener,
552 MemoryRegionSection *section)
554 XenIOState *state = container_of(listener, XenIOState, io_listener);
555 MemoryRegion *mr = section->mr;
557 if (mr->ops == &unassigned_io_ops) {
561 xen_unmap_io_section(xen_domid, state->ioservid, section);
563 memory_region_unref(mr);
566 static void xen_device_realize(DeviceListener *listener,
569 XenIOState *state = container_of(listener, XenIOState, device_listener);
571 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
572 PCIDevice *pci_dev = PCI_DEVICE(dev);
574 xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
578 static void xen_device_unrealize(DeviceListener *listener,
581 XenIOState *state = container_of(listener, XenIOState, device_listener);
583 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
584 PCIDevice *pci_dev = PCI_DEVICE(dev);
586 xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
590 static void xen_sync_dirty_bitmap(XenIOState *state,
594 hwaddr npages = size >> TARGET_PAGE_BITS;
595 const int width = sizeof(unsigned long) * 8;
596 unsigned long bitmap[DIV_ROUND_UP(npages, width)];
598 const XenPhysmap *physmap = NULL;
600 physmap = get_physmapping(state, start_addr, size);
601 if (physmap == NULL) {
606 if (state->log_for_dirtybit == NULL) {
607 state->log_for_dirtybit = physmap;
608 } else if (state->log_for_dirtybit != physmap) {
609 /* Only one range for dirty bitmap can be tracked. */
613 rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
617 #define ENODATA ENOENT
619 if (errno == ENODATA) {
620 memory_region_set_dirty(framebuffer, 0, size);
621 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
622 ", 0x" TARGET_FMT_plx "): %s\n",
623 start_addr, start_addr + size, strerror(errno));
628 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
629 unsigned long map = bitmap[i];
633 memory_region_set_dirty(framebuffer,
634 (i * width + j) * TARGET_PAGE_SIZE,
640 static void xen_log_start(MemoryListener *listener,
641 MemoryRegionSection *section,
644 XenIOState *state = container_of(listener, XenIOState, memory_listener);
646 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
647 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
648 int128_get64(section->size));
652 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
655 XenIOState *state = container_of(listener, XenIOState, memory_listener);
657 if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
658 state->log_for_dirtybit = NULL;
659 /* Disable dirty bit tracking */
660 xen_track_dirty_vram(xen_domid, 0, 0, NULL);
664 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
666 XenIOState *state = container_of(listener, XenIOState, memory_listener);
668 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
669 int128_get64(section->size));
672 static void xen_log_global_start(MemoryListener *listener)
675 xen_in_migration = true;
679 static void xen_log_global_stop(MemoryListener *listener)
681 xen_in_migration = false;
684 static MemoryListener xen_memory_listener = {
685 .region_add = xen_region_add,
686 .region_del = xen_region_del,
687 .log_start = xen_log_start,
688 .log_stop = xen_log_stop,
689 .log_sync = xen_log_sync,
690 .log_global_start = xen_log_global_start,
691 .log_global_stop = xen_log_global_stop,
695 static MemoryListener xen_io_listener = {
696 .region_add = xen_io_add,
697 .region_del = xen_io_del,
701 static DeviceListener xen_device_listener = {
702 .realize = xen_device_realize,
703 .unrealize = xen_device_unrealize,
706 /* get the ioreq packets from share mem */
707 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
709 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
711 if (req->state != STATE_IOREQ_READY) {
712 DPRINTF("I/O request not ready: "
713 "%x, ptr: %x, port: %"PRIx64", "
714 "data: %"PRIx64", count: %u, size: %u\n",
715 req->state, req->data_is_ptr, req->addr,
716 req->data, req->count, req->size);
720 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
722 req->state = STATE_IOREQ_INPROCESS;
726 /* use poll to get the port notification */
727 /* ioreq_vec--out,the */
728 /* retval--the number of ioreq packet */
729 static ioreq_t *cpu_get_ioreq(XenIOState *state)
734 port = xenevtchn_pending(state->xce_handle);
735 if (port == state->bufioreq_local_port) {
736 timer_mod(state->buffered_io_timer,
737 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
742 for (i = 0; i < max_cpus; i++) {
743 if (state->ioreq_local_port[i] == port) {
749 hw_error("Fatal error while trying to get io event!\n");
752 /* unmask the wanted port again */
753 xenevtchn_unmask(state->xce_handle, port);
755 /* get the io packet from shared memory */
756 state->send_vcpu = i;
757 return cpu_get_ioreq_from_shared_memory(state, i);
760 /* read error or read nothing */
764 static uint32_t do_inp(uint32_t addr, unsigned long size)
768 return cpu_inb(addr);
770 return cpu_inw(addr);
772 return cpu_inl(addr);
774 hw_error("inp: bad size: %04x %lx", addr, size);
778 static void do_outp(uint32_t addr,
779 unsigned long size, uint32_t val)
783 return cpu_outb(addr, val);
785 return cpu_outw(addr, val);
787 return cpu_outl(addr, val);
789 hw_error("outp: bad size: %04x %lx", addr, size);
794 * Helper functions which read/write an object from/to physical guest
795 * memory, as part of the implementation of an ioreq.
798 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
799 * val, req->size, 0/1)
800 * except without the integer overflow problems.
802 static void rw_phys_req_item(hwaddr addr,
803 ioreq_t *req, uint32_t i, void *val, int rw)
805 /* Do everything unsigned so overflow just results in a truncated result
806 * and accesses to undesired parts of guest memory, which is up
808 hwaddr offset = (hwaddr)req->size * i;
814 cpu_physical_memory_rw(addr, val, req->size, rw);
817 static inline void read_phys_req_item(hwaddr addr,
818 ioreq_t *req, uint32_t i, void *val)
820 rw_phys_req_item(addr, req, i, val, 0);
822 static inline void write_phys_req_item(hwaddr addr,
823 ioreq_t *req, uint32_t i, void *val)
825 rw_phys_req_item(addr, req, i, val, 1);
829 static void cpu_ioreq_pio(ioreq_t *req)
833 trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
834 req->data, req->count, req->size);
836 if (req->size > sizeof(uint32_t)) {
837 hw_error("PIO: bad size (%u)", req->size);
840 if (req->dir == IOREQ_READ) {
841 if (!req->data_is_ptr) {
842 req->data = do_inp(req->addr, req->size);
843 trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
848 for (i = 0; i < req->count; i++) {
849 tmp = do_inp(req->addr, req->size);
850 write_phys_req_item(req->data, req, i, &tmp);
853 } else if (req->dir == IOREQ_WRITE) {
854 if (!req->data_is_ptr) {
855 trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
857 do_outp(req->addr, req->size, req->data);
859 for (i = 0; i < req->count; i++) {
862 read_phys_req_item(req->data, req, i, &tmp);
863 do_outp(req->addr, req->size, tmp);
869 static void cpu_ioreq_move(ioreq_t *req)
873 trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
874 req->data, req->count, req->size);
876 if (req->size > sizeof(req->data)) {
877 hw_error("MMIO: bad size (%u)", req->size);
880 if (!req->data_is_ptr) {
881 if (req->dir == IOREQ_READ) {
882 for (i = 0; i < req->count; i++) {
883 read_phys_req_item(req->addr, req, i, &req->data);
885 } else if (req->dir == IOREQ_WRITE) {
886 for (i = 0; i < req->count; i++) {
887 write_phys_req_item(req->addr, req, i, &req->data);
893 if (req->dir == IOREQ_READ) {
894 for (i = 0; i < req->count; i++) {
895 read_phys_req_item(req->addr, req, i, &tmp);
896 write_phys_req_item(req->data, req, i, &tmp);
898 } else if (req->dir == IOREQ_WRITE) {
899 for (i = 0; i < req->count; i++) {
900 read_phys_req_item(req->data, req, i, &tmp);
901 write_phys_req_item(req->addr, req, i, &tmp);
907 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
912 cpu = X86_CPU(current_cpu);
914 env->regs[R_EAX] = req->data;
915 env->regs[R_EBX] = vmport_regs->ebx;
916 env->regs[R_ECX] = vmport_regs->ecx;
917 env->regs[R_EDX] = vmport_regs->edx;
918 env->regs[R_ESI] = vmport_regs->esi;
919 env->regs[R_EDI] = vmport_regs->edi;
922 static void regs_from_cpu(vmware_regs_t *vmport_regs)
924 X86CPU *cpu = X86_CPU(current_cpu);
925 CPUX86State *env = &cpu->env;
927 vmport_regs->ebx = env->regs[R_EBX];
928 vmport_regs->ecx = env->regs[R_ECX];
929 vmport_regs->edx = env->regs[R_EDX];
930 vmport_regs->esi = env->regs[R_ESI];
931 vmport_regs->edi = env->regs[R_EDI];
934 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
936 vmware_regs_t *vmport_regs;
938 assert(state->shared_vmport_page);
940 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
941 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
943 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
944 regs_to_cpu(vmport_regs, req);
946 regs_from_cpu(vmport_regs);
950 static void handle_ioreq(XenIOState *state, ioreq_t *req)
952 trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
953 req->addr, req->data, req->count, req->size);
955 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
956 (req->size < sizeof (target_ulong))) {
957 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
960 if (req->dir == IOREQ_WRITE)
961 trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
962 req->addr, req->data, req->count, req->size);
968 case IOREQ_TYPE_COPY:
971 case IOREQ_TYPE_VMWARE_PORT:
972 handle_vmport_ioreq(state, req);
974 case IOREQ_TYPE_TIMEOFFSET:
976 case IOREQ_TYPE_INVALIDATE:
977 xen_invalidate_map_cache();
979 case IOREQ_TYPE_PCI_CONFIG: {
980 uint32_t sbdf = req->addr >> 32;
983 /* Fake a write to port 0xCF8 so that
984 * the config space access will target the
985 * correct device model.
988 ((req->addr & 0x0f00) << 16) |
989 ((sbdf & 0xffff) << 8) |
991 do_outp(0xcf8, 4, val);
993 /* Now issue the config space access via
996 req->addr = 0xcfc | (req->addr & 0x03);
1001 hw_error("Invalid ioreq type 0x%x\n", req->type);
1003 if (req->dir == IOREQ_READ) {
1004 trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
1005 req->addr, req->data, req->count, req->size);
1009 static int handle_buffered_iopage(XenIOState *state)
1011 buffered_iopage_t *buf_page = state->buffered_io_page;
1012 buf_ioreq_t *buf_req = NULL;
1020 memset(&req, 0x00, sizeof(req));
1021 req.state = STATE_IOREQ_READY;
1023 req.dir = IOREQ_WRITE;
1026 uint32_t rdptr = buf_page->read_pointer, wrptr;
1029 wrptr = buf_page->write_pointer;
1031 if (rdptr != buf_page->read_pointer) {
1034 if (rdptr == wrptr) {
1037 buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1038 req.size = 1U << buf_req->size;
1039 req.addr = buf_req->addr;
1040 req.data = buf_req->data;
1041 req.type = buf_req->type;
1043 qw = (req.size == 8);
1045 if (rdptr + 1 == wrptr) {
1046 hw_error("Incomplete quad word buffered ioreq");
1048 buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1049 IOREQ_BUFFER_SLOT_NUM];
1050 req.data |= ((uint64_t)buf_req->data) << 32;
1054 handle_ioreq(state, &req);
1056 /* Only req.data may get updated by handle_ioreq(), albeit even that
1057 * should not happen as such data would never make it to the guest (we
1058 * can only usefully see writes here after all).
1060 assert(req.state == STATE_IOREQ_READY);
1061 assert(req.count == 1);
1062 assert(req.dir == IOREQ_WRITE);
1063 assert(!req.data_is_ptr);
1065 atomic_add(&buf_page->read_pointer, qw + 1);
1071 static void handle_buffered_io(void *opaque)
1073 XenIOState *state = opaque;
1075 if (handle_buffered_iopage(state)) {
1076 timer_mod(state->buffered_io_timer,
1077 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1079 timer_del(state->buffered_io_timer);
1080 xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1084 static void cpu_handle_ioreq(void *opaque)
1086 XenIOState *state = opaque;
1087 ioreq_t *req = cpu_get_ioreq(state);
1089 handle_buffered_iopage(state);
1091 ioreq_t copy = *req;
1094 handle_ioreq(state, ©);
1095 req->data = copy.data;
1097 if (req->state != STATE_IOREQ_INPROCESS) {
1098 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1099 "%x, ptr: %x, port: %"PRIx64", "
1100 "data: %"PRIx64", count: %u, size: %u, type: %u\n",
1101 req->state, req->data_is_ptr, req->addr,
1102 req->data, req->count, req->size, req->type);
1103 destroy_hvm_domain(false);
1107 xen_wmb(); /* Update ioreq contents /then/ update state. */
1110 * We do this before we send the response so that the tools
1111 * have the opportunity to pick up on the reset before the
1112 * guest resumes and does a hlt with interrupts disabled which
1113 * causes Xen to powerdown the domain.
1115 if (runstate_is_running()) {
1116 ShutdownCause request;
1118 if (qemu_shutdown_requested_get()) {
1119 destroy_hvm_domain(false);
1121 request = qemu_reset_requested_get();
1123 qemu_system_reset(request);
1124 destroy_hvm_domain(true);
1128 req->state = STATE_IORESP_READY;
1129 xenevtchn_notify(state->xce_handle,
1130 state->ioreq_local_port[state->send_vcpu]);
1134 static void xen_main_loop_prepare(XenIOState *state)
1138 if (state->xce_handle != NULL) {
1139 evtchn_fd = xenevtchn_fd(state->xce_handle);
1142 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1145 if (evtchn_fd != -1) {
1146 CPUState *cpu_state;
1148 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1149 CPU_FOREACH(cpu_state) {
1150 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1151 __func__, cpu_state->cpu_index, cpu_state);
1152 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1154 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1159 static void xen_hvm_change_state_handler(void *opaque, int running,
1162 XenIOState *state = opaque;
1165 xen_main_loop_prepare(state);
1168 xen_set_ioreq_server_state(xen_domid,
1170 (rstate == RUN_STATE_RUNNING));
1173 static void xen_exit_notifier(Notifier *n, void *data)
1175 XenIOState *state = container_of(n, XenIOState, exit);
1177 xenevtchn_close(state->xce_handle);
1178 xs_daemon_close(state->xenstore);
1181 #ifdef XEN_COMPAT_PHYSMAP
1182 static void xen_read_physmap(XenIOState *state)
1184 XenPhysmap *physmap = NULL;
1185 unsigned int len, num, i;
1186 char path[80], *value = NULL;
1187 char **entries = NULL;
1189 snprintf(path, sizeof(path),
1190 "/local/domain/0/device-model/%d/physmap", xen_domid);
1191 entries = xs_directory(state->xenstore, 0, path, &num);
1192 if (entries == NULL)
1195 for (i = 0; i < num; i++) {
1196 physmap = g_malloc(sizeof (XenPhysmap));
1197 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1198 snprintf(path, sizeof(path),
1199 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1200 xen_domid, entries[i]);
1201 value = xs_read(state->xenstore, 0, path, &len);
1202 if (value == NULL) {
1206 physmap->start_addr = strtoull(value, NULL, 16);
1209 snprintf(path, sizeof(path),
1210 "/local/domain/0/device-model/%d/physmap/%s/size",
1211 xen_domid, entries[i]);
1212 value = xs_read(state->xenstore, 0, path, &len);
1213 if (value == NULL) {
1217 physmap->size = strtoull(value, NULL, 16);
1220 snprintf(path, sizeof(path),
1221 "/local/domain/0/device-model/%d/physmap/%s/name",
1222 xen_domid, entries[i]);
1223 physmap->name = xs_read(state->xenstore, 0, path, &len);
1225 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1230 static void xen_read_physmap(XenIOState *state)
1235 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1237 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1240 static int xen_map_ioreq_server(XenIOState *state)
1242 xen_pfn_t ioreq_pfn;
1243 xen_pfn_t bufioreq_pfn;
1244 evtchn_port_t bufioreq_evtchn;
1247 rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1248 &ioreq_pfn, &bufioreq_pfn,
1251 error_report("failed to get ioreq server info: error %d handle=%p",
1256 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1257 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1258 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1260 state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1261 PROT_READ | PROT_WRITE,
1262 1, &ioreq_pfn, NULL);
1263 if (state->shared_page == NULL) {
1264 error_report("map shared IO page returned error %d handle=%p",
1269 state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1270 PROT_READ | PROT_WRITE,
1271 1, &bufioreq_pfn, NULL);
1272 if (state->buffered_io_page == NULL) {
1273 error_report("map buffered IO page returned error %d", errno);
1277 state->bufioreq_remote_port = bufioreq_evtchn;
1282 void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory)
1285 xen_pfn_t ioreq_pfn;
1288 state = g_malloc0(sizeof (XenIOState));
1290 state->xce_handle = xenevtchn_open(NULL, 0);
1291 if (state->xce_handle == NULL) {
1292 perror("xen: event channel open");
1296 state->xenstore = xs_daemon_open();
1297 if (state->xenstore == NULL) {
1298 perror("xen: xenstore open");
1302 xen_create_ioreq_server(xen_domid, &state->ioservid);
1304 state->exit.notify = xen_exit_notifier;
1305 qemu_add_exit_notifier(&state->exit);
1307 state->suspend.notify = xen_suspend_notifier;
1308 qemu_register_suspend_notifier(&state->suspend);
1310 state->wakeup.notify = xen_wakeup_notifier;
1311 qemu_register_wakeup_notifier(&state->wakeup);
1313 rc = xen_map_ioreq_server(state);
1318 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1320 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1321 state->shared_vmport_page =
1322 xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1323 1, &ioreq_pfn, NULL);
1324 if (state->shared_vmport_page == NULL) {
1325 error_report("map shared vmport IO page returned error %d handle=%p",
1329 } else if (rc != -ENOSYS) {
1330 error_report("get vmport regs pfn returned error %d, rc=%d",
1335 /* Note: cpus is empty at this point in init */
1336 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1338 rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
1340 error_report("failed to enable ioreq server info: error %d handle=%p",
1345 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1347 /* FIXME: how about if we overflow the page here? */
1348 for (i = 0; i < max_cpus; i++) {
1349 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1350 xen_vcpu_eport(state->shared_page, i));
1352 error_report("shared evtchn %d bind error %d", i, errno);
1355 state->ioreq_local_port[i] = rc;
1358 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1359 state->bufioreq_remote_port);
1361 error_report("buffered evtchn bind error %d", errno);
1364 state->bufioreq_local_port = rc;
1366 /* Init RAM management */
1367 #ifdef XEN_COMPAT_PHYSMAP
1368 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1370 xen_map_cache_init(NULL, state);
1372 xen_ram_init(pcms, ram_size, ram_memory);
1374 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1376 state->memory_listener = xen_memory_listener;
1377 QLIST_INIT(&state->physmap);
1378 memory_listener_register(&state->memory_listener, &address_space_memory);
1379 state->log_for_dirtybit = NULL;
1381 state->io_listener = xen_io_listener;
1382 memory_listener_register(&state->io_listener, &address_space_io);
1384 state->device_listener = xen_device_listener;
1385 device_listener_register(&state->device_listener);
1387 /* Initialize backend core & drivers */
1388 if (xen_be_init() != 0) {
1389 error_report("xen backend core setup failed");
1392 xen_be_register_common();
1393 xen_read_physmap(state);
1395 /* Disable ACPI build because Xen handles it */
1396 pcms->acpi_build_enabled = false;
1401 error_report("xen hardware virtual machine initialisation failed");
1405 void destroy_hvm_domain(bool reboot)
1407 xc_interface *xc_handle;
1411 unsigned int reason = reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff;
1414 rc = xendevicemodel_shutdown(xen_dmod, xen_domid, reason);
1418 if (errno != ENOTTY /* old Xen */) {
1419 perror("xendevicemodel_shutdown failed");
1421 /* well, try the old thing then */
1424 xc_handle = xc_interface_open(0, 0, 0);
1425 if (xc_handle == NULL) {
1426 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1428 sts = xc_domain_shutdown(xc_handle, xen_domid, reason);
1430 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1431 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1432 sts, strerror(errno));
1434 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1435 reboot ? "reboot" : "poweroff");
1437 xc_interface_close(xc_handle);
1441 void xen_register_framebuffer(MemoryRegion *mr)
1446 void xen_shutdown_fatal_error(const char *fmt, ...)
1451 vfprintf(stderr, fmt, ap);
1453 fprintf(stderr, "Will destroy the domain.\n");
1454 /* destroy the domain */
1455 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
1458 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
1460 if (unlikely(xen_in_migration)) {
1462 ram_addr_t start_pfn, nb_pages;
1465 length = TARGET_PAGE_SIZE;
1467 start_pfn = start >> TARGET_PAGE_BITS;
1468 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1470 rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
1473 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1474 __func__, start, nb_pages, errno, strerror(errno));
1479 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1482 memory_global_dirty_log_start();
1484 memory_global_dirty_log_stop();