From 51d1d6fb126b82409548d006e6826e942593af8b Mon Sep 17 00:00:00 2001 From: Jie Deng Date: Tue, 22 Sep 2020 13:33:11 +0800 Subject: [PATCH] virtio: add virtio callbacks check We can only call these callbacks when they are not NULL. Tracked-On: #5342 Signed-off-by: Jie Deng Acked-by: Wang, Yu1 --- devicemodel/hw/pci/virtio/virtio.c | 33 +++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/devicemodel/hw/pci/virtio/virtio.c b/devicemodel/hw/pci/virtio/virtio.c index 6a97928e8..018b9c9e4 100644 --- a/devicemodel/hw/pci/virtio/virtio.c +++ b/devicemodel/hw/pci/virtio/virtio.c @@ -763,7 +763,7 @@ virtio_pci_legacy_read(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, const char *name; uint32_t newoff; uint32_t value; - int error; + int error = -1; if (base->mtx) @@ -791,8 +791,11 @@ virtio_pci_legacy_read(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, max = vops->cfgsize ? vops->cfgsize : 0x100000000; if (newoff + size > max) goto bad; - error = (*vops->cfgread)(DEV_STRUCT(base), newoff, - size, &value); + + if (vops->cfgread) { + error = (*vops->cfgread)(DEV_STRUCT(base), newoff, + size, &value); + } if (!error) goto done; } @@ -873,7 +876,7 @@ virtio_pci_legacy_write(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, uint64_t virtio_config_size, max; const char *name; uint32_t newoff; - int error; + int error = -1; if (base->mtx) @@ -899,8 +902,10 @@ virtio_pci_legacy_write(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, max = vops->cfgsize ? vops->cfgsize : 0x100000000; if (newoff + size > max) goto bad; - error = (*vops->cfgwrite)(DEV_STRUCT(base), newoff, - size, value); + if (vops->cfgwrite) { + error = (*vops->cfgwrite)(DEV_STRUCT(base), newoff, + size, value); + } if (!error) goto done; } @@ -962,7 +967,7 @@ bad: base->status = value; if (vops->set_status) (*vops->set_status)(DEV_STRUCT(base), value); - if (value == 0) + if ((value == 0) && (vops->reset)) (*vops->reset)(DEV_STRUCT(base)); if ((value & VIRTIO_CONFIG_S_DRIVER_OK) && base->backend_type == BACKEND_VBSU && @@ -1390,7 +1395,7 @@ virtio_common_cfg_write(struct pci_vdev *dev, uint64_t offset, int size, base->status = value & 0xff; if (vops->set_status) (*vops->set_status)(DEV_STRUCT(base), value); - if (base->status == 0) + if ((base->status == 0) && (vops->reset)) (*vops->reset)(DEV_STRUCT(base)); /* TODO: virtio poll mode for modern devices */ break; @@ -1487,7 +1492,7 @@ virtio_device_cfg_read(struct pci_vdev *dev, uint64_t offset, int size) const char *name; uint32_t value; uint64_t max; - int error; + int error = -1; vops = base->vops; name = vops->name; @@ -1500,7 +1505,9 @@ virtio_device_cfg_read(struct pci_vdev *dev, uint64_t offset, int size) return value; } - error = (*vops->cfgread)(DEV_STRUCT(base), offset, size, &value); + if (vops->cfgread) { + error = (*vops->cfgread)(DEV_STRUCT(base), offset, size, &value); + } if (error) { pr_err("%s: reading from 0x%lx size %d failed %d\r\n", name, offset, size, error); @@ -1518,7 +1525,7 @@ virtio_device_cfg_write(struct pci_vdev *dev, uint64_t offset, int size, struct virtio_ops *vops; const char *name; uint64_t max; - int error; + int error = -1; vops = base->vops; name = vops->name; @@ -1530,7 +1537,9 @@ virtio_device_cfg_write(struct pci_vdev *dev, uint64_t offset, int size, return; } - error = (*vops->cfgwrite)(DEV_STRUCT(base), offset, size, value); + if (vops->cfgwrite) { + error = (*vops->cfgwrite)(DEV_STRUCT(base), offset, size, value); + } if (error) pr_err("%s: writing ot 0x%lx size %d failed %d\r\n", name, offset, size, error);