2009-04-22 23:19:19 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) International Business Machines Corp., 2005
|
|
|
|
* Author(s): Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) Red Hat 2007
|
|
|
|
*
|
|
|
|
* Xen Console
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; under version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2009-07-17 04:47:01 +08:00
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2009-04-22 23:19:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/hw.h"
|
2013-04-08 22:55:25 +08:00
|
|
|
#include "sysemu/char.h"
|
2013-02-06 00:06:20 +08:00
|
|
|
#include "hw/xen/xen_backend.h"
|
2009-04-22 23:19:19 +08:00
|
|
|
|
2012-06-21 19:43:59 +08:00
|
|
|
#include <xen/io/console.h>
|
|
|
|
|
2009-04-22 23:19:19 +08:00
|
|
|
struct buffer {
|
|
|
|
uint8_t *data;
|
|
|
|
size_t consumed;
|
|
|
|
size_t size;
|
|
|
|
size_t capacity;
|
|
|
|
size_t max_capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct XenConsole {
|
|
|
|
struct XenDevice xendev; /* must be first */
|
|
|
|
struct buffer buffer;
|
|
|
|
char console[XEN_BUFSIZE];
|
|
|
|
int ring_ref;
|
|
|
|
void *sring;
|
|
|
|
CharDriverState *chr;
|
|
|
|
int backlog;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void buffer_append(struct XenConsole *con)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = &con->buffer;
|
|
|
|
XENCONS_RING_IDX cons, prod, size;
|
|
|
|
struct xencons_interface *intf = con->sring;
|
|
|
|
|
|
|
|
cons = intf->out_cons;
|
|
|
|
prod = intf->out_prod;
|
|
|
|
xen_mb();
|
|
|
|
|
|
|
|
size = prod - cons;
|
|
|
|
if ((size == 0) || (size > sizeof(intf->out)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((buffer->capacity - buffer->size) < size) {
|
|
|
|
buffer->capacity += (size + 1024);
|
2011-08-21 11:09:37 +08:00
|
|
|
buffer->data = g_realloc(buffer->data, buffer->capacity);
|
2009-04-22 23:19:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (cons != prod)
|
|
|
|
buffer->data[buffer->size++] = intf->out[
|
|
|
|
MASK_XENCONS_IDX(cons++, intf->out)];
|
|
|
|
|
|
|
|
xen_mb();
|
|
|
|
intf->out_cons = cons;
|
|
|
|
xen_be_send_notify(&con->xendev);
|
|
|
|
|
|
|
|
if (buffer->max_capacity &&
|
|
|
|
buffer->size > buffer->max_capacity) {
|
|
|
|
/* Discard the middle of the data. */
|
|
|
|
|
|
|
|
size_t over = buffer->size - buffer->max_capacity;
|
|
|
|
uint8_t *maxpos = buffer->data + buffer->max_capacity;
|
|
|
|
|
|
|
|
memmove(maxpos - over, maxpos, over);
|
2011-08-21 11:09:37 +08:00
|
|
|
buffer->data = g_realloc(buffer->data, buffer->max_capacity);
|
2009-04-22 23:19:19 +08:00
|
|
|
buffer->size = buffer->capacity = buffer->max_capacity;
|
|
|
|
|
|
|
|
if (buffer->consumed > buffer->max_capacity - over)
|
|
|
|
buffer->consumed = buffer->max_capacity - over;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_advance(struct buffer *buffer, size_t len)
|
|
|
|
{
|
|
|
|
buffer->consumed += len;
|
|
|
|
if (buffer->consumed == buffer->size) {
|
|
|
|
buffer->consumed = 0;
|
|
|
|
buffer->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ring_free_bytes(struct XenConsole *con)
|
|
|
|
{
|
|
|
|
struct xencons_interface *intf = con->sring;
|
|
|
|
XENCONS_RING_IDX cons, prod, space;
|
|
|
|
|
|
|
|
cons = intf->in_cons;
|
|
|
|
prod = intf->in_prod;
|
|
|
|
xen_mb();
|
|
|
|
|
|
|
|
space = prod - cons;
|
|
|
|
if (space > sizeof(intf->in))
|
|
|
|
return 0; /* ring is screwed: ignore it */
|
|
|
|
|
|
|
|
return (sizeof(intf->in) - space);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xencons_can_receive(void *opaque)
|
|
|
|
{
|
|
|
|
struct XenConsole *con = opaque;
|
|
|
|
return ring_free_bytes(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xencons_receive(void *opaque, const uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
struct XenConsole *con = opaque;
|
|
|
|
struct xencons_interface *intf = con->sring;
|
|
|
|
XENCONS_RING_IDX prod;
|
|
|
|
int i, max;
|
|
|
|
|
|
|
|
max = ring_free_bytes(con);
|
|
|
|
/* The can_receive() func limits this, but check again anyway */
|
|
|
|
if (max < len)
|
|
|
|
len = max;
|
|
|
|
|
|
|
|
prod = intf->in_prod;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
|
|
|
|
buf[i];
|
|
|
|
}
|
|
|
|
xen_wmb();
|
|
|
|
intf->in_prod = prod;
|
|
|
|
xen_be_send_notify(&con->xendev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xencons_send(struct XenConsole *con)
|
|
|
|
{
|
|
|
|
ssize_t len, size;
|
|
|
|
|
|
|
|
size = con->buffer.size - con->buffer.consumed;
|
|
|
|
if (con->chr)
|
2011-08-16 00:17:28 +08:00
|
|
|
len = qemu_chr_fe_write(con->chr, con->buffer.data + con->buffer.consumed,
|
2009-04-22 23:19:19 +08:00
|
|
|
size);
|
|
|
|
else
|
|
|
|
len = size;
|
|
|
|
if (len < 1) {
|
|
|
|
if (!con->backlog) {
|
|
|
|
con->backlog = 1;
|
|
|
|
xen_be_printf(&con->xendev, 1, "backlog piling up, nobody listening?\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer_advance(&con->buffer, len);
|
|
|
|
if (con->backlog && len == size) {
|
|
|
|
con->backlog = 0;
|
|
|
|
xen_be_printf(&con->xendev, 1, "backlog is gone\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int con_init(struct XenDevice *xendev)
|
|
|
|
{
|
|
|
|
struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
|
2011-07-01 01:26:29 +08:00
|
|
|
char *type, *dom, label[32];
|
2011-06-24 23:59:46 +08:00
|
|
|
int ret = 0;
|
2011-07-01 01:26:29 +08:00
|
|
|
const char *output;
|
2009-04-22 23:19:19 +08:00
|
|
|
|
|
|
|
/* setup */
|
|
|
|
dom = xs_get_domain_path(xenstore, con->xendev.dom);
|
2012-12-17 19:36:09 +08:00
|
|
|
if (!xendev->dev) {
|
|
|
|
snprintf(con->console, sizeof(con->console), "%s/console", dom);
|
|
|
|
} else {
|
|
|
|
snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
|
|
|
|
}
|
2009-04-22 23:19:19 +08:00
|
|
|
free(dom);
|
|
|
|
|
|
|
|
type = xenstore_read_str(con->console, "type");
|
2009-04-24 02:42:30 +08:00
|
|
|
if (!type || strcmp(type, "ioemu") != 0) {
|
2009-04-22 23:19:19 +08:00
|
|
|
xen_be_printf(xendev, 1, "not for me (type=%s)\n", type);
|
2011-06-24 23:59:46 +08:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2009-04-22 23:19:19 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 01:26:29 +08:00
|
|
|
output = xenstore_read_str(con->console, "output");
|
2011-07-03 15:44:48 +08:00
|
|
|
|
|
|
|
/* no Xen override, use qemu output device */
|
2011-07-01 01:26:29 +08:00
|
|
|
if (output == NULL) {
|
2011-07-03 15:44:48 +08:00
|
|
|
con->chr = serial_hds[con->xendev.dev];
|
|
|
|
} else {
|
|
|
|
snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
|
2011-08-16 00:17:36 +08:00
|
|
|
con->chr = qemu_chr_new(label, output, NULL);
|
2011-07-01 01:26:29 +08:00
|
|
|
}
|
2011-07-03 15:44:48 +08:00
|
|
|
|
2011-07-01 01:26:29 +08:00
|
|
|
xenstore_store_pv_console_info(con->xendev.dev, con->chr);
|
2009-04-22 23:19:19 +08:00
|
|
|
|
2011-06-24 23:59:46 +08:00
|
|
|
out:
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free(type);
|
2011-06-24 23:59:46 +08:00
|
|
|
return ret;
|
2009-04-22 23:19:19 +08:00
|
|
|
}
|
|
|
|
|
2011-06-17 20:15:35 +08:00
|
|
|
static int con_initialise(struct XenDevice *xendev)
|
2009-04-22 23:19:19 +08:00
|
|
|
{
|
|
|
|
struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
|
|
|
|
int limit;
|
|
|
|
|
|
|
|
if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
|
|
|
|
return -1;
|
|
|
|
if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
|
|
|
|
return -1;
|
|
|
|
if (xenstore_read_int(con->console, "limit", &limit) == 0)
|
|
|
|
con->buffer.max_capacity = limit;
|
|
|
|
|
2012-12-17 19:36:09 +08:00
|
|
|
if (!xendev->dev) {
|
|
|
|
con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom,
|
|
|
|
XC_PAGE_SIZE,
|
|
|
|
PROT_READ|PROT_WRITE,
|
|
|
|
con->ring_ref);
|
|
|
|
} else {
|
|
|
|
con->sring = xc_gnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
|
|
|
|
con->ring_ref,
|
|
|
|
PROT_READ|PROT_WRITE);
|
|
|
|
}
|
2009-04-22 23:19:19 +08:00
|
|
|
if (!con->sring)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xen_be_bind_evtchn(&con->xendev);
|
2013-03-28 03:29:40 +08:00
|
|
|
if (con->chr) {
|
|
|
|
if (qemu_chr_fe_claim(con->chr) == 0) {
|
|
|
|
qemu_chr_add_handlers(con->chr, xencons_can_receive,
|
|
|
|
xencons_receive, NULL, con);
|
|
|
|
} else {
|
|
|
|
xen_be_printf(xendev, 0,
|
|
|
|
"xen_console_init error chardev %s already used\n",
|
|
|
|
con->chr->label);
|
|
|
|
con->chr = NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:19:19 +08:00
|
|
|
|
|
|
|
xen_be_printf(xendev, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
|
|
|
|
con->ring_ref,
|
|
|
|
con->xendev.remote_port,
|
|
|
|
con->xendev.local_port,
|
|
|
|
con->buffer.max_capacity);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void con_disconnect(struct XenDevice *xendev)
|
|
|
|
{
|
|
|
|
struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
|
|
|
|
|
2012-03-28 00:02:21 +08:00
|
|
|
if (!xendev->dev) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 03:29:40 +08:00
|
|
|
if (con->chr) {
|
2009-04-22 23:19:19 +08:00
|
|
|
qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
|
2013-03-28 03:29:40 +08:00
|
|
|
qemu_chr_fe_release(con->chr);
|
|
|
|
}
|
2009-04-22 23:19:19 +08:00
|
|
|
xen_be_unbind_evtchn(&con->xendev);
|
|
|
|
|
|
|
|
if (con->sring) {
|
2012-12-17 19:36:09 +08:00
|
|
|
if (!xendev->gnttabdev) {
|
|
|
|
munmap(con->sring, XC_PAGE_SIZE);
|
|
|
|
} else {
|
|
|
|
xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
|
|
|
|
}
|
2009-04-22 23:19:19 +08:00
|
|
|
con->sring = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void con_event(struct XenDevice *xendev)
|
|
|
|
{
|
|
|
|
struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
|
|
|
|
|
|
|
|
buffer_append(con);
|
|
|
|
if (con->buffer.size - con->buffer.consumed)
|
|
|
|
xencons_send(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct XenDevOps xen_console_ops = {
|
|
|
|
.size = sizeof(struct XenConsole),
|
2012-12-17 19:36:09 +08:00
|
|
|
.flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
|
2009-04-22 23:19:19 +08:00
|
|
|
.init = con_init,
|
2011-06-17 20:15:35 +08:00
|
|
|
.initialise = con_initialise,
|
2009-04-22 23:19:19 +08:00
|
|
|
.event = con_event,
|
|
|
|
.disconnect = con_disconnect,
|
|
|
|
};
|