changeset a26a20060ba3 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=a26a20060ba3
description:
        dev, pci: Implement basic VirtIO support

        This patch adds support for VirtIO over the PCI bus. It does so by
        providing the following new SimObjects:

         * VirtIODeviceBase - Abstract base class for VirtIO devices.
         * PciVirtIO - VirtIO PCI transport interface.

        A VirtIO device is hooked up to the guest system by adding a PciVirtIO
        device to the PCI bus and connecting it to a VirtIO device using the
        vio parameter.

        New VirtIO devices should inherit from VirtIODevice base and
        implementing one or more VirtQueues. The VirtQueues are usually
        device-specific and all derive from the VirtQueue class. Queues must
        be registered with the base class from the constructor since the
        device assumes that the number of queues stay constant.

diffstat:

 src/dev/virtio/SConscript    |   51 ++
 src/dev/virtio/VirtIO.py     |   71 +++
 src/dev/virtio/base.cc       |  481 +++++++++++++++++++++++
 src/dev/virtio/base.hh       |  881 +++++++++++++++++++++++++++++++++++++++++++
 src/dev/virtio/pci.cc        |  222 ++++++++++
 src/dev/virtio/pci.hh        |   89 ++++
 src/dev/virtio/virtio_ring.h |  163 +++++++
 7 files changed, 1958 insertions(+), 0 deletions(-)

diffs (truncated from 1986 to 300 lines):

