]> git.proxmox.com Git - mirror_qemu.git/blame - xen-hvm.c
target-s390x: wire up I/O instructions in TCG mode
[mirror_qemu.git] / xen-hvm.c
CommitLineData
3285cf4f
AP
1/*
2 * Copyright (C) 2010 Citrix Ltd.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 *
6b620ca3
PB
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.
3285cf4f
AP
9 */
10
9ce94e7c
AS
11#include <sys/mman.h>
12
a2cb15b0 13#include "hw/pci/pci.h"
0d09e41a
PB
14#include "hw/i386/pc.h"
15#include "hw/xen/xen_common.h"
16#include "hw/xen/xen_backend.h"
39f42439 17#include "qmp-commands.h"
3285cf4f 18
dccfcd0e 19#include "sysemu/char.h"
1de7afc9 20#include "qemu/range.h"
9c17d615 21#include "sysemu/xen-mapcache.h"
432d268c 22#include "trace.h"
022c62cb 23#include "exec/address-spaces.h"
432d268c 24
9ce94e7c
AS
25#include <xen/hvm/ioreq.h>
26#include <xen/hvm/params.h>
8a369e20 27#include <xen/hvm/e820.h>
9ce94e7c 28
04b0de0e 29//#define DEBUG_XEN_HVM
9ce94e7c 30
04b0de0e 31#ifdef DEBUG_XEN_HVM
9ce94e7c
AS
32#define DPRINTF(fmt, ...) \
33 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
34#else
35#define DPRINTF(fmt, ...) \
36 do { } while (0)
37#endif
38
ce76b8a8 39static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
c65adf9b 40static MemoryRegion *framebuffer;
39f42439 41static bool xen_in_migration;
ce76b8a8 42
9ce94e7c 43/* Compatibility with older version */
37f9e258
DS
44
45/* This allows QEMU to build on a system that has Xen 4.5 or earlier
46 * installed. This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
47 * needs to be included before this block and hw/xen/xen_common.h needs to
48 * be included before xen/hvm/ioreq.h
49 */
50#ifndef IOREQ_TYPE_VMWARE_PORT
51#define IOREQ_TYPE_VMWARE_PORT 3
52struct vmware_regs {
53 uint32_t esi;
54 uint32_t edi;
55 uint32_t ebx;
56 uint32_t ecx;
57 uint32_t edx;
58};
59typedef struct vmware_regs vmware_regs_t;
60
61struct shared_vmport_iopage {
62 struct vmware_regs vcpu_vmport_regs[1];
63};
64typedef struct shared_vmport_iopage shared_vmport_iopage_t;
65#endif
66
9ce94e7c
AS
67#if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
68static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
69{
70 return shared_page->vcpu_iodata[i].vp_eport;
71}
72static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
73{
74 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
75}
76# define FMT_ioreq_size PRIx64
77#else
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# define FMT_ioreq_size "u"
87#endif
88
89#define BUFFER_IO_MAX_DELAY 100
c1d322e6
SS
90/* Leave some slack so that hvmloader does not complain about lack of
91 * memory at boot time ("Could not allocate order=0 extent").
92 * Once hvmloader is modified to cope with that situation without
93 * printing warning messages, QEMU_SPARE_PAGES can be removed.
94 */
95#define QEMU_SPARE_PAGES 16
9ce94e7c 96
b4dd7802 97typedef struct XenPhysmap {
a8170e5e 98 hwaddr start_addr;
b4dd7802 99 ram_addr_t size;
dc6c4fe8 100 const char *name;
a8170e5e 101 hwaddr phys_offset;
b4dd7802
AP
102
103 QLIST_ENTRY(XenPhysmap) list;
104} XenPhysmap;
105
9ce94e7c 106typedef struct XenIOState {
3996e85c 107 ioservid_t ioservid;
9ce94e7c 108 shared_iopage_t *shared_page;
37f9e258 109 shared_vmport_iopage_t *shared_vmport_page;
9ce94e7c
AS
110 buffered_iopage_t *buffered_io_page;
111 QEMUTimer *buffered_io_timer;
37f9e258 112 CPUState **cpu_by_vcpu_id;
9ce94e7c
AS
113 /* the evtchn port for polling the notification, */
114 evtchn_port_t *ioreq_local_port;
fda1f768
SS
115 /* evtchn local port for buffered io */
116 evtchn_port_t bufioreq_local_port;
9ce94e7c
AS
117 /* the evtchn fd for polling */
118 XenEvtchn xce_handle;
119 /* which vcpu we are serving */
120 int send_vcpu;
121
29321335 122 struct xs_handle *xenstore;
20581d20 123 MemoryListener memory_listener;
3996e85c
PD
124 MemoryListener io_listener;
125 DeviceListener device_listener;
b4dd7802 126 QLIST_HEAD(, XenPhysmap) physmap;
a8170e5e 127 hwaddr free_phys_offset;
b4dd7802 128 const XenPhysmap *log_for_dirtybit;
29321335 129
9ce94e7c 130 Notifier exit;
da98c8eb 131 Notifier suspend;
11addd0a 132 Notifier wakeup;
9ce94e7c
AS
133} XenIOState;
134
41445300
AP
135/* Xen specific function for piix pci */
136
137int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
138{
139 return irq_num + ((pci_dev->devfn >> 3) << 2);
140}
141
142void xen_piix3_set_irq(void *opaque, int irq_num, int level)
143{
144 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
145 irq_num & 3, level);
146}
147
148void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
149{
150 int i;
151
152 /* Scan for updates to PCI link routes (0x60-0x63). */
153 for (i = 0; i < len; i++) {
154 uint8_t v = (val >> (8 * i)) & 0xff;
155 if (v & 0x80) {
156 v = 0;
157 }
158 v &= 0xf;
159 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
160 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
161 }
162 }
163}
164
f1dbf015
WL
165void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
166{
4c9f8d1b 167 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
f1dbf015
WL
168}
169
da98c8eb 170static void xen_suspend_notifier(Notifier *notifier, void *data)
c9622478 171{
da98c8eb 172 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
c9622478
AP
173}
174
9c11a8ac
AP
175/* Xen Interrupt Controller */
176
177static void xen_set_irq(void *opaque, int irq, int level)
178{
179 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
180}
181
182qemu_irq *xen_interrupt_controller_init(void)
183{
184 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
185}
186
432d268c
JN
187/* Memory Ops */
188
3c2a9669
DS
189static void xen_ram_init(ram_addr_t *below_4g_mem_size,
190 ram_addr_t *above_4g_mem_size,
191 ram_addr_t ram_size, MemoryRegion **ram_memory_p)
432d268c 192{
ce76b8a8 193 MemoryRegion *sysmem = get_system_memory();
ce76b8a8 194 ram_addr_t block_len;
c4f5cdc5
DS
195 uint64_t user_lowmem = object_property_get_int(qdev_get_machine(),
196 PC_MACHINE_MAX_RAM_BELOW_4G,
197 &error_abort);
432d268c 198
a9dd38db 199 /* Handle the machine opt max-ram-below-4g. It is basically doing
c4f5cdc5
DS
200 * min(xen limit, user limit).
201 */
202 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
203 user_lowmem = HVM_BELOW_4G_RAM_END;
8a369e20 204 }
432d268c 205
c4f5cdc5
DS
206 if (ram_size >= user_lowmem) {
207 *above_4g_mem_size = ram_size - user_lowmem;
208 *below_4g_mem_size = user_lowmem;
432d268c 209 } else {
3c2a9669
DS
210 *above_4g_mem_size = 0;
211 *below_4g_mem_size = ram_size;
432d268c 212 }
c4f5cdc5
DS
213 if (!*above_4g_mem_size) {
214 block_len = ram_size;
215 } else {
216 /*
217 * Xen does not allocate the memory continuously, it keeps a
218 * hole of the size computed above or passed in.
219 */
220 block_len = (1ULL << 32) + *above_4g_mem_size;
221 }
49946538
HT
222 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
223 &error_abort);
c4f5cdc5
DS
224 *ram_memory_p = &ram_memory;
225 vmstate_register_ram_global(&ram_memory);
432d268c 226
2c9b15ca 227 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
ce76b8a8
AK
228 &ram_memory, 0, 0xa0000);
229 memory_region_add_subregion(sysmem, 0, &ram_640k);
8a369e20
AP
230 /* Skip of the VGA IO memory space, it will be registered later by the VGA
231 * emulated device.
232 *
233 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
234 * the Options ROM, so it is registered here as RAM.
235 */
2c9b15ca 236 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
3c2a9669
DS
237 &ram_memory, 0xc0000,
238 *below_4g_mem_size - 0xc0000);
ce76b8a8 239 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
3c2a9669 240 if (*above_4g_mem_size > 0) {
2c9b15ca 241 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
ce76b8a8 242 &ram_memory, 0x100000000ULL,
3c2a9669 243 *above_4g_mem_size);
ce76b8a8 244 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
432d268c 245 }
432d268c
JN
246}
247
fce537d4 248void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
432d268c
JN
249{
250 unsigned long nr_pfn;
251 xen_pfn_t *pfn_list;
252 int i;
c1d322e6
SS
253 xc_domaininfo_t info;
254 unsigned long free_pages;
432d268c 255
c234572d
AP
256 if (runstate_check(RUN_STATE_INMIGRATE)) {
257 /* RAM already populated in Xen */
258 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
259 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
260 __func__, size, ram_addr);
261 return;
262 }
263
ce76b8a8
AK
264 if (mr == &ram_memory) {
265 return;
266 }
267
432d268c
JN
268 trace_xen_ram_alloc(ram_addr, size);
269
270 nr_pfn = size >> TARGET_PAGE_BITS;
7267c094 271 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
432d268c
JN
272
273 for (i = 0; i < nr_pfn; i++) {
274 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
275 }
276
c1d322e6
SS
277 if ((xc_domain_getinfolist(xen_xc, xen_domid, 1, &info) != 1) ||
278 (info.domain != xen_domid)) {
279 hw_error("xc_domain_getinfolist failed");
280 }
281 free_pages = info.max_pages - info.tot_pages;
282 if (free_pages > QEMU_SPARE_PAGES) {
283 free_pages -= QEMU_SPARE_PAGES;
284 } else {
285 free_pages = 0;
286 }
287 if ((free_pages < nr_pfn) &&
288 (xc_domain_setmaxmem(xen_xc, xen_domid,
289 ((info.max_pages + nr_pfn - free_pages)
290 << (XC_PAGE_SHIFT - 10))) < 0)) {
291 hw_error("xc_domain_setmaxmem failed");
292 }
432d268c 293 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
f15fbc4b 294 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
432d268c
JN
295 }
296
7267c094 297 g_free(pfn_list);
432d268c
JN
298}
299
b4dd7802 300static XenPhysmap *get_physmapping(XenIOState *state,
a8170e5e 301 hwaddr start_addr, ram_addr_t size)
b4dd7802
AP
302{
303 XenPhysmap *physmap = NULL;
304
305 start_addr &= TARGET_PAGE_MASK;
306
307 QLIST_FOREACH(physmap, &state->physmap, list) {
308 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
309 return physmap;
310 }
311 }
312 return NULL;
313}
314
a8170e5e 315static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
cd1ba7de
AP
316 ram_addr_t size, void *opaque)
317{
a8170e5e 318 hwaddr addr = start_addr & TARGET_PAGE_MASK;
cd1ba7de
AP
319 XenIOState *xen_io_state = opaque;
320 XenPhysmap *physmap = NULL;
321
322 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
323 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
324 return physmap->start_addr;
325 }
326 }
327
328 return start_addr;
329}
330
b4dd7802
AP
331#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
332static int xen_add_to_physmap(XenIOState *state,
a8170e5e 333 hwaddr start_addr,
b4dd7802 334 ram_addr_t size,
20581d20 335 MemoryRegion *mr,
a8170e5e 336 hwaddr offset_within_region)
b4dd7802
AP
337{
338 unsigned long i = 0;
339 int rc = 0;
340 XenPhysmap *physmap = NULL;
a8170e5e
AK
341 hwaddr pfn, start_gpfn;
342 hwaddr phys_offset = memory_region_get_ram_addr(mr);
d1814e08 343 char path[80], value[17];
3e1f5086 344 const char *mr_name;
b4dd7802
AP
345
346 if (get_physmapping(state, start_addr, size)) {
347 return 0;
348 }
349 if (size <= 0) {
350 return -1;
351 }
352
ebed8505
SS
353 /* Xen can only handle a single dirty log region for now and we want
354 * the linear framebuffer to be that region.
355 * Avoid tracking any regions that is not videoram and avoid tracking
356 * the legacy vga region. */
20581d20
AK
357 if (mr == framebuffer && start_addr > 0xbffff) {
358 goto go_physmap;
ebed8505
SS
359 }
360 return -1;
361
362go_physmap:
f1b8caf1
SE
363 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
364 start_addr, start_addr + size);
b4dd7802
AP
365
366 pfn = phys_offset >> TARGET_PAGE_BITS;
367 start_gpfn = start_addr >> TARGET_PAGE_BITS;
368 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
369 unsigned long idx = pfn + i;
370 xen_pfn_t gpfn = start_gpfn + i;
371
372 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
373 if (rc) {
374 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
375 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
376 return -rc;
377 }
378 }
379
3e1f5086
PC
380 mr_name = memory_region_name(mr);
381
7267c094 382 physmap = g_malloc(sizeof (XenPhysmap));
b4dd7802
AP
383
384 physmap->start_addr = start_addr;
385 physmap->size = size;
3e1f5086 386 physmap->name = mr_name;
b4dd7802
AP
387 physmap->phys_offset = phys_offset;
388
389 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
390
391 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
392 start_addr >> TARGET_PAGE_BITS,
8b6bb0ad 393 (start_addr + size - 1) >> TARGET_PAGE_BITS,
b4dd7802 394 XEN_DOMCTL_MEM_CACHEATTR_WB);
d1814e08
SS
395
396 snprintf(path, sizeof(path),
397 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
398 xen_domid, (uint64_t)phys_offset);
399 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
400 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
401 return -1;
402 }
403 snprintf(path, sizeof(path),
404 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
405 xen_domid, (uint64_t)phys_offset);
406 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
407 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
408 return -1;
409 }
3e1f5086 410 if (mr_name) {
d1814e08
SS
411 snprintf(path, sizeof(path),
412 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
413 xen_domid, (uint64_t)phys_offset);
3e1f5086 414 if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
d1814e08
SS
415 return -1;
416 }
417 }
418
b4dd7802
AP
419 return 0;
420}
421
422static int xen_remove_from_physmap(XenIOState *state,
a8170e5e 423 hwaddr start_addr,
b4dd7802
AP
424 ram_addr_t size)
425{
426 unsigned long i = 0;
427 int rc = 0;
428 XenPhysmap *physmap = NULL;
a8170e5e 429 hwaddr phys_offset = 0;
b4dd7802
AP
430
431 physmap = get_physmapping(state, start_addr, size);
432 if (physmap == NULL) {
433 return -1;
434 }
435
436 phys_offset = physmap->phys_offset;
437 size = physmap->size;
438
d18e173a
WL
439 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
440 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
b4dd7802
AP
441
442 size >>= TARGET_PAGE_BITS;
443 start_addr >>= TARGET_PAGE_BITS;
444 phys_offset >>= TARGET_PAGE_BITS;
445 for (i = 0; i < size; i++) {
643f5932 446 xen_pfn_t idx = start_addr + i;
b4dd7802
AP
447 xen_pfn_t gpfn = phys_offset + i;
448
449 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
450 if (rc) {
451 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
452 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
453 return -rc;
454 }
455 }
456
457 QLIST_REMOVE(physmap, list);
458 if (state->log_for_dirtybit == physmap) {
459 state->log_for_dirtybit = NULL;
460 }
c5633d99 461 g_free(physmap);
b4dd7802
AP
462
463 return 0;
464}
465
466#else
467static int xen_add_to_physmap(XenIOState *state,
a8170e5e 468 hwaddr start_addr,
b4dd7802 469 ram_addr_t size,
20581d20 470 MemoryRegion *mr,
a8170e5e 471 hwaddr offset_within_region)
b4dd7802
AP
472{
473 return -ENOSYS;
474}
475
476static int xen_remove_from_physmap(XenIOState *state,
a8170e5e 477 hwaddr start_addr,
b4dd7802
AP
478 ram_addr_t size)
479{
480 return -ENOSYS;
481}
482#endif
483
20581d20
AK
484static void xen_set_memory(struct MemoryListener *listener,
485 MemoryRegionSection *section,
486 bool add)
b4dd7802 487{
20581d20 488 XenIOState *state = container_of(listener, XenIOState, memory_listener);
a8170e5e 489 hwaddr start_addr = section->offset_within_address_space;
052e87b0 490 ram_addr_t size = int128_get64(section->size);
2d1a35be 491 bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
b4dd7802
AP
492 hvmmem_type_t mem_type;
493
3996e85c
PD
494 if (section->mr == &ram_memory) {
495 return;
496 } else {
497 if (add) {
498 xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
499 section);
500 } else {
501 xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
502 section);
503 }
504 }
505
20581d20 506 if (!memory_region_is_ram(section->mr)) {
b4dd7802
AP
507 return;
508 }
509
3996e85c 510 if (log_dirty != add) {
20581d20
AK
511 return;
512 }
513
514 trace_xen_client_set_memory(start_addr, size, log_dirty);
b4dd7802
AP
515
516 start_addr &= TARGET_PAGE_MASK;
517 size = TARGET_PAGE_ALIGN(size);
20581d20
AK
518
519 if (add) {
520 if (!memory_region_is_rom(section->mr)) {
521 xen_add_to_physmap(state, start_addr, size,
522 section->mr, section->offset_within_region);
523 } else {
524 mem_type = HVMMEM_ram_ro;
525 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
526 start_addr >> TARGET_PAGE_BITS,
527 size >> TARGET_PAGE_BITS)) {
528 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
529 start_addr);
530 }
b4dd7802 531 }
20581d20 532 } else {
b4dd7802
AP
533 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
534 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
535 }
b4dd7802
AP
536 }
537}
538
20581d20
AK
539static void xen_region_add(MemoryListener *listener,
540 MemoryRegionSection *section)
541{
dfde4e6e 542 memory_region_ref(section->mr);
20581d20
AK
543 xen_set_memory(listener, section, true);
544}
545
546static void xen_region_del(MemoryListener *listener,
547 MemoryRegionSection *section)
548{
549 xen_set_memory(listener, section, false);
dfde4e6e 550 memory_region_unref(section->mr);
20581d20
AK
551}
552
3996e85c
PD
553static void xen_io_add(MemoryListener *listener,
554 MemoryRegionSection *section)
555{
556 XenIOState *state = container_of(listener, XenIOState, io_listener);
557
558 memory_region_ref(section->mr);
559
560 xen_map_io_section(xen_xc, xen_domid, state->ioservid, section);
561}
562
563static void xen_io_del(MemoryListener *listener,
564 MemoryRegionSection *section)
565{
566 XenIOState *state = container_of(listener, XenIOState, io_listener);
567
568 xen_unmap_io_section(xen_xc, xen_domid, state->ioservid, section);
569
570 memory_region_unref(section->mr);
571}
572
573static void xen_device_realize(DeviceListener *listener,
574 DeviceState *dev)
575{
576 XenIOState *state = container_of(listener, XenIOState, device_listener);
577
578 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
579 PCIDevice *pci_dev = PCI_DEVICE(dev);
580
581 xen_map_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
582 }
583}
584
585static void xen_device_unrealize(DeviceListener *listener,
586 DeviceState *dev)
587{
588 XenIOState *state = container_of(listener, XenIOState, device_listener);
589
590 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
591 PCIDevice *pci_dev = PCI_DEVICE(dev);
592
593 xen_unmap_pcidev(xen_xc, xen_domid, state->ioservid, pci_dev);
594 }
595}
596
b18620cf 597static void xen_sync_dirty_bitmap(XenIOState *state,
a8170e5e 598 hwaddr start_addr,
b18620cf 599 ram_addr_t size)
b4dd7802 600{
a8170e5e 601 hwaddr npages = size >> TARGET_PAGE_BITS;
b4dd7802
AP
602 const int width = sizeof(unsigned long) * 8;
603 unsigned long bitmap[(npages + width - 1) / width];
604 int rc, i, j;
605 const XenPhysmap *physmap = NULL;
606
607 physmap = get_physmapping(state, start_addr, size);
608 if (physmap == NULL) {
609 /* not handled */
b18620cf 610 return;
b4dd7802
AP
611 }
612
613 if (state->log_for_dirtybit == NULL) {
614 state->log_for_dirtybit = physmap;
615 } else if (state->log_for_dirtybit != physmap) {
b18620cf
AP
616 /* Only one range for dirty bitmap can be tracked. */
617 return;
b4dd7802 618 }
b4dd7802
AP
619
620 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
621 start_addr >> TARGET_PAGE_BITS, npages,
622 bitmap);
b18620cf 623 if (rc < 0) {
74bc4151
RPM
624#ifndef ENODATA
625#define ENODATA ENOENT
626#endif
627 if (errno == ENODATA) {
8aba7dc0
AP
628 memory_region_set_dirty(framebuffer, 0, size);
629 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
b18620cf 630 ", 0x" TARGET_FMT_plx "): %s\n",
74bc4151 631 start_addr, start_addr + size, strerror(errno));
b18620cf
AP
632 }
633 return;
b4dd7802
AP
634 }
635
636 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
637 unsigned long map = bitmap[i];
638 while (map != 0) {
adf9d70b 639 j = ctzl(map);
b4dd7802 640 map &= ~(1ul << j);
5a97065b 641 memory_region_set_dirty(framebuffer,
fd4aa979
BS
642 (i * width + j) * TARGET_PAGE_SIZE,
643 TARGET_PAGE_SIZE);
b4dd7802
AP
644 };
645 }
b4dd7802
AP
646}
647
20581d20 648static void xen_log_start(MemoryListener *listener,
b2dfd71c
PB
649 MemoryRegionSection *section,
650 int old, int new)
b4dd7802 651{
20581d20 652 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802 653
b2dfd71c
PB
654 if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
655 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
656 int128_get64(section->size));
657 }
b4dd7802
AP
658}
659
b2dfd71c
PB
660static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
661 int old, int new)
b4dd7802 662{
20581d20 663 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802 664
b2dfd71c
PB
665 if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
666 state->log_for_dirtybit = NULL;
667 /* Disable dirty bit tracking */
668 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
669 }
b4dd7802
AP
670}
671
20581d20 672static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
b4dd7802 673{
20581d20 674 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802 675
b18620cf 676 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
052e87b0 677 int128_get64(section->size));
b4dd7802
AP
678}
679
20581d20
AK
680static void xen_log_global_start(MemoryListener *listener)
681{
39f42439
AP
682 if (xen_enabled()) {
683 xen_in_migration = true;
684 }
20581d20
AK
685}
686
687static void xen_log_global_stop(MemoryListener *listener)
b4dd7802 688{
39f42439 689 xen_in_migration = false;
b4dd7802
AP
690}
691
20581d20
AK
692static MemoryListener xen_memory_listener = {
693 .region_add = xen_region_add,
694 .region_del = xen_region_del,
b4dd7802
AP
695 .log_start = xen_log_start,
696 .log_stop = xen_log_stop,
20581d20
AK
697 .log_sync = xen_log_sync,
698 .log_global_start = xen_log_global_start,
699 .log_global_stop = xen_log_global_stop,
72e22d2f 700 .priority = 10,
b4dd7802 701};
432d268c 702
3996e85c
PD
703static MemoryListener xen_io_listener = {
704 .region_add = xen_io_add,
705 .region_del = xen_io_del,
706 .priority = 10,
707};
708
709static DeviceListener xen_device_listener = {
710 .realize = xen_device_realize,
711 .unrealize = xen_device_unrealize,
712};
713
9ce94e7c
AS
714/* get the ioreq packets from share mem */
715static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
716{
717 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
718
719 if (req->state != STATE_IOREQ_READY) {
720 DPRINTF("I/O request not ready: "
721 "%x, ptr: %x, port: %"PRIx64", "
722 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
723 req->state, req->data_is_ptr, req->addr,
724 req->data, req->count, req->size);
725 return NULL;
726 }
727
728 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
729
730 req->state = STATE_IOREQ_INPROCESS;
731 return req;
732}
733
734/* use poll to get the port notification */
735/* ioreq_vec--out,the */
736/* retval--the number of ioreq packet */
737static ioreq_t *cpu_get_ioreq(XenIOState *state)
738{
739 int i;
740 evtchn_port_t port;
741
742 port = xc_evtchn_pending(state->xce_handle);
fda1f768 743 if (port == state->bufioreq_local_port) {
bc72ad67
AB
744 timer_mod(state->buffered_io_timer,
745 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
fda1f768
SS
746 return NULL;
747 }
748
9ce94e7c 749 if (port != -1) {
1cd25a88 750 for (i = 0; i < max_cpus; i++) {
9ce94e7c
AS
751 if (state->ioreq_local_port[i] == port) {
752 break;
753 }
754 }
755
1cd25a88 756 if (i == max_cpus) {
9ce94e7c
AS
757 hw_error("Fatal error while trying to get io event!\n");
758 }
759
760 /* unmask the wanted port again */
761 xc_evtchn_unmask(state->xce_handle, port);
762
763 /* get the io packet from shared memory */
764 state->send_vcpu = i;
765 return cpu_get_ioreq_from_shared_memory(state, i);
766 }
767
768 /* read error or read nothing */
769 return NULL;
770}
771
772static uint32_t do_inp(pio_addr_t addr, unsigned long size)
773{
774 switch (size) {
775 case 1:
776 return cpu_inb(addr);
777 case 2:
778 return cpu_inw(addr);
779 case 4:
780 return cpu_inl(addr);
781 default:
782 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
783 }
784}
785
786static void do_outp(pio_addr_t addr,
787 unsigned long size, uint32_t val)
788{
789 switch (size) {
790 case 1:
791 return cpu_outb(addr, val);
792 case 2:
793 return cpu_outw(addr, val);
794 case 4:
795 return cpu_outl(addr, val);
796 default:
797 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
798 }
799}
800
a3864829
IJ
801/*
802 * Helper functions which read/write an object from/to physical guest
803 * memory, as part of the implementation of an ioreq.
804 *
805 * Equivalent to
806 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
807 * val, req->size, 0/1)
808 * except without the integer overflow problems.
809 */
810static void rw_phys_req_item(hwaddr addr,
811 ioreq_t *req, uint32_t i, void *val, int rw)
812{
813 /* Do everything unsigned so overflow just results in a truncated result
814 * and accesses to undesired parts of guest memory, which is up
815 * to the guest */
816 hwaddr offset = (hwaddr)req->size * i;
817 if (req->df) {
818 addr -= offset;
819 } else {
820 addr += offset;
821 }
822 cpu_physical_memory_rw(addr, val, req->size, rw);
823}
824
825static inline void read_phys_req_item(hwaddr addr,
826 ioreq_t *req, uint32_t i, void *val)
9ce94e7c 827{
a3864829
IJ
828 rw_phys_req_item(addr, req, i, val, 0);
829}
830static inline void write_phys_req_item(hwaddr addr,
831 ioreq_t *req, uint32_t i, void *val)
832{
833 rw_phys_req_item(addr, req, i, val, 1);
834}
9ce94e7c 835
a3864829
IJ
836
837static void cpu_ioreq_pio(ioreq_t *req)
838{
249e7e0f 839 uint32_t i;
9ce94e7c
AS
840
841 if (req->dir == IOREQ_READ) {
842 if (!req->data_is_ptr) {
843 req->data = do_inp(req->addr, req->size);
844 } else {
845 uint32_t tmp;
846
847 for (i = 0; i < req->count; i++) {
848 tmp = do_inp(req->addr, req->size);
a3864829 849 write_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
850 }
851 }
852 } else if (req->dir == IOREQ_WRITE) {
853 if (!req->data_is_ptr) {
854 do_outp(req->addr, req->size, req->data);
855 } else {
856 for (i = 0; i < req->count; i++) {
857 uint32_t tmp = 0;
858
a3864829 859 read_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
860 do_outp(req->addr, req->size, tmp);
861 }
862 }
863 }
864}
865
866static void cpu_ioreq_move(ioreq_t *req)
867{
249e7e0f 868 uint32_t i;
9ce94e7c
AS
869
870 if (!req->data_is_ptr) {
871 if (req->dir == IOREQ_READ) {
872 for (i = 0; i < req->count; i++) {
a3864829 873 read_phys_req_item(req->addr, req, i, &req->data);
9ce94e7c
AS
874 }
875 } else if (req->dir == IOREQ_WRITE) {
876 for (i = 0; i < req->count; i++) {
a3864829 877 write_phys_req_item(req->addr, req, i, &req->data);
9ce94e7c
AS
878 }
879 }
880 } else {
2b734340 881 uint64_t tmp;
9ce94e7c
AS
882
883 if (req->dir == IOREQ_READ) {
884 for (i = 0; i < req->count; i++) {
a3864829
IJ
885 read_phys_req_item(req->addr, req, i, &tmp);
886 write_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
887 }
888 } else if (req->dir == IOREQ_WRITE) {
889 for (i = 0; i < req->count; i++) {
a3864829
IJ
890 read_phys_req_item(req->data, req, i, &tmp);
891 write_phys_req_item(req->addr, req, i, &tmp);
9ce94e7c
AS
892 }
893 }
894 }
895}
896
37f9e258
DS
897static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
898{
899 X86CPU *cpu;
900 CPUX86State *env;
901
902 cpu = X86_CPU(current_cpu);
903 env = &cpu->env;
904 env->regs[R_EAX] = req->data;
905 env->regs[R_EBX] = vmport_regs->ebx;
906 env->regs[R_ECX] = vmport_regs->ecx;
907 env->regs[R_EDX] = vmport_regs->edx;
908 env->regs[R_ESI] = vmport_regs->esi;
909 env->regs[R_EDI] = vmport_regs->edi;
910}
911
912static void regs_from_cpu(vmware_regs_t *vmport_regs)
913{
914 X86CPU *cpu = X86_CPU(current_cpu);
915 CPUX86State *env = &cpu->env;
916
917 vmport_regs->ebx = env->regs[R_EBX];
918 vmport_regs->ecx = env->regs[R_ECX];
919 vmport_regs->edx = env->regs[R_EDX];
920 vmport_regs->esi = env->regs[R_ESI];
921 vmport_regs->edi = env->regs[R_EDI];
922}
923
924static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
925{
926 vmware_regs_t *vmport_regs;
927
928 assert(state->shared_vmport_page);
929 vmport_regs =
930 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
931 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
932
933 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
934 regs_to_cpu(vmport_regs, req);
935 cpu_ioreq_pio(req);
936 regs_from_cpu(vmport_regs);
937 current_cpu = NULL;
938}
939
940static void handle_ioreq(XenIOState *state, ioreq_t *req)
9ce94e7c
AS
941{
942 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
943 (req->size < sizeof (target_ulong))) {
944 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
945 }
946
947 switch (req->type) {
948 case IOREQ_TYPE_PIO:
949 cpu_ioreq_pio(req);
950 break;
951 case IOREQ_TYPE_COPY:
952 cpu_ioreq_move(req);
953 break;
37f9e258
DS
954 case IOREQ_TYPE_VMWARE_PORT:
955 handle_vmport_ioreq(state, req);
956 break;
9ce94e7c
AS
957 case IOREQ_TYPE_TIMEOFFSET:
958 break;
959 case IOREQ_TYPE_INVALIDATE:
e41d7c69 960 xen_invalidate_map_cache();
9ce94e7c 961 break;
3996e85c
PD
962 case IOREQ_TYPE_PCI_CONFIG: {
963 uint32_t sbdf = req->addr >> 32;
964 uint32_t val;
965
966 /* Fake a write to port 0xCF8 so that
967 * the config space access will target the
968 * correct device model.
969 */
970 val = (1u << 31) |
971 ((req->addr & 0x0f00) << 16) |
972 ((sbdf & 0xffff) << 8) |
973 (req->addr & 0xfc);
974 do_outp(0xcf8, 4, val);
975
976 /* Now issue the config space access via
977 * port 0xCFC
978 */
979 req->addr = 0xcfc | (req->addr & 0x03);
980 cpu_ioreq_pio(req);
981 break;
982 }
9ce94e7c
AS
983 default:
984 hw_error("Invalid ioreq type 0x%x\n", req->type);
985 }
986}
987
fda1f768 988static int handle_buffered_iopage(XenIOState *state)
9ce94e7c
AS
989{
990 buf_ioreq_t *buf_req = NULL;
991 ioreq_t req;
992 int qw;
993
994 if (!state->buffered_io_page) {
fda1f768 995 return 0;
9ce94e7c
AS
996 }
997
fda1f768
SS
998 memset(&req, 0x00, sizeof(req));
999
9ce94e7c
AS
1000 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
1001 buf_req = &state->buffered_io_page->buf_ioreq[
1002 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
1003 req.size = 1UL << buf_req->size;
1004 req.count = 1;
1005 req.addr = buf_req->addr;
1006 req.data = buf_req->data;
1007 req.state = STATE_IOREQ_READY;
1008 req.dir = buf_req->dir;
1009 req.df = 1;
1010 req.type = buf_req->type;
1011 req.data_is_ptr = 0;
1012 qw = (req.size == 8);
1013 if (qw) {
1014 buf_req = &state->buffered_io_page->buf_ioreq[
1015 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
1016 req.data |= ((uint64_t)buf_req->data) << 32;
1017 }
1018
37f9e258 1019 handle_ioreq(state, &req);
9ce94e7c
AS
1020
1021 xen_mb();
1022 state->buffered_io_page->read_pointer += qw ? 2 : 1;
1023 }
fda1f768
SS
1024
1025 return req.count;
9ce94e7c
AS
1026}
1027
1028static void handle_buffered_io(void *opaque)
1029{
1030 XenIOState *state = opaque;
1031
fda1f768 1032 if (handle_buffered_iopage(state)) {
bc72ad67
AB
1033 timer_mod(state->buffered_io_timer,
1034 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
fda1f768 1035 } else {
bc72ad67 1036 timer_del(state->buffered_io_timer);
fda1f768
SS
1037 xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
1038 }
9ce94e7c
AS
1039}
1040
1041static void cpu_handle_ioreq(void *opaque)
1042{
1043 XenIOState *state = opaque;
1044 ioreq_t *req = cpu_get_ioreq(state);
1045
1046 handle_buffered_iopage(state);
1047 if (req) {
37f9e258 1048 handle_ioreq(state, req);
9ce94e7c
AS
1049
1050 if (req->state != STATE_IOREQ_INPROCESS) {
1051 fprintf(stderr, "Badness in I/O request ... not in service?!: "
1052 "%x, ptr: %x, port: %"PRIx64", "
37f9e258
DS
1053 "data: %"PRIx64", count: %" FMT_ioreq_size
1054 ", size: %" FMT_ioreq_size
1055 ", type: %"FMT_ioreq_size"\n",
9ce94e7c 1056 req->state, req->data_is_ptr, req->addr,
37f9e258 1057 req->data, req->count, req->size, req->type);
180640ea 1058 destroy_hvm_domain(false);
9ce94e7c
AS
1059 return;
1060 }
1061
1062 xen_wmb(); /* Update ioreq contents /then/ update state. */
1063
1064 /*
1065 * We do this before we send the response so that the tools
1066 * have the opportunity to pick up on the reset before the
1067 * guest resumes and does a hlt with interrupts disabled which
1068 * causes Xen to powerdown the domain.
1069 */
1354869c 1070 if (runstate_is_running()) {
9ce94e7c 1071 if (qemu_shutdown_requested_get()) {
180640ea 1072 destroy_hvm_domain(false);
9ce94e7c
AS
1073 }
1074 if (qemu_reset_requested_get()) {
e063eb1f 1075 qemu_system_reset(VMRESET_REPORT);
180640ea 1076 destroy_hvm_domain(true);
9ce94e7c
AS
1077 }
1078 }
1079
1080 req->state = STATE_IORESP_READY;
1081 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
1082 }
1083}
1084
1085static void xen_main_loop_prepare(XenIOState *state)
1086{
1087 int evtchn_fd = -1;
1088
1089 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
1090 evtchn_fd = xc_evtchn_fd(state->xce_handle);
1091 }
1092
bc72ad67 1093 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
9ce94e7c 1094 state);
9ce94e7c
AS
1095
1096 if (evtchn_fd != -1) {
37f9e258
DS
1097 CPUState *cpu_state;
1098
1099 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1100 CPU_FOREACH(cpu_state) {
1101 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1102 __func__, cpu_state->cpu_index, cpu_state);
1103 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1104 }
9ce94e7c
AS
1105 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1106 }
1107}
1108
1109
1dfb4dd9
LC
1110static void xen_hvm_change_state_handler(void *opaque, int running,
1111 RunState rstate)
9ce94e7c 1112{
3996e85c
PD
1113 XenIOState *state = opaque;
1114
9ce94e7c 1115 if (running) {
3996e85c 1116 xen_main_loop_prepare(state);
9ce94e7c 1117 }
3996e85c
PD
1118
1119 xen_set_ioreq_server_state(xen_xc, xen_domid,
1120 state->ioservid,
1121 (rstate == RUN_STATE_RUNNING));
9ce94e7c
AS
1122}
1123
9e8dd451 1124static void xen_exit_notifier(Notifier *n, void *data)
9ce94e7c
AS
1125{
1126 XenIOState *state = container_of(n, XenIOState, exit);
1127
1128 xc_evtchn_close(state->xce_handle);
29321335 1129 xs_daemon_close(state->xenstore);
9ce94e7c
AS
1130}
1131
d1814e08
SS
1132static void xen_read_physmap(XenIOState *state)
1133{
1134 XenPhysmap *physmap = NULL;
1135 unsigned int len, num, i;
1136 char path[80], *value = NULL;
1137 char **entries = NULL;
1138
1139 snprintf(path, sizeof(path),
1140 "/local/domain/0/device-model/%d/physmap", xen_domid);
1141 entries = xs_directory(state->xenstore, 0, path, &num);
1142 if (entries == NULL)
1143 return;
1144
1145 for (i = 0; i < num; i++) {
1146 physmap = g_malloc(sizeof (XenPhysmap));
1147 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1148 snprintf(path, sizeof(path),
1149 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1150 xen_domid, entries[i]);
1151 value = xs_read(state->xenstore, 0, path, &len);
1152 if (value == NULL) {
c5633d99 1153 g_free(physmap);
d1814e08
SS
1154 continue;
1155 }
1156 physmap->start_addr = strtoull(value, NULL, 16);
1157 free(value);
1158
1159 snprintf(path, sizeof(path),
1160 "/local/domain/0/device-model/%d/physmap/%s/size",
1161 xen_domid, entries[i]);
1162 value = xs_read(state->xenstore, 0, path, &len);
1163 if (value == NULL) {
c5633d99 1164 g_free(physmap);
d1814e08
SS
1165 continue;
1166 }
1167 physmap->size = strtoull(value, NULL, 16);
1168 free(value);
1169
1170 snprintf(path, sizeof(path),
1171 "/local/domain/0/device-model/%d/physmap/%s/name",
1172 xen_domid, entries[i]);
1173 physmap->name = xs_read(state->xenstore, 0, path, &len);
1174
1175 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1176 }
1177 free(entries);
d1814e08
SS
1178}
1179
11addd0a
LJ
1180static void xen_wakeup_notifier(Notifier *notifier, void *data)
1181{
1182 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1183}
1184
6c5b0c0a 1185/* return 0 means OK, or -1 means critical issue -- will exit(1) */
3c2a9669
DS
1186int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
1187 MemoryRegion **ram_memory)
29d3ccde 1188{
9ce94e7c 1189 int i, rc;
3996e85c
PD
1190 xen_pfn_t ioreq_pfn;
1191 xen_pfn_t bufioreq_pfn;
1192 evtchn_port_t bufioreq_evtchn;
9ce94e7c
AS
1193 XenIOState *state;
1194
7267c094 1195 state = g_malloc0(sizeof (XenIOState));
9ce94e7c
AS
1196
1197 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
1198 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
1199 perror("xen: event channel open");
6c5b0c0a 1200 return -1;
9ce94e7c
AS
1201 }
1202
29321335
AP
1203 state->xenstore = xs_daemon_open();
1204 if (state->xenstore == NULL) {
1205 perror("xen: xenstore open");
6c5b0c0a 1206 return -1;
29321335
AP
1207 }
1208
3996e85c
PD
1209 rc = xen_create_ioreq_server(xen_xc, xen_domid, &state->ioservid);
1210 if (rc < 0) {
1211 perror("xen: ioreq server create");
1212 return -1;
1213 }
1214
9ce94e7c
AS
1215 state->exit.notify = xen_exit_notifier;
1216 qemu_add_exit_notifier(&state->exit);
1217
da98c8eb
GH
1218 state->suspend.notify = xen_suspend_notifier;
1219 qemu_register_suspend_notifier(&state->suspend);
1220
11addd0a
LJ
1221 state->wakeup.notify = xen_wakeup_notifier;
1222 qemu_register_wakeup_notifier(&state->wakeup);
1223
3996e85c
PD
1224 rc = xen_get_ioreq_server_info(xen_xc, xen_domid, state->ioservid,
1225 &ioreq_pfn, &bufioreq_pfn,
1226 &bufioreq_evtchn);
1227 if (rc < 0) {
1228 hw_error("failed to get ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1229 errno, xen_xc);
1230 }
1231
9ce94e7c 1232 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
3996e85c
PD
1233 DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
1234 DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
1235
9ce94e7c
AS
1236 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1237 PROT_READ|PROT_WRITE, ioreq_pfn);
1238 if (state->shared_page == NULL) {
1239 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1240 errno, xen_xc);
1241 }
1242
37f9e258
DS
1243 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1244 if (!rc) {
1245 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1246 state->shared_vmport_page =
1247 xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1248 PROT_READ|PROT_WRITE, ioreq_pfn);
1249 if (state->shared_vmport_page == NULL) {
1250 hw_error("map shared vmport IO page returned error %d handle="
1251 XC_INTERFACE_FMT, errno, xen_xc);
1252 }
1253 } else if (rc != -ENOSYS) {
1254 hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
1255 }
1256
3996e85c
PD
1257 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid,
1258 XC_PAGE_SIZE,
1259 PROT_READ|PROT_WRITE,
1260 bufioreq_pfn);
9ce94e7c
AS
1261 if (state->buffered_io_page == NULL) {
1262 hw_error("map buffered IO page returned error %d", errno);
1263 }
1264
37f9e258
DS
1265 /* Note: cpus is empty at this point in init */
1266 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1267
3996e85c
PD
1268 rc = xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, true);
1269 if (rc < 0) {
1270 hw_error("failed to enable ioreq server info: error %d handle=" XC_INTERFACE_FMT,
1271 errno, xen_xc);
1272 }
1273
1cd25a88 1274 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
9ce94e7c
AS
1275
1276 /* FIXME: how about if we overflow the page here? */
1cd25a88 1277 for (i = 0; i < max_cpus; i++) {
9ce94e7c
AS
1278 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1279 xen_vcpu_eport(state->shared_page, i));
1280 if (rc == -1) {
3996e85c 1281 fprintf(stderr, "shared evtchn %d bind error %d\n", i, errno);
9ce94e7c
AS
1282 return -1;
1283 }
1284 state->ioreq_local_port[i] = rc;
1285 }
1286
fda1f768 1287 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
3996e85c 1288 bufioreq_evtchn);
fda1f768 1289 if (rc == -1) {
3996e85c 1290 fprintf(stderr, "buffered evtchn bind error %d\n", errno);
fda1f768
SS
1291 return -1;
1292 }
1293 state->bufioreq_local_port = rc;
1294
432d268c 1295 /* Init RAM management */
cd1ba7de 1296 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
3c2a9669 1297 xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
432d268c 1298
fb4bb2b5 1299 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
9ce94e7c 1300
20581d20 1301 state->memory_listener = xen_memory_listener;
b4dd7802 1302 QLIST_INIT(&state->physmap);
f6790af6 1303 memory_listener_register(&state->memory_listener, &address_space_memory);
b4dd7802
AP
1304 state->log_for_dirtybit = NULL;
1305
3996e85c
PD
1306 state->io_listener = xen_io_listener;
1307 memory_listener_register(&state->io_listener, &address_space_io);
1308
1309 state->device_listener = xen_device_listener;
1310 device_listener_register(&state->device_listener);
1311
ad35a7da
SS
1312 /* Initialize backend core & drivers */
1313 if (xen_be_init() != 0) {
1314 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
6c5b0c0a 1315 return -1;
ad35a7da
SS
1316 }
1317 xen_be_register("console", &xen_console_ops);
37cdfcf1 1318 xen_be_register("vkbd", &xen_kbdmouse_ops);
ad35a7da 1319 xen_be_register("qdisk", &xen_blkdev_ops);
d1814e08 1320 xen_read_physmap(state);
ad35a7da 1321
29d3ccde
AP
1322 return 0;
1323}
9ce94e7c 1324
180640ea 1325void destroy_hvm_domain(bool reboot)
9ce94e7c
AS
1326{
1327 XenXC xc_handle;
1328 int sts;
1329
1330 xc_handle = xen_xc_interface_open(0, 0, 0);
1331 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1332 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1333 } else {
180640ea
JB
1334 sts = xc_domain_shutdown(xc_handle, xen_domid,
1335 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
9ce94e7c 1336 if (sts != 0) {
180640ea
JB
1337 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1338 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1339 sts, strerror(errno));
9ce94e7c 1340 } else {
180640ea
JB
1341 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1342 reboot ? "reboot" : "poweroff");
9ce94e7c
AS
1343 }
1344 xc_interface_close(xc_handle);
1345 }
1346}
c65adf9b
AK
1347
1348void xen_register_framebuffer(MemoryRegion *mr)
1349{
1350 framebuffer = mr;
1351}
eaab4d60
AK
1352
1353void xen_shutdown_fatal_error(const char *fmt, ...)
1354{
1355 va_list ap;
1356
1357 va_start(ap, fmt);
1358 vfprintf(stderr, fmt, ap);
1359 va_end(ap);
1360 fprintf(stderr, "Will destroy the domain.\n");
1361 /* destroy the domain */
1362 qemu_system_shutdown_request();
1363}
910b38e4
AP
1364
1365void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1366{
1367 if (unlikely(xen_in_migration)) {
1368 int rc;
1369 ram_addr_t start_pfn, nb_pages;
1370
1371 if (length == 0) {
1372 length = TARGET_PAGE_SIZE;
1373 }
1374 start_pfn = start >> TARGET_PAGE_BITS;
1375 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1376 - start_pfn;
1377 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1378 if (rc) {
1379 fprintf(stderr,
1380 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1381 __func__, start, nb_pages, rc, strerror(-rc));
1382 }
1383 }
1384}
04b0de0e
WL
1385
1386void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1387{
1388 if (enable) {
1389 memory_global_dirty_log_start();
1390 } else {
1391 memory_global_dirty_log_stop();
1392 }
1393}