]> git.proxmox.com Git - qemu.git/blob - xen-all.c
sun4u: give ISA bus to ISA methods
[qemu.git] / xen-all.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 */
8
9 #include <sys/mman.h>
10
11 #include "hw/pci.h"
12 #include "hw/pc.h"
13 #include "hw/xen_common.h"
14 #include "hw/xen_backend.h"
15
16 #include "range.h"
17 #include "xen-mapcache.h"
18 #include "trace.h"
19 #include "exec-memory.h"
20
21 #include <xen/hvm/ioreq.h>
22 #include <xen/hvm/params.h>
23 #include <xen/hvm/e820.h>
24
25 //#define DEBUG_XEN
26
27 #ifdef DEBUG_XEN
28 #define DPRINTF(fmt, ...) \
29 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
30 #else
31 #define DPRINTF(fmt, ...) \
32 do { } while (0)
33 #endif
34
35 static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
36
37 /* Compatibility with older version */
38 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
39 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
40 {
41 return shared_page->vcpu_iodata[i].vp_eport;
42 }
43 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
44 {
45 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
46 }
47 # define FMT_ioreq_size PRIx64
48 #else
49 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
50 {
51 return shared_page->vcpu_ioreq[i].vp_eport;
52 }
53 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
54 {
55 return &shared_page->vcpu_ioreq[vcpu];
56 }
57 # define FMT_ioreq_size "u"
58 #endif
59
60 #define BUFFER_IO_MAX_DELAY 100
61
62 typedef struct XenPhysmap {
63 target_phys_addr_t start_addr;
64 ram_addr_t size;
65 target_phys_addr_t phys_offset;
66
67 QLIST_ENTRY(XenPhysmap) list;
68 } XenPhysmap;
69
70 typedef struct XenIOState {
71 shared_iopage_t *shared_page;
72 buffered_iopage_t *buffered_io_page;
73 QEMUTimer *buffered_io_timer;
74 /* the evtchn port for polling the notification, */
75 evtchn_port_t *ioreq_local_port;
76 /* the evtchn fd for polling */
77 XenEvtchn xce_handle;
78 /* which vcpu we are serving */
79 int send_vcpu;
80
81 struct xs_handle *xenstore;
82 CPUPhysMemoryClient client;
83 QLIST_HEAD(, XenPhysmap) physmap;
84 const XenPhysmap *log_for_dirtybit;
85
86 Notifier exit;
87 } XenIOState;
88
89 /* Xen specific function for piix pci */
90
91 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
92 {
93 return irq_num + ((pci_dev->devfn >> 3) << 2);
94 }
95
96 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
97 {
98 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
99 irq_num & 3, level);
100 }
101
102 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
103 {
104 int i;
105
106 /* Scan for updates to PCI link routes (0x60-0x63). */
107 for (i = 0; i < len; i++) {
108 uint8_t v = (val >> (8 * i)) & 0xff;
109 if (v & 0x80) {
110 v = 0;
111 }
112 v &= 0xf;
113 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
114 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
115 }
116 }
117 }
118
119 void xen_cmos_set_s3_resume(void *opaque, int irq, int level)
120 {
121 pc_cmos_set_s3_resume(opaque, irq, level);
122 if (level) {
123 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
124 }
125 }
126
127 /* Xen Interrupt Controller */
128
129 static void xen_set_irq(void *opaque, int irq, int level)
130 {
131 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
132 }
133
134 qemu_irq *xen_interrupt_controller_init(void)
135 {
136 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
137 }
138
139 /* Memory Ops */
140
141 static void xen_ram_init(ram_addr_t ram_size)
142 {
143 MemoryRegion *sysmem = get_system_memory();
144 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
145 ram_addr_t block_len;
146
147 block_len = 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
151 */
152 block_len += HVM_BELOW_4G_MMIO_LENGTH;
153 }
154 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len);
155
156 if (ram_size >= HVM_BELOW_4G_RAM_END) {
157 above_4g_mem_size = ram_size - HVM_BELOW_4G_RAM_END;
158 below_4g_mem_size = HVM_BELOW_4G_RAM_END;
159 } else {
160 below_4g_mem_size = ram_size;
161 }
162
163 memory_region_init_alias(&ram_640k, "xen.ram.640k",
164 &ram_memory, 0, 0xa0000);
165 memory_region_add_subregion(sysmem, 0, &ram_640k);
166 /* Skip of the VGA IO memory space, it will be registered later by the VGA
167 * emulated device.
168 *
169 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
170 * the Options ROM, so it is registered here as RAM.
171 */
172 memory_region_init_alias(&ram_lo, "xen.ram.lo",
173 &ram_memory, 0xc0000, below_4g_mem_size - 0xc0000);
174 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
175 if (above_4g_mem_size > 0) {
176 memory_region_init_alias(&ram_hi, "xen.ram.hi",
177 &ram_memory, 0x100000000ULL,
178 above_4g_mem_size);
179 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
180 }
181 }
182
183 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
184 {
185 unsigned long nr_pfn;
186 xen_pfn_t *pfn_list;
187 int i;
188
189 if (mr == &ram_memory) {
190 return;
191 }
192
193 trace_xen_ram_alloc(ram_addr, size);
194
195 nr_pfn = size >> TARGET_PAGE_BITS;
196 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
197
198 for (i = 0; i < nr_pfn; i++) {
199 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
200 }
201
202 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
203 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
204 }
205
206 g_free(pfn_list);
207 }
208
209 static XenPhysmap *get_physmapping(XenIOState *state,
210 target_phys_addr_t start_addr, ram_addr_t size)
211 {
212 XenPhysmap *physmap = NULL;
213
214 start_addr &= TARGET_PAGE_MASK;
215
216 QLIST_FOREACH(physmap, &state->physmap, list) {
217 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
218 return physmap;
219 }
220 }
221 return NULL;
222 }
223
224 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
225 static int xen_add_to_physmap(XenIOState *state,
226 target_phys_addr_t start_addr,
227 ram_addr_t size,
228 target_phys_addr_t phys_offset)
229 {
230 unsigned long i = 0;
231 int rc = 0;
232 XenPhysmap *physmap = NULL;
233 target_phys_addr_t pfn, start_gpfn;
234 RAMBlock *block;
235
236 if (get_physmapping(state, start_addr, size)) {
237 return 0;
238 }
239 if (size <= 0) {
240 return -1;
241 }
242
243 /* Xen can only handle a single dirty log region for now and we want
244 * the linear framebuffer to be that region.
245 * Avoid tracking any regions that is not videoram and avoid tracking
246 * the legacy vga region. */
247 QLIST_FOREACH(block, &ram_list.blocks, next) {
248 if (!strcmp(block->idstr, "vga.vram") && block->offset == phys_offset
249 && start_addr > 0xbffff) {
250 goto go_physmap;
251 }
252 }
253 return -1;
254
255 go_physmap:
256 DPRINTF("mapping vram to %llx - %llx, from %llx\n",
257 start_addr, start_addr + size, phys_offset);
258
259 pfn = phys_offset >> TARGET_PAGE_BITS;
260 start_gpfn = start_addr >> TARGET_PAGE_BITS;
261 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
262 unsigned long idx = pfn + i;
263 xen_pfn_t gpfn = start_gpfn + i;
264
265 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
266 if (rc) {
267 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
268 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
269 return -rc;
270 }
271 }
272
273 physmap = g_malloc(sizeof (XenPhysmap));
274
275 physmap->start_addr = start_addr;
276 physmap->size = size;
277 physmap->phys_offset = phys_offset;
278
279 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
280
281 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
282 start_addr >> TARGET_PAGE_BITS,
283 (start_addr + size) >> TARGET_PAGE_BITS,
284 XEN_DOMCTL_MEM_CACHEATTR_WB);
285 return 0;
286 }
287
288 static int xen_remove_from_physmap(XenIOState *state,
289 target_phys_addr_t start_addr,
290 ram_addr_t size)
291 {
292 unsigned long i = 0;
293 int rc = 0;
294 XenPhysmap *physmap = NULL;
295 target_phys_addr_t phys_offset = 0;
296
297 physmap = get_physmapping(state, start_addr, size);
298 if (physmap == NULL) {
299 return -1;
300 }
301
302 phys_offset = physmap->phys_offset;
303 size = physmap->size;
304
305 DPRINTF("unmapping vram to %llx - %llx, from %llx\n",
306 phys_offset, phys_offset + size, start_addr);
307
308 size >>= TARGET_PAGE_BITS;
309 start_addr >>= TARGET_PAGE_BITS;
310 phys_offset >>= TARGET_PAGE_BITS;
311 for (i = 0; i < size; i++) {
312 unsigned long idx = start_addr + i;
313 xen_pfn_t gpfn = phys_offset + i;
314
315 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
316 if (rc) {
317 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
318 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
319 return -rc;
320 }
321 }
322
323 QLIST_REMOVE(physmap, list);
324 if (state->log_for_dirtybit == physmap) {
325 state->log_for_dirtybit = NULL;
326 }
327 free(physmap);
328
329 return 0;
330 }
331
332 #else
333 static int xen_add_to_physmap(XenIOState *state,
334 target_phys_addr_t start_addr,
335 ram_addr_t size,
336 target_phys_addr_t phys_offset)
337 {
338 return -ENOSYS;
339 }
340
341 static int xen_remove_from_physmap(XenIOState *state,
342 target_phys_addr_t start_addr,
343 ram_addr_t size)
344 {
345 return -ENOSYS;
346 }
347 #endif
348
349 static void xen_client_set_memory(struct CPUPhysMemoryClient *client,
350 target_phys_addr_t start_addr,
351 ram_addr_t size,
352 ram_addr_t phys_offset,
353 bool log_dirty)
354 {
355 XenIOState *state = container_of(client, XenIOState, client);
356 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
357 hvmmem_type_t mem_type;
358
359 if (!(start_addr != phys_offset
360 && ( (log_dirty && flags < IO_MEM_UNASSIGNED)
361 || (!log_dirty && flags == IO_MEM_UNASSIGNED)))) {
362 return;
363 }
364
365 trace_xen_client_set_memory(start_addr, size, phys_offset, log_dirty);
366
367 start_addr &= TARGET_PAGE_MASK;
368 size = TARGET_PAGE_ALIGN(size);
369 phys_offset &= TARGET_PAGE_MASK;
370
371 switch (flags) {
372 case IO_MEM_RAM:
373 xen_add_to_physmap(state, start_addr, size, phys_offset);
374 break;
375 case IO_MEM_ROM:
376 mem_type = HVMMEM_ram_ro;
377 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
378 start_addr >> TARGET_PAGE_BITS,
379 size >> TARGET_PAGE_BITS)) {
380 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
381 start_addr);
382 }
383 break;
384 case IO_MEM_UNASSIGNED:
385 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
386 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
387 }
388 break;
389 }
390 }
391
392 static int xen_sync_dirty_bitmap(XenIOState *state,
393 target_phys_addr_t start_addr,
394 ram_addr_t size)
395 {
396 target_phys_addr_t npages = size >> TARGET_PAGE_BITS;
397 target_phys_addr_t vram_offset = 0;
398 const int width = sizeof(unsigned long) * 8;
399 unsigned long bitmap[(npages + width - 1) / width];
400 int rc, i, j;
401 const XenPhysmap *physmap = NULL;
402
403 physmap = get_physmapping(state, start_addr, size);
404 if (physmap == NULL) {
405 /* not handled */
406 return -1;
407 }
408
409 if (state->log_for_dirtybit == NULL) {
410 state->log_for_dirtybit = physmap;
411 } else if (state->log_for_dirtybit != physmap) {
412 return -1;
413 }
414 vram_offset = physmap->phys_offset;
415
416 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
417 start_addr >> TARGET_PAGE_BITS, npages,
418 bitmap);
419 if (rc) {
420 return rc;
421 }
422
423 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
424 unsigned long map = bitmap[i];
425 while (map != 0) {
426 j = ffsl(map) - 1;
427 map &= ~(1ul << j);
428 cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE);
429 };
430 }
431
432 return 0;
433 }
434
435 static int xen_log_start(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
436 {
437 XenIOState *state = container_of(client, XenIOState, client);
438
439 return xen_sync_dirty_bitmap(state, phys_addr, size);
440 }
441
442 static int xen_log_stop(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
443 {
444 XenIOState *state = container_of(client, XenIOState, client);
445
446 state->log_for_dirtybit = NULL;
447 /* Disable dirty bit tracking */
448 return xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
449 }
450
451 static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client,
452 target_phys_addr_t start_addr,
453 target_phys_addr_t end_addr)
454 {
455 XenIOState *state = container_of(client, XenIOState, client);
456
457 return xen_sync_dirty_bitmap(state, start_addr, end_addr - start_addr);
458 }
459
460 static int xen_client_migration_log(struct CPUPhysMemoryClient *client,
461 int enable)
462 {
463 return 0;
464 }
465
466 static CPUPhysMemoryClient xen_cpu_phys_memory_client = {
467 .set_memory = xen_client_set_memory,
468 .sync_dirty_bitmap = xen_client_sync_dirty_bitmap,
469 .migration_log = xen_client_migration_log,
470 .log_start = xen_log_start,
471 .log_stop = xen_log_stop,
472 };
473
474 /* VCPU Operations, MMIO, IO ring ... */
475
476 static void xen_reset_vcpu(void *opaque)
477 {
478 CPUState *env = opaque;
479
480 env->halted = 1;
481 }
482
483 void xen_vcpu_init(void)
484 {
485 CPUState *first_cpu;
486
487 if ((first_cpu = qemu_get_cpu(0))) {
488 qemu_register_reset(xen_reset_vcpu, first_cpu);
489 xen_reset_vcpu(first_cpu);
490 }
491 }
492
493 /* get the ioreq packets from share mem */
494 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
495 {
496 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
497
498 if (req->state != STATE_IOREQ_READY) {
499 DPRINTF("I/O request not ready: "
500 "%x, ptr: %x, port: %"PRIx64", "
501 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
502 req->state, req->data_is_ptr, req->addr,
503 req->data, req->count, req->size);
504 return NULL;
505 }
506
507 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
508
509 req->state = STATE_IOREQ_INPROCESS;
510 return req;
511 }
512
513 /* use poll to get the port notification */
514 /* ioreq_vec--out,the */
515 /* retval--the number of ioreq packet */
516 static ioreq_t *cpu_get_ioreq(XenIOState *state)
517 {
518 int i;
519 evtchn_port_t port;
520
521 port = xc_evtchn_pending(state->xce_handle);
522 if (port != -1) {
523 for (i = 0; i < smp_cpus; i++) {
524 if (state->ioreq_local_port[i] == port) {
525 break;
526 }
527 }
528
529 if (i == smp_cpus) {
530 hw_error("Fatal error while trying to get io event!\n");
531 }
532
533 /* unmask the wanted port again */
534 xc_evtchn_unmask(state->xce_handle, port);
535
536 /* get the io packet from shared memory */
537 state->send_vcpu = i;
538 return cpu_get_ioreq_from_shared_memory(state, i);
539 }
540
541 /* read error or read nothing */
542 return NULL;
543 }
544
545 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
546 {
547 switch (size) {
548 case 1:
549 return cpu_inb(addr);
550 case 2:
551 return cpu_inw(addr);
552 case 4:
553 return cpu_inl(addr);
554 default:
555 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
556 }
557 }
558
559 static void do_outp(pio_addr_t addr,
560 unsigned long size, uint32_t val)
561 {
562 switch (size) {
563 case 1:
564 return cpu_outb(addr, val);
565 case 2:
566 return cpu_outw(addr, val);
567 case 4:
568 return cpu_outl(addr, val);
569 default:
570 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
571 }
572 }
573
574 static void cpu_ioreq_pio(ioreq_t *req)
575 {
576 int i, sign;
577
578 sign = req->df ? -1 : 1;
579
580 if (req->dir == IOREQ_READ) {
581 if (!req->data_is_ptr) {
582 req->data = do_inp(req->addr, req->size);
583 } else {
584 uint32_t tmp;
585
586 for (i = 0; i < req->count; i++) {
587 tmp = do_inp(req->addr, req->size);
588 cpu_physical_memory_write(req->data + (sign * i * req->size),
589 (uint8_t *) &tmp, req->size);
590 }
591 }
592 } else if (req->dir == IOREQ_WRITE) {
593 if (!req->data_is_ptr) {
594 do_outp(req->addr, req->size, req->data);
595 } else {
596 for (i = 0; i < req->count; i++) {
597 uint32_t tmp = 0;
598
599 cpu_physical_memory_read(req->data + (sign * i * req->size),
600 (uint8_t*) &tmp, req->size);
601 do_outp(req->addr, req->size, tmp);
602 }
603 }
604 }
605 }
606
607 static void cpu_ioreq_move(ioreq_t *req)
608 {
609 int i, sign;
610
611 sign = req->df ? -1 : 1;
612
613 if (!req->data_is_ptr) {
614 if (req->dir == IOREQ_READ) {
615 for (i = 0; i < req->count; i++) {
616 cpu_physical_memory_read(req->addr + (sign * i * req->size),
617 (uint8_t *) &req->data, req->size);
618 }
619 } else if (req->dir == IOREQ_WRITE) {
620 for (i = 0; i < req->count; i++) {
621 cpu_physical_memory_write(req->addr + (sign * i * req->size),
622 (uint8_t *) &req->data, req->size);
623 }
624 }
625 } else {
626 uint64_t tmp;
627
628 if (req->dir == IOREQ_READ) {
629 for (i = 0; i < req->count; i++) {
630 cpu_physical_memory_read(req->addr + (sign * i * req->size),
631 (uint8_t*) &tmp, req->size);
632 cpu_physical_memory_write(req->data + (sign * i * req->size),
633 (uint8_t*) &tmp, req->size);
634 }
635 } else if (req->dir == IOREQ_WRITE) {
636 for (i = 0; i < req->count; i++) {
637 cpu_physical_memory_read(req->data + (sign * i * req->size),
638 (uint8_t*) &tmp, req->size);
639 cpu_physical_memory_write(req->addr + (sign * i * req->size),
640 (uint8_t*) &tmp, req->size);
641 }
642 }
643 }
644 }
645
646 static void handle_ioreq(ioreq_t *req)
647 {
648 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
649 (req->size < sizeof (target_ulong))) {
650 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
651 }
652
653 switch (req->type) {
654 case IOREQ_TYPE_PIO:
655 cpu_ioreq_pio(req);
656 break;
657 case IOREQ_TYPE_COPY:
658 cpu_ioreq_move(req);
659 break;
660 case IOREQ_TYPE_TIMEOFFSET:
661 break;
662 case IOREQ_TYPE_INVALIDATE:
663 xen_invalidate_map_cache();
664 break;
665 default:
666 hw_error("Invalid ioreq type 0x%x\n", req->type);
667 }
668 }
669
670 static void handle_buffered_iopage(XenIOState *state)
671 {
672 buf_ioreq_t *buf_req = NULL;
673 ioreq_t req;
674 int qw;
675
676 if (!state->buffered_io_page) {
677 return;
678 }
679
680 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
681 buf_req = &state->buffered_io_page->buf_ioreq[
682 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
683 req.size = 1UL << buf_req->size;
684 req.count = 1;
685 req.addr = buf_req->addr;
686 req.data = buf_req->data;
687 req.state = STATE_IOREQ_READY;
688 req.dir = buf_req->dir;
689 req.df = 1;
690 req.type = buf_req->type;
691 req.data_is_ptr = 0;
692 qw = (req.size == 8);
693 if (qw) {
694 buf_req = &state->buffered_io_page->buf_ioreq[
695 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
696 req.data |= ((uint64_t)buf_req->data) << 32;
697 }
698
699 handle_ioreq(&req);
700
701 xen_mb();
702 state->buffered_io_page->read_pointer += qw ? 2 : 1;
703 }
704 }
705
706 static void handle_buffered_io(void *opaque)
707 {
708 XenIOState *state = opaque;
709
710 handle_buffered_iopage(state);
711 qemu_mod_timer(state->buffered_io_timer,
712 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
713 }
714
715 static void cpu_handle_ioreq(void *opaque)
716 {
717 XenIOState *state = opaque;
718 ioreq_t *req = cpu_get_ioreq(state);
719
720 handle_buffered_iopage(state);
721 if (req) {
722 handle_ioreq(req);
723
724 if (req->state != STATE_IOREQ_INPROCESS) {
725 fprintf(stderr, "Badness in I/O request ... not in service?!: "
726 "%x, ptr: %x, port: %"PRIx64", "
727 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
728 req->state, req->data_is_ptr, req->addr,
729 req->data, req->count, req->size);
730 destroy_hvm_domain();
731 return;
732 }
733
734 xen_wmb(); /* Update ioreq contents /then/ update state. */
735
736 /*
737 * We do this before we send the response so that the tools
738 * have the opportunity to pick up on the reset before the
739 * guest resumes and does a hlt with interrupts disabled which
740 * causes Xen to powerdown the domain.
741 */
742 if (runstate_is_running()) {
743 if (qemu_shutdown_requested_get()) {
744 destroy_hvm_domain();
745 }
746 if (qemu_reset_requested_get()) {
747 qemu_system_reset(VMRESET_REPORT);
748 }
749 }
750
751 req->state = STATE_IORESP_READY;
752 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
753 }
754 }
755
756 static int store_dev_info(int domid, CharDriverState *cs, const char *string)
757 {
758 struct xs_handle *xs = NULL;
759 char *path = NULL;
760 char *newpath = NULL;
761 char *pts = NULL;
762 int ret = -1;
763
764 /* Only continue if we're talking to a pty. */
765 if (strncmp(cs->filename, "pty:", 4)) {
766 return 0;
767 }
768 pts = cs->filename + 4;
769
770 /* We now have everything we need to set the xenstore entry. */
771 xs = xs_open(0);
772 if (xs == NULL) {
773 fprintf(stderr, "Could not contact XenStore\n");
774 goto out;
775 }
776
777 path = xs_get_domain_path(xs, domid);
778 if (path == NULL) {
779 fprintf(stderr, "xs_get_domain_path() error\n");
780 goto out;
781 }
782 newpath = realloc(path, (strlen(path) + strlen(string) +
783 strlen("/tty") + 1));
784 if (newpath == NULL) {
785 fprintf(stderr, "realloc error\n");
786 goto out;
787 }
788 path = newpath;
789
790 strcat(path, string);
791 strcat(path, "/tty");
792 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
793 fprintf(stderr, "xs_write for '%s' fail", string);
794 goto out;
795 }
796 ret = 0;
797
798 out:
799 free(path);
800 xs_close(xs);
801
802 return ret;
803 }
804
805 void xenstore_store_pv_console_info(int i, CharDriverState *chr)
806 {
807 if (i == 0) {
808 store_dev_info(xen_domid, chr, "/console");
809 } else {
810 char buf[32];
811 snprintf(buf, sizeof(buf), "/device/console/%d", i);
812 store_dev_info(xen_domid, chr, buf);
813 }
814 }
815
816 static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
817 {
818 char path[50];
819
820 if (xs == NULL) {
821 fprintf(stderr, "xenstore connection not initialized\n");
822 exit(1);
823 }
824
825 snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
826 if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
827 fprintf(stderr, "error recording dm state\n");
828 exit(1);
829 }
830 }
831
832 static void xen_main_loop_prepare(XenIOState *state)
833 {
834 int evtchn_fd = -1;
835
836 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
837 evtchn_fd = xc_evtchn_fd(state->xce_handle);
838 }
839
840 state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
841 state);
842 qemu_mod_timer(state->buffered_io_timer, qemu_get_clock_ms(rt_clock));
843
844 if (evtchn_fd != -1) {
845 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
846 }
847 }
848
849
850 /* Initialise Xen */
851
852 static void xen_change_state_handler(void *opaque, int running,
853 RunState state)
854 {
855 if (running) {
856 /* record state running */
857 xenstore_record_dm_state(xenstore, "running");
858 }
859 }
860
861 static void xen_hvm_change_state_handler(void *opaque, int running,
862 RunState rstate)
863 {
864 XenIOState *xstate = opaque;
865 if (running) {
866 xen_main_loop_prepare(xstate);
867 }
868 }
869
870 static void xen_exit_notifier(Notifier *n, void *data)
871 {
872 XenIOState *state = container_of(n, XenIOState, exit);
873
874 xc_evtchn_close(state->xce_handle);
875 xs_daemon_close(state->xenstore);
876 }
877
878 int xen_init(void)
879 {
880 xen_xc = xen_xc_interface_open(0, 0, 0);
881 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
882 xen_be_printf(NULL, 0, "can't open xen interface\n");
883 return -1;
884 }
885 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
886
887 return 0;
888 }
889
890 int xen_hvm_init(void)
891 {
892 int i, rc;
893 unsigned long ioreq_pfn;
894 XenIOState *state;
895
896 state = g_malloc0(sizeof (XenIOState));
897
898 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
899 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
900 perror("xen: event channel open");
901 return -errno;
902 }
903
904 state->xenstore = xs_daemon_open();
905 if (state->xenstore == NULL) {
906 perror("xen: xenstore open");
907 return -errno;
908 }
909
910 state->exit.notify = xen_exit_notifier;
911 qemu_add_exit_notifier(&state->exit);
912
913 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
914 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
915 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
916 PROT_READ|PROT_WRITE, ioreq_pfn);
917 if (state->shared_page == NULL) {
918 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
919 errno, xen_xc);
920 }
921
922 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
923 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
924 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
925 PROT_READ|PROT_WRITE, ioreq_pfn);
926 if (state->buffered_io_page == NULL) {
927 hw_error("map buffered IO page returned error %d", errno);
928 }
929
930 state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
931
932 /* FIXME: how about if we overflow the page here? */
933 for (i = 0; i < smp_cpus; i++) {
934 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
935 xen_vcpu_eport(state->shared_page, i));
936 if (rc == -1) {
937 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
938 return -1;
939 }
940 state->ioreq_local_port[i] = rc;
941 }
942
943 /* Init RAM management */
944 xen_map_cache_init();
945 xen_ram_init(ram_size);
946
947 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
948
949 state->client = xen_cpu_phys_memory_client;
950 QLIST_INIT(&state->physmap);
951 cpu_register_phys_memory_client(&state->client);
952 state->log_for_dirtybit = NULL;
953
954 /* Initialize backend core & drivers */
955 if (xen_be_init() != 0) {
956 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
957 exit(1);
958 }
959 xen_be_register("console", &xen_console_ops);
960 xen_be_register("vkbd", &xen_kbdmouse_ops);
961 xen_be_register("qdisk", &xen_blkdev_ops);
962
963 return 0;
964 }
965
966 void destroy_hvm_domain(void)
967 {
968 XenXC xc_handle;
969 int sts;
970
971 xc_handle = xen_xc_interface_open(0, 0, 0);
972 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
973 fprintf(stderr, "Cannot acquire xenctrl handle\n");
974 } else {
975 sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
976 if (sts != 0) {
977 fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
978 "sts %d, %s\n", sts, strerror(errno));
979 } else {
980 fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
981 }
982 xc_interface_close(xc_handle);
983 }
984 }