qemu/hw/xen/xen-common.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2014       Citrix Systems UK 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 * Contributions after 2012-01-13 are licensed under the terms of the
   8 * GNU GPL, version 2 or (at your option) any later version.
   9 */
  10
  11#include "qemu/osdep.h"
  12#include "qemu/error-report.h"
  13#include "qemu/module.h"
  14#include "hw/xen/xen-legacy-backend.h"
  15#include "chardev/char.h"
  16#include "sysemu/accel.h"
  17#include "migration/misc.h"
  18#include "migration/global_state.h"
  19
  20//#define DEBUG_XEN
  21
  22#ifdef DEBUG_XEN
  23#define DPRINTF(fmt, ...) \
  24    do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
  25#else
  26#define DPRINTF(fmt, ...) \
  27    do { } while (0)
  28#endif
  29
  30xc_interface *xen_xc;
  31xenforeignmemory_handle *xen_fmem;
  32xendevicemodel_handle *xen_dmod;
  33
  34static int store_dev_info(int domid, Chardev *cs, const char *string)
  35{
  36    struct xs_handle *xs = NULL;
  37    char *path = NULL;
  38    char *newpath = NULL;
  39    char *pts = NULL;
  40    int ret = -1;
  41
  42    /* Only continue if we're talking to a pty. */
  43    if (!CHARDEV_IS_PTY(cs)) {
  44        return 0;
  45    }
  46    pts = cs->filename + 4;
  47
  48    /* We now have everything we need to set the xenstore entry. */
  49    xs = xs_open(0);
  50    if (xs == NULL) {
  51        fprintf(stderr, "Could not contact XenStore\n");
  52        goto out;
  53    }
  54
  55    path = xs_get_domain_path(xs, domid);
  56    if (path == NULL) {
  57        fprintf(stderr, "xs_get_domain_path() error\n");
  58        goto out;
  59    }
  60    newpath = realloc(path, (strlen(path) + strlen(string) +
  61                strlen("/tty") + 1));
  62    if (newpath == NULL) {
  63        fprintf(stderr, "realloc error\n");
  64        goto out;
  65    }
  66    path = newpath;
  67
  68    strcat(path, string);
  69    strcat(path, "/tty");
  70    if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  71        fprintf(stderr, "xs_write for '%s' fail", string);
  72        goto out;
  73    }
  74    ret = 0;
  75
  76out:
  77    free(path);
  78    xs_close(xs);
  79
  80    return ret;
  81}
  82
  83void xenstore_store_pv_console_info(int i, Chardev *chr)
  84{
  85    if (i == 0) {
  86        store_dev_info(xen_domid, chr, "/console");
  87    } else {
  88        char buf[32];
  89        snprintf(buf, sizeof(buf), "/device/console/%d", i);
  90        store_dev_info(xen_domid, chr, buf);
  91    }
  92}
  93
  94
  95static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  96{
  97    char path[50];
  98
  99    if (xs == NULL) {
 100        error_report("xenstore connection not initialized");
 101        exit(1);
 102    }
 103
 104    snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
 105    /*
 106     * This call may fail when running restricted so don't make it fatal in
 107     * that case. Toolstacks should instead use QMP to listen for state changes.
 108     */
 109    if (!xs_write(xs, XBT_NULL, path, state, strlen(state)) &&
 110            !xen_domid_restrict) {
 111        error_report("error recording dm state");
 112        exit(1);
 113    }
 114}
 115
 116
 117static void xen_change_state_handler(void *opaque, int running,
 118                                     RunState state)
 119{
 120    if (running) {
 121        /* record state running */
 122        xenstore_record_dm_state(xenstore, "running");
 123    }
 124}
 125
 126static void xen_setup_post(MachineState *ms, AccelState *accel)
 127{
 128    int rc;
 129
 130    if (xen_domid_restrict) {
 131        rc = xen_restrict(xen_domid);
 132        if (rc < 0) {
 133            perror("xen: failed to restrict");
 134            exit(1);
 135        }
 136    }
 137}
 138
 139static int xen_init(MachineState *ms)
 140{
 141    xen_xc = xc_interface_open(0, 0, 0);
 142    if (xen_xc == NULL) {
 143        xen_pv_printf(NULL, 0, "can't open xen interface\n");
 144        return -1;
 145    }
 146    xen_fmem = xenforeignmemory_open(0, 0);
 147    if (xen_fmem == NULL) {
 148        xen_pv_printf(NULL, 0, "can't open xen fmem interface\n");
 149        xc_interface_close(xen_xc);
 150        return -1;
 151    }
 152    xen_dmod = xendevicemodel_open(0, 0);
 153    if (xen_dmod == NULL) {
 154        xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n");
 155        xenforeignmemory_close(xen_fmem);
 156        xc_interface_close(xen_xc);
 157        return -1;
 158    }
 159    qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
 160    return 0;
 161}
 162
 163static void xen_accel_class_init(ObjectClass *oc, void *data)
 164{
 165    AccelClass *ac = ACCEL_CLASS(oc);
 166    static GlobalProperty compat[] = {
 167        { "migration", "store-global-state", "off" },
 168        { "migration", "send-configuration", "off" },
 169        { "migration", "send-section-footer", "off" },
 170    };
 171
 172    ac->name = "Xen";
 173    ac->init_machine = xen_init;
 174    ac->setup_post = xen_setup_post;
 175    ac->allowed = &xen_allowed;
 176    ac->compat_props = g_ptr_array_new();
 177
 178    compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat));
 179}
 180
 181#define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
 182
 183static const TypeInfo xen_accel_type = {
 184    .name = TYPE_XEN_ACCEL,
 185    .parent = TYPE_ACCEL,
 186    .class_init = xen_accel_class_init,
 187};
 188
 189static void xen_type_init(void)
 190{
 191    type_register_static(&xen_accel_type);
 192}
 193
 194type_init(xen_type_init);
 195