2
0

sclpconsole-lm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "qemu/osdep.h"
  16. #include "qemu/thread.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/module.h"
  19. #include "chardev/char-fe.h"
  20. #include "hw/s390x/sclp.h"
  21. #include "migration/vmstate.h"
  22. #include "hw/s390x/event-facility.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/qdev-properties-system.h"
  25. #include "hw/s390x/ebcdic.h"
  26. #include "qom/object.h"
  27. #define SIZE_BUFFER 4096
  28. #define NEWLINE "\n"
  29. typedef struct OprtnsCommand {
  30. EventBufferHeader header;
  31. MDMSU message_unit;
  32. char data[];
  33. } QEMU_PACKED OprtnsCommand;
  34. /* max size for line-mode data in 4K SCCB page */
  35. #define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand))
  36. struct SCLPConsoleLM {
  37. SCLPEvent event;
  38. CharBackend chr;
  39. bool echo; /* immediate echo of input if true */
  40. uint32_t write_errors; /* errors writing to char layer */
  41. uint32_t length; /* length of byte stream in buffer */
  42. uint8_t buf[SIZE_CONSOLE_BUFFER];
  43. };
  44. typedef struct SCLPConsoleLM SCLPConsoleLM;
  45. #define TYPE_SCLPLM_CONSOLE "sclplmconsole"
  46. DECLARE_INSTANCE_CHECKER(SCLPConsoleLM, SCLPLM_CONSOLE,
  47. TYPE_SCLPLM_CONSOLE)
  48. /*
  49. * Character layer call-back functions
  50. *
  51. * Allow 1 character at a time
  52. *
  53. * Accumulate bytes from character layer in console buffer,
  54. * event_pending is set when a newline character is encountered
  55. *
  56. * The maximum command line length is limited by the maximum
  57. * space available in an SCCB. Line mode console input is sent
  58. * truncated to the guest in case it doesn't fit into the SCCB.
  59. */
  60. static int chr_can_read(void *opaque)
  61. {
  62. SCLPConsoleLM *scon = opaque;
  63. if (scon->event.event_pending) {
  64. return 0;
  65. }
  66. return 1;
  67. }
  68. static void chr_read(void *opaque, const uint8_t *buf, int size)
  69. {
  70. SCLPConsoleLM *scon = opaque;
  71. assert(size == 1);
  72. if (*buf == '\r' || *buf == '\n') {
  73. scon->event.event_pending = true;
  74. sclp_service_interrupt(0);
  75. return;
  76. }
  77. if (scon->length == SIZE_CONSOLE_BUFFER) {
  78. /* Eat the character, but still process CR and LF. */
  79. return;
  80. }
  81. scon->buf[scon->length] = *buf;
  82. scon->length += 1;
  83. if (scon->echo) {
  84. /* XXX this blocks entire thread. Rewrite to use
  85. * qemu_chr_fe_write and background I/O callbacks */
  86. qemu_chr_fe_write_all(&scon->chr, buf, size);
  87. }
  88. }
  89. /* functions to be called by event facility */
  90. static bool can_handle_event(uint8_t type)
  91. {
  92. return type == SCLP_EVENT_MESSAGE || type == SCLP_EVENT_PMSGCMD;
  93. }
  94. static sccb_mask_t send_mask(void)
  95. {
  96. return SCLP_EVENT_MASK_OP_CMD | SCLP_EVENT_MASK_PMSGCMD;
  97. }
  98. static sccb_mask_t receive_mask(void)
  99. {
  100. return SCLP_EVENT_MASK_MSG | SCLP_EVENT_MASK_PMSGCMD;
  101. }
  102. /*
  103. * Triggered by SCLP's read_event_data
  104. * - convert ASCII byte stream to EBCDIC and
  105. * - copy converted data into provided (SCLP) buffer
  106. */
  107. static int get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
  108. int avail)
  109. {
  110. int len;
  111. SCLPConsoleLM *cons = SCLPLM_CONSOLE(event);
  112. len = cons->length;
  113. /* data need to fit into provided SCLP buffer */
  114. if (len > avail) {
  115. return 1;
  116. }
  117. ebcdic_put(buf, (char *)&cons->buf, len);
  118. *size = len;
  119. cons->length = 0;
  120. /* data provided and no more data pending */
  121. event->event_pending = false;
  122. qemu_notify_event();
  123. return 0;
  124. }
  125. static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
  126. int *slen)
  127. {
  128. int avail, rc;
  129. size_t src_len;
  130. uint8_t *to;
  131. OprtnsCommand *oc = (OprtnsCommand *) evt_buf_hdr;
  132. if (!event->event_pending) {
  133. /* no data pending */
  134. return 0;
  135. }
  136. to = (uint8_t *)&oc->data;
  137. avail = *slen - sizeof(OprtnsCommand);
  138. rc = get_console_data(event, to, &src_len, avail);
  139. if (rc) {
  140. /* data didn't fit, try next SCCB */
  141. return 1;
  142. }
  143. oc->message_unit.mdmsu.gds_id = GDS_ID_MDSMU;
  144. oc->message_unit.mdmsu.length = cpu_to_be16(sizeof(struct MDMSU));
  145. oc->message_unit.cpmsu.gds_id = GDS_ID_CPMSU;
  146. oc->message_unit.cpmsu.length =
  147. cpu_to_be16(sizeof(struct MDMSU) - sizeof(GdsVector));
  148. oc->message_unit.text_command.gds_id = GDS_ID_TEXTCMD;
  149. oc->message_unit.text_command.length =
  150. cpu_to_be16(sizeof(struct MDMSU) - (2 * sizeof(GdsVector)));
  151. oc->message_unit.self_def_text_message.key = GDS_KEY_SELFDEFTEXTMSG;
  152. oc->message_unit.self_def_text_message.length =
  153. cpu_to_be16(sizeof(struct MDMSU) - (3 * sizeof(GdsVector)));
  154. oc->message_unit.text_message.key = GDS_KEY_TEXTMSG;
  155. oc->message_unit.text_message.length =
  156. cpu_to_be16(sizeof(GdsSubvector) + src_len);
  157. oc->header.length = cpu_to_be16(sizeof(OprtnsCommand) + src_len);
  158. oc->header.type = SCLP_EVENT_OPRTNS_COMMAND;
  159. *slen = avail - src_len;
  160. return 1;
  161. }
  162. /*
  163. * Triggered by SCLP's write_event_data
  164. * - write console data to character layer
  165. * returns < 0 if an error occurred
  166. */
  167. static int write_console_data(SCLPEvent *event, const uint8_t *buf, int len)
  168. {
  169. SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
  170. if (!qemu_chr_fe_backend_connected(&scon->chr)) {
  171. /* If there's no backend, we can just say we consumed all data. */
  172. return len;
  173. }
  174. /* XXX this blocks entire thread. Rewrite to use
  175. * qemu_chr_fe_write and background I/O callbacks */
  176. return qemu_chr_fe_write_all(&scon->chr, buf, len);
  177. }
  178. static int process_mdb(SCLPEvent *event, MDBO *mdbo)
  179. {
  180. int rc;
  181. int len;
  182. uint8_t buffer[SIZE_BUFFER];
  183. len = be16_to_cpu(mdbo->length);
  184. len -= sizeof(mdbo->length) + sizeof(mdbo->type)
  185. + sizeof(mdbo->mto.line_type_flags)
  186. + sizeof(mdbo->mto.alarm_control)
  187. + sizeof(mdbo->mto._reserved);
  188. assert(len <= SIZE_BUFFER);
  189. /* convert EBCDIC SCLP contents to ASCII console message */
  190. ascii_put(buffer, mdbo->mto.message, len);
  191. rc = write_console_data(event, (uint8_t *)NEWLINE, 1);
  192. if (rc < 0) {
  193. return rc;
  194. }
  195. return write_console_data(event, buffer, len);
  196. }
  197. static int write_event_data(SCLPEvent *event, EventBufferHeader *ebh)
  198. {
  199. int len;
  200. int written;
  201. int errors = 0;
  202. MDBO *mdbo;
  203. SclpMsg *data = (SclpMsg *) ebh;
  204. SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
  205. len = be16_to_cpu(data->mdb.header.length);
  206. if (len < sizeof(data->mdb.header)) {
  207. return SCLP_RC_INCONSISTENT_LENGTHS;
  208. }
  209. len -= sizeof(data->mdb.header);
  210. /* first check message buffers */
  211. mdbo = data->mdb.mdbo;
  212. while (len > 0) {
  213. if (be16_to_cpu(mdbo->length) > len
  214. || be16_to_cpu(mdbo->length) == 0) {
  215. return SCLP_RC_INCONSISTENT_LENGTHS;
  216. }
  217. len -= be16_to_cpu(mdbo->length);
  218. mdbo = (void *) mdbo + be16_to_cpu(mdbo->length);
  219. }
  220. /* then execute */
  221. len = be16_to_cpu(data->mdb.header.length) - sizeof(data->mdb.header);
  222. mdbo = data->mdb.mdbo;
  223. while (len > 0) {
  224. switch (be16_to_cpu(mdbo->type)) {
  225. case MESSAGE_TEXT:
  226. /* message text object */
  227. written = process_mdb(event, mdbo);
  228. if (written < 0) {
  229. /* character layer error */
  230. errors++;
  231. }
  232. break;
  233. default: /* ignore */
  234. break;
  235. }
  236. len -= be16_to_cpu(mdbo->length);
  237. mdbo = (void *) mdbo + be16_to_cpu(mdbo->length);
  238. }
  239. if (errors) {
  240. scon->write_errors += errors;
  241. }
  242. data->header.flags = SCLP_EVENT_BUFFER_ACCEPTED;
  243. return SCLP_RC_NORMAL_COMPLETION;
  244. }
  245. /* functions for live migration */
  246. static const VMStateDescription vmstate_sclplmconsole = {
  247. .name = "sclplmconsole",
  248. .version_id = 0,
  249. .minimum_version_id = 0,
  250. .fields = (const VMStateField[]) {
  251. VMSTATE_BOOL(event.event_pending, SCLPConsoleLM),
  252. VMSTATE_UINT32(write_errors, SCLPConsoleLM),
  253. VMSTATE_UINT32(length, SCLPConsoleLM),
  254. VMSTATE_UINT8_ARRAY(buf, SCLPConsoleLM, SIZE_CONSOLE_BUFFER),
  255. VMSTATE_END_OF_LIST()
  256. }
  257. };
  258. /* qemu object creation and initialization functions */
  259. /* tell character layer our call-back functions */
  260. static int console_init(SCLPEvent *event)
  261. {
  262. static bool console_available;
  263. SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
  264. if (console_available) {
  265. error_report("Multiple line-mode operator consoles are not supported");
  266. return -1;
  267. }
  268. console_available = true;
  269. qemu_chr_fe_set_handlers(&scon->chr, chr_can_read,
  270. chr_read, NULL, NULL, scon, NULL, true);
  271. return 0;
  272. }
  273. static void console_reset(DeviceState *dev)
  274. {
  275. SCLPEvent *event = SCLP_EVENT(dev);
  276. SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
  277. event->event_pending = false;
  278. scon->length = 0;
  279. scon->write_errors = 0;
  280. }
  281. static const Property console_properties[] = {
  282. DEFINE_PROP_CHR("chardev", SCLPConsoleLM, chr),
  283. DEFINE_PROP_UINT32("write_errors", SCLPConsoleLM, write_errors, 0),
  284. DEFINE_PROP_BOOL("echo", SCLPConsoleLM, echo, true),
  285. };
  286. static void console_class_init(ObjectClass *klass, void *data)
  287. {
  288. DeviceClass *dc = DEVICE_CLASS(klass);
  289. SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
  290. device_class_set_props(dc, console_properties);
  291. device_class_set_legacy_reset(dc, console_reset);
  292. dc->vmsd = &vmstate_sclplmconsole;
  293. ec->init = console_init;
  294. ec->get_send_mask = send_mask;
  295. ec->get_receive_mask = receive_mask;
  296. ec->can_handle_event = can_handle_event;
  297. ec->read_event_data = read_event_data;
  298. ec->write_event_data = write_event_data;
  299. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  300. }
  301. static const TypeInfo sclp_console_info = {
  302. .name = TYPE_SCLPLM_CONSOLE,
  303. .parent = TYPE_SCLP_EVENT,
  304. .instance_size = sizeof(SCLPConsoleLM),
  305. .class_init = console_class_init,
  306. .class_size = sizeof(SCLPEventClass),
  307. };
  308. static void register_types(void)
  309. {
  310. type_register_static(&sclp_console_info);
  311. }
  312. type_init(register_types)