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/>.
25 #include <sys/select.h>
32 #include <xen/io/console.h>
36 #include "qemu-char.h"
37 #include "xen_backend.h"
48 struct XenDevice xendev
; /* must be first */
50 char console
[XEN_BUFSIZE
];
57 static void buffer_append(struct XenConsole
*con
)
59 struct buffer
*buffer
= &con
->buffer
;
60 XENCONS_RING_IDX cons
, prod
, size
;
61 struct xencons_interface
*intf
= con
->sring
;
63 cons
= intf
->out_cons
;
64 prod
= intf
->out_prod
;
68 if ((size
== 0) || (size
> sizeof(intf
->out
)))
71 if ((buffer
->capacity
- buffer
->size
) < size
) {
72 buffer
->capacity
+= (size
+ 1024);
73 buffer
->data
= qemu_realloc(buffer
->data
, buffer
->capacity
);
77 buffer
->data
[buffer
->size
++] = intf
->out
[
78 MASK_XENCONS_IDX(cons
++, intf
->out
)];
81 intf
->out_cons
= cons
;
82 xen_be_send_notify(&con
->xendev
);
84 if (buffer
->max_capacity
&&
85 buffer
->size
> buffer
->max_capacity
) {
86 /* Discard the middle of the data. */
88 size_t over
= buffer
->size
- buffer
->max_capacity
;
89 uint8_t *maxpos
= buffer
->data
+ buffer
->max_capacity
;
91 memmove(maxpos
- over
, maxpos
, over
);
92 buffer
->data
= qemu_realloc(buffer
->data
, buffer
->max_capacity
);
93 buffer
->size
= buffer
->capacity
= buffer
->max_capacity
;
95 if (buffer
->consumed
> buffer
->max_capacity
- over
)
96 buffer
->consumed
= buffer
->max_capacity
- over
;
100 static void buffer_advance(struct buffer
*buffer
, size_t len
)
102 buffer
->consumed
+= len
;
103 if (buffer
->consumed
== buffer
->size
) {
104 buffer
->consumed
= 0;
109 static int ring_free_bytes(struct XenConsole
*con
)
111 struct xencons_interface
*intf
= con
->sring
;
112 XENCONS_RING_IDX cons
, prod
, space
;
114 cons
= intf
->in_cons
;
115 prod
= intf
->in_prod
;
119 if (space
> sizeof(intf
->in
))
120 return 0; /* ring is screwed: ignore it */
122 return (sizeof(intf
->in
) - space
);
125 static int xencons_can_receive(void *opaque
)
127 struct XenConsole
*con
= opaque
;
128 return ring_free_bytes(con
);
131 static void xencons_receive(void *opaque
)
133 struct XenConsole
*con
= opaque
;
134 struct xencons_interface
*intf
= con
->sring
;
135 XENCONS_RING_IDX prod
;
139 max
= ring_free_bytes(con
);
140 max
= MIN(max
, sizeof(buf
));
141 len
= qemu_chr_fe_read(con
->chr
, buf
, max
);
143 prod
= intf
->in_prod
;
144 for (i
= 0; i
< len
; i
++) {
145 intf
->in
[MASK_XENCONS_IDX(prod
++, intf
->in
)] =
149 intf
->in_prod
= prod
;
150 xen_be_send_notify(&con
->xendev
);
153 static void xencons_update_handlers(struct XenConsole
*con
)
155 if (xencons_can_receive(con
) > 0) {
156 qemu_chr_fe_set_handlers(con
->chr
, xencons_receive
,
159 qemu_chr_fe_set_handlers(con
->chr
, NULL
, NULL
, NULL
, con
);
163 static void xencons_send(struct XenConsole
*con
)
167 size
= con
->buffer
.size
- con
->buffer
.consumed
;
169 len
= qemu_chr_fe_write(con
->chr
, con
->buffer
.data
+ con
->buffer
.consumed
,
176 xen_be_printf(&con
->xendev
, 1, "backlog piling up, nobody listening?\n");
179 buffer_advance(&con
->buffer
, len
);
180 if (con
->backlog
&& len
== size
) {
182 xen_be_printf(&con
->xendev
, 1, "backlog is gone\n");
187 /* -------------------------------------------------------------------- */
189 static int con_init(struct XenDevice
*xendev
)
191 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
192 char *type
, *dom
, label
[32];
197 dom
= xs_get_domain_path(xenstore
, con
->xendev
.dom
);
198 snprintf(con
->console
, sizeof(con
->console
), "%s/console", dom
);
201 type
= xenstore_read_str(con
->console
, "type");
202 if (!type
|| strcmp(type
, "ioemu") != 0) {
203 xen_be_printf(xendev
, 1, "not for me (type=%s)\n", type
);
208 output
= xenstore_read_str(con
->console
, "output");
210 /* no Xen override, use qemu output device */
211 if (output
== NULL
) {
212 con
->chr
= serial_hds
[con
->xendev
.dev
];
214 snprintf(label
, sizeof(label
), "xencons%d", con
->xendev
.dev
);
215 con
->chr
= qemu_chr_new(label
, output
, NULL
);
218 xenstore_store_pv_console_info(con
->xendev
.dev
, con
->chr
);
225 static int con_connect(struct XenDevice
*xendev
)
227 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
230 if (xenstore_read_int(con
->console
, "ring-ref", &con
->ring_ref
) == -1)
232 if (xenstore_read_int(con
->console
, "port", &con
->xendev
.remote_port
) == -1)
234 if (xenstore_read_int(con
->console
, "limit", &limit
) == 0)
235 con
->buffer
.max_capacity
= limit
;
237 con
->sring
= xc_map_foreign_range(xen_xc
, con
->xendev
.dom
,
239 PROT_READ
|PROT_WRITE
,
244 xen_be_bind_evtchn(&con
->xendev
);
246 qemu_chr_fe_open(con
->chr
);
247 xencons_update_handlers(con
);
250 xen_be_printf(xendev
, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
252 con
->xendev
.remote_port
,
253 con
->xendev
.local_port
,
254 con
->buffer
.max_capacity
);
258 static void con_disconnect(struct XenDevice
*xendev
)
260 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
263 qemu_chr_fe_set_handlers(con
->chr
, NULL
, NULL
, NULL
, NULL
);
264 qemu_chr_fe_close(con
->chr
);
266 xen_be_unbind_evtchn(&con
->xendev
);
269 munmap(con
->sring
, XC_PAGE_SIZE
);
274 static void con_event(struct XenDevice
*xendev
)
276 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
279 if (con
->buffer
.size
- con
->buffer
.consumed
)
281 xencons_update_handlers(con
);
284 /* -------------------------------------------------------------------- */
286 struct XenDevOps xen_console_ops
= {
287 .size
= sizeof(struct XenConsole
),
288 .flags
= DEVOPS_FLAG_IGNORE_STATE
,
290 .connect
= con_connect
,
292 .disconnect
= con_disconnect
,