Merge remote-tracking branch 'remotes/cody/tags/block-pull-request' into staging
[qemu/ar7.git] / hw / char / xen_console.c
blob5e68326c19c7eaa6499d0f25e5139ff3ee6793f3
1 /*
2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) Red Hat 2007
7 * Xen Console
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include <sys/select.h>
24 #include <termios.h>
26 #include "qapi/error.h"
27 #include "hw/hw.h"
28 #include "chardev/char-fe.h"
29 #include "hw/xen/xen_backend.h"
31 #include <xen/io/console.h>
33 struct buffer {
34 uint8_t *data;
35 size_t consumed;
36 size_t size;
37 size_t capacity;
38 size_t max_capacity;
41 struct 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 CharBackend chr;
48 int backlog;
51 static void buffer_append(struct XenConsole *con)
53 struct buffer *buffer = &con->buffer;
54 XENCONS_RING_IDX cons, prod, size;
55 struct xencons_interface *intf = con->sring;
57 cons = intf->out_cons;
58 prod = intf->out_prod;
59 xen_mb();
61 size = prod - cons;
62 if ((size == 0) || (size > sizeof(intf->out)))
63 return;
65 if ((buffer->capacity - buffer->size) < size) {
66 buffer->capacity += (size + 1024);
67 buffer->data = g_realloc(buffer->data, buffer->capacity);
70 while (cons != prod)
71 buffer->data[buffer->size++] = intf->out[
72 MASK_XENCONS_IDX(cons++, intf->out)];
74 xen_mb();
75 intf->out_cons = cons;
76 xen_pv_send_notify(&con->xendev);
78 if (buffer->max_capacity &&
79 buffer->size > buffer->max_capacity) {
80 /* Discard the middle of the data. */
82 size_t over = buffer->size - buffer->max_capacity;
83 uint8_t *maxpos = buffer->data + buffer->max_capacity;
85 memmove(maxpos - over, maxpos, over);
86 buffer->data = g_realloc(buffer->data, buffer->max_capacity);
87 buffer->size = buffer->capacity = buffer->max_capacity;
89 if (buffer->consumed > buffer->max_capacity - over)
90 buffer->consumed = buffer->max_capacity - over;
94 static void buffer_advance(struct buffer *buffer, size_t len)
96 buffer->consumed += len;
97 if (buffer->consumed == buffer->size) {
98 buffer->consumed = 0;
99 buffer->size = 0;
103 static int ring_free_bytes(struct XenConsole *con)
105 struct xencons_interface *intf = con->sring;
106 XENCONS_RING_IDX cons, prod, space;
108 cons = intf->in_cons;
109 prod = intf->in_prod;
110 xen_mb();
112 space = prod - cons;
113 if (space > sizeof(intf->in))
114 return 0; /* ring is screwed: ignore it */
116 return (sizeof(intf->in) - space);
119 static int xencons_can_receive(void *opaque)
121 struct XenConsole *con = opaque;
122 return ring_free_bytes(con);
125 static void xencons_receive(void *opaque, const uint8_t *buf, int len)
127 struct XenConsole *con = opaque;
128 struct xencons_interface *intf = con->sring;
129 XENCONS_RING_IDX prod;
130 int i, max;
132 max = ring_free_bytes(con);
133 /* The can_receive() func limits this, but check again anyway */
134 if (max < len)
135 len = max;
137 prod = intf->in_prod;
138 for (i = 0; i < len; i++) {
139 intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
140 buf[i];
142 xen_wmb();
143 intf->in_prod = prod;
144 xen_pv_send_notify(&con->xendev);
147 static void xencons_send(struct XenConsole *con)
149 ssize_t len, size;
151 size = con->buffer.size - con->buffer.consumed;
152 if (qemu_chr_fe_backend_connected(&con->chr)) {
153 len = qemu_chr_fe_write(&con->chr,
154 con->buffer.data + con->buffer.consumed,
155 size);
156 } else {
157 len = size;
159 if (len < 1) {
160 if (!con->backlog) {
161 con->backlog = 1;
162 xen_pv_printf(&con->xendev, 1,
163 "backlog piling up, nobody listening?\n");
165 } else {
166 buffer_advance(&con->buffer, len);
167 if (con->backlog && len == size) {
168 con->backlog = 0;
169 xen_pv_printf(&con->xendev, 1, "backlog is gone\n");
174 /* -------------------------------------------------------------------- */
176 static int con_init(struct XenDevice *xendev)
178 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
179 char *type, *dom, label[32];
180 int ret = 0;
181 const char *output;
183 /* setup */
184 dom = xs_get_domain_path(xenstore, con->xendev.dom);
185 if (!xendev->dev) {
186 snprintf(con->console, sizeof(con->console), "%s/console", dom);
187 } else {
188 snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
190 free(dom);
192 type = xenstore_read_str(con->console, "type");
193 if (!type || strcmp(type, "ioemu") != 0) {
194 xen_pv_printf(xendev, 1, "not for me (type=%s)\n", type);
195 ret = -1;
196 goto out;
199 output = xenstore_read_str(con->console, "output");
201 /* no Xen override, use qemu output device */
202 if (output == NULL) {
203 if (con->xendev.dev) {
204 qemu_chr_fe_init(&con->chr, serial_hds[con->xendev.dev],
205 &error_abort);
207 } else {
208 snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
209 qemu_chr_fe_init(&con->chr,
210 qemu_chr_new(label, output), &error_abort);
213 xenstore_store_pv_console_info(con->xendev.dev,
214 qemu_chr_fe_get_driver(&con->chr));
216 out:
217 g_free(type);
218 return ret;
221 static int con_initialise(struct XenDevice *xendev)
223 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
224 int limit;
226 if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
227 return -1;
228 if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
229 return -1;
230 if (xenstore_read_int(con->console, "limit", &limit) == 0)
231 con->buffer.max_capacity = limit;
233 if (!xendev->dev) {
234 xen_pfn_t mfn = con->ring_ref;
235 con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
236 PROT_READ|PROT_WRITE,
237 1, &mfn, NULL);
238 } else {
239 con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
240 con->ring_ref,
241 PROT_READ|PROT_WRITE);
243 if (!con->sring)
244 return -1;
246 xen_be_bind_evtchn(&con->xendev);
247 qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
248 xencons_receive, NULL, NULL, con, NULL, true);
250 xen_pv_printf(xendev, 1,
251 "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;
259 static void con_disconnect(struct XenDevice *xendev)
261 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
263 qemu_chr_fe_deinit(&con->chr, false);
264 xen_pv_unbind_evtchn(&con->xendev);
266 if (con->sring) {
267 if (!xendev->dev) {
268 xenforeignmemory_unmap(xen_fmem, con->sring, 1);
269 } else {
270 xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
272 con->sring = NULL;
276 static void con_event(struct XenDevice *xendev)
278 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
280 buffer_append(con);
281 if (con->buffer.size - con->buffer.consumed)
282 xencons_send(con);
285 /* -------------------------------------------------------------------- */
287 struct XenDevOps xen_console_ops = {
288 .size = sizeof(struct XenConsole),
289 .flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
290 .init = con_init,
291 .initialise = con_initialise,
292 .event = con_event,
293 .disconnect = con_disconnect,