1
2
3
4
5
6
7
8
9
10
11#include "qemu/osdep.h"
12#include "qemu/units.h"
13
14#include "cpu.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"
19#include "hw/irq.h"
20#include "hw/hw.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"
35#include "trace.h"
36
37#include <xen/hvm/ioreq.h>
38#include <xen/hvm/e820.h>
39
40
41
42#ifdef DEBUG_XEN_HVM
43#define DPRINTF(fmt, ...) \
44 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
45#else
46#define DPRINTF(fmt, ...) \
47 do { } while (0)
48#endif
49
50static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
51static MemoryRegion *framebuffer;
52static bool xen_in_migration;
53
54
55
56
57
58
59
60
61#ifndef IOREQ_TYPE_VMWARE_PORT
62#define IOREQ_TYPE_VMWARE_PORT 3
63struct vmware_regs {
64 uint32_t esi;
65 uint32_t edi;
66 uint32_t ebx;
67 uint32_t ecx;
68 uint32_t edx;
69};
70typedef struct vmware_regs vmware_regs_t;
71
72struct shared_vmport_iopage {
73 struct vmware_regs vcpu_vmport_regs[1];
74};
75typedef struct shared_vmport_iopage shared_vmport_iopage_t;
76#endif
77
78static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
79{
80 return shared_page->vcpu_ioreq[i].vp_eport;
81}
82static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
83{
84 return &shared_page->vcpu_ioreq[vcpu];
85}
86
87#define BUFFER_IO_MAX_DELAY 100
88
89typedef struct XenPhysmap {
90 hwaddr start_addr;
91 ram_addr_t size;
92 const char *name;
93 hwaddr phys_offset;
94
95 QLIST_ENTRY(XenPhysmap) list;
96} XenPhysmap;
97
98static QLIST_HEAD(, XenPhysmap) xen_physmap;
99
100typedef struct XenPciDevice {
101 PCIDevice *pci_dev;
102 uint32_t sbdf;
103 QLIST_ENTRY(XenPciDevice) entry;
104} XenPciDevice;
105
106typedef struct XenIOState {
107 ioservid_t ioservid;
108 shared_iopage_t *shared_page;
109 shared_vmport_iopage_t *shared_vmport_page;
110 buffered_iopage_t *buffered_io_page;
111 xenforeignmemory_resource_handle *fres;
112 QEMUTimer *buffered_io_timer;
113 CPUState **cpu_by_vcpu_id;
114
115 evtchn_port_t *ioreq_local_port;
116
117 evtchn_port_t bufioreq_remote_port;
118 evtchn_port_t bufioreq_local_port;
119
120 xenevtchn_handle *xce_handle;
121
122 int send_vcpu;
123
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
132 unsigned long *dirty_bitmap;
133
134 Notifier exit;
135 Notifier suspend;
136 Notifier wakeup;
137} XenIOState;
138
139
140
141int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
142{
143 return irq_num + (PCI_SLOT(pci_dev->devfn) << 2);
144}
145
146void xen_piix3_set_irq(void *opaque, int irq_num, int level)
147{
148 xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
149 irq_num & 3, level);
150}
151
152void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
153{
154 int i;
155
156
157 for (i = 0; i < len; i++) {
158 uint8_t v = (val >> (8 * i)) & 0xff;
159 if (v & 0x80) {
160 v = 0;
161 }
162 v &= 0xf;
163 if (((address + i) >= PIIX_PIRQCA) && ((address + i) <= PIIX_PIRQCD)) {
164 xen_set_pci_link_route(xen_domid, address + i - PIIX_PIRQCA, v);
165 }
166 }
167}
168
169int xen_is_pirq_msi(uint32_t msi_data)
170{
171
172
173
174 return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
175}
176
177void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
178{
179 xen_inject_msi(xen_domid, addr, data);
180}
181
182static void xen_suspend_notifier(Notifier *notifier, void *data)
183{
184 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
185}
186
187
188
189static void xen_set_irq(void *opaque, int irq, int level)
190{
191 xen_set_isa_irq_level(xen_domid, irq, level);
192}
193
194qemu_irq *xen_interrupt_controller_init(void)
195{
196 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
197}
198
199
200
201static void xen_ram_init(PCMachineState *pcms,
202 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
203{
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,
210 &error_abort);
211
212
213
214
215 if (!user_lowmem) {
216 user_lowmem = HVM_BELOW_4G_RAM_END;
217 }
218 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
219 user_lowmem = HVM_BELOW_4G_RAM_END;
220 }
221
222 if (ram_size >= user_lowmem) {
223 x86ms->above_4g_mem_size = ram_size - user_lowmem;
224 x86ms->below_4g_mem_size = user_lowmem;
225 } else {
226 x86ms->above_4g_mem_size = 0;
227 x86ms->below_4g_mem_size = ram_size;
228 }
229 if (!x86ms->above_4g_mem_size) {
230 block_len = ram_size;
231 } else {
232
233
234
235
236 block_len = (4 * GiB) + x86ms->above_4g_mem_size;
237 }
238 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
239 &error_fatal);
240 *ram_memory_p = &ram_memory;
241
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
246
247
248
249
250
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);
260 }
261}
262
263void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr,
264 Error **errp)
265{
266 unsigned long nr_pfn;
267 xen_pfn_t *pfn_list;
268 int i;
269
270 if (runstate_check(RUN_STATE_INMIGRATE)) {
271
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);
275 return;
276 }
277
278 if (mr == &ram_memory) {
279 return;
280 }
281
282 trace_xen_ram_alloc(ram_addr, size);
283
284 nr_pfn = size >> TARGET_PAGE_BITS;
285 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
286
287 for (i = 0; i < nr_pfn; i++) {
288 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
289 }
290
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,
293 ram_addr);
294 }
295
296 g_free(pfn_list);
297}
298
299static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size)
300{
301 XenPhysmap *physmap = NULL;
302
303 start_addr &= TARGET_PAGE_MASK;
304
305 QLIST_FOREACH(physmap, &xen_physmap, list) {
306 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
307 return physmap;
308 }
309 }
310 return NULL;
311}
312
313static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size)
314{
315 hwaddr addr = phys_offset & TARGET_PAGE_MASK;
316 XenPhysmap *physmap = NULL;
317
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);
321 }
322 }
323
324 return phys_offset;
325}
326
327#ifdef XEN_COMPAT_PHYSMAP
328static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
329{
330 char path[80], value[17];
331
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))) {
337 return -1;
338 }
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))) {
344 return -1;
345 }
346 if (physmap->name) {
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))) {
352 return -1;
353 }
354 }
355 return 0;
356}
357#else
358static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
359{
360 return 0;
361}
362#endif
363
364static int xen_add_to_physmap(XenIOState *state,
365 hwaddr start_addr,
366 ram_addr_t size,
367 MemoryRegion *mr,
368 hwaddr offset_within_region)
369{
370 unsigned long nr_pages;
371 int rc = 0;
372 XenPhysmap *physmap = NULL;
373 hwaddr pfn, start_gpfn;
374 hwaddr phys_offset = memory_region_get_ram_addr(mr);
375 const char *mr_name;
376
377 if (get_physmapping(start_addr, size)) {
378 return 0;
379 }
380 if (size <= 0) {
381 return -1;
382 }
383
384
385
386
387
388 if (mr == framebuffer && start_addr > 0xbffff) {
389 goto go_physmap;
390 }
391 return -1;
392
393go_physmap:
394 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
395 start_addr, start_addr + size);
396
397 mr_name = memory_region_name(mr);
398
399 physmap = g_new(XenPhysmap, 1);
400
401 physmap->start_addr = start_addr;
402 physmap->size = size;
403 physmap->name = mr_name;
404 physmap->phys_offset = phys_offset;
405
406 QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
407
408 if (runstate_check(RUN_STATE_INMIGRATE)) {
409
410
411 uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
412 assert(p && p == memory_region_get_ram_ptr(mr));
413
414 return 0;
415 }
416
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,
421 start_gpfn);
422 if (rc) {
423 int saved_errno = errno;
424
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));
428 errno = saved_errno;
429 return -1;
430 }
431
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);
436 if (rc) {
437 error_report("pin_memory_cacheattr failed: %s", strerror(errno));
438 }
439 return xen_save_physmap(state, physmap);
440}
441
442static int xen_remove_from_physmap(XenIOState *state,
443 hwaddr start_addr,
444 ram_addr_t size)
445{
446 int rc = 0;
447 XenPhysmap *physmap = NULL;
448 hwaddr phys_offset = 0;
449
450 physmap = get_physmapping(start_addr, size);
451 if (physmap == NULL) {
452 return -1;
453 }
454
455 phys_offset = physmap->phys_offset;
456 size = physmap->size;
457
458 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
459 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
460
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,
465 phys_offset);
466 if (rc) {
467 int saved_errno = errno;
468
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));
473 errno = saved_errno;
474 return -1;
475 }
476
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;
482 }
483 g_free(physmap);
484
485 return 0;
486}
487
488static void xen_set_memory(struct MemoryListener *listener,
489 MemoryRegionSection *section,
490 bool add)
491{
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;
497
498 if (section->mr == &ram_memory) {
499 return;
500 } else {
501 if (add) {
502 xen_map_memory_section(xen_domid, state->ioservid,
503 section);
504 } else {
505 xen_unmap_memory_section(xen_domid, state->ioservid,
506 section);
507 }
508 }
509
510 if (!memory_region_is_ram(section->mr)) {
511 return;
512 }
513
514 if (log_dirty != add) {
515 return;
516 }
517
518 trace_xen_client_set_memory(start_addr, size, log_dirty);
519
520 start_addr &= TARGET_PAGE_MASK;
521 size = TARGET_PAGE_ALIGN(size);
522
523 if (add) {
524 if (!memory_region_is_rom(section->mr)) {
525 xen_add_to_physmap(state, start_addr, size,
526 section->mr, section->offset_within_region);
527 } else {
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",
533 start_addr);
534 }
535 }
536 } else {
537 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
538 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
539 }
540 }
541}
542
543static void xen_region_add(MemoryListener *listener,
544 MemoryRegionSection *section)
545{
546 memory_region_ref(section->mr);
547 xen_set_memory(listener, section, true);
548}
549
550static void xen_region_del(MemoryListener *listener,
551 MemoryRegionSection *section)
552{
553 xen_set_memory(listener, section, false);
554 memory_region_unref(section->mr);
555}
556
557static void xen_io_add(MemoryListener *listener,
558 MemoryRegionSection *section)
559{
560 XenIOState *state = container_of(listener, XenIOState, io_listener);
561 MemoryRegion *mr = section->mr;
562
563 if (mr->ops == &unassigned_io_ops) {
564 return;
565 }
566
567 memory_region_ref(mr);
568
569 xen_map_io_section(xen_domid, state->ioservid, section);
570}
571
572static void xen_io_del(MemoryListener *listener,
573 MemoryRegionSection *section)
574{
575 XenIOState *state = container_of(listener, XenIOState, io_listener);
576 MemoryRegion *mr = section->mr;
577
578 if (mr->ops == &unassigned_io_ops) {
579 return;
580 }
581
582 xen_unmap_io_section(xen_domid, state->ioservid, section);
583
584 memory_region_unref(mr);
585}
586
587static void xen_device_realize(DeviceListener *listener,
588 DeviceState *dev)
589{
590 XenIOState *state = container_of(listener, XenIOState, device_listener);
591
592 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
593 PCIDevice *pci_dev = PCI_DEVICE(dev);
594 XenPciDevice *xendev = g_new(XenPciDevice, 1);
595
596 xendev->pci_dev = pci_dev;
597 xendev->sbdf = PCI_BUILD_BDF(pci_dev_bus_num(pci_dev),
598 pci_dev->devfn);
599 QLIST_INSERT_HEAD(&state->dev_list, xendev, entry);
600
601 xen_map_pcidev(xen_domid, state->ioservid, pci_dev);
602 }
603}
604
605static void xen_device_unrealize(DeviceListener *listener,
606 DeviceState *dev)
607{
608 XenIOState *state = container_of(listener, XenIOState, device_listener);
609
610 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
611 PCIDevice *pci_dev = PCI_DEVICE(dev);
612 XenPciDevice *xendev, *next;
613
614 xen_unmap_pcidev(xen_domid, state->ioservid, pci_dev);
615
616 QLIST_FOREACH_SAFE(xendev, &state->dev_list, entry, next) {
617 if (xendev->pci_dev == pci_dev) {
618 QLIST_REMOVE(xendev, entry);
619 g_free(xendev);
620 break;
621 }
622 }
623 }
624}
625
626static void xen_sync_dirty_bitmap(XenIOState *state,
627 hwaddr start_addr,
628 ram_addr_t size)
629{
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);
633 int rc, i, j;
634 const XenPhysmap *physmap = NULL;
635
636 physmap = get_physmapping(start_addr, size);
637 if (physmap == NULL) {
638
639 return;
640 }
641
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
647 return;
648 }
649
650 rc = xen_track_dirty_vram(xen_domid, start_addr >> TARGET_PAGE_BITS,
651 npages, state->dirty_bitmap);
652 if (rc < 0) {
653#ifndef ENODATA
654#define ENODATA ENOENT
655#endif
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));
661 }
662 return;
663 }
664
665 for (i = 0; i < bitmap_size; i++) {
666 unsigned long map = state->dirty_bitmap[i];
667 while (map != 0) {
668 j = ctzl(map);
669 map &= ~(1ul << j);
670 memory_region_set_dirty(framebuffer,
671 (i * width + j) * TARGET_PAGE_SIZE,
672 TARGET_PAGE_SIZE);
673 };
674 }
675}
676
677static void xen_log_start(MemoryListener *listener,
678 MemoryRegionSection *section,
679 int old, int new)
680{
681 XenIOState *state = container_of(listener, XenIOState, memory_listener);
682
683 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
684 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
685 int128_get64(section->size));
686 }
687}
688
689static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
690 int old, int new)
691{
692 XenIOState *state = container_of(listener, XenIOState, memory_listener);
693
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
699 xen_track_dirty_vram(xen_domid, 0, 0, NULL);
700 }
701}
702
703static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
704{
705 XenIOState *state = container_of(listener, XenIOState, memory_listener);
706
707 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
708 int128_get64(section->size));
709}
710
711static void xen_log_global_start(MemoryListener *listener)
712{
713 if (xen_enabled()) {
714 xen_in_migration = true;
715 }
716}
717
718static void xen_log_global_stop(MemoryListener *listener)
719{
720 xen_in_migration = false;
721}
722
723static MemoryListener xen_memory_listener = {
724 .name = "xen-memory",
725 .region_add = xen_region_add,
726 .region_del = xen_region_del,
727 .log_start = xen_log_start,
728 .log_stop = xen_log_stop,
729 .log_sync = xen_log_sync,
730 .log_global_start = xen_log_global_start,
731 .log_global_stop = xen_log_global_stop,
732 .priority = 10,
733};
734
735static MemoryListener xen_io_listener = {
736 .name = "xen-io",
737 .region_add = xen_io_add,
738 .region_del = xen_io_del,
739 .priority = 10,
740};
741
742static DeviceListener xen_device_listener = {
743 .realize = xen_device_realize,
744 .unrealize = xen_device_unrealize,
745};
746
747
748static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
749{
750 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
751
752 if (req->state != STATE_IOREQ_READY) {
753 DPRINTF("I/O request not ready: "
754 "%x, ptr: %x, port: %"PRIx64", "
755 "data: %"PRIx64", count: %u, size: %u\n",
756 req->state, req->data_is_ptr, req->addr,
757 req->data, req->count, req->size);
758 return NULL;
759 }
760
761 xen_rmb();
762
763 req->state = STATE_IOREQ_INPROCESS;
764 return req;
765}
766
767
768
769
770static ioreq_t *cpu_get_ioreq(XenIOState *state)
771{
772 MachineState *ms = MACHINE(qdev_get_machine());
773 unsigned int max_cpus = ms->smp.max_cpus;
774 int i;
775 evtchn_port_t port;
776
777 port = xenevtchn_pending(state->xce_handle);
778 if (port == state->bufioreq_local_port) {
779 timer_mod(state->buffered_io_timer,
780 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
781 return NULL;
782 }
783
784 if (port != -1) {
785 for (i = 0; i < max_cpus; i++) {
786 if (state->ioreq_local_port[i] == port) {
787 break;
788 }
789 }
790
791 if (i == max_cpus) {
792 hw_error("Fatal error while trying to get io event!\n");
793 }
794
795
796 xenevtchn_unmask(state->xce_handle, port);
797
798
799 state->send_vcpu = i;
800 return cpu_get_ioreq_from_shared_memory(state, i);
801 }
802
803
804 return NULL;
805}
806
807static uint32_t do_inp(uint32_t addr, unsigned long size)
808{
809 switch (size) {
810 case 1:
811 return cpu_inb(addr);
812 case 2:
813 return cpu_inw(addr);
814 case 4:
815 return cpu_inl(addr);
816 default:
817 hw_error("inp: bad size: %04x %lx", addr, size);
818 }
819}
820
821static void do_outp(uint32_t addr,
822 unsigned long size, uint32_t val)
823{
824 switch (size) {
825 case 1:
826 return cpu_outb(addr, val);
827 case 2:
828 return cpu_outw(addr, val);
829 case 4:
830 return cpu_outl(addr, val);
831 default:
832 hw_error("outp: bad size: %04x %lx", addr, size);
833 }
834}
835
836
837
838
839
840
841
842
843
844
845static void rw_phys_req_item(hwaddr addr,
846 ioreq_t *req, uint32_t i, void *val, int rw)
847{
848
849
850
851 hwaddr offset = (hwaddr)req->size * i;
852 if (req->df) {
853 addr -= offset;
854 } else {
855 addr += offset;
856 }
857 cpu_physical_memory_rw(addr, val, req->size, rw);
858}
859
860static inline void read_phys_req_item(hwaddr addr,
861 ioreq_t *req, uint32_t i, void *val)
862{
863 rw_phys_req_item(addr, req, i, val, 0);
864}
865static inline void write_phys_req_item(hwaddr addr,
866 ioreq_t *req, uint32_t i, void *val)
867{
868 rw_phys_req_item(addr, req, i, val, 1);
869}
870
871
872static void cpu_ioreq_pio(ioreq_t *req)
873{
874 uint32_t i;
875
876 trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr,
877 req->data, req->count, req->size);
878
879 if (req->size > sizeof(uint32_t)) {
880 hw_error("PIO: bad size (%u)", req->size);
881 }
882
883 if (req->dir == IOREQ_READ) {
884 if (!req->data_is_ptr) {
885 req->data = do_inp(req->addr, req->size);
886 trace_cpu_ioreq_pio_read_reg(req, req->data, req->addr,
887 req->size);
888 } else {
889 uint32_t tmp;
890
891 for (i = 0; i < req->count; i++) {
892 tmp = do_inp(req->addr, req->size);
893 write_phys_req_item(req->data, req, i, &tmp);
894 }
895 }
896 } else if (req->dir == IOREQ_WRITE) {
897 if (!req->data_is_ptr) {
898 trace_cpu_ioreq_pio_write_reg(req, req->data, req->addr,
899 req->size);
900 do_outp(req->addr, req->size, req->data);
901 } else {
902 for (i = 0; i < req->count; i++) {
903 uint32_t tmp = 0;
904
905 read_phys_req_item(req->data, req, i, &tmp);
906 do_outp(req->addr, req->size, tmp);
907 }
908 }
909 }
910}
911
912static void cpu_ioreq_move(ioreq_t *req)
913{
914 uint32_t i;
915
916 trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr,
917 req->data, req->count, req->size);
918
919 if (req->size > sizeof(req->data)) {
920 hw_error("MMIO: bad size (%u)", req->size);
921 }
922
923 if (!req->data_is_ptr) {
924 if (req->dir == IOREQ_READ) {
925 for (i = 0; i < req->count; i++) {
926 read_phys_req_item(req->addr, req, i, &req->data);
927 }
928 } else if (req->dir == IOREQ_WRITE) {
929 for (i = 0; i < req->count; i++) {
930 write_phys_req_item(req->addr, req, i, &req->data);
931 }
932 }
933 } else {
934 uint64_t tmp;
935
936 if (req->dir == IOREQ_READ) {
937 for (i = 0; i < req->count; i++) {
938 read_phys_req_item(req->addr, req, i, &tmp);
939 write_phys_req_item(req->data, req, i, &tmp);
940 }
941 } else if (req->dir == IOREQ_WRITE) {
942 for (i = 0; i < req->count; i++) {
943 read_phys_req_item(req->data, req, i, &tmp);
944 write_phys_req_item(req->addr, req, i, &tmp);
945 }
946 }
947 }
948}
949
950static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
951{
952 uint32_t sbdf = req->addr >> 32;
953 uint32_t reg = req->addr;
954 XenPciDevice *xendev;
955
956 if (req->size != sizeof(uint8_t) && req->size != sizeof(uint16_t) &&
957 req->size != sizeof(uint32_t)) {
958 hw_error("PCI config access: bad size (%u)", req->size);
959 }
960
961 if (req->count != 1) {
962 hw_error("PCI config access: bad count (%u)", req->count);
963 }
964
965 QLIST_FOREACH(xendev, &state->dev_list, entry) {
966 if (xendev->sbdf != sbdf) {
967 continue;
968 }
969
970 if (!req->data_is_ptr) {
971 if (req->dir == IOREQ_READ) {
972 req->data = pci_host_config_read_common(
973 xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
974 req->size);
975 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
976 req->size, req->data);
977 } else if (req->dir == IOREQ_WRITE) {
978 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
979 req->size, req->data);
980 pci_host_config_write_common(
981 xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
982 req->data, req->size);
983 }
984 } else {
985 uint32_t tmp;
986
987 if (req->dir == IOREQ_READ) {
988 tmp = pci_host_config_read_common(
989 xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
990 req->size);
991 trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
992 req->size, tmp);
993 write_phys_req_item(req->data, req, 0, &tmp);
994 } else if (req->dir == IOREQ_WRITE) {
995 read_phys_req_item(req->data, req, 0, &tmp);
996 trace_cpu_ioreq_config_write(req, xendev->sbdf, reg,
997 req->size, tmp);
998 pci_host_config_write_common(
999 xendev->pci_dev, reg, PCI_CONFIG_SPACE_SIZE,
1000 tmp, req->size);
1001 }
1002 }
1003 }
1004}
1005
1006static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
1007{
1008 X86CPU *cpu;
1009 CPUX86State *env;
1010
1011 cpu = X86_CPU(current_cpu);
1012 env = &cpu->env;
1013 env->regs[R_EAX] = req->data;
1014 env->regs[R_EBX] = vmport_regs->ebx;
1015 env->regs[R_ECX] = vmport_regs->ecx;
1016 env->regs[R_EDX] = vmport_regs->edx;
1017 env->regs[R_ESI] = vmport_regs->esi;
1018 env->regs[R_EDI] = vmport_regs->edi;
1019}
1020
1021static void regs_from_cpu(vmware_regs_t *vmport_regs)
1022{
1023 X86CPU *cpu = X86_CPU(current_cpu);
1024 CPUX86State *env = &cpu->env;
1025
1026 vmport_regs->ebx = env->regs[R_EBX];
1027 vmport_regs->ecx = env->regs[R_ECX];
1028 vmport_regs->edx = env->regs[R_EDX];
1029 vmport_regs->esi = env->regs[R_ESI];
1030 vmport_regs->edi = env->regs[R_EDI];
1031}
1032
1033static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
1034{
1035 vmware_regs_t *vmport_regs;
1036
1037 assert(state->shared_vmport_page);
1038 vmport_regs =
1039 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
1040 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
1041
1042 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
1043 regs_to_cpu(vmport_regs, req);
1044 cpu_ioreq_pio(req);
1045 regs_from_cpu(vmport_regs);
1046 current_cpu = NULL;
1047}
1048
1049static void handle_ioreq(XenIOState *state, ioreq_t *req)
1050{
1051 trace_handle_ioreq(req, req->type, req->dir, req->df, req->data_is_ptr,
1052 req->addr, req->data, req->count, req->size);
1053
1054 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
1055 (req->size < sizeof (target_ulong))) {
1056 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
1057 }
1058
1059 if (req->dir == IOREQ_WRITE)
1060 trace_handle_ioreq_write(req, req->type, req->df, req->data_is_ptr,
1061 req->addr, req->data, req->count, req->size);
1062
1063 switch (req->type) {
1064 case IOREQ_TYPE_PIO:
1065 cpu_ioreq_pio(req);
1066 break;
1067 case IOREQ_TYPE_COPY:
1068 cpu_ioreq_move(req);
1069 break;
1070 case IOREQ_TYPE_VMWARE_PORT:
1071 handle_vmport_ioreq(state, req);
1072 break;
1073 case IOREQ_TYPE_TIMEOFFSET:
1074 break;
1075 case IOREQ_TYPE_INVALIDATE:
1076 xen_invalidate_map_cache();
1077 break;
1078 case IOREQ_TYPE_PCI_CONFIG:
1079 cpu_ioreq_config(state, req);
1080 break;
1081 default:
1082 hw_error("Invalid ioreq type 0x%x\n", req->type);
1083 }
1084 if (req->dir == IOREQ_READ) {
1085 trace_handle_ioreq_read(req, req->type, req->df, req->data_is_ptr,
1086 req->addr, req->data, req->count, req->size);
1087 }
1088}
1089
1090static bool handle_buffered_iopage(XenIOState *state)
1091{
1092 buffered_iopage_t *buf_page = state->buffered_io_page;
1093 buf_ioreq_t *buf_req = NULL;
1094 bool handled_ioreq = false;
1095 ioreq_t req;
1096 int qw;
1097
1098 if (!buf_page) {
1099 return 0;
1100 }
1101
1102 memset(&req, 0x00, sizeof(req));
1103 req.state = STATE_IOREQ_READY;
1104 req.count = 1;
1105 req.dir = IOREQ_WRITE;
1106
1107 for (;;) {
1108 uint32_t rdptr = buf_page->read_pointer, wrptr;
1109
1110 xen_rmb();
1111 wrptr = buf_page->write_pointer;
1112 xen_rmb();
1113 if (rdptr != buf_page->read_pointer) {
1114 continue;
1115 }
1116 if (rdptr == wrptr) {
1117 break;
1118 }
1119 buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
1120 req.size = 1U << buf_req->size;
1121 req.addr = buf_req->addr;
1122 req.data = buf_req->data;
1123 req.type = buf_req->type;
1124 xen_rmb();
1125 qw = (req.size == 8);
1126 if (qw) {
1127 if (rdptr + 1 == wrptr) {
1128 hw_error("Incomplete quad word buffered ioreq");
1129 }
1130 buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
1131 IOREQ_BUFFER_SLOT_NUM];
1132 req.data |= ((uint64_t)buf_req->data) << 32;
1133 xen_rmb();
1134 }
1135
1136 handle_ioreq(state, &req);
1137
1138
1139
1140
1141
1142 assert(req.state == STATE_IOREQ_READY);
1143 assert(req.count == 1);
1144 assert(req.dir == IOREQ_WRITE);
1145 assert(!req.data_is_ptr);
1146
1147 qatomic_add(&buf_page->read_pointer, qw + 1);
1148 handled_ioreq = true;
1149 }
1150
1151 return handled_ioreq;
1152}
1153
1154static void handle_buffered_io(void *opaque)
1155{
1156 XenIOState *state = opaque;
1157
1158 if (handle_buffered_iopage(state)) {
1159 timer_mod(state->buffered_io_timer,
1160 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1161 } else {
1162 timer_del(state->buffered_io_timer);
1163 xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1164 }
1165}
1166
1167static void cpu_handle_ioreq(void *opaque)
1168{
1169 XenIOState *state = opaque;
1170 ioreq_t *req = cpu_get_ioreq(state);
1171
1172 handle_buffered_iopage(state);
1173 if (req) {
1174 ioreq_t copy = *req;
1175
1176 xen_rmb();
1177 handle_ioreq(state, ©);
1178 req->data = copy.data;
1179
1180 if (req->state != STATE_IOREQ_INPROCESS) {
1181 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1182 "%x, ptr: %x, port: %"PRIx64", "
1183 "data: %"PRIx64", count: %u, size: %u, type: %u\n",
1184 req->state, req->data_is_ptr, req->addr,
1185 req->data, req->count, req->size, req->type);
1186 destroy_hvm_domain(false);
1187 return;
1188 }
1189
1190 xen_wmb();
1191
1192
1193
1194
1195
1196
1197
1198 if (runstate_is_running()) {
1199 ShutdownCause request;
1200
1201 if (qemu_shutdown_requested_get()) {
1202 destroy_hvm_domain(false);
1203 }
1204 request = qemu_reset_requested_get();
1205 if (request) {
1206 qemu_system_reset(request);
1207 destroy_hvm_domain(true);
1208 }
1209 }
1210
1211 req->state = STATE_IORESP_READY;
1212 xenevtchn_notify(state->xce_handle,
1213 state->ioreq_local_port[state->send_vcpu]);
1214 }
1215}
1216
1217static void xen_main_loop_prepare(XenIOState *state)
1218{
1219 int evtchn_fd = -1;
1220
1221 if (state->xce_handle != NULL) {
1222 evtchn_fd = xenevtchn_fd(state->xce_handle);
1223 }
1224
1225 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1226 state);
1227
1228 if (evtchn_fd != -1) {
1229 CPUState *cpu_state;
1230
1231 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1232 CPU_FOREACH(cpu_state) {
1233 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1234 __func__, cpu_state->cpu_index, cpu_state);
1235 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1236 }
1237 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1238 }
1239}
1240
1241
1242static void xen_hvm_change_state_handler(void *opaque, bool running,
1243 RunState rstate)
1244{
1245 XenIOState *state = opaque;
1246
1247 if (running) {
1248 xen_main_loop_prepare(state);
1249 }
1250
1251 xen_set_ioreq_server_state(xen_domid,
1252 state->ioservid,
1253 (rstate == RUN_STATE_RUNNING));
1254}
1255
1256static void xen_exit_notifier(Notifier *n, void *data)
1257{
1258 XenIOState *state = container_of(n, XenIOState, exit);
1259
1260 xen_destroy_ioreq_server(xen_domid, state->ioservid);
1261 if (state->fres != NULL) {
1262 xenforeignmemory_unmap_resource(xen_fmem, state->fres);
1263 }
1264
1265 xenevtchn_close(state->xce_handle);
1266 xs_daemon_close(state->xenstore);
1267}
1268
1269#ifdef XEN_COMPAT_PHYSMAP
1270static void xen_read_physmap(XenIOState *state)
1271{
1272 XenPhysmap *physmap = NULL;
1273 unsigned int len, num, i;
1274 char path[80], *value = NULL;
1275 char **entries = NULL;
1276
1277 snprintf(path, sizeof(path),
1278 "/local/domain/0/device-model/%d/physmap", xen_domid);
1279 entries = xs_directory(state->xenstore, 0, path, &num);
1280 if (entries == NULL)
1281 return;
1282
1283 for (i = 0; i < num; i++) {
1284 physmap = g_new(XenPhysmap, 1);
1285 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1286 snprintf(path, sizeof(path),
1287 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1288 xen_domid, entries[i]);
1289 value = xs_read(state->xenstore, 0, path, &len);
1290 if (value == NULL) {
1291 g_free(physmap);
1292 continue;
1293 }
1294 physmap->start_addr = strtoull(value, NULL, 16);
1295 free(value);
1296
1297 snprintf(path, sizeof(path),
1298 "/local/domain/0/device-model/%d/physmap/%s/size",
1299 xen_domid, entries[i]);
1300 value = xs_read(state->xenstore, 0, path, &len);
1301 if (value == NULL) {
1302 g_free(physmap);
1303 continue;
1304 }
1305 physmap->size = strtoull(value, NULL, 16);
1306 free(value);
1307
1308 snprintf(path, sizeof(path),
1309 "/local/domain/0/device-model/%d/physmap/%s/name",
1310 xen_domid, entries[i]);
1311 physmap->name = xs_read(state->xenstore, 0, path, &len);
1312
1313 QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
1314 }
1315 free(entries);
1316}
1317#else
1318static void xen_read_physmap(XenIOState *state)
1319{
1320}
1321#endif
1322
1323static void xen_wakeup_notifier(Notifier *notifier, void *data)
1324{
1325 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1326}
1327
1328static int xen_map_ioreq_server(XenIOState *state)
1329{
1330 void *addr = NULL;
1331 xen_pfn_t ioreq_pfn;
1332 xen_pfn_t bufioreq_pfn;
1333 evtchn_port_t bufioreq_evtchn;
1334 int rc;
1335
1336
1337
1338
1339
1340 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_bufioreq != 0);
1341 QEMU_BUILD_BUG_ON(XENMEM_resource_ioreq_server_frame_ioreq(0) != 1);
1342 state->fres = xenforeignmemory_map_resource(xen_fmem, xen_domid,
1343 XENMEM_resource_ioreq_server,
1344 state->ioservid, 0, 2,
1345 &addr,
1346 PROT_READ | PROT_WRITE, 0);
1347 if (state->fres != NULL) {
1348 trace_xen_map_resource_ioreq(state->ioservid, addr);
1349 state->buffered_io_page = addr;
1350 state->shared_page = addr + TARGET_PAGE_SIZE;
1351 } else if (errno != EOPNOTSUPP) {
1352 error_report("failed to map ioreq server resources: error %d handle=%p",
1353 errno, xen_xc);
1354 return -1;
1355 }
1356
1357 rc = xen_get_ioreq_server_info(xen_domid, state->ioservid,
1358 (state->shared_page == NULL) ?
1359 &ioreq_pfn : NULL,
1360 (state->buffered_io_page == NULL) ?
1361 &bufioreq_pfn : NULL,
1362 &bufioreq_evtchn);
1363 if (rc < 0) {
1364 error_report("failed to get ioreq server info: error %d handle=%p",
1365 errno, xen_xc);
1366 return rc;
1367 }
1368
1369 if (state->shared_page == NULL) {
1370 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1371
1372 state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
1373 PROT_READ | PROT_WRITE,
1374 1, &ioreq_pfn, NULL);
1375 if (state->shared_page == NULL) {
1376 error_report("map shared IO page returned error %d handle=%p",
1377 errno, xen_xc);
1378 }
1379 }
1380
1381 if (state->buffered_io_page == NULL) {
1382 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1383
1384 state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
1385 PROT_READ | PROT_WRITE,
1386 1, &bufioreq_pfn,
1387 NULL);
1388 if (state->buffered_io_page == NULL) {
1389 error_report("map buffered IO page returned error %d", errno);
1390 return -1;
1391 }
1392 }
1393
1394 if (state->shared_page == NULL || state->buffered_io_page == NULL) {
1395 return -1;
1396 }
1397
1398 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1399
1400 state->bufioreq_remote_port = bufioreq_evtchn;
1401
1402 return 0;
1403}
1404
1405void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
1406{
1407 MachineState *ms = MACHINE(pcms);
1408 unsigned int max_cpus = ms->smp.max_cpus;
1409 int i, rc;
1410 xen_pfn_t ioreq_pfn;
1411 XenIOState *state;
1412
1413 state = g_new0(XenIOState, 1);
1414
1415 state->xce_handle = xenevtchn_open(NULL, 0);
1416 if (state->xce_handle == NULL) {
1417 perror("xen: event channel open");
1418 goto err;
1419 }
1420
1421 state->xenstore = xs_daemon_open();
1422 if (state->xenstore == NULL) {
1423 perror("xen: xenstore open");
1424 goto err;
1425 }
1426
1427 xen_create_ioreq_server(xen_domid, &state->ioservid);
1428
1429 state->exit.notify = xen_exit_notifier;
1430 qemu_add_exit_notifier(&state->exit);
1431
1432 state->suspend.notify = xen_suspend_notifier;
1433 qemu_register_suspend_notifier(&state->suspend);
1434
1435 state->wakeup.notify = xen_wakeup_notifier;
1436 qemu_register_wakeup_notifier(&state->wakeup);
1437
1438
1439
1440
1441 qemu_register_wakeup_support();
1442
1443 rc = xen_map_ioreq_server(state);
1444 if (rc < 0) {
1445 goto err;
1446 }
1447
1448 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1449 if (!rc) {
1450 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1451 state->shared_vmport_page =
1452 xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
1453 1, &ioreq_pfn, NULL);
1454 if (state->shared_vmport_page == NULL) {
1455 error_report("map shared vmport IO page returned error %d handle=%p",
1456 errno, xen_xc);
1457 goto err;
1458 }
1459 } else if (rc != -ENOSYS) {
1460 error_report("get vmport regs pfn returned error %d, rc=%d",
1461 errno, rc);
1462 goto err;
1463 }
1464
1465
1466 state->cpu_by_vcpu_id = g_new0(CPUState *, max_cpus);
1467
1468 rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
1469 if (rc < 0) {
1470 error_report("failed to enable ioreq server info: error %d handle=%p",
1471 errno, xen_xc);
1472 goto err;
1473 }
1474
1475 state->ioreq_local_port = g_new0(evtchn_port_t, max_cpus);
1476
1477
1478 for (i = 0; i < max_cpus; i++) {
1479 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1480 xen_vcpu_eport(state->shared_page, i));
1481 if (rc == -1) {
1482 error_report("shared evtchn %d bind error %d", i, errno);
1483 goto err;
1484 }
1485 state->ioreq_local_port[i] = rc;
1486 }
1487
1488 rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
1489 state->bufioreq_remote_port);
1490 if (rc == -1) {
1491 error_report("buffered evtchn bind error %d", errno);
1492 goto err;
1493 }
1494 state->bufioreq_local_port = rc;
1495
1496
1497#ifdef XEN_COMPAT_PHYSMAP
1498 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1499#else
1500 xen_map_cache_init(NULL, state);
1501#endif
1502 xen_ram_init(pcms, ms->ram_size, ram_memory);
1503
1504 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1505
1506 state->memory_listener = xen_memory_listener;
1507 memory_listener_register(&state->memory_listener, &address_space_memory);
1508 state->log_for_dirtybit = NULL;
1509
1510 state->io_listener = xen_io_listener;
1511 memory_listener_register(&state->io_listener, &address_space_io);
1512
1513 state->device_listener = xen_device_listener;
1514 QLIST_INIT(&state->dev_list);
1515 device_listener_register(&state->device_listener);
1516
1517 xen_bus_init();
1518
1519
1520 if (xen_be_init() != 0) {
1521 error_report("xen backend core setup failed");
1522 goto err;
1523 }
1524 xen_be_register_common();
1525
1526 QLIST_INIT(&xen_physmap);
1527 xen_read_physmap(state);
1528
1529
1530 pcms->acpi_build_enabled = false;
1531
1532 return;
1533
1534err:
1535 error_report("xen hardware virtual machine initialisation failed");
1536 exit(1);
1537}
1538
1539void destroy_hvm_domain(bool reboot)
1540{
1541 xc_interface *xc_handle;
1542 int sts;
1543 int rc;
1544
1545 unsigned int reason = reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff;
1546
1547 if (xen_dmod) {
1548 rc = xendevicemodel_shutdown(xen_dmod, xen_domid, reason);
1549 if (!rc) {
1550 return;
1551 }
1552 if (errno != ENOTTY ) {
1553 perror("xendevicemodel_shutdown failed");
1554 }
1555
1556 }
1557
1558 xc_handle = xc_interface_open(0, 0, 0);
1559 if (xc_handle == NULL) {
1560 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1561 } else {
1562 sts = xc_domain_shutdown(xc_handle, xen_domid, reason);
1563 if (sts != 0) {
1564 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1565 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1566 sts, strerror(errno));
1567 } else {
1568 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1569 reboot ? "reboot" : "poweroff");
1570 }
1571 xc_interface_close(xc_handle);
1572 }
1573}
1574
1575void xen_register_framebuffer(MemoryRegion *mr)
1576{
1577 framebuffer = mr;
1578}
1579
1580void xen_shutdown_fatal_error(const char *fmt, ...)
1581{
1582 va_list ap;
1583
1584 va_start(ap, fmt);
1585 vfprintf(stderr, fmt, ap);
1586 va_end(ap);
1587 fprintf(stderr, "Will destroy the domain.\n");
1588
1589 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
1590}
1591
1592void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
1593{
1594 if (unlikely(xen_in_migration)) {
1595 int rc;
1596 ram_addr_t start_pfn, nb_pages;
1597
1598 start = xen_phys_offset_to_gaddr(start, length);
1599
1600 if (length == 0) {
1601 length = TARGET_PAGE_SIZE;
1602 }
1603 start_pfn = start >> TARGET_PAGE_BITS;
1604 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1605 - start_pfn;
1606 rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
1607 if (rc) {
1608 fprintf(stderr,
1609 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1610 __func__, start, nb_pages, errno, strerror(errno));
1611 }
1612 }
1613}
1614
1615void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1616{
1617 if (enable) {
1618 memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION);
1619 } else {
1620 memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION);
1621 }
1622}
1623