]>
Commit | Line | Data |
---|---|---|
6a444f85 HG |
1 | /* |
2 | * SCLP event types | |
3 | * Operations Command - Line Mode input | |
4 | * Message - Line Mode output | |
5 | * | |
6 | * Copyright IBM, Corp. 2013 | |
7 | * | |
8 | * Authors: | |
9 | * Heinz Graalfs <graalfs@linux.vnet.ibm.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or (at your | |
12 | * option) any later version. See the COPYING file in the top-level directory. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include "hw/qdev.h" | |
17 | #include "qemu/thread.h" | |
18 | #include "qemu/error-report.h" | |
19 | #include "sysemu/char.h" | |
20 | ||
21 | #include "hw/s390x/sclp.h" | |
22 | #include "hw/s390x/event-facility.h" | |
23 | #include "hw/s390x/ebcdic.h" | |
24 | ||
25 | #define SIZE_BUFFER 4096 | |
26 | #define NEWLINE "\n" | |
27 | ||
28 | typedef struct OprtnsCommand { | |
29 | EventBufferHeader header; | |
30 | MDMSU message_unit; | |
31 | char data[0]; | |
32 | } QEMU_PACKED OprtnsCommand; | |
33 | ||
34 | /* max size for line-mode data in 4K SCCB page */ | |
35 | #define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand)) | |
36 | ||
37 | typedef struct SCLPConsoleLM { | |
38 | SCLPEvent event; | |
39 | CharDriverState *chr; | |
40 | bool echo; /* immediate echo of input if true */ | |
41 | uint32_t write_errors; /* errors writing to char layer */ | |
42 | uint32_t length; /* length of byte stream in buffer */ | |
43 | uint8_t buf[SIZE_CONSOLE_BUFFER]; | |
6a444f85 HG |
44 | } SCLPConsoleLM; |
45 | ||
46 | /* | |
47 | * Character layer call-back functions | |
48 | * | |
49 | * Allow 1 character at a time | |
50 | * | |
51 | * Accumulate bytes from character layer in console buffer, | |
52 | * event_pending is set when a newline character is encountered | |
53 | * | |
54 | * The maximum command line length is limited by the maximum | |
b3191432 HG |
55 | * space available in an SCCB. Line mode console input is sent |
56 | * truncated to the guest in case it doesn't fit into the SCCB. | |
6a444f85 HG |
57 | */ |
58 | ||
59 | static int chr_can_read(void *opaque) | |
60 | { | |
61 | SCLPConsoleLM *scon = opaque; | |
62 | ||
63 | if (scon->event.event_pending) { | |
64 | return 0; | |
6a444f85 | 65 | } |
b3191432 | 66 | return 1; |
6a444f85 HG |
67 | } |
68 | ||
4f3ed190 | 69 | static void chr_read(void *opaque, const uint8_t *buf, int size) |
6a444f85 | 70 | { |
4f3ed190 CB |
71 | SCLPConsoleLM *scon = opaque; |
72 | ||
6a444f85 HG |
73 | assert(size == 1); |
74 | ||
75 | if (*buf == '\r' || *buf == '\n') { | |
76 | scon->event.event_pending = true; | |
4f3ed190 | 77 | sclp_service_interrupt(0); |
6a444f85 HG |
78 | return; |
79 | } | |
b3191432 HG |
80 | if (scon->length == SIZE_CONSOLE_BUFFER) { |
81 | /* Eat the character, but still process CR and LF. */ | |
82 | return; | |
83 | } | |
6a444f85 HG |
84 | scon->buf[scon->length] = *buf; |
85 | scon->length += 1; | |
86 | if (scon->echo) { | |
87 | qemu_chr_fe_write(scon->chr, buf, size); | |
88 | } | |
89 | } | |
90 | ||
6a444f85 HG |
91 | /* functions to be called by event facility */ |
92 | ||
93 | static bool can_handle_event(uint8_t type) | |
94 | { | |
95 | return type == SCLP_EVENT_MESSAGE || type == SCLP_EVENT_PMSGCMD; | |
96 | } | |
97 | ||
98 | static unsigned int send_mask(void) | |
99 | { | |
100 | return SCLP_EVENT_MASK_OP_CMD | SCLP_EVENT_MASK_PMSGCMD; | |
101 | } | |
102 | ||
103 | static unsigned int receive_mask(void) | |
104 | { | |
105 | return SCLP_EVENT_MASK_MSG | SCLP_EVENT_MASK_PMSGCMD; | |
106 | } | |
107 | ||
108 | /* | |
109 | * Triggered by SCLP's read_event_data | |
110 | * - convert ASCII byte stream to EBCDIC and | |
111 | * - copy converted data into provided (SCLP) buffer | |
112 | */ | |
113 | static int get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size, | |
114 | int avail) | |
115 | { | |
116 | int len; | |
117 | ||
118 | SCLPConsoleLM *cons = DO_UPCAST(SCLPConsoleLM, event, event); | |
119 | ||
120 | len = cons->length; | |
121 | /* data need to fit into provided SCLP buffer */ | |
122 | if (len > avail) { | |
123 | return 1; | |
124 | } | |
125 | ||
126 | ebcdic_put(buf, (char *)&cons->buf, len); | |
127 | *size = len; | |
128 | cons->length = 0; | |
129 | /* data provided and no more data pending */ | |
130 | event->event_pending = false; | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr, | |
135 | int *slen) | |
136 | { | |
137 | int avail, rc; | |
138 | size_t src_len; | |
139 | uint8_t *to; | |
140 | OprtnsCommand *oc = (OprtnsCommand *) evt_buf_hdr; | |
141 | ||
142 | if (!event->event_pending) { | |
143 | /* no data pending */ | |
144 | return 0; | |
145 | } | |
146 | ||
147 | to = (uint8_t *)&oc->data; | |
148 | avail = *slen - sizeof(OprtnsCommand); | |
149 | rc = get_console_data(event, to, &src_len, avail); | |
150 | if (rc) { | |
151 | /* data didn't fit, try next SCCB */ | |
152 | return 1; | |
153 | } | |
154 | ||
155 | oc->message_unit.mdmsu.gds_id = GDS_ID_MDSMU; | |
156 | oc->message_unit.mdmsu.length = cpu_to_be16(sizeof(struct MDMSU)); | |
157 | ||
158 | oc->message_unit.cpmsu.gds_id = GDS_ID_CPMSU; | |
159 | oc->message_unit.cpmsu.length = | |
160 | cpu_to_be16(sizeof(struct MDMSU) - sizeof(GdsVector)); | |
161 | ||
162 | oc->message_unit.text_command.gds_id = GDS_ID_TEXTCMD; | |
163 | oc->message_unit.text_command.length = | |
164 | cpu_to_be16(sizeof(struct MDMSU) - (2 * sizeof(GdsVector))); | |
165 | ||
166 | oc->message_unit.self_def_text_message.key = GDS_KEY_SELFDEFTEXTMSG; | |
167 | oc->message_unit.self_def_text_message.length = | |
168 | cpu_to_be16(sizeof(struct MDMSU) - (3 * sizeof(GdsVector))); | |
169 | ||
170 | oc->message_unit.text_message.key = GDS_KEY_TEXTMSG; | |
171 | oc->message_unit.text_message.length = | |
172 | cpu_to_be16(sizeof(GdsSubvector) + src_len); | |
173 | ||
174 | oc->header.length = cpu_to_be16(sizeof(OprtnsCommand) + src_len); | |
175 | oc->header.type = SCLP_EVENT_OPRTNS_COMMAND; | |
176 | *slen = avail - src_len; | |
177 | ||
178 | return 1; | |
179 | } | |
180 | ||
181 | /* | |
182 | * Triggered by SCLP's write_event_data | |
183 | * - write console data to character layer | |
184 | * returns < 0 if an error occurred | |
185 | */ | |
186 | static int write_console_data(SCLPEvent *event, const uint8_t *buf, int len) | |
187 | { | |
188 | int ret = 0; | |
189 | const uint8_t *buf_offset; | |
190 | ||
191 | SCLPConsoleLM *scon = DO_UPCAST(SCLPConsoleLM, event, event); | |
192 | ||
193 | if (!scon->chr) { | |
194 | /* If there's no backend, we can just say we consumed all data. */ | |
195 | return len; | |
196 | } | |
197 | ||
198 | buf_offset = buf; | |
199 | while (len > 0) { | |
200 | ret = qemu_chr_fe_write(scon->chr, buf, len); | |
201 | if (ret == 0) { | |
202 | /* a pty doesn't seem to be connected - no error */ | |
203 | len = 0; | |
204 | } else if (ret == -EAGAIN || (ret > 0 && ret < len)) { | |
205 | len -= ret; | |
206 | buf_offset += ret; | |
207 | } else { | |
208 | len = 0; | |
209 | } | |
210 | } | |
211 | ||
212 | return ret; | |
213 | } | |
214 | ||
215 | static int process_mdb(SCLPEvent *event, MDBO *mdbo) | |
216 | { | |
217 | int rc; | |
218 | int len; | |
219 | uint8_t buffer[SIZE_BUFFER]; | |
220 | ||
221 | len = be16_to_cpu(mdbo->length); | |
222 | len -= sizeof(mdbo->length) + sizeof(mdbo->type) | |
223 | + sizeof(mdbo->mto.line_type_flags) | |
224 | + sizeof(mdbo->mto.alarm_control) | |
225 | + sizeof(mdbo->mto._reserved); | |
226 | ||
227 | assert(len <= SIZE_BUFFER); | |
228 | ||
229 | /* convert EBCDIC SCLP contents to ASCII console message */ | |
230 | ascii_put(buffer, mdbo->mto.message, len); | |
231 | rc = write_console_data(event, (uint8_t *)NEWLINE, 1); | |
232 | if (rc < 0) { | |
233 | return rc; | |
234 | } | |
235 | return write_console_data(event, buffer, len); | |
236 | } | |
237 | ||
238 | static int write_event_data(SCLPEvent *event, EventBufferHeader *ebh) | |
239 | { | |
240 | int len; | |
241 | int written; | |
242 | int errors = 0; | |
243 | MDBO *mdbo; | |
244 | SclpMsg *data = (SclpMsg *) ebh; | |
245 | SCLPConsoleLM *scon = DO_UPCAST(SCLPConsoleLM, event, event); | |
246 | ||
247 | len = be16_to_cpu(data->mdb.header.length); | |
248 | if (len < sizeof(data->mdb.header)) { | |
249 | return SCLP_RC_INCONSISTENT_LENGTHS; | |
250 | } | |
251 | len -= sizeof(data->mdb.header); | |
252 | ||
253 | /* first check message buffers */ | |
254 | mdbo = data->mdb.mdbo; | |
255 | while (len > 0) { | |
256 | if (be16_to_cpu(mdbo->length) > len | |
257 | || be16_to_cpu(mdbo->length) == 0) { | |
258 | return SCLP_RC_INCONSISTENT_LENGTHS; | |
259 | } | |
260 | len -= be16_to_cpu(mdbo->length); | |
261 | mdbo = (void *) mdbo + be16_to_cpu(mdbo->length); | |
262 | } | |
263 | ||
264 | /* then execute */ | |
265 | len = be16_to_cpu(data->mdb.header.length) - sizeof(data->mdb.header); | |
266 | mdbo = data->mdb.mdbo; | |
267 | while (len > 0) { | |
268 | switch (be16_to_cpu(mdbo->type)) { | |
269 | case MESSAGE_TEXT: | |
270 | /* message text object */ | |
271 | written = process_mdb(event, mdbo); | |
272 | if (written < 0) { | |
273 | /* character layer error */ | |
274 | errors++; | |
275 | } | |
276 | break; | |
277 | default: /* ignore */ | |
278 | break; | |
279 | } | |
280 | len -= be16_to_cpu(mdbo->length); | |
281 | mdbo = (void *) mdbo + be16_to_cpu(mdbo->length); | |
282 | } | |
283 | if (errors) { | |
284 | scon->write_errors += errors; | |
285 | } | |
286 | data->header.flags = SCLP_EVENT_BUFFER_ACCEPTED; | |
287 | ||
288 | return SCLP_RC_NORMAL_COMPLETION; | |
289 | } | |
290 | ||
6a444f85 HG |
291 | /* functions for live migration */ |
292 | ||
293 | static const VMStateDescription vmstate_sclplmconsole = { | |
294 | .name = "sclplmconsole", | |
295 | .version_id = 0, | |
296 | .minimum_version_id = 0, | |
35d08458 | 297 | .fields = (VMStateField[]) { |
6a444f85 HG |
298 | VMSTATE_BOOL(event.event_pending, SCLPConsoleLM), |
299 | VMSTATE_UINT32(write_errors, SCLPConsoleLM), | |
300 | VMSTATE_UINT32(length, SCLPConsoleLM), | |
301 | VMSTATE_UINT8_ARRAY(buf, SCLPConsoleLM, SIZE_CONSOLE_BUFFER), | |
302 | VMSTATE_END_OF_LIST() | |
303 | } | |
304 | }; | |
305 | ||
306 | /* qemu object creation and initialization functions */ | |
307 | ||
308 | /* tell character layer our call-back functions */ | |
309 | ||
310 | static int console_init(SCLPEvent *event) | |
311 | { | |
312 | static bool console_available; | |
313 | ||
314 | SCLPConsoleLM *scon = DO_UPCAST(SCLPConsoleLM, event, event); | |
315 | ||
316 | if (console_available) { | |
317 | error_report("Multiple line-mode operator consoles are not supported"); | |
318 | return -1; | |
319 | } | |
320 | console_available = true; | |
321 | ||
322 | if (scon->chr) { | |
323 | qemu_chr_add_handlers(scon->chr, chr_can_read, chr_read, NULL, scon); | |
324 | } | |
6a444f85 HG |
325 | |
326 | return 0; | |
327 | } | |
328 | ||
329 | static int console_exit(SCLPEvent *event) | |
330 | { | |
331 | return 0; | |
332 | } | |
333 | ||
334 | static void console_reset(DeviceState *dev) | |
335 | { | |
336 | SCLPEvent *event = SCLP_EVENT(dev); | |
337 | SCLPConsoleLM *scon = DO_UPCAST(SCLPConsoleLM, event, event); | |
338 | ||
339 | event->event_pending = false; | |
340 | scon->length = 0; | |
341 | scon->write_errors = 0; | |
342 | } | |
343 | ||
344 | static Property console_properties[] = { | |
345 | DEFINE_PROP_CHR("chardev", SCLPConsoleLM, chr), | |
346 | DEFINE_PROP_UINT32("write_errors", SCLPConsoleLM, write_errors, 0), | |
347 | DEFINE_PROP_BOOL("echo", SCLPConsoleLM, echo, true), | |
348 | DEFINE_PROP_END_OF_LIST(), | |
349 | }; | |
350 | ||
351 | static void console_class_init(ObjectClass *klass, void *data) | |
352 | { | |
353 | DeviceClass *dc = DEVICE_CLASS(klass); | |
354 | SCLPEventClass *ec = SCLP_EVENT_CLASS(klass); | |
355 | ||
356 | dc->props = console_properties; | |
357 | dc->reset = console_reset; | |
358 | dc->vmsd = &vmstate_sclplmconsole; | |
359 | ec->init = console_init; | |
360 | ec->exit = console_exit; | |
361 | ec->get_send_mask = send_mask; | |
362 | ec->get_receive_mask = receive_mask; | |
363 | ec->can_handle_event = can_handle_event; | |
364 | ec->read_event_data = read_event_data; | |
365 | ec->write_event_data = write_event_data; | |
366 | } | |
367 | ||
368 | static const TypeInfo sclp_console_info = { | |
369 | .name = "sclplmconsole", | |
370 | .parent = TYPE_SCLP_EVENT, | |
371 | .instance_size = sizeof(SCLPConsoleLM), | |
372 | .class_init = console_class_init, | |
373 | .class_size = sizeof(SCLPEventClass), | |
374 | }; | |
375 | ||
376 | static void register_types(void) | |
377 | { | |
378 | type_register_static(&sclp_console_info); | |
379 | } | |
380 | ||
381 | type_init(register_types) |