block: Remove unused struct definition BlockFinishData
[qemu/rayw.git] / xen-hvm.c
blob1c9fb129550010033961c333e383186a50d6f1b6
1 /*
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.
9 */
11 #include "qemu/osdep.h"
12 #include <sys/mman.h>
14 #include "hw/pci/pci.h"
15 #include "hw/i386/pc.h"
16 #include "hw/xen/xen_common.h"
17 #include "hw/xen/xen_backend.h"
18 #include "qmp-commands.h"
20 #include "sysemu/char.h"
21 #include "qemu/error-report.h"
22 #include "qemu/range.h"
23 #include "sysemu/xen-mapcache.h"
24 #include "trace.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
33 #ifdef DEBUG_XEN_HVM
34 #define DPRINTF(fmt, ...) \
35 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
36 #else
37 #define DPRINTF(fmt, ...) \
38 do { } while (0)
39 #endif
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
54 struct vmware_regs {
55 uint32_t esi;
56 uint32_t edi;
57 uint32_t ebx;
58 uint32_t ecx;
59 uint32_t edx;
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;
67 #endif
69 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
70 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
72 return shared_page->vcpu_iodata[i].vp_eport;
74 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
76 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
78 # define FMT_ioreq_size PRIx64
79 #else
80 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
82 return shared_page->vcpu_ioreq[i].vp_eport;
84 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
86 return &shared_page->vcpu_ioreq[vcpu];
88 # define FMT_ioreq_size "u"
89 #endif
91 #define BUFFER_IO_MAX_DELAY 100
93 typedef struct XenPhysmap {
94 hwaddr start_addr;
95 ram_addr_t size;
96 const char *name;
97 hwaddr phys_offset;
99 QLIST_ENTRY(XenPhysmap) list;
100 } XenPhysmap;
102 typedef struct XenIOState {
103 ioservid_t ioservid;
104 shared_iopage_t *shared_page;
105 shared_vmport_iopage_t *shared_vmport_page;
106 buffered_iopage_t *buffered_io_page;
107 QEMUTimer *buffered_io_timer;
108 CPUState **cpu_by_vcpu_id;
109 /* the evtchn port for polling the notification, */
110 evtchn_port_t *ioreq_local_port;
111 /* evtchn local port for buffered io */
112 evtchn_port_t bufioreq_local_port;
113 /* the evtchn fd for polling */
114 xenevtchn_handle *xce_handle;
115 /* which vcpu we are serving */
116 int send_vcpu;
118 struct xs_handle *xenstore;
119 MemoryListener memory_listener;
120 MemoryListener io_listener;
121 DeviceListener device_listener;
122 QLIST_HEAD(, XenPhysmap) physmap;
123 hwaddr free_phys_offset;
124 const XenPhysmap *log_for_dirtybit;
126 Notifier exit;
127 Notifier suspend;
128 Notifier wakeup;
129 } XenIOState;
131 /* Xen specific function for piix pci */
133 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
135 return irq_num + ((pci_dev->devfn >> 3) << 2);
138 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
140 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
141 irq_num & 3, level);
144 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
146 int i;
148 /* Scan for updates to PCI link routes (0x60-0x63). */
149 for (i = 0; i < len; i++) {
150 uint8_t v = (val >> (8 * i)) & 0xff;
151 if (v & 0x80) {
152 v = 0;
154 v &= 0xf;
155 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
156 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
161 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
163 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
166 static void xen_suspend_notifier(Notifier *notifier, void *data)
168 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
171 /* Xen Interrupt Controller */
173 static void xen_set_irq(void *opaque, int irq, int level)
175 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
178 qemu_irq *xen_interrupt_controller_init(void)
180 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
183 /* Memory Ops */
185 static void xen_ram_init(PCMachineState *pcms,
186 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
188 MemoryRegion *sysmem = get_system_memory();
189 ram_addr_t block_len;
190 uint64_t user_lowmem = object_property_get_int(qdev_get_machine(),
191 PC_MACHINE_MAX_RAM_BELOW_4G,
192 &error_abort);
194 /* Handle the machine opt max-ram-below-4g. It is basically doing
195 * min(xen limit, user limit).
197 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
198 user_lowmem = HVM_BELOW_4G_RAM_END;
201 if (ram_size >= user_lowmem) {
202 pcms->above_4g_mem_size = ram_size - user_lowmem;
203 pcms->below_4g_mem_size = user_lowmem;
204 } else {
205 pcms->above_4g_mem_size = 0;
206 pcms->below_4g_mem_size = ram_size;
208 if (!pcms->above_4g_mem_size) {
209 block_len = ram_size;
210 } else {
212 * Xen does not allocate the memory continuously, it keeps a
213 * hole of the size computed above or passed in.
215 block_len = (1ULL << 32) + pcms->above_4g_mem_size;
217 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
218 &error_fatal);
219 *ram_memory_p = &ram_memory;
220 vmstate_register_ram_global(&ram_memory);
222 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
223 &ram_memory, 0, 0xa0000);
224 memory_region_add_subregion(sysmem, 0, &ram_640k);
225 /* Skip of the VGA IO memory space, it will be registered later by the VGA
226 * emulated device.
228 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
229 * the Options ROM, so it is registered here as RAM.
231 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
232 &ram_memory, 0xc0000,
233 pcms->below_4g_mem_size - 0xc0000);
234 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
235 if (pcms->above_4g_mem_size > 0) {
236 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
237 &ram_memory, 0x100000000ULL,
238 pcms->above_4g_mem_size);
239 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
243 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
244 Error **errp)
246 unsigned long nr_pfn;
247 xen_pfn_t *pfn_list;
248 int i;
250 if (runstate_check(RUN_STATE_INMIGRATE)) {
251 /* RAM already populated in Xen */
252 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
253 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
254 __func__, size, ram_addr);
255 return;
258 if (mr == &ram_memory) {
259 return;
262 trace_xen_ram_alloc(ram_addr, size);
264 nr_pfn = size >> TARGET_PAGE_BITS;
265 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
267 for (i = 0; i < nr_pfn; i++) {
268 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
271 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
272 error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
273 ram_addr);
276 g_free(pfn_list);
279 static XenPhysmap *get_physmapping(XenIOState *state,
280 hwaddr start_addr, ram_addr_t size)
282 XenPhysmap *physmap = NULL;
284 start_addr &= TARGET_PAGE_MASK;
286 QLIST_FOREACH(physmap, &state->physmap, list) {
287 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
288 return physmap;
291 return NULL;
294 static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
295 ram_addr_t size, void *opaque)
297 hwaddr addr = start_addr & TARGET_PAGE_MASK;
298 XenIOState *xen_io_state = opaque;
299 XenPhysmap *physmap = NULL;
301 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
302 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
303 return physmap->start_addr;
307 return start_addr;
310 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
311 static int xen_add_to_physmap(XenIOState *state,
312 hwaddr start_addr,
313 ram_addr_t size,
314 MemoryRegion *mr,
315 hwaddr offset_within_region)
317 unsigned long i = 0;
318 int rc = 0;
319 XenPhysmap *physmap = NULL;
320 hwaddr pfn, start_gpfn;
321 hwaddr phys_offset = memory_region_get_ram_addr(mr);
322 char path[80], value[17];
323 const char *mr_name;
325 if (get_physmapping(state, start_addr, size)) {
326 return 0;
328 if (size <= 0) {
329 return -1;
332 /* Xen can only handle a single dirty log region for now and we want
333 * the linear framebuffer to be that region.
334 * Avoid tracking any regions that is not videoram and avoid tracking
335 * the legacy vga region. */
336 if (mr == framebuffer && start_addr > 0xbffff) {
337 goto go_physmap;
339 return -1;
341 go_physmap:
342 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
343 start_addr, start_addr + size);
345 pfn = phys_offset >> TARGET_PAGE_BITS;
346 start_gpfn = start_addr >> TARGET_PAGE_BITS;
347 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
348 unsigned long idx = pfn + i;
349 xen_pfn_t gpfn = start_gpfn + i;
351 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
352 if (rc) {
353 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
354 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
355 return -rc;
359 mr_name = memory_region_name(mr);
361 physmap = g_malloc(sizeof (XenPhysmap));
363 physmap->start_addr = start_addr;
364 physmap->size = size;
365 physmap->name = mr_name;
366 physmap->phys_offset = phys_offset;
368 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
370 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
371 start_addr >> TARGET_PAGE_BITS,
372 (start_addr + size - 1) >> TARGET_PAGE_BITS,
373 XEN_DOMCTL_MEM_CACHEATTR_WB);
375 snprintf(path, sizeof(path),
376 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
377 xen_domid, (uint64_t)phys_offset);
378 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
379 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
380 return -1;
382 snprintf(path, sizeof(path),
383 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
384 xen_domid, (uint64_t)phys_offset);
385 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
386 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
387 return -1;
389 if (mr_name) {
390 snprintf(path, sizeof(path),
391 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
392 xen_domid, (uint64_t)phys_offset);
393 if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
394 return -1;
398 return 0;
401 static int xen_remove_from_physmap(XenIOState *state,
402 hwaddr start_addr,
403 ram_addr_t size)
405 unsigned long i = 0;
406 int rc = 0;
407 XenPhysmap *physmap = NULL;
408 hwaddr phys_offset = 0;
410 physmap = get_physmapping(state, start_addr, size);
411 if (physmap == NULL) {
412 return -1;
415 phys_offset = physmap->phys_offset;
416 size = physmap->size;
418 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
419 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
421 size >>= TARGET_PAGE_BITS;
422 start_addr >>= TARGET_PAGE_BITS;
423 phys_offset >>= TARGET_PAGE_BITS;
424 for (i = 0; i < size; i++) {
425 xen_pfn_t idx = start_addr + i;
426 xen_pfn_t gpfn = phys_offset + i;
428 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
429 if (rc) {
430 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
431 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
432 return -rc;
436 QLIST_REMOVE(physmap, list);
437 if (state->log_for_dirtybit == physmap) {
438 state->log_for_dirtybit = NULL;
440 g_free(physmap);
442 return 0;
445 #else
446 static int xen_add_to_physmap(XenIOState *state,
447 hwaddr start_addr,
448 ram_addr_t size,
449 MemoryRegion *mr,
450 hwaddr offset_within_region)
452 return -ENOSYS;
455 static int xen_remove_from_physmap(XenIOState *state,
456 hwaddr start_addr,
457 ram_addr_t size)
459 return -ENOSYS;
461 #endif
463 static void xen_set_memory(struct MemoryListener *listener,
464 MemoryRegionSection *section,
465 bool add)
467 XenIOState *state = container_of(listener, XenIOState, memory_listener);
468 hwaddr start_addr = section->offset_within_address_space;
469 ram_addr_t size = int128_get64(section->size);
470 bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
471 hvmmem_type_t mem_type;
473 if (section->mr == &ram_memory) {
474 return;
475 } else {
476 if (add) {
477 xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
478 section);
479 } else {
480 xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
481 section);
485 if (!memory_region_is_ram(section->mr)) {
486 return;
489 if (log_dirty != add) {
490 return;
493 trace_xen_client_set_memory(start_addr, size, log_dirty);
495 start_addr &= TARGET_PAGE_MASK;
496 size = TARGET_PAGE_ALIGN(size);
498 if (add) {
499 if (!memory_region_is_rom(section->mr)) {
500 xen_add_to_physmap(state, start_addr, size,
501 section->mr, section->offset_within_region);
502 } else {
503 mem_type = HVMMEM_ram_ro;
504 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
505 start_addr >> TARGET_PAGE_BITS,
506 size >> TARGET_PAGE_BITS)) {
507 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
508 start_addr);
511 } else {
512 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
513 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
518 static void xen_region_add(MemoryListener *listener,
519 MemoryRegionSection *section)
521 memory_region_ref(section->mr);
522 xen_set_memory(listener, section, true);
525 static void xen_region_del(MemoryListener *listener,
526 MemoryRegionSection *section)
528 xen_set_memory(listener, section, false);
529 memory_region_unref(section->mr);
532 static void xen_io_add(MemoryListener *listener,
533 MemoryRegionSection *section)
535 XenIOState *state = container_of(listener, XenIOState, io_listener);
537 memory_region_ref(section->mr);
539 xen_map_io_section(xen_xc, xen_domid, state->ioservid, section);
542 static void xen_io_del(MemoryListener *listener,
543 MemoryRegionSection *section)
545 XenIOState *state = container_of(listener, XenIOState, io_listener);
547 xen_unmap_io_section(xen_xc, xen_domid, state->ioservid, section);
549 memory_region_unref(section->mr);
552 static void xen_device_realize(DeviceListener *listener,
553 DeviceState *dev)
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_map_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
564 static void xen_device_unrealize(DeviceListener *listener,
565 DeviceState *dev)
567 XenIOState *state = container_of(listener, XenIOState, device_listener);
569 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
570 PCIDevice *pci_dev = PCI_DEVICE(dev);
572 xen_unmap_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
576 static void xen_sync_dirty_bitmap(XenIOState *state,
577 hwaddr start_addr,
578 ram_addr_t size)
580 hwaddr npages = size >> TARGET_PAGE_BITS;
581 const int width = sizeof(unsigned long) * 8;
582 unsigned long bitmap[(npages + width - 1) / width];
583 int rc, i, j;
584 const XenPhysmap *physmap = NULL;
586 physmap = get_physmapping(state, start_addr, size);
587 if (physmap == NULL) {
588 /* not handled */
589 return;
592 if (state->log_for_dirtybit == NULL) {
593 state->log_for_dirtybit = physmap;
594 } else if (state->log_for_dirtybit != physmap) {
595 /* Only one range for dirty bitmap can be tracked. */
596 return;
599 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
600 start_addr >> TARGET_PAGE_BITS, npages,
601 bitmap);
602 if (rc < 0) {
603 #ifndef ENODATA
604 #define ENODATA ENOENT
605 #endif
606 if (errno == ENODATA) {
607 memory_region_set_dirty(framebuffer, 0, size);
608 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
609 ", 0x" TARGET_FMT_plx "): %s\n",
610 start_addr, start_addr + size, strerror(errno));
612 return;
615 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
616 unsigned long map = bitmap[i];
617 while (map != 0) {
618 j = ctzl(map);
619 map &= ~(1ul << j);
620 memory_region_set_dirty(framebuffer,
621 (i * width + j) * TARGET_PAGE_SIZE,
622 TARGET_PAGE_SIZE);
627 static void xen_log_start(MemoryListener *listener,
628 MemoryRegionSection *section,
629 int old, int new)
631 XenIOState *state = container_of(listener, XenIOState, memory_listener);
633 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
634 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
635 int128_get64(section->size));
639 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
640 int old, int new)
642 XenIOState *state = container_of(listener, XenIOState, memory_listener);
644 if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
645 state->log_for_dirtybit = NULL;
646 /* Disable dirty bit tracking */
647 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
651 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
653 XenIOState *state = container_of(listener, XenIOState, memory_listener);
655 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
656 int128_get64(section->size));
659 static void xen_log_global_start(MemoryListener *listener)
661 if (xen_enabled()) {
662 xen_in_migration = true;
666 static void xen_log_global_stop(MemoryListener *listener)
668 xen_in_migration = false;
671 static MemoryListener xen_memory_listener = {
672 .region_add = xen_region_add,
673 .region_del = xen_region_del,
674 .log_start = xen_log_start,
675 .log_stop = xen_log_stop,
676 .log_sync = xen_log_sync,
677 .log_global_start = xen_log_global_start,
678 .log_global_stop = xen_log_global_stop,
679 .priority = 10,
682 static MemoryListener xen_io_listener = {
683 .region_add = xen_io_add,
684 .region_del = xen_io_del,
685 .priority = 10,
688 static DeviceListener xen_device_listener = {
689 .realize = xen_device_realize,
690 .unrealize = xen_device_unrealize,
693 /* get the ioreq packets from share mem */
694 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
696 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
698 if (req->state != STATE_IOREQ_READY) {
699 DPRINTF("I/O request not ready: "
700 "%x, ptr: %x, port: %"PRIx64", "
701 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
702 req->state, req->data_is_ptr, req->addr,
703 req->data, req->count, req->size);
704 return NULL;
707 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
709 req->state = STATE_IOREQ_INPROCESS;
710 return req;
713 /* use poll to get the port notification */
714 /* ioreq_vec--out,the */
715 /* retval--the number of ioreq packet */
716 static ioreq_t *cpu_get_ioreq(XenIOState *state)
718 int i;
719 evtchn_port_t port;
721 port = xenevtchn_pending(state->xce_handle);
722 if (port == state->bufioreq_local_port) {
723 timer_mod(state->buffered_io_timer,
724 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
725 return NULL;
728 if (port != -1) {
729 for (i = 0; i < max_cpus; i++) {
730 if (state->ioreq_local_port[i] == port) {
731 break;
735 if (i == max_cpus) {
736 hw_error("Fatal error while trying to get io event!\n");
739 /* unmask the wanted port again */
740 xenevtchn_unmask(state->xce_handle, port);
742 /* get the io packet from shared memory */
743 state->send_vcpu = i;
744 return cpu_get_ioreq_from_shared_memory(state, i);
747 /* read error or read nothing */
748 return NULL;
751 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
753 switch (size) {
754 case 1:
755 return cpu_inb(addr);
756 case 2:
757 return cpu_inw(addr);
758 case 4:
759 return cpu_inl(addr);
760 default:
761 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
765 static void do_outp(pio_addr_t addr,
766 unsigned long size, uint32_t val)
768 switch (size) {
769 case 1:
770 return cpu_outb(addr, val);
771 case 2:
772 return cpu_outw(addr, val);
773 case 4:
774 return cpu_outl(addr, val);
775 default:
776 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
781 * Helper functions which read/write an object from/to physical guest
782 * memory, as part of the implementation of an ioreq.
784 * Equivalent to
785 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
786 * val, req->size, 0/1)
787 * except without the integer overflow problems.
789 static void rw_phys_req_item(hwaddr addr,
790 ioreq_t *req, uint32_t i, void *val, int rw)
792 /* Do everything unsigned so overflow just results in a truncated result
793 * and accesses to undesired parts of guest memory, which is up
794 * to the guest */
795 hwaddr offset = (hwaddr)req->size * i;
796 if (req->df) {
797 addr -= offset;
798 } else {
799 addr += offset;
801 cpu_physical_memory_rw(addr, val, req->size, rw);
804 static inline void read_phys_req_item(hwaddr addr,
805 ioreq_t *req, uint32_t i, void *val)
807 rw_phys_req_item(addr, req, i, val, 0);
809 static inline void write_phys_req_item(hwaddr addr,
810 ioreq_t *req, uint32_t i, void *val)
812 rw_phys_req_item(addr, req, i, val, 1);
816 static void cpu_ioreq_pio(ioreq_t *req)
818 uint32_t i;
820 trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
821 req->data, req->count, req->size);
823 if (req->dir == IOREQ_READ) {
824 if (!req->data_is_ptr) {
825 req->data = do_inp(req->addr, req->size);
826 trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
827 req->size);
828 } else {
829 uint32_t tmp;
831 for (i = 0; i < req->count; i++) {
832 tmp = do_inp(req->addr, req->size);
833 write_phys_req_item(req->data, req, i, &tmp);
836 } else if (req->dir == IOREQ_WRITE) {
837 if (!req->data_is_ptr) {
838 trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
839 req->size);
840 do_outp(req->addr, req->size, req->data);
841 } else {
842 for (i = 0; i < req->count; i++) {
843 uint32_t tmp = 0;
845 read_phys_req_item(req->data, req, i, &tmp);
846 do_outp(req->addr, req->size, tmp);
852 static void cpu_ioreq_move(ioreq_t *req)
854 uint32_t i;
856 trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
857 req->data, req->count, req->size);
859 if (!req->data_is_ptr) {
860 if (req->dir == IOREQ_READ) {
861 for (i = 0; i < req->count; i++) {
862 read_phys_req_item(req->addr, req, i, &req->data);
864 } else if (req->dir == IOREQ_WRITE) {
865 for (i = 0; i < req->count; i++) {
866 write_phys_req_item(req->addr, req, i, &req->data);
869 } else {
870 uint64_t tmp;
872 if (req->dir == IOREQ_READ) {
873 for (i = 0; i < req->count; i++) {
874 read_phys_req_item(req->addr, req, i, &tmp);
875 write_phys_req_item(req->data, req, i, &tmp);
877 } else if (req->dir == IOREQ_WRITE) {
878 for (i = 0; i < req->count; i++) {
879 read_phys_req_item(req->data, req, i, &tmp);
880 write_phys_req_item(req->addr, req, i, &tmp);
886 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
888 X86CPU *cpu;
889 CPUX86State *env;
891 cpu = X86_CPU(current_cpu);
892 env = &cpu->env;
893 env->regs[R_EAX] = req->data;
894 env->regs[R_EBX] = vmport_regs->ebx;
895 env->regs[R_ECX] = vmport_regs->ecx;
896 env->regs[R_EDX] = vmport_regs->edx;
897 env->regs[R_ESI] = vmport_regs->esi;
898 env->regs[R_EDI] = vmport_regs->edi;
901 static void regs_from_cpu(vmware_regs_t *vmport_regs)
903 X86CPU *cpu = X86_CPU(current_cpu);
904 CPUX86State *env = &cpu->env;
906 vmport_regs->ebx = env->regs[R_EBX];
907 vmport_regs->ecx = env->regs[R_ECX];
908 vmport_regs->edx = env->regs[R_EDX];
909 vmport_regs->esi = env->regs[R_ESI];
910 vmport_regs->edi = env->regs[R_EDI];
913 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
915 vmware_regs_t *vmport_regs;
917 assert(state->shared_vmport_page);
918 vmport_regs =
919 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
920 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
922 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
923 regs_to_cpu(vmport_regs, req);
924 cpu_ioreq_pio(req);
925 regs_from_cpu(vmport_regs);
926 current_cpu = NULL;
929 static void handle_ioreq(XenIOState *state, ioreq_t *req)
931 trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
932 req->addr, req->data, req->count, req->size);
934 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
935 (req->size < sizeof (target_ulong))) {
936 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
939 if (req->dir == IOREQ_WRITE)
940 trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
941 req->addr, req->data, req->count, req->size);
943 switch (req->type) {
944 case IOREQ_TYPE_PIO:
945 cpu_ioreq_pio(req);
946 break;
947 case IOREQ_TYPE_COPY:
948 cpu_ioreq_move(req);
949 break;
950 case IOREQ_TYPE_VMWARE_PORT:
951 handle_vmport_ioreq(state, req);
952 break;
953 case IOREQ_TYPE_TIMEOFFSET:
954 break;
955 case IOREQ_TYPE_INVALIDATE:
956 xen_invalidate_map_cache();
957 break;
958 case IOREQ_TYPE_PCI_CONFIG: {
959 uint32_t sbdf = req->addr >> 32;
960 uint32_t val;
962 /* Fake a write to port 0xCF8 so that
963 * the config space access will target the
964 * correct device model.
966 val = (1u << 31) |
967 ((req->addr & 0x0f00) << 16) |
968 ((sbdf & 0xffff) << 8) |
969 (req->addr & 0xfc);
970 do_outp(0xcf8, 4, val);
972 /* Now issue the config space access via
973 * port 0xCFC
975 req->addr = 0xcfc | (req->addr & 0x03);
976 cpu_ioreq_pio(req);
977 break;
979 default:
980 hw_error("Invalid ioreq type 0x%x\n", req->type);
982 if (req->dir == IOREQ_READ) {
983 trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
984 req->addr, req->data, req->count, req->size);
988 static int handle_buffered_iopage(XenIOState *state)
990 buffered_iopage_t *buf_page = state->buffered_io_page;
991 buf_ioreq_t *buf_req = NULL;
992 ioreq_t req;
993 int qw;
995 if (!buf_page) {
996 return 0;
999 memset(&req, 0x00, sizeof(req));
1001 for (;;) {
1002 uint32_t rdptr = buf_page->read_pointer, wrptr;
1004 xen_rmb();
1005 wrptr = buf_page->write_pointer;
1006 xen_rmb();
1007 if (rdptr != buf_page->read_pointer) {
1008 continue;
1010 if (rdptr == wrptr) {
1011 break;
1013 buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1014 req.size = 1UL << buf_req->size;
1015 req.count = 1;
1016 req.addr = buf_req->addr;
1017 req.data = buf_req->data;
1018 req.state = STATE_IOREQ_READY;
1019 req.dir = buf_req->dir;
1020 req.df = 1;
1021 req.type = buf_req->type;
1022 req.data_is_ptr = 0;
1023 qw = (req.size == 8);
1024 if (qw) {
1025 buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1026 IOREQ_BUFFER_SLOT_NUM];
1027 req.data |= ((uint64_t)buf_req->data) << 32;
1030 handle_ioreq(state, &req);
1032 atomic_add(&buf_page->read_pointer, qw + 1);
1035 return req.count;
1038 static void handle_buffered_io(void *opaque)
1040 XenIOState *state = opaque;
1042 if (handle_buffered_iopage(state)) {
1043 timer_mod(state->buffered_io_timer,
1044 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1045 } else {
1046 timer_del(state->buffered_io_timer);
1047 xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1051 static void cpu_handle_ioreq(void *opaque)
1053 XenIOState *state = opaque;
1054 ioreq_t *req = cpu_get_ioreq(state);
1056 handle_buffered_iopage(state);
1057 if (req) {
1058 handle_ioreq(state, req);
1060 if (req->state != STATE_IOREQ_INPROCESS) {
1061 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1062 "%x, ptr: %x, port: %"PRIx64", "
1063 "data: %"PRIx64", count: %" FMT_ioreq_size
1064 ", size: %" FMT_ioreq_size
1065 ", type: %"FMT_ioreq_size"\n",
1066 req->state, req->data_is_ptr, req->addr,
1067 req->data, req->count, req->size, req->type);
1068 destroy_hvm_domain(false);
1069 return;
1072 xen_wmb(); /* Update ioreq contents /then/ update state. */
1075 * We do this before we send the response so that the tools
1076 * have the opportunity to pick up on the reset before the
1077 * guest resumes and does a hlt with interrupts disabled which
1078 * causes Xen to powerdown the domain.
1080 if (runstate_is_running()) {
1081 if (qemu_shutdown_requested_get()) {
1082 destroy_hvm_domain(false);
1084 if (qemu_reset_requested_get()) {
1085 qemu_system_reset(VMRESET_REPORT);
1086 destroy_hvm_domain(true);
1090 req->state = STATE_IORESP_READY;
1091 xenevtchn_notify(state->xce_handle,
1092 state->ioreq_local_port[state->send_vcpu]);
1096 static void xen_main_loop_prepare(XenIOState *state)
1098 int evtchn_fd = -1;
1100 if (state->xce_handle != NULL) {
1101 evtchn_fd = xenevtchn_fd(state->xce_handle);
1104 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1105 state);
1107 if (evtchn_fd != -1) {
1108 CPUState *cpu_state;
1110 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1111 CPU_FOREACH(cpu_state) {
1112 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1113 __func__, cpu_state->cpu_index, cpu_state);
1114 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1116 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1121 static void xen_hvm_change_state_handler(void *opaque, int running,
1122 RunState rstate)
1124 XenIOState *state = opaque;
1126 if (running) {
1127 xen_main_loop_prepare(state);
1130 xen_set_ioreq_server_state(xen_xc, xen_domid,
1131 state->ioservid,
1132 (rstate == RUN_STATE_RUNNING));
1135 static void xen_exit_notifier(Notifier *n, void *data)
1137 XenIOState *state = container_of(n, XenIOState, exit);
1139 xenevtchn_close(state->xce_handle);
1140 xs_daemon_close(state->xenstore);
1143 static void xen_read_physmap(XenIOState *state)
1145 XenPhysmap *physmap = NULL;
1146 unsigned int len, num, i;
1147 char path[80], *value = NULL;
1148 char **entries = NULL;
1150 snprintf(path, sizeof(path),
1151 "/local/domain/0/device-model/%d/physmap", xen_domid);
1152 entries = xs_directory(state->xenstore, 0, path, &num);
1153 if (entries == NULL)
1154 return;
1156 for (i = 0; i < num; i++) {
1157 physmap = g_malloc(sizeof (XenPhysmap));
1158 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1159 snprintf(path, sizeof(path),
1160 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1161 xen_domid, entries[i]);
1162 value = xs_read(state->xenstore, 0, path, &len);
1163 if (value == NULL) {
1164 g_free(physmap);
1165 continue;
1167 physmap->start_addr = strtoull(value, NULL, 16);
1168 free(value);
1170 snprintf(path, sizeof(path),
1171 "/local/domain/0/device-model/%d/physmap/%s/size",
1172 xen_domid, entries[i]);
1173 value = xs_read(state->xenstore, 0, path, &len);
1174 if (value == NULL) {
1175 g_free(physmap);
1176 continue;
1178 physmap->size = strtoull(value, NULL, 16);
1179 free(value);
1181 snprintf(path, sizeof(path),
1182 "/local/domain/0/device-model/%d/physmap/%s/name",
1183 xen_domid, entries[i]);
1184 physmap->name = xs_read(state->xenstore, 0, path, &len);
1186 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1188 free(entries);
1191 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1193 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1196 void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory)
1198 int i, rc;
1199 xen_pfn_t ioreq_pfn;
1200 xen_pfn_t bufioreq_pfn;
1201 evtchn_port_t bufioreq_evtchn;
1202 XenIOState *state;
1204 state = g_malloc0(sizeof (XenIOState));
1206 state->xce_handle = xenevtchn_open(NULL, 0);
1207 if (state->xce_handle == NULL) {
1208 perror("xen: event channel open");
1209 goto err;
1212 state->xenstore = xs_daemon_open();
1213 if (state->xenstore == NULL) {
1214 perror("xen: xenstore open");
1215 goto err;
1218 rc = xen_create_ioreq_server(xen_xc, xen_domid, &state->ioservid);
1219 if (rc < 0) {
1220 perror("xen: ioreq server create");
1221 goto err;
1224 state->exit.notify = xen_exit_notifier;
1225 qemu_add_exit_notifier(&state->exit);
1227 state->suspend.notify = xen_suspend_notifier;
1228 qemu_register_suspend_notifier(&state->suspend);
1230 state->wakeup.notify = xen_wakeup_notifier;
1231 qemu_register_wakeup_notifier(&state->wakeup);
1233 rc = xen_get_ioreq_server_info(xen_xc, xen_domid, state->ioservid,
1234 &ioreq_pfn, &bufioreq_pfn,
1235 &bufioreq_evtchn);
1236 if (rc < 0) {
1237 error_report("failed to get ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1238 errno, xen_xc);
1239 goto err;
1242 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1243 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1244 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1246 state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1247 PROT_READ|PROT_WRITE,
1248 1, &ioreq_pfn, NULL);
1249 if (state->shared_page == NULL) {
1250 error_report("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1251 errno, xen_xc);
1252 goto err;
1255 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1256 if (!rc) {
1257 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1258 state->shared_vmport_page =
1259 xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1260 1, &ioreq_pfn, NULL);
1261 if (state->shared_vmport_page == NULL) {
1262 error_report("map shared vmport IO page returned error %d handle="
1263 XC_INTERFACE_FMT, errno, xen_xc);
1264 goto err;
1266 } else if (rc != -ENOSYS) {
1267 error_report("get vmport regs pfn returned error %d, rc=%d",
1268 errno, rc);
1269 goto err;
1272 state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1273 PROT_READ|PROT_WRITE,
1274 1, &bufioreq_pfn, NULL);
1275 if (state->buffered_io_page == NULL) {
1276 error_report("map buffered IO page returned error %d", errno);
1277 goto err;
1280 /* Note: cpus is empty at this point in init */
1281 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1283 rc = xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, true);
1284 if (rc < 0) {
1285 error_report("failed to enable ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1286 errno, xen_xc);
1287 goto err;
1290 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1292 /* FIXME: how about if we overflow the page here? */
1293 for (i = 0; i < max_cpus; i++) {
1294 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1295 xen_vcpu_eport(state->shared_page, i));
1296 if (rc == -1) {
1297 error_report("shared evtchn %d bind error %d", i, errno);
1298 goto err;
1300 state->ioreq_local_port[i] = rc;
1303 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1304 bufioreq_evtchn);
1305 if (rc == -1) {
1306 error_report("buffered evtchn bind error %d", errno);
1307 goto err;
1309 state->bufioreq_local_port = rc;
1311 /* Init RAM management */
1312 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1313 xen_ram_init(pcms, ram_size, ram_memory);
1315 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1317 state->memory_listener = xen_memory_listener;
1318 QLIST_INIT(&state->physmap);
1319 memory_listener_register(&state->memory_listener, &address_space_memory);
1320 state->log_for_dirtybit = NULL;
1322 state->io_listener = xen_io_listener;
1323 memory_listener_register(&state->io_listener, &address_space_io);
1325 state->device_listener = xen_device_listener;
1326 device_listener_register(&state->device_listener);
1328 /* Initialize backend core & drivers */
1329 if (xen_be_init() != 0) {
1330 error_report("xen backend core setup failed");
1331 goto err;
1333 xen_be_register("console", &xen_console_ops);
1334 xen_be_register("vkbd", &xen_kbdmouse_ops);
1335 xen_be_register("qdisk", &xen_blkdev_ops);
1336 xen_read_physmap(state);
1337 return;
1339 err:
1340 error_report("xen hardware virtual machine initialisation failed");
1341 exit(1);
1344 void destroy_hvm_domain(bool reboot)
1346 XenXC xc_handle;
1347 int sts;
1349 xc_handle = xen_xc_interface_open(0, 0, 0);
1350 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1351 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1352 } else {
1353 sts = xc_domain_shutdown(xc_handle, xen_domid,
1354 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
1355 if (sts != 0) {
1356 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1357 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1358 sts, strerror(errno));
1359 } else {
1360 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1361 reboot ? "reboot" : "poweroff");
1363 xc_interface_close(xc_handle);
1367 void xen_register_framebuffer(MemoryRegion *mr)
1369 framebuffer = mr;
1372 void xen_shutdown_fatal_error(const char *fmt, ...)
1374 va_list ap;
1376 va_start(ap, fmt);
1377 vfprintf(stderr, fmt, ap);
1378 va_end(ap);
1379 fprintf(stderr, "Will destroy the domain.\n");
1380 /* destroy the domain */
1381 qemu_system_shutdown_request();
1384 void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1386 if (unlikely(xen_in_migration)) {
1387 int rc;
1388 ram_addr_t start_pfn, nb_pages;
1390 if (length == 0) {
1391 length = TARGET_PAGE_SIZE;
1393 start_pfn = start >> TARGET_PAGE_BITS;
1394 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1395 - start_pfn;
1396 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1397 if (rc) {
1398 fprintf(stderr,
1399 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1400 __func__, start, nb_pages, rc, strerror(-rc));
1405 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1407 if (enable) {
1408 memory_global_dirty_log_start();
1409 } else {
1410 memory_global_dirty_log_stop();