diff -r 8a7724f13288 -r a26a20060ba3 src/dev/virtio/SConscript
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dev/virtio/SConscript Sat Sep 20 17:17:51 2014 -0400
@@ -0,0 +1,51 @@
+# -*- mode:python -*-
+
+# Copyright (c) 2014 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Andreas Sandberg
+
+Import('*')
+
+if env['TARGET_ISA'] == 'null':
+    Return()
+
+SimObject('VirtIO.py')
+
+Source('base.cc')
+Source('pci.cc')
+
+DebugFlag('VIO', 'VirtIO base functionality')
+DebugFlag('VIOPci', 'VirtIO PCI transport')
diff -r 8a7724f13288 -r a26a20060ba3 src/dev/virtio/VirtIO.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dev/virtio/VirtIO.py  Sat Sep 20 17:17:51 2014 -0400
@@ -0,0 +1,71 @@
+# -*- mode:python -*-
+
+# Copyright (c) 2014 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Andreas Sandberg
+
+from m5.SimObject import SimObject
+from m5.params import *
+from m5.proxy import *
+from Device import PioDevice
+from Pci import PciDevice
+
+
+class VirtIODeviceBase(SimObject):
+    type = 'VirtIODeviceBase'
+    cxx_header = 'dev/virtio/base.hh'
+    abstract = True
+
+    subsystem = Param.UInt8(0x00, "VirtIO subsystem ID")
+
+    system = Param.System(Parent.any, "system object")
+
+class PciVirtIO(PciDevice):
+    type = 'PciVirtIO'
+    cxx_header = 'dev/virtio/pci.hh'
+
+    vio = Param.VirtIODeviceBase("VirtIO device")
+
+    VendorID = 0x1AF4
+    SubsystemVendorID = VendorID;
+    DeviceID = 0x1000
+
+    ClassCode = 0xff # Misc device
+
+    BAR0 = 0x00000000 # Anywhere in 32-bit space
+    BAR0Size = '0B' # Overridden by the device model
+
+    InterruptPin = 0x01 # Use #INTA
diff -r 8a7724f13288 -r a26a20060ba3 src/dev/virtio/base.cc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dev/virtio/base.cc    Sat Sep 20 17:17:51 2014 -0400
@@ -0,0 +1,481 @@
+/*
+ * Copyright (c) 2014 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andreas Sandberg
+ */
+
+#include "debug/VIO.hh"
+#include "dev/virtio/base.hh"
+#include "params/VirtIODeviceBase.hh"
+
+VirtDescriptor::VirtDescriptor(PortProxy &_memProxy, VirtQueue &_queue,
+                               Index descIndex)
+    : memProxy(&_memProxy), queue(&_queue), _index(descIndex)
+{
+}
+
+VirtDescriptor::VirtDescriptor(VirtDescriptor &&other) noexcept
+{
+    *this = std::forward<VirtDescriptor>(other);
+}
+
+VirtDescriptor::~VirtDescriptor() noexcept
+{
+}
+
+VirtDescriptor &
+VirtDescriptor::operator=(VirtDescriptor &&rhs) noexcept
+{
+    memProxy = std::move(rhs.memProxy);
+    queue = std::move(rhs.queue);
+    _index = std::move(rhs._index);
+    desc = std::move(rhs.desc);
+
+    return *this;
+}
+
+void
+VirtDescriptor::update()
+{
+    const Addr vq_addr(queue->getAddress());
+    // Check if the queue has been initialized yet
+    if (vq_addr == 0)
+        return;
+
+    assert(_index < queue->getSize());
+    const Addr desc_addr(vq_addr + sizeof(desc) * _index);
+    vring_desc guest_desc;
+    memProxy->readBlob(desc_addr, (uint8_t *)&guest_desc, sizeof(guest_desc));
+    desc = vtoh_legacy(guest_desc);
+    DPRINTF(VIO,
+            "VirtDescriptor(%i): Addr: 0x%x, Len: %i, Flags: 0x%x, "
+            "Next: 0x%x\n",
+            _index, desc.addr, desc.len, desc.flags, desc.next);
+}
+
+void
+VirtDescriptor::updateChain()
+{
+    VirtDescriptor *desc(this);
+    do {
+        desc->update();
+    } while((desc = desc->next()) != NULL && desc != this);
+
+    if (desc == this)
+        panic("Loop in descriptor chain!\n");
+}
+
+void
+VirtDescriptor::dump() const
+{
+    if (!DTRACE(VIO))
+        return;
+
+    DPRINTF(VIO, "Descriptor[%i]: "
+            "Addr: 0x%x, Len: %i, Flags: 0x%x, Next: 0x%x\n",
+            _index, desc.addr, desc.len, desc.flags, desc.next);
+
+    if (isIncoming()) {
+        uint8_t data[desc.len];
+        read(0, data, desc.len);
+        DDUMP(VIO, data, desc.len);
+    }
+}
+
+void
+VirtDescriptor::dumpChain() const
+{
+    if (!DTRACE(VIO))
+        return;
+
+    const VirtDescriptor *desc(this);
+    do {
+        desc->dump();
+    } while((desc = desc->next()) != NULL);
+}
+
+VirtDescriptor *
+VirtDescriptor::next() const
+{
+    if (hasNext()) {
+        return queue->getDescriptor(desc.next);
+    } else {
+        return NULL;
+    }
+}
+
+void
+VirtDescriptor::read(size_t offset, uint8_t *dst, size_t size) const
+{
+    DPRINTF(VIO, "VirtDescriptor(%p, 0x%x, %i)::read: offset: %i, dst: 0x%x, 
size: %i\n",
+            this, desc.addr, desc.len, offset, (long)dst, size);
+    assert(size <= desc.len - offset);
+    if (!isIncoming())
+        panic("Trying to read from outgoing buffer\n");
+
+    memProxy->readBlob(desc.addr + offset, dst, size);
+}
+
+void
+VirtDescriptor::write(size_t offset, const uint8_t *src, size_t size)
+{
+    DPRINTF(VIO, "VirtDescriptor(%p, 0x%x, %i)::write: offset: %i, src: 0x%x, 
size: %i\n",
+            this, desc.addr, desc.len, offset, (long)src, size);
+    assert(size <= desc.len - offset);
+    if (!isOutgoing())
+        panic("Trying to write to incoming buffer\n");
+
+    memProxy->writeBlob(desc.addr + offset, const_cast<uint8_t *>(src), size);
+}
+
+void
+VirtDescriptor::chainRead(size_t offset, uint8_t *dst, size_t size) const
+{
_______________________________________________
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to