2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) Red Hat 2007
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>
26 #include "qapi/error.h"
27 #include "sysemu/sysemu.h"
28 #include "chardev/char-fe.h"
29 #include "hw/xen/xen-legacy-backend.h"
31 #include "hw/xen/interface/io/console.h"
42 struct XenLegacyDevice xendev
; /* must be first */
44 char console
[XEN_BUFSIZE
];
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
;
62 if ((size
== 0) || (size
> sizeof(intf
->out
)))
65 if ((buffer
->capacity
- buffer
->size
) < size
) {
66 buffer
->capacity
+= (size
+ 1024);
67 buffer
->data
= g_realloc(buffer
->data
, buffer
->capacity
);
71 buffer
->data
[buffer
->size
++] = intf
->out
[
72 MASK_XENCONS_IDX(cons
++, intf
->out
)];
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
) {
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
;
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
;
132 max
= ring_free_bytes(con
);
133 /* The can_receive() func limits this, but check again anyway */
137 prod
= intf
->in_prod
;
138 for (i
= 0; i
< len
; i
++) {
139 intf
->in
[MASK_XENCONS_IDX(prod
++, intf
->in
)] =
143 intf
->in_prod
= prod
;
144 xen_pv_send_notify(&con
->xendev
);
147 static void xencons_send(struct XenConsole
*con
)
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
,
162 xen_pv_printf(&con
->xendev
, 1,
163 "backlog piling up, nobody listening?\n");
166 buffer_advance(&con
->buffer
, len
);
167 if (con
->backlog
&& len
== size
) {
169 xen_pv_printf(&con
->xendev
, 1, "backlog is gone\n");
174 /* -------------------------------------------------------------------- */
176 static int store_con_info(struct XenConsole
*con
)
178 Chardev
*cs
= qemu_chr_fe_get_driver(&con
->chr
);
181 g_autoptr(GString
) path
= NULL
;
183 /* Only continue if we're talking to a pty. */
184 if (!CHARDEV_IS_PTY(cs
)) {
187 pts
= cs
->filename
+ 4;
189 dom_path
= qemu_xen_xs_get_domain_path(xenstore
, xen_domid
);
194 path
= g_string_new(dom_path
);
197 if (con
->xendev
.dev
) {
198 g_string_append_printf(path
, "/device/console/%d", con
->xendev
.dev
);
200 g_string_append(path
, "/console");
202 g_string_append(path
, "/tty");
204 if (xenstore_write_str(con
->console
, path
->str
, pts
)) {
205 fprintf(stderr
, "xenstore_write_str for '%s' fail", path
->str
);
211 static int con_init(struct XenLegacyDevice
*xendev
)
213 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
214 char *type
, *dom
, label
[32];
219 dom
= qemu_xen_xs_get_domain_path(xenstore
, con
->xendev
.dom
);
221 snprintf(con
->console
, sizeof(con
->console
), "%s/console", dom
);
223 snprintf(con
->console
, sizeof(con
->console
), "%s/device/console/%d", dom
, xendev
->dev
);
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
);
234 output
= xenstore_read_str(con
->console
, "output");
236 /* no Xen override, use qemu output device */
237 if (output
== NULL
) {
238 if (con
->xendev
.dev
) {
239 qemu_chr_fe_init(&con
->chr
, serial_hd(con
->xendev
.dev
),
243 snprintf(label
, sizeof(label
), "xencons%d", con
->xendev
.dev
);
244 qemu_chr_fe_init(&con
->chr
,
246 * FIXME: sure we want to support implicit
247 * muxed monitors here?
249 qemu_chr_new_mux_mon(label
, output
, NULL
),
260 static int con_initialise(struct XenLegacyDevice
*xendev
)
262 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
265 if (xenstore_read_int(con
->console
, "ring-ref", &con
->ring_ref
) == -1)
267 if (xenstore_read_int(con
->console
, "port", &con
->xendev
.remote_port
) == -1)
269 if (xenstore_read_int(con
->console
, "limit", &limit
) == 0)
270 con
->buffer
.max_capacity
= limit
;
273 xen_pfn_t mfn
= con
->ring_ref
;
274 con
->sring
= qemu_xen_foreignmem_map(con
->xendev
.dom
, NULL
,
275 PROT_READ
| PROT_WRITE
,
278 con
->sring
= xen_be_map_grant_ref(xendev
, con
->ring_ref
,
279 PROT_READ
| PROT_WRITE
);
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);
288 xen_pv_printf(xendev
, 1,
289 "ring mfn %d, remote port %d, local port %d, limit %zd\n",
291 con
->xendev
.remote_port
,
292 con
->xendev
.local_port
,
293 con
->buffer
.max_capacity
);
297 static void con_disconnect(struct XenLegacyDevice
*xendev
)
299 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
301 qemu_chr_fe_deinit(&con
->chr
, false);
302 xen_pv_unbind_evtchn(&con
->xendev
);
306 qemu_xen_foreignmem_unmap(con
->sring
, 1);
308 xen_be_unmap_grant_ref(xendev
, con
->sring
, con
->ring_ref
);
314 static void con_event(struct XenLegacyDevice
*xendev
)
316 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
319 if (con
->buffer
.size
- con
->buffer
.consumed
)
323 /* -------------------------------------------------------------------- */
325 struct XenDevOps xen_console_ops
= {
326 .size
= sizeof(struct XenConsole
),
327 .flags
= DEVOPS_FLAG_IGNORE_STATE
|DEVOPS_FLAG_NEED_GNTDEV
,
329 .initialise
= con_initialise
,
331 .disconnect
= con_disconnect
,