root / hw / xen_console.c @ bfb811ad
History | View | Annotate | Download (7 kB)
1 | e57dd20b | aliguori | /*
|
---|---|---|---|
2 | e57dd20b | aliguori | * Copyright (C) International Business Machines Corp., 2005
|
3 | e57dd20b | aliguori | * Author(s): Anthony Liguori <aliguori@us.ibm.com>
|
4 | e57dd20b | aliguori | *
|
5 | e57dd20b | aliguori | * Copyright (C) Red Hat 2007
|
6 | e57dd20b | aliguori | *
|
7 | e57dd20b | aliguori | * Xen Console
|
8 | e57dd20b | aliguori | *
|
9 | e57dd20b | aliguori | * This program is free software; you can redistribute it and/or modify
|
10 | e57dd20b | aliguori | * it under the terms of the GNU General Public License as published by
|
11 | e57dd20b | aliguori | * the Free Software Foundation; under version 2 of the License.
|
12 | e57dd20b | aliguori | *
|
13 | e57dd20b | aliguori | * This program is distributed in the hope that it will be useful,
|
14 | e57dd20b | aliguori | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 | e57dd20b | aliguori | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 | e57dd20b | aliguori | * GNU General Public License for more details.
|
17 | e57dd20b | aliguori | *
|
18 | e57dd20b | aliguori | * You should have received a copy of the GNU General Public License along
|
19 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
20 | e57dd20b | aliguori | */
|
21 | e57dd20b | aliguori | |
22 | e57dd20b | aliguori | #include <stdlib.h> |
23 | e57dd20b | aliguori | #include <errno.h> |
24 | e57dd20b | aliguori | #include <string.h> |
25 | e57dd20b | aliguori | #include <sys/select.h> |
26 | e57dd20b | aliguori | #include <fcntl.h> |
27 | e57dd20b | aliguori | #include <unistd.h> |
28 | e57dd20b | aliguori | #include <termios.h> |
29 | e57dd20b | aliguori | #include <stdarg.h> |
30 | e57dd20b | aliguori | #include <sys/mman.h> |
31 | e57dd20b | aliguori | #include <xs.h> |
32 | e57dd20b | aliguori | #include <xen/io/console.h> |
33 | e57dd20b | aliguori | #include <xenctrl.h> |
34 | e57dd20b | aliguori | |
35 | e57dd20b | aliguori | #include "hw.h" |
36 | e57dd20b | aliguori | #include "sysemu.h" |
37 | e57dd20b | aliguori | #include "qemu-char.h" |
38 | e57dd20b | aliguori | #include "xen_backend.h" |
39 | e57dd20b | aliguori | |
40 | e57dd20b | aliguori | struct buffer {
|
41 | e57dd20b | aliguori | uint8_t *data; |
42 | e57dd20b | aliguori | size_t consumed; |
43 | e57dd20b | aliguori | size_t size; |
44 | e57dd20b | aliguori | size_t capacity; |
45 | e57dd20b | aliguori | size_t max_capacity; |
46 | e57dd20b | aliguori | }; |
47 | e57dd20b | aliguori | |
48 | e57dd20b | aliguori | struct XenConsole {
|
49 | e57dd20b | aliguori | struct XenDevice xendev; /* must be first */ |
50 | e57dd20b | aliguori | struct buffer buffer;
|
51 | e57dd20b | aliguori | char console[XEN_BUFSIZE];
|
52 | e57dd20b | aliguori | int ring_ref;
|
53 | e57dd20b | aliguori | void *sring;
|
54 | e57dd20b | aliguori | CharDriverState *chr; |
55 | e57dd20b | aliguori | int backlog;
|
56 | e57dd20b | aliguori | }; |
57 | e57dd20b | aliguori | |
58 | e57dd20b | aliguori | static void buffer_append(struct XenConsole *con) |
59 | e57dd20b | aliguori | { |
60 | e57dd20b | aliguori | struct buffer *buffer = &con->buffer;
|
61 | e57dd20b | aliguori | XENCONS_RING_IDX cons, prod, size; |
62 | e57dd20b | aliguori | struct xencons_interface *intf = con->sring;
|
63 | e57dd20b | aliguori | |
64 | e57dd20b | aliguori | cons = intf->out_cons; |
65 | e57dd20b | aliguori | prod = intf->out_prod; |
66 | e57dd20b | aliguori | xen_mb(); |
67 | e57dd20b | aliguori | |
68 | e57dd20b | aliguori | size = prod - cons; |
69 | e57dd20b | aliguori | if ((size == 0) || (size > sizeof(intf->out))) |
70 | e57dd20b | aliguori | return;
|
71 | e57dd20b | aliguori | |
72 | e57dd20b | aliguori | if ((buffer->capacity - buffer->size) < size) {
|
73 | e57dd20b | aliguori | buffer->capacity += (size + 1024);
|
74 | e57dd20b | aliguori | buffer->data = qemu_realloc(buffer->data, buffer->capacity); |
75 | e57dd20b | aliguori | } |
76 | e57dd20b | aliguori | |
77 | e57dd20b | aliguori | while (cons != prod)
|
78 | e57dd20b | aliguori | buffer->data[buffer->size++] = intf->out[ |
79 | e57dd20b | aliguori | MASK_XENCONS_IDX(cons++, intf->out)]; |
80 | e57dd20b | aliguori | |
81 | e57dd20b | aliguori | xen_mb(); |
82 | e57dd20b | aliguori | intf->out_cons = cons; |
83 | e57dd20b | aliguori | xen_be_send_notify(&con->xendev); |
84 | e57dd20b | aliguori | |
85 | e57dd20b | aliguori | if (buffer->max_capacity &&
|
86 | e57dd20b | aliguori | buffer->size > buffer->max_capacity) { |
87 | e57dd20b | aliguori | /* Discard the middle of the data. */
|
88 | e57dd20b | aliguori | |
89 | e57dd20b | aliguori | size_t over = buffer->size - buffer->max_capacity; |
90 | e57dd20b | aliguori | uint8_t *maxpos = buffer->data + buffer->max_capacity; |
91 | e57dd20b | aliguori | |
92 | e57dd20b | aliguori | memmove(maxpos - over, maxpos, over); |
93 | e57dd20b | aliguori | buffer->data = qemu_realloc(buffer->data, buffer->max_capacity); |
94 | e57dd20b | aliguori | buffer->size = buffer->capacity = buffer->max_capacity; |
95 | e57dd20b | aliguori | |
96 | e57dd20b | aliguori | if (buffer->consumed > buffer->max_capacity - over)
|
97 | e57dd20b | aliguori | buffer->consumed = buffer->max_capacity - over; |
98 | e57dd20b | aliguori | } |
99 | e57dd20b | aliguori | } |
100 | e57dd20b | aliguori | |
101 | e57dd20b | aliguori | static void buffer_advance(struct buffer *buffer, size_t len) |
102 | e57dd20b | aliguori | { |
103 | e57dd20b | aliguori | buffer->consumed += len; |
104 | e57dd20b | aliguori | if (buffer->consumed == buffer->size) {
|
105 | e57dd20b | aliguori | buffer->consumed = 0;
|
106 | e57dd20b | aliguori | buffer->size = 0;
|
107 | e57dd20b | aliguori | } |
108 | e57dd20b | aliguori | } |
109 | e57dd20b | aliguori | |
110 | e57dd20b | aliguori | static int ring_free_bytes(struct XenConsole *con) |
111 | e57dd20b | aliguori | { |
112 | e57dd20b | aliguori | struct xencons_interface *intf = con->sring;
|
113 | e57dd20b | aliguori | XENCONS_RING_IDX cons, prod, space; |
114 | e57dd20b | aliguori | |
115 | e57dd20b | aliguori | cons = intf->in_cons; |
116 | e57dd20b | aliguori | prod = intf->in_prod; |
117 | e57dd20b | aliguori | xen_mb(); |
118 | e57dd20b | aliguori | |
119 | e57dd20b | aliguori | space = prod - cons; |
120 | e57dd20b | aliguori | if (space > sizeof(intf->in)) |
121 | e57dd20b | aliguori | return 0; /* ring is screwed: ignore it */ |
122 | e57dd20b | aliguori | |
123 | e57dd20b | aliguori | return (sizeof(intf->in) - space); |
124 | e57dd20b | aliguori | } |
125 | e57dd20b | aliguori | |
126 | e57dd20b | aliguori | static int xencons_can_receive(void *opaque) |
127 | e57dd20b | aliguori | { |
128 | e57dd20b | aliguori | struct XenConsole *con = opaque;
|
129 | e57dd20b | aliguori | return ring_free_bytes(con);
|
130 | e57dd20b | aliguori | } |
131 | e57dd20b | aliguori | |
132 | e57dd20b | aliguori | static void xencons_receive(void *opaque, const uint8_t *buf, int len) |
133 | e57dd20b | aliguori | { |
134 | e57dd20b | aliguori | struct XenConsole *con = opaque;
|
135 | e57dd20b | aliguori | struct xencons_interface *intf = con->sring;
|
136 | e57dd20b | aliguori | XENCONS_RING_IDX prod; |
137 | e57dd20b | aliguori | int i, max;
|
138 | e57dd20b | aliguori | |
139 | e57dd20b | aliguori | max = ring_free_bytes(con); |
140 | e57dd20b | aliguori | /* The can_receive() func limits this, but check again anyway */
|
141 | e57dd20b | aliguori | if (max < len)
|
142 | e57dd20b | aliguori | len = max; |
143 | e57dd20b | aliguori | |
144 | e57dd20b | aliguori | prod = intf->in_prod; |
145 | e57dd20b | aliguori | for (i = 0; i < len; i++) { |
146 | e57dd20b | aliguori | intf->in[MASK_XENCONS_IDX(prod++, intf->in)] = |
147 | e57dd20b | aliguori | buf[i]; |
148 | e57dd20b | aliguori | } |
149 | e57dd20b | aliguori | xen_wmb(); |
150 | e57dd20b | aliguori | intf->in_prod = prod; |
151 | e57dd20b | aliguori | xen_be_send_notify(&con->xendev); |
152 | e57dd20b | aliguori | } |
153 | e57dd20b | aliguori | |
154 | e57dd20b | aliguori | static void xencons_send(struct XenConsole *con) |
155 | e57dd20b | aliguori | { |
156 | e57dd20b | aliguori | ssize_t len, size; |
157 | e57dd20b | aliguori | |
158 | e57dd20b | aliguori | size = con->buffer.size - con->buffer.consumed; |
159 | e57dd20b | aliguori | if (con->chr)
|
160 | e57dd20b | aliguori | len = qemu_chr_write(con->chr, con->buffer.data + con->buffer.consumed, |
161 | e57dd20b | aliguori | size); |
162 | e57dd20b | aliguori | else
|
163 | e57dd20b | aliguori | len = size; |
164 | e57dd20b | aliguori | if (len < 1) { |
165 | e57dd20b | aliguori | if (!con->backlog) {
|
166 | e57dd20b | aliguori | con->backlog = 1;
|
167 | e57dd20b | aliguori | xen_be_printf(&con->xendev, 1, "backlog piling up, nobody listening?\n"); |
168 | e57dd20b | aliguori | } |
169 | e57dd20b | aliguori | } else {
|
170 | e57dd20b | aliguori | buffer_advance(&con->buffer, len); |
171 | e57dd20b | aliguori | if (con->backlog && len == size) {
|
172 | e57dd20b | aliguori | con->backlog = 0;
|
173 | e57dd20b | aliguori | xen_be_printf(&con->xendev, 1, "backlog is gone\n"); |
174 | e57dd20b | aliguori | } |
175 | e57dd20b | aliguori | } |
176 | e57dd20b | aliguori | } |
177 | e57dd20b | aliguori | |
178 | e57dd20b | aliguori | /* -------------------------------------------------------------------- */
|
179 | e57dd20b | aliguori | |
180 | e57dd20b | aliguori | static int con_init(struct XenDevice *xendev) |
181 | e57dd20b | aliguori | { |
182 | e57dd20b | aliguori | struct XenConsole *con = container_of(xendev, struct XenConsole, xendev); |
183 | e57dd20b | aliguori | char *type, *dom;
|
184 | e57dd20b | aliguori | |
185 | e57dd20b | aliguori | /* setup */
|
186 | e57dd20b | aliguori | dom = xs_get_domain_path(xenstore, con->xendev.dom); |
187 | e57dd20b | aliguori | snprintf(con->console, sizeof(con->console), "%s/console", dom); |
188 | e57dd20b | aliguori | free(dom); |
189 | e57dd20b | aliguori | |
190 | e57dd20b | aliguori | type = xenstore_read_str(con->console, "type");
|
191 | 96248fd8 | blueswir1 | if (!type || strcmp(type, "ioemu") != 0) { |
192 | e57dd20b | aliguori | xen_be_printf(xendev, 1, "not for me (type=%s)\n", type); |
193 | e57dd20b | aliguori | return -1; |
194 | e57dd20b | aliguori | } |
195 | e57dd20b | aliguori | |
196 | e57dd20b | aliguori | if (!serial_hds[con->xendev.dev])
|
197 | e57dd20b | aliguori | xen_be_printf(xendev, 1, "WARNING: serial line %d not configured\n", |
198 | e57dd20b | aliguori | con->xendev.dev); |
199 | e57dd20b | aliguori | else
|
200 | e57dd20b | aliguori | con->chr = serial_hds[con->xendev.dev]; |
201 | e57dd20b | aliguori | |
202 | e57dd20b | aliguori | return 0; |
203 | e57dd20b | aliguori | } |
204 | e57dd20b | aliguori | |
205 | e57dd20b | aliguori | static int con_connect(struct XenDevice *xendev) |
206 | e57dd20b | aliguori | { |
207 | e57dd20b | aliguori | struct XenConsole *con = container_of(xendev, struct XenConsole, xendev); |
208 | e57dd20b | aliguori | int limit;
|
209 | e57dd20b | aliguori | |
210 | e57dd20b | aliguori | if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1) |
211 | e57dd20b | aliguori | return -1; |
212 | e57dd20b | aliguori | if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1) |
213 | e57dd20b | aliguori | return -1; |
214 | e57dd20b | aliguori | if (xenstore_read_int(con->console, "limit", &limit) == 0) |
215 | e57dd20b | aliguori | con->buffer.max_capacity = limit; |
216 | e57dd20b | aliguori | |
217 | e57dd20b | aliguori | con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom, |
218 | e57dd20b | aliguori | XC_PAGE_SIZE, |
219 | e57dd20b | aliguori | PROT_READ|PROT_WRITE, |
220 | e57dd20b | aliguori | con->ring_ref); |
221 | e57dd20b | aliguori | if (!con->sring)
|
222 | e57dd20b | aliguori | return -1; |
223 | e57dd20b | aliguori | |
224 | e57dd20b | aliguori | xen_be_bind_evtchn(&con->xendev); |
225 | e57dd20b | aliguori | if (con->chr)
|
226 | e57dd20b | aliguori | qemu_chr_add_handlers(con->chr, xencons_can_receive, xencons_receive, |
227 | e57dd20b | aliguori | NULL, con);
|
228 | e57dd20b | aliguori | |
229 | e57dd20b | aliguori | xen_be_printf(xendev, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n", |
230 | e57dd20b | aliguori | con->ring_ref, |
231 | e57dd20b | aliguori | con->xendev.remote_port, |
232 | e57dd20b | aliguori | con->xendev.local_port, |
233 | e57dd20b | aliguori | con->buffer.max_capacity); |
234 | e57dd20b | aliguori | return 0; |
235 | e57dd20b | aliguori | } |
236 | e57dd20b | aliguori | |
237 | e57dd20b | aliguori | static void con_disconnect(struct XenDevice *xendev) |
238 | e57dd20b | aliguori | { |
239 | e57dd20b | aliguori | struct XenConsole *con = container_of(xendev, struct XenConsole, xendev); |
240 | e57dd20b | aliguori | |
241 | e57dd20b | aliguori | if (con->chr)
|
242 | e57dd20b | aliguori | qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL); |
243 | e57dd20b | aliguori | xen_be_unbind_evtchn(&con->xendev); |
244 | e57dd20b | aliguori | |
245 | e57dd20b | aliguori | if (con->sring) {
|
246 | e57dd20b | aliguori | munmap(con->sring, XC_PAGE_SIZE); |
247 | e57dd20b | aliguori | con->sring = NULL;
|
248 | e57dd20b | aliguori | } |
249 | e57dd20b | aliguori | } |
250 | e57dd20b | aliguori | |
251 | e57dd20b | aliguori | static void con_event(struct XenDevice *xendev) |
252 | e57dd20b | aliguori | { |
253 | e57dd20b | aliguori | struct XenConsole *con = container_of(xendev, struct XenConsole, xendev); |
254 | e57dd20b | aliguori | |
255 | e57dd20b | aliguori | buffer_append(con); |
256 | e57dd20b | aliguori | if (con->buffer.size - con->buffer.consumed)
|
257 | e57dd20b | aliguori | xencons_send(con); |
258 | e57dd20b | aliguori | } |
259 | e57dd20b | aliguori | |
260 | e57dd20b | aliguori | /* -------------------------------------------------------------------- */
|
261 | e57dd20b | aliguori | |
262 | e57dd20b | aliguori | struct XenDevOps xen_console_ops = {
|
263 | e57dd20b | aliguori | .size = sizeof(struct XenConsole), |
264 | e57dd20b | aliguori | .flags = DEVOPS_FLAG_IGNORE_STATE, |
265 | e57dd20b | aliguori | .init = con_init, |
266 | e57dd20b | aliguori | .connect = con_connect, |
267 | e57dd20b | aliguori | .event = con_event, |
268 | e57dd20b | aliguori | .disconnect = con_disconnect, |
269 | e57dd20b | aliguori | }; |