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 "hw/xen/xen-x86.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-migration.h"
28 #include "qemu/error-report.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/range.h"
31 #include "sysemu/runstate.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/xen.h"
34 #include "sysemu/xen-mapcache.h"
36 #include "exec/address-spaces.h"
38 #include <xen/hvm/ioreq.h>
39 #include <xen/hvm/e820.h>
41 //#define DEBUG_XEN_HVM
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
47 #define DPRINTF(fmt, ...) \
51 static MemoryRegion ram_memory
, ram_640k
, ram_lo
, ram_hi
;
52 static MemoryRegion
*framebuffer
;
53 static bool xen_in_migration
;
55 /* Compatibility with older version */
57 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
58 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
59 * needs to be included before this block and hw/xen/xen_common.h needs to
60 * be included before xen/hvm/ioreq.h
62 #ifndef IOREQ_TYPE_VMWARE_PORT
63 #define IOREQ_TYPE_VMWARE_PORT 3
71 typedef struct vmware_regs vmware_regs_t
;
73 struct shared_vmport_iopage
{
74 struct vmware_regs vcpu_vmport_regs
[1];
76 typedef struct shared_vmport_iopage shared_vmport_iopage_t
;
79 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
81 return shared_page
->vcpu_ioreq
[i
].vp_eport
;
83 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
85 return &shared_page
->vcpu_ioreq
[vcpu
];
88 #define BUFFER_IO_MAX_DELAY 100
90 typedef struct XenPhysmap
{
96 QLIST_ENTRY(XenPhysmap
) list
;
99 static QLIST_HEAD(, XenPhysmap
) xen_physmap
;
101 typedef struct XenPciDevice
{
104 QLIST_ENTRY(XenPciDevice
) entry
;
107 typedef struct XenIOState
{
109 shared_iopage_t
*shared_page
;
110 shared_vmport_iopage_t
*shared_vmport_page
;
111 buffered_iopage_t
*buffered_io_page
;
112 QEMUTimer
*buffered_io_timer
;
113 CPUState
**cpu_by_vcpu_id
;
114 /* the evtchn port for polling the notification, */
115 evtchn_port_t
*ioreq_local_port
;
116 /* evtchn remote and local ports for buffered io */
117 evtchn_port_t bufioreq_remote_port
;
118 evtchn_port_t bufioreq_local_port
;
119 /* the evtchn fd for polling */
120 xenevtchn_handle
*xce_handle
;
121 /* which vcpu we are serving */
124 struct xs_handle
*xenstore
;
125 MemoryListener memory_listener
;
126 MemoryListener io_listener
;
127 QLIST_HEAD(, XenPciDevice
) dev_list
;
128 DeviceListener device_listener
;
129 hwaddr free_phys_offset
;
130 const XenPhysmap
*log_for_dirtybit
;
131 /* Buffer used by xen_sync_dirty_bitmap */
132 unsigned long *dirty_bitmap
;
139 /* Xen specific function for piix pci */
141 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
143 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
146 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
148 xen_set_pci_intx_level(xen_domid
, 0, 0, irq_num
>> 2,
152 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
156 /* Scan for updates to PCI link routes (0x60-0x63). */
157 for (i
= 0; i
< len
; i
++) {
158 uint8_t v
= (val
>> (8 * i
)) & 0xff;
163 if (((address
+ i
) >= PIIX_PIRQCA
) && ((address
+ i
) <= PIIX_PIRQCD
)) {
164 xen_set_pci_link_route(xen_domid
, address
+ i
- PIIX_PIRQCA
, v
);
169 int xen_is_pirq_msi(uint32_t msi_data
)
171 /* If vector is 0, the msi is remapped into a pirq, passed as
174 return ((msi_data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
) == 0;
177 void xen_hvm_inject_msi(uint64_t addr
, uint32_t data
)
179 xen_inject_msi(xen_domid
, addr
, data
);
182 static void xen_suspend_notifier(Notifier
*notifier
, void *data
)
184 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 3);
187 /* Xen Interrupt Controller */
189 static void xen_set_irq(void *opaque
, int irq
, int level
)
191 xen_set_isa_irq_level(xen_domid
, irq
, level
);
194 qemu_irq
*xen_interrupt_controller_init(void)
196 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
201 static void xen_ram_init(PCMachineState
*pcms
,
202 ram_addr_t ram_size
, MemoryRegion
**ram_memory_p
)
204 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
205 MemoryRegion
*sysmem
= get_system_memory();
206 ram_addr_t block_len
;
207 uint64_t user_lowmem
=
208 object_property_get_uint(qdev_get_machine(),
209 PC_MACHINE_MAX_RAM_BELOW_4G
,
212 /* Handle the machine opt max-ram-below-4g. It is basically doing
213 * min(xen limit, user limit).
216 user_lowmem
= HVM_BELOW_4G_RAM_END
; /* default */
218 if (HVM_BELOW_4G_RAM_END
<= user_lowmem
) {
219 user_lowmem
= HVM_BELOW_4G_RAM_END
;
222 if (ram_size
>= user_lowmem
) {
223 x86ms
->above_4g_mem_size
= ram_size
- user_lowmem
;
224 x86ms
->below_4g_mem_size
= user_lowmem
;
226 x86ms
->above_4g_mem_size
= 0;
227 x86ms
->below_4g_mem_size
= ram_size
;
229 if (!x86ms
->above_4g_mem_size
) {
230 block_len
= ram_size
;
233 * Xen does not allocate the memory continuously, it keeps a
234 * hole of the size computed above or passed in.
236 block_len
= (4 * GiB
) + x86ms
->above_4g_mem_size
;
238 memory_region_init_ram(&ram_memory
, NULL
, "xen.ram", block_len
,
240 *ram_memory_p
= &ram_memory
;
242 memory_region_init_alias(&ram_640k
, NULL
, "xen.ram.640k",
243 &ram_memory
, 0, 0xa0000);
244 memory_region_add_subregion(sysmem
, 0, &ram_640k
);
245 /* Skip of the VGA IO memory space, it will be registered later by the VGA
248 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
249 * the Options ROM, so it is registered here as RAM.
251 memory_region_init_alias(&ram_lo
, NULL
, "xen.ram.lo",
252 &ram_memory
, 0xc0000,
253 x86ms
->below_4g_mem_size
- 0xc0000);
254 memory_region_add_subregion(sysmem
, 0xc0000, &ram_lo
);
255 if (x86ms
->above_4g_mem_size
> 0) {
256 memory_region_init_alias(&ram_hi
, NULL
, "xen.ram.hi",
257 &ram_memory
, 0x100000000ULL
,
258 x86ms
->above_4g_mem_size
);
259 memory_region_add_subregion(sysmem
, 0x100000000ULL
, &ram_hi
);
263 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
, MemoryRegion
*mr
,
266 unsigned long nr_pfn
;
270 if (runstate_check(RUN_STATE_INMIGRATE
)) {
271 /* RAM already populated in Xen */
272 fprintf(stderr
, "%s: do not alloc "RAM_ADDR_FMT
273 " bytes of ram at "RAM_ADDR_FMT
" when runstate is INMIGRATE\n",
274 __func__
, size
, ram_addr
);
278 if (mr
== &ram_memory
) {
282 trace_xen_ram_alloc(ram_addr
, size
);
284 nr_pfn
= size
>> TARGET_PAGE_BITS
;
285 pfn_list
= g_malloc(sizeof (*pfn_list
) * nr_pfn
);
287 for (i
= 0; i
< nr_pfn
; i
++) {
288 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
291 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
292 error_setg(errp
, "xen: failed to populate ram at " RAM_ADDR_FMT
,
299 static XenPhysmap
*get_physmapping(hwaddr start_addr
, ram_addr_t size
)
301 XenPhysmap
*physmap
= NULL
;
303 start_addr
&= TARGET_PAGE_MASK
;
305 QLIST_FOREACH(physmap
, &xen_physmap
, list
) {
306 if (range_covers_byte(physmap
->start_addr
, physmap
->size
, start_addr
)) {
313 static hwaddr
xen_phys_offset_to_gaddr(hwaddr phys_offset
, ram_addr_t size
)
315 hwaddr addr
= phys_offset
& TARGET_PAGE_MASK
;
316 XenPhysmap
*physmap
= NULL
;
318 QLIST_FOREACH(physmap
, &xen_physmap
, list
) {
319 if (range_covers_byte(physmap
->phys_offset
, physmap
->size
, addr
)) {
320 return physmap
->start_addr
+ (phys_offset
- physmap
->phys_offset
);
327 #ifdef XEN_COMPAT_PHYSMAP
328 static int xen_save_physmap(XenIOState
*state
, XenPhysmap
*physmap
)
330 char path
[80], value
[17];
332 snprintf(path
, sizeof(path
),
333 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/start_addr",
334 xen_domid
, (uint64_t)physmap
->phys_offset
);
335 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)physmap
->start_addr
);
336 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
339 snprintf(path
, sizeof(path
),
340 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/size",
341 xen_domid
, (uint64_t)physmap
->phys_offset
);
342 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)physmap
->size
);
343 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
347 snprintf(path
, sizeof(path
),
348 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/name",
349 xen_domid
, (uint64_t)physmap
->phys_offset
);
350 if (!xs_write(state
->xenstore
, 0, path
,
351 physmap
->name
, strlen(physmap
->name
))) {
358 static int xen_save_physmap(XenIOState
*state
, XenPhysmap
*physmap
)
364 static int xen_add_to_physmap(XenIOState
*state
,
368 hwaddr offset_within_region
)
370 unsigned long nr_pages
;
372 XenPhysmap
*physmap
= NULL
;
373 hwaddr pfn
, start_gpfn
;
374 hwaddr phys_offset
= memory_region_get_ram_addr(mr
);
377 if (get_physmapping(start_addr
, size
)) {
384 /* Xen can only handle a single dirty log region for now and we want
385 * the linear framebuffer to be that region.
386 * Avoid tracking any regions that is not videoram and avoid tracking
387 * the legacy vga region. */
388 if (mr
== framebuffer
&& start_addr
> 0xbffff) {
394 DPRINTF("mapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
395 start_addr
, start_addr
+ size
);
397 mr_name
= memory_region_name(mr
);
399 physmap
= g_malloc(sizeof(XenPhysmap
));
401 physmap
->start_addr
= start_addr
;
402 physmap
->size
= size
;
403 physmap
->name
= mr_name
;
404 physmap
->phys_offset
= phys_offset
;
406 QLIST_INSERT_HEAD(&xen_physmap
, physmap
, list
);
408 if (runstate_check(RUN_STATE_INMIGRATE
)) {
409 /* Now when we have a physmap entry we can replace a dummy mapping with
410 * a real one of guest foreign memory. */
411 uint8_t *p
= xen_replace_cache_entry(phys_offset
, start_addr
, size
);
412 assert(p
&& p
== memory_region_get_ram_ptr(mr
));
417 pfn
= phys_offset
>> TARGET_PAGE_BITS
;
418 start_gpfn
= start_addr
>> TARGET_PAGE_BITS
;
419 nr_pages
= size
>> TARGET_PAGE_BITS
;
420 rc
= xendevicemodel_relocate_memory(xen_dmod
, xen_domid
, nr_pages
, pfn
,
423 int saved_errno
= errno
;
425 error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
426 " to GFN %"HWADDR_PRIx
" failed: %s",
427 nr_pages
, pfn
, start_gpfn
, strerror(saved_errno
));
432 rc
= xendevicemodel_pin_memory_cacheattr(xen_dmod
, xen_domid
,
433 start_addr
>> TARGET_PAGE_BITS
,
434 (start_addr
+ size
- 1) >> TARGET_PAGE_BITS
,
435 XEN_DOMCTL_MEM_CACHEATTR_WB
);
437 error_report("pin_memory_cacheattr failed: %s", strerror(errno
));
439 return xen_save_physmap(state
, physmap
);
442 static int xen_remove_from_physmap(XenIOState
*state
,
447 XenPhysmap
*physmap
= NULL
;
448 hwaddr phys_offset
= 0;
450 physmap
= get_physmapping(start_addr
, size
);
451 if (physmap
== NULL
) {
455 phys_offset
= physmap
->phys_offset
;
456 size
= physmap
->size
;
458 DPRINTF("unmapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
", at "
459 "%"HWADDR_PRIx
"\n", start_addr
, start_addr
+ size
, phys_offset
);
461 size
>>= TARGET_PAGE_BITS
;
462 start_addr
>>= TARGET_PAGE_BITS
;
463 phys_offset
>>= TARGET_PAGE_BITS
;
464 rc
= xendevicemodel_relocate_memory(xen_dmod
, xen_domid
, size
, start_addr
,
467 int saved_errno
= errno
;
469 error_report("relocate_memory "RAM_ADDR_FMT
" pages"
470 " from GFN %"HWADDR_PRIx
471 " to GFN %"HWADDR_PRIx
" failed: %s",
472 size
, start_addr
, phys_offset
, strerror(saved_errno
));
477 QLIST_REMOVE(physmap
, list
);
478 if (state
->log_for_dirtybit
== physmap
) {
479 state
->log_for_dirtybit
= NULL
;
480 g_free(state
->dirty_bitmap
);
481 state
->dirty_bitmap
= NULL
;
488 static void xen_set_memory(struct MemoryListener
*listener
,
489 MemoryRegionSection
*section
,
492 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
493 hwaddr start_addr
= section
->offset_within_address_space
;
494 ram_addr_t size
= int128_get64(section
->size
);
495 bool log_dirty
= memory_region_is_logging(section
->mr
, DIRTY_MEMORY_VGA
);
496 hvmmem_type_t mem_type
;
498 if (section
->mr
== &ram_memory
) {
502 xen_map_memory_section(xen_domid
, state
->ioservid
,
505 xen_unmap_memory_section(xen_domid
, state
->ioservid
,
510 if (!memory_region_is_ram(section
->mr
)) {
514 if (log_dirty
!= add
) {
518 trace_xen_client_set_memory(start_addr
, size
, log_dirty
);
520 start_addr
&= TARGET_PAGE_MASK
;
521 size
= TARGET_PAGE_ALIGN(size
);
524 if (!memory_region_is_rom(section
->mr
)) {
525 xen_add_to_physmap(state
, start_addr
, size
,
526 section
->mr
, section
->offset_within_region
);
528 mem_type
= HVMMEM_ram_ro
;
529 if (xen_set_mem_type(xen_domid
, mem_type
,
530 start_addr
>> TARGET_PAGE_BITS
,
531 size
>> TARGET_PAGE_BITS
)) {
532 DPRINTF("xen_set_mem_type error, addr: "TARGET_FMT_plx
"\n",
537 if (xen_remove_from_physmap(state
, start_addr
, size
) < 0) {
538 DPRINTF("physmapping does not exist at "TARGET_FMT_plx
"\n", start_addr
);
543 static void xen_region_add(MemoryListener
*listener
,
544 MemoryRegionSection
*section
)
546 memory_region_ref(section
->mr
);
547 xen_set_memory(listener
, section
, true);
550 static void xen_region_del(MemoryListener
*listener
,
551 MemoryRegionSection
*section
)
553 xen_set_memory(listener
, section
, false);
554 memory_region_unref(section
->mr
);
557 static void xen_io_add(MemoryListener
*listener
,
558 MemoryRegionSection
*section
)
560 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
561 MemoryRegion
*mr
= section
->mr
;
563 if (mr
->ops
== &unassigned_io_ops
) {
567 memory_region_ref(mr
);
569 xen_map_io_section(xen_domid
, state
->ioservid
, section
);
572 static void xen_io_del(MemoryListener
*listener
,
573 MemoryRegionSection
*section
)
575 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
576 MemoryRegion
*mr
= section
->mr
;
578 if (mr
->ops
== &unassigned_io_ops
) {
582 xen_unmap_io_section(xen_domid
, state
->ioservid
, section
);
584 memory_region_unref(mr
);
587 static void xen_device_realize(DeviceListener
*listener
,
590 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
592 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
593 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
594 XenPciDevice
*xendev
= g_new(XenPciDevice
, 1);
596 xendev
->pci_dev
= pci_dev
;
597 xendev
->sbdf
= PCI_BUILD_BDF(pci_dev_bus_num(pci_dev
),
599 QLIST_INSERT_HEAD(&state
->dev_list
, xendev
, entry
);
601 xen_map_pcidev(xen_domid
, state
->ioservid
, pci_dev
);
605 static void xen_device_unrealize(DeviceListener
*listener
,
608 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
610 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
611 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
612 XenPciDevice
*xendev
, *next
;
614 xen_unmap_pcidev(xen_domid
, state
->ioservid
, pci_dev
);
616 QLIST_FOREACH_SAFE(xendev
, &state
->dev_list
, entry
, next
) {
617 if (xendev
->pci_dev
== pci_dev
) {
618 QLIST_REMOVE(xendev
, entry
);
626 static void xen_sync_dirty_bitmap(XenIOState
*state
,
630 hwaddr npages
= size
>> TARGET_PAGE_BITS
;
631 const int width
= sizeof(unsigned long) * 8;
632 size_t bitmap_size
= DIV_ROUND_UP(npages
, width
);
634 const XenPhysmap
*physmap
= NULL
;
636 physmap
= get_physmapping(start_addr
, size
);
637 if (physmap
== NULL
) {
642 if (state
->log_for_dirtybit
== NULL
) {
643 state
->log_for_dirtybit
= physmap
;
644 state
->dirty_bitmap
= g_new(unsigned long, bitmap_size
);
645 } else if (state
->log_for_dirtybit
!= physmap
) {
646 /* Only one range for dirty bitmap can be tracked. */
650 rc
= xen_track_dirty_vram(xen_domid
, start_addr
>> TARGET_PAGE_BITS
,
651 npages
, state
->dirty_bitmap
);
654 #define ENODATA ENOENT
656 if (errno
== ENODATA
) {
657 memory_region_set_dirty(framebuffer
, 0, size
);
658 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
659 ", 0x" TARGET_FMT_plx
"): %s\n",
660 start_addr
, start_addr
+ size
, strerror(errno
));
665 for (i
= 0; i
< bitmap_size
; i
++) {
666 unsigned long map
= state
->dirty_bitmap
[i
];
670 memory_region_set_dirty(framebuffer
,
671 (i
* width
+ j
) * TARGET_PAGE_SIZE
,
677 static void xen_log_start(MemoryListener
*listener
,
678 MemoryRegionSection
*section
,
681 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
683 if (new & ~old
& (1 << DIRTY_MEMORY_VGA
)) {
684 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
685 int128_get64(section
->size
));
689 static void xen_log_stop(MemoryListener
*listener
, MemoryRegionSection
*section
,
692 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
694 if (old
& ~new & (1 << DIRTY_MEMORY_VGA
)) {
695 state
->log_for_dirtybit
= NULL
;
696 g_free(state
->dirty_bitmap
);
697 state
->dirty_bitmap
= NULL
;
698 /* Disable dirty bit tracking */
699 xen_track_dirty_vram(xen_domid
, 0, 0, NULL
);
703 static void xen_log_sync(MemoryListener
*listener
, MemoryRegionSection
*section
)
705 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
707 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
708 int128_get64(section
->size
));
711 static void xen_log_global_start(MemoryListener
*listener
)
714 xen_in_migration
= true;
718 static void xen_log_global_stop(MemoryListener
*listener
)
720 xen_in_migration
= false;
723 static MemoryListener xen_memory_listener
= {
724 .region_add
= xen_region_add
,
725 .region_del
= xen_region_del
,
726 .log_start
= xen_log_start
,
727 .log_stop
= xen_log_stop
,
728 .log_sync
= xen_log_sync
,
729 .log_global_start
= xen_log_global_start
,
730 .log_global_stop
= xen_log_global_stop
,
734 static MemoryListener xen_io_listener
= {
735 .region_add
= xen_io_add
,
736 .region_del
= xen_io_del
,
740 static DeviceListener xen_device_listener
= {
741 .realize
= xen_device_realize
,
742 .unrealize
= xen_device_unrealize
,
745 /* get the ioreq packets from share mem */
746 static ioreq_t
*cpu_get_ioreq_from_shared_memory(XenIOState
*state
, int vcpu
)
748 ioreq_t
*req
= xen_vcpu_ioreq(state
->shared_page
, vcpu
);
750 if (req
->state
!= STATE_IOREQ_READY
) {
751 DPRINTF("I/O request not ready: "
752 "%x, ptr: %x, port: %"PRIx64
", "
753 "data: %"PRIx64
", count: %u, size: %u\n",
754 req
->state
, req
->data_is_ptr
, req
->addr
,
755 req
->data
, req
->count
, req
->size
);
759 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
761 req
->state
= STATE_IOREQ_INPROCESS
;
765 /* use poll to get the port notification */
766 /* ioreq_vec--out,the */
767 /* retval--the number of ioreq packet */
768 static ioreq_t
*cpu_get_ioreq(XenIOState
*state
)
770 MachineState
*ms
= MACHINE(qdev_get_machine());
771 unsigned int max_cpus
= ms
->smp
.max_cpus
;
775 port
= xenevtchn_pending(state
->xce_handle
);
776 if (port
== state
->bufioreq_local_port
) {
777 timer_mod(state
->buffered_io_timer
,
778 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
783 for (i
= 0; i
< max_cpus
; i
++) {
784 if (state
->ioreq_local_port
[i
] == port
) {
790 hw_error("Fatal error while trying to get io event!\n");
793 /* unmask the wanted port again */
794 xenevtchn_unmask(state
->xce_handle
, port
);
796 /* get the io packet from shared memory */
797 state
->send_vcpu
= i
;
798 return cpu_get_ioreq_from_shared_memory(state
, i
);
801 /* read error or read nothing */
805 static uint32_t do_inp(uint32_t addr
, unsigned long size
)
809 return cpu_inb(addr
);
811 return cpu_inw(addr
);
813 return cpu_inl(addr
);
815 hw_error("inp: bad size: %04x %lx", addr
, size
);
819 static void do_outp(uint32_t addr
,
820 unsigned long size
, uint32_t val
)
824 return cpu_outb(addr
, val
);
826 return cpu_outw(addr
, val
);
828 return cpu_outl(addr
, val
);
830 hw_error("outp: bad size: %04x %lx", addr
, size
);
835 * Helper functions which read/write an object from/to physical guest
836 * memory, as part of the implementation of an ioreq.
839 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
840 * val, req->size, 0/1)
841 * except without the integer overflow problems.
843 static void rw_phys_req_item(hwaddr addr
,
844 ioreq_t
*req
, uint32_t i
, void *val
, int rw
)
846 /* Do everything unsigned so overflow just results in a truncated result
847 * and accesses to undesired parts of guest memory, which is up
849 hwaddr offset
= (hwaddr
)req
->size
* i
;
855 cpu_physical_memory_rw(addr
, val
, req
->size
, rw
);
858 static inline void read_phys_req_item(hwaddr addr
,
859 ioreq_t
*req
, uint32_t i
, void *val
)
861 rw_phys_req_item(addr
, req
, i
, val
, 0);
863 static inline void write_phys_req_item(hwaddr addr
,
864 ioreq_t
*req
, uint32_t i
, void *val
)
866 rw_phys_req_item(addr
, req
, i
, val
, 1);
870 static void cpu_ioreq_pio(ioreq_t
*req
)
874 trace_cpu_ioreq_pio(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
875 req
->data
, req
->count
, req
->size
);
877 if (req
->size
> sizeof(uint32_t)) {
878 hw_error("PIO: bad size (%u)", req
->size
);
881 if (req
->dir
== IOREQ_READ
) {
882 if (!req
->data_is_ptr
) {
883 req
->data
= do_inp(req
->addr
, req
->size
);
884 trace_cpu_ioreq_pio_read_reg(req
, req
->data
, req
->addr
,
889 for (i
= 0; i
< req
->count
; i
++) {
890 tmp
= do_inp(req
->addr
, req
->size
);
891 write_phys_req_item(req
->data
, req
, i
, &tmp
);
894 } else if (req
->dir
== IOREQ_WRITE
) {
895 if (!req
->data_is_ptr
) {
896 trace_cpu_ioreq_pio_write_reg(req
, req
->data
, req
->addr
,
898 do_outp(req
->addr
, req
->size
, req
->data
);
900 for (i
= 0; i
< req
->count
; i
++) {
903 read_phys_req_item(req
->data
, req
, i
, &tmp
);
904 do_outp(req
->addr
, req
->size
, tmp
);
910 static void cpu_ioreq_move(ioreq_t
*req
)
914 trace_cpu_ioreq_move(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
915 req
->data
, req
->count
, req
->size
);
917 if (req
->size
> sizeof(req
->data
)) {
918 hw_error("MMIO: bad size (%u)", req
->size
);
921 if (!req
->data_is_ptr
) {
922 if (req
->dir
== IOREQ_READ
) {
923 for (i
= 0; i
< req
->count
; i
++) {
924 read_phys_req_item(req
->addr
, req
, i
, &req
->data
);
926 } else if (req
->dir
== IOREQ_WRITE
) {
927 for (i
= 0; i
< req
->count
; i
++) {
928 write_phys_req_item(req
->addr
, req
, i
, &req
->data
);
934 if (req
->dir
== IOREQ_READ
) {
935 for (i
= 0; i
< req
->count
; i
++) {
936 read_phys_req_item(req
->addr
, req
, i
, &tmp
);
937 write_phys_req_item(req
->data
, req
, i
, &tmp
);
939 } else if (req
->dir
== IOREQ_WRITE
) {
940 for (i
= 0; i
< req
->count
; i
++) {
941 read_phys_req_item(req
->data
, req
, i
, &tmp
);
942 write_phys_req_item(req
->addr
, req
, i
, &tmp
);
948 static void cpu_ioreq_config(XenIOState
*state
, ioreq_t
*req
)
950 uint32_t sbdf
= req
->addr
>> 32;
951 uint32_t reg
= req
->addr
;
952 XenPciDevice
*xendev
;
954 if (req
->size
!= sizeof(uint8_t) && req
->size
!= sizeof(uint16_t) &&
955 req
->size
!= sizeof(uint32_t)) {
956 hw_error("PCI config access: bad size (%u)", req
->size
);
959 if (req
->count
!= 1) {
960 hw_error("PCI config access: bad count (%u)", req
->count
);
963 QLIST_FOREACH(xendev
, &state
->dev_list
, entry
) {
964 if (xendev
->sbdf
!= sbdf
) {
968 if (!req
->data_is_ptr
) {
969 if (req
->dir
== IOREQ_READ
) {
970 req
->data
= pci_host_config_read_common(
971 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
973 trace_cpu_ioreq_config_read(req
, xendev
->sbdf
, reg
,
974 req
->size
, req
->data
);
975 } else if (req
->dir
== IOREQ_WRITE
) {
976 trace_cpu_ioreq_config_write(req
, xendev
->sbdf
, reg
,
977 req
->size
, req
->data
);
978 pci_host_config_write_common(
979 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
980 req
->data
, req
->size
);
985 if (req
->dir
== IOREQ_READ
) {
986 tmp
= pci_host_config_read_common(
987 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
989 trace_cpu_ioreq_config_read(req
, xendev
->sbdf
, reg
,
991 write_phys_req_item(req
->data
, req
, 0, &tmp
);
992 } else if (req
->dir
== IOREQ_WRITE
) {
993 read_phys_req_item(req
->data
, req
, 0, &tmp
);
994 trace_cpu_ioreq_config_write(req
, xendev
->sbdf
, reg
,
996 pci_host_config_write_common(
997 xendev
->pci_dev
, reg
, PCI_CONFIG_SPACE_SIZE
,
1004 static void regs_to_cpu(vmware_regs_t
*vmport_regs
, ioreq_t
*req
)
1009 cpu
= X86_CPU(current_cpu
);
1011 env
->regs
[R_EAX
] = req
->data
;
1012 env
->regs
[R_EBX
] = vmport_regs
->ebx
;
1013 env
->regs
[R_ECX
] = vmport_regs
->ecx
;
1014 env
->regs
[R_EDX
] = vmport_regs
->edx
;
1015 env
->regs
[R_ESI
] = vmport_regs
->esi
;
1016 env
->regs
[R_EDI
] = vmport_regs
->edi
;
1019 static void regs_from_cpu(vmware_regs_t
*vmport_regs
)
1021 X86CPU
*cpu
= X86_CPU(current_cpu
);
1022 CPUX86State
*env
= &cpu
->env
;
1024 vmport_regs
->ebx
= env
->regs
[R_EBX
];
1025 vmport_regs
->ecx
= env
->regs
[R_ECX
];
1026 vmport_regs
->edx
= env
->regs
[R_EDX
];
1027 vmport_regs
->esi
= env
->regs
[R_ESI
];
1028 vmport_regs
->edi
= env
->regs
[R_EDI
];
1031 static void handle_vmport_ioreq(XenIOState
*state
, ioreq_t
*req
)
1033 vmware_regs_t
*vmport_regs
;
1035 assert(state
->shared_vmport_page
);
1037 &state
->shared_vmport_page
->vcpu_vmport_regs
[state
->send_vcpu
];
1038 QEMU_BUILD_BUG_ON(sizeof(*req
) < sizeof(*vmport_regs
));
1040 current_cpu
= state
->cpu_by_vcpu_id
[state
->send_vcpu
];
1041 regs_to_cpu(vmport_regs
, req
);
1043 regs_from_cpu(vmport_regs
);
1047 static void handle_ioreq(XenIOState
*state
, ioreq_t
*req
)
1049 trace_handle_ioreq(req
, req
->type
, req
->dir
, req
->df
, req
->data_is_ptr
,
1050 req
->addr
, req
->data
, req
->count
, req
->size
);
1052 if (!req
->data_is_ptr
&& (req
->dir
== IOREQ_WRITE
) &&
1053 (req
->size
< sizeof (target_ulong
))) {
1054 req
->data
&= ((target_ulong
) 1 << (8 * req
->size
)) - 1;
1057 if (req
->dir
== IOREQ_WRITE
)
1058 trace_handle_ioreq_write(req
, req
->type
, req
->df
, req
->data_is_ptr
,
1059 req
->addr
, req
->data
, req
->count
, req
->size
);
1061 switch (req
->type
) {
1062 case IOREQ_TYPE_PIO
:
1065 case IOREQ_TYPE_COPY
:
1066 cpu_ioreq_move(req
);
1068 case IOREQ_TYPE_VMWARE_PORT
:
1069 handle_vmport_ioreq(state
, req
);
1071 case IOREQ_TYPE_TIMEOFFSET
:
1073 case IOREQ_TYPE_INVALIDATE
:
1074 xen_invalidate_map_cache();
1076 case IOREQ_TYPE_PCI_CONFIG
:
1077 cpu_ioreq_config(state
, req
);
1080 hw_error("Invalid ioreq type 0x%x\n", req
->type
);
1082 if (req
->dir
== IOREQ_READ
) {
1083 trace_handle_ioreq_read(req
, req
->type
, req
->df
, req
->data_is_ptr
,
1084 req
->addr
, req
->data
, req
->count
, req
->size
);
1088 static int handle_buffered_iopage(XenIOState
*state
)
1090 buffered_iopage_t
*buf_page
= state
->buffered_io_page
;
1091 buf_ioreq_t
*buf_req
= NULL
;
1099 memset(&req
, 0x00, sizeof(req
));
1100 req
.state
= STATE_IOREQ_READY
;
1102 req
.dir
= IOREQ_WRITE
;
1105 uint32_t rdptr
= buf_page
->read_pointer
, wrptr
;
1108 wrptr
= buf_page
->write_pointer
;
1110 if (rdptr
!= buf_page
->read_pointer
) {
1113 if (rdptr
== wrptr
) {
1116 buf_req
= &buf_page
->buf_ioreq
[rdptr
% IOREQ_BUFFER_SLOT_NUM
];
1117 req
.size
= 1U << buf_req
->size
;
1118 req
.addr
= buf_req
->addr
;
1119 req
.data
= buf_req
->data
;
1120 req
.type
= buf_req
->type
;
1122 qw
= (req
.size
== 8);
1124 if (rdptr
+ 1 == wrptr
) {
1125 hw_error("Incomplete quad word buffered ioreq");
1127 buf_req
= &buf_page
->buf_ioreq
[(rdptr
+ 1) %
1128 IOREQ_BUFFER_SLOT_NUM
];
1129 req
.data
|= ((uint64_t)buf_req
->data
) << 32;
1133 handle_ioreq(state
, &req
);
1135 /* Only req.data may get updated by handle_ioreq(), albeit even that
1136 * should not happen as such data would never make it to the guest (we
1137 * can only usefully see writes here after all).
1139 assert(req
.state
== STATE_IOREQ_READY
);
1140 assert(req
.count
== 1);
1141 assert(req
.dir
== IOREQ_WRITE
);
1142 assert(!req
.data_is_ptr
);
1144 qatomic_add(&buf_page
->read_pointer
, qw
+ 1);
1150 static void handle_buffered_io(void *opaque
)
1152 XenIOState
*state
= opaque
;
1154 if (handle_buffered_iopage(state
)) {
1155 timer_mod(state
->buffered_io_timer
,
1156 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
1158 timer_del(state
->buffered_io_timer
);
1159 xenevtchn_unmask(state
->xce_handle
, state
->bufioreq_local_port
);
1163 static void cpu_handle_ioreq(void *opaque
)
1165 XenIOState
*state
= opaque
;
1166 ioreq_t
*req
= cpu_get_ioreq(state
);
1168 handle_buffered_iopage(state
);
1170 ioreq_t copy
= *req
;
1173 handle_ioreq(state
, ©
);
1174 req
->data
= copy
.data
;
1176 if (req
->state
!= STATE_IOREQ_INPROCESS
) {
1177 fprintf(stderr
, "Badness in I/O request ... not in service?!: "
1178 "%x, ptr: %x, port: %"PRIx64
", "
1179 "data: %"PRIx64
", count: %u, size: %u, type: %u\n",
1180 req
->state
, req
->data_is_ptr
, req
->addr
,
1181 req
->data
, req
->count
, req
->size
, req
->type
);
1182 destroy_hvm_domain(false);
1186 xen_wmb(); /* Update ioreq contents /then/ update state. */
1189 * We do this before we send the response so that the tools
1190 * have the opportunity to pick up on the reset before the
1191 * guest resumes and does a hlt with interrupts disabled which
1192 * causes Xen to powerdown the domain.
1194 if (runstate_is_running()) {
1195 ShutdownCause request
;
1197 if (qemu_shutdown_requested_get()) {
1198 destroy_hvm_domain(false);
1200 request
= qemu_reset_requested_get();
1202 qemu_system_reset(request
);
1203 destroy_hvm_domain(true);
1207 req
->state
= STATE_IORESP_READY
;
1208 xenevtchn_notify(state
->xce_handle
,
1209 state
->ioreq_local_port
[state
->send_vcpu
]);
1213 static void xen_main_loop_prepare(XenIOState
*state
)
1217 if (state
->xce_handle
!= NULL
) {
1218 evtchn_fd
= xenevtchn_fd(state
->xce_handle
);
1221 state
->buffered_io_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, handle_buffered_io
,
1224 if (evtchn_fd
!= -1) {
1225 CPUState
*cpu_state
;
1227 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__
);
1228 CPU_FOREACH(cpu_state
) {
1229 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1230 __func__
, cpu_state
->cpu_index
, cpu_state
);
1231 state
->cpu_by_vcpu_id
[cpu_state
->cpu_index
] = cpu_state
;
1233 qemu_set_fd_handler(evtchn_fd
, cpu_handle_ioreq
, NULL
, state
);
1238 static void xen_hvm_change_state_handler(void *opaque
, int running
,
1241 XenIOState
*state
= opaque
;
1244 xen_main_loop_prepare(state
);
1247 xen_set_ioreq_server_state(xen_domid
,
1249 (rstate
== RUN_STATE_RUNNING
));
1252 static void xen_exit_notifier(Notifier
*n
, void *data
)
1254 XenIOState
*state
= container_of(n
, XenIOState
, exit
);
1256 xen_destroy_ioreq_server(xen_domid
, state
->ioservid
);
1258 xenevtchn_close(state
->xce_handle
);
1259 xs_daemon_close(state
->xenstore
);
1262 #ifdef XEN_COMPAT_PHYSMAP
1263 static void xen_read_physmap(XenIOState
*state
)
1265 XenPhysmap
*physmap
= NULL
;
1266 unsigned int len
, num
, i
;
1267 char path
[80], *value
= NULL
;
1268 char **entries
= NULL
;
1270 snprintf(path
, sizeof(path
),
1271 "/local/domain/0/device-model/%d/physmap", xen_domid
);
1272 entries
= xs_directory(state
->xenstore
, 0, path
, &num
);
1273 if (entries
== NULL
)
1276 for (i
= 0; i
< num
; i
++) {
1277 physmap
= g_malloc(sizeof (XenPhysmap
));
1278 physmap
->phys_offset
= strtoull(entries
[i
], NULL
, 16);
1279 snprintf(path
, sizeof(path
),
1280 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1281 xen_domid
, entries
[i
]);
1282 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1283 if (value
== NULL
) {
1287 physmap
->start_addr
= strtoull(value
, NULL
, 16);
1290 snprintf(path
, sizeof(path
),
1291 "/local/domain/0/device-model/%d/physmap/%s/size",
1292 xen_domid
, entries
[i
]);
1293 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1294 if (value
== NULL
) {
1298 physmap
->size
= strtoull(value
, NULL
, 16);
1301 snprintf(path
, sizeof(path
),
1302 "/local/domain/0/device-model/%d/physmap/%s/name",
1303 xen_domid
, entries
[i
]);
1304 physmap
->name
= xs_read(state
->xenstore
, 0, path
, &len
);
1306 QLIST_INSERT_HEAD(&xen_physmap
, physmap
, list
);
1311 static void xen_read_physmap(XenIOState
*state
)
1316 static void xen_wakeup_notifier(Notifier
*notifier
, void *data
)
1318 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 0);
1321 static int xen_map_ioreq_server(XenIOState
*state
)
1324 xenforeignmemory_resource_handle
*fres
;
1325 xen_pfn_t ioreq_pfn
;
1326 xen_pfn_t bufioreq_pfn
;
1327 evtchn_port_t bufioreq_evtchn
;
1331 * Attempt to map using the resource API and fall back to normal
1332 * foreign mapping if this is not supported.
1334 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq
!= 0);
1335 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1336 fres
= xenforeignmemory_map_resource(xen_fmem
, xen_domid
,
1337 XENMEM_resource_ioreq_server
,
1338 state
->ioservid
, 0, 2,
1340 PROT_READ
| PROT_WRITE
, 0);
1342 trace_xen_map_resource_ioreq(state
->ioservid
, addr
);
1343 state
->buffered_io_page
= addr
;
1344 state
->shared_page
= addr
+ TARGET_PAGE_SIZE
;
1345 } else if (errno
!= EOPNOTSUPP
) {
1346 error_report("failed to map ioreq server resources: error %d handle=%p",
1351 rc
= xen_get_ioreq_server_info(xen_domid
, state
->ioservid
,
1352 (state
->shared_page
== NULL
) ?
1354 (state
->buffered_io_page
== NULL
) ?
1355 &bufioreq_pfn
: NULL
,
1358 error_report("failed to get ioreq server info: error %d handle=%p",
1363 if (state
->shared_page
== NULL
) {
1364 DPRINTF("shared page at pfn %lx\n", ioreq_pfn
);
1366 state
->shared_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1367 PROT_READ
| PROT_WRITE
,
1368 1, &ioreq_pfn
, NULL
);
1369 if (state
->shared_page
== NULL
) {
1370 error_report("map shared IO page returned error %d handle=%p",
1375 if (state
->buffered_io_page
== NULL
) {
1376 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn
);
1378 state
->buffered_io_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1379 PROT_READ
| PROT_WRITE
,
1382 if (state
->buffered_io_page
== NULL
) {
1383 error_report("map buffered IO page returned error %d", errno
);
1388 if (state
->shared_page
== NULL
|| state
->buffered_io_page
== NULL
) {
1392 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn
);
1394 state
->bufioreq_remote_port
= bufioreq_evtchn
;
1399 void xen_hvm_init_pc(PCMachineState
*pcms
, MemoryRegion
**ram_memory
)
1401 MachineState
*ms
= MACHINE(pcms
);
1402 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1404 xen_pfn_t ioreq_pfn
;
1407 state
= g_malloc0(sizeof (XenIOState
));
1409 state
->xce_handle
= xenevtchn_open(NULL
, 0);
1410 if (state
->xce_handle
== NULL
) {
1411 perror("xen: event channel open");
1415 state
->xenstore
= xs_daemon_open();
1416 if (state
->xenstore
== NULL
) {
1417 perror("xen: xenstore open");
1421 xen_create_ioreq_server(xen_domid
, &state
->ioservid
);
1423 state
->exit
.notify
= xen_exit_notifier
;
1424 qemu_add_exit_notifier(&state
->exit
);
1426 state
->suspend
.notify
= xen_suspend_notifier
;
1427 qemu_register_suspend_notifier(&state
->suspend
);
1429 state
->wakeup
.notify
= xen_wakeup_notifier
;
1430 qemu_register_wakeup_notifier(&state
->wakeup
);
1433 * Register wake-up support in QMP query-current-machine API
1435 qemu_register_wakeup_support();
1437 rc
= xen_map_ioreq_server(state
);
1442 rc
= xen_get_vmport_regs_pfn(xen_xc
, xen_domid
, &ioreq_pfn
);
1444 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn
);
1445 state
->shared_vmport_page
=
1446 xenforeignmemory_map(xen_fmem
, xen_domid
, PROT_READ
|PROT_WRITE
,
1447 1, &ioreq_pfn
, NULL
);
1448 if (state
->shared_vmport_page
== NULL
) {
1449 error_report("map shared vmport IO page returned error %d handle=%p",
1453 } else if (rc
!= -ENOSYS
) {
1454 error_report("get vmport regs pfn returned error %d, rc=%d",
1459 /* Note: cpus is empty at this point in init */
1460 state
->cpu_by_vcpu_id
= g_malloc0(max_cpus
* sizeof(CPUState
*));
1462 rc
= xen_set_ioreq_server_state(xen_domid
, state
->ioservid
, true);
1464 error_report("failed to enable ioreq server info: error %d handle=%p",
1469 state
->ioreq_local_port
= g_malloc0(max_cpus
* sizeof (evtchn_port_t
));
1471 /* FIXME: how about if we overflow the page here? */
1472 for (i
= 0; i
< max_cpus
; i
++) {
1473 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1474 xen_vcpu_eport(state
->shared_page
, i
));
1476 error_report("shared evtchn %d bind error %d", i
, errno
);
1479 state
->ioreq_local_port
[i
] = rc
;
1482 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1483 state
->bufioreq_remote_port
);
1485 error_report("buffered evtchn bind error %d", errno
);
1488 state
->bufioreq_local_port
= rc
;
1490 /* Init RAM management */
1491 #ifdef XEN_COMPAT_PHYSMAP
1492 xen_map_cache_init(xen_phys_offset_to_gaddr
, state
);
1494 xen_map_cache_init(NULL
, state
);
1496 xen_ram_init(pcms
, ram_size
, ram_memory
);
1498 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler
, state
);
1500 state
->memory_listener
= xen_memory_listener
;
1501 memory_listener_register(&state
->memory_listener
, &address_space_memory
);
1502 state
->log_for_dirtybit
= NULL
;
1504 state
->io_listener
= xen_io_listener
;
1505 memory_listener_register(&state
->io_listener
, &address_space_io
);
1507 state
->device_listener
= xen_device_listener
;
1508 QLIST_INIT(&state
->dev_list
);
1509 device_listener_register(&state
->device_listener
);
1513 /* Initialize backend core & drivers */
1514 if (xen_be_init() != 0) {
1515 error_report("xen backend core setup failed");
1518 xen_be_register_common();
1520 QLIST_INIT(&xen_physmap
);
1521 xen_read_physmap(state
);
1523 /* Disable ACPI build because Xen handles it */
1524 pcms
->acpi_build_enabled
= false;
1529 error_report("xen hardware virtual machine initialisation failed");
1533 void destroy_hvm_domain(bool reboot
)
1535 xc_interface
*xc_handle
;
1539 unsigned int reason
= reboot
? SHUTDOWN_reboot
: SHUTDOWN_poweroff
;
1542 rc
= xendevicemodel_shutdown(xen_dmod
, xen_domid
, reason
);
1546 if (errno
!= ENOTTY
/* old Xen */) {
1547 perror("xendevicemodel_shutdown failed");
1549 /* well, try the old thing then */
1552 xc_handle
= xc_interface_open(0, 0, 0);
1553 if (xc_handle
== NULL
) {
1554 fprintf(stderr
, "Cannot acquire xenctrl handle\n");
1556 sts
= xc_domain_shutdown(xc_handle
, xen_domid
, reason
);
1558 fprintf(stderr
, "xc_domain_shutdown failed to issue %s, "
1559 "sts %d, %s\n", reboot
? "reboot" : "poweroff",
1560 sts
, strerror(errno
));
1562 fprintf(stderr
, "Issued domain %d %s\n", xen_domid
,
1563 reboot
? "reboot" : "poweroff");
1565 xc_interface_close(xc_handle
);
1569 void xen_register_framebuffer(MemoryRegion
*mr
)
1574 void xen_shutdown_fatal_error(const char *fmt
, ...)
1579 vfprintf(stderr
, fmt
, ap
);
1581 fprintf(stderr
, "Will destroy the domain.\n");
1582 /* destroy the domain */
1583 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR
);
1586 void xen_hvm_modified_memory(ram_addr_t start
, ram_addr_t length
)
1588 if (unlikely(xen_in_migration
)) {
1590 ram_addr_t start_pfn
, nb_pages
;
1592 start
= xen_phys_offset_to_gaddr(start
, length
);
1595 length
= TARGET_PAGE_SIZE
;
1597 start_pfn
= start
>> TARGET_PAGE_BITS
;
1598 nb_pages
= ((start
+ length
+ TARGET_PAGE_SIZE
- 1) >> TARGET_PAGE_BITS
)
1600 rc
= xen_modified_memory(xen_domid
, start_pfn
, nb_pages
);
1603 "%s failed for "RAM_ADDR_FMT
" ("RAM_ADDR_FMT
"): %i, %s\n",
1604 __func__
, start
, nb_pages
, errno
, strerror(errno
));
1609 void qmp_xen_set_global_dirty_log(bool enable
, Error
**errp
)
1612 memory_global_dirty_log_start();
1614 memory_global_dirty_log_stop();