]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/xen_console.c
Merge tag 'for_upstream' of https://git.kernel.org/pub/scm/virt/kvm/mst/qemu into...
[mirror_qemu.git] / hw / char / xen_console.c
CommitLineData
e57dd20b
AL
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
8167ee88 19 * with this program; if not, see <http://www.gnu.org/licenses/>.
e57dd20b
AL
20 */
21
21cbfe5f 22#include "qemu/osdep.h"
e57dd20b 23#include <sys/select.h>
e57dd20b 24#include <termios.h>
e57dd20b 25
5345fdb4 26#include "qapi/error.h"
46517dd4 27#include "sysemu/sysemu.h"
4d43a603 28#include "chardev/char-fe.h"
2d0ed5e6 29#include "hw/xen/xen-legacy-backend.h"
e57dd20b 30
a3434a2d 31#include "hw/xen/interface/io/console.h"
b41f6719 32
e57dd20b
AL
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 {
2d0ed5e6 42 struct XenLegacyDevice xendev; /* must be first */
e57dd20b
AL
43 struct buffer buffer;
44 char console[XEN_BUFSIZE];
45 int ring_ref;
46 void *sring;
32a6ebec 47 CharBackend chr;
e57dd20b
AL
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)))
7d37435b 63 return;
e57dd20b
AL
64
65 if ((buffer->capacity - buffer->size) < size) {
7d37435b
PB
66 buffer->capacity += (size + 1024);
67 buffer->data = g_realloc(buffer->data, buffer->capacity);
e57dd20b
AL
68 }
69
70 while (cons != prod)
7d37435b
PB
71 buffer->data[buffer->size++] = intf->out[
72 MASK_XENCONS_IDX(cons++, intf->out)];
e57dd20b
AL
73
74 xen_mb();
75 intf->out_cons = cons;
ba18fa2a 76 xen_pv_send_notify(&con->xendev);
e57dd20b
AL
77
78 if (buffer->max_capacity &&
7d37435b
PB
79 buffer->size > buffer->max_capacity) {
80 /* Discard the middle of the data. */
e57dd20b 81
7d37435b
PB
82 size_t over = buffer->size - buffer->max_capacity;
83 uint8_t *maxpos = buffer->data + buffer->max_capacity;
e57dd20b 84
7d37435b
PB
85 memmove(maxpos - over, maxpos, over);
86 buffer->data = g_realloc(buffer->data, buffer->max_capacity);
87 buffer->size = buffer->capacity = buffer->max_capacity;
e57dd20b 88
7d37435b
PB
89 if (buffer->consumed > buffer->max_capacity - over)
90 buffer->consumed = buffer->max_capacity - over;
e57dd20b
AL
91 }
92}
93
94static void buffer_advance(struct buffer *buffer, size_t len)
95{
96 buffer->consumed += len;
97 if (buffer->consumed == buffer->size) {
7d37435b
PB
98 buffer->consumed = 0;
99 buffer->size = 0;
e57dd20b
AL
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))
7d37435b 114 return 0; /* ring is screwed: ignore it */
e57dd20b
AL
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)
7d37435b 135 len = max;
e57dd20b
AL
136
137 prod = intf->in_prod;
138 for (i = 0; i < len; i++) {
7d37435b
PB
139 intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
140 buf[i];
e57dd20b
AL
141 }
142 xen_wmb();
143 intf->in_prod = prod;
ba18fa2a 144 xen_pv_send_notify(&con->xendev);
e57dd20b
AL
145}
146
147static void xencons_send(struct XenConsole *con)
148{
149 ssize_t len, size;
150
151 size = con->buffer.size - con->buffer.consumed;
30650701 152 if (qemu_chr_fe_backend_connected(&con->chr)) {
5345fdb4 153 len = qemu_chr_fe_write(&con->chr,
32a6ebec
MAL
154 con->buffer.data + con->buffer.consumed,
155 size);
156 } else {
e57dd20b 157 len = size;
32a6ebec 158 }
e57dd20b 159 if (len < 1) {
c22e91b1
EC
160 if (!con->backlog) {
161 con->backlog = 1;
96c77dba 162 xen_pv_printf(&con->xendev, 1,
c22e91b1
EC
163 "backlog piling up, nobody listening?\n");
164 }
e57dd20b 165 } else {
c22e91b1
EC
166 buffer_advance(&con->buffer, len);
167 if (con->backlog && len == size) {
168 con->backlog = 0;
96c77dba 169 xen_pv_printf(&con->xendev, 1, "backlog is gone\n");
c22e91b1 170 }
e57dd20b
AL
171 }
172}
173
174/* -------------------------------------------------------------------- */
175
7a8a749d
DW
176static int store_con_info(struct XenConsole *con)
177{
178 Chardev *cs = qemu_chr_fe_get_driver(&con->chr);
179 char *pts = NULL;
180 char *dom_path;
181 GString *path;
182 int ret = -1;
183
184 /* Only continue if we're talking to a pty. */
185 if (!CHARDEV_IS_PTY(cs)) {
186 return 0;
187 }
188 pts = cs->filename + 4;
189
190 dom_path = qemu_xen_xs_get_domain_path(xenstore, xen_domid);
191 if (!dom_path) {
192 return 0;
193 }
194
195 path = g_string_new(dom_path);
196 free(dom_path);
197
198 if (con->xendev.dev) {
199 g_string_append_printf(path, "/device/console/%d", con->xendev.dev);
200 } else {
201 g_string_append(path, "/console");
202 }
203 g_string_append(path, "/tty");
204
205 if (xenstore_write_str(con->console, path->str, pts)) {
206 fprintf(stderr, "xenstore_write_str for '%s' fail", path->str);
207 goto out;
208 }
209 ret = 0;
210
211out:
212 g_string_free(path, true);
213 free(path);
214
215 return ret;
216}
217
2d0ed5e6 218static int con_init(struct XenLegacyDevice *xendev)
e57dd20b
AL
219{
220 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
0f51726a 221 char *type, *dom, label[32];
5e6b701a 222 int ret = 0;
0f51726a 223 const char *output;
e57dd20b
AL
224
225 /* setup */
ba2a92db 226 dom = qemu_xen_xs_get_domain_path(xenstore, con->xendev.dom);
2c1d4d15
SS
227 if (!xendev->dev) {
228 snprintf(con->console, sizeof(con->console), "%s/console", dom);
229 } else {
230 snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
231 }
e57dd20b
AL
232 free(dom);
233
234 type = xenstore_read_str(con->console, "type");
96248fd8 235 if (!type || strcmp(type, "ioemu") != 0) {
96c77dba 236 xen_pv_printf(xendev, 1, "not for me (type=%s)\n", type);
5e6b701a
SS
237 ret = -1;
238 goto out;
e57dd20b
AL
239 }
240
0f51726a 241 output = xenstore_read_str(con->console, "output");
25a11813
AG
242
243 /* no Xen override, use qemu output device */
0f51726a 244 if (output == NULL) {
32a6ebec 245 if (con->xendev.dev) {
9bca0edb 246 qemu_chr_fe_init(&con->chr, serial_hd(con->xendev.dev),
32a6ebec
MAL
247 &error_abort);
248 }
25a11813
AG
249 } else {
250 snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
32a6ebec 251 qemu_chr_fe_init(&con->chr,
95e30b2a
MAL
252 /*
253 * FIXME: sure we want to support implicit
254 * muxed monitors here?
255 */
4ad6f6cb
PB
256 qemu_chr_new_mux_mon(label, output, NULL),
257 &error_abort);
0f51726a 258 }
25a11813 259
7a8a749d 260 store_con_info(con);
e57dd20b 261
5e6b701a 262out:
7267c094 263 g_free(type);
5e6b701a 264 return ret;
e57dd20b
AL
265}
266
2d0ed5e6 267static int con_initialise(struct XenLegacyDevice *xendev)
e57dd20b
AL
268{
269 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
270 int limit;
271
272 if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
7d37435b 273 return -1;
e57dd20b 274 if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
7d37435b 275 return -1;
e57dd20b 276 if (xenstore_read_int(con->console, "limit", &limit) == 0)
7d37435b 277 con->buffer.max_capacity = limit;
e57dd20b 278
2c1d4d15 279 if (!xendev->dev) {
9ed257d1 280 xen_pfn_t mfn = con->ring_ref;
15e283c5
DW
281 con->sring = qemu_xen_foreignmem_map(con->xendev.dom, NULL,
282 PROT_READ | PROT_WRITE,
283 1, &mfn, NULL);
2c1d4d15 284 } else {
58560f2a
PD
285 con->sring = xen_be_map_grant_ref(xendev, con->ring_ref,
286 PROT_READ | PROT_WRITE);
2c1d4d15 287 }
e57dd20b 288 if (!con->sring)
7d37435b 289 return -1;
e57dd20b
AL
290
291 xen_be_bind_evtchn(&con->xendev);
fa394ed6 292 qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
81517ba3 293 xencons_receive, NULL, NULL, con, NULL, true);
e57dd20b 294
96c77dba 295 xen_pv_printf(xendev, 1,
b9730c5b 296 "ring mfn %d, remote port %d, local port %d, limit %zd\n",
7d37435b
PB
297 con->ring_ref,
298 con->xendev.remote_port,
299 con->xendev.local_port,
300 con->buffer.max_capacity);
e57dd20b
AL
301 return 0;
302}
303
2d0ed5e6 304static void con_disconnect(struct XenLegacyDevice *xendev)
e57dd20b
AL
305{
306 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
307
1ce2610c 308 qemu_chr_fe_deinit(&con->chr, false);
65807f4b 309 xen_pv_unbind_evtchn(&con->xendev);
e57dd20b
AL
310
311 if (con->sring) {
549e9bca 312 if (!xendev->dev) {
15e283c5 313 qemu_xen_foreignmem_unmap(con->sring, 1);
2c1d4d15 314 } else {
f80fad16 315 xen_be_unmap_grant_ref(xendev, con->sring, con->ring_ref);
2c1d4d15 316 }
549e9bca 317 con->sring = NULL;
e57dd20b
AL
318 }
319}
320
2d0ed5e6 321static void con_event(struct XenLegacyDevice *xendev)
e57dd20b
AL
322{
323 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
324
325 buffer_append(con);
326 if (con->buffer.size - con->buffer.consumed)
7d37435b 327 xencons_send(con);
e57dd20b
AL
328}
329
330/* -------------------------------------------------------------------- */
331
332struct XenDevOps xen_console_ops = {
333 .size = sizeof(struct XenConsole),
2c1d4d15 334 .flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
e57dd20b 335 .init = con_init,
384087b2 336 .initialise = con_initialise,
e57dd20b
AL
337 .event = con_event,
338 .disconnect = con_disconnect,
339};