]> git.ipfire.org Git - thirdparty/qemu.git/blob - xen-hvm.c
Update version for v2.2.1 release
[thirdparty/qemu.git] / xen-hvm.c
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 *
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.
9 */
10
11 #include <sys/mman.h>
12
13 #include "hw/pci/pci.h"
14 #include "hw/i386/pc.h"
15 #include "hw/xen/xen_common.h"
16 #include "hw/xen/xen_backend.h"
17 #include "qmp-commands.h"
18
19 #include "sysemu/char.h"
20 #include "qemu/range.h"
21 #include "sysemu/xen-mapcache.h"
22 #include "trace.h"
23 #include "exec/address-spaces.h"
24
25 #include <xen/hvm/ioreq.h>
26 #include <xen/hvm/params.h>
27 #include <xen/hvm/e820.h>
28
29 //#define DEBUG_XEN_HVM
30
31 #ifdef DEBUG_XEN_HVM
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
39 static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
40 static MemoryRegion *framebuffer;
41 static bool xen_in_migration;
42
43 /* Compatibility with older version */
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
52 struct vmware_regs {
53 uint32_t esi;
54 uint32_t edi;
55 uint32_t ebx;
56 uint32_t ecx;
57 uint32_t edx;
58 };
59 typedef struct vmware_regs vmware_regs_t;
60
61 struct shared_vmport_iopage {
62 struct vmware_regs vcpu_vmport_regs[1];
63 };
64 typedef struct shared_vmport_iopage shared_vmport_iopage_t;
65 #endif
66
67 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
68 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
69 {
70 return shared_page->vcpu_iodata[i].vp_eport;
71 }
72 static 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
78 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
79 {
80 return shared_page->vcpu_ioreq[i].vp_eport;
81 }
82 static 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 #ifndef HVM_PARAM_BUFIOREQ_EVTCHN
89 #define HVM_PARAM_BUFIOREQ_EVTCHN 26
90 #endif
91
92 #define BUFFER_IO_MAX_DELAY 100
93 /* Leave some slack so that hvmloader does not complain about lack of
94 * memory at boot time ("Could not allocate order=0 extent").
95 * Once hvmloader is modified to cope with that situation without
96 * printing warning messages, QEMU_SPARE_PAGES can be removed.
97 */
98 #define QEMU_SPARE_PAGES 16
99
100 typedef struct XenPhysmap {
101 hwaddr start_addr;
102 ram_addr_t size;
103 const char *name;
104 hwaddr phys_offset;
105
106 QLIST_ENTRY(XenPhysmap) list;
107 } XenPhysmap;
108
109 typedef struct XenIOState {
110 shared_iopage_t *shared_page;
111 shared_vmport_iopage_t *shared_vmport_page;
112 buffered_iopage_t *buffered_io_page;
113 QEMUTimer *buffered_io_timer;
114 CPUState **cpu_by_vcpu_id;
115 /* the evtchn port for polling the notification, */
116 evtchn_port_t *ioreq_local_port;
117 /* evtchn local port for buffered io */
118 evtchn_port_t bufioreq_local_port;
119 /* the evtchn fd for polling */
120 XenEvtchn xce_handle;
121 /* which vcpu we are serving */
122 int send_vcpu;
123
124 struct xs_handle *xenstore;
125 MemoryListener memory_listener;
126 QLIST_HEAD(, XenPhysmap) physmap;
127 hwaddr free_phys_offset;
128 const XenPhysmap *log_for_dirtybit;
129
130 Notifier exit;
131 Notifier suspend;
132 Notifier wakeup;
133 } XenIOState;
134
135 /* Xen specific function for piix pci */
136
137 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
138 {
139 return irq_num + ((pci_dev->devfn >> 3) << 2);
140 }
141
142 void 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
148 void 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
165 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
166 {
167 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
168 }
169
170 static void xen_suspend_notifier(Notifier *notifier, void *data)
171 {
172 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
173 }
174
175 /* Xen Interrupt Controller */
176
177 static 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
182 qemu_irq *xen_interrupt_controller_init(void)
183 {
184 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
185 }
186
187 /* Memory Ops */
188
189 static 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)
192 {
193 MemoryRegion *sysmem = get_system_memory();
194 ram_addr_t block_len;
195 uint64_t user_lowmem = object_property_get_int(qdev_get_machine(),
196 PC_MACHINE_MAX_RAM_BELOW_4G,
197 &error_abort);
198
199 /* Handle the machine opt max-ram-below-4g. It is basically doing
200 * min(xen limit, user limit).
201 */
202 if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
203 user_lowmem = HVM_BELOW_4G_RAM_END;
204 }
205
206 if (ram_size >= user_lowmem) {
207 *above_4g_mem_size = ram_size - user_lowmem;
208 *below_4g_mem_size = user_lowmem;
209 } else {
210 *above_4g_mem_size = 0;
211 *below_4g_mem_size = ram_size;
212 }
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 }
222 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
223 &error_abort);
224 *ram_memory_p = &ram_memory;
225 vmstate_register_ram_global(&ram_memory);
226
227 memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
228 &ram_memory, 0, 0xa0000);
229 memory_region_add_subregion(sysmem, 0, &ram_640k);
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 */
236 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
237 &ram_memory, 0xc0000,
238 *below_4g_mem_size - 0xc0000);
239 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
240 if (*above_4g_mem_size > 0) {
241 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
242 &ram_memory, 0x100000000ULL,
243 *above_4g_mem_size);
244 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
245 }
246 }
247
248 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
249 {
250 unsigned long nr_pfn;
251 xen_pfn_t *pfn_list;
252 int i;
253 xc_domaininfo_t info;
254 unsigned long free_pages;
255
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
264 if (mr == &ram_memory) {
265 return;
266 }
267
268 trace_xen_ram_alloc(ram_addr, size);
269
270 nr_pfn = size >> TARGET_PAGE_BITS;
271 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
272
273 for (i = 0; i < nr_pfn; i++) {
274 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
275 }
276
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 }
293 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
294 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
295 }
296
297 g_free(pfn_list);
298 }
299
300 static XenPhysmap *get_physmapping(XenIOState *state,
301 hwaddr start_addr, ram_addr_t size)
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
315 static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
316 ram_addr_t size, void *opaque)
317 {
318 hwaddr addr = start_addr & TARGET_PAGE_MASK;
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
331 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
332 static int xen_add_to_physmap(XenIOState *state,
333 hwaddr start_addr,
334 ram_addr_t size,
335 MemoryRegion *mr,
336 hwaddr offset_within_region)
337 {
338 unsigned long i = 0;
339 int rc = 0;
340 XenPhysmap *physmap = NULL;
341 hwaddr pfn, start_gpfn;
342 hwaddr phys_offset = memory_region_get_ram_addr(mr);
343 char path[80], value[17];
344 const char *mr_name;
345
346 if (get_physmapping(state, start_addr, size)) {
347 return 0;
348 }
349 if (size <= 0) {
350 return -1;
351 }
352
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. */
357 if (mr == framebuffer && start_addr > 0xbffff) {
358 goto go_physmap;
359 }
360 return -1;
361
362 go_physmap:
363 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
364 start_addr, start_addr + size);
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
380 mr_name = memory_region_name(mr);
381
382 physmap = g_malloc(sizeof (XenPhysmap));
383
384 physmap->start_addr = start_addr;
385 physmap->size = size;
386 physmap->name = mr_name;
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,
393 (start_addr + size - 1) >> TARGET_PAGE_BITS,
394 XEN_DOMCTL_MEM_CACHEATTR_WB);
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 }
410 if (mr_name) {
411 snprintf(path, sizeof(path),
412 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
413 xen_domid, (uint64_t)phys_offset);
414 if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
415 return -1;
416 }
417 }
418
419 return 0;
420 }
421
422 static int xen_remove_from_physmap(XenIOState *state,
423 hwaddr start_addr,
424 ram_addr_t size)
425 {
426 unsigned long i = 0;
427 int rc = 0;
428 XenPhysmap *physmap = NULL;
429 hwaddr phys_offset = 0;
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
439 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
440 "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
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++) {
446 xen_pfn_t idx = start_addr + i;
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 }
461 g_free(physmap);
462
463 return 0;
464 }
465
466 #else
467 static int xen_add_to_physmap(XenIOState *state,
468 hwaddr start_addr,
469 ram_addr_t size,
470 MemoryRegion *mr,
471 hwaddr offset_within_region)
472 {
473 return -ENOSYS;
474 }
475
476 static int xen_remove_from_physmap(XenIOState *state,
477 hwaddr start_addr,
478 ram_addr_t size)
479 {
480 return -ENOSYS;
481 }
482 #endif
483
484 static void xen_set_memory(struct MemoryListener *listener,
485 MemoryRegionSection *section,
486 bool add)
487 {
488 XenIOState *state = container_of(listener, XenIOState, memory_listener);
489 hwaddr start_addr = section->offset_within_address_space;
490 ram_addr_t size = int128_get64(section->size);
491 bool log_dirty = memory_region_is_logging(section->mr);
492 hvmmem_type_t mem_type;
493
494 if (!memory_region_is_ram(section->mr)) {
495 return;
496 }
497
498 if (!(section->mr != &ram_memory
499 && ( (log_dirty && add) || (!log_dirty && !add)))) {
500 return;
501 }
502
503 trace_xen_client_set_memory(start_addr, size, log_dirty);
504
505 start_addr &= TARGET_PAGE_MASK;
506 size = TARGET_PAGE_ALIGN(size);
507
508 if (add) {
509 if (!memory_region_is_rom(section->mr)) {
510 xen_add_to_physmap(state, start_addr, size,
511 section->mr, section->offset_within_region);
512 } else {
513 mem_type = HVMMEM_ram_ro;
514 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
515 start_addr >> TARGET_PAGE_BITS,
516 size >> TARGET_PAGE_BITS)) {
517 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
518 start_addr);
519 }
520 }
521 } else {
522 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
523 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
524 }
525 }
526 }
527
528 static void xen_region_add(MemoryListener *listener,
529 MemoryRegionSection *section)
530 {
531 memory_region_ref(section->mr);
532 xen_set_memory(listener, section, true);
533 }
534
535 static void xen_region_del(MemoryListener *listener,
536 MemoryRegionSection *section)
537 {
538 xen_set_memory(listener, section, false);
539 memory_region_unref(section->mr);
540 }
541
542 static void xen_sync_dirty_bitmap(XenIOState *state,
543 hwaddr start_addr,
544 ram_addr_t size)
545 {
546 hwaddr npages = size >> TARGET_PAGE_BITS;
547 const int width = sizeof(unsigned long) * 8;
548 unsigned long bitmap[(npages + width - 1) / width];
549 int rc, i, j;
550 const XenPhysmap *physmap = NULL;
551
552 physmap = get_physmapping(state, start_addr, size);
553 if (physmap == NULL) {
554 /* not handled */
555 return;
556 }
557
558 if (state->log_for_dirtybit == NULL) {
559 state->log_for_dirtybit = physmap;
560 } else if (state->log_for_dirtybit != physmap) {
561 /* Only one range for dirty bitmap can be tracked. */
562 return;
563 }
564
565 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
566 start_addr >> TARGET_PAGE_BITS, npages,
567 bitmap);
568 if (rc < 0) {
569 #ifndef ENODATA
570 #define ENODATA ENOENT
571 #endif
572 if (errno == ENODATA) {
573 memory_region_set_dirty(framebuffer, 0, size);
574 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
575 ", 0x" TARGET_FMT_plx "): %s\n",
576 start_addr, start_addr + size, strerror(errno));
577 }
578 return;
579 }
580
581 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
582 unsigned long map = bitmap[i];
583 while (map != 0) {
584 j = ctzl(map);
585 map &= ~(1ul << j);
586 memory_region_set_dirty(framebuffer,
587 (i * width + j) * TARGET_PAGE_SIZE,
588 TARGET_PAGE_SIZE);
589 };
590 }
591 }
592
593 static void xen_log_start(MemoryListener *listener,
594 MemoryRegionSection *section)
595 {
596 XenIOState *state = container_of(listener, XenIOState, memory_listener);
597
598 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
599 int128_get64(section->size));
600 }
601
602 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section)
603 {
604 XenIOState *state = container_of(listener, XenIOState, memory_listener);
605
606 state->log_for_dirtybit = NULL;
607 /* Disable dirty bit tracking */
608 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
609 }
610
611 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
612 {
613 XenIOState *state = container_of(listener, XenIOState, memory_listener);
614
615 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
616 int128_get64(section->size));
617 }
618
619 static void xen_log_global_start(MemoryListener *listener)
620 {
621 if (xen_enabled()) {
622 xen_in_migration = true;
623 }
624 }
625
626 static void xen_log_global_stop(MemoryListener *listener)
627 {
628 xen_in_migration = false;
629 }
630
631 static MemoryListener xen_memory_listener = {
632 .region_add = xen_region_add,
633 .region_del = xen_region_del,
634 .log_start = xen_log_start,
635 .log_stop = xen_log_stop,
636 .log_sync = xen_log_sync,
637 .log_global_start = xen_log_global_start,
638 .log_global_stop = xen_log_global_stop,
639 .priority = 10,
640 };
641
642 /* get the ioreq packets from share mem */
643 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
644 {
645 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
646
647 if (req->state != STATE_IOREQ_READY) {
648 DPRINTF("I/O request not ready: "
649 "%x, ptr: %x, port: %"PRIx64", "
650 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
651 req->state, req->data_is_ptr, req->addr,
652 req->data, req->count, req->size);
653 return NULL;
654 }
655
656 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
657
658 req->state = STATE_IOREQ_INPROCESS;
659 return req;
660 }
661
662 /* use poll to get the port notification */
663 /* ioreq_vec--out,the */
664 /* retval--the number of ioreq packet */
665 static ioreq_t *cpu_get_ioreq(XenIOState *state)
666 {
667 int i;
668 evtchn_port_t port;
669
670 port = xc_evtchn_pending(state->xce_handle);
671 if (port == state->bufioreq_local_port) {
672 timer_mod(state->buffered_io_timer,
673 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
674 return NULL;
675 }
676
677 if (port != -1) {
678 for (i = 0; i < max_cpus; i++) {
679 if (state->ioreq_local_port[i] == port) {
680 break;
681 }
682 }
683
684 if (i == max_cpus) {
685 hw_error("Fatal error while trying to get io event!\n");
686 }
687
688 /* unmask the wanted port again */
689 xc_evtchn_unmask(state->xce_handle, port);
690
691 /* get the io packet from shared memory */
692 state->send_vcpu = i;
693 return cpu_get_ioreq_from_shared_memory(state, i);
694 }
695
696 /* read error or read nothing */
697 return NULL;
698 }
699
700 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
701 {
702 switch (size) {
703 case 1:
704 return cpu_inb(addr);
705 case 2:
706 return cpu_inw(addr);
707 case 4:
708 return cpu_inl(addr);
709 default:
710 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
711 }
712 }
713
714 static void do_outp(pio_addr_t addr,
715 unsigned long size, uint32_t val)
716 {
717 switch (size) {
718 case 1:
719 return cpu_outb(addr, val);
720 case 2:
721 return cpu_outw(addr, val);
722 case 4:
723 return cpu_outl(addr, val);
724 default:
725 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
726 }
727 }
728
729 /*
730 * Helper functions which read/write an object from/to physical guest
731 * memory, as part of the implementation of an ioreq.
732 *
733 * Equivalent to
734 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
735 * val, req->size, 0/1)
736 * except without the integer overflow problems.
737 */
738 static void rw_phys_req_item(hwaddr addr,
739 ioreq_t *req, uint32_t i, void *val, int rw)
740 {
741 /* Do everything unsigned so overflow just results in a truncated result
742 * and accesses to undesired parts of guest memory, which is up
743 * to the guest */
744 hwaddr offset = (hwaddr)req->size * i;
745 if (req->df) {
746 addr -= offset;
747 } else {
748 addr += offset;
749 }
750 cpu_physical_memory_rw(addr, val, req->size, rw);
751 }
752
753 static inline void read_phys_req_item(hwaddr addr,
754 ioreq_t *req, uint32_t i, void *val)
755 {
756 rw_phys_req_item(addr, req, i, val, 0);
757 }
758 static inline void write_phys_req_item(hwaddr addr,
759 ioreq_t *req, uint32_t i, void *val)
760 {
761 rw_phys_req_item(addr, req, i, val, 1);
762 }
763
764
765 static void cpu_ioreq_pio(ioreq_t *req)
766 {
767 uint32_t i;
768
769 if (req->dir == IOREQ_READ) {
770 if (!req->data_is_ptr) {
771 req->data = do_inp(req->addr, req->size);
772 } else {
773 uint32_t tmp;
774
775 for (i = 0; i < req->count; i++) {
776 tmp = do_inp(req->addr, req->size);
777 write_phys_req_item(req->data, req, i, &tmp);
778 }
779 }
780 } else if (req->dir == IOREQ_WRITE) {
781 if (!req->data_is_ptr) {
782 do_outp(req->addr, req->size, req->data);
783 } else {
784 for (i = 0; i < req->count; i++) {
785 uint32_t tmp = 0;
786
787 read_phys_req_item(req->data, req, i, &tmp);
788 do_outp(req->addr, req->size, tmp);
789 }
790 }
791 }
792 }
793
794 static void cpu_ioreq_move(ioreq_t *req)
795 {
796 uint32_t i;
797
798 if (!req->data_is_ptr) {
799 if (req->dir == IOREQ_READ) {
800 for (i = 0; i < req->count; i++) {
801 read_phys_req_item(req->addr, req, i, &req->data);
802 }
803 } else if (req->dir == IOREQ_WRITE) {
804 for (i = 0; i < req->count; i++) {
805 write_phys_req_item(req->addr, req, i, &req->data);
806 }
807 }
808 } else {
809 uint64_t tmp;
810
811 if (req->dir == IOREQ_READ) {
812 for (i = 0; i < req->count; i++) {
813 read_phys_req_item(req->addr, req, i, &tmp);
814 write_phys_req_item(req->data, req, i, &tmp);
815 }
816 } else if (req->dir == IOREQ_WRITE) {
817 for (i = 0; i < req->count; i++) {
818 read_phys_req_item(req->data, req, i, &tmp);
819 write_phys_req_item(req->addr, req, i, &tmp);
820 }
821 }
822 }
823 }
824
825 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
826 {
827 X86CPU *cpu;
828 CPUX86State *env;
829
830 cpu = X86_CPU(current_cpu);
831 env = &cpu->env;
832 env->regs[R_EAX] = req->data;
833 env->regs[R_EBX] = vmport_regs->ebx;
834 env->regs[R_ECX] = vmport_regs->ecx;
835 env->regs[R_EDX] = vmport_regs->edx;
836 env->regs[R_ESI] = vmport_regs->esi;
837 env->regs[R_EDI] = vmport_regs->edi;
838 }
839
840 static void regs_from_cpu(vmware_regs_t *vmport_regs)
841 {
842 X86CPU *cpu = X86_CPU(current_cpu);
843 CPUX86State *env = &cpu->env;
844
845 vmport_regs->ebx = env->regs[R_EBX];
846 vmport_regs->ecx = env->regs[R_ECX];
847 vmport_regs->edx = env->regs[R_EDX];
848 vmport_regs->esi = env->regs[R_ESI];
849 vmport_regs->edi = env->regs[R_EDI];
850 }
851
852 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
853 {
854 vmware_regs_t *vmport_regs;
855
856 assert(state->shared_vmport_page);
857 vmport_regs =
858 &state->shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
859 QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
860
861 current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
862 regs_to_cpu(vmport_regs, req);
863 cpu_ioreq_pio(req);
864 regs_from_cpu(vmport_regs);
865 current_cpu = NULL;
866 }
867
868 static void handle_ioreq(XenIOState *state, ioreq_t *req)
869 {
870 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
871 (req->size < sizeof (target_ulong))) {
872 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
873 }
874
875 switch (req->type) {
876 case IOREQ_TYPE_PIO:
877 cpu_ioreq_pio(req);
878 break;
879 case IOREQ_TYPE_COPY:
880 cpu_ioreq_move(req);
881 break;
882 case IOREQ_TYPE_VMWARE_PORT:
883 handle_vmport_ioreq(state, req);
884 break;
885 case IOREQ_TYPE_TIMEOFFSET:
886 break;
887 case IOREQ_TYPE_INVALIDATE:
888 xen_invalidate_map_cache();
889 break;
890 default:
891 hw_error("Invalid ioreq type 0x%x\n", req->type);
892 }
893 }
894
895 static int handle_buffered_iopage(XenIOState *state)
896 {
897 buf_ioreq_t *buf_req = NULL;
898 ioreq_t req;
899 int qw;
900
901 if (!state->buffered_io_page) {
902 return 0;
903 }
904
905 memset(&req, 0x00, sizeof(req));
906
907 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
908 buf_req = &state->buffered_io_page->buf_ioreq[
909 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
910 req.size = 1UL << buf_req->size;
911 req.count = 1;
912 req.addr = buf_req->addr;
913 req.data = buf_req->data;
914 req.state = STATE_IOREQ_READY;
915 req.dir = buf_req->dir;
916 req.df = 1;
917 req.type = buf_req->type;
918 req.data_is_ptr = 0;
919 qw = (req.size == 8);
920 if (qw) {
921 buf_req = &state->buffered_io_page->buf_ioreq[
922 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
923 req.data |= ((uint64_t)buf_req->data) << 32;
924 }
925
926 handle_ioreq(state, &req);
927
928 xen_mb();
929 state->buffered_io_page->read_pointer += qw ? 2 : 1;
930 }
931
932 return req.count;
933 }
934
935 static void handle_buffered_io(void *opaque)
936 {
937 XenIOState *state = opaque;
938
939 if (handle_buffered_iopage(state)) {
940 timer_mod(state->buffered_io_timer,
941 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
942 } else {
943 timer_del(state->buffered_io_timer);
944 xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
945 }
946 }
947
948 static void cpu_handle_ioreq(void *opaque)
949 {
950 XenIOState *state = opaque;
951 ioreq_t *req = cpu_get_ioreq(state);
952
953 handle_buffered_iopage(state);
954 if (req) {
955 handle_ioreq(state, req);
956
957 if (req->state != STATE_IOREQ_INPROCESS) {
958 fprintf(stderr, "Badness in I/O request ... not in service?!: "
959 "%x, ptr: %x, port: %"PRIx64", "
960 "data: %"PRIx64", count: %" FMT_ioreq_size
961 ", size: %" FMT_ioreq_size
962 ", type: %"FMT_ioreq_size"\n",
963 req->state, req->data_is_ptr, req->addr,
964 req->data, req->count, req->size, req->type);
965 destroy_hvm_domain(false);
966 return;
967 }
968
969 xen_wmb(); /* Update ioreq contents /then/ update state. */
970
971 /*
972 * We do this before we send the response so that the tools
973 * have the opportunity to pick up on the reset before the
974 * guest resumes and does a hlt with interrupts disabled which
975 * causes Xen to powerdown the domain.
976 */
977 if (runstate_is_running()) {
978 if (qemu_shutdown_requested_get()) {
979 destroy_hvm_domain(false);
980 }
981 if (qemu_reset_requested_get()) {
982 qemu_system_reset(VMRESET_REPORT);
983 destroy_hvm_domain(true);
984 }
985 }
986
987 req->state = STATE_IORESP_READY;
988 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
989 }
990 }
991
992 static void xen_main_loop_prepare(XenIOState *state)
993 {
994 int evtchn_fd = -1;
995
996 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
997 evtchn_fd = xc_evtchn_fd(state->xce_handle);
998 }
999
1000 state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
1001 state);
1002
1003 if (evtchn_fd != -1) {
1004 CPUState *cpu_state;
1005
1006 DPRINTF("%s: Init cpu_by_vcpu_id\n", __func__);
1007 CPU_FOREACH(cpu_state) {
1008 DPRINTF("%s: cpu_by_vcpu_id[%d]=%p\n",
1009 __func__, cpu_state->cpu_index, cpu_state);
1010 state->cpu_by_vcpu_id[cpu_state->cpu_index] = cpu_state;
1011 }
1012 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
1013 }
1014 }
1015
1016
1017 static void xen_hvm_change_state_handler(void *opaque, int running,
1018 RunState rstate)
1019 {
1020 if (running) {
1021 xen_main_loop_prepare((XenIOState *)opaque);
1022 }
1023 }
1024
1025 static void xen_exit_notifier(Notifier *n, void *data)
1026 {
1027 XenIOState *state = container_of(n, XenIOState, exit);
1028
1029 xc_evtchn_close(state->xce_handle);
1030 xs_daemon_close(state->xenstore);
1031 }
1032
1033 static void xen_read_physmap(XenIOState *state)
1034 {
1035 XenPhysmap *physmap = NULL;
1036 unsigned int len, num, i;
1037 char path[80], *value = NULL;
1038 char **entries = NULL;
1039
1040 snprintf(path, sizeof(path),
1041 "/local/domain/0/device-model/%d/physmap", xen_domid);
1042 entries = xs_directory(state->xenstore, 0, path, &num);
1043 if (entries == NULL)
1044 return;
1045
1046 for (i = 0; i < num; i++) {
1047 physmap = g_malloc(sizeof (XenPhysmap));
1048 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1049 snprintf(path, sizeof(path),
1050 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1051 xen_domid, entries[i]);
1052 value = xs_read(state->xenstore, 0, path, &len);
1053 if (value == NULL) {
1054 g_free(physmap);
1055 continue;
1056 }
1057 physmap->start_addr = strtoull(value, NULL, 16);
1058 free(value);
1059
1060 snprintf(path, sizeof(path),
1061 "/local/domain/0/device-model/%d/physmap/%s/size",
1062 xen_domid, entries[i]);
1063 value = xs_read(state->xenstore, 0, path, &len);
1064 if (value == NULL) {
1065 g_free(physmap);
1066 continue;
1067 }
1068 physmap->size = strtoull(value, NULL, 16);
1069 free(value);
1070
1071 snprintf(path, sizeof(path),
1072 "/local/domain/0/device-model/%d/physmap/%s/name",
1073 xen_domid, entries[i]);
1074 physmap->name = xs_read(state->xenstore, 0, path, &len);
1075
1076 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1077 }
1078 free(entries);
1079 }
1080
1081 static void xen_wakeup_notifier(Notifier *notifier, void *data)
1082 {
1083 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
1084 }
1085
1086 /* return 0 means OK, or -1 means critical issue -- will exit(1) */
1087 int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
1088 MemoryRegion **ram_memory)
1089 {
1090 int i, rc;
1091 unsigned long ioreq_pfn;
1092 unsigned long bufioreq_evtchn;
1093 XenIOState *state;
1094
1095 state = g_malloc0(sizeof (XenIOState));
1096
1097 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
1098 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
1099 perror("xen: event channel open");
1100 return -1;
1101 }
1102
1103 state->xenstore = xs_daemon_open();
1104 if (state->xenstore == NULL) {
1105 perror("xen: xenstore open");
1106 return -1;
1107 }
1108
1109 state->exit.notify = xen_exit_notifier;
1110 qemu_add_exit_notifier(&state->exit);
1111
1112 state->suspend.notify = xen_suspend_notifier;
1113 qemu_register_suspend_notifier(&state->suspend);
1114
1115 state->wakeup.notify = xen_wakeup_notifier;
1116 qemu_register_wakeup_notifier(&state->wakeup);
1117
1118 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
1119 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1120 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1121 PROT_READ|PROT_WRITE, ioreq_pfn);
1122 if (state->shared_page == NULL) {
1123 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1124 errno, xen_xc);
1125 }
1126
1127 rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
1128 if (!rc) {
1129 DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
1130 state->shared_vmport_page =
1131 xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1132 PROT_READ|PROT_WRITE, ioreq_pfn);
1133 if (state->shared_vmport_page == NULL) {
1134 hw_error("map shared vmport IO page returned error %d handle="
1135 XC_INTERFACE_FMT, errno, xen_xc);
1136 }
1137 } else if (rc != -ENOSYS) {
1138 hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
1139 }
1140
1141 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
1142 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
1143 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1144 PROT_READ|PROT_WRITE, ioreq_pfn);
1145 if (state->buffered_io_page == NULL) {
1146 hw_error("map buffered IO page returned error %d", errno);
1147 }
1148
1149 /* Note: cpus is empty at this point in init */
1150 state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
1151
1152 state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
1153
1154 /* FIXME: how about if we overflow the page here? */
1155 for (i = 0; i < max_cpus; i++) {
1156 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1157 xen_vcpu_eport(state->shared_page, i));
1158 if (rc == -1) {
1159 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1160 return -1;
1161 }
1162 state->ioreq_local_port[i] = rc;
1163 }
1164
1165 rc = xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_EVTCHN,
1166 &bufioreq_evtchn);
1167 if (rc < 0) {
1168 fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
1169 return -1;
1170 }
1171 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1172 (uint32_t)bufioreq_evtchn);
1173 if (rc == -1) {
1174 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1175 return -1;
1176 }
1177 state->bufioreq_local_port = rc;
1178
1179 /* Init RAM management */
1180 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
1181 xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
1182
1183 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
1184
1185 state->memory_listener = xen_memory_listener;
1186 QLIST_INIT(&state->physmap);
1187 memory_listener_register(&state->memory_listener, &address_space_memory);
1188 state->log_for_dirtybit = NULL;
1189
1190 /* Initialize backend core & drivers */
1191 if (xen_be_init() != 0) {
1192 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
1193 return -1;
1194 }
1195 xen_be_register("console", &xen_console_ops);
1196 xen_be_register("vkbd", &xen_kbdmouse_ops);
1197 xen_be_register("qdisk", &xen_blkdev_ops);
1198 xen_read_physmap(state);
1199
1200 return 0;
1201 }
1202
1203 void destroy_hvm_domain(bool reboot)
1204 {
1205 XenXC xc_handle;
1206 int sts;
1207
1208 xc_handle = xen_xc_interface_open(0, 0, 0);
1209 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1210 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1211 } else {
1212 sts = xc_domain_shutdown(xc_handle, xen_domid,
1213 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
1214 if (sts != 0) {
1215 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1216 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1217 sts, strerror(errno));
1218 } else {
1219 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1220 reboot ? "reboot" : "poweroff");
1221 }
1222 xc_interface_close(xc_handle);
1223 }
1224 }
1225
1226 void xen_register_framebuffer(MemoryRegion *mr)
1227 {
1228 framebuffer = mr;
1229 }
1230
1231 void xen_shutdown_fatal_error(const char *fmt, ...)
1232 {
1233 va_list ap;
1234
1235 va_start(ap, fmt);
1236 vfprintf(stderr, fmt, ap);
1237 va_end(ap);
1238 fprintf(stderr, "Will destroy the domain.\n");
1239 /* destroy the domain */
1240 qemu_system_shutdown_request();
1241 }
1242
1243 void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1244 {
1245 if (unlikely(xen_in_migration)) {
1246 int rc;
1247 ram_addr_t start_pfn, nb_pages;
1248
1249 if (length == 0) {
1250 length = TARGET_PAGE_SIZE;
1251 }
1252 start_pfn = start >> TARGET_PAGE_BITS;
1253 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1254 - start_pfn;
1255 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1256 if (rc) {
1257 fprintf(stderr,
1258 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1259 __func__, start, nb_pages, rc, strerror(-rc));
1260 }
1261 }
1262 }
1263
1264 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
1265 {
1266 if (enable) {
1267 memory_global_dirty_log_start();
1268 } else {
1269 memory_global_dirty_log_stop();
1270 }
1271 }