2 * Copyright (C) 2010 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
13 #include "hw/xen_common.h"
14 #include "hw/xen_backend.h"
17 #include "xen-mapcache.h"
20 #include <xen/hvm/ioreq.h>
21 #include <xen/hvm/params.h>
22 #include <xen/hvm/e820.h>
27 #define DPRINTF(fmt, ...) \
28 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) \
34 /* Compatibility with older version */
35 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
36 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
38 return shared_page->vcpu_iodata[i].vp_eport;
40 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
42 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
44 # define FMT_ioreq_size PRIx64
46 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
48 return shared_page->vcpu_ioreq[i].vp_eport;
50 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
52 return &shared_page->vcpu_ioreq[vcpu];
54 # define FMT_ioreq_size "u"
57 #define BUFFER_IO_MAX_DELAY 100
59 typedef struct XenPhysmap {
60 target_phys_addr_t start_addr;
62 target_phys_addr_t phys_offset;
64 QLIST_ENTRY(XenPhysmap) list;
67 typedef struct XenIOState {
68 shared_iopage_t *shared_page;
69 buffered_iopage_t *buffered_io_page;
70 QEMUTimer *buffered_io_timer;
71 /* the evtchn port for polling the notification, */
72 evtchn_port_t *ioreq_local_port;
73 /* the evtchn fd for polling */
75 /* which vcpu we are serving */
78 struct xs_handle *xenstore;
79 CPUPhysMemoryClient client;
80 QLIST_HEAD(, XenPhysmap) physmap;
81 const XenPhysmap *log_for_dirtybit;
86 /* Xen specific function for piix pci */
88 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
90 return irq_num + ((pci_dev->devfn >> 3) << 2);
93 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
95 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
99 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
103 /* Scan for updates to PCI link routes (0x60-0x63). */
104 for (i = 0; i < len; i++) {
105 uint8_t v = (val >> (8 * i)) & 0xff;
110 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
111 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
116 void xen_cmos_set_s3_resume(void *opaque, int irq, int level)
118 pc_cmos_set_s3_resume(opaque, irq, level);
120 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
124 /* Xen Interrupt Controller */
126 static void xen_set_irq(void *opaque, int irq, int level)
128 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
131 qemu_irq *xen_interrupt_controller_init(void)
133 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
138 static void xen_ram_init(ram_addr_t ram_size)
141 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
143 new_block = g_malloc0(sizeof (*new_block));
144 pstrcpy(new_block->idstr, sizeof (new_block->idstr), "xen.ram");
145 new_block->host = NULL;
146 new_block->offset = 0;
147 new_block->length = ram_size;
148 if (ram_size >= HVM_BELOW_4G_RAM_END) {
149 /* Xen does not allocate the memory continuously, and keep a hole at
150 * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH
152 new_block->length += HVM_BELOW_4G_MMIO_LENGTH;
155 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
157 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
158 new_block->length >> TARGET_PAGE_BITS);
159 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
160 0xff, new_block->length >> TARGET_PAGE_BITS);
162 if (ram_size >= HVM_BELOW_4G_RAM_END) {
163 above_4g_mem_size = ram_size - HVM_BELOW_4G_RAM_END;
164 below_4g_mem_size = HVM_BELOW_4G_RAM_END;
166 below_4g_mem_size = ram_size;
169 cpu_register_physical_memory(0, 0xa0000, 0);
170 /* Skip of the VGA IO memory space, it will be registered later by the VGA
173 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
174 * the Options ROM, so it is registered here as RAM.
176 cpu_register_physical_memory(0xc0000, below_4g_mem_size - 0xc0000,
178 if (above_4g_mem_size > 0) {
179 cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
184 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size)
186 unsigned long nr_pfn;
190 trace_xen_ram_alloc(ram_addr, size);
192 nr_pfn = size >> TARGET_PAGE_BITS;
193 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
195 for (i = 0; i < nr_pfn; i++) {
196 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
199 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
200 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
206 static XenPhysmap *get_physmapping(XenIOState *state,
207 target_phys_addr_t start_addr, ram_addr_t size)
209 XenPhysmap *physmap = NULL;
211 start_addr &= TARGET_PAGE_MASK;
213 QLIST_FOREACH(physmap, &state->physmap, list) {
214 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
221 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
222 static int xen_add_to_physmap(XenIOState *state,
223 target_phys_addr_t start_addr,
225 target_phys_addr_t phys_offset)
229 XenPhysmap *physmap = NULL;
230 target_phys_addr_t pfn, start_gpfn;
233 if (get_physmapping(state, start_addr, size)) {
240 /* Xen can only handle a single dirty log region for now and we want
241 * the linear framebuffer to be that region.
242 * Avoid tracking any regions that is not videoram and avoid tracking
243 * the legacy vga region. */
244 QLIST_FOREACH(block, &ram_list.blocks, next) {
245 if (!strcmp(block->idstr, "vga.vram") && block->offset == phys_offset
246 && start_addr > 0xbffff) {
253 DPRINTF("mapping vram to %llx - %llx, from %llx\n",
254 start_addr, start_addr + size, phys_offset);
256 pfn = phys_offset >> TARGET_PAGE_BITS;
257 start_gpfn = start_addr >> TARGET_PAGE_BITS;
258 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
259 unsigned long idx = pfn + i;
260 xen_pfn_t gpfn = start_gpfn + i;
262 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
264 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
265 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
270 physmap = g_malloc(sizeof (XenPhysmap));
272 physmap->start_addr = start_addr;
273 physmap->size = size;
274 physmap->phys_offset = phys_offset;
276 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
278 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
279 start_addr >> TARGET_PAGE_BITS,
280 (start_addr + size) >> TARGET_PAGE_BITS,
281 XEN_DOMCTL_MEM_CACHEATTR_WB);
285 static int xen_remove_from_physmap(XenIOState *state,
286 target_phys_addr_t start_addr,
291 XenPhysmap *physmap = NULL;
292 target_phys_addr_t phys_offset = 0;
294 physmap = get_physmapping(state, start_addr, size);
295 if (physmap == NULL) {
299 phys_offset = physmap->phys_offset;
300 size = physmap->size;
302 DPRINTF("unmapping vram to %llx - %llx, from %llx\n",
303 phys_offset, phys_offset + size, start_addr);
305 size >>= TARGET_PAGE_BITS;
306 start_addr >>= TARGET_PAGE_BITS;
307 phys_offset >>= TARGET_PAGE_BITS;
308 for (i = 0; i < size; i++) {
309 unsigned long idx = start_addr + i;
310 xen_pfn_t gpfn = phys_offset + i;
312 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
314 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
315 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
320 QLIST_REMOVE(physmap, list);
321 if (state->log_for_dirtybit == physmap) {
322 state->log_for_dirtybit = NULL;
330 static int xen_add_to_physmap(XenIOState *state,
331 target_phys_addr_t start_addr,
333 target_phys_addr_t phys_offset)
338 static int xen_remove_from_physmap(XenIOState *state,
339 target_phys_addr_t start_addr,
346 static void xen_client_set_memory(struct CPUPhysMemoryClient *client,
347 target_phys_addr_t start_addr,
349 ram_addr_t phys_offset,
352 XenIOState *state = container_of(client, XenIOState, client);
353 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
354 hvmmem_type_t mem_type;
356 if (!(start_addr != phys_offset
357 && ( (log_dirty && flags < IO_MEM_UNASSIGNED)
358 || (!log_dirty && flags == IO_MEM_UNASSIGNED)))) {
362 trace_xen_client_set_memory(start_addr, size, phys_offset, log_dirty);
364 start_addr &= TARGET_PAGE_MASK;
365 size = TARGET_PAGE_ALIGN(size);
366 phys_offset &= TARGET_PAGE_MASK;
370 xen_add_to_physmap(state, start_addr, size, phys_offset);
373 mem_type = HVMMEM_ram_ro;
374 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
375 start_addr >> TARGET_PAGE_BITS,
376 size >> TARGET_PAGE_BITS)) {
377 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
381 case IO_MEM_UNASSIGNED:
382 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
383 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
389 static int xen_sync_dirty_bitmap(XenIOState *state,
390 target_phys_addr_t start_addr,
393 target_phys_addr_t npages = size >> TARGET_PAGE_BITS;
394 target_phys_addr_t vram_offset = 0;
395 const int width = sizeof(unsigned long) * 8;
396 unsigned long bitmap[(npages + width - 1) / width];
398 const XenPhysmap *physmap = NULL;
400 physmap = get_physmapping(state, start_addr, size);
401 if (physmap == NULL) {
406 if (state->log_for_dirtybit == NULL) {
407 state->log_for_dirtybit = physmap;
408 } else if (state->log_for_dirtybit != physmap) {
411 vram_offset = physmap->phys_offset;
413 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
414 start_addr >> TARGET_PAGE_BITS, npages,
420 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
421 unsigned long map = bitmap[i];
425 cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE);
432 static int xen_log_start(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
434 XenIOState *state = container_of(client, XenIOState, client);
436 return xen_sync_dirty_bitmap(state, phys_addr, size);
439 static int xen_log_stop(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
441 XenIOState *state = container_of(client, XenIOState, client);
443 state->log_for_dirtybit = NULL;
444 /* Disable dirty bit tracking */
445 return xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
448 static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client,
449 target_phys_addr_t start_addr,
450 target_phys_addr_t end_addr)
452 XenIOState *state = container_of(client, XenIOState, client);
454 return xen_sync_dirty_bitmap(state, start_addr, end_addr - start_addr);
457 static int xen_client_migration_log(struct CPUPhysMemoryClient *client,
463 static CPUPhysMemoryClient xen_cpu_phys_memory_client = {
464 .set_memory = xen_client_set_memory,
465 .sync_dirty_bitmap = xen_client_sync_dirty_bitmap,
466 .migration_log = xen_client_migration_log,
467 .log_start = xen_log_start,
468 .log_stop = xen_log_stop,
471 /* VCPU Operations, MMIO, IO ring ... */
473 static void xen_reset_vcpu(void *opaque)
475 CPUState *env = opaque;
480 void xen_vcpu_init(void)
484 if ((first_cpu = qemu_get_cpu(0))) {
485 qemu_register_reset(xen_reset_vcpu, first_cpu);
486 xen_reset_vcpu(first_cpu);
490 /* get the ioreq packets from share mem */
491 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
493 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
495 if (req->state != STATE_IOREQ_READY) {
496 DPRINTF("I/O request not ready: "
497 "%x, ptr: %x, port: %"PRIx64", "
498 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
499 req->state, req->data_is_ptr, req->addr,
500 req->data, req->count, req->size);
504 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
506 req->state = STATE_IOREQ_INPROCESS;
510 /* use poll to get the port notification */
511 /* ioreq_vec--out,the */
512 /* retval--the number of ioreq packet */
513 static ioreq_t *cpu_get_ioreq(XenIOState *state)
518 port = xc_evtchn_pending(state->xce_handle);
520 for (i = 0; i < smp_cpus; i++) {
521 if (state->ioreq_local_port[i] == port) {
527 hw_error("Fatal error while trying to get io event!\n");
530 /* unmask the wanted port again */
531 xc_evtchn_unmask(state->xce_handle, port);
533 /* get the io packet from shared memory */
534 state->send_vcpu = i;
535 return cpu_get_ioreq_from_shared_memory(state, i);
538 /* read error or read nothing */
542 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
546 return cpu_inb(addr);
548 return cpu_inw(addr);
550 return cpu_inl(addr);
552 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
556 static void do_outp(pio_addr_t addr,
557 unsigned long size, uint32_t val)
561 return cpu_outb(addr, val);
563 return cpu_outw(addr, val);
565 return cpu_outl(addr, val);
567 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
571 static void cpu_ioreq_pio(ioreq_t *req)
575 sign = req->df ? -1 : 1;
577 if (req->dir == IOREQ_READ) {
578 if (!req->data_is_ptr) {
579 req->data = do_inp(req->addr, req->size);
583 for (i = 0; i < req->count; i++) {
584 tmp = do_inp(req->addr, req->size);
585 cpu_physical_memory_write(req->data + (sign * i * req->size),
586 (uint8_t *) &tmp, req->size);
589 } else if (req->dir == IOREQ_WRITE) {
590 if (!req->data_is_ptr) {
591 do_outp(req->addr, req->size, req->data);
593 for (i = 0; i < req->count; i++) {
596 cpu_physical_memory_read(req->data + (sign * i * req->size),
597 (uint8_t*) &tmp, req->size);
598 do_outp(req->addr, req->size, tmp);
604 static void cpu_ioreq_move(ioreq_t *req)
608 sign = req->df ? -1 : 1;
610 if (!req->data_is_ptr) {
611 if (req->dir == IOREQ_READ) {
612 for (i = 0; i < req->count; i++) {
613 cpu_physical_memory_read(req->addr + (sign * i * req->size),
614 (uint8_t *) &req->data, req->size);
616 } else if (req->dir == IOREQ_WRITE) {
617 for (i = 0; i < req->count; i++) {
618 cpu_physical_memory_write(req->addr + (sign * i * req->size),
619 (uint8_t *) &req->data, req->size);
625 if (req->dir == IOREQ_READ) {
626 for (i = 0; i < req->count; i++) {
627 cpu_physical_memory_read(req->addr + (sign * i * req->size),
628 (uint8_t*) &tmp, req->size);
629 cpu_physical_memory_write(req->data + (sign * i * req->size),
630 (uint8_t*) &tmp, req->size);
632 } else if (req->dir == IOREQ_WRITE) {
633 for (i = 0; i < req->count; i++) {
634 cpu_physical_memory_read(req->data + (sign * i * req->size),
635 (uint8_t*) &tmp, req->size);
636 cpu_physical_memory_write(req->addr + (sign * i * req->size),
637 (uint8_t*) &tmp, req->size);
643 static void handle_ioreq(ioreq_t *req)
645 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
646 (req->size < sizeof (target_ulong))) {
647 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
654 case IOREQ_TYPE_COPY:
657 case IOREQ_TYPE_TIMEOFFSET:
659 case IOREQ_TYPE_INVALIDATE:
660 xen_invalidate_map_cache();
663 hw_error("Invalid ioreq type 0x%x\n", req->type);
667 static void handle_buffered_iopage(XenIOState *state)
669 buf_ioreq_t *buf_req = NULL;
673 if (!state->buffered_io_page) {
677 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
678 buf_req = &state->buffered_io_page->buf_ioreq[
679 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
680 req.size = 1UL << buf_req->size;
682 req.addr = buf_req->addr;
683 req.data = buf_req->data;
684 req.state = STATE_IOREQ_READY;
685 req.dir = buf_req->dir;
687 req.type = buf_req->type;
689 qw = (req.size == 8);
691 buf_req = &state->buffered_io_page->buf_ioreq[
692 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
693 req.data |= ((uint64_t)buf_req->data) << 32;
699 state->buffered_io_page->read_pointer += qw ? 2 : 1;
703 static void handle_buffered_io(void *opaque)
705 XenIOState *state = opaque;
707 handle_buffered_iopage(state);
708 qemu_mod_timer(state->buffered_io_timer,
709 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
712 static void cpu_handle_ioreq(void *opaque)
714 XenIOState *state = opaque;
715 ioreq_t *req = cpu_get_ioreq(state);
717 handle_buffered_iopage(state);
721 if (req->state != STATE_IOREQ_INPROCESS) {
722 fprintf(stderr, "Badness in I/O request ... not in service?!: "
723 "%x, ptr: %x, port: %"PRIx64", "
724 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
725 req->state, req->data_is_ptr, req->addr,
726 req->data, req->count, req->size);
727 destroy_hvm_domain();
731 xen_wmb(); /* Update ioreq contents /then/ update state. */
734 * We do this before we send the response so that the tools
735 * have the opportunity to pick up on the reset before the
736 * guest resumes and does a hlt with interrupts disabled which
737 * causes Xen to powerdown the domain.
739 if (runstate_is_running()) {
740 if (qemu_shutdown_requested_get()) {
741 destroy_hvm_domain();
743 if (qemu_reset_requested_get()) {
744 qemu_system_reset(VMRESET_REPORT);
748 req->state = STATE_IORESP_READY;
749 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
753 static int store_dev_info(int domid, CharDriverState *cs, const char *string)
755 struct xs_handle *xs = NULL;
757 char *newpath = NULL;
761 /* Only continue if we're talking to a pty. */
762 if (strncmp(cs->filename, "pty:", 4)) {
765 pts = cs->filename + 4;
767 /* We now have everything we need to set the xenstore entry. */
770 fprintf(stderr, "Could not contact XenStore\n");
774 path = xs_get_domain_path(xs, domid);
776 fprintf(stderr, "xs_get_domain_path() error\n");
779 newpath = realloc(path, (strlen(path) + strlen(string) +
780 strlen("/tty") + 1));
781 if (newpath == NULL) {
782 fprintf(stderr, "realloc error\n");
787 strcat(path, string);
788 strcat(path, "/tty");
789 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
790 fprintf(stderr, "xs_write for '%s' fail", string);
802 void xenstore_store_pv_console_info(int i, CharDriverState *chr)
805 store_dev_info(xen_domid, chr, "/console");
808 snprintf(buf, sizeof(buf), "/device/console/%d", i);
809 store_dev_info(xen_domid, chr, buf);
813 static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
818 fprintf(stderr, "xenstore connection not initialized\n");
822 snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
823 if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
824 fprintf(stderr, "error recording dm state\n");
829 static void xen_main_loop_prepare(XenIOState *state)
833 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
834 evtchn_fd = xc_evtchn_fd(state->xce_handle);
837 state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
839 qemu_mod_timer(state->buffered_io_timer, qemu_get_clock_ms(rt_clock));
841 if (evtchn_fd != -1) {
842 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
849 static void xen_change_state_handler(void *opaque, int running,
853 /* record state running */
854 xenstore_record_dm_state(xenstore, "running");
858 static void xen_hvm_change_state_handler(void *opaque, int running,
861 XenIOState *xstate = opaque;
863 xen_main_loop_prepare(xstate);
867 static void xen_exit_notifier(Notifier *n, void *data)
869 XenIOState *state = container_of(n, XenIOState, exit);
871 xc_evtchn_close(state->xce_handle);
872 xs_daemon_close(state->xenstore);
877 xen_xc = xen_xc_interface_open(0, 0, 0);
878 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
879 xen_be_printf(NULL, 0, "can't open xen interface\n");
882 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
887 int xen_hvm_init(void)
890 unsigned long ioreq_pfn;
893 state = g_malloc0(sizeof (XenIOState));
895 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
896 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
897 perror("xen: event channel open");
901 state->xenstore = xs_daemon_open();
902 if (state->xenstore == NULL) {
903 perror("xen: xenstore open");
907 state->exit.notify = xen_exit_notifier;
908 qemu_add_exit_notifier(&state->exit);
910 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
911 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
912 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
913 PROT_READ|PROT_WRITE, ioreq_pfn);
914 if (state->shared_page == NULL) {
915 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
919 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
920 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
921 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
922 PROT_READ|PROT_WRITE, ioreq_pfn);
923 if (state->buffered_io_page == NULL) {
924 hw_error("map buffered IO page returned error %d", errno);
927 state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
929 /* FIXME: how about if we overflow the page here? */
930 for (i = 0; i < smp_cpus; i++) {
931 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
932 xen_vcpu_eport(state->shared_page, i));
934 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
937 state->ioreq_local_port[i] = rc;
940 /* Init RAM management */
941 xen_map_cache_init();
942 xen_ram_init(ram_size);
944 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
946 state->client = xen_cpu_phys_memory_client;
947 QLIST_INIT(&state->physmap);
948 cpu_register_phys_memory_client(&state->client);
949 state->log_for_dirtybit = NULL;
951 /* Initialize backend core & drivers */
952 if (xen_be_init() != 0) {
953 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
956 xen_be_register("console", &xen_console_ops);
957 xen_be_register("vkbd", &xen_kbdmouse_ops);
958 xen_be_register("qdisk", &xen_blkdev_ops);
963 void destroy_hvm_domain(void)
968 xc_handle = xen_xc_interface_open(0, 0, 0);
969 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
970 fprintf(stderr, "Cannot acquire xenctrl handle\n");
972 sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
974 fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
975 "sts %d, %s\n", sts, strerror(errno));
977 fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
979 xc_interface_close(xc_handle);