Merge remote-tracking branch 'agraf/xen-next' into staging
[qemu.git] / xen-all.c
blob9eaeac1654581d4db0d23a7fcbd05d0bcc714811
1 /*
2 * Copyright (C) 2010 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
7 */
9 #include <sys/mman.h>
11 #include "hw/pci.h"
12 #include "hw/pc.h"
13 #include "hw/xen_common.h"
14 #include "hw/xen_backend.h"
16 #include "range.h"
17 #include "xen-mapcache.h"
18 #include "trace.h"
20 #include <xen/hvm/ioreq.h>
21 #include <xen/hvm/params.h>
22 #include <xen/hvm/e820.h>
24 //#define DEBUG_XEN
26 #ifdef DEBUG_XEN
27 #define DPRINTF(fmt, ...) \
28 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
29 #else
30 #define DPRINTF(fmt, ...) \
31 do { } while (0)
32 #endif
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
45 #else
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"
55 #endif
57 #define BUFFER_IO_MAX_DELAY 100
59 typedef struct XenPhysmap {
60 target_phys_addr_t start_addr;
61 ram_addr_t size;
62 target_phys_addr_t phys_offset;
64 QLIST_ENTRY(XenPhysmap) list;
65 } XenPhysmap;
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 */
74 XenEvtchn xce_handle;
75 /* which vcpu we are serving */
76 int send_vcpu;
78 struct xs_handle *xenstore;
79 CPUPhysMemoryClient client;
80 QLIST_HEAD(, XenPhysmap) physmap;
81 const XenPhysmap *log_for_dirtybit;
83 Notifier exit;
84 } XenIOState;
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,
96 irq_num & 3, level);
99 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
101 int i;
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;
106 if (v & 0x80) {
107 v = 0;
109 v &= 0xf;
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);
119 if (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);
136 /* Memory Ops */
138 static void xen_ram_init(ram_addr_t ram_size)
140 RAMBlock *new_block;
141 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
143 new_block = qemu_mallocz(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 = qemu_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;
165 } else {
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
171 * emulated device.
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,
177 0xc0000);
178 if (above_4g_mem_size > 0) {
179 cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
180 0x100000000ULL);
184 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size)
186 unsigned long nr_pfn;
187 xen_pfn_t *pfn_list;
188 int i;
190 trace_xen_ram_alloc(ram_addr, size);
192 nr_pfn = size >> TARGET_PAGE_BITS;
193 pfn_list = qemu_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);
203 qemu_free(pfn_list);
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)) {
215 return physmap;
218 return NULL;
221 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
222 static int xen_add_to_physmap(XenIOState *state,
223 target_phys_addr_t start_addr,
224 ram_addr_t size,
225 target_phys_addr_t phys_offset)
227 unsigned long i = 0;
228 int rc = 0;
229 XenPhysmap *physmap = NULL;
230 target_phys_addr_t pfn, start_gpfn;
231 RAMBlock *block;
233 if (get_physmapping(state, start_addr, size)) {
234 return 0;
236 if (size <= 0) {
237 return -1;
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) {
247 goto go_physmap;
250 return -1;
252 go_physmap:
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);
263 if (rc) {
264 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
265 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
266 return -rc;
270 physmap = qemu_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);
282 return 0;
285 static int xen_remove_from_physmap(XenIOState *state,
286 target_phys_addr_t start_addr,
287 ram_addr_t size)
289 unsigned long i = 0;
290 int rc = 0;
291 XenPhysmap *physmap = NULL;
292 target_phys_addr_t phys_offset = 0;
294 physmap = get_physmapping(state, start_addr, size);
295 if (physmap == NULL) {
296 return -1;
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);
313 if (rc) {
314 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
315 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
316 return -rc;
320 QLIST_REMOVE(physmap, list);
321 if (state->log_for_dirtybit == physmap) {
322 state->log_for_dirtybit = NULL;
324 free(physmap);
326 return 0;
329 #else
330 static int xen_add_to_physmap(XenIOState *state,
331 target_phys_addr_t start_addr,
332 ram_addr_t size,
333 target_phys_addr_t phys_offset)
335 return -ENOSYS;
338 static int xen_remove_from_physmap(XenIOState *state,
339 target_phys_addr_t start_addr,
340 ram_addr_t size)
342 return -ENOSYS;
344 #endif
346 static void xen_client_set_memory(struct CPUPhysMemoryClient *client,
347 target_phys_addr_t start_addr,
348 ram_addr_t size,
349 ram_addr_t phys_offset,
350 bool log_dirty)
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)))) {
359 return;
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;
368 switch (flags) {
369 case IO_MEM_RAM:
370 xen_add_to_physmap(state, start_addr, size, phys_offset);
371 break;
372 case IO_MEM_ROM:
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",
378 start_addr);
380 break;
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);
385 break;
389 static int xen_sync_dirty_bitmap(XenIOState *state,
390 target_phys_addr_t start_addr,
391 ram_addr_t size)
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];
397 int rc, i, j;
398 const XenPhysmap *physmap = NULL;
400 physmap = get_physmapping(state, start_addr, size);
401 if (physmap == NULL) {
402 /* not handled */
403 return -1;
406 if (state->log_for_dirtybit == NULL) {
407 state->log_for_dirtybit = physmap;
408 } else if (state->log_for_dirtybit != physmap) {
409 return -1;
411 vram_offset = physmap->phys_offset;
413 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
414 start_addr >> TARGET_PAGE_BITS, npages,
415 bitmap);
416 if (rc) {
417 return rc;
420 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
421 unsigned long map = bitmap[i];
422 while (map != 0) {
423 j = ffsl(map) - 1;
424 map &= ~(1ul << j);
425 cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE);
429 return 0;
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,
458 int enable)
460 return 0;
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;
477 env->halted = 1;
480 void xen_vcpu_init(void)
482 CPUState *first_cpu;
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);
501 return NULL;
504 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
506 req->state = STATE_IOREQ_INPROCESS;
507 return req;
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)
515 int i;
516 evtchn_port_t port;
518 port = xc_evtchn_pending(state->xce_handle);
519 if (port != -1) {
520 for (i = 0; i < smp_cpus; i++) {
521 if (state->ioreq_local_port[i] == port) {
522 break;
526 if (i == smp_cpus) {
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 */
539 return NULL;
542 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
544 switch (size) {
545 case 1:
546 return cpu_inb(addr);
547 case 2:
548 return cpu_inw(addr);
549 case 4:
550 return cpu_inl(addr);
551 default:
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)
559 switch (size) {
560 case 1:
561 return cpu_outb(addr, val);
562 case 2:
563 return cpu_outw(addr, val);
564 case 4:
565 return cpu_outl(addr, val);
566 default:
567 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
571 static void cpu_ioreq_pio(ioreq_t *req)
573 int i, sign;
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);
580 } else {
581 uint32_t tmp;
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);
592 } else {
593 for (i = 0; i < req->count; i++) {
594 uint32_t tmp = 0;
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)
606 int i, sign;
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);
622 } else {
623 target_ulong tmp;
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;
650 switch (req->type) {
651 case IOREQ_TYPE_PIO:
652 cpu_ioreq_pio(req);
653 break;
654 case IOREQ_TYPE_COPY:
655 cpu_ioreq_move(req);
656 break;
657 case IOREQ_TYPE_TIMEOFFSET:
658 break;
659 case IOREQ_TYPE_INVALIDATE:
660 xen_invalidate_map_cache();
661 break;
662 default:
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;
670 ioreq_t req;
671 int qw;
673 if (!state->buffered_io_page) {
674 return;
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;
681 req.count = 1;
682 req.addr = buf_req->addr;
683 req.data = buf_req->data;
684 req.state = STATE_IOREQ_READY;
685 req.dir = buf_req->dir;
686 req.df = 1;
687 req.type = buf_req->type;
688 req.data_is_ptr = 0;
689 qw = (req.size == 8);
690 if (qw) {
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;
696 handle_ioreq(&req);
698 xen_mb();
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);
718 if (req) {
719 handle_ioreq(req);
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();
728 return;
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 (vm_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;
756 char *path = NULL;
757 char *newpath = NULL;
758 char *pts = NULL;
759 int ret = -1;
761 /* Only continue if we're talking to a pty. */
762 if (strncmp(cs->filename, "pty:", 4)) {
763 return 0;
765 pts = cs->filename + 4;
767 /* We now have everything we need to set the xenstore entry. */
768 xs = xs_open(0);
769 if (xs == NULL) {
770 fprintf(stderr, "Could not contact XenStore\n");
771 goto out;
774 path = xs_get_domain_path(xs, domid);
775 if (path == NULL) {
776 fprintf(stderr, "xs_get_domain_path() error\n");
777 goto out;
779 newpath = realloc(path, (strlen(path) + strlen(string) +
780 strlen("/tty") + 1));
781 if (newpath == NULL) {
782 fprintf(stderr, "realloc error\n");
783 goto out;
785 path = newpath;
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);
791 goto out;
793 ret = 0;
795 out:
796 free(path);
797 xs_close(xs);
799 return ret;
802 void xenstore_store_pv_console_info(int i, CharDriverState *chr)
804 if (i == 0) {
805 store_dev_info(xen_domid, chr, "/console");
806 } else {
807 char buf[32];
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)
815 char path[50];
817 if (xs == NULL) {
818 fprintf(stderr, "xenstore connection not initialized\n");
819 exit(1);
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");
825 exit(1);
829 static void xen_main_loop_prepare(XenIOState *state)
831 int evtchn_fd = -1;
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,
838 state);
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);
847 /* Initialise Xen */
849 static void xen_change_state_handler(void *opaque, int running, int reason)
851 if (running) {
852 /* record state running */
853 xenstore_record_dm_state(xenstore, "running");
857 static void xen_hvm_change_state_handler(void *opaque, int running, int reason)
859 XenIOState *state = opaque;
860 if (running) {
861 xen_main_loop_prepare(state);
865 static void xen_exit_notifier(Notifier *n, void *data)
867 XenIOState *state = container_of(n, XenIOState, exit);
869 xc_evtchn_close(state->xce_handle);
870 xs_daemon_close(state->xenstore);
873 int xen_init(void)
875 xen_xc = xen_xc_interface_open(0, 0, 0);
876 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
877 xen_be_printf(NULL, 0, "can't open xen interface\n");
878 return -1;
880 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
882 return 0;
885 int xen_hvm_init(void)
887 int i, rc;
888 unsigned long ioreq_pfn;
889 XenIOState *state;
891 state = qemu_mallocz(sizeof (XenIOState));
893 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
894 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
895 perror("xen: event channel open");
896 return -errno;
899 state->xenstore = xs_daemon_open();
900 if (state->xenstore == NULL) {
901 perror("xen: xenstore open");
902 return -errno;
905 state->exit.notify = xen_exit_notifier;
906 qemu_add_exit_notifier(&state->exit);
908 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
909 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
910 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
911 PROT_READ|PROT_WRITE, ioreq_pfn);
912 if (state->shared_page == NULL) {
913 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
914 errno, xen_xc);
917 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
918 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
919 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
920 PROT_READ|PROT_WRITE, ioreq_pfn);
921 if (state->buffered_io_page == NULL) {
922 hw_error("map buffered IO page returned error %d", errno);
925 state->ioreq_local_port = qemu_mallocz(smp_cpus * sizeof (evtchn_port_t));
927 /* FIXME: how about if we overflow the page here? */
928 for (i = 0; i < smp_cpus; i++) {
929 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
930 xen_vcpu_eport(state->shared_page, i));
931 if (rc == -1) {
932 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
933 return -1;
935 state->ioreq_local_port[i] = rc;
938 /* Init RAM management */
939 xen_map_cache_init();
940 xen_ram_init(ram_size);
942 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
944 state->client = xen_cpu_phys_memory_client;
945 QLIST_INIT(&state->physmap);
946 cpu_register_phys_memory_client(&state->client);
947 state->log_for_dirtybit = NULL;
949 /* Initialize backend core & drivers */
950 if (xen_be_init() != 0) {
951 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
952 exit(1);
954 xen_be_register("console", &xen_console_ops);
955 xen_be_register("vkbd", &xen_kbdmouse_ops);
956 xen_be_register("qdisk", &xen_blkdev_ops);
958 return 0;
961 void destroy_hvm_domain(void)
963 XenXC xc_handle;
964 int sts;
966 xc_handle = xen_xc_interface_open(0, 0, 0);
967 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
968 fprintf(stderr, "Cannot acquire xenctrl handle\n");
969 } else {
970 sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
971 if (sts != 0) {
972 fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
973 "sts %d, %s\n", sts, strerror(errno));
974 } else {
975 fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
977 xc_interface_close(xc_handle);