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.
15 #include "hw/xen_common.h"
16 #include "hw/xen_backend.h"
17 #include "qmp-commands.h"
20 #include "xen-mapcache.h"
22 #include "exec-memory.h"
24 #include <xen/hvm/ioreq.h>
25 #include <xen/hvm/params.h>
26 #include <xen/hvm/e820.h>
31 #define DPRINTF(fmt, ...) \
32 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) \
38 static MemoryRegion ram_memory
, ram_640k
, ram_lo
, ram_hi
;
39 static MemoryRegion
*framebuffer
;
40 static bool xen_in_migration
;
42 /* Compatibility with older version */
43 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
44 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
46 return shared_page
->vcpu_iodata
[i
].vp_eport
;
48 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
50 return &shared_page
->vcpu_iodata
[vcpu
].vp_ioreq
;
52 # define FMT_ioreq_size PRIx64
54 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
56 return shared_page
->vcpu_ioreq
[i
].vp_eport
;
58 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
60 return &shared_page
->vcpu_ioreq
[vcpu
];
62 # define FMT_ioreq_size "u"
64 #ifndef HVM_PARAM_BUFIOREQ_EVTCHN
65 #define HVM_PARAM_BUFIOREQ_EVTCHN 26
68 #define BUFFER_IO_MAX_DELAY 100
70 typedef struct XenPhysmap
{
76 QLIST_ENTRY(XenPhysmap
) list
;
79 typedef struct XenIOState
{
80 shared_iopage_t
*shared_page
;
81 buffered_iopage_t
*buffered_io_page
;
82 QEMUTimer
*buffered_io_timer
;
83 /* the evtchn port for polling the notification, */
84 evtchn_port_t
*ioreq_local_port
;
85 /* evtchn local port for buffered io */
86 evtchn_port_t bufioreq_local_port
;
87 /* the evtchn fd for polling */
89 /* which vcpu we are serving */
92 struct xs_handle
*xenstore
;
93 MemoryListener memory_listener
;
94 QLIST_HEAD(, XenPhysmap
) physmap
;
95 hwaddr free_phys_offset
;
96 const XenPhysmap
*log_for_dirtybit
;
102 /* Xen specific function for piix pci */
104 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
106 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
109 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
111 xc_hvm_set_pci_intx_level(xen_xc
, xen_domid
, 0, 0, irq_num
>> 2,
115 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
119 /* Scan for updates to PCI link routes (0x60-0x63). */
120 for (i
= 0; i
< len
; i
++) {
121 uint8_t v
= (val
>> (8 * i
)) & 0xff;
126 if (((address
+ i
) >= 0x60) && ((address
+ i
) <= 0x63)) {
127 xc_hvm_set_pci_link_route(xen_xc
, xen_domid
, address
+ i
- 0x60, v
);
132 void xen_hvm_inject_msi(uint64_t addr
, uint32_t data
)
134 xen_xc_hvm_inject_msi(xen_xc
, xen_domid
, addr
, data
);
137 static void xen_suspend_notifier(Notifier
*notifier
, void *data
)
139 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 3);
142 /* Xen Interrupt Controller */
144 static void xen_set_irq(void *opaque
, int irq
, int level
)
146 xc_hvm_set_isa_irq_level(xen_xc
, xen_domid
, irq
, level
);
149 qemu_irq
*xen_interrupt_controller_init(void)
151 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
156 static void xen_ram_init(ram_addr_t ram_size
)
158 MemoryRegion
*sysmem
= get_system_memory();
159 ram_addr_t below_4g_mem_size
, above_4g_mem_size
= 0;
160 ram_addr_t block_len
;
162 block_len
= ram_size
;
163 if (ram_size
>= HVM_BELOW_4G_RAM_END
) {
164 /* Xen does not allocate the memory continuously, and keep a hole at
165 * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH
167 block_len
+= HVM_BELOW_4G_MMIO_LENGTH
;
169 memory_region_init_ram(&ram_memory
, "xen.ram", block_len
);
170 vmstate_register_ram_global(&ram_memory
);
172 if (ram_size
>= HVM_BELOW_4G_RAM_END
) {
173 above_4g_mem_size
= ram_size
- HVM_BELOW_4G_RAM_END
;
174 below_4g_mem_size
= HVM_BELOW_4G_RAM_END
;
176 below_4g_mem_size
= ram_size
;
179 memory_region_init_alias(&ram_640k
, "xen.ram.640k",
180 &ram_memory
, 0, 0xa0000);
181 memory_region_add_subregion(sysmem
, 0, &ram_640k
);
182 /* Skip of the VGA IO memory space, it will be registered later by the VGA
185 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
186 * the Options ROM, so it is registered here as RAM.
188 memory_region_init_alias(&ram_lo
, "xen.ram.lo",
189 &ram_memory
, 0xc0000, below_4g_mem_size
- 0xc0000);
190 memory_region_add_subregion(sysmem
, 0xc0000, &ram_lo
);
191 if (above_4g_mem_size
> 0) {
192 memory_region_init_alias(&ram_hi
, "xen.ram.hi",
193 &ram_memory
, 0x100000000ULL
,
195 memory_region_add_subregion(sysmem
, 0x100000000ULL
, &ram_hi
);
199 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
, MemoryRegion
*mr
)
201 unsigned long nr_pfn
;
205 if (runstate_check(RUN_STATE_INMIGRATE
)) {
206 /* RAM already populated in Xen */
207 fprintf(stderr
, "%s: do not alloc "RAM_ADDR_FMT
208 " bytes of ram at "RAM_ADDR_FMT
" when runstate is INMIGRATE\n",
209 __func__
, size
, ram_addr
);
213 if (mr
== &ram_memory
) {
217 trace_xen_ram_alloc(ram_addr
, size
);
219 nr_pfn
= size
>> TARGET_PAGE_BITS
;
220 pfn_list
= g_malloc(sizeof (*pfn_list
) * nr_pfn
);
222 for (i
= 0; i
< nr_pfn
; i
++) {
223 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
226 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
227 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT
, ram_addr
);
233 static XenPhysmap
*get_physmapping(XenIOState
*state
,
234 hwaddr start_addr
, ram_addr_t size
)
236 XenPhysmap
*physmap
= NULL
;
238 start_addr
&= TARGET_PAGE_MASK
;
240 QLIST_FOREACH(physmap
, &state
->physmap
, list
) {
241 if (range_covers_byte(physmap
->start_addr
, physmap
->size
, start_addr
)) {
248 static hwaddr
xen_phys_offset_to_gaddr(hwaddr start_addr
,
249 ram_addr_t size
, void *opaque
)
251 hwaddr addr
= start_addr
& TARGET_PAGE_MASK
;
252 XenIOState
*xen_io_state
= opaque
;
253 XenPhysmap
*physmap
= NULL
;
255 QLIST_FOREACH(physmap
, &xen_io_state
->physmap
, list
) {
256 if (range_covers_byte(physmap
->phys_offset
, physmap
->size
, addr
)) {
257 return physmap
->start_addr
;
264 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
265 static int xen_add_to_physmap(XenIOState
*state
,
269 hwaddr offset_within_region
)
273 XenPhysmap
*physmap
= NULL
;
274 hwaddr pfn
, start_gpfn
;
275 hwaddr phys_offset
= memory_region_get_ram_addr(mr
);
276 char path
[80], value
[17];
278 if (get_physmapping(state
, start_addr
, size
)) {
285 /* Xen can only handle a single dirty log region for now and we want
286 * the linear framebuffer to be that region.
287 * Avoid tracking any regions that is not videoram and avoid tracking
288 * the legacy vga region. */
289 if (mr
== framebuffer
&& start_addr
> 0xbffff) {
295 DPRINTF("mapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
296 start_addr
, start_addr
+ size
);
298 pfn
= phys_offset
>> TARGET_PAGE_BITS
;
299 start_gpfn
= start_addr
>> TARGET_PAGE_BITS
;
300 for (i
= 0; i
< size
>> TARGET_PAGE_BITS
; i
++) {
301 unsigned long idx
= pfn
+ i
;
302 xen_pfn_t gpfn
= start_gpfn
+ i
;
304 rc
= xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
306 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
307 PRI_xen_pfn
" failed: %d\n", idx
, gpfn
, rc
);
312 physmap
= g_malloc(sizeof (XenPhysmap
));
314 physmap
->start_addr
= start_addr
;
315 physmap
->size
= size
;
316 physmap
->name
= (char *)mr
->name
;
317 physmap
->phys_offset
= phys_offset
;
319 QLIST_INSERT_HEAD(&state
->physmap
, physmap
, list
);
321 xc_domain_pin_memory_cacheattr(xen_xc
, xen_domid
,
322 start_addr
>> TARGET_PAGE_BITS
,
323 (start_addr
+ size
) >> TARGET_PAGE_BITS
,
324 XEN_DOMCTL_MEM_CACHEATTR_WB
);
326 snprintf(path
, sizeof(path
),
327 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/start_addr",
328 xen_domid
, (uint64_t)phys_offset
);
329 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)start_addr
);
330 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
333 snprintf(path
, sizeof(path
),
334 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/size",
335 xen_domid
, (uint64_t)phys_offset
);
336 snprintf(value
, sizeof(value
), "%"PRIx64
, (uint64_t)size
);
337 if (!xs_write(state
->xenstore
, 0, path
, value
, strlen(value
))) {
341 snprintf(path
, sizeof(path
),
342 "/local/domain/0/device-model/%d/physmap/%"PRIx64
"/name",
343 xen_domid
, (uint64_t)phys_offset
);
344 if (!xs_write(state
->xenstore
, 0, path
, mr
->name
, strlen(mr
->name
))) {
352 static int xen_remove_from_physmap(XenIOState
*state
,
358 XenPhysmap
*physmap
= NULL
;
359 hwaddr phys_offset
= 0;
361 physmap
= get_physmapping(state
, start_addr
, size
);
362 if (physmap
== NULL
) {
366 phys_offset
= physmap
->phys_offset
;
367 size
= physmap
->size
;
369 DPRINTF("unmapping vram to %"HWADDR_PRIx
" - %"HWADDR_PRIx
", from ",
370 "%"HWADDR_PRIx
"\n", phys_offset
, phys_offset
+ size
, start_addr
);
372 size
>>= TARGET_PAGE_BITS
;
373 start_addr
>>= TARGET_PAGE_BITS
;
374 phys_offset
>>= TARGET_PAGE_BITS
;
375 for (i
= 0; i
< size
; i
++) {
376 unsigned long idx
= start_addr
+ i
;
377 xen_pfn_t gpfn
= phys_offset
+ i
;
379 rc
= xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
381 fprintf(stderr
, "add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
382 PRI_xen_pfn
" failed: %d\n", idx
, gpfn
, rc
);
387 QLIST_REMOVE(physmap
, list
);
388 if (state
->log_for_dirtybit
== physmap
) {
389 state
->log_for_dirtybit
= NULL
;
397 static int xen_add_to_physmap(XenIOState
*state
,
401 hwaddr offset_within_region
)
406 static int xen_remove_from_physmap(XenIOState
*state
,
414 static void xen_set_memory(struct MemoryListener
*listener
,
415 MemoryRegionSection
*section
,
418 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
419 hwaddr start_addr
= section
->offset_within_address_space
;
420 ram_addr_t size
= section
->size
;
421 bool log_dirty
= memory_region_is_logging(section
->mr
);
422 hvmmem_type_t mem_type
;
424 if (!memory_region_is_ram(section
->mr
)) {
428 if (!(section
->mr
!= &ram_memory
429 && ( (log_dirty
&& add
) || (!log_dirty
&& !add
)))) {
433 trace_xen_client_set_memory(start_addr
, size
, log_dirty
);
435 start_addr
&= TARGET_PAGE_MASK
;
436 size
= TARGET_PAGE_ALIGN(size
);
439 if (!memory_region_is_rom(section
->mr
)) {
440 xen_add_to_physmap(state
, start_addr
, size
,
441 section
->mr
, section
->offset_within_region
);
443 mem_type
= HVMMEM_ram_ro
;
444 if (xc_hvm_set_mem_type(xen_xc
, xen_domid
, mem_type
,
445 start_addr
>> TARGET_PAGE_BITS
,
446 size
>> TARGET_PAGE_BITS
)) {
447 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx
"\n",
452 if (xen_remove_from_physmap(state
, start_addr
, size
) < 0) {
453 DPRINTF("physmapping does not exist at "TARGET_FMT_plx
"\n", start_addr
);
458 static void xen_region_add(MemoryListener
*listener
,
459 MemoryRegionSection
*section
)
461 xen_set_memory(listener
, section
, true);
464 static void xen_region_del(MemoryListener
*listener
,
465 MemoryRegionSection
*section
)
467 xen_set_memory(listener
, section
, false);
470 static void xen_sync_dirty_bitmap(XenIOState
*state
,
474 hwaddr npages
= size
>> TARGET_PAGE_BITS
;
475 const int width
= sizeof(unsigned long) * 8;
476 unsigned long bitmap
[(npages
+ width
- 1) / width
];
478 const XenPhysmap
*physmap
= NULL
;
480 physmap
= get_physmapping(state
, start_addr
, size
);
481 if (physmap
== NULL
) {
486 if (state
->log_for_dirtybit
== NULL
) {
487 state
->log_for_dirtybit
= physmap
;
488 } else if (state
->log_for_dirtybit
!= physmap
) {
489 /* Only one range for dirty bitmap can be tracked. */
493 rc
= xc_hvm_track_dirty_vram(xen_xc
, xen_domid
,
494 start_addr
>> TARGET_PAGE_BITS
, npages
,
497 if (rc
!= -ENODATA
) {
498 memory_region_set_dirty(framebuffer
, 0, size
);
499 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
500 ", 0x" TARGET_FMT_plx
"): %s\n",
501 start_addr
, start_addr
+ size
, strerror(-rc
));
506 for (i
= 0; i
< ARRAY_SIZE(bitmap
); i
++) {
507 unsigned long map
= bitmap
[i
];
511 memory_region_set_dirty(framebuffer
,
512 (i
* width
+ j
) * TARGET_PAGE_SIZE
,
518 static void xen_log_start(MemoryListener
*listener
,
519 MemoryRegionSection
*section
)
521 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
523 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
527 static void xen_log_stop(MemoryListener
*listener
, MemoryRegionSection
*section
)
529 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
531 state
->log_for_dirtybit
= NULL
;
532 /* Disable dirty bit tracking */
533 xc_hvm_track_dirty_vram(xen_xc
, xen_domid
, 0, 0, NULL
);
536 static void xen_log_sync(MemoryListener
*listener
, MemoryRegionSection
*section
)
538 XenIOState
*state
= container_of(listener
, XenIOState
, memory_listener
);
540 xen_sync_dirty_bitmap(state
, section
->offset_within_address_space
,
544 static void xen_log_global_start(MemoryListener
*listener
)
547 xen_in_migration
= true;
551 static void xen_log_global_stop(MemoryListener
*listener
)
553 xen_in_migration
= false;
556 static MemoryListener xen_memory_listener
= {
557 .region_add
= xen_region_add
,
558 .region_del
= xen_region_del
,
559 .log_start
= xen_log_start
,
560 .log_stop
= xen_log_stop
,
561 .log_sync
= xen_log_sync
,
562 .log_global_start
= xen_log_global_start
,
563 .log_global_stop
= xen_log_global_stop
,
567 void qmp_xen_set_global_dirty_log(bool enable
, Error
**errp
)
570 memory_global_dirty_log_start();
572 memory_global_dirty_log_stop();
576 /* VCPU Operations, MMIO, IO ring ... */
578 static void xen_reset_vcpu(void *opaque
)
580 CPUArchState
*env
= opaque
;
585 void xen_vcpu_init(void)
587 CPUArchState
*first_cpu
;
589 if ((first_cpu
= qemu_get_cpu(0))) {
590 qemu_register_reset(xen_reset_vcpu
, first_cpu
);
591 xen_reset_vcpu(first_cpu
);
593 /* if rtc_clock is left to default (host_clock), disable it */
594 if (rtc_clock
== host_clock
) {
595 qemu_clock_enable(rtc_clock
, false);
599 /* get the ioreq packets from share mem */
600 static ioreq_t
*cpu_get_ioreq_from_shared_memory(XenIOState
*state
, int vcpu
)
602 ioreq_t
*req
= xen_vcpu_ioreq(state
->shared_page
, vcpu
);
604 if (req
->state
!= STATE_IOREQ_READY
) {
605 DPRINTF("I/O request not ready: "
606 "%x, ptr: %x, port: %"PRIx64
", "
607 "data: %"PRIx64
", count: %" FMT_ioreq_size
", size: %" FMT_ioreq_size
"\n",
608 req
->state
, req
->data_is_ptr
, req
->addr
,
609 req
->data
, req
->count
, req
->size
);
613 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
615 req
->state
= STATE_IOREQ_INPROCESS
;
619 /* use poll to get the port notification */
620 /* ioreq_vec--out,the */
621 /* retval--the number of ioreq packet */
622 static ioreq_t
*cpu_get_ioreq(XenIOState
*state
)
627 port
= xc_evtchn_pending(state
->xce_handle
);
628 if (port
== state
->bufioreq_local_port
) {
629 qemu_mod_timer(state
->buffered_io_timer
,
630 BUFFER_IO_MAX_DELAY
+ qemu_get_clock_ms(rt_clock
));
635 for (i
= 0; i
< smp_cpus
; i
++) {
636 if (state
->ioreq_local_port
[i
] == port
) {
642 hw_error("Fatal error while trying to get io event!\n");
645 /* unmask the wanted port again */
646 xc_evtchn_unmask(state
->xce_handle
, port
);
648 /* get the io packet from shared memory */
649 state
->send_vcpu
= i
;
650 return cpu_get_ioreq_from_shared_memory(state
, i
);
653 /* read error or read nothing */
657 static uint32_t do_inp(pio_addr_t addr
, unsigned long size
)
661 return cpu_inb(addr
);
663 return cpu_inw(addr
);
665 return cpu_inl(addr
);
667 hw_error("inp: bad size: %04"FMT_pioaddr
" %lx", addr
, size
);
671 static void do_outp(pio_addr_t addr
,
672 unsigned long size
, uint32_t val
)
676 return cpu_outb(addr
, val
);
678 return cpu_outw(addr
, val
);
680 return cpu_outl(addr
, val
);
682 hw_error("outp: bad size: %04"FMT_pioaddr
" %lx", addr
, size
);
686 static void cpu_ioreq_pio(ioreq_t
*req
)
690 sign
= req
->df
? -1 : 1;
692 if (req
->dir
== IOREQ_READ
) {
693 if (!req
->data_is_ptr
) {
694 req
->data
= do_inp(req
->addr
, req
->size
);
698 for (i
= 0; i
< req
->count
; i
++) {
699 tmp
= do_inp(req
->addr
, req
->size
);
700 cpu_physical_memory_write(
701 req
->data
+ (sign
* i
* (int64_t)req
->size
),
702 (uint8_t *) &tmp
, req
->size
);
705 } else if (req
->dir
== IOREQ_WRITE
) {
706 if (!req
->data_is_ptr
) {
707 do_outp(req
->addr
, req
->size
, req
->data
);
709 for (i
= 0; i
< req
->count
; i
++) {
712 cpu_physical_memory_read(
713 req
->data
+ (sign
* i
* (int64_t)req
->size
),
714 (uint8_t*) &tmp
, req
->size
);
715 do_outp(req
->addr
, req
->size
, tmp
);
721 static void cpu_ioreq_move(ioreq_t
*req
)
725 sign
= req
->df
? -1 : 1;
727 if (!req
->data_is_ptr
) {
728 if (req
->dir
== IOREQ_READ
) {
729 for (i
= 0; i
< req
->count
; i
++) {
730 cpu_physical_memory_read(
731 req
->addr
+ (sign
* i
* (int64_t)req
->size
),
732 (uint8_t *) &req
->data
, req
->size
);
734 } else if (req
->dir
== IOREQ_WRITE
) {
735 for (i
= 0; i
< req
->count
; i
++) {
736 cpu_physical_memory_write(
737 req
->addr
+ (sign
* i
* (int64_t)req
->size
),
738 (uint8_t *) &req
->data
, req
->size
);
744 if (req
->dir
== IOREQ_READ
) {
745 for (i
= 0; i
< req
->count
; i
++) {
746 cpu_physical_memory_read(
747 req
->addr
+ (sign
* i
* (int64_t)req
->size
),
748 (uint8_t*) &tmp
, req
->size
);
749 cpu_physical_memory_write(
750 req
->data
+ (sign
* i
* (int64_t)req
->size
),
751 (uint8_t*) &tmp
, req
->size
);
753 } else if (req
->dir
== IOREQ_WRITE
) {
754 for (i
= 0; i
< req
->count
; i
++) {
755 cpu_physical_memory_read(
756 req
->data
+ (sign
* i
* (int64_t)req
->size
),
757 (uint8_t*) &tmp
, req
->size
);
758 cpu_physical_memory_write(
759 req
->addr
+ (sign
* i
* (int64_t)req
->size
),
760 (uint8_t*) &tmp
, req
->size
);
766 static void handle_ioreq(ioreq_t
*req
)
768 if (!req
->data_is_ptr
&& (req
->dir
== IOREQ_WRITE
) &&
769 (req
->size
< sizeof (target_ulong
))) {
770 req
->data
&= ((target_ulong
) 1 << (8 * req
->size
)) - 1;
777 case IOREQ_TYPE_COPY
:
780 case IOREQ_TYPE_TIMEOFFSET
:
782 case IOREQ_TYPE_INVALIDATE
:
783 xen_invalidate_map_cache();
786 hw_error("Invalid ioreq type 0x%x\n", req
->type
);
790 static int handle_buffered_iopage(XenIOState
*state
)
792 buf_ioreq_t
*buf_req
= NULL
;
796 if (!state
->buffered_io_page
) {
800 memset(&req
, 0x00, sizeof(req
));
802 while (state
->buffered_io_page
->read_pointer
!= state
->buffered_io_page
->write_pointer
) {
803 buf_req
= &state
->buffered_io_page
->buf_ioreq
[
804 state
->buffered_io_page
->read_pointer
% IOREQ_BUFFER_SLOT_NUM
];
805 req
.size
= 1UL << buf_req
->size
;
807 req
.addr
= buf_req
->addr
;
808 req
.data
= buf_req
->data
;
809 req
.state
= STATE_IOREQ_READY
;
810 req
.dir
= buf_req
->dir
;
812 req
.type
= buf_req
->type
;
814 qw
= (req
.size
== 8);
816 buf_req
= &state
->buffered_io_page
->buf_ioreq
[
817 (state
->buffered_io_page
->read_pointer
+ 1) % IOREQ_BUFFER_SLOT_NUM
];
818 req
.data
|= ((uint64_t)buf_req
->data
) << 32;
824 state
->buffered_io_page
->read_pointer
+= qw
? 2 : 1;
830 static void handle_buffered_io(void *opaque
)
832 XenIOState
*state
= opaque
;
834 if (handle_buffered_iopage(state
)) {
835 qemu_mod_timer(state
->buffered_io_timer
,
836 BUFFER_IO_MAX_DELAY
+ qemu_get_clock_ms(rt_clock
));
838 qemu_del_timer(state
->buffered_io_timer
);
839 xc_evtchn_unmask(state
->xce_handle
, state
->bufioreq_local_port
);
843 static void cpu_handle_ioreq(void *opaque
)
845 XenIOState
*state
= opaque
;
846 ioreq_t
*req
= cpu_get_ioreq(state
);
848 handle_buffered_iopage(state
);
852 if (req
->state
!= STATE_IOREQ_INPROCESS
) {
853 fprintf(stderr
, "Badness in I/O request ... not in service?!: "
854 "%x, ptr: %x, port: %"PRIx64
", "
855 "data: %"PRIx64
", count: %" FMT_ioreq_size
", size: %" FMT_ioreq_size
"\n",
856 req
->state
, req
->data_is_ptr
, req
->addr
,
857 req
->data
, req
->count
, req
->size
);
858 destroy_hvm_domain(false);
862 xen_wmb(); /* Update ioreq contents /then/ update state. */
865 * We do this before we send the response so that the tools
866 * have the opportunity to pick up on the reset before the
867 * guest resumes and does a hlt with interrupts disabled which
868 * causes Xen to powerdown the domain.
870 if (runstate_is_running()) {
871 if (qemu_shutdown_requested_get()) {
872 destroy_hvm_domain(false);
874 if (qemu_reset_requested_get()) {
875 qemu_system_reset(VMRESET_REPORT
);
876 destroy_hvm_domain(true);
880 req
->state
= STATE_IORESP_READY
;
881 xc_evtchn_notify(state
->xce_handle
, state
->ioreq_local_port
[state
->send_vcpu
]);
885 static int store_dev_info(int domid
, CharDriverState
*cs
, const char *string
)
887 struct xs_handle
*xs
= NULL
;
889 char *newpath
= NULL
;
893 /* Only continue if we're talking to a pty. */
894 if (strncmp(cs
->filename
, "pty:", 4)) {
897 pts
= cs
->filename
+ 4;
899 /* We now have everything we need to set the xenstore entry. */
902 fprintf(stderr
, "Could not contact XenStore\n");
906 path
= xs_get_domain_path(xs
, domid
);
908 fprintf(stderr
, "xs_get_domain_path() error\n");
911 newpath
= realloc(path
, (strlen(path
) + strlen(string
) +
912 strlen("/tty") + 1));
913 if (newpath
== NULL
) {
914 fprintf(stderr
, "realloc error\n");
919 strcat(path
, string
);
920 strcat(path
, "/tty");
921 if (!xs_write(xs
, XBT_NULL
, path
, pts
, strlen(pts
))) {
922 fprintf(stderr
, "xs_write for '%s' fail", string
);
934 void xenstore_store_pv_console_info(int i
, CharDriverState
*chr
)
937 store_dev_info(xen_domid
, chr
, "/console");
940 snprintf(buf
, sizeof(buf
), "/device/console/%d", i
);
941 store_dev_info(xen_domid
, chr
, buf
);
945 static void xenstore_record_dm_state(struct xs_handle
*xs
, const char *state
)
950 fprintf(stderr
, "xenstore connection not initialized\n");
954 snprintf(path
, sizeof (path
), "/local/domain/0/device-model/%u/state", xen_domid
);
955 if (!xs_write(xs
, XBT_NULL
, path
, state
, strlen(state
))) {
956 fprintf(stderr
, "error recording dm state\n");
961 static void xen_main_loop_prepare(XenIOState
*state
)
965 if (state
->xce_handle
!= XC_HANDLER_INITIAL_VALUE
) {
966 evtchn_fd
= xc_evtchn_fd(state
->xce_handle
);
969 state
->buffered_io_timer
= qemu_new_timer_ms(rt_clock
, handle_buffered_io
,
972 if (evtchn_fd
!= -1) {
973 qemu_set_fd_handler(evtchn_fd
, cpu_handle_ioreq
, NULL
, state
);
980 static void xen_change_state_handler(void *opaque
, int running
,
984 /* record state running */
985 xenstore_record_dm_state(xenstore
, "running");
989 static void xen_hvm_change_state_handler(void *opaque
, int running
,
992 XenIOState
*xstate
= opaque
;
994 xen_main_loop_prepare(xstate
);
998 static void xen_exit_notifier(Notifier
*n
, void *data
)
1000 XenIOState
*state
= container_of(n
, XenIOState
, exit
);
1002 xc_evtchn_close(state
->xce_handle
);
1003 xs_daemon_close(state
->xenstore
);
1008 xen_xc
= xen_xc_interface_open(0, 0, 0);
1009 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
1010 xen_be_printf(NULL
, 0, "can't open xen interface\n");
1013 qemu_add_vm_change_state_handler(xen_change_state_handler
, NULL
);
1018 static void xen_read_physmap(XenIOState
*state
)
1020 XenPhysmap
*physmap
= NULL
;
1021 unsigned int len
, num
, i
;
1022 char path
[80], *value
= NULL
;
1023 char **entries
= NULL
;
1025 snprintf(path
, sizeof(path
),
1026 "/local/domain/0/device-model/%d/physmap", xen_domid
);
1027 entries
= xs_directory(state
->xenstore
, 0, path
, &num
);
1028 if (entries
== NULL
)
1031 for (i
= 0; i
< num
; i
++) {
1032 physmap
= g_malloc(sizeof (XenPhysmap
));
1033 physmap
->phys_offset
= strtoull(entries
[i
], NULL
, 16);
1034 snprintf(path
, sizeof(path
),
1035 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1036 xen_domid
, entries
[i
]);
1037 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1038 if (value
== NULL
) {
1042 physmap
->start_addr
= strtoull(value
, NULL
, 16);
1045 snprintf(path
, sizeof(path
),
1046 "/local/domain/0/device-model/%d/physmap/%s/size",
1047 xen_domid
, entries
[i
]);
1048 value
= xs_read(state
->xenstore
, 0, path
, &len
);
1049 if (value
== NULL
) {
1053 physmap
->size
= strtoull(value
, NULL
, 16);
1056 snprintf(path
, sizeof(path
),
1057 "/local/domain/0/device-model/%d/physmap/%s/name",
1058 xen_domid
, entries
[i
]);
1059 physmap
->name
= xs_read(state
->xenstore
, 0, path
, &len
);
1061 QLIST_INSERT_HEAD(&state
->physmap
, physmap
, list
);
1066 int xen_hvm_init(void)
1069 unsigned long ioreq_pfn
;
1070 unsigned long bufioreq_evtchn
;
1073 state
= g_malloc0(sizeof (XenIOState
));
1075 state
->xce_handle
= xen_xc_evtchn_open(NULL
, 0);
1076 if (state
->xce_handle
== XC_HANDLER_INITIAL_VALUE
) {
1077 perror("xen: event channel open");
1081 state
->xenstore
= xs_daemon_open();
1082 if (state
->xenstore
== NULL
) {
1083 perror("xen: xenstore open");
1087 state
->exit
.notify
= xen_exit_notifier
;
1088 qemu_add_exit_notifier(&state
->exit
);
1090 state
->suspend
.notify
= xen_suspend_notifier
;
1091 qemu_register_suspend_notifier(&state
->suspend
);
1093 xc_get_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_IOREQ_PFN
, &ioreq_pfn
);
1094 DPRINTF("shared page at pfn %lx\n", ioreq_pfn
);
1095 state
->shared_page
= xc_map_foreign_range(xen_xc
, xen_domid
, XC_PAGE_SIZE
,
1096 PROT_READ
|PROT_WRITE
, ioreq_pfn
);
1097 if (state
->shared_page
== NULL
) {
1098 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT
,
1102 xc_get_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_BUFIOREQ_PFN
, &ioreq_pfn
);
1103 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn
);
1104 state
->buffered_io_page
= xc_map_foreign_range(xen_xc
, xen_domid
, XC_PAGE_SIZE
,
1105 PROT_READ
|PROT_WRITE
, ioreq_pfn
);
1106 if (state
->buffered_io_page
== NULL
) {
1107 hw_error("map buffered IO page returned error %d", errno
);
1110 state
->ioreq_local_port
= g_malloc0(smp_cpus
* sizeof (evtchn_port_t
));
1112 /* FIXME: how about if we overflow the page here? */
1113 for (i
= 0; i
< smp_cpus
; i
++) {
1114 rc
= xc_evtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1115 xen_vcpu_eport(state
->shared_page
, i
));
1117 fprintf(stderr
, "bind interdomain ioctl error %d\n", errno
);
1120 state
->ioreq_local_port
[i
] = rc
;
1123 rc
= xc_get_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_BUFIOREQ_EVTCHN
,
1126 fprintf(stderr
, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
1129 rc
= xc_evtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
1130 (uint32_t)bufioreq_evtchn
);
1132 fprintf(stderr
, "bind interdomain ioctl error %d\n", errno
);
1135 state
->bufioreq_local_port
= rc
;
1137 /* Init RAM management */
1138 xen_map_cache_init(xen_phys_offset_to_gaddr
, state
);
1139 xen_ram_init(ram_size
);
1141 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler
, state
);
1143 state
->memory_listener
= xen_memory_listener
;
1144 QLIST_INIT(&state
->physmap
);
1145 memory_listener_register(&state
->memory_listener
, &address_space_memory
);
1146 state
->log_for_dirtybit
= NULL
;
1148 /* Initialize backend core & drivers */
1149 if (xen_be_init() != 0) {
1150 fprintf(stderr
, "%s: xen backend core setup failed\n", __FUNCTION__
);
1153 xen_be_register("console", &xen_console_ops
);
1154 xen_be_register("vkbd", &xen_kbdmouse_ops
);
1155 xen_be_register("qdisk", &xen_blkdev_ops
);
1156 xen_read_physmap(state
);
1161 void destroy_hvm_domain(bool reboot
)
1166 xc_handle
= xen_xc_interface_open(0, 0, 0);
1167 if (xc_handle
== XC_HANDLER_INITIAL_VALUE
) {
1168 fprintf(stderr
, "Cannot acquire xenctrl handle\n");
1170 sts
= xc_domain_shutdown(xc_handle
, xen_domid
,
1171 reboot
? SHUTDOWN_reboot
: SHUTDOWN_poweroff
);
1173 fprintf(stderr
, "xc_domain_shutdown failed to issue %s, "
1174 "sts %d, %s\n", reboot
? "reboot" : "poweroff",
1175 sts
, strerror(errno
));
1177 fprintf(stderr
, "Issued domain %d %s\n", xen_domid
,
1178 reboot
? "reboot" : "poweroff");
1180 xc_interface_close(xc_handle
);
1184 void xen_register_framebuffer(MemoryRegion
*mr
)
1189 void xen_shutdown_fatal_error(const char *fmt
, ...)
1194 vfprintf(stderr
, fmt
, ap
);
1196 fprintf(stderr
, "Will destroy the domain.\n");
1197 /* destroy the domain */
1198 qemu_system_shutdown_request();
1201 void xen_modified_memory(ram_addr_t start
, ram_addr_t length
)
1203 if (unlikely(xen_in_migration
)) {
1205 ram_addr_t start_pfn
, nb_pages
;
1208 length
= TARGET_PAGE_SIZE
;
1210 start_pfn
= start
>> TARGET_PAGE_BITS
;
1211 nb_pages
= ((start
+ length
+ TARGET_PAGE_SIZE
- 1) >> TARGET_PAGE_BITS
)
1213 rc
= xc_hvm_modified_memory(xen_xc
, xen_domid
, start_pfn
, nb_pages
);
1216 "%s failed for "RAM_ADDR_FMT
" ("RAM_ADDR_FMT
"): %i, %s\n",
1217 __func__
, start
, nb_pages
, rc
, strerror(-rc
));