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"
12 #include "qemu/units.h"
15 #include "hw/pci/pci.h"
16 #include "hw/pci/pci_host.h"
17 #include "hw/i386/pc.h"
18 #include "hw/southbridge/piix.h"
21 #include "hw/i386/apic-msidef.h"
22 #include "hw/xen/xen_common.h"
23 #include "hw/xen/xen-legacy-backend.h"
24 #include "hw/xen/xen-bus.h"
25 #include "qapi/error.h"
26 #include "qapi/qapi-commands-misc.h"
27 #include "qemu/error-report.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/range.h"
30 #include "sysemu/runstate.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/xen.h"
33 #include "sysemu/xen-mapcache.h"
35 #include "exec/address-spaces.h"
37 #include <xen/hvm/ioreq.h>
38 #include <xen/hvm/e820.h>
40 //#define DEBUG_XEN_HVM
43 #define DPRINTF(fmt, ...) \
44 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
46 #define DPRINTF(fmt, ...) \
50 static MemoryRegion ram_memory
, ram_640k
, ram_lo
, ram_hi
;
51 static MemoryRegion
*framebuffer
;
52 static bool xen_in_migration
;
54 /* Compatibility with older version */
56 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
57 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
58 * needs to be included before this block and hw/xen/xen_common.h needs to
59 * be included before xen/hvm/ioreq.h
61 #ifndef IOREQ_TYPE_VMWARE_PORT
62 #define IOREQ_TYPE_VMWARE_PORT 3
70 typedef struct vmware_regs vmware_regs_t
;
72 struct shared_vmport_iopage
{
73 struct vmware_regs vcpu_vmport_regs
[1];
75 typedef struct shared_vmport_iopage shared_vmport_iopage_t
;
78 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
80 return shared_page
->vcpu_ioreq
[i
].vp_eport
;
82 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
84 return &shared_page
->vcpu_ioreq
[vcpu
];
87 #define BUFFER_IO_MAX_DELAY 100
89 typedef struct XenPhysmap
{
95 QLIST_ENTRY(XenPhysmap
) list
;
98 static QLIST_HEAD(, XenPhysmap
) xen_physmap
;
100 typedef struct XenPciDevice
{
103 QLIST_ENTRY(XenPciDevice
) entry
;
106 typedef struct XenIOState
{
108 shared_iopage_t
*shared_page
;
109 shared_vmport_iopage_t
*shared_vmport_page
;
110 buffered_iopage_t
*buffered_io_page
;
111 QEMUTimer
*buffered_io_timer
;
112 CPUState
**cpu_by_vcpu_id
;
113 /* the evtchn port for polling the notification, */
114 evtchn_port_t
*ioreq_local_port
;
115 /* evtchn remote and local ports for buffered io */
116 evtchn_port_t bufioreq_remote_port
;
117 evtchn_port_t bufioreq_local_port
;
118 /* the evtchn fd for polling */
119 xenevtchn_handle
*xce_handle
;
120 /* which vcpu we are serving */
123 struct xs_handle
*xenstore
;
124 MemoryListener memory_listener
;
125 MemoryListener io_listener
;
126 QLIST_HEAD(, XenPciDevice
) dev_list
;
127 DeviceListener device_listener
;
128 hwaddr free_phys_offset
;
129 const XenPhysmap
*log_for_dirtybit
;
130 /* Buffer used by xen_sync_dirty_bitmap */
131 unsigned long *dirty_bitmap
;
138 /* Xen specific function for piix pci */
140 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
142 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
145 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
147 xen_set_pci_intx_level(xen_domid
, 0, 0, irq_num
>> 2,
151 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
155 /* Scan for updates to PCI link routes (0x60-0x63). */
156 for (i
= 0; i
< len
; i
++) {
157 uint8_t v
= (val
>> (8 * i
)) & 0xff;
162 if (((address
+ i
) >= PIIX_PIRQCA
) && ((address
+ i
) <= PIIX_PIRQCD
)) {
163 xen_set_pci_link_route(xen_domid
, address
+ i
- PIIX_PIRQCA
, v
);
168 int xen_is_pirq_msi(uint32_t msi_data
)
170 /* If vector is 0, the msi is remapped into a pirq, passed as
173 return ((msi_data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
) == 0;
176 void xen_hvm_inject_msi(uint64_t addr
, uint32_t data
)
178 xen_inject_msi(xen_domid
, addr
, data
);
181 static void xen_suspend_notifier(Notifier
*notifier
, void *data
)
183 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 3);
186 /* Xen Interrupt Controller */
188 static void xen_set_irq(void *opaque
, int irq
, int level
)
190 xen_set_isa_irq_level(xen_domid
, irq
, level
);
193 qemu_irq
*xen_interrupt_controller_init(void)
195 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
200 static void xen_ram_init(PCMachineState
*pcms
,
201 ram_addr_t ram_size
, MemoryRegion
**ram_memory_p
)
203 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
204 MemoryRegion
*sysmem
= get_system_memory();
205 ram_addr_t block_len
;
206 uint64_t user_lowmem
=
207 object_property_get_uint(qdev_get_machine(),
208 PC_MACHINE_MAX_RAM_BELOW_4G
,
211 /* Handle the machine opt max-ram-below-4g. It is basically doing
212 * min(xen limit, user limit).
215 user_lowmem
= HVM_BELOW_4G_RAM_END
; /* default */
217 if (HVM_BELOW_4G_RAM_END
<= user_lowmem
) {
218 user_lowmem
= HVM_BELOW_4G_RAM_END
;
221 if (ram_size
>= user_lowmem
) {
222 x86ms
->above_4g_mem_size
= ram_size
- user_lowmem
;
223 x86ms
->below_4g_mem_size
= user_lowmem
;
225 x86ms
->above_4g_mem_size
= 0;
226 x86ms
->below_4g_mem_size
= ram_size
;
228 if (!x86ms
->above_4g_mem_size
) {
229 block_len
= ram_size
;
232 * Xen does not allocate the memory continuously, it keeps a
233 * hole of the size computed above or passed in.
235 block_len
= (4 * GiB
) + x86ms
->above_4g_mem_size
;
237 memory_region_init_ram(&ram_memory
, NULL
, "xen.ram", block_len
,
239 *ram_memory_p
= &ram_memory
;
241 memory_region_init_alias(&ram_640k
, NULL
, "xen.ram.640k",
242 &ram_memory
, 0, 0xa0000);
243 memory_region_add_subregion(sysmem
, 0, &ram_640k
);
244 /* Skip of the VGA IO memory space, it will be registered later by the VGA
247 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
248 * the Options ROM, so it is registered here as RAM.
250 memory_region_init_alias(&ram_lo
, NULL
, "xen.ram.lo",
251 &ram_memory
, 0xc0000,
252 x86ms
->below_4g_mem_size
- 0xc0000);
253 memory_region_add_subregion(sysmem
, 0xc0000, &ram_lo
);
254 if (x86ms
->above_4g_mem_size
> 0) {
255 memory_region_init_alias(&ram_hi
, NULL
, "xen.ram.hi",
256 &ram_memory
, 0x100000000ULL
,
257 x86ms
->above_4g_mem_size
);
258 memory_region_add_subregion(sysmem
, 0x100000000ULL
, &ram_hi
);
262 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
, MemoryRegion
*mr
,
265 unsigned long nr_pfn
;
269 if (runstate_check(RUN_STATE_INMIGRATE
)) {
270 /* RAM already populated in Xen */
271 fprintf(stderr
, "%s: do not alloc "RAM_ADDR_FMT
272 " bytes of ram at "RAM_ADDR_FMT
" when runstate is INMIGRATE\n",
273 __func__
, size
, ram_addr
);
277 if (mr
== &ram_memory
) {
281 trace_xen_ram_alloc(ram_addr
, size
);
283 nr_pfn
= size
>> TARGET_PAGE_BITS
;
284 pfn_list
= g_malloc(sizeof (*pfn_list
) * nr_pfn
);
286 for (i
= 0; i
< nr_pfn
; i
++) {
287 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
290 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
291 error_setg(errp
, "xen: failed to populate ram at " RAM_ADDR_FMT
,
298 static XenPhysmap
*get_physmapping(hwaddr start_addr
, ram_addr_t size
)
300 XenPhysmap
*physmap
= NULL
;
302 start_addr
&= TARGET_PAGE_MASK
;
304 QLIST_FOREACH(physmap
, &xen_physmap
, list
) {
305 if (range_covers_byte(physmap
->start_addr
, physmap
->size
, start_addr
)) {
312 static hwaddr
xen_phys_offset_to_gaddr(hwaddr phys_offset
, ram_addr_t size
)
314 hwaddr addr
= phys_offset
& TARGET_PAGE_MASK
;
315 XenPhysmap
*physmap
= NULL
;
317 QLIST_FOREACH(physmap
, &xen_physmap
, list
) {
318 if (range_covers_byte(physmap
->phys_offset
, physmap
->size
, addr
)) {
319 return physmap
->start_addr
+ (phys_offset
- physmap
->phys_offset
);
326 #ifdef XEN_COMPAT_PHYSMAP
327 static int xen_save_physmap(XenIOState
*state
, XenPhysmap
*physmap
)
329 char path
[80], value
[17];
331 snprintf(path
, sizeof(path
),
332 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/start_addr",
333 xen_domid
, (uint64_t)physmap
->phys_offset
);
334 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)physmap
->start_addr
);
335 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
338 snprintf(path
, sizeof(path
),
339 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/size",
340 xen_domid
, (uint64_t)physmap
->phys_offset
);
341 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)physmap
->size
);
342 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
346 snprintf(path
, sizeof(path
),
347 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/name",
348 xen_domid
, (uint64_t)physmap
->phys_offset
);
349 if (!xs_write(state
->xenstore
, 0, path
,
350 physmap
->name
, strlen(physmap
->name
))) {
357 static int xen_save_physmap(XenIOState
*state
, XenPhysmap
*physmap
)
363 static int xen_add_to_physmap(XenIOState
*state
,
367 hwaddr offset_within_region
)
369 unsigned long nr_pages
;
371 XenPhysmap
*physmap
= NULL
;
372 hwaddr pfn
, start_gpfn
;
373 hwaddr phys_offset
= memory_region_get_ram_addr(mr
);
376 if (get_physmapping(start_addr
, size
)) {
383 /* Xen can only handle a single dirty log region for now and we want
384 * the linear framebuffer to be that region.
385 * Avoid tracking any regions that is not videoram and avoid tracking
386 * the legacy vga region. */
387 if (mr
== framebuffer
&& start_addr
> 0xbffff) {
393 DPRINTF("mapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
394 start_addr
, start_addr
+ size
);
396 mr_name
= memory_region_name(mr
);
398 physmap
= g_malloc(sizeof(XenPhysmap
));
400 physmap
->start_addr
= start_addr
;
401 physmap
->size
= size
;
402 physmap
->name
= mr_name
;
403 physmap
->phys_offset
= phys_offset
;
405 QLIST_INSERT_HEAD(&xen_physmap
, physmap
, list
);
407 if (runstate_check(RUN_STATE_INMIGRATE
)) {
408 /* Now when we have a physmap entry we can replace a dummy mapping with
409 * a real one of guest foreign memory. */
410 uint8_t *p
= xen_replace_cache_entry(phys_offset
, start_addr
, size
);
411 assert(p
&& p
== memory_region_get_ram_ptr(mr
));
416 pfn
= phys_offset
>> TARGET_PAGE_BITS
;
417 start_gpfn
= start_addr
>> TARGET_PAGE_BITS
;
418 nr_pages
= size
>> TARGET_PAGE_BITS
;
419 rc
= xendevicemodel_relocate_memory(xen_dmod
, xen_domid
, nr_pages
, pfn
,
422 int saved_errno
= errno
;
424 error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
425 " to GFN %"HWADDR_PRIx
" failed: %s",
426 nr_pages
, pfn
, start_gpfn
, strerror(saved_errno
));
431 rc
= xendevicemodel_pin_memory_cacheattr(xen_dmod
, xen_domid
,
432 start_addr
>> TARGET_PAGE_BITS
,
433 (start_addr
+ size
- 1) >> TARGET_PAGE_BITS
,
434 XEN_DOMCTL_MEM_CACHEATTR_WB
);
436 error_report("pin_memory_cacheattr failed: %s", strerror(errno
));
438 return xen_save_physmap(state
, physmap
);
441 static int xen_remove_from_physmap(XenIOState
*state
,
446 XenPhysmap
*physmap
= NULL
;
447 hwaddr phys_offset
= 0;
449 physmap
= get_physmapping(start_addr
, size
);
450 if (physmap
== NULL
) {
454 phys_offset
= physmap
->phys_offset
;
455 size
= physmap
->size
;
457 DPRINTF("unmapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
", at "
458 "%"HWADDR_PRIx
"\n", start_addr
, start_addr
+ size
, phys_offset
);
460 size
>>= TARGET_PAGE_BITS
;
461 start_addr
>>= TARGET_PAGE_BITS
;
462 phys_offset
>>= TARGET_PAGE_BITS
;
463 rc
= xendevicemodel_relocate_memory(xen_dmod
, xen_domid
, size
, start_addr
,
466 int saved_errno
= errno
;
468 error_report("relocate_memory "RAM_ADDR_FMT
" pages"
469 " from GFN %"HWADDR_PRIx
470 " to GFN %"HWADDR_PRIx
" failed: %s",
471 size
, start_addr
, phys_offset
, strerror(saved_errno
));
476 QLIST_REMOVE(physmap
, list
);
477 if (state
->log_for_dirtybit
== physmap
) {
478 state
->log_for_dirtybit
= NULL
;
479 g_free(state
->dirty_bitmap
);
480 state
->dirty_bitmap
= NULL
;
487 static void xen_set_memory(struct MemoryListener
*listener
,
488 MemoryRegionSection
*section
,
491 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
492 hwaddr start_addr
= section
->offset_within_address_space
;
493 ram_addr_t size
= int128_get64(section
->size
);
494 bool log_dirty
= memory_region_is_logging(section
->mr
, DIRTY_MEMORY_VGA
);
495 hvmmem_type_t mem_type
;
497 if (section
->mr
== &ram_memory
) {
501 xen_map_memory_section(xen_domid
, state
->ioservid
,
504 xen_unmap_memory_section(xen_domid
, state
->ioservid
,
509 if (!memory_region_is_ram(section
->mr
)) {
513 if (log_dirty
!= add
) {
517 trace_xen_client_set_memory(start_addr
, size
, log_dirty
);
519 start_addr
&= TARGET_PAGE_MASK
;
520 size
= TARGET_PAGE_ALIGN(size
);
523 if (!memory_region_is_rom(section
->mr
)) {
524 xen_add_to_physmap(state
, start_addr
, size
,
525 section
->mr
, section
->offset_within_region
);
527 mem_type
= HVMMEM_ram_ro
;
528 if (xen_set_mem_type(xen_domid
, mem_type
,
529 start_addr
>> TARGET_PAGE_BITS
,
530 size
>> TARGET_PAGE_BITS
)) {
531 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx
"\n",
536 if (xen_remove_from_physmap(state
, start_addr
, size
) < 0) {
537 DPRINTF("physmapping does not exist at "TARGET_FMT_plx
"\n", start_addr
);
542 static void xen_region_add(MemoryListener
*listener
,
543 MemoryRegionSection
*section
)
545 memory_region_ref(section
->mr
);
546 xen_set_memory(listener
, section
, true);
549 static void xen_region_del(MemoryListener
*listener
,
550 MemoryRegionSection
*section
)
552 xen_set_memory(listener
, section
, false);
553 memory_region_unref(section
->mr
);
556 static void xen_io_add(MemoryListener
*listener
,
557 MemoryRegionSection
*section
)
559 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
560 MemoryRegion
*mr
= section
->mr
;
562 if (mr
->ops
== &unassigned_io_ops
) {
566 memory_region_ref(mr
);
568 xen_map_io_section(xen_domid
, state
->ioservid
, section
);
571 static void xen_io_del(MemoryListener
*listener
,
572 MemoryRegionSection
*section
)
574 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
575 MemoryRegion
*mr
= section
->mr
;
577 if (mr
->ops
== &unassigned_io_ops
) {
581 xen_unmap_io_section(xen_domid
, state
->ioservid
, section
);
583 memory_region_unref(mr
);
586 static void xen_device_realize(DeviceListener
*listener
,
589 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
591 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
592 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
593 XenPciDevice
*xendev
= g_new(XenPciDevice
, 1);
595 xendev
->pci_dev
= pci_dev
;
596 xendev
->sbdf
= PCI_BUILD_BDF(pci_dev_bus_num(pci_dev
),
598 QLIST_INSERT_HEAD(&state
->dev_list
, xendev
, entry
);
600 xen_map_pcidev(xen_domid
, state
->ioservid
, pci_dev
);
604 static void xen_device_unrealize(DeviceListener
*listener
,
607 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
609 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
610 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
611 XenPciDevice
*xendev
, *next
;
613 xen_unmap_pcidev(xen_domid
, state
->ioservid
, pci_dev
);
615 QLIST_FOREACH_SAFE(xendev
, &state
->dev_list
, entry
, next
) {
616 if (xendev
->pci_dev
== pci_dev
) {
617 QLIST_REMOVE(xendev
, entry
);
625 static void xen_sync_dirty_bitmap(XenIOState
*state
,
629 hwaddr npages
= size
>> TARGET_PAGE_BITS
;
630 const int width
= sizeof(unsigned long) * 8;
631 size_t bitmap_size
= DIV_ROUND_UP(npages
, width
);
633 const XenPhysmap
*physmap
= NULL
;
635 physmap
= get_physmapping(start_addr
, size
);
636 if (physmap
== NULL
) {
641 if (state
->log_for_dirtybit
== NULL
) {
642 state
->log_for_dirtybit
= physmap
;
643 state
->dirty_bitmap
= g_new(unsigned long, bitmap_size
);
644 } else if (state
->log_for_dirtybit
!= physmap
) {
645 /* Only one range for dirty bitmap can be tracked. */
649 rc
= xen_track_dirty_vram(xen_domid
, start_addr
>> TARGET_PAGE_BITS
,
650 npages
, state
->dirty_bitmap
);
653 #define ENODATA ENOENT
655 if (errno
== ENODATA
) {
656 memory_region_set_dirty(framebuffer
, 0, size
);
657 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
658 ", 0x" TARGET_FMT_plx
"): %s\n",
659 start_addr
, start_addr
+ size
, strerror(errno
));
664 for (i
= 0; i
< bitmap_size
; i
++) {
665 unsigned long map
= state
->dirty_bitmap
[i
];
669 memory_region_set_dirty(framebuffer
,
670 (i
* width
+ j
) * TARGET_PAGE_SIZE
,
676 static void xen_log_start(MemoryListener
*listener
,
677 MemoryRegionSection
*section
,
680 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
682 if (new & ~old
& (1 << DIRTY_MEMORY_VGA
)) {
683 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
684 int128_get64(section
->size
));
688 static void xen_log_stop(MemoryListener
*listener
, MemoryRegionSection
*section
,
691 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
693 if (old
& ~new & (1 << DIRTY_MEMORY_VGA
)) {
694 state
->log_for_dirtybit
= NULL
;
695 g_free(state
->dirty_bitmap
);
696 state
->dirty_bitmap
= NULL
;
697 /* Disable dirty bit tracking */
698 xen_track_dirty_vram(xen_domid
, 0, 0, NULL
);
702 static void xen_log_sync(MemoryListener
*listener
, MemoryRegionSection
*section
)
704 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
706 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
707 int128_get64(section
->size
));
710 static void xen_log_global_start(MemoryListener
*listener
)
713 xen_in_migration
= true;
717 static void xen_log_global_stop(MemoryListener
*listener
)
719 xen_in_migration
= false;
722 static MemoryListener xen_memory_listener
= {
723 .region_add
= xen_region_add
,
724 .region_del
= xen_region_del
,
725 .log_start
= xen_log_start
,
726 .log_stop
= xen_log_stop
,
727 .log_sync
= xen_log_sync
,
728 .log_global_start
= xen_log_global_start
,
729 .log_global_stop
= xen_log_global_stop
,
733 static MemoryListener xen_io_listener
= {
734 .region_add
= xen_io_add
,
735 .region_del
= xen_io_del
,
739 static DeviceListener xen_device_listener
= {
740 .realize
= xen_device_realize
,
741 .unrealize
= xen_device_unrealize
,
744 /* get the ioreq packets from share mem */
745 static ioreq_t
*cpu_get_ioreq_from_shared_memory(XenIOState
*state
, int vcpu
)
747 ioreq_t
*req
= xen_vcpu_ioreq(state
->shared_page
, vcpu
);
749 if (req
->state
!= STATE_IOREQ_READY
) {
750 DPRINTF("I/O request not ready: "
751 "%x, ptr: %x, port: %"PRIx64
", "
752 "data: %"PRIx64
", count: %u, size: %u\n",
753 req
->state
, req
->data_is_ptr
, req
->addr
,
754 req
->data
, req
->count
, req
->size
);
758 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
760 req
->state
= STATE_IOREQ_INPROCESS
;
764 /* use poll to get the port notification */
765 /* ioreq_vec--out,the */
766 /* retval--the number of ioreq packet */
767 static ioreq_t
*cpu_get_ioreq(XenIOState
*state
)
769 MachineState
*ms
= MACHINE(qdev_get_machine());
770 unsigned int max_cpus
= ms
->smp
.max_cpus
;
774 port
= xenevtchn_pending(state
->xce_handle
);
775 if (port
== state
->bufioreq_local_port
) {
776 timer_mod(state
->buffered_io_timer
,
777 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
782 for (i
= 0; i
< max_cpus
; i
++) {
783 if (state
->ioreq_local_port
[i
] == port
) {
789 hw_error("Fatal error while trying to get io event!\n");
792 /* unmask the wanted port again */
793 xenevtchn_unmask(state
->xce_handle
, port
);
795 /* get the io packet from shared memory */
796 state
->send_vcpu
= i
;
797 return cpu_get_ioreq_from_shared_memory(state
, i
);
800 /* read error or read nothing */
804 static uint32_t do_inp(uint32_t addr
, unsigned long size
)
808 return cpu_inb(addr
);
810 return cpu_inw(addr
);
812 return cpu_inl(addr
);
814 hw_error("inp: bad size: %04x %lx", addr
, size
);
818 static void do_outp(uint32_t addr
,
819 unsigned long size
, uint32_t val
)
823 return cpu_outb(addr
, val
);
825 return cpu_outw(addr
, val
);
827 return cpu_outl(addr
, val
);
829 hw_error("outp: bad size: %04x %lx", addr
, size
);
834 * Helper functions which read/write an object from/to physical guest
835 * memory, as part of the implementation of an ioreq.
838 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
839 * val, req->size, 0/1)
840 * except without the integer overflow problems.
842 static void rw_phys_req_item(hwaddr addr
,
843 ioreq_t
*req
, uint32_t i
, void *val
, int rw
)
845 /* Do everything unsigned so overflow just results in a truncated result
846 * and accesses to undesired parts of guest memory, which is up
848 hwaddr offset
= (hwaddr
)req
->size
* i
;
854 cpu_physical_memory_rw(addr
, val
, req
->size
, rw
);
857 static inline void read_phys_req_item(hwaddr addr
,
858 ioreq_t
*req
, uint32_t i
, void *val
)
860 rw_phys_req_item(addr
, req
, i
, val
, 0);
862 static inline void write_phys_req_item(hwaddr addr
,
863 ioreq_t
*req
, uint32_t i
, void *val
)
865 rw_phys_req_item(addr
, req
, i
, val
, 1);
869 static void cpu_ioreq_pio(ioreq_t
*req
)
873 trace_cpu_ioreq_pio(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
874 req
->data
, req
->count
, req
->size
);
876 if (req
->size
> sizeof(uint32_t)) {
877 hw_error("PIO: bad size (%u)", req
->size
);
880 if (req
->dir
== IOREQ_READ
) {
881 if (!req
->data_is_ptr
) {
882 req
->data
= do_inp(req
->addr
, req
->size
);
883 trace_cpu_ioreq_pio_read_reg(req
, req
->data
, req
->addr
,
888 for (i
= 0; i
< req
->count
; i
++) {
889 tmp
= do_inp(req
->addr
, req
->size
);
890 write_phys_req_item(req
->data
, req
, i
, &tmp
);
893 } else if (req
->dir
== IOREQ_WRITE
) {
894 if (!req
->data_is_ptr
) {
895 trace_cpu_ioreq_pio_write_reg(req
, req
->data
, req
->addr
,
897 do_outp(req
->addr
, req
->size
, req
->data
);
899 for (i
= 0; i
< req
->count
; i
++) {
902 read_phys_req_item(req
->data
, req
, i
, &tmp
);
903 do_outp(req
->addr
, req
->size
, tmp
);
909 static void cpu_ioreq_move(ioreq_t
*req
)
913 trace_cpu_ioreq_move(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
914 req
->data
, req
->count
, req
->size
);
916 if (req
->size
> sizeof(req
->data
)) {
917 hw_error("MMIO: bad size (%u)", req
->size
);
920 if (!req
->data_is_ptr
) {
921 if (req
->dir
== IOREQ_READ
) {
922 for (i
= 0; i
< req
->count
; i
++) {
923 read_phys_req_item(req
->addr
, req
, i
, &req
->data
);
925 } else if (req
->dir
== IOREQ_WRITE
) {
926 for (i
= 0; i
< req
->count
; i
++) {
927 write_phys_req_item(req
->addr
, req
, i
, &req
->data
);
933 if (req
->dir
== IOREQ_READ
) {
934 for (i
= 0; i
< req
->count
; i
++) {
935 read_phys_req_item(req
->addr
, req
, i
, &tmp
);
936 write_phys_req_item(req
->data
, req
, i
, &tmp
);
938 } else if (req
->dir
== IOREQ_WRITE
) {
939 for (i
= 0; i
< req
->count
; i
++) {
940 read_phys_req_item(req
->data
, req
, i
, &tmp
);
941 write_phys_req_item(req
->addr
, req
, i
, &tmp
);
947 static void cpu_ioreq_config(XenIOState
*state
, ioreq_t
*req
)
949 uint32_t sbdf
= req
->addr
>> 32;
950 uint32_t reg
= req
->addr
;
951 XenPciDevice
*xendev
;
953 if (req
->size
!= sizeof(uint8_t) && req
->size
!= sizeof(uint16_t) &&
954 req
->size
!= sizeof(uint32_t)) {
955 hw_error("PCI config access: bad size (%u)", req
->size
);
958 if (req
->count
!= 1) {
959 hw_error("PCI config access: bad count (%u)", req
->count
);
962 QLIST_FOREACH(xendev
, &state
->dev_list
, entry
) {
963 if (xendev
->sbdf
!= sbdf
) {
967 if (!req
->data_is_ptr
) {
968 if (req
->dir
== IOREQ_READ
) {
969 req
->data
= pci_host_config_read_common(
970 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
972 trace_cpu_ioreq_config_read(req
, xendev
->sbdf
, reg
,
973 req
->size
, req
->data
);
974 } else if (req
->dir
== IOREQ_WRITE
) {
975 trace_cpu_ioreq_config_write(req
, xendev
->sbdf
, reg
,
976 req
->size
, req
->data
);
977 pci_host_config_write_common(
978 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
979 req
->data
, req
->size
);
984 if (req
->dir
== IOREQ_READ
) {
985 tmp
= pci_host_config_read_common(
986 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
988 trace_cpu_ioreq_config_read(req
, xendev
->sbdf
, reg
,
990 write_phys_req_item(req
->data
, req
, 0, &tmp
);
991 } else if (req
->dir
== IOREQ_WRITE
) {
992 read_phys_req_item(req
->data
, req
, 0, &tmp
);
993 trace_cpu_ioreq_config_write(req
, xendev
->sbdf
, reg
,
995 pci_host_config_write_common(
996 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
1003 static void regs_to_cpu(vmware_regs_t
*vmport_regs
, ioreq_t
*req
)
1008 cpu
= X86_CPU(current_cpu
);
1010 env
->regs
[R_EAX
] = req
->data
;
1011 env
->regs
[R_EBX
] = vmport_regs
->ebx
;
1012 env
->regs
[R_ECX
] = vmport_regs
->ecx
;
1013 env
->regs
[R_EDX
] = vmport_regs
->edx
;
1014 env
->regs
[R_ESI
] = vmport_regs
->esi
;
1015 env
->regs
[R_EDI
] = vmport_regs
->edi
;
1018 static void regs_from_cpu(vmware_regs_t
*vmport_regs
)
1020 X86CPU
*cpu
= X86_CPU(current_cpu
);
1021 CPUX86State
*env
= &cpu
->env
;
1023 vmport_regs
->ebx
= env
->regs
[R_EBX
];
1024 vmport_regs
->ecx
= env
->regs
[R_ECX
];
1025 vmport_regs
->edx
= env
->regs
[R_EDX
];
1026 vmport_regs
->esi
= env
->regs
[R_ESI
];
1027 vmport_regs
->edi
= env
->regs
[R_EDI
];
1030 static void handle_vmport_ioreq(XenIOState
*state
, ioreq_t
*req
)
1032 vmware_regs_t
*vmport_regs
;
1034 assert(state
->shared_vmport_page
);
1036 &state
->shared_vmport_page
->vcpu_vmport_regs
[state
->send_vcpu
];
1037 QEMU_BUILD_BUG_ON(sizeof(*req
) < sizeof(*vmport_regs
));
1039 current_cpu
= state
->cpu_by_vcpu_id
[state
->send_vcpu
];
1040 regs_to_cpu(vmport_regs
, req
);
1042 regs_from_cpu(vmport_regs
);
1046 static void handle_ioreq(XenIOState
*state
, ioreq_t
*req
)
1048 trace_handle_ioreq(req
, req
->type
, req
->dir
, req
->df
, req
->data_is_ptr
,
1049 req
->addr
, req
->data
, req
->count
, req
->size
);
1051 if (!req
->data_is_ptr
&& (req
->dir
== IOREQ_WRITE
) &&
1052 (req
->size
< sizeof (target_ulong
))) {
1053 req
->data
&= ((target_ulong
) 1 << (8 * req
->size
)) - 1;
1056 if (req
->dir
== IOREQ_WRITE
)
1057 trace_handle_ioreq_write(req
, req
->type
, req
->df
, req
->data_is_ptr
,
1058 req
->addr
, req
->data
, req
->count
, req
->size
);
1060 switch (req
->type
) {
1061 case IOREQ_TYPE_PIO
:
1064 case IOREQ_TYPE_COPY
:
1065 cpu_ioreq_move(req
);
1067 case IOREQ_TYPE_VMWARE_PORT
:
1068 handle_vmport_ioreq(state
, req
);
1070 case IOREQ_TYPE_TIMEOFFSET
:
1072 case IOREQ_TYPE_INVALIDATE
:
1073 xen_invalidate_map_cache();
1075 case IOREQ_TYPE_PCI_CONFIG
:
1076 cpu_ioreq_config(state
, req
);
1079 hw_error("Invalid ioreq type 0x%x\n", req
->type
);
1081 if (req
->dir
== IOREQ_READ
) {
1082 trace_handle_ioreq_read(req
, req
->type
, req
->df
, req
->data_is_ptr
,
1083 req
->addr
, req
->data
, req
->count
, req
->size
);
1087 static int handle_buffered_iopage(XenIOState
*state
)
1089 buffered_iopage_t
*buf_page
= state
->buffered_io_page
;
1090 buf_ioreq_t
*buf_req
= NULL
;
1098 memset(&req
, 0x00, sizeof(req
));
1099 req
.state
= STATE_IOREQ_READY
;
1101 req
.dir
= IOREQ_WRITE
;
1104 uint32_t rdptr
= buf_page
->read_pointer
, wrptr
;
1107 wrptr
= buf_page
->write_pointer
;
1109 if (rdptr
!= buf_page
->read_pointer
) {
1112 if (rdptr
== wrptr
) {
1115 buf_req
= &buf_page
->buf_ioreq
[rdptr
% IOREQ_BUFFER_SLOT_NUM
];
1116 req
.size
= 1U << buf_req
->size
;
1117 req
.addr
= buf_req
->addr
;
1118 req
.data
= buf_req
->data
;
1119 req
.type
= buf_req
->type
;
1121 qw
= (req
.size
== 8);
1123 if (rdptr
+ 1 == wrptr
) {
1124 hw_error("Incomplete quad word buffered ioreq");
1126 buf_req
= &buf_page
->buf_ioreq
[(rdptr
+ 1) %
1127 IOREQ_BUFFER_SLOT_NUM
];
1128 req
.data
|= ((uint64_t)buf_req
->data
) << 32;
1132 handle_ioreq(state
, &req
);
1134 /* Only req.data may get updated by handle_ioreq(), albeit even that
1135 * should not happen as such data would never make it to the guest (we
1136 * can only usefully see writes here after all).
1138 assert(req
.state
== STATE_IOREQ_READY
);
1139 assert(req
.count
== 1);
1140 assert(req
.dir
== IOREQ_WRITE
);
1141 assert(!req
.data_is_ptr
);
1143 atomic_add(&buf_page
->read_pointer
, qw
+ 1);
1149 static void handle_buffered_io(void *opaque
)
1151 XenIOState
*state
= opaque
;
1153 if (handle_buffered_iopage(state
)) {
1154 timer_mod(state
->buffered_io_timer
,
1155 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
1157 timer_del(state
->buffered_io_timer
);
1158 xenevtchn_unmask(state
->xce_handle
, state
->bufioreq_local_port
);
1162 static void cpu_handle_ioreq(void *opaque
)
1164 XenIOState
*state
= opaque
;
1165 ioreq_t
*req
= cpu_get_ioreq(state
);
1167 handle_buffered_iopage(state
);
1169 ioreq_t copy
= *req
;
1172 handle_ioreq(state
, ©
);
1173 req
->data
= copy
.data
;
1175 if (req
->state
!= STATE_IOREQ_INPROCESS
) {
1176 fprintf(stderr
, "Badness in I/O request ... not in service?!: "
1177 "%x, ptr: %x, port: %"PRIx64
", "
1178 "data: %"PRIx64
", count: %u, size: %u, type: %u\n",
1179 req
->state
, req
->data_is_ptr
, req
->addr
,
1180 req
->data
, req
->count
, req
->size
, req
->type
);
1181 destroy_hvm_domain(false);
1185 xen_wmb(); /* Update ioreq contents /then/ update state. */
1188 * We do this before we send the response so that the tools
1189 * have the opportunity to pick up on the reset before the
1190 * guest resumes and does a hlt with interrupts disabled which
1191 * causes Xen to powerdown the domain.
1193 if (runstate_is_running()) {
1194 ShutdownCause request
;
1196 if (qemu_shutdown_requested_get()) {
1197 destroy_hvm_domain(false);
1199 request
= qemu_reset_requested_get();
1201 qemu_system_reset(request
);
1202 destroy_hvm_domain(true);
1206 req
->state
= STATE_IORESP_READY
;
1207 xenevtchn_notify(state
->xce_handle
,
1208 state
->ioreq_local_port
[state
->send_vcpu
]);
1212 static void xen_main_loop_prepare(XenIOState
*state
)
1216 if (state
->xce_handle
!= NULL
) {
1217 evtchn_fd
= xenevtchn_fd(state
->xce_handle
);
1220 state
->buffered_io_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, handle_buffered_io
,
1223 if (evtchn_fd
!= -1) {
1224 CPUState
*cpu_state
;
1226 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__
);
1227 CPU_FOREACH(cpu_state
) {
1228 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1229 __func__
, cpu_state
->cpu_index
, cpu_state
);
1230 state
->cpu_by_vcpu_id
[cpu_state
->cpu_index
] = cpu_state
;
1232 qemu_set_fd_handler(evtchn_fd
, cpu_handle_ioreq
, NULL
, state
);
1237 static void xen_hvm_change_state_handler(void *opaque
, int running
,
1240 XenIOState
*state
= opaque
;
1243 xen_main_loop_prepare(state
);
1246 xen_set_ioreq_server_state(xen_domid
,
1248 (rstate
== RUN_STATE_RUNNING
));
1251 static void xen_exit_notifier(Notifier
*n
, void *data
)
1253 XenIOState
*state
= container_of(n
, XenIOState
, exit
);
1255 xen_destroy_ioreq_server(xen_domid
, state
->ioservid
);
1257 xenevtchn_close(state
->xce_handle
);
1258 xs_daemon_close(state
->xenstore
);
1261 #ifdef XEN_COMPAT_PHYSMAP
1262 static void xen_read_physmap(XenIOState
*state
)
1264 XenPhysmap
*physmap
= NULL
;
1265 unsigned int len
, num
, i
;
1266 char path
[80], *value
= NULL
;
1267 char **entries
= NULL
;
1269 snprintf(path
, sizeof(path
),
1270 "/local/domain/0/device-model/%d/physmap", xen_domid
);
1271 entries
= xs_directory(state
->xenstore
, 0, path
, &num
);
1272 if (entries
== NULL
)
1275 for (i
= 0; i
< num
; i
++) {
1276 physmap
= g_malloc(sizeof (XenPhysmap
));
1277 physmap
->phys_offset
= strtoull(entries
[i
], NULL
, 16);
1278 snprintf(path
, sizeof(path
),
1279 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1280 xen_domid
, entries
[i
]);
1281 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1282 if (value
== NULL
) {
1286 physmap
->start_addr
= strtoull(value
, NULL
, 16);
1289 snprintf(path
, sizeof(path
),
1290 "/local/domain/0/device-model/%d/physmap/%s/size",
1291 xen_domid
, entries
[i
]);
1292 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1293 if (value
== NULL
) {
1297 physmap
->size
= strtoull(value
, NULL
, 16);
1300 snprintf(path
, sizeof(path
),
1301 "/local/domain/0/device-model/%d/physmap/%s/name",
1302 xen_domid
, entries
[i
]);
1303 physmap
->name
= xs_read(state
->xenstore
, 0, path
, &len
);
1305 QLIST_INSERT_HEAD(&xen_physmap
, physmap
, list
);
1310 static void xen_read_physmap(XenIOState
*state
)
1315 static void xen_wakeup_notifier(Notifier
*notifier
, void *data
)
1317 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 0);
1320 static int xen_map_ioreq_server(XenIOState
*state
)
1323 xenforeignmemory_resource_handle
*fres
;
1324 xen_pfn_t ioreq_pfn
;
1325 xen_pfn_t bufioreq_pfn
;
1326 evtchn_port_t bufioreq_evtchn
;
1330 * Attempt to map using the resource API and fall back to normal
1331 * foreign mapping if this is not supported.
1333 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq
!= 0);
1334 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1335 fres
= xenforeignmemory_map_resource(xen_fmem
, xen_domid
,
1336 XENMEM_resource_ioreq_server
,
1337 state
->ioservid
, 0, 2,
1339 PROT_READ
| PROT_WRITE
, 0);
1341 trace_xen_map_resource_ioreq(state
->ioservid
, addr
);
1342 state
->buffered_io_page
= addr
;
1343 state
->shared_page
= addr
+ TARGET_PAGE_SIZE
;
1344 } else if (errno
!= EOPNOTSUPP
) {
1345 error_report("failed to map ioreq server resources: error %d handle=%p",
1350 rc
= xen_get_ioreq_server_info(xen_domid
, state
->ioservid
,
1351 (state
->shared_page
== NULL
) ?
1353 (state
->buffered_io_page
== NULL
) ?
1354 &bufioreq_pfn
: NULL
,
1357 error_report("failed to get ioreq server info: error %d handle=%p",
1362 if (state
->shared_page
== NULL
) {
1363 DPRINTF("shared page at pfn %lx\n", ioreq_pfn
);
1365 state
->shared_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1366 PROT_READ
| PROT_WRITE
,
1367 1, &ioreq_pfn
, NULL
);
1368 if (state
->shared_page
== NULL
) {
1369 error_report("map shared IO page returned error %d handle=%p",
1374 if (state
->buffered_io_page
== NULL
) {
1375 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn
);
1377 state
->buffered_io_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1378 PROT_READ
| PROT_WRITE
,
1381 if (state
->buffered_io_page
== NULL
) {
1382 error_report("map buffered IO page returned error %d", errno
);
1387 if (state
->shared_page
== NULL
|| state
->buffered_io_page
== NULL
) {
1391 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn
);
1393 state
->bufioreq_remote_port
= bufioreq_evtchn
;
1398 void xen_hvm_init(PCMachineState
*pcms
, MemoryRegion
**ram_memory
)
1400 MachineState
*ms
= MACHINE(pcms
);
1401 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1403 xen_pfn_t ioreq_pfn
;
1406 state
= g_malloc0(sizeof (XenIOState
));
1408 state
->xce_handle
= xenevtchn_open(NULL
, 0);
1409 if (state
->xce_handle
== NULL
) {
1410 perror("xen: event channel open");
1414 state
->xenstore
= xs_daemon_open();
1415 if (state
->xenstore
== NULL
) {
1416 perror("xen: xenstore open");
1420 xen_create_ioreq_server(xen_domid
, &state
->ioservid
);
1422 state
->exit
.notify
= xen_exit_notifier
;
1423 qemu_add_exit_notifier(&state
->exit
);
1425 state
->suspend
.notify
= xen_suspend_notifier
;
1426 qemu_register_suspend_notifier(&state
->suspend
);
1428 state
->wakeup
.notify
= xen_wakeup_notifier
;
1429 qemu_register_wakeup_notifier(&state
->wakeup
);
1432 * Register wake-up support in QMP query-current-machine API
1434 qemu_register_wakeup_support();
1436 rc
= xen_map_ioreq_server(state
);
1441 rc
= xen_get_vmport_regs_pfn(xen_xc
, xen_domid
, &ioreq_pfn
);
1443 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn
);
1444 state
->shared_vmport_page
=
1445 xenforeignmemory_map(xen_fmem
, xen_domid
, PROT_READ
|PROT_WRITE
,
1446 1, &ioreq_pfn
, NULL
);
1447 if (state
->shared_vmport_page
== NULL
) {
1448 error_report("map shared vmport IO page returned error %d handle=%p",
1452 } else if (rc
!= -ENOSYS
) {
1453 error_report("get vmport regs pfn returned error %d, rc=%d",
1458 /* Note: cpus is empty at this point in init */
1459 state
->cpu_by_vcpu_id
= g_malloc0(max_cpus
* sizeof(CPUState
*));
1461 rc
= xen_set_ioreq_server_state(xen_domid
, state
->ioservid
, true);
1463 error_report("failed to enable ioreq server info: error %d handle=%p",
1468 state
->ioreq_local_port
= g_malloc0(max_cpus
* sizeof (evtchn_port_t
));
1470 /* FIXME: how about if we overflow the page here? */
1471 for (i
= 0; i
< max_cpus
; i
++) {
1472 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1473 xen_vcpu_eport(state
->shared_page
, i
));
1475 error_report("shared evtchn %d bind error %d", i
, errno
);
1478 state
->ioreq_local_port
[i
] = rc
;
1481 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1482 state
->bufioreq_remote_port
);
1484 error_report("buffered evtchn bind error %d", errno
);
1487 state
->bufioreq_local_port
= rc
;
1489 /* Init RAM management */
1490 #ifdef XEN_COMPAT_PHYSMAP
1491 xen_map_cache_init(xen_phys_offset_to_gaddr
, state
);
1493 xen_map_cache_init(NULL
, state
);
1495 xen_ram_init(pcms
, ram_size
, ram_memory
);
1497 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler
, state
);
1499 state
->memory_listener
= xen_memory_listener
;
1500 memory_listener_register(&state
->memory_listener
, &address_space_memory
);
1501 state
->log_for_dirtybit
= NULL
;
1503 state
->io_listener
= xen_io_listener
;
1504 memory_listener_register(&state
->io_listener
, &address_space_io
);
1506 state
->device_listener
= xen_device_listener
;
1507 QLIST_INIT(&state
->dev_list
);
1508 device_listener_register(&state
->device_listener
);
1512 /* Initialize backend core & drivers */
1513 if (xen_be_init() != 0) {
1514 error_report("xen backend core setup failed");
1517 xen_be_register_common();
1519 QLIST_INIT(&xen_physmap
);
1520 xen_read_physmap(state
);
1522 /* Disable ACPI build because Xen handles it */
1523 pcms
->acpi_build_enabled
= false;
1528 error_report("xen hardware virtual machine initialisation failed");
1532 void destroy_hvm_domain(bool reboot
)
1534 xc_interface
*xc_handle
;
1538 unsigned int reason
= reboot
? SHUTDOWN_reboot
: SHUTDOWN_poweroff
;
1541 rc
= xendevicemodel_shutdown(xen_dmod
, xen_domid
, reason
);
1545 if (errno
!= ENOTTY
/* old Xen */) {
1546 perror("xendevicemodel_shutdown failed");
1548 /* well, try the old thing then */
1551 xc_handle
= xc_interface_open(0, 0, 0);
1552 if (xc_handle
== NULL
) {
1553 fprintf(stderr
, "Cannot acquire xenctrl handle\n");
1555 sts
= xc_domain_shutdown(xc_handle
, xen_domid
, reason
);
1557 fprintf(stderr
, "xc_domain_shutdown failed to issue %s, "
1558 "sts %d, %s\n", reboot
? "reboot" : "poweroff",
1559 sts
, strerror(errno
));
1561 fprintf(stderr
, "Issued domain %d %s\n", xen_domid
,
1562 reboot
? "reboot" : "poweroff");
1564 xc_interface_close(xc_handle
);
1568 void xen_register_framebuffer(MemoryRegion
*mr
)
1573 void xen_shutdown_fatal_error(const char *fmt
, ...)
1578 vfprintf(stderr
, fmt
, ap
);
1580 fprintf(stderr
, "Will destroy the domain.\n");
1581 /* destroy the domain */
1582 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR
);
1585 void xen_hvm_modified_memory(ram_addr_t start
, ram_addr_t length
)
1587 if (unlikely(xen_in_migration
)) {
1589 ram_addr_t start_pfn
, nb_pages
;
1591 start
= xen_phys_offset_to_gaddr(start
, length
);
1594 length
= TARGET_PAGE_SIZE
;
1596 start_pfn
= start
>> TARGET_PAGE_BITS
;
1597 nb_pages
= ((start
+ length
+ TARGET_PAGE_SIZE
- 1) >> TARGET_PAGE_BITS
)
1599 rc
= xen_modified_memory(xen_domid
, start_pfn
, nb_pages
);
1602 "%s failed for "RAM_ADDR_FMT
" ("RAM_ADDR_FMT
"): %i, %s\n",
1603 __func__
, start
, nb_pages
, errno
, strerror(errno
));
1608 void qmp_xen_set_global_dirty_log(bool enable
, Error
**errp
)
1611 memory_global_dirty_log_start();
1613 memory_global_dirty_log_stop();