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"
15 #include "hw/pci/pci.h"
16 #include "hw/i386/pc.h"
17 #include "hw/i386/apic-msidef.h"
18 #include "hw/xen/xen_common.h"
19 #include "hw/xen/xen_backend.h"
20 #include "qmp-commands.h"
22 #include "sysemu/char.h"
23 #include "qemu/error-report.h"
24 #include "qemu/range.h"
25 #include "sysemu/xen-mapcache.h"
27 #include "exec/address-spaces.h"
29 #include <xen/hvm/ioreq.h>
30 #include <xen/hvm/params.h>
31 #include <xen/hvm/e820.h>
33 //#define DEBUG_XEN_HVM
36 #define DPRINTF(fmt, ...) \
37 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...) \
43 static MemoryRegion ram_memory
, ram_640k
, ram_lo
, ram_hi
;
44 static MemoryRegion
*framebuffer
;
45 static bool xen_in_migration
;
47 /* Compatibility with older version */
49 /* This allows QEMU to build on a system that has Xen 4.5 or earlier
50 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
51 * needs to be included before this block and hw/xen/xen_common.h needs to
52 * be included before xen/hvm/ioreq.h
54 #ifndef IOREQ_TYPE_VMWARE_PORT
55 #define IOREQ_TYPE_VMWARE_PORT 3
63 typedef struct vmware_regs vmware_regs_t
;
65 struct shared_vmport_iopage
{
66 struct vmware_regs vcpu_vmport_regs
[1];
68 typedef struct shared_vmport_iopage shared_vmport_iopage_t
;
71 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
73 return shared_page
->vcpu_ioreq
[i
].vp_eport
;
75 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
77 return &shared_page
->vcpu_ioreq
[vcpu
];
80 #define BUFFER_IO_MAX_DELAY 100
82 typedef struct XenPhysmap
{
88 QLIST_ENTRY(XenPhysmap
) list
;
91 typedef struct XenIOState
{
93 shared_iopage_t
*shared_page
;
94 shared_vmport_iopage_t
*shared_vmport_page
;
95 buffered_iopage_t
*buffered_io_page
;
96 QEMUTimer
*buffered_io_timer
;
97 CPUState
**cpu_by_vcpu_id
;
98 /* the evtchn port for polling the notification, */
99 evtchn_port_t
*ioreq_local_port
;
100 /* evtchn local port for buffered io */
101 evtchn_port_t bufioreq_local_port
;
102 /* the evtchn fd for polling */
103 xenevtchn_handle
*xce_handle
;
104 /* which vcpu we are serving */
107 struct xs_handle
*xenstore
;
108 MemoryListener memory_listener
;
109 MemoryListener io_listener
;
110 DeviceListener device_listener
;
111 QLIST_HEAD(, XenPhysmap
) physmap
;
112 hwaddr free_phys_offset
;
113 const XenPhysmap
*log_for_dirtybit
;
120 /* Xen specific function for piix pci */
122 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
124 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
127 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
129 xc_hvm_set_pci_intx_level(xen_xc
, xen_domid
, 0, 0, irq_num
>> 2,
133 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
137 /* Scan for updates to PCI link routes (0x60-0x63). */
138 for (i
= 0; i
< len
; i
++) {
139 uint8_t v
= (val
>> (8 * i
)) & 0xff;
144 if (((address
+ i
) >= 0x60) && ((address
+ i
) <= 0x63)) {
145 xc_hvm_set_pci_link_route(xen_xc
, xen_domid
, address
+ i
- 0x60, v
);
150 int xen_is_pirq_msi(uint32_t msi_data
)
152 /* If vector is 0, the msi is remapped into a pirq, passed as
155 return ((msi_data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
) == 0;
158 void xen_hvm_inject_msi(uint64_t addr
, uint32_t data
)
160 xc_hvm_inject_msi(xen_xc
, xen_domid
, addr
, data
);
163 static void xen_suspend_notifier(Notifier
*notifier
, void *data
)
165 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 3);
168 /* Xen Interrupt Controller */
170 static void xen_set_irq(void *opaque
, int irq
, int level
)
172 xc_hvm_set_isa_irq_level(xen_xc
, xen_domid
, irq
, level
);
175 qemu_irq
*xen_interrupt_controller_init(void)
177 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
182 static void xen_ram_init(PCMachineState
*pcms
,
183 ram_addr_t ram_size
, MemoryRegion
**ram_memory_p
)
185 MemoryRegion
*sysmem
= get_system_memory();
186 ram_addr_t block_len
;
187 uint64_t user_lowmem
= object_property_get_int(qdev_get_machine(),
188 PC_MACHINE_MAX_RAM_BELOW_4G
,
191 /* Handle the machine opt max-ram-below-4g. It is basically doing
192 * min(xen limit, user limit).
194 if (HVM_BELOW_4G_RAM_END
<= user_lowmem
) {
195 user_lowmem
= HVM_BELOW_4G_RAM_END
;
198 if (ram_size
>= user_lowmem
) {
199 pcms
->above_4g_mem_size
= ram_size
- user_lowmem
;
200 pcms
->below_4g_mem_size
= user_lowmem
;
202 pcms
->above_4g_mem_size
= 0;
203 pcms
->below_4g_mem_size
= ram_size
;
205 if (!pcms
->above_4g_mem_size
) {
206 block_len
= ram_size
;
209 * Xen does not allocate the memory continuously, it keeps a
210 * hole of the size computed above or passed in.
212 block_len
= (1ULL << 32) + pcms
->above_4g_mem_size
;
214 memory_region_init_ram(&ram_memory
, NULL
, "xen.ram", block_len
,
216 *ram_memory_p
= &ram_memory
;
217 vmstate_register_ram_global(&ram_memory
);
219 memory_region_init_alias(&ram_640k
, NULL
, "xen.ram.640k",
220 &ram_memory
, 0, 0xa0000);
221 memory_region_add_subregion(sysmem
, 0, &ram_640k
);
222 /* Skip of the VGA IO memory space, it will be registered later by the VGA
225 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
226 * the Options ROM, so it is registered here as RAM.
228 memory_region_init_alias(&ram_lo
, NULL
, "xen.ram.lo",
229 &ram_memory
, 0xc0000,
230 pcms
->below_4g_mem_size
- 0xc0000);
231 memory_region_add_subregion(sysmem
, 0xc0000, &ram_lo
);
232 if (pcms
->above_4g_mem_size
> 0) {
233 memory_region_init_alias(&ram_hi
, NULL
, "xen.ram.hi",
234 &ram_memory
, 0x100000000ULL
,
235 pcms
->above_4g_mem_size
);
236 memory_region_add_subregion(sysmem
, 0x100000000ULL
, &ram_hi
);
240 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
, MemoryRegion
*mr
,
243 unsigned long nr_pfn
;
247 if (runstate_check(RUN_STATE_INMIGRATE
)) {
248 /* RAM already populated in Xen */
249 fprintf(stderr
, "%s: do not alloc "RAM_ADDR_FMT
250 " bytes of ram at "RAM_ADDR_FMT
" when runstate is INMIGRATE\n",
251 __func__
, size
, ram_addr
);
255 if (mr
== &ram_memory
) {
259 trace_xen_ram_alloc(ram_addr
, size
);
261 nr_pfn
= size
>> TARGET_PAGE_BITS
;
262 pfn_list
= g_malloc(sizeof (*pfn_list
) * nr_pfn
);
264 for (i
= 0; i
< nr_pfn
; i
++) {
265 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
268 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
269 error_setg(errp
, "xen: failed to populate ram at " RAM_ADDR_FMT
,
276 static XenPhysmap
*get_physmapping(XenIOState
*state
,
277 hwaddr start_addr
, ram_addr_t size
)
279 XenPhysmap
*physmap
= NULL
;
281 start_addr
&= TARGET_PAGE_MASK
;
283 QLIST_FOREACH(physmap
, &state
->physmap
, list
) {
284 if (range_covers_byte(physmap
->start_addr
, physmap
->size
, start_addr
)) {
291 static hwaddr
xen_phys_offset_to_gaddr(hwaddr start_addr
,
292 ram_addr_t size
, void *opaque
)
294 hwaddr addr
= start_addr
& TARGET_PAGE_MASK
;
295 XenIOState
*xen_io_state
= opaque
;
296 XenPhysmap
*physmap
= NULL
;
298 QLIST_FOREACH(physmap
, &xen_io_state
->physmap
, list
) {
299 if (range_covers_byte(physmap
->phys_offset
, physmap
->size
, addr
)) {
300 return physmap
->start_addr
;
307 static int xen_add_to_physmap(XenIOState
*state
,
311 hwaddr offset_within_region
)
315 XenPhysmap
*physmap
= NULL
;
316 hwaddr pfn
, start_gpfn
;
317 hwaddr phys_offset
= memory_region_get_ram_addr(mr
);
318 char path
[80], value
[17];
321 if (get_physmapping(state
, start_addr
, size
)) {
328 /* Xen can only handle a single dirty log region for now and we want
329 * the linear framebuffer to be that region.
330 * Avoid tracking any regions that is not videoram and avoid tracking
331 * the legacy vga region. */
332 if (mr
== framebuffer
&& start_addr
> 0xbffff) {
338 DPRINTF("mapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
339 start_addr
, start_addr
+ size
);
341 pfn
= phys_offset
>> TARGET_PAGE_BITS
;
342 start_gpfn
= start_addr
>> TARGET_PAGE_BITS
;
343 for (i
= 0; i
< size
>> TARGET_PAGE_BITS
; i
++) {
344 unsigned long idx
= pfn
+ i
;
345 xen_pfn_t gpfn
= start_gpfn
+ i
;
347 rc
= xen_xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
349 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
350 PRI_xen_pfn
" failed: %d (errno: %d)\n", idx
, gpfn
, rc
, errno
);
355 mr_name
= memory_region_name(mr
);
357 physmap
= g_malloc(sizeof (XenPhysmap
));
359 physmap
->start_addr
= start_addr
;
360 physmap
->size
= size
;
361 physmap
->name
= mr_name
;
362 physmap
->phys_offset
= phys_offset
;
364 QLIST_INSERT_HEAD(&state
->physmap
, physmap
, list
);
366 xc_domain_pin_memory_cacheattr(xen_xc
, xen_domid
,
367 start_addr
>> TARGET_PAGE_BITS
,
368 (start_addr
+ size
- 1) >> TARGET_PAGE_BITS
,
369 XEN_DOMCTL_MEM_CACHEATTR_WB
);
371 snprintf(path
, sizeof(path
),
372 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/start_addr",
373 xen_domid
, (uint64_t)phys_offset
);
374 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)start_addr
);
375 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
378 snprintf(path
, sizeof(path
),
379 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/size",
380 xen_domid
, (uint64_t)phys_offset
);
381 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)size
);
382 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
386 snprintf(path
, sizeof(path
),
387 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/name",
388 xen_domid
, (uint64_t)phys_offset
);
389 if (!xs_write(state
->xenstore
, 0, path
, mr_name
, strlen(mr_name
))) {
397 static int xen_remove_from_physmap(XenIOState
*state
,
403 XenPhysmap
*physmap
= NULL
;
404 hwaddr phys_offset
= 0;
406 physmap
= get_physmapping(state
, start_addr
, size
);
407 if (physmap
== NULL
) {
411 phys_offset
= physmap
->phys_offset
;
412 size
= physmap
->size
;
414 DPRINTF("unmapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
", at "
415 "%"HWADDR_PRIx
"\n", start_addr
, start_addr
+ size
, phys_offset
);
417 size
>>= TARGET_PAGE_BITS
;
418 start_addr
>>= TARGET_PAGE_BITS
;
419 phys_offset
>>= TARGET_PAGE_BITS
;
420 for (i
= 0; i
< size
; i
++) {
421 xen_pfn_t idx
= start_addr
+ i
;
422 xen_pfn_t gpfn
= phys_offset
+ i
;
424 rc
= xen_xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
426 fprintf(stderr
, "add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
427 PRI_xen_pfn
" failed: %d (errno: %d)\n", idx
, gpfn
, rc
, errno
);
432 QLIST_REMOVE(physmap
, list
);
433 if (state
->log_for_dirtybit
== physmap
) {
434 state
->log_for_dirtybit
= NULL
;
441 static void xen_set_memory(struct MemoryListener
*listener
,
442 MemoryRegionSection
*section
,
445 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
446 hwaddr start_addr
= section
->offset_within_address_space
;
447 ram_addr_t size
= int128_get64(section
->size
);
448 bool log_dirty
= memory_region_is_logging(section
->mr
, DIRTY_MEMORY_VGA
);
449 hvmmem_type_t mem_type
;
451 if (section
->mr
== &ram_memory
) {
455 xen_map_memory_section(xen_xc
, xen_domid
, state
->ioservid
,
458 xen_unmap_memory_section(xen_xc
, xen_domid
, state
->ioservid
,
463 if (!memory_region_is_ram(section
->mr
)) {
467 if (log_dirty
!= add
) {
471 trace_xen_client_set_memory(start_addr
, size
, log_dirty
);
473 start_addr
&= TARGET_PAGE_MASK
;
474 size
= TARGET_PAGE_ALIGN(size
);
477 if (!memory_region_is_rom(section
->mr
)) {
478 xen_add_to_physmap(state
, start_addr
, size
,
479 section
->mr
, section
->offset_within_region
);
481 mem_type
= HVMMEM_ram_ro
;
482 if (xc_hvm_set_mem_type(xen_xc
, xen_domid
, mem_type
,
483 start_addr
>> TARGET_PAGE_BITS
,
484 size
>> TARGET_PAGE_BITS
)) {
485 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx
"\n",
490 if (xen_remove_from_physmap(state
, start_addr
, size
) < 0) {
491 DPRINTF("physmapping does not exist at "TARGET_FMT_plx
"\n", start_addr
);
496 static void xen_region_add(MemoryListener
*listener
,
497 MemoryRegionSection
*section
)
499 memory_region_ref(section
->mr
);
500 xen_set_memory(listener
, section
, true);
503 static void xen_region_del(MemoryListener
*listener
,
504 MemoryRegionSection
*section
)
506 xen_set_memory(listener
, section
, false);
507 memory_region_unref(section
->mr
);
510 static void xen_io_add(MemoryListener
*listener
,
511 MemoryRegionSection
*section
)
513 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
514 MemoryRegion
*mr
= section
->mr
;
516 if (mr
->ops
== &unassigned_io_ops
) {
520 memory_region_ref(mr
);
522 xen_map_io_section(xen_xc
, xen_domid
, state
->ioservid
, section
);
525 static void xen_io_del(MemoryListener
*listener
,
526 MemoryRegionSection
*section
)
528 XenIOState
*state
= container_of(listener
, XenIOState
, io_listener
);
529 MemoryRegion
*mr
= section
->mr
;
531 if (mr
->ops
== &unassigned_io_ops
) {
535 xen_unmap_io_section(xen_xc
, xen_domid
, state
->ioservid
, section
);
537 memory_region_unref(mr
);
540 static void xen_device_realize(DeviceListener
*listener
,
543 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
545 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
546 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
548 xen_map_pcidev(xen_xc
, xen_domid
, state
->ioservid
, pci_dev
);
552 static void xen_device_unrealize(DeviceListener
*listener
,
555 XenIOState
*state
= container_of(listener
, XenIOState
, device_listener
);
557 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
558 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
560 xen_unmap_pcidev(xen_xc
, xen_domid
, state
->ioservid
, pci_dev
);
564 static void xen_sync_dirty_bitmap(XenIOState
*state
,
568 hwaddr npages
= size
>> TARGET_PAGE_BITS
;
569 const int width
= sizeof(unsigned long) * 8;
570 unsigned long bitmap
[DIV_ROUND_UP(npages
, width
)];
572 const XenPhysmap
*physmap
= NULL
;
574 physmap
= get_physmapping(state
, start_addr
, size
);
575 if (physmap
== NULL
) {
580 if (state
->log_for_dirtybit
== NULL
) {
581 state
->log_for_dirtybit
= physmap
;
582 } else if (state
->log_for_dirtybit
!= physmap
) {
583 /* Only one range for dirty bitmap can be tracked. */
587 rc
= xc_hvm_track_dirty_vram(xen_xc
, xen_domid
,
588 start_addr
>> TARGET_PAGE_BITS
, npages
,
592 #define ENODATA ENOENT
594 if (errno
== ENODATA
) {
595 memory_region_set_dirty(framebuffer
, 0, size
);
596 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
597 ", 0x" TARGET_FMT_plx
"): %s\n",
598 start_addr
, start_addr
+ size
, strerror(errno
));
603 for (i
= 0; i
< ARRAY_SIZE(bitmap
); i
++) {
604 unsigned long map
= bitmap
[i
];
608 memory_region_set_dirty(framebuffer
,
609 (i
* width
+ j
) * TARGET_PAGE_SIZE
,
615 static void xen_log_start(MemoryListener
*listener
,
616 MemoryRegionSection
*section
,
619 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
621 if (new & ~old
& (1 << DIRTY_MEMORY_VGA
)) {
622 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
623 int128_get64(section
->size
));
627 static void xen_log_stop(MemoryListener
*listener
, MemoryRegionSection
*section
,
630 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
632 if (old
& ~new & (1 << DIRTY_MEMORY_VGA
)) {
633 state
->log_for_dirtybit
= NULL
;
634 /* Disable dirty bit tracking */
635 xc_hvm_track_dirty_vram(xen_xc
, xen_domid
, 0, 0, NULL
);
639 static void xen_log_sync(MemoryListener
*listener
, MemoryRegionSection
*section
)
641 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
643 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
644 int128_get64(section
->size
));
647 static void xen_log_global_start(MemoryListener
*listener
)
650 xen_in_migration
= true;
654 static void xen_log_global_stop(MemoryListener
*listener
)
656 xen_in_migration
= false;
659 static MemoryListener xen_memory_listener
= {
660 .region_add
= xen_region_add
,
661 .region_del
= xen_region_del
,
662 .log_start
= xen_log_start
,
663 .log_stop
= xen_log_stop
,
664 .log_sync
= xen_log_sync
,
665 .log_global_start
= xen_log_global_start
,
666 .log_global_stop
= xen_log_global_stop
,
670 static MemoryListener xen_io_listener
= {
671 .region_add
= xen_io_add
,
672 .region_del
= xen_io_del
,
676 static DeviceListener xen_device_listener
= {
677 .realize
= xen_device_realize
,
678 .unrealize
= xen_device_unrealize
,
681 /* get the ioreq packets from share mem */
682 static ioreq_t
*cpu_get_ioreq_from_shared_memory(XenIOState
*state
, int vcpu
)
684 ioreq_t
*req
= xen_vcpu_ioreq(state
->shared_page
, vcpu
);
686 if (req
->state
!= STATE_IOREQ_READY
) {
687 DPRINTF("I/O request not ready: "
688 "%x, ptr: %x, port: %"PRIx64
", "
689 "data: %"PRIx64
", count: %u, size: %u\n",
690 req
->state
, req
->data_is_ptr
, req
->addr
,
691 req
->data
, req
->count
, req
->size
);
695 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
697 req
->state
= STATE_IOREQ_INPROCESS
;
701 /* use poll to get the port notification */
702 /* ioreq_vec--out,the */
703 /* retval--the number of ioreq packet */
704 static ioreq_t
*cpu_get_ioreq(XenIOState
*state
)
709 port
= xenevtchn_pending(state
->xce_handle
);
710 if (port
== state
->bufioreq_local_port
) {
711 timer_mod(state
->buffered_io_timer
,
712 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
717 for (i
= 0; i
< max_cpus
; i
++) {
718 if (state
->ioreq_local_port
[i
] == port
) {
724 hw_error("Fatal error while trying to get io event!\n");
727 /* unmask the wanted port again */
728 xenevtchn_unmask(state
->xce_handle
, port
);
730 /* get the io packet from shared memory */
731 state
->send_vcpu
= i
;
732 return cpu_get_ioreq_from_shared_memory(state
, i
);
735 /* read error or read nothing */
739 static uint32_t do_inp(uint32_t addr
, unsigned long size
)
743 return cpu_inb(addr
);
745 return cpu_inw(addr
);
747 return cpu_inl(addr
);
749 hw_error("inp: bad size: %04x %lx", addr
, size
);
753 static void do_outp(uint32_t addr
,
754 unsigned long size
, uint32_t val
)
758 return cpu_outb(addr
, val
);
760 return cpu_outw(addr
, val
);
762 return cpu_outl(addr
, val
);
764 hw_error("outp: bad size: %04x %lx", addr
, size
);
769 * Helper functions which read/write an object from/to physical guest
770 * memory, as part of the implementation of an ioreq.
773 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
774 * val, req->size, 0/1)
775 * except without the integer overflow problems.
777 static void rw_phys_req_item(hwaddr addr
,
778 ioreq_t
*req
, uint32_t i
, void *val
, int rw
)
780 /* Do everything unsigned so overflow just results in a truncated result
781 * and accesses to undesired parts of guest memory, which is up
783 hwaddr offset
= (hwaddr
)req
->size
* i
;
789 cpu_physical_memory_rw(addr
, val
, req
->size
, rw
);
792 static inline void read_phys_req_item(hwaddr addr
,
793 ioreq_t
*req
, uint32_t i
, void *val
)
795 rw_phys_req_item(addr
, req
, i
, val
, 0);
797 static inline void write_phys_req_item(hwaddr addr
,
798 ioreq_t
*req
, uint32_t i
, void *val
)
800 rw_phys_req_item(addr
, req
, i
, val
, 1);
804 static void cpu_ioreq_pio(ioreq_t
*req
)
808 trace_cpu_ioreq_pio(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
809 req
->data
, req
->count
, req
->size
);
811 if (req
->dir
== IOREQ_READ
) {
812 if (!req
->data_is_ptr
) {
813 req
->data
= do_inp(req
->addr
, req
->size
);
814 trace_cpu_ioreq_pio_read_reg(req
, req
->data
, req
->addr
,
819 for (i
= 0; i
< req
->count
; i
++) {
820 tmp
= do_inp(req
->addr
, req
->size
);
821 write_phys_req_item(req
->data
, req
, i
, &tmp
);
824 } else if (req
->dir
== IOREQ_WRITE
) {
825 if (!req
->data_is_ptr
) {
826 trace_cpu_ioreq_pio_write_reg(req
, req
->data
, req
->addr
,
828 do_outp(req
->addr
, req
->size
, req
->data
);
830 for (i
= 0; i
< req
->count
; i
++) {
833 read_phys_req_item(req
->data
, req
, i
, &tmp
);
834 do_outp(req
->addr
, req
->size
, tmp
);
840 static void cpu_ioreq_move(ioreq_t
*req
)
844 trace_cpu_ioreq_move(req
, req
->dir
, req
->df
, req
->data_is_ptr
, req
->addr
,
845 req
->data
, req
->count
, req
->size
);
847 if (!req
->data_is_ptr
) {
848 if (req
->dir
== IOREQ_READ
) {
849 for (i
= 0; i
< req
->count
; i
++) {
850 read_phys_req_item(req
->addr
, req
, i
, &req
->data
);
852 } else if (req
->dir
== IOREQ_WRITE
) {
853 for (i
= 0; i
< req
->count
; i
++) {
854 write_phys_req_item(req
->addr
, req
, i
, &req
->data
);
860 if (req
->dir
== IOREQ_READ
) {
861 for (i
= 0; i
< req
->count
; i
++) {
862 read_phys_req_item(req
->addr
, req
, i
, &tmp
);
863 write_phys_req_item(req
->data
, req
, i
, &tmp
);
865 } else if (req
->dir
== IOREQ_WRITE
) {
866 for (i
= 0; i
< req
->count
; i
++) {
867 read_phys_req_item(req
->data
, req
, i
, &tmp
);
868 write_phys_req_item(req
->addr
, req
, i
, &tmp
);
874 static void regs_to_cpu(vmware_regs_t
*vmport_regs
, ioreq_t
*req
)
879 cpu
= X86_CPU(current_cpu
);
881 env
->regs
[R_EAX
] = req
->data
;
882 env
->regs
[R_EBX
] = vmport_regs
->ebx
;
883 env
->regs
[R_ECX
] = vmport_regs
->ecx
;
884 env
->regs
[R_EDX
] = vmport_regs
->edx
;
885 env
->regs
[R_ESI
] = vmport_regs
->esi
;
886 env
->regs
[R_EDI
] = vmport_regs
->edi
;
889 static void regs_from_cpu(vmware_regs_t
*vmport_regs
)
891 X86CPU
*cpu
= X86_CPU(current_cpu
);
892 CPUX86State
*env
= &cpu
->env
;
894 vmport_regs
->ebx
= env
->regs
[R_EBX
];
895 vmport_regs
->ecx
= env
->regs
[R_ECX
];
896 vmport_regs
->edx
= env
->regs
[R_EDX
];
897 vmport_regs
->esi
= env
->regs
[R_ESI
];
898 vmport_regs
->edi
= env
->regs
[R_EDI
];
901 static void handle_vmport_ioreq(XenIOState
*state
, ioreq_t
*req
)
903 vmware_regs_t
*vmport_regs
;
905 assert(state
->shared_vmport_page
);
907 &state
->shared_vmport_page
->vcpu_vmport_regs
[state
->send_vcpu
];
908 QEMU_BUILD_BUG_ON(sizeof(*req
) < sizeof(*vmport_regs
));
910 current_cpu
= state
->cpu_by_vcpu_id
[state
->send_vcpu
];
911 regs_to_cpu(vmport_regs
, req
);
913 regs_from_cpu(vmport_regs
);
917 static void handle_ioreq(XenIOState
*state
, ioreq_t
*req
)
919 trace_handle_ioreq(req
, req
->type
, req
->dir
, req
->df
, req
->data_is_ptr
,
920 req
->addr
, req
->data
, req
->count
, req
->size
);
922 if (!req
->data_is_ptr
&& (req
->dir
== IOREQ_WRITE
) &&
923 (req
->size
< sizeof (target_ulong
))) {
924 req
->data
&= ((target_ulong
) 1 << (8 * req
->size
)) - 1;
927 if (req
->dir
== IOREQ_WRITE
)
928 trace_handle_ioreq_write(req
, req
->type
, req
->df
, req
->data_is_ptr
,
929 req
->addr
, req
->data
, req
->count
, req
->size
);
935 case IOREQ_TYPE_COPY
:
938 case IOREQ_TYPE_VMWARE_PORT
:
939 handle_vmport_ioreq(state
, req
);
941 case IOREQ_TYPE_TIMEOFFSET
:
943 case IOREQ_TYPE_INVALIDATE
:
944 xen_invalidate_map_cache();
946 case IOREQ_TYPE_PCI_CONFIG
: {
947 uint32_t sbdf
= req
->addr
>> 32;
950 /* Fake a write to port 0xCF8 so that
951 * the config space access will target the
952 * correct device model.
955 ((req
->addr
& 0x0f00) << 16) |
956 ((sbdf
& 0xffff) << 8) |
958 do_outp(0xcf8, 4, val
);
960 /* Now issue the config space access via
963 req
->addr
= 0xcfc | (req
->addr
& 0x03);
968 hw_error("Invalid ioreq type 0x%x\n", req
->type
);
970 if (req
->dir
== IOREQ_READ
) {
971 trace_handle_ioreq_read(req
, req
->type
, req
->df
, req
->data_is_ptr
,
972 req
->addr
, req
->data
, req
->count
, req
->size
);
976 static int handle_buffered_iopage(XenIOState
*state
)
978 buffered_iopage_t
*buf_page
= state
->buffered_io_page
;
979 buf_ioreq_t
*buf_req
= NULL
;
987 memset(&req
, 0x00, sizeof(req
));
990 uint32_t rdptr
= buf_page
->read_pointer
, wrptr
;
993 wrptr
= buf_page
->write_pointer
;
995 if (rdptr
!= buf_page
->read_pointer
) {
998 if (rdptr
== wrptr
) {
1001 buf_req
= &buf_page
->buf_ioreq
[rdptr
% IOREQ_BUFFER_SLOT_NUM
];
1002 req
.size
= 1UL << buf_req
->size
;
1004 req
.addr
= buf_req
->addr
;
1005 req
.data
= buf_req
->data
;
1006 req
.state
= STATE_IOREQ_READY
;
1007 req
.dir
= buf_req
->dir
;
1009 req
.type
= buf_req
->type
;
1010 req
.data_is_ptr
= 0;
1011 qw
= (req
.size
== 8);
1013 buf_req
= &buf_page
->buf_ioreq
[(rdptr
+ 1) %
1014 IOREQ_BUFFER_SLOT_NUM
];
1015 req
.data
|= ((uint64_t)buf_req
->data
) << 32;
1018 handle_ioreq(state
, &req
);
1020 atomic_add(&buf_page
->read_pointer
, qw
+ 1);
1026 static void handle_buffered_io(void *opaque
)
1028 XenIOState
*state
= opaque
;
1030 if (handle_buffered_iopage(state
)) {
1031 timer_mod(state
->buffered_io_timer
,
1032 BUFFER_IO_MAX_DELAY
+ qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
1034 timer_del(state
->buffered_io_timer
);
1035 xenevtchn_unmask(state
->xce_handle
, state
->bufioreq_local_port
);
1039 static void cpu_handle_ioreq(void *opaque
)
1041 XenIOState
*state
= opaque
;
1042 ioreq_t
*req
= cpu_get_ioreq(state
);
1044 handle_buffered_iopage(state
);
1046 handle_ioreq(state
, req
);
1048 if (req
->state
!= STATE_IOREQ_INPROCESS
) {
1049 fprintf(stderr
, "Badness in I/O request ... not in service?!: "
1050 "%x, ptr: %x, port: %"PRIx64
", "
1051 "data: %"PRIx64
", count: %u, size: %u, type: %u\n",
1052 req
->state
, req
->data_is_ptr
, req
->addr
,
1053 req
->data
, req
->count
, req
->size
, req
->type
);
1054 destroy_hvm_domain(false);
1058 xen_wmb(); /* Update ioreq contents /then/ update state. */
1061 * We do this before we send the response so that the tools
1062 * have the opportunity to pick up on the reset before the
1063 * guest resumes and does a hlt with interrupts disabled which
1064 * causes Xen to powerdown the domain.
1066 if (runstate_is_running()) {
1067 if (qemu_shutdown_requested_get()) {
1068 destroy_hvm_domain(false);
1070 if (qemu_reset_requested_get()) {
1071 qemu_system_reset(VMRESET_REPORT
);
1072 destroy_hvm_domain(true);
1076 req
->state
= STATE_IORESP_READY
;
1077 xenevtchn_notify(state
->xce_handle
,
1078 state
->ioreq_local_port
[state
->send_vcpu
]);
1082 static void xen_main_loop_prepare(XenIOState
*state
)
1086 if (state
->xce_handle
!= NULL
) {
1087 evtchn_fd
= xenevtchn_fd(state
->xce_handle
);
1090 state
->buffered_io_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, handle_buffered_io
,
1093 if (evtchn_fd
!= -1) {
1094 CPUState
*cpu_state
;
1096 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__
);
1097 CPU_FOREACH(cpu_state
) {
1098 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1099 __func__
, cpu_state
->cpu_index
, cpu_state
);
1100 state
->cpu_by_vcpu_id
[cpu_state
->cpu_index
] = cpu_state
;
1102 qemu_set_fd_handler(evtchn_fd
, cpu_handle_ioreq
, NULL
, state
);
1107 static void xen_hvm_change_state_handler(void *opaque
, int running
,
1110 XenIOState
*state
= opaque
;
1113 xen_main_loop_prepare(state
);
1116 xen_set_ioreq_server_state(xen_xc
, xen_domid
,
1118 (rstate
== RUN_STATE_RUNNING
));
1121 static void xen_exit_notifier(Notifier
*n
, void *data
)
1123 XenIOState
*state
= container_of(n
, XenIOState
, exit
);
1125 xenevtchn_close(state
->xce_handle
);
1126 xs_daemon_close(state
->xenstore
);
1129 static void xen_read_physmap(XenIOState
*state
)
1131 XenPhysmap
*physmap
= NULL
;
1132 unsigned int len
, num
, i
;
1133 char path
[80], *value
= NULL
;
1134 char **entries
= NULL
;
1136 snprintf(path
, sizeof(path
),
1137 "/local/domain/0/device-model/%d/physmap", xen_domid
);
1138 entries
= xs_directory(state
->xenstore
, 0, path
, &num
);
1139 if (entries
== NULL
)
1142 for (i
= 0; i
< num
; i
++) {
1143 physmap
= g_malloc(sizeof (XenPhysmap
));
1144 physmap
->phys_offset
= strtoull(entries
[i
], NULL
, 16);
1145 snprintf(path
, sizeof(path
),
1146 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1147 xen_domid
, entries
[i
]);
1148 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1149 if (value
== NULL
) {
1153 physmap
->start_addr
= strtoull(value
, NULL
, 16);
1156 snprintf(path
, sizeof(path
),
1157 "/local/domain/0/device-model/%d/physmap/%s/size",
1158 xen_domid
, entries
[i
]);
1159 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1160 if (value
== NULL
) {
1164 physmap
->size
= strtoull(value
, NULL
, 16);
1167 snprintf(path
, sizeof(path
),
1168 "/local/domain/0/device-model/%d/physmap/%s/name",
1169 xen_domid
, entries
[i
]);
1170 physmap
->name
= xs_read(state
->xenstore
, 0, path
, &len
);
1172 QLIST_INSERT_HEAD(&state
->physmap
, physmap
, list
);
1177 static void xen_wakeup_notifier(Notifier
*notifier
, void *data
)
1179 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 0);
1182 void xen_hvm_init(PCMachineState
*pcms
, MemoryRegion
**ram_memory
)
1185 xen_pfn_t ioreq_pfn
;
1186 xen_pfn_t bufioreq_pfn
;
1187 evtchn_port_t bufioreq_evtchn
;
1190 state
= g_malloc0(sizeof (XenIOState
));
1192 state
->xce_handle
= xenevtchn_open(NULL
, 0);
1193 if (state
->xce_handle
== NULL
) {
1194 perror("xen: event channel open");
1198 state
->xenstore
= xs_daemon_open();
1199 if (state
->xenstore
== NULL
) {
1200 perror("xen: xenstore open");
1204 rc
= xen_create_ioreq_server(xen_xc
, xen_domid
, &state
->ioservid
);
1206 perror("xen: ioreq server create");
1210 state
->exit
.notify
= xen_exit_notifier
;
1211 qemu_add_exit_notifier(&state
->exit
);
1213 state
->suspend
.notify
= xen_suspend_notifier
;
1214 qemu_register_suspend_notifier(&state
->suspend
);
1216 state
->wakeup
.notify
= xen_wakeup_notifier
;
1217 qemu_register_wakeup_notifier(&state
->wakeup
);
1219 rc
= xen_get_ioreq_server_info(xen_xc
, xen_domid
, state
->ioservid
,
1220 &ioreq_pfn
, &bufioreq_pfn
,
1223 error_report("failed to get ioreq server info: error %d handle=%p",
1228 DPRINTF("shared page at pfn %lx\n", ioreq_pfn
);
1229 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn
);
1230 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn
);
1232 state
->shared_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1233 PROT_READ
|PROT_WRITE
,
1234 1, &ioreq_pfn
, NULL
);
1235 if (state
->shared_page
== NULL
) {
1236 error_report("map shared IO page returned error %d handle=%p",
1241 rc
= xen_get_vmport_regs_pfn(xen_xc
, xen_domid
, &ioreq_pfn
);
1243 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn
);
1244 state
->shared_vmport_page
=
1245 xenforeignmemory_map(xen_fmem
, xen_domid
, PROT_READ
|PROT_WRITE
,
1246 1, &ioreq_pfn
, NULL
);
1247 if (state
->shared_vmport_page
== NULL
) {
1248 error_report("map shared vmport IO page returned error %d handle=%p",
1252 } else if (rc
!= -ENOSYS
) {
1253 error_report("get vmport regs pfn returned error %d, rc=%d",
1258 state
->buffered_io_page
= xenforeignmemory_map(xen_fmem
, xen_domid
,
1259 PROT_READ
|PROT_WRITE
,
1260 1, &bufioreq_pfn
, NULL
);
1261 if (state
->buffered_io_page
== NULL
) {
1262 error_report("map buffered IO page returned error %d", errno
);
1266 /* Note: cpus is empty at this point in init */
1267 state
->cpu_by_vcpu_id
= g_malloc0(max_cpus
* sizeof(CPUState
*));
1269 rc
= xen_set_ioreq_server_state(xen_xc
, xen_domid
, state
->ioservid
, true);
1271 error_report("failed to enable ioreq server info: error %d handle=%p",
1276 state
->ioreq_local_port
= g_malloc0(max_cpus
* sizeof (evtchn_port_t
));
1278 /* FIXME: how about if we overflow the page here? */
1279 for (i
= 0; i
< max_cpus
; i
++) {
1280 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1281 xen_vcpu_eport(state
->shared_page
, i
));
1283 error_report("shared evtchn %d bind error %d", i
, errno
);
1286 state
->ioreq_local_port
[i
] = rc
;
1289 rc
= xenevtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1292 error_report("buffered evtchn bind error %d", errno
);
1295 state
->bufioreq_local_port
= rc
;
1297 /* Init RAM management */
1298 xen_map_cache_init(xen_phys_offset_to_gaddr
, state
);
1299 xen_ram_init(pcms
, ram_size
, ram_memory
);
1301 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler
, state
);
1303 state
->memory_listener
= xen_memory_listener
;
1304 QLIST_INIT(&state
->physmap
);
1305 memory_listener_register(&state
->memory_listener
, &address_space_memory
);
1306 state
->log_for_dirtybit
= NULL
;
1308 state
->io_listener
= xen_io_listener
;
1309 memory_listener_register(&state
->io_listener
, &address_space_io
);
1311 state
->device_listener
= xen_device_listener
;
1312 device_listener_register(&state
->device_listener
);
1314 /* Initialize backend core & drivers */
1315 if (xen_be_init() != 0) {
1316 error_report("xen backend core setup failed");
1319 xen_be_register("console", &xen_console_ops
);
1320 xen_be_register("vkbd", &xen_kbdmouse_ops
);
1321 xen_be_register("qdisk", &xen_blkdev_ops
);
1322 xen_read_physmap(state
);
1326 error_report("xen hardware virtual machine initialisation failed");
1330 void destroy_hvm_domain(bool reboot
)
1332 xc_interface
*xc_handle
;
1335 xc_handle
= xc_interface_open(0, 0, 0);
1336 if (xc_handle
== NULL
) {
1337 fprintf(stderr
, "Cannot acquire xenctrl handle\n");
1339 sts
= xc_domain_shutdown(xc_handle
, xen_domid
,
1340 reboot
? SHUTDOWN_reboot
: SHUTDOWN_poweroff
);
1342 fprintf(stderr
, "xc_domain_shutdown failed to issue %s, "
1343 "sts %d, %s\n", reboot
? "reboot" : "poweroff",
1344 sts
, strerror(errno
));
1346 fprintf(stderr
, "Issued domain %d %s\n", xen_domid
,
1347 reboot
? "reboot" : "poweroff");
1349 xc_interface_close(xc_handle
);
1353 void xen_register_framebuffer(MemoryRegion
*mr
)
1358 void xen_shutdown_fatal_error(const char *fmt
, ...)
1363 vfprintf(stderr
, fmt
, ap
);
1365 fprintf(stderr
, "Will destroy the domain.\n");
1366 /* destroy the domain */
1367 qemu_system_shutdown_request();
1370 void xen_modified_memory(ram_addr_t start
, ram_addr_t length
)
1372 if (unlikely(xen_in_migration
)) {
1374 ram_addr_t start_pfn
, nb_pages
;
1377 length
= TARGET_PAGE_SIZE
;
1379 start_pfn
= start
>> TARGET_PAGE_BITS
;
1380 nb_pages
= ((start
+ length
+ TARGET_PAGE_SIZE
- 1) >> TARGET_PAGE_BITS
)
1382 rc
= xc_hvm_modified_memory(xen_xc
, xen_domid
, start_pfn
, nb_pages
);
1385 "%s failed for "RAM_ADDR_FMT
" ("RAM_ADDR_FMT
"): %i, %s\n",
1386 __func__
, start
, nb_pages
, rc
, strerror(-rc
));
1391 void qmp_xen_set_global_dirty_log(bool enable
, Error
**errp
)
1394 memory_global_dirty_log_start();
1396 memory_global_dirty_log_stop();