qemu/xen-all.c
<<
>>
Prefs
   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
  20#include <xen/hvm/ioreq.h>
  21#include <xen/hvm/params.h>
  22#include <xen/hvm/e820.h>
  23
  24//#define DEBUG_XEN
  25
  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
  33
  34/* Compatibility with older version */
  35#if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
  36static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
  37{
  38    return shared_page->vcpu_iodata[i].vp_eport;
  39}
  40static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
  41{
  42    return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
  43}
  44#  define FMT_ioreq_size PRIx64
  45#else
  46static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
  47{
  48    return shared_page->vcpu_ioreq[i].vp_eport;
  49}
  50static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
  51{
  52    return &shared_page->vcpu_ioreq[vcpu];
  53}
  54#  define FMT_ioreq_size "u"
  55#endif
  56
  57#define BUFFER_IO_MAX_DELAY  100
  58
  59typedef struct XenPhysmap {
  60    target_phys_addr_t start_addr;
  61    ram_addr_t size;
  62    target_phys_addr_t phys_offset;
  63
  64    QLIST_ENTRY(XenPhysmap) list;
  65} XenPhysmap;
  66
  67typedef 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;
  77
  78    struct xs_handle *xenstore;
  79    CPUPhysMemoryClient client;
  80    QLIST_HEAD(, XenPhysmap) physmap;
  81    const XenPhysmap *log_for_dirtybit;
  82
  83    Notifier exit;
  84} XenIOState;
  85
  86/* Xen specific function for piix pci */
  87
  88int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
  89{
  90    return irq_num + ((pci_dev->devfn >> 3) << 2);
  91}
  92
  93void xen_piix3_set_irq(void *opaque, int irq_num, int level)
  94{
  95    xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
  96                              irq_num & 3, level);
  97}
  98
  99void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
 100{
 101    int i;
 102
 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;
 108        }
 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);
 112        }
 113    }
 114}
 115
 116void xen_cmos_set_s3_resume(void *opaque, int irq, int level)
 117{
 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);
 121    }
 122}
 123
 124/* Xen Interrupt Controller */
 125
 126static void xen_set_irq(void *opaque, int irq, int level)
 127{
 128    xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
 129}
 130
 131qemu_irq *xen_interrupt_controller_init(void)
 132{
 133    return qemu_allocate_irqs(xen_set_irq, NULL, 16);
 134}
 135
 136/* Memory Ops */
 137
 138static void xen_ram_init(ram_addr_t ram_size)
 139{
 140    RAMBlock *new_block;
 141    ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
 142
 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
 151         */
 152        new_block->length += HVM_BELOW_4G_MMIO_LENGTH;
 153    }
 154
 155    QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
 156
 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);
 161
 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;
 167    }
 168
 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.
 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     */
 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);
 181    }
 182}
 183
 184void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size)
 185{
 186    unsigned long nr_pfn;
 187    xen_pfn_t *pfn_list;
 188    int i;
 189
 190    trace_xen_ram_alloc(ram_addr, size);
 191
 192    nr_pfn = size >> TARGET_PAGE_BITS;
 193    pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
 194
 195    for (i = 0; i < nr_pfn; i++) {
 196        pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
 197    }
 198
 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);
 201    }
 202
 203    g_free(pfn_list);
 204}
 205
 206static XenPhysmap *get_physmapping(XenIOState *state,
 207                                   target_phys_addr_t start_addr, ram_addr_t size)
 208{
 209    XenPhysmap *physmap = NULL;
 210
 211    start_addr &= TARGET_PAGE_MASK;
 212
 213    QLIST_FOREACH(physmap, &state->physmap, list) {
 214        if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
 215            return physmap;
 216        }
 217    }
 218    return NULL;
 219}
 220
 221#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
 222static 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)
 226{
 227    unsigned long i = 0;
 228    int rc = 0;
 229    XenPhysmap *physmap = NULL;
 230    target_phys_addr_t pfn, start_gpfn;
 231    RAMBlock *block;
 232
 233    if (get_physmapping(state, start_addr, size)) {
 234        return 0;
 235    }
 236    if (size <= 0) {
 237        return -1;
 238    }
 239
 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;
 248        }
 249    }
 250    return -1;
 251
 252go_physmap:
 253    DPRINTF("mapping vram to %llx - %llx, from %llx\n",
 254            start_addr, start_addr + size, phys_offset);
 255
 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;
 261
 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;
 267        }
 268    }
 269
 270    physmap = g_malloc(sizeof (XenPhysmap));
 271
 272    physmap->start_addr = start_addr;
 273    physmap->size = size;
 274    physmap->phys_offset = phys_offset;
 275
 276    QLIST_INSERT_HEAD(&state->physmap, physmap, list);
 277
 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;
 283}
 284
 285static int xen_remove_from_physmap(XenIOState *state,
 286                                   target_phys_addr_t start_addr,
 287                                   ram_addr_t size)
 288{
 289    unsigned long i = 0;
 290    int rc = 0;
 291    XenPhysmap *physmap = NULL;
 292    target_phys_addr_t phys_offset = 0;
 293
 294    physmap = get_physmapping(state, start_addr, size);
 295    if (physmap == NULL) {
 296        return -1;
 297    }
 298
 299    phys_offset = physmap->phys_offset;
 300    size = physmap->size;
 301
 302    DPRINTF("unmapping vram to %llx - %llx, from %llx\n",
 303            phys_offset, phys_offset + size, start_addr);
 304
 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;
 311
 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;
 317        }
 318    }
 319
 320    QLIST_REMOVE(physmap, list);
 321    if (state->log_for_dirtybit == physmap) {
 322        state->log_for_dirtybit = NULL;
 323    }
 324    free(physmap);
 325
 326    return 0;
 327}
 328
 329#else
 330static 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)
 334{
 335    return -ENOSYS;
 336}
 337
 338static int xen_remove_from_physmap(XenIOState *state,
 339                                   target_phys_addr_t start_addr,
 340                                   ram_addr_t size)
 341{
 342    return -ENOSYS;
 343}
 344#endif
 345
 346static 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)
 351{
 352    XenIOState *state = container_of(client, XenIOState, client);
 353    ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
 354    hvmmem_type_t mem_type;
 355
 356    if (!(start_addr != phys_offset
 357          && ( (log_dirty && flags < IO_MEM_UNASSIGNED)
 358               || (!log_dirty && flags == IO_MEM_UNASSIGNED)))) {
 359        return;
 360    }
 361
 362    trace_xen_client_set_memory(start_addr, size, phys_offset, log_dirty);
 363
 364    start_addr &= TARGET_PAGE_MASK;
 365    size = TARGET_PAGE_ALIGN(size);
 366    phys_offset &= TARGET_PAGE_MASK;
 367
 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);
 379        }
 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);
 384        }
 385        break;
 386    }
 387}
 388
 389static int xen_sync_dirty_bitmap(XenIOState *state,
 390                                 target_phys_addr_t start_addr,
 391                                 ram_addr_t size)
 392{
 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;
 399
 400    physmap = get_physmapping(state, start_addr, size);
 401    if (physmap == NULL) {
 402        /* not handled */
 403        return -1;
 404    }
 405
 406    if (state->log_for_dirtybit == NULL) {
 407        state->log_for_dirtybit = physmap;
 408    } else if (state->log_for_dirtybit != physmap) {
 409        return -1;
 410    }
 411    vram_offset = physmap->phys_offset;
 412
 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;
 418    }
 419
 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);
 426        };
 427    }
 428
 429    return 0;
 430}
 431
 432static int xen_log_start(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
 433{
 434    XenIOState *state = container_of(client, XenIOState, client);
 435
 436    return xen_sync_dirty_bitmap(state, phys_addr, size);
 437}
 438
 439static int xen_log_stop(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
 440{
 441    XenIOState *state = container_of(client, XenIOState, client);
 442
 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);
 446}
 447
 448static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client,
 449                                        target_phys_addr_t start_addr,
 450                                        target_phys_addr_t end_addr)
 451{
 452    XenIOState *state = container_of(client, XenIOState, client);
 453
 454    return xen_sync_dirty_bitmap(state, start_addr, end_addr - start_addr);
 455}
 456
 457static int xen_client_migration_log(struct CPUPhysMemoryClient *client,
 458                                    int enable)
 459{
 460    return 0;
 461}
 462
 463static 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,
 469};
 470
 471/* VCPU Operations, MMIO, IO ring ... */
 472
 473static void xen_reset_vcpu(void *opaque)
 474{
 475    CPUState *env = opaque;
 476
 477    env->halted = 1;
 478}
 479
 480void xen_vcpu_init(void)
 481{
 482    CPUState *first_cpu;
 483
 484    if ((first_cpu = qemu_get_cpu(0))) {
 485        qemu_register_reset(xen_reset_vcpu, first_cpu);
 486        xen_reset_vcpu(first_cpu);
 487    }
 488}
 489
 490/* get the ioreq packets from share mem */
 491static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
 492{
 493    ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
 494
 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;
 502    }
 503
 504    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
 505
 506    req->state = STATE_IOREQ_INPROCESS;
 507    return req;
 508}
 509
 510/* use poll to get the port notification */
 511/* ioreq_vec--out,the */
 512/* retval--the number of ioreq packet */
 513static ioreq_t *cpu_get_ioreq(XenIOState *state)
 514{
 515    int i;
 516    evtchn_port_t port;
 517
 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;
 523            }
 524        }
 525
 526        if (i == smp_cpus) {
 527            hw_error("Fatal error while trying to get io event!\n");
 528        }
 529
 530        /* unmask the wanted port again */
 531        xc_evtchn_unmask(state->xce_handle, port);
 532
 533        /* get the io packet from shared memory */
 534        state->send_vcpu = i;
 535        return cpu_get_ioreq_from_shared_memory(state, i);
 536    }
 537
 538    /* read error or read nothing */
 539    return NULL;
 540}
 541
 542static uint32_t do_inp(pio_addr_t addr, unsigned long size)
 543{
 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);
 553    }
 554}
 555
 556static void do_outp(pio_addr_t addr,
 557        unsigned long size, uint32_t val)
 558{
 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);
 568    }
 569}
 570
 571static void cpu_ioreq_pio(ioreq_t *req)
 572{
 573    int i, sign;
 574
 575    sign = req->df ? -1 : 1;
 576
 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;
 582
 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);
 587            }
 588        }
 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;
 595
 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);
 599            }
 600        }
 601    }
 602}
 603
 604static void cpu_ioreq_move(ioreq_t *req)
 605{
 606    int i, sign;
 607
 608    sign = req->df ? -1 : 1;
 609
 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);
 615            }
 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);
 620            }
 621        }
 622    } else {
 623        uint64_t tmp;
 624
 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);
 631            }
 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);
 638            }
 639        }
 640    }
 641}
 642
 643static void handle_ioreq(ioreq_t *req)
 644{
 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;
 648    }
 649
 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);
 664    }
 665}
 666
 667static void handle_buffered_iopage(XenIOState *state)
 668{
 669    buf_ioreq_t *buf_req = NULL;
 670    ioreq_t req;
 671    int qw;
 672
 673    if (!state->buffered_io_page) {
 674        return;
 675    }
 676
 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;
 694        }
 695
 696        handle_ioreq(&req);
 697
 698        xen_mb();
 699        state->buffered_io_page->read_pointer += qw ? 2 : 1;
 700    }
 701}
 702
 703static void handle_buffered_io(void *opaque)
 704{
 705    XenIOState *state = opaque;
 706
 707    handle_buffered_iopage(state);
 708    qemu_mod_timer(state->buffered_io_timer,
 709                   BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
 710}
 711
 712static void cpu_handle_ioreq(void *opaque)
 713{
 714    XenIOState *state = opaque;
 715    ioreq_t *req = cpu_get_ioreq(state);
 716
 717    handle_buffered_iopage(state);
 718    if (req) {
 719        handle_ioreq(req);
 720
 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;
 729        }
 730
 731        xen_wmb(); /* Update ioreq contents /then/ update state. */
 732
 733        /*
 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.
 738         */
 739        if (runstate_is_running()) {
 740            if (qemu_shutdown_requested_get()) {
 741                destroy_hvm_domain();
 742            }
 743            if (qemu_reset_requested_get()) {
 744                qemu_system_reset(VMRESET_REPORT);
 745            }
 746        }
 747
 748        req->state = STATE_IORESP_READY;
 749        xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
 750    }
 751}
 752
 753static int store_dev_info(int domid, CharDriverState *cs, const char *string)
 754{
 755    struct xs_handle *xs = NULL;
 756    char *path = NULL;
 757    char *newpath = NULL;
 758    char *pts = NULL;
 759    int ret = -1;
 760
 761    /* Only continue if we're talking to a pty. */
 762    if (strncmp(cs->filename, "pty:", 4)) {
 763        return 0;
 764    }
 765    pts = cs->filename + 4;
 766
 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;
 772    }
 773
 774    path = xs_get_domain_path(xs, domid);
 775    if (path == NULL) {
 776        fprintf(stderr, "xs_get_domain_path() error\n");
 777        goto out;
 778    }
 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;
 784    }
 785    path = newpath;
 786
 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;
 792    }
 793    ret = 0;
 794
 795out:
 796    free(path);
 797    xs_close(xs);
 798
 799    return ret;
 800}
 801
 802void xenstore_store_pv_console_info(int i, CharDriverState *chr)
 803{
 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);
 810    }
 811}
 812
 813static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
 814{
 815    char path[50];
 816
 817    if (xs == NULL) {
 818        fprintf(stderr, "xenstore connection not initialized\n");
 819        exit(1);
 820    }
 821
 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);
 826    }
 827}
 828
 829static void xen_main_loop_prepare(XenIOState *state)
 830{
 831    int evtchn_fd = -1;
 832
 833    if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
 834        evtchn_fd = xc_evtchn_fd(state->xce_handle);
 835    }
 836
 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));
 840
 841    if (evtchn_fd != -1) {
 842        qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
 843    }
 844}
 845
 846
 847/* Initialise Xen */
 848
 849static void xen_change_state_handler(void *opaque, int running,
 850                                     RunState state)
 851{
 852    if (running) {
 853        /* record state running */
 854        xenstore_record_dm_state(xenstore, "running");
 855    }
 856}
 857
 858static void xen_hvm_change_state_handler(void *opaque, int running,
 859                                         RunState rstate)
 860{
 861    XenIOState *xstate = opaque;
 862    if (running) {
 863        xen_main_loop_prepare(xstate);
 864    }
 865}
 866
 867static void xen_exit_notifier(Notifier *n, void *data)
 868{
 869    XenIOState *state = container_of(n, XenIOState, exit);
 870
 871    xc_evtchn_close(state->xce_handle);
 872    xs_daemon_close(state->xenstore);
 873}
 874
 875int xen_init(void)
 876{
 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");
 880        return -1;
 881    }
 882    qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
 883
 884    return 0;
 885}
 886
 887int xen_hvm_init(void)
 888{
 889    int i, rc;
 890    unsigned long ioreq_pfn;
 891    XenIOState *state;
 892
 893    state = g_malloc0(sizeof (XenIOState));
 894
 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");
 898        return -errno;
 899    }
 900
 901    state->xenstore = xs_daemon_open();
 902    if (state->xenstore == NULL) {
 903        perror("xen: xenstore open");
 904        return -errno;
 905    }
 906
 907    state->exit.notify = xen_exit_notifier;
 908    qemu_add_exit_notifier(&state->exit);
 909
 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,
 916                 errno, xen_xc);
 917    }
 918
 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);
 925    }
 926
 927    state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
 928
 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));
 933        if (rc == -1) {
 934            fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
 935            return -1;
 936        }
 937        state->ioreq_local_port[i] = rc;
 938    }
 939
 940    /* Init RAM management */
 941    xen_map_cache_init();
 942    xen_ram_init(ram_size);
 943
 944    qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
 945
 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;
 950
 951    /* Initialize backend core & drivers */
 952    if (xen_be_init() != 0) {
 953        fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
 954        exit(1);
 955    }
 956    xen_be_register("console", &xen_console_ops);
 957    xen_be_register("vkbd", &xen_kbdmouse_ops);
 958    xen_be_register("qdisk", &xen_blkdev_ops);
 959
 960    return 0;
 961}
 962
 963void destroy_hvm_domain(void)
 964{
 965    XenXC xc_handle;
 966    int sts;
 967
 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");
 971    } else {
 972        sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
 973        if (sts != 0) {
 974            fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
 975                    "sts %d, %s\n", sts, strerror(errno));
 976        } else {
 977            fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
 978        }
 979        xc_interface_close(xc_handle);
 980    }
 981}
 982