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>
33 #include "char/char.h"
34 #include "xen_backend.h"
36 #include <xen/io/console.h>
47 struct XenDevice xendev
; /* must be first */
49 char console
[XEN_BUFSIZE
];
56 static void buffer_append(struct XenConsole
*con
)
58 struct buffer
*buffer
= &con
->buffer
;
59 XENCONS_RING_IDX cons
, prod
, size
;
60 struct xencons_interface
*intf
= con
->sring
;
62 cons
= intf
->out_cons
;
63 prod
= intf
->out_prod
;
67 if ((size
== 0) || (size
> sizeof(intf
->out
)))
70 if ((buffer
->capacity
- buffer
->size
) < size
) {
71 buffer
->capacity
+= (size
+ 1024);
72 buffer
->data
= g_realloc(buffer
->data
, buffer
->capacity
);
76 buffer
->data
[buffer
->size
++] = intf
->out
[
77 MASK_XENCONS_IDX(cons
++, intf
->out
)];
80 intf
->out_cons
= cons
;
81 xen_be_send_notify(&con
->xendev
);
83 if (buffer
->max_capacity
&&
84 buffer
->size
> buffer
->max_capacity
) {
85 /* Discard the middle of the data. */
87 size_t over
= buffer
->size
- buffer
->max_capacity
;
88 uint8_t *maxpos
= buffer
->data
+ buffer
->max_capacity
;
90 memmove(maxpos
- over
, maxpos
, over
);
91 buffer
->data
= g_realloc(buffer
->data
, buffer
->max_capacity
);
92 buffer
->size
= buffer
->capacity
= buffer
->max_capacity
;
94 if (buffer
->consumed
> buffer
->max_capacity
- over
)
95 buffer
->consumed
= buffer
->max_capacity
- over
;
99 static void buffer_advance(struct buffer
*buffer
, size_t len
)
101 buffer
->consumed
+= len
;
102 if (buffer
->consumed
== buffer
->size
) {
103 buffer
->consumed
= 0;
108 static int ring_free_bytes(struct XenConsole
*con
)
110 struct xencons_interface
*intf
= con
->sring
;
111 XENCONS_RING_IDX cons
, prod
, space
;
113 cons
= intf
->in_cons
;
114 prod
= intf
->in_prod
;
118 if (space
> sizeof(intf
->in
))
119 return 0; /* ring is screwed: ignore it */
121 return (sizeof(intf
->in
) - space
);
124 static int xencons_can_receive(void *opaque
)
126 struct XenConsole
*con
= opaque
;
127 return ring_free_bytes(con
);
130 static void xencons_receive(void *opaque
, const uint8_t *buf
, int len
)
132 struct XenConsole
*con
= opaque
;
133 struct xencons_interface
*intf
= con
->sring
;
134 XENCONS_RING_IDX prod
;
137 max
= ring_free_bytes(con
);
138 /* The can_receive() func limits this, but check again anyway */
142 prod
= intf
->in_prod
;
143 for (i
= 0; i
< len
; i
++) {
144 intf
->in
[MASK_XENCONS_IDX(prod
++, intf
->in
)] =
148 intf
->in_prod
= prod
;
149 xen_be_send_notify(&con
->xendev
);
152 static void xencons_send(struct XenConsole
*con
)
156 size
= con
->buffer
.size
- con
->buffer
.consumed
;
158 len
= qemu_chr_fe_write(con
->chr
, con
->buffer
.data
+ con
->buffer
.consumed
,
165 xen_be_printf(&con
->xendev
, 1, "backlog piling up, nobody listening?\n");
168 buffer_advance(&con
->buffer
, len
);
169 if (con
->backlog
&& len
== size
) {
171 xen_be_printf(&con
->xendev
, 1, "backlog is gone\n");
176 /* -------------------------------------------------------------------- */
178 static int con_init(struct XenDevice
*xendev
)
180 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
181 char *type
, *dom
, label
[32];
186 dom
= xs_get_domain_path(xenstore
, con
->xendev
.dom
);
188 snprintf(con
->console
, sizeof(con
->console
), "%s/console", dom
);
190 snprintf(con
->console
, sizeof(con
->console
), "%s/device/console/%d", dom
, xendev
->dev
);
194 type
= xenstore_read_str(con
->console
, "type");
195 if (!type
|| strcmp(type
, "ioemu") != 0) {
196 xen_be_printf(xendev
, 1, "not for me (type=%s)\n", type
);
201 output
= xenstore_read_str(con
->console
, "output");
203 /* no Xen override, use qemu output device */
204 if (output
== NULL
) {
205 con
->chr
= serial_hds
[con
->xendev
.dev
];
207 snprintf(label
, sizeof(label
), "xencons%d", con
->xendev
.dev
);
208 con
->chr
= qemu_chr_new(label
, output
, NULL
);
211 xenstore_store_pv_console_info(con
->xendev
.dev
, con
->chr
);
218 static int con_initialise(struct XenDevice
*xendev
)
220 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
223 if (xenstore_read_int(con
->console
, "ring-ref", &con
->ring_ref
) == -1)
225 if (xenstore_read_int(con
->console
, "port", &con
->xendev
.remote_port
) == -1)
227 if (xenstore_read_int(con
->console
, "limit", &limit
) == 0)
228 con
->buffer
.max_capacity
= limit
;
231 con
->sring
= xc_map_foreign_range(xen_xc
, con
->xendev
.dom
,
233 PROT_READ
|PROT_WRITE
,
236 con
->sring
= xc_gnttab_map_grant_ref(xendev
->gnttabdev
, con
->xendev
.dom
,
238 PROT_READ
|PROT_WRITE
);
243 xen_be_bind_evtchn(&con
->xendev
);
245 qemu_chr_add_handlers(con
->chr
, xencons_can_receive
, xencons_receive
,
248 xen_be_printf(xendev
, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
250 con
->xendev
.remote_port
,
251 con
->xendev
.local_port
,
252 con
->buffer
.max_capacity
);
256 static void con_disconnect(struct XenDevice
*xendev
)
258 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
264 qemu_chr_add_handlers(con
->chr
, NULL
, NULL
, NULL
, NULL
);
265 xen_be_unbind_evtchn(&con
->xendev
);
268 if (!xendev
->gnttabdev
) {
269 munmap(con
->sring
, XC_PAGE_SIZE
);
271 xc_gnttab_munmap(xendev
->gnttabdev
, con
->sring
, 1);
277 static void con_event(struct XenDevice
*xendev
)
279 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
282 if (con
->buffer
.size
- con
->buffer
.consumed
)
286 /* -------------------------------------------------------------------- */
288 struct XenDevOps xen_console_ops
= {
289 .size
= sizeof(struct XenConsole
),
290 .flags
= DEVOPS_FLAG_IGNORE_STATE
|DEVOPS_FLAG_NEED_GNTDEV
,
292 .initialise
= con_initialise
,
294 .disconnect
= con_disconnect
,