提交 e0ab7fac 编写于 作者: R Rusty Russell 提交者: Michael S. Tsirkin

virtio-serial-bus: use virtio wrappers to access headers

We also fix max_nr_ports at reset time as the device endianness may have
changed.
Signed-off-by: NRusty Russell <rusty@rustcorp.com.au>
Reviewed-by: NAnthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  fix max_nr_ports at reset time,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: NGreg Kurz <gkurz@linux.vnet.ibm.com>
Reviewed-by: NAlexander Graf <agraf@suse.de>
Reviewed-by: NMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: NMichael S. Tsirkin <mst@redhat.com>
上级 8c085dbe
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "hw/sysbus.h" #include "hw/sysbus.h"
#include "trace.h" #include "trace.h"
#include "hw/virtio/virtio-serial.h" #include "hw/virtio/virtio-serial.h"
#include "hw/virtio/virtio-access.h"
static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id) static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
{ {
...@@ -183,11 +184,12 @@ static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len) ...@@ -183,11 +184,12 @@ static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id, static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
uint16_t event, uint16_t value) uint16_t event, uint16_t value)
{ {
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
struct virtio_console_control cpkt; struct virtio_console_control cpkt;
stl_p(&cpkt.id, port_id); virtio_stl_p(vdev, &cpkt.id, port_id);
stw_p(&cpkt.event, event); virtio_stw_p(vdev, &cpkt.event, event);
stw_p(&cpkt.value, value); virtio_stw_p(vdev, &cpkt.value, value);
trace_virtio_serial_send_control_event(port_id, event, value); trace_virtio_serial_send_control_event(port_id, event, value);
return send_control_msg(vser, &cpkt, sizeof(cpkt)); return send_control_msg(vser, &cpkt, sizeof(cpkt));
...@@ -278,6 +280,7 @@ void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle) ...@@ -278,6 +280,7 @@ void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
/* Guest wants to notify us of some event */ /* Guest wants to notify us of some event */
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
{ {
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
struct VirtIOSerialPort *port; struct VirtIOSerialPort *port;
VirtIOSerialPortClass *vsc; VirtIOSerialPortClass *vsc;
struct virtio_console_control cpkt, *gcpkt; struct virtio_console_control cpkt, *gcpkt;
...@@ -291,8 +294,8 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) ...@@ -291,8 +294,8 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
return; return;
} }
cpkt.event = lduw_p(&gcpkt->event); cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
cpkt.value = lduw_p(&gcpkt->value); cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value); trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
...@@ -312,10 +315,10 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) ...@@ -312,10 +315,10 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
return; return;
} }
port = find_port_by_id(vser, ldl_p(&gcpkt->id)); port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
if (!port) { if (!port) {
error_report("virtio-serial-bus: Unexpected port id %u for device %s", error_report("virtio-serial-bus: Unexpected port id %u for device %s",
ldl_p(&gcpkt->id), vser->bus.qbus.name); virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
return; return;
} }
...@@ -342,9 +345,9 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) ...@@ -342,9 +345,9 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
} }
if (port->name) { if (port->name) {
stl_p(&cpkt.id, port->id); virtio_stl_p(vdev, &cpkt.id, port->id);
stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME); virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
stw_p(&cpkt.value, 1); virtio_stw_p(vdev, &cpkt.value, 1);
buffer_len = sizeof(cpkt) + strlen(port->name) + 1; buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
buffer = g_malloc(buffer_len); buffer = g_malloc(buffer_len);
...@@ -510,6 +513,10 @@ static void vser_reset(VirtIODevice *vdev) ...@@ -510,6 +513,10 @@ static void vser_reset(VirtIODevice *vdev)
vser = VIRTIO_SERIAL(vdev); vser = VIRTIO_SERIAL(vdev);
guest_reset(vser); guest_reset(vser);
/* In case we have switched endianness */
vser->config.max_nr_ports =
virtio_tswap32(vdev, vser->serial.max_virtserial_ports);
} }
static void virtio_serial_save(QEMUFile *f, void *opaque) static void virtio_serial_save(QEMUFile *f, void *opaque)
...@@ -532,7 +539,7 @@ static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f) ...@@ -532,7 +539,7 @@ static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
qemu_put_be32s(f, &s->config.max_nr_ports); qemu_put_be32s(f, &s->config.max_nr_ports);
/* The ports map */ /* The ports map */
max_nr_ports = tswap32(s->config.max_nr_ports); max_nr_ports = virtio_tswap32(vdev, s->config.max_nr_ports);
for (i = 0; i < (max_nr_ports + 31) / 32; i++) { for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
qemu_put_be32s(f, &s->ports_map[i]); qemu_put_be32s(f, &s->ports_map[i]);
} }
...@@ -688,6 +695,12 @@ static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f, ...@@ -688,6 +695,12 @@ static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
qemu_get_be16s(f, (uint16_t *) &tmp); qemu_get_be16s(f, (uint16_t *) &tmp);
qemu_get_be32s(f, &tmp); qemu_get_be32s(f, &tmp);
/* Note: this is the only location where we use tswap32() instead of
* virtio_tswap32() because:
* - virtio_tswap32() only makes sense when the device is fully restored
* - the target endianness that was used to populate s->config is
* necessarly the default one
*/
max_nr_ports = tswap32(s->config.max_nr_ports); max_nr_ports = tswap32(s->config.max_nr_ports);
for (i = 0; i < (max_nr_ports + 31) / 32; i++) { for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
qemu_get_be32s(f, &ports_map); qemu_get_be32s(f, &ports_map);
...@@ -751,9 +764,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) ...@@ -751,9 +764,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
/* This function is only used if a port id is not provided by the user */ /* This function is only used if a port id is not provided by the user */
static uint32_t find_free_port_id(VirtIOSerial *vser) static uint32_t find_free_port_id(VirtIOSerial *vser)
{ {
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
unsigned int i, max_nr_ports; unsigned int i, max_nr_ports;
max_nr_ports = tswap32(vser->config.max_nr_ports); max_nr_ports = virtio_tswap32(vdev, vser->config.max_nr_ports);
for (i = 0; i < (max_nr_ports + 31) / 32; i++) { for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
uint32_t map, bit; uint32_t map, bit;
...@@ -806,6 +820,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp) ...@@ -806,6 +820,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev)); VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
VirtIODevice *vdev = VIRTIO_DEVICE(bus->vser);
int max_nr_ports; int max_nr_ports;
bool plugging_port0; bool plugging_port0;
Error *err = NULL; Error *err = NULL;
...@@ -841,7 +856,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp) ...@@ -841,7 +856,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
} }
} }
max_nr_ports = tswap32(port->vser->config.max_nr_ports); max_nr_ports = virtio_tswap32(vdev, port->vser->config.max_nr_ports);
if (port->id >= max_nr_ports) { if (port->id >= max_nr_ports) {
error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, " error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
"max. allowed: %u", max_nr_ports - 1); "max. allowed: %u", max_nr_ports - 1);
...@@ -863,7 +878,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp) ...@@ -863,7 +878,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
add_port(port->vser, port->id); add_port(port->vser, port->id);
/* Send an update to the guest about this new port added */ /* Send an update to the guest about this new port added */
virtio_notify_config(VIRTIO_DEVICE(port->vser)); virtio_notify_config(vdev);
} }
static void virtser_port_device_unrealize(DeviceState *dev, Error **errp) static void virtser_port_device_unrealize(DeviceState *dev, Error **errp)
...@@ -942,7 +957,8 @@ static void virtio_serial_device_realize(DeviceState *dev, Error **errp) ...@@ -942,7 +957,8 @@ static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output); vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
} }
vser->config.max_nr_ports = tswap32(vser->serial.max_virtserial_ports); vser->config.max_nr_ports =
virtio_tswap32(vdev, vser->serial.max_virtserial_ports);
vser->ports_map = g_malloc0(((vser->serial.max_virtserial_ports + 31) / 32) vser->ports_map = g_malloc0(((vser->serial.max_virtserial_ports + 31) / 32)
* sizeof(vser->ports_map[0])); * sizeof(vser->ports_map[0]));
/* /*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册