qemu/hw/char/xen_console.c
<<
>>
Prefs
   1/*
   2 *  Copyright (C) International Business Machines  Corp., 2005
   3 *  Author(s): Anthony Liguori <aliguori@us.ibm.com>
   4 *
   5 *  Copyright (C) Red Hat 2007
   6 *
   7 *  Xen Console
   8 *
   9 *  This program is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation; under version 2 of the License.
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *  GNU General Public License for more details.
  17 *
  18 *  You should have received a copy of the GNU General Public License along
  19 *  with this program; if not, see <http://www.gnu.org/licenses/>.
  20 */
  21
  22#include "qemu/osdep.h"
  23#include <sys/select.h>
  24#include <termios.h>
  25#include <sys/mman.h>
  26
  27#include "hw/hw.h"
  28#include "sysemu/char.h"
  29#include "hw/xen/xen_backend.h"
  30
  31#include <xen/io/console.h>
  32
  33struct buffer {
  34    uint8_t *data;
  35    size_t consumed;
  36    size_t size;
  37    size_t capacity;
  38    size_t max_capacity;
  39};
  40
  41struct XenConsole {
  42    struct XenDevice  xendev;  /* must be first */
  43    struct buffer     buffer;
  44    char              console[XEN_BUFSIZE];
  45    int               ring_ref;
  46    void              *sring;
  47    CharDriverState   *chr;
  48    int               backlog;
  49};
  50
  51static void buffer_append(struct XenConsole *con)
  52{
  53    struct buffer *buffer = &con->buffer;
  54    XENCONS_RING_IDX cons, prod, size;
  55    struct xencons_interface *intf = con->sring;
  56
  57    cons = intf->out_cons;
  58    prod = intf->out_prod;
  59    xen_mb();
  60
  61    size = prod - cons;
  62    if ((size == 0) || (size > sizeof(intf->out)))
  63        return;
  64
  65    if ((buffer->capacity - buffer->size) < size) {
  66        buffer->capacity += (size + 1024);
  67        buffer->data = g_realloc(buffer->data, buffer->capacity);
  68    }
  69
  70    while (cons != prod)
  71        buffer->data[buffer->size++] = intf->out[
  72            MASK_XENCONS_IDX(cons++, intf->out)];
  73
  74    xen_mb();
  75    intf->out_cons = cons;
  76    xen_be_send_notify(&con->xendev);
  77
  78    if (buffer->max_capacity &&
  79        buffer->size > buffer->max_capacity) {
  80        /* Discard the middle of the data. */
  81
  82        size_t over = buffer->size - buffer->max_capacity;
  83        uint8_t *maxpos = buffer->data + buffer->max_capacity;
  84
  85        memmove(maxpos - over, maxpos, over);
  86        buffer->data = g_realloc(buffer->data, buffer->max_capacity);
  87        buffer->size = buffer->capacity = buffer->max_capacity;
  88
  89        if (buffer->consumed > buffer->max_capacity - over)
  90            buffer->consumed = buffer->max_capacity - over;
  91    }
  92}
  93
  94static void buffer_advance(struct buffer *buffer, size_t len)
  95{
  96    buffer->consumed += len;
  97    if (buffer->consumed == buffer->size) {
  98        buffer->consumed = 0;
  99        buffer->size = 0;
 100    }
 101}
 102
 103static int ring_free_bytes(struct XenConsole *con)
 104{
 105    struct xencons_interface *intf = con->sring;
 106    XENCONS_RING_IDX cons, prod, space;
 107
 108    cons = intf->in_cons;
 109    prod = intf->in_prod;
 110    xen_mb();
 111
 112    space = prod - cons;
 113    if (space > sizeof(intf->in))
 114        return 0; /* ring is screwed: ignore it */
 115
 116    return (sizeof(intf->in) - space);
 117}
 118
 119static int xencons_can_receive(void *opaque)
 120{
 121    struct XenConsole *con = opaque;
 122    return ring_free_bytes(con);
 123}
 124
 125static void xencons_receive(void *opaque, const uint8_t *buf, int len)
 126{
 127    struct XenConsole *con = opaque;
 128    struct xencons_interface *intf = con->sring;
 129    XENCONS_RING_IDX prod;
 130    int i, max;
 131
 132    max = ring_free_bytes(con);
 133    /* The can_receive() func limits this, but check again anyway */
 134    if (max < len)
 135        len = max;
 136
 137    prod = intf->in_prod;
 138    for (i = 0; i < len; i++) {
 139        intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
 140            buf[i];
 141    }
 142    xen_wmb();
 143    intf->in_prod = prod;
 144    xen_be_send_notify(&con->xendev);
 145}
 146
 147static void xencons_send(struct XenConsole *con)
 148{
 149    ssize_t len, size;
 150
 151    size = con->buffer.size - con->buffer.consumed;
 152    if (con->chr)
 153        len = qemu_chr_fe_write(con->chr, con->buffer.data + con->buffer.consumed,
 154                             size);
 155    else
 156        len = size;
 157    if (len < 1) {
 158        if (!con->backlog) {
 159            con->backlog = 1;
 160            xen_be_printf(&con->xendev, 1, "backlog piling up, nobody listening?\n");
 161        }
 162    } else {
 163        buffer_advance(&con->buffer, len);
 164        if (con->backlog && len == size) {
 165            con->backlog = 0;
 166            xen_be_printf(&con->xendev, 1, "backlog is gone\n");
 167        }
 168    }
 169}
 170
 171/* -------------------------------------------------------------------- */
 172
 173static int con_init(struct XenDevice *xendev)
 174{
 175    struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 176    char *type, *dom, label[32];
 177    int ret = 0;
 178    const char *output;
 179
 180    /* setup */
 181    dom = xs_get_domain_path(xenstore, con->xendev.dom);
 182    if (!xendev->dev) {
 183        snprintf(con->console, sizeof(con->console), "%s/console", dom);
 184    } else {
 185        snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
 186    }
 187    free(dom);
 188
 189    type = xenstore_read_str(con->console, "type");
 190    if (!type || strcmp(type, "ioemu") != 0) {
 191        xen_be_printf(xendev, 1, "not for me (type=%s)\n", type);
 192        ret = -1;
 193        goto out;
 194    }
 195
 196    output = xenstore_read_str(con->console, "output");
 197
 198    /* no Xen override, use qemu output device */
 199    if (output == NULL) {
 200        con->chr = serial_hds[con->xendev.dev];
 201    } else {
 202        snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
 203        con->chr = qemu_chr_new(label, output, NULL);
 204    }
 205
 206    xenstore_store_pv_console_info(con->xendev.dev, con->chr);
 207
 208out:
 209    g_free(type);
 210    return ret;
 211}
 212
 213static int con_initialise(struct XenDevice *xendev)
 214{
 215    struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 216    int limit;
 217
 218    if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
 219        return -1;
 220    if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
 221        return -1;
 222    if (xenstore_read_int(con->console, "limit", &limit) == 0)
 223        con->buffer.max_capacity = limit;
 224
 225    if (!xendev->dev) {
 226        xen_pfn_t mfn = con->ring_ref;
 227        con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
 228                                          PROT_READ|PROT_WRITE,
 229                                          1, &mfn, NULL);
 230    } else {
 231        con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
 232                                             con->ring_ref,
 233                                             PROT_READ|PROT_WRITE);
 234    }
 235    if (!con->sring)
 236        return -1;
 237
 238    xen_be_bind_evtchn(&con->xendev);
 239    if (con->chr) {
 240        if (qemu_chr_fe_claim(con->chr) == 0) {
 241            qemu_chr_add_handlers(con->chr, xencons_can_receive,
 242                                  xencons_receive, NULL, con);
 243        } else {
 244            xen_be_printf(xendev, 0,
 245                          "xen_console_init error chardev %s already used\n",
 246                          con->chr->label);
 247            con->chr = NULL;
 248        }
 249    }
 250
 251    xen_be_printf(xendev, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
 252                  con->ring_ref,
 253                  con->xendev.remote_port,
 254                  con->xendev.local_port,
 255                  con->buffer.max_capacity);
 256    return 0;
 257}
 258
 259static void con_disconnect(struct XenDevice *xendev)
 260{
 261    struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 262
 263    if (con->chr) {
 264        qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
 265        qemu_chr_fe_release(con->chr);
 266    }
 267    xen_be_unbind_evtchn(&con->xendev);
 268
 269    if (con->sring) {
 270        if (!xendev->dev) {
 271            xenforeignmemory_unmap(xen_fmem, con->sring, 1);
 272        } else {
 273            xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
 274        }
 275        con->sring = NULL;
 276    }
 277}
 278
 279static void con_event(struct XenDevice *xendev)
 280{
 281    struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 282
 283    buffer_append(con);
 284    if (con->buffer.size - con->buffer.consumed)
 285        xencons_send(con);
 286}
 287
 288/* -------------------------------------------------------------------- */
 289
 290struct XenDevOps xen_console_ops = {
 291    .size       = sizeof(struct XenConsole),
 292    .flags      = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
 293    .init       = con_init,
 294    .initialise = con_initialise,
 295    .event      = con_event,
 296    .disconnect = con_disconnect,
 297};
 298