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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/select.h>
33 #include <xen/io/console.h>
38 #include "qemu-char.h"
39 #include "xen_backend.h"
50 struct XenDevice xendev
; /* must be first */
52 char console
[XEN_BUFSIZE
];
59 static void buffer_append(struct XenConsole
*con
)
61 struct buffer
*buffer
= &con
->buffer
;
62 XENCONS_RING_IDX cons
, prod
, size
;
63 struct xencons_interface
*intf
= con
->sring
;
65 cons
= intf
->out_cons
;
66 prod
= intf
->out_prod
;
70 if ((size
== 0) || (size
> sizeof(intf
->out
)))
73 if ((buffer
->capacity
- buffer
->size
) < size
) {
74 buffer
->capacity
+= (size
+ 1024);
75 buffer
->data
= qemu_realloc(buffer
->data
, buffer
->capacity
);
79 buffer
->data
[buffer
->size
++] = intf
->out
[
80 MASK_XENCONS_IDX(cons
++, intf
->out
)];
83 intf
->out_cons
= cons
;
84 xen_be_send_notify(&con
->xendev
);
86 if (buffer
->max_capacity
&&
87 buffer
->size
> buffer
->max_capacity
) {
88 /* Discard the middle of the data. */
90 size_t over
= buffer
->size
- buffer
->max_capacity
;
91 uint8_t *maxpos
= buffer
->data
+ buffer
->max_capacity
;
93 memmove(maxpos
- over
, maxpos
, over
);
94 buffer
->data
= qemu_realloc(buffer
->data
, buffer
->max_capacity
);
95 buffer
->size
= buffer
->capacity
= buffer
->max_capacity
;
97 if (buffer
->consumed
> buffer
->max_capacity
- over
)
98 buffer
->consumed
= buffer
->max_capacity
- over
;
102 static void buffer_advance(struct buffer
*buffer
, size_t len
)
104 buffer
->consumed
+= len
;
105 if (buffer
->consumed
== buffer
->size
) {
106 buffer
->consumed
= 0;
111 static int ring_free_bytes(struct XenConsole
*con
)
113 struct xencons_interface
*intf
= con
->sring
;
114 XENCONS_RING_IDX cons
, prod
, space
;
116 cons
= intf
->in_cons
;
117 prod
= intf
->in_prod
;
121 if (space
> sizeof(intf
->in
))
122 return 0; /* ring is screwed: ignore it */
124 return (sizeof(intf
->in
) - space
);
127 static int xencons_can_receive(void *opaque
)
129 struct XenConsole
*con
= opaque
;
130 return ring_free_bytes(con
);
133 static void xencons_receive(void *opaque
, const uint8_t *buf
, int len
)
135 struct XenConsole
*con
= opaque
;
136 struct xencons_interface
*intf
= con
->sring
;
137 XENCONS_RING_IDX prod
;
140 max
= ring_free_bytes(con
);
141 /* The can_receive() func limits this, but check again anyway */
145 prod
= intf
->in_prod
;
146 for (i
= 0; i
< len
; i
++) {
147 intf
->in
[MASK_XENCONS_IDX(prod
++, intf
->in
)] =
151 intf
->in_prod
= prod
;
152 xen_be_send_notify(&con
->xendev
);
155 static void xencons_send(struct XenConsole
*con
)
159 size
= con
->buffer
.size
- con
->buffer
.consumed
;
161 len
= qemu_chr_write(con
->chr
, con
->buffer
.data
+ con
->buffer
.consumed
,
168 xen_be_printf(&con
->xendev
, 1, "backlog piling up, nobody listening?\n");
171 buffer_advance(&con
->buffer
, len
);
172 if (con
->backlog
&& len
== size
) {
174 xen_be_printf(&con
->xendev
, 1, "backlog is gone\n");
179 /* -------------------------------------------------------------------- */
181 static int con_init(struct XenDevice
*xendev
)
183 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
187 dom
= xs_get_domain_path(xenstore
, con
->xendev
.dom
);
188 snprintf(con
->console
, sizeof(con
->console
), "%s/console", dom
);
191 type
= xenstore_read_str(con
->console
, "type");
192 if (!type
|| strcmp(type
, "ioemu") != 0) {
193 xen_be_printf(xendev
, 1, "not for me (type=%s)\n", type
);
197 if (!serial_hds
[con
->xendev
.dev
])
198 xen_be_printf(xendev
, 1, "WARNING: serial line %d not configured\n",
201 con
->chr
= serial_hds
[con
->xendev
.dev
];
206 static int con_connect(struct XenDevice
*xendev
)
208 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
211 if (xenstore_read_int(con
->console
, "ring-ref", &con
->ring_ref
) == -1)
213 if (xenstore_read_int(con
->console
, "port", &con
->xendev
.remote_port
) == -1)
215 if (xenstore_read_int(con
->console
, "limit", &limit
) == 0)
216 con
->buffer
.max_capacity
= limit
;
218 con
->sring
= xc_map_foreign_range(xen_xc
, con
->xendev
.dom
,
220 PROT_READ
|PROT_WRITE
,
225 xen_be_bind_evtchn(&con
->xendev
);
227 qemu_chr_add_handlers(con
->chr
, xencons_can_receive
, xencons_receive
,
230 xen_be_printf(xendev
, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
232 con
->xendev
.remote_port
,
233 con
->xendev
.local_port
,
234 con
->buffer
.max_capacity
);
238 static void con_disconnect(struct XenDevice
*xendev
)
240 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
243 qemu_chr_add_handlers(con
->chr
, NULL
, NULL
, NULL
, NULL
);
244 xen_be_unbind_evtchn(&con
->xendev
);
247 munmap(con
->sring
, XC_PAGE_SIZE
);
252 static void con_event(struct XenDevice
*xendev
)
254 struct XenConsole
*con
= container_of(xendev
, struct XenConsole
, xendev
);
257 if (con
->buffer
.size
- con
->buffer
.consumed
)
261 /* -------------------------------------------------------------------- */
263 struct XenDevOps xen_console_ops
= {
264 .size
= sizeof(struct XenConsole
),
265 .flags
= DEVOPS_FLAG_IGNORE_STATE
,
267 .connect
= con_connect
,
269 .disconnect
= con_disconnect
,