1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "qemu/osdep.h"
23#include <sys/select.h>
24#include <termios.h>
25
26#include "qapi/error.h"
27#include "sysemu/sysemu.h"
28#include "chardev/char-fe.h"
29#include "hw/xen/xen-legacy-backend.h"
30
31#include "hw/xen/interface/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 XenLegacyDevice xendev;
43 struct buffer buffer;
44 char console[XEN_BUFSIZE];
45 int ring_ref;
46 void *sring;
47 CharBackend 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_pv_send_notify(&con->xendev);
77
78 if (buffer->max_capacity &&
79 buffer->size > buffer->max_capacity) {
80
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;
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
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_pv_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 (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;
158 }
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");
164 }
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");
170 }
171 }
172}
173
174
175
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 g_autoptr(GString) path = NULL;
182
183
184 if (!CHARDEV_IS_PTY(cs)) {
185 return 0;
186 }
187 pts = cs->filename + 4;
188
189 dom_path = qemu_xen_xs_get_domain_path(xenstore, xen_domid);
190 if (!dom_path) {
191 return 0;
192 }
193
194 path = g_string_new(dom_path);
195 free(dom_path);
196
197 if (con->xendev.dev) {
198 g_string_append_printf(path, "/device/console/%d", con->xendev.dev);
199 } else {
200 g_string_append(path, "/console");
201 }
202 g_string_append(path, "/tty");
203
204 if (xenstore_write_str(con->console, path->str, pts)) {
205 fprintf(stderr, "xenstore_write_str for '%s' fail", path->str);
206 return -1;
207 }
208 return 0;
209}
210
211static int con_init(struct XenLegacyDevice *xendev)
212{
213 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
214 char *type, *dom, label[32];
215 int ret = 0;
216 const char *output;
217
218
219 dom = qemu_xen_xs_get_domain_path(xenstore, con->xendev.dom);
220 if (!xendev->dev) {
221 snprintf(con->console, sizeof(con->console), "%s/console", dom);
222 } else {
223 snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
224 }
225 free(dom);
226
227 type = xenstore_read_str(con->console, "type");
228 if (!type || strcmp(type, "ioemu") != 0) {
229 xen_pv_printf(xendev, 1, "not for me (type=%s)\n", type);
230 ret = -1;
231 goto out;
232 }
233
234 output = xenstore_read_str(con->console, "output");
235
236
237 if (output == NULL) {
238 if (con->xendev.dev) {
239 qemu_chr_fe_init(&con->chr, serial_hd(con->xendev.dev),
240 &error_abort);
241 }
242 } else {
243 snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
244 qemu_chr_fe_init(&con->chr,
245
246
247
248
249 qemu_chr_new_mux_mon(label, output, NULL),
250 &error_abort);
251 }
252
253 store_con_info(con);
254
255out:
256 g_free(type);
257 return ret;
258}
259
260static int con_initialise(struct XenLegacyDevice *xendev)
261{
262 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
263 int limit;
264
265 if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
266 return -1;
267 if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
268 return -1;
269 if (xenstore_read_int(con->console, "limit", &limit) == 0)
270 con->buffer.max_capacity = limit;
271
272 if (!xendev->dev) {
273 xen_pfn_t mfn = con->ring_ref;
274 con->sring = qemu_xen_foreignmem_map(con->xendev.dom, NULL,
275 PROT_READ | PROT_WRITE,
276 1, &mfn, NULL);
277 } else {
278 con->sring = xen_be_map_grant_ref(xendev, con->ring_ref,
279 PROT_READ | PROT_WRITE);
280 }
281 if (!con->sring)
282 return -1;
283
284 xen_be_bind_evtchn(&con->xendev);
285 qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
286 xencons_receive, NULL, NULL, con, NULL, true);
287
288 xen_pv_printf(xendev, 1,
289 "ring mfn %d, remote port %d, local port %d, limit %zd\n",
290 con->ring_ref,
291 con->xendev.remote_port,
292 con->xendev.local_port,
293 con->buffer.max_capacity);
294 return 0;
295}
296
297static void con_disconnect(struct XenLegacyDevice *xendev)
298{
299 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
300
301 qemu_chr_fe_deinit(&con->chr, false);
302 xen_pv_unbind_evtchn(&con->xendev);
303
304 if (con->sring) {
305 if (!xendev->dev) {
306 qemu_xen_foreignmem_unmap(con->sring, 1);
307 } else {
308 xen_be_unmap_grant_ref(xendev, con->sring, con->ring_ref);
309 }
310 con->sring = NULL;
311 }
312}
313
314static void con_event(struct XenLegacyDevice *xendev)
315{
316 struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
317
318 buffer_append(con);
319 if (con->buffer.size - con->buffer.consumed)
320 xencons_send(con);
321}
322
323
324
325struct XenDevOps xen_console_ops = {
326 .size = sizeof(struct XenConsole),
327 .flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
328 .init = con_init,
329 .initialise = con_initialise,
330 .event = con_event,
331 .disconnect = con_disconnect,
332};
333