Received: from eggs.gnu.org ([2001:4830:134:3::10]:38114) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMrRr-0006oO-MF for qemu-devel@nongnu.org; Thu, 19 Sep 2013 23:32:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VMrRk-0002Zh-Ad for qemu-devel@nongnu.org; Thu, 19 Sep 2013 23:32:03 -0400 Received: from cantor2.suse.de ([195.135.220.15]:51481 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMrRj-0002Zd-S8 for qemu-devel@nongnu.org; Thu, 19 Sep 2013 23:31:56 -0400 Mime-Version: 1.0 (Apple Message framework v1278) Content-Type: text/plain; charset=us-ascii From: Alexander Graf In-Reply-To: <1379499572-49737-12-git-send-email-borntraeger@de.ibm.com> Date: Thu, 19 Sep 2013 22:31:46 -0500 Content-Transfer-Encoding: quoted-printable Message-Id: References: <1379499572-49737-1-git-send-email-borntraeger@de.ibm.com> <1379499572-49737-12-git-send-email-borntraeger@de.ibm.com> Subject: Re: [Qemu-devel] [PATCH 11/11] s390/sclplmconsole: Add support for SCLP line-mode console List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Christian Borntraeger Cc: Jens Freimann , Heinz Graalfs , qemu-devel On 18.09.2013, at 05:19, Christian Borntraeger wrote: > From: Heinz Graalfs >=20 > Add simple support for SCLP line-mode also known as operating > system messages. This can be added in addition to or instead of > the SCLP full screen console with -device sclplmconsole. >=20 > Signed-off-by: Heinz Graalfs > Signed-off-by: Christian Borntraeger > --- > hw/char/Makefile.objs | 2 +- > hw/char/sclpconsole-lm.c | 393 = ++++++++++++++++++++++++++++++++++++++ > include/hw/s390x/event-facility.h | 78 ++++++++ > 3 files changed, 472 insertions(+), 1 deletion(-) > create mode 100644 hw/char/sclpconsole-lm.c >=20 > diff --git a/hw/char/Makefile.objs b/hw/char/Makefile.objs > index f8f3dbc..cbd6a00 100644 > --- a/hw/char/Makefile.objs > +++ b/hw/char/Makefile.objs > @@ -22,6 +22,6 @@ common-obj-$(CONFIG_IMX) +=3D imx_serial.o > common-obj-$(CONFIG_LM32) +=3D lm32_juart.o > common-obj-$(CONFIG_LM32) +=3D lm32_uart.o > common-obj-$(CONFIG_MILKYMIST) +=3D milkymist-uart.o > -common-obj-$(CONFIG_SCLPCONSOLE) +=3D sclpconsole.o > +common-obj-$(CONFIG_SCLPCONSOLE) +=3D sclpconsole.o sclpconsole-lm.o >=20 > obj-$(CONFIG_VIRTIO) +=3D virtio-serial-bus.o > diff --git a/hw/char/sclpconsole-lm.c b/hw/char/sclpconsole-lm.c > new file mode 100644 > index 0000000..a0eb59b > --- /dev/null > +++ b/hw/char/sclpconsole-lm.c > @@ -0,0 +1,393 @@ > +/* > + * SCLP event types > + * Operations Command - Line Mode input > + * Message - Line Mode output > + * > + * Copyright IBM, Corp. 2013 > + * > + * Authors: > + * Heinz Graalfs > + * > + * This work is licensed under the terms of the GNU GPL, version 2 or = (at your > + * option) any later version. See the COPYING file in the top-level = directory. > + * > + */ > + > +#include "hw/qdev.h" > +#include "qemu/thread.h" > +#include "qemu/error-report.h" > +#include "sysemu/char.h" > + > +#include "hw/s390x/sclp.h" > +#include "hw/s390x/event-facility.h" > +#include "hw/s390x/ebcdic.h" > + > +typedef struct OprtnsCommand { > + EventBufferHeader header; > + MDMSU message_unit; > + char data[0]; > +} QEMU_PACKED OprtnsCommand; > + > +/* max size for line-mode data in 4K SCCB page */ > +#define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand)) > + > +typedef struct SCLPConsoleLM { > + SCLPEvent event; > + CharDriverState *chr; > + bool echo; /* immediate echo of input if true = */ > + uint32_t write_errors; /* errors writing to char layer = */ > + uint32_t length; /* length of byte stream in buffer = */ > + uint8_t buf[SIZE_CONSOLE_BUFFER]; > + qemu_irq irq_console_read; > +} SCLPConsoleLM; > + > +/* > +* Character layer call-back functions > + * > + * Allow 1 character at a time > + * > + * Accumulate bytes from character layer in console buffer, > + * event_pending is set when a newline character is encountered > + * > + * The maximum command line length is limited by the maximum > + * space available in an SCCB > + */ > + > +static int chr_can_read(void *opaque) > +{ > + SCLPConsoleLM *scon =3D opaque; > + > + if (scon->event.event_pending) { > + return 0; > + } else if (SIZE_CONSOLE_BUFFER - scon->length) { > + return 1; > + } > + return 0; > +} > + > +static void receive_from_chr_layer(SCLPConsoleLM *scon, const uint8_t = *buf, > + int size) > +{ > + assert(size =3D=3D 1); > + > + if (*buf =3D=3D '\r' || *buf =3D=3D '\n') { > + scon->event.event_pending =3D true; > + return; > + } > + memcpy(&scon->buf[scon->length], buf, 1); Why use memcpy here? > + scon->length +=3D 1; > + if (scon->echo) { > + qemu_chr_fe_write(scon->chr, buf, size); > + } > +} > + > +/* > + * Send data from a char device over to the guest > + */ > +static void chr_read(void *opaque, const uint8_t *buf, int size) > +{ > + SCLPConsoleLM *scon =3D opaque; > + > + receive_from_chr_layer(scon, buf, size); > + if (scon->event.event_pending) { > + /* trigger SCLP read operation */ > + qemu_irq_raise(scon->irq_console_read); > + } > +} > + > +/* functions to be called by event facility */ > + > +static bool can_handle_event(uint8_t type) > +{ > + return type =3D=3D SCLP_EVENT_MESSAGE || type =3D=3D = SCLP_EVENT_PMSGCMD; > +} > + > +static unsigned int send_mask(void) > +{ > + return SCLP_EVENT_MASK_OP_CMD | SCLP_EVENT_MASK_PMSGCMD; > +} > + > +static unsigned int receive_mask(void) > +{ > + return SCLP_EVENT_MASK_MSG | SCLP_EVENT_MASK_PMSGCMD; > +} > + > +/* > + * Triggered by SCLP's read_event_data > + * - convert ASCII byte stream to EBCDIC and > + * - copy converted data into provided (SCLP) buffer > + */ > +static int get_console_data(SCLPEvent *event, uint8_t *buf, size_t = *size, > + int avail) > +{ > + int len; > + > + SCLPConsoleLM *cons =3D DO_UPCAST(SCLPConsoleLM, event, event); > + > + len =3D cons->length; > + /* data need to fit into provided SCLP buffer */ > + if (len > avail) { > + return 1; > + } > + > + ebcdic_put(buf, (char *)&cons->buf, len); > + *size =3D len; > + cons->length =3D 0; > + /* data provided and no more data pending */ > + event->event_pending =3D false; > + return 0; > +} > + > +static int read_event_data(SCLPEvent *event, EventBufferHeader = *evt_buf_hdr, > + int *slen) > +{ > + int avail, rc; > + size_t src_len; > + uint8_t *to; > + OprtnsCommand *oc =3D (OprtnsCommand *) evt_buf_hdr; > + > + if (!event->event_pending) { > + /* no data pending */ > + return 0; > + } > + > + to =3D (uint8_t *)&oc->data; > + avail =3D *slen - sizeof(OprtnsCommand); > + rc =3D get_console_data(event, to, &src_len, avail); > + if (rc) { > + /* data didn't fit, try next SCCB */ > + return 1; > + } > + > + oc->message_unit.mdmsu.gds_id =3D GDS_ID_MDSMU; > + oc->message_unit.mdmsu.length =3D cpu_to_be16(sizeof(struct = MDMSU)); > + > + oc->message_unit.cpmsu.gds_id =3D GDS_ID_CPMSU; > + oc->message_unit.cpmsu.length =3D > + cpu_to_be16(sizeof(struct MDMSU) - sizeof(GdsVector)); > + > + oc->message_unit.text_command.gds_id =3D GDS_ID_TEXTCMD; > + oc->message_unit.text_command.length =3D > + cpu_to_be16(sizeof(struct MDMSU) - (2 * sizeof(GdsVector))); > + > + oc->message_unit.self_def_text_message.key =3D = GDS_KEY_SELFDEFTEXTMSG; > + oc->message_unit.self_def_text_message.length =3D > + cpu_to_be16(sizeof(struct MDMSU) - (3 * sizeof(GdsVector))); > + > + oc->message_unit.text_message.key =3D GDS_KEY_TEXTMSG; > + oc->message_unit.text_message.length =3D > + cpu_to_be16(sizeof(GdsSubvector) + src_len); > + > + oc->header.length =3D cpu_to_be16(sizeof(OprtnsCommand) + = src_len); > + oc->header.type =3D SCLP_EVENT_OPRTNS_COMMAND; > + *slen =3D avail - src_len; > + > + return 1; > +} > + > +/* > + * Triggered by SCLP's write_event_data > + * - write console data to character layer > + * returns < 0 if an error occurred > + */ > +static int write_console_data(SCLPEvent *event, const uint8_t *buf, = int len) > +{ > + int ret =3D 0; > + const uint8_t *buf_offset; > + > + SCLPConsoleLM *scon =3D DO_UPCAST(SCLPConsoleLM, event, event); > + > + if (!scon->chr) { > + /* If there's no backend, we can just say we consumed all = data. */ > + return len; > + } > + > + buf_offset =3D buf; > + while (len > 0) { > + ret =3D qemu_chr_fe_write(scon->chr, buf, len); > + if (ret =3D=3D 0) { > + /* a pty doesn't seem to be connected - no error */ > + len =3D 0; > + } else if (ret =3D=3D -EAGAIN || (ret > 0 && ret < len)) { > + len -=3D ret; > + buf_offset +=3D ret; > + } else { > + len =3D 0; > + } > + } > + > + return ret; > +} > + > +#define SIZE_BUFFER 4096 > +#define NEWLINE "\n" Why are these in the middle of the file? > + > +static int process_mdb(SCLPEvent *event, MDBO *mdbo) > +{ > + int rc; > + int len; > + uint8_t buffer[SIZE_BUFFER]; > + > + len =3D be16_to_cpu(mdbo->length); > + len -=3D sizeof(mdbo->length) + sizeof(mdbo->type) > + + sizeof(mdbo->mto.line_type_flags) > + + sizeof(mdbo->mto.alarm_control) > + + sizeof(mdbo->mto._reserved); > + > + assert(len <=3D SIZE_BUFFER); > + > + /* convert EBCDIC SCLP contents to ASCII console message */ > + ascii_put(buffer, mdbo->mto.message, len); > + rc =3D write_console_data(event, (uint8_t *)NEWLINE, 1); > + if (rc < 0) { > + return rc; > + } > + return write_console_data(event, buffer, len); > +} > + > +static int write_event_data(SCLPEvent *event, EventBufferHeader *ebh) > +{ > + int len; > + int written; > + int errors =3D 0; > + MDBO *mdbo; > + SclpMsg *data =3D (SclpMsg *) ebh; > + SCLPConsoleLM *scon =3D DO_UPCAST(SCLPConsoleLM, event, event); > + > + len =3D be16_to_cpu(data->mdb.header.length) - = sizeof(data->mdb.header); > + > + /* first check message buffers */ > + mdbo =3D data->mdb.mdbo; > + while (len > 0) { > + if (be16_to_cpu(mdbo->length) > len > + || be16_to_cpu(mdbo->length) =3D=3D 0) { > + return SCLP_RC_INCONSISTENT_LENGTHS; > + } > + len -=3D be16_to_cpu(mdbo->length); > + mdbo =3D (void *) mdbo + be16_to_cpu(mdbo->length); > + } > + > + /* then execute */ > + len =3D be16_to_cpu(data->mdb.header.length) - = sizeof(data->mdb.header); Can this overflow? > + mdbo =3D data->mdb.mdbo; > + while (len > 0) { > + switch (be16_to_cpu(mdbo->type)) { > + case 4: What is 4? This wants to be a #define. > + /* message text object */ > + written =3D process_mdb(event, mdbo); > + if (written < 0) { > + /* character layer error */ > + errors++; > + } > + break; > + default: /* ignore */ > + break; > + } > + len -=3D be16_to_cpu(mdbo->length); > + mdbo =3D (void *) mdbo + be16_to_cpu(mdbo->length); > + } > + if (errors) { > + scon->write_errors +=3D errors; > + } > + data->header.flags =3D SCLP_EVENT_BUFFER_ACCEPTED; > + > + return SCLP_RC_NORMAL_COMPLETION; > +} > + > +static void trigger_console_data(void *opaque, int n, int level) > +{ > + sclp_service_interrupt(0); > +} > + > +/* functions for live migration */ > + > +static const VMStateDescription vmstate_sclplmconsole =3D { > + .name =3D "sclplmconsole", > + .version_id =3D 0, > + .minimum_version_id =3D 0, > + .minimum_version_id_old =3D 0, > + .fields =3D (VMStateField[]) { > + VMSTATE_BOOL(event.event_pending, SCLPConsoleLM), > + VMSTATE_UINT32(write_errors, SCLPConsoleLM), > + VMSTATE_UINT32(length, SCLPConsoleLM), > + VMSTATE_UINT8_ARRAY(buf, SCLPConsoleLM, SIZE_CONSOLE_BUFFER), > + VMSTATE_END_OF_LIST() > + } > +}; > + > +/* qemu object creation and initialization functions */ > + > +/* tell character layer our call-back functions */ > + > +static int console_init(SCLPEvent *event) > +{ > + static bool console_available; > + > + SCLPConsoleLM *scon =3D DO_UPCAST(SCLPConsoleLM, event, event); > + > + if (console_available) { > + error_report("Multiple line-mode operator consoles are not = supported"); > + return -1; > + } > + console_available =3D true; > + > + if (scon->chr) { > + qemu_chr_add_handlers(scon->chr, chr_can_read, chr_read, = NULL, scon); > + } > + scon->irq_console_read =3D = *qemu_allocate_irqs(trigger_console_data, NULL, 1); > + > + return vmstate_register(NULL, 0, &vmstate_sclplmconsole, scon); This should go into your QOM class. Alex