2012-10-29 06:13:25 +04:00
|
|
|
/*
|
|
|
|
* SCLP event type
|
|
|
|
* Ascii Console Data (VT220 Console)
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Heinz Graalfs <graalfs@de.ibm.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:00 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-10-29 06:13:25 +04:00
|
|
|
#include <hw/qdev.h>
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/thread.h"
|
2013-02-04 14:37:52 +04:00
|
|
|
#include "qemu/error-report.h"
|
2012-10-29 06:13:25 +04:00
|
|
|
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/s390x/sclp.h"
|
|
|
|
#include "hw/s390x/event-facility.h"
|
2013-04-08 18:55:25 +04:00
|
|
|
#include "sysemu/char.h"
|
2012-10-29 06:13:25 +04:00
|
|
|
|
|
|
|
typedef struct ASCIIConsoleData {
|
|
|
|
EventBufferHeader ebh;
|
|
|
|
char data[0];
|
|
|
|
} QEMU_PACKED ASCIIConsoleData;
|
|
|
|
|
|
|
|
/* max size for ASCII data in 4K SCCB page */
|
|
|
|
#define SIZE_BUFFER_VT220 4080
|
|
|
|
|
|
|
|
typedef struct SCLPConsole {
|
|
|
|
SCLPEvent event;
|
|
|
|
CharDriverState *chr;
|
2013-08-28 18:30:28 +04:00
|
|
|
uint8_t iov[SIZE_BUFFER_VT220];
|
|
|
|
uint32_t iov_sclp; /* offset in buf for SCLP read operation */
|
|
|
|
uint32_t iov_bs; /* offset in buf for char layer read operation */
|
|
|
|
uint32_t iov_data_len; /* length of byte stream in buffer */
|
|
|
|
uint32_t iov_sclp_rest; /* length of byte stream not read via SCLP */
|
2014-10-24 17:10:49 +04:00
|
|
|
bool notify; /* qemu_notify_event() req'd if true */
|
2012-10-29 06:13:25 +04:00
|
|
|
} SCLPConsole;
|
|
|
|
|
2016-03-29 10:47:25 +03:00
|
|
|
#define TYPE_SCLP_CONSOLE "sclpconsole"
|
|
|
|
#define SCLP_CONSOLE(obj) \
|
|
|
|
OBJECT_CHECK(SCLPConsole, (obj), TYPE_SCLP_CONSOLE)
|
|
|
|
|
2012-10-29 06:13:25 +04:00
|
|
|
/* character layer call-back functions */
|
|
|
|
|
|
|
|
/* Return number of bytes that fit into iov buffer */
|
|
|
|
static int chr_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
SCLPConsole *scon = opaque;
|
2014-10-24 17:10:49 +04:00
|
|
|
int avail = SIZE_BUFFER_VT220 - scon->iov_data_len;
|
2012-10-29 06:13:25 +04:00
|
|
|
|
2014-10-24 17:10:49 +04:00
|
|
|
if (avail == 0) {
|
|
|
|
scon->notify = true;
|
|
|
|
}
|
|
|
|
return avail;
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 17:17:04 +04:00
|
|
|
/* Send data from a char device over to the guest */
|
|
|
|
static void chr_read(void *opaque, const uint8_t *buf, int size)
|
2012-10-29 06:13:25 +04:00
|
|
|
{
|
2014-03-10 17:17:04 +04:00
|
|
|
SCLPConsole *scon = opaque;
|
|
|
|
|
|
|
|
assert(scon);
|
2012-10-29 06:13:25 +04:00
|
|
|
/* read data must fit into current buffer */
|
|
|
|
assert(size <= SIZE_BUFFER_VT220 - scon->iov_data_len);
|
|
|
|
|
|
|
|
/* put byte-stream from character layer into buffer */
|
2013-08-28 18:30:28 +04:00
|
|
|
memcpy(&scon->iov[scon->iov_bs], buf, size);
|
2012-10-29 06:13:25 +04:00
|
|
|
scon->iov_data_len += size;
|
|
|
|
scon->iov_sclp_rest += size;
|
|
|
|
scon->iov_bs += size;
|
|
|
|
scon->event.event_pending = true;
|
2014-03-10 17:17:04 +04:00
|
|
|
sclp_service_interrupt(0);
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* functions to be called by event facility */
|
|
|
|
|
2013-09-17 15:07:30 +04:00
|
|
|
static bool can_handle_event(uint8_t type)
|
2012-10-29 06:13:25 +04:00
|
|
|
{
|
2013-09-17 15:07:30 +04:00
|
|
|
return type == SCLP_EVENT_ASCII_CONSOLE_DATA;
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int send_mask(void)
|
|
|
|
{
|
|
|
|
return SCLP_EVENT_MASK_MSG_ASCII;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int receive_mask(void)
|
|
|
|
{
|
|
|
|
return SCLP_EVENT_MASK_MSG_ASCII;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* triggered by SCLP's read_event_data -
|
|
|
|
* copy console data byte-stream into provided (SCLP) buffer
|
|
|
|
*/
|
|
|
|
static void get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
|
|
|
|
int avail)
|
|
|
|
{
|
2016-03-29 10:47:25 +03:00
|
|
|
SCLPConsole *cons = SCLP_CONSOLE(event);
|
2012-10-29 06:13:25 +04:00
|
|
|
|
|
|
|
/* first byte is hex 0 saying an ascii string follows */
|
|
|
|
*buf++ = '\0';
|
|
|
|
avail--;
|
|
|
|
/* if all data fit into provided SCLP buffer */
|
|
|
|
if (avail >= cons->iov_sclp_rest) {
|
|
|
|
/* copy character byte-stream to SCLP buffer */
|
2013-08-28 18:30:28 +04:00
|
|
|
memcpy(buf, &cons->iov[cons->iov_sclp], cons->iov_sclp_rest);
|
2012-10-29 06:13:25 +04:00
|
|
|
*size = cons->iov_sclp_rest + 1;
|
2013-08-28 18:30:28 +04:00
|
|
|
cons->iov_sclp = 0;
|
|
|
|
cons->iov_bs = 0;
|
2012-10-29 06:13:25 +04:00
|
|
|
cons->iov_data_len = 0;
|
|
|
|
cons->iov_sclp_rest = 0;
|
|
|
|
event->event_pending = false;
|
|
|
|
/* data provided and no more data pending */
|
|
|
|
} else {
|
|
|
|
/* if provided buffer is too small, just copy part */
|
2013-08-28 18:30:28 +04:00
|
|
|
memcpy(buf, &cons->iov[cons->iov_sclp], avail);
|
2012-10-29 06:13:25 +04:00
|
|
|
*size = avail + 1;
|
|
|
|
cons->iov_sclp_rest -= avail;
|
|
|
|
cons->iov_sclp += avail;
|
|
|
|
/* more data pending */
|
|
|
|
}
|
2014-10-24 17:10:49 +04:00
|
|
|
if (cons->notify) {
|
|
|
|
cons->notify = false;
|
|
|
|
qemu_notify_event();
|
|
|
|
}
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
|
|
|
|
int *slen)
|
|
|
|
{
|
|
|
|
int avail;
|
|
|
|
size_t src_len;
|
|
|
|
uint8_t *to;
|
|
|
|
ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
|
|
|
|
|
|
|
|
if (!event->event_pending) {
|
|
|
|
/* no data pending */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
to = (uint8_t *)&acd->data;
|
|
|
|
avail = *slen - sizeof(ASCIIConsoleData);
|
|
|
|
get_console_data(event, to, &src_len, avail);
|
|
|
|
|
|
|
|
acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len);
|
|
|
|
acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
|
|
|
|
acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED;
|
|
|
|
*slen = avail - src_len;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* triggered by SCLP's write_event_data
|
2012-11-02 11:35:54 +04:00
|
|
|
* - write console data to character layer
|
|
|
|
* returns < 0 if an error occurred
|
2012-10-29 06:13:25 +04:00
|
|
|
*/
|
|
|
|
static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
2016-03-29 10:47:25 +03:00
|
|
|
SCLPConsole *scon = SCLP_CONSOLE(event);
|
2012-10-29 06:13:25 +04:00
|
|
|
|
|
|
|
if (!scon->chr) {
|
|
|
|
/* If there's no backend, we can just say we consumed all data. */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-05-28 17:03:55 +04:00
|
|
|
return qemu_chr_fe_write_all(scon->chr, buf, len);
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int length;
|
|
|
|
ssize_t written;
|
|
|
|
ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
|
|
|
|
|
|
|
|
length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader);
|
|
|
|
written = write_console_data(event, (uint8_t *)acd->data, length);
|
|
|
|
|
|
|
|
rc = SCLP_RC_NORMAL_COMPLETION;
|
|
|
|
/* set event buffer accepted flag */
|
|
|
|
evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED;
|
|
|
|
|
|
|
|
/* written will be zero if a pty is not connected - don't treat as error */
|
|
|
|
if (written < 0) {
|
|
|
|
/* event buffer not accepted due to error in character layer */
|
|
|
|
evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
|
|
|
|
rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:41:26 +04:00
|
|
|
static const VMStateDescription vmstate_sclpconsole = {
|
|
|
|
.name = "sclpconsole",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2014-04-16 18:01:33 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2012-09-11 15:41:26 +04:00
|
|
|
VMSTATE_BOOL(event.event_pending, SCLPConsole),
|
|
|
|
VMSTATE_UINT8_ARRAY(iov, SCLPConsole, SIZE_BUFFER_VT220),
|
|
|
|
VMSTATE_UINT32(iov_sclp, SCLPConsole),
|
|
|
|
VMSTATE_UINT32(iov_bs, SCLPConsole),
|
|
|
|
VMSTATE_UINT32(iov_data_len, SCLPConsole),
|
|
|
|
VMSTATE_UINT32(iov_sclp_rest, SCLPConsole),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-29 06:13:25 +04:00
|
|
|
/* qemu object creation and initialization functions */
|
|
|
|
|
|
|
|
/* tell character layer our call-back functions */
|
2012-09-11 15:41:26 +04:00
|
|
|
|
2012-10-29 06:13:25 +04:00
|
|
|
static int console_init(SCLPEvent *event)
|
|
|
|
{
|
|
|
|
static bool console_available;
|
|
|
|
|
2016-03-29 10:47:25 +03:00
|
|
|
SCLPConsole *scon = SCLP_CONSOLE(event);
|
2012-10-29 06:13:25 +04:00
|
|
|
|
|
|
|
if (console_available) {
|
|
|
|
error_report("Multiple VT220 operator consoles are not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
console_available = true;
|
|
|
|
if (scon->chr) {
|
|
|
|
qemu_chr_add_handlers(scon->chr, chr_can_read,
|
2013-08-28 18:30:28 +04:00
|
|
|
chr_read, NULL, scon);
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-04 14:55:45 +04:00
|
|
|
static void console_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
SCLPEvent *event = SCLP_EVENT(dev);
|
2016-03-29 10:47:25 +03:00
|
|
|
SCLPConsole *scon = SCLP_CONSOLE(event);
|
2013-09-04 14:55:45 +04:00
|
|
|
|
|
|
|
event->event_pending = false;
|
|
|
|
scon->iov_sclp = 0;
|
|
|
|
scon->iov_bs = 0;
|
|
|
|
scon->iov_data_len = 0;
|
|
|
|
scon->iov_sclp_rest = 0;
|
2014-10-24 17:10:49 +04:00
|
|
|
scon->notify = false;
|
2013-09-04 14:55:45 +04:00
|
|
|
}
|
|
|
|
|
2012-10-29 06:13:25 +04:00
|
|
|
static int console_exit(SCLPEvent *event)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property console_properties[] = {
|
|
|
|
DEFINE_PROP_CHR("chardev", SCLPConsole, chr),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void console_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
|
|
|
|
|
|
|
|
dc->props = console_properties;
|
2013-09-04 14:55:45 +04:00
|
|
|
dc->reset = console_reset;
|
2012-09-11 15:41:26 +04:00
|
|
|
dc->vmsd = &vmstate_sclpconsole;
|
2012-10-29 06:13:25 +04:00
|
|
|
ec->init = console_init;
|
|
|
|
ec->exit = console_exit;
|
|
|
|
ec->get_send_mask = send_mask;
|
|
|
|
ec->get_receive_mask = receive_mask;
|
2013-09-17 15:07:30 +04:00
|
|
|
ec->can_handle_event = can_handle_event;
|
2012-10-29 06:13:25 +04:00
|
|
|
ec->read_event_data = read_event_data;
|
|
|
|
ec->write_event_data = write_event_data;
|
2015-03-17 15:44:39 +03:00
|
|
|
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
|
2012-10-29 06:13:25 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo sclp_console_info = {
|
2012-10-29 06:13:25 +04:00
|
|
|
.name = "sclpconsole",
|
|
|
|
.parent = TYPE_SCLP_EVENT,
|
|
|
|
.instance_size = sizeof(SCLPConsole),
|
|
|
|
.class_init = console_class_init,
|
|
|
|
.class_size = sizeof(SCLPEventClass),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&sclp_console_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types)
|