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