scsi: push WWN fields up to SCSIDevice
[qemu.git] / xen-hvm.c
blob6861c51ef93590b38793fb1232231db71600ba31
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/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 "sysemu/char.h"
22 #include "qemu/error-report.h"
23 #include "qemu/range.h"
24 #include "sysemu/xen-mapcache.h"
25 #include "trace.h"
26 #include "exec/address-spaces.h"
28 #include <xen/hvm/ioreq.h>
29 #include <xen/hvm/params.h>
30 #include <xen/hvm/e820.h>
32 //#define DEBUG_XEN_HVM
34 #ifdef DEBUG_XEN_HVM
35 #define DPRINTF(fmt, ...) \
36 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
37 #else
38 #define DPRINTF(fmt, ...) \
39 do { } while (0)
40 #endif
42 static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
43 static MemoryRegion *framebuffer;
44 static bool xen_in_migration;
46 /* Compatibility with older version */
48 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
49 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
50 * needs to be included before this block and hw/xen/xen_common.h needs to
51 * be included before xen/hvm/ioreq.h
53 #ifndef IOREQ_TYPE_VMWARE_PORT
54 #define IOREQ_TYPE_VMWARE_PORT 3
55 struct vmware_regs {
56 uint32_t esi;
57 uint32_t edi;
58 uint32_t ebx;
59 uint32_t ecx;
60 uint32_t edx;
62 typedef struct vmware_regs vmware_regs_t;
64 struct shared_vmport_iopage {
65 struct vmware_regs vcpu_vmport_regs[1];
67 typedef struct shared_vmport_iopage shared_vmport_iopage_t;
68 #endif
70 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
71 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
73 return shared_page->vcpu_iodata[i].vp_eport;
75 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
77 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
79 # define FMT_ioreq_size PRIx64
80 #else
81 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
83 return shared_page->vcpu_ioreq[i].vp_eport;
85 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
87 return &shared_page->vcpu_ioreq[vcpu];
89 # define FMT_ioreq_size "u"
90 #endif
92 #define BUFFER_IO_MAX_DELAY 100
94 typedef struct XenPhysmap {
95 hwaddr start_addr;
96 ram_addr_t size;
97 const char *name;
98 hwaddr phys_offset;
100 QLIST_ENTRY(XenPhysmap) list;
101 } XenPhysmap;
103 typedef struct XenIOState {
104 ioservid_t ioservid;
105 shared_iopage_t *shared_page;
106 shared_vmport_iopage_t *shared_vmport_page;
107 buffered_iopage_t *buffered_io_page;
108 QEMUTimer *buffered_io_timer;
109 CPUState **cpu_by_vcpu_id;
110 /* the evtchn port for polling the notification, */
111 evtchn_port_t *ioreq_local_port;
112 /* evtchn local port for buffered io */
113 evtchn_port_t bufioreq_local_port;
114 /* the evtchn fd for polling */
115 xenevtchn_handle *xce_handle;
116 /* which vcpu we are serving */
117 int send_vcpu;
119 struct xs_handle *xenstore;
120 MemoryListener memory_listener;
121 MemoryListener io_listener;
122 DeviceListener device_listener;
123 QLIST_HEAD(, XenPhysmap) physmap;
124 hwaddr free_phys_offset;
125 const XenPhysmap *log_for_dirtybit;
127 Notifier exit;
128 Notifier suspend;
129 Notifier wakeup;
130 } XenIOState;
132 /* Xen specific function for piix pci */
134 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
136 return irq_num + ((pci_dev->devfn >> 3) << 2);
139 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
141 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
142 irq_num & 3, level);
145 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
147 int i;
149 /* Scan for updates to PCI link routes (0x60-0x63). */
150 for (i = 0; i < len; i++) {
151 uint8_t v = (val >> (8 * i)) & 0xff;
152 if (v & 0x80) {
153 v = 0;
155 v &= 0xf;
156 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
157 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
162 int xen_is_pirq_msi(uint32_t msi_data)
164 /* If vector is 0, the msi is remapped into a pirq, passed as
165 * dest_id.
167 return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
170 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
172 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
175 static void xen_suspend_notifier(Notifier *notifier, void *data)
177 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
180 /* Xen Interrupt Controller */
182 static void xen_set_irq(void *opaque, int irq, int level)
184 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
187 qemu_irq *xen_interrupt_controller_init(void)
189 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
192 /* Memory Ops */
194 static void xen_ram_init(PCMachineState *pcms,
195 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
197 MemoryRegion *sysmem = get_system_memory();
198 ram_addr_t block_len;
199 uint64_t user_lowmem = object_property_get_int(qdev_get_machine(),
200 PC_MACHINE_MAX_RAM_BELOW_4G,
201 &error_abort);
203 /* Handle the machine opt max-ram-below-4g. It is basically doing
204 * min(xen limit, user limit).
206 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
207 user_lowmem = HVM_BELOW_4G_RAM_END;
210 if (ram_size >= user_lowmem) {
211 pcms->above_4g_mem_size = ram_size - user_lowmem;
212 pcms->below_4g_mem_size = user_lowmem;
213 } else {
214 pcms->above_4g_mem_size = 0;
215 pcms->below_4g_mem_size = ram_size;
217 if (!pcms->above_4g_mem_size) {
218 block_len = ram_size;
219 } else {
221 * Xen does not allocate the memory continuously, it keeps a
222 * hole of the size computed above or passed in.
224 block_len = (1ULL << 32) + pcms->above_4g_mem_size;
226 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
227 &error_fatal);
228 *ram_memory_p = &ram_memory;
229 vmstate_register_ram_global(&ram_memory);
231 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
232 &ram_memory, 0, 0xa0000);
233 memory_region_add_subregion(sysmem, 0, &ram_640k);
234 /* Skip of the VGA IO memory space, it will be registered later by the VGA
235 * emulated device.
237 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
238 * the Options ROM, so it is registered here as RAM.
240 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
241 &ram_memory, 0xc0000,
242 pcms->below_4g_mem_size - 0xc0000);
243 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
244 if (pcms->above_4g_mem_size > 0) {
245 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
246 &ram_memory, 0x100000000ULL,
247 pcms->above_4g_mem_size);
248 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
252 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
253 Error **errp)
255 unsigned long nr_pfn;
256 xen_pfn_t *pfn_list;
257 int i;
259 if (runstate_check(RUN_STATE_INMIGRATE)) {
260 /* RAM already populated in Xen */
261 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
262 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
263 __func__, size, ram_addr);
264 return;
267 if (mr == &ram_memory) {
268 return;
271 trace_xen_ram_alloc(ram_addr, size);
273 nr_pfn = size >> TARGET_PAGE_BITS;
274 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
276 for (i = 0; i < nr_pfn; i++) {
277 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
280 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
281 error_setg(errp, "xen: failed to populate ram at " RAM_ADDR_FMT,
282 ram_addr);
285 g_free(pfn_list);
288 static XenPhysmap *get_physmapping(XenIOState *state,
289 hwaddr start_addr, ram_addr_t size)
291 XenPhysmap *physmap = NULL;
293 start_addr &= TARGET_PAGE_MASK;
295 QLIST_FOREACH(physmap, &state->physmap, list) {
296 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
297 return physmap;
300 return NULL;
303 static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
304 ram_addr_t size, void *opaque)
306 hwaddr addr = start_addr & TARGET_PAGE_MASK;
307 XenIOState *xen_io_state = opaque;
308 XenPhysmap *physmap = NULL;
310 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
311 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
312 return physmap->start_addr;
316 return start_addr;
319 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
320 static int xen_add_to_physmap(XenIOState *state,
321 hwaddr start_addr,
322 ram_addr_t size,
323 MemoryRegion *mr,
324 hwaddr offset_within_region)
326 unsigned long i = 0;
327 int rc = 0;
328 XenPhysmap *physmap = NULL;
329 hwaddr pfn, start_gpfn;
330 hwaddr phys_offset = memory_region_get_ram_addr(mr);
331 char path[80], value[17];
332 const char *mr_name;
334 if (get_physmapping(state, start_addr, size)) {
335 return 0;
337 if (size <= 0) {
338 return -1;
341 /* Xen can only handle a single dirty log region for now and we want
342 * the linear framebuffer to be that region.
343 * Avoid tracking any regions that is not videoram and avoid tracking
344 * the legacy vga region. */
345 if (mr == framebuffer && start_addr > 0xbffff) {
346 goto go_physmap;
348 return -1;
350 go_physmap:
351 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
352 start_addr, start_addr + size);
354 pfn = phys_offset >> TARGET_PAGE_BITS;
355 start_gpfn = start_addr >> TARGET_PAGE_BITS;
356 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
357 unsigned long idx = pfn + i;
358 xen_pfn_t gpfn = start_gpfn + i;
360 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
361 if (rc) {
362 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
363 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
364 return -rc;
368 mr_name = memory_region_name(mr);
370 physmap = g_malloc(sizeof (XenPhysmap));
372 physmap->start_addr = start_addr;
373 physmap->size = size;
374 physmap->name = mr_name;
375 physmap->phys_offset = phys_offset;
377 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
379 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
380 start_addr >> TARGET_PAGE_BITS,
381 (start_addr + size - 1) >> TARGET_PAGE_BITS,
382 XEN_DOMCTL_MEM_CACHEATTR_WB);
384 snprintf(path, sizeof(path),
385 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
386 xen_domid, (uint64_t)phys_offset);
387 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
388 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
389 return -1;
391 snprintf(path, sizeof(path),
392 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
393 xen_domid, (uint64_t)phys_offset);
394 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
395 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
396 return -1;
398 if (mr_name) {
399 snprintf(path, sizeof(path),
400 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
401 xen_domid, (uint64_t)phys_offset);
402 if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
403 return -1;
407 return 0;
410 static int xen_remove_from_physmap(XenIOState *state,
411 hwaddr start_addr,
412 ram_addr_t size)
414 unsigned long i = 0;
415 int rc = 0;
416 XenPhysmap *physmap = NULL;
417 hwaddr phys_offset = 0;
419 physmap = get_physmapping(state, start_addr, size);
420 if (physmap == NULL) {
421 return -1;
424 phys_offset = physmap->phys_offset;
425 size = physmap->size;
427 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
428 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
430 size >>= TARGET_PAGE_BITS;
431 start_addr >>= TARGET_PAGE_BITS;
432 phys_offset >>= TARGET_PAGE_BITS;
433 for (i = 0; i < size; i++) {
434 xen_pfn_t idx = start_addr + i;
435 xen_pfn_t gpfn = phys_offset + i;
437 rc = xen_xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
438 if (rc) {
439 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
440 PRI_xen_pfn" failed: %d (errno: %d)\n", idx, gpfn, rc, errno);
441 return -rc;
445 QLIST_REMOVE(physmap, list);
446 if (state->log_for_dirtybit == physmap) {
447 state->log_for_dirtybit = NULL;
449 g_free(physmap);
451 return 0;
454 #else
455 static int xen_add_to_physmap(XenIOState *state,
456 hwaddr start_addr,
457 ram_addr_t size,
458 MemoryRegion *mr,
459 hwaddr offset_within_region)
461 return -ENOSYS;
464 static int xen_remove_from_physmap(XenIOState *state,
465 hwaddr start_addr,
466 ram_addr_t size)
468 return -ENOSYS;
470 #endif
472 static void xen_set_memory(struct MemoryListener *listener,
473 MemoryRegionSection *section,
474 bool add)
476 XenIOState *state = container_of(listener, XenIOState, memory_listener);
477 hwaddr start_addr = section->offset_within_address_space;
478 ram_addr_t size = int128_get64(section->size);
479 bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
480 hvmmem_type_t mem_type;
482 if (section->mr == &ram_memory) {
483 return;
484 } else {
485 if (add) {
486 xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
487 section);
488 } else {
489 xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
490 section);
494 if (!memory_region_is_ram(section->mr)) {
495 return;
498 if (log_dirty != add) {
499 return;
502 trace_xen_client_set_memory(start_addr, size, log_dirty);
504 start_addr &= TARGET_PAGE_MASK;
505 size = TARGET_PAGE_ALIGN(size);
507 if (add) {
508 if (!memory_region_is_rom(section->mr)) {
509 xen_add_to_physmap(state, start_addr, size,
510 section->mr, section->offset_within_region);
511 } else {
512 mem_type = HVMMEM_ram_ro;
513 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
514 start_addr >> TARGET_PAGE_BITS,
515 size >> TARGET_PAGE_BITS)) {
516 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
517 start_addr);
520 } else {
521 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
522 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
527 static void xen_region_add(MemoryListener *listener,
528 MemoryRegionSection *section)
530 memory_region_ref(section->mr);
531 xen_set_memory(listener, section, true);
534 static void xen_region_del(MemoryListener *listener,
535 MemoryRegionSection *section)
537 xen_set_memory(listener, section, false);
538 memory_region_unref(section->mr);
541 static void xen_io_add(MemoryListener *listener,
542 MemoryRegionSection *section)
544 XenIOState *state = container_of(listener, XenIOState, io_listener);
546 memory_region_ref(section->mr);
548 xen_map_io_section(xen_xc, 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);
556 xen_unmap_io_section(xen_xc, xen_domid, state->ioservid, section);
558 memory_region_unref(section->mr);
561 static void xen_device_realize(DeviceListener *listener,
562 DeviceState *dev)
564 XenIOState *state = container_of(listener, XenIOState, device_listener);
566 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
567 PCIDevice *pci_dev = PCI_DEVICE(dev);
569 xen_map_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
573 static void xen_device_unrealize(DeviceListener *listener,
574 DeviceState *dev)
576 XenIOState *state = container_of(listener, XenIOState, device_listener);
578 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
579 PCIDevice *pci_dev = PCI_DEVICE(dev);
581 xen_unmap_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
585 static void xen_sync_dirty_bitmap(XenIOState *state,
586 hwaddr start_addr,
587 ram_addr_t size)
589 hwaddr npages = size >> TARGET_PAGE_BITS;
590 const int width = sizeof(unsigned long) * 8;
591 unsigned long bitmap[(npages + width - 1) / width];
592 int rc, i, j;
593 const XenPhysmap *physmap = NULL;
595 physmap = get_physmapping(state, start_addr, size);
596 if (physmap == NULL) {
597 /* not handled */
598 return;
601 if (state->log_for_dirtybit == NULL) {
602 state->log_for_dirtybit = physmap;
603 } else if (state->log_for_dirtybit != physmap) {
604 /* Only one range for dirty bitmap can be tracked. */
605 return;
608 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
609 start_addr >> TARGET_PAGE_BITS, npages,
610 bitmap);
611 if (rc < 0) {
612 #ifndef ENODATA
613 #define ENODATA ENOENT
614 #endif
615 if (errno == ENODATA) {
616 memory_region_set_dirty(framebuffer, 0, size);
617 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
618 ", 0x" TARGET_FMT_plx "): %s\n",
619 start_addr, start_addr + size, strerror(errno));
621 return;
624 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
625 unsigned long map = bitmap[i];
626 while (map != 0) {
627 j = ctzl(map);
628 map &= ~(1ul << j);
629 memory_region_set_dirty(framebuffer,
630 (i * width + j) * TARGET_PAGE_SIZE,
631 TARGET_PAGE_SIZE);
636 static void xen_log_start(MemoryListener *listener,
637 MemoryRegionSection *section,
638 int old, int new)
640 XenIOState *state = container_of(listener, XenIOState, memory_listener);
642 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
643 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
644 int128_get64(section->size));
648 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
649 int old, int new)
651 XenIOState *state = container_of(listener, XenIOState, memory_listener);
653 if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
654 state->log_for_dirtybit = NULL;
655 /* Disable dirty bit tracking */
656 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
660 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
662 XenIOState *state = container_of(listener, XenIOState, memory_listener);
664 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
665 int128_get64(section->size));
668 static void xen_log_global_start(MemoryListener *listener)
670 if (xen_enabled()) {
671 xen_in_migration = true;
675 static void xen_log_global_stop(MemoryListener *listener)
677 xen_in_migration = false;
680 static MemoryListener xen_memory_listener = {
681 .region_add = xen_region_add,
682 .region_del = xen_region_del,
683 .log_start = xen_log_start,
684 .log_stop = xen_log_stop,
685 .log_sync = xen_log_sync,
686 .log_global_start = xen_log_global_start,
687 .log_global_stop = xen_log_global_stop,
688 .priority = 10,
691 static MemoryListener xen_io_listener = {
692 .region_add = xen_io_add,
693 .region_del = xen_io_del,
694 .priority = 10,
697 static DeviceListener xen_device_listener = {
698 .realize = xen_device_realize,
699 .unrealize = xen_device_unrealize,
702 /* get the ioreq packets from share mem */
703 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
705 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
707 if (req->state != STATE_IOREQ_READY) {
708 DPRINTF("I/O request not ready: "
709 "%x, ptr: %x, port: %"PRIx64", "
710 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
711 req->state, req->data_is_ptr, req->addr,
712 req->data, req->count, req->size);
713 return NULL;
716 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
718 req->state = STATE_IOREQ_INPROCESS;
719 return req;
722 /* use poll to get the port notification */
723 /* ioreq_vec--out,the */
724 /* retval--the number of ioreq packet */
725 static ioreq_t *cpu_get_ioreq(XenIOState *state)
727 int i;
728 evtchn_port_t port;
730 port = xenevtchn_pending(state->xce_handle);
731 if (port == state->bufioreq_local_port) {
732 timer_mod(state->buffered_io_timer,
733 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
734 return NULL;
737 if (port != -1) {
738 for (i = 0; i < max_cpus; i++) {
739 if (state->ioreq_local_port[i] == port) {
740 break;
744 if (i == max_cpus) {
745 hw_error("Fatal error while trying to get io event!\n");
748 /* unmask the wanted port again */
749 xenevtchn_unmask(state->xce_handle, port);
751 /* get the io packet from shared memory */
752 state->send_vcpu = i;
753 return cpu_get_ioreq_from_shared_memory(state, i);
756 /* read error or read nothing */
757 return NULL;
760 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
762 switch (size) {
763 case 1:
764 return cpu_inb(addr);
765 case 2:
766 return cpu_inw(addr);
767 case 4:
768 return cpu_inl(addr);
769 default:
770 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
774 static void do_outp(pio_addr_t addr,
775 unsigned long size, uint32_t val)
777 switch (size) {
778 case 1:
779 return cpu_outb(addr, val);
780 case 2:
781 return cpu_outw(addr, val);
782 case 4:
783 return cpu_outl(addr, val);
784 default:
785 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
790 * Helper functions which read/write an object from/to physical guest
791 * memory, as part of the implementation of an ioreq.
793 * Equivalent to
794 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
795 * val, req->size, 0/1)
796 * except without the integer overflow problems.
798 static void rw_phys_req_item(hwaddr addr,
799 ioreq_t *req, uint32_t i, void *val, int rw)
801 /* Do everything unsigned so overflow just results in a truncated result
802 * and accesses to undesired parts of guest memory, which is up
803 * to the guest */
804 hwaddr offset = (hwaddr)req->size * i;
805 if (req->df) {
806 addr -= offset;
807 } else {
808 addr += offset;
810 cpu_physical_memory_rw(addr, val, req->size, rw);
813 static inline void read_phys_req_item(hwaddr addr,
814 ioreq_t *req, uint32_t i, void *val)
816 rw_phys_req_item(addr, req, i, val, 0);
818 static inline void write_phys_req_item(hwaddr addr,
819 ioreq_t *req, uint32_t i, void *val)
821 rw_phys_req_item(addr, req, i, val, 1);
825 static void cpu_ioreq_pio(ioreq_t *req)
827 uint32_t i;
829 trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
830 req->data, req->count, req->size);
832 if (req->dir == IOREQ_READ) {
833 if (!req->data_is_ptr) {
834 req->data = do_inp(req->addr, req->size);
835 trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
836 req->size);
837 } else {
838 uint32_t tmp;
840 for (i = 0; i < req->count; i++) {
841 tmp = do_inp(req->addr, req->size);
842 write_phys_req_item(req->data, req, i, &tmp);
845 } else if (req->dir == IOREQ_WRITE) {
846 if (!req->data_is_ptr) {
847 trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
848 req->size);
849 do_outp(req->addr, req->size, req->data);
850 } else {
851 for (i = 0; i < req->count; i++) {
852 uint32_t tmp = 0;
854 read_phys_req_item(req->data, req, i, &tmp);
855 do_outp(req->addr, req->size, tmp);
861 static void cpu_ioreq_move(ioreq_t *req)
863 uint32_t i;
865 trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
866 req->data, req->count, req->size);
868 if (!req->data_is_ptr) {
869 if (req->dir == IOREQ_READ) {
870 for (i = 0; i < req->count; i++) {
871 read_phys_req_item(req->addr, req, i, &req->data);
873 } else if (req->dir == IOREQ_WRITE) {
874 for (i = 0; i < req->count; i++) {
875 write_phys_req_item(req->addr, req, i, &req->data);
878 } else {
879 uint64_t tmp;
881 if (req->dir == IOREQ_READ) {
882 for (i = 0; i < req->count; i++) {
883 read_phys_req_item(req->addr, req, i, &tmp);
884 write_phys_req_item(req->data, req, i, &tmp);
886 } else if (req->dir == IOREQ_WRITE) {
887 for (i = 0; i < req->count; i++) {
888 read_phys_req_item(req->data, req, i, &tmp);
889 write_phys_req_item(req->addr, req, i, &tmp);
895 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
897 X86CPU *cpu;
898 CPUX86State *env;
900 cpu = X86_CPU(current_cpu);
901 env = &cpu->env;
902 env->regs[R_EAX] = req->data;
903 env->regs[R_EBX] = vmport_regs->ebx;
904 env->regs[R_ECX] = vmport_regs->ecx;
905 env->regs[R_EDX] = vmport_regs->edx;
906 env->regs[R_ESI] = vmport_regs->esi;
907 env->regs[R_EDI] = vmport_regs->edi;
910 static void regs_from_cpu(vmware_regs_t *vmport_regs)
912 X86CPU *cpu = X86_CPU(current_cpu);
913 CPUX86State *env = &cpu->env;
915 vmport_regs->ebx = env->regs[R_EBX];
916 vmport_regs->ecx = env->regs[R_ECX];
917 vmport_regs->edx = env->regs[R_EDX];
918 vmport_regs->esi = env->regs[R_ESI];
919 vmport_regs->edi = env->regs[R_EDI];
922 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
924 vmware_regs_t *vmport_regs;
926 assert(state->shared_vmport_page);
927 vmport_regs =
928 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
929 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
931 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
932 regs_to_cpu(vmport_regs, req);
933 cpu_ioreq_pio(req);
934 regs_from_cpu(vmport_regs);
935 current_cpu = NULL;
938 static void handle_ioreq(XenIOState *state, ioreq_t *req)
940 trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
941 req->addr, req->data, req->count, req->size);
943 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
944 (req->size < sizeof (target_ulong))) {
945 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
948 if (req->dir == IOREQ_WRITE)
949 trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
950 req->addr, req->data, req->count, req->size);
952 switch (req->type) {
953 case IOREQ_TYPE_PIO:
954 cpu_ioreq_pio(req);
955 break;
956 case IOREQ_TYPE_COPY:
957 cpu_ioreq_move(req);
958 break;
959 case IOREQ_TYPE_VMWARE_PORT:
960 handle_vmport_ioreq(state, req);
961 break;
962 case IOREQ_TYPE_TIMEOFFSET:
963 break;
964 case IOREQ_TYPE_INVALIDATE:
965 xen_invalidate_map_cache();
966 break;
967 case IOREQ_TYPE_PCI_CONFIG: {
968 uint32_t sbdf = req->addr >> 32;
969 uint32_t val;
971 /* Fake a write to port 0xCF8 so that
972 * the config space access will target the
973 * correct device model.
975 val = (1u << 31) |
976 ((req->addr & 0x0f00) << 16) |
977 ((sbdf & 0xffff) << 8) |
978 (req->addr & 0xfc);
979 do_outp(0xcf8, 4, val);
981 /* Now issue the config space access via
982 * port 0xCFC
984 req->addr = 0xcfc | (req->addr & 0x03);
985 cpu_ioreq_pio(req);
986 break;
988 default:
989 hw_error("Invalid ioreq type 0x%x\n", req->type);
991 if (req->dir == IOREQ_READ) {
992 trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
993 req->addr, req->data, req->count, req->size);
997 static int handle_buffered_iopage(XenIOState *state)
999 buffered_iopage_t *buf_page = state->buffered_io_page;
1000 buf_ioreq_t *buf_req = NULL;
1001 ioreq_t req;
1002 int qw;
1004 if (!buf_page) {
1005 return 0;
1008 memset(&req, 0x00, sizeof(req));
1010 for (;;) {
1011 uint32_t rdptr = buf_page->read_pointer, wrptr;
1013 xen_rmb();
1014 wrptr = buf_page->write_pointer;
1015 xen_rmb();
1016 if (rdptr != buf_page->read_pointer) {
1017 continue;
1019 if (rdptr == wrptr) {
1020 break;
1022 buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1023 req.size = 1UL << buf_req->size;
1024 req.count = 1;
1025 req.addr = buf_req->addr;
1026 req.data = buf_req->data;
1027 req.state = STATE_IOREQ_READY;
1028 req.dir = buf_req->dir;
1029 req.df = 1;
1030 req.type = buf_req->type;
1031 req.data_is_ptr = 0;
1032 qw = (req.size == 8);
1033 if (qw) {
1034 buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1035 IOREQ_BUFFER_SLOT_NUM];
1036 req.data |= ((uint64_t)buf_req->data) << 32;
1039 handle_ioreq(state, &req);
1041 atomic_add(&buf_page->read_pointer, qw + 1);
1044 return req.count;
1047 static void handle_buffered_io(void *opaque)
1049 XenIOState *state = opaque;
1051 if (handle_buffered_iopage(state)) {
1052 timer_mod(state->buffered_io_timer,
1053 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1054 } else {
1055 timer_del(state->buffered_io_timer);
1056 xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1060 static void cpu_handle_ioreq(void *opaque)
1062 XenIOState *state = opaque;
1063 ioreq_t *req = cpu_get_ioreq(state);
1065 handle_buffered_iopage(state);
1066 if (req) {
1067 handle_ioreq(state, req);
1069 if (req->state != STATE_IOREQ_INPROCESS) {
1070 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1071 "%x, ptr: %x, port: %"PRIx64", "
1072 "data: %"PRIx64", count: %" FMT_ioreq_size
1073 ", size: %" FMT_ioreq_size
1074 ", type: %"FMT_ioreq_size"\n",
1075 req->state, req->data_is_ptr, req->addr,
1076 req->data, req->count, req->size, req->type);
1077 destroy_hvm_domain(false);
1078 return;
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 if (qemu_shutdown_requested_get()) {
1091 destroy_hvm_domain(false);
1093 if (qemu_reset_requested_get()) {
1094 qemu_system_reset(VMRESET_REPORT);
1095 destroy_hvm_domain(true);
1099 req->state = STATE_IORESP_READY;
1100 xenevtchn_notify(state->xce_handle,
1101 state->ioreq_local_port[state->send_vcpu]);
1105 static void xen_main_loop_prepare(XenIOState *state)
1107 int evtchn_fd = -1;
1109 if (state->xce_handle != NULL) {
1110 evtchn_fd = xenevtchn_fd(state->xce_handle);
1113 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1114 state);
1116 if (evtchn_fd != -1) {
1117 CPUState *cpu_state;
1119 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1120 CPU_FOREACH(cpu_state) {
1121 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1122 __func__, cpu_state->cpu_index, cpu_state);
1123 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1125 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1130 static void xen_hvm_change_state_handler(void *opaque, int running,
1131 RunState rstate)
1133 XenIOState *state = opaque;
1135 if (running) {
1136 xen_main_loop_prepare(state);
1139 xen_set_ioreq_server_state(xen_xc, xen_domid,
1140 state->ioservid,
1141 (rstate == RUN_STATE_RUNNING));
1144 static void xen_exit_notifier(Notifier *n, void *data)
1146 XenIOState *state = container_of(n, XenIOState, exit);
1148 xenevtchn_close(state->xce_handle);
1149 xs_daemon_close(state->xenstore);
1152 static void xen_read_physmap(XenIOState *state)
1154 XenPhysmap *physmap = NULL;
1155 unsigned int len, num, i;
1156 char path[80], *value = NULL;
1157 char **entries = NULL;
1159 snprintf(path, sizeof(path),
1160 "/local/domain/0/device-model/%d/physmap", xen_domid);
1161 entries = xs_directory(state->xenstore, 0, path, &num);
1162 if (entries == NULL)
1163 return;
1165 for (i = 0; i < num; i++) {
1166 physmap = g_malloc(sizeof (XenPhysmap));
1167 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1168 snprintf(path, sizeof(path),
1169 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1170 xen_domid, entries[i]);
1171 value = xs_read(state->xenstore, 0, path, &len);
1172 if (value == NULL) {
1173 g_free(physmap);
1174 continue;
1176 physmap->start_addr = strtoull(value, NULL, 16);
1177 free(value);
1179 snprintf(path, sizeof(path),
1180 "/local/domain/0/device-model/%d/physmap/%s/size",
1181 xen_domid, entries[i]);
1182 value = xs_read(state->xenstore, 0, path, &len);
1183 if (value == NULL) {
1184 g_free(physmap);
1185 continue;
1187 physmap->size = strtoull(value, NULL, 16);
1188 free(value);
1190 snprintf(path, sizeof(path),
1191 "/local/domain/0/device-model/%d/physmap/%s/name",
1192 xen_domid, entries[i]);
1193 physmap->name = xs_read(state->xenstore, 0, path, &len);
1195 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1197 free(entries);
1200 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1202 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1205 void xen_hvm_init(PCMachineState *pcms, MemoryRegion **ram_memory)
1207 int i, rc;
1208 xen_pfn_t ioreq_pfn;
1209 xen_pfn_t bufioreq_pfn;
1210 evtchn_port_t bufioreq_evtchn;
1211 XenIOState *state;
1213 state = g_malloc0(sizeof (XenIOState));
1215 state->xce_handle = xenevtchn_open(NULL, 0);
1216 if (state->xce_handle == NULL) {
1217 perror("xen: event channel open");
1218 goto err;
1221 state->xenstore = xs_daemon_open();
1222 if (state->xenstore == NULL) {
1223 perror("xen: xenstore open");
1224 goto err;
1227 rc = xen_create_ioreq_server(xen_xc, xen_domid, &state->ioservid);
1228 if (rc < 0) {
1229 perror("xen: ioreq server create");
1230 goto err;
1233 state->exit.notify = xen_exit_notifier;
1234 qemu_add_exit_notifier(&state->exit);
1236 state->suspend.notify = xen_suspend_notifier;
1237 qemu_register_suspend_notifier(&state->suspend);
1239 state->wakeup.notify = xen_wakeup_notifier;
1240 qemu_register_wakeup_notifier(&state->wakeup);
1242 rc = xen_get_ioreq_server_info(xen_xc, xen_domid, state->ioservid,
1243 &ioreq_pfn, &bufioreq_pfn,
1244 &bufioreq_evtchn);
1245 if (rc < 0) {
1246 error_report("failed to get ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1247 errno, xen_xc);
1248 goto err;
1251 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1252 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1253 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1255 state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1256 PROT_READ|PROT_WRITE,
1257 1, &ioreq_pfn, NULL);
1258 if (state->shared_page == NULL) {
1259 error_report("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1260 errno, xen_xc);
1261 goto err;
1264 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1265 if (!rc) {
1266 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1267 state->shared_vmport_page =
1268 xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1269 1, &ioreq_pfn, NULL);
1270 if (state->shared_vmport_page == NULL) {
1271 error_report("map shared vmport IO page returned error %d handle="
1272 XC_INTERFACE_FMT, errno, xen_xc);
1273 goto err;
1275 } else if (rc != -ENOSYS) {
1276 error_report("get vmport regs pfn returned error %d, rc=%d",
1277 errno, rc);
1278 goto err;
1281 state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1282 PROT_READ|PROT_WRITE,
1283 1, &bufioreq_pfn, NULL);
1284 if (state->buffered_io_page == NULL) {
1285 error_report("map buffered IO page returned error %d", errno);
1286 goto err;
1289 /* Note: cpus is empty at this point in init */
1290 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1292 rc = xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, true);
1293 if (rc < 0) {
1294 error_report("failed to enable ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1295 errno, xen_xc);
1296 goto err;
1299 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1301 /* FIXME: how about if we overflow the page here? */
1302 for (i = 0; i < max_cpus; i++) {
1303 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1304 xen_vcpu_eport(state->shared_page, i));
1305 if (rc == -1) {
1306 error_report("shared evtchn %d bind error %d", i, errno);
1307 goto err;
1309 state->ioreq_local_port[i] = rc;
1312 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1313 bufioreq_evtchn);
1314 if (rc == -1) {
1315 error_report("buffered evtchn bind error %d", errno);
1316 goto err;
1318 state->bufioreq_local_port = rc;
1320 /* Init RAM management */
1321 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1322 xen_ram_init(pcms, ram_size, ram_memory);
1324 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1326 state->memory_listener = xen_memory_listener;
1327 QLIST_INIT(&state->physmap);
1328 memory_listener_register(&state->memory_listener, &address_space_memory);
1329 state->log_for_dirtybit = NULL;
1331 state->io_listener = xen_io_listener;
1332 memory_listener_register(&state->io_listener, &address_space_io);
1334 state->device_listener = xen_device_listener;
1335 device_listener_register(&state->device_listener);
1337 /* Initialize backend core & drivers */
1338 if (xen_be_init() != 0) {
1339 error_report("xen backend core setup failed");
1340 goto err;
1342 xen_be_register("console", &xen_console_ops);
1343 xen_be_register("vkbd", &xen_kbdmouse_ops);
1344 xen_be_register("qdisk", &xen_blkdev_ops);
1345 xen_read_physmap(state);
1346 return;
1348 err:
1349 error_report("xen hardware virtual machine initialisation failed");
1350 exit(1);
1353 void destroy_hvm_domain(bool reboot)
1355 XenXC xc_handle;
1356 int sts;
1358 xc_handle = xen_xc_interface_open(0, 0, 0);
1359 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1360 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1361 } else {
1362 sts = xc_domain_shutdown(xc_handle, xen_domid,
1363 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
1364 if (sts != 0) {
1365 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1366 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1367 sts, strerror(errno));
1368 } else {
1369 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1370 reboot ? "reboot" : "poweroff");
1372 xc_interface_close(xc_handle);
1376 void xen_register_framebuffer(MemoryRegion *mr)
1378 framebuffer = mr;
1381 void xen_shutdown_fatal_error(const char *fmt, ...)
1383 va_list ap;
1385 va_start(ap, fmt);
1386 vfprintf(stderr, fmt, ap);
1387 va_end(ap);
1388 fprintf(stderr, "Will destroy the domain.\n");
1389 /* destroy the domain */
1390 qemu_system_shutdown_request();
1393 void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1395 if (unlikely(xen_in_migration)) {
1396 int rc;
1397 ram_addr_t start_pfn, nb_pages;
1399 if (length == 0) {
1400 length = TARGET_PAGE_SIZE;
1402 start_pfn = start >> TARGET_PAGE_BITS;
1403 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1404 - start_pfn;
1405 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1406 if (rc) {
1407 fprintf(stderr,
1408 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1409 __func__, start, nb_pages, rc, strerror(-rc));
1414 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1416 if (enable) {
1417 memory_global_dirty_log_start();
1418 } else {
1419 memory_global_dirty_log_stop();