gem5  v20.0.0.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
base.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2016-2017 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __DEV_VIRTIO_BASE_HH__
39 #define __DEV_VIRTIO_BASE_HH__
40 
41 #include "arch/isa_traits.hh"
42 #include "base/bitunion.hh"
43 #include "base/callback.hh"
44 #include "dev/virtio/virtio_ring.h"
45 #include "mem/port_proxy.hh"
46 #include "sim/sim_object.hh"
47 
48 struct VirtIODeviceBaseParams;
49 struct VirtIODummyDeviceParams;
50 
51 class VirtQueue;
52 
67 template <> inline vring_used_elem
69  v.id = swap_byte(v.id);
70  v.len = swap_byte(v.len);
71  return v;
72 }
73 
74 template <> inline vring_desc
76  v.addr = swap_byte(v.addr);
77  v.len = swap_byte(v.len);
78  v.flags = swap_byte(v.flags);
79  v.next = swap_byte(v.next);
80  return v;
81 }
82 
107 {
108  public:
110  typedef uint16_t Index;
111 
123  VirtQueue &queue, Index index);
124  // WORKAROUND: The noexcept declaration works around a bug where
125  // gcc 4.7 tries to call the wrong constructor when emplacing
126  // something into a vector.
127  VirtDescriptor(VirtDescriptor &&other) noexcept;
128  ~VirtDescriptor() noexcept;
129 
130  VirtDescriptor &operator=(VirtDescriptor &&rhs) noexcept;
131 
133  Index index() const { return _index; }
134 
136  void update();
137 
139  void updateChain();
148  void dump() const;
153  void dumpChain() const;
174  void read(size_t offset, uint8_t *dst, size_t size) const;
189  void write(size_t offset, const uint8_t *src, size_t size);
202  size_t size() const { return desc.len; }
203 
209  bool hasNext() const { return desc.flags & VRING_DESC_F_NEXT; }
216  VirtDescriptor *next() const;
217 
219  bool isIncoming() const { return !isOutgoing(); }
221  bool isOutgoing() const { return desc.flags & VRING_DESC_F_WRITE; }
222 
223 
236  void chainRead(size_t offset, uint8_t *dst, size_t size) const;
249  void chainWrite(size_t offset, const uint8_t *src, size_t size);
258  size_t chainSize() const;
261  private:
262  // Remove default constructor
263  VirtDescriptor();
264  // Prevent copying
265  VirtDescriptor(const VirtDescriptor &other);
266 
271 
274 
276  Index _index;
277 
280 };
281 
291 class VirtQueue : public Serializable {
292 public:
293  virtual ~VirtQueue() {};
294 
298  void serialize(CheckpointOut &cp) const override;
299  void unserialize(CheckpointIn &cp) override;
300 
309  void setAddress(Addr address);
315  Addr getAddress() const { return _address; }
316 
322  uint16_t getSize() const { return _size; }
323 
334  return &descriptors[index];
335  }
347  VirtDescriptor *consumeDescriptor();
366  void produceDescriptor(VirtDescriptor *desc, uint32_t len);
384  virtual void onNotify();
395  virtual void onNotifyDescriptor(VirtDescriptor *desc) {};
402  void dump() const;
410  static const Addr ALIGN_BITS = 12;
411  static const Addr ALIGN_SIZE = 1 << ALIGN_BITS;
414  protected:
424  VirtQueue(PortProxy &proxy, ByteOrder bo, uint16_t size);
425 
428 
429  private:
430  VirtQueue();
431 
433  const uint16_t _size;
438 
439  private:
447  template<typename T>
448  class VirtRing
449  {
450  public:
451  typedef uint16_t Flags;
452  typedef uint16_t Index;
453 
454  struct Header {
455  Flags flags;
456  Index index;
457  } M5_ATTR_PACKED;
458 
459  VirtRing<T>(PortProxy &proxy, ByteOrder bo, uint16_t size) :
460  header{0, 0}, ring(size), _proxy(proxy), _base(0), byteOrder(bo)
461  {}
462 
468  void setAddress(Addr addr) { _base = addr; }
469 
471  void
473  {
474  assert(_base != 0);
475  _proxy.readBlob(_base, &header, sizeof(header));
476  header.flags = gtoh(header.flags, byteOrder);
477  header.index = gtoh(header.index, byteOrder);
478  }
479 
480  void
482  {
483  Header out;
484  assert(_base != 0);
485  out.flags = htog(header.flags, byteOrder);
486  out.index = htog(header.index, byteOrder);
487  _proxy.writeBlob(_base, &out, sizeof(out));
488  }
489 
490  void
492  {
493  readHeader();
494 
495  /* Read and byte-swap the elements in the ring */
496  T temp[ring.size()];
497  _proxy.readBlob(_base + sizeof(header),
498  temp, sizeof(T) * ring.size());
499  for (int i = 0; i < ring.size(); ++i)
500  ring[i] = gtoh(temp[i], byteOrder);
501  }
502 
503  void
505  {
506  assert(_base != 0);
507  /* Create a byte-swapped copy of the ring and write it to
508  * guest memory. */
509  T temp[ring.size()];
510  for (int i = 0; i < ring.size(); ++i)
511  temp[i] = htog(ring[i], byteOrder);
512  _proxy.writeBlob(_base + sizeof(header),
513  temp, sizeof(T) * ring.size());
514  writeHeader();
515  }
516 
518  Header header;
521 
522  private:
523  // Remove default constructor
524  VirtRing<T>();
525 
532  };
533 
538 
541  uint16_t _last_avail;
542 
546 };
547 
559 {
560  public:
561  typedef uint16_t QueueID;
562  typedef uint32_t FeatureBits;
568  typedef uint16_t VirtAddress;
570  typedef uint16_t DeviceId;
571 
572  BitUnion8(DeviceStatus)
573  Bitfield<7> failed;
574  Bitfield<2> driver_ok;
575  Bitfield<1> driver;
576  Bitfield<0> acknowledge;
577  EndBitUnion(DeviceStatus)
578 
579  typedef VirtIODeviceBaseParams Params;
580  VirtIODeviceBase(Params *params, DeviceId id, size_t config_size,
581  FeatureBits features);
582  virtual ~VirtIODeviceBase();
583 
584  public:
588  void serialize(CheckpointOut &cp) const override;
589  void unserialize(CheckpointIn &cp) override;
593  protected:
608  void kick() {
609  assert(transKick);
610  transKick->process();
611  };
612 
624  void registerQueue(VirtQueue &queue);
625 
626 
637  FeatureBits guestFeatures;
640  public:
658  virtual void readConfig(PacketPtr pkt, Addr cfgOffset);
673  virtual void writeConfig(PacketPtr pkt, Addr cfgOffset);
674 
687  virtual void reset();
690  protected:
702  void readConfigBlob(PacketPtr pkt, Addr cfgOffset, const uint8_t *cfg);
703 
711  void writeConfigBlob(PacketPtr pkt, Addr cfgOffset, uint8_t *cfg);
712 
717 
720  public:
731  assert(!transKick);
732  transKick = c;
733  }
734 
735 
745  void onNotify(QueueID index);
746 
747 
757  void setQueueSelect(QueueID idx) { _queueSelect = idx; }
767  QueueID getQueueSelect() const { return _queueSelect; }
768 
783  void setQueueAddress(uint32_t address);
797  uint32_t getQueueAddress() const;
798 
805  uint16_t getQueueSize() const { return getCurrentQueue().getSize(); }
806 
815  void setDeviceStatus(DeviceStatus status);
816 
822  DeviceStatus getDeviceStatus() const { return _deviceStatus; }
823 
830  void setGuestFeatures(FeatureBits features);
831 
837  FeatureBits getGuestFeatures() const { return guestFeatures; }
838 
840  const DeviceId deviceId;
841 
843  const size_t configSize;
844 
846  const FeatureBits deviceFeatures;
849  private:
851  const VirtQueue &getCurrentQueue() const;
853  VirtQueue &getCurrentQueue();
854 
861  DeviceStatus _deviceStatus;
862 
864  QueueID _queueSelect;
865 
868 
871 };
872 
874 {
875  public:
876  VirtIODummyDevice(VirtIODummyDeviceParams *params);
877 
878  protected:
880  static const DeviceId ID_INVALID = 0x00;
881 };
882 
883 #endif // __DEV_VIRTIO_BASE_HH__
Base class for all VirtIO-based devices.
Definition: base.hh:558
void write(size_t offset, const uint8_t *src, size_t size)
Write to the contents of a descriptor.
Definition: base.cc:156
VirtDescriptor * next() const
Get the pointer to the next descriptor in a chain.
Definition: base.cc:134
output header
Definition: nop.cc:36
uint16_t getSize() const
Get the number of descriptors available in this queue.
Definition: base.hh:322
Bitfield< 28 > v
uint16_t VirtAddress
This is a VirtQueue address as exposed through the low-level interface. The address needs to be multi...
Definition: base.hh:568
Generic callback class.
Definition: callback.hh:39
EndBitUnion(UserDescFlags) struct UserDesc32
Definition: process.cc:152
uint16_t getQueueSize() const
Get the size (descriptors) of the currently active queue.
Definition: base.hh:805
void writeHeader()
Definition: base.hh:481
T gtoh(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:162
Bitfield< 7 > i
~VirtDescriptor() noexcept
Definition: base.cc:57
SimObjectParams Params
Definition: sim_object.hh:113
uint16_t Index
Definition: base.hh:452
void setQueueSelect(QueueID idx)
Change currently active queue.
Definition: base.hh:757
ip6_addr_t addr
Definition: inet.hh:330
Bitfield< 1 > driver
Definition: base.hh:575
#define VRING_DESC_F_WRITE
Definition: virtio_ring.h:39
VirtDescriptor * getDescriptor(VirtDescriptor::Index index)
Get a pointer to a specific descriptor in the queue.
Definition: base.hh:333
void dumpChain() const
Dump the contents of a descriptor chain starting at this descriptor.
Definition: base.cc:122
void reset()
Definition: statistics.cc:569
void setAddress(Addr addr)
Set the base address of the VirtIO ring buffer.
Definition: base.hh:468
Bitfield< 23, 0 > offset
Definition: types.hh:152
Definition: cprintf.cc:40
const DeviceId deviceId
Device ID (sometimes known as subsystem ID)
Definition: base.hh:840
STL vector class.
Definition: stl.hh:37
size_t size() const
Retrieve the size of this descriptor.
Definition: base.hh:202
void readHeader()
Update the ring buffer header with data from the guest.
Definition: base.hh:472
Bitfield< 5, 0 > status
void chainRead(size_t offset, uint8_t *dst, size_t size) const
Read the contents of a descriptor chain.
Definition: base.cc:168
Addr _address
Base address of the queue.
Definition: base.hh:435
ByteOrder byteOrder
The byte order of the queues, descriptors, etc.
Definition: base.hh:716
PortProxy Object Declaration.
Bitfield< 0 > acknowledge
Definition: base.hh:576
ByteOrder byteOrder
The byte order the descriptor is stored in.
Definition: base.hh:273
DeviceStatus _deviceStatus
Status of the device.
Definition: base.hh:861
PortProxy & memProxy
Guest physical memory proxy.
Definition: base.hh:437
T htog(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:155
#define VRING_DESC_F_NEXT
Definition: virtio_ring.h:37
bool isIncoming() const
Check if this is a read-only descriptor (incoming data).
Definition: base.hh:219
void read(size_t offset, uint8_t *dst, size_t size) const
Read the contents of a descriptor.
Definition: base.cc:144
size_t chainSize() const
Retrieve the size of this descriptor chain.
Definition: base.cc:214
QueueID _queueSelect
Queue select register (set by guest)
Definition: base.hh:864
std::vector< VirtDescriptor > descriptors
Vector of pre-created descriptors indexed by their index into the queue.
Definition: base.hh:545
void kick()
Inform the guest of available buffers.
Definition: base.hh:608
uint64_t addr
Definition: virtio_ring.h:64
ByteOrder byteOrder
Byte order in the ring.
Definition: base.hh:531
uint16_t next
Definition: virtio_ring.h:70
ByteOrder
Definition: types.hh:245
void chainWrite(size_t offset, const uint8_t *src, size_t size)
Write to a descriptor chain.
Definition: base.cc:191
std::vector< VirtQueue * > _queues
List of virtual queues supported by this device.
Definition: base.hh:867
Header header
Ring buffer header in host byte order.
Definition: base.hh:518
Bitfield< 18, 16 > len
Bitfield< 2 > driver_ok
Definition: base.hh:574
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
VirtIO descriptor (chain) wrapper.
Definition: base.hh:106
PortProxy & _proxy
Guest physical memory proxy.
Definition: base.hh:527
void dump() const
Dump the contents of a descriptor.
Definition: base.cc:105
Index _index
Index in virtqueue.
Definition: base.hh:276
Callback * transKick
Callbacks to kick the guest through the transport layer.
Definition: base.hh:870
uint32_t len
Definition: virtio_ring.h:84
uint16_t Flags
Definition: base.hh:451
VirtRing< VirtDescriptor::Index > avail
Ring of available (incoming) descriptors.
Definition: base.hh:535
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
Index index() const
Get the descriptor&#39;s index into the virtqueue.
Definition: base.hh:133
DeviceStatus getDeviceStatus() const
Retrieve the device status.
Definition: base.hh:822
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:249
Basic support for object serialization.
Definition: serialize.hh:166
VirtQueue * queue
Pointer to virtqueue owning this descriptor.
Definition: base.hh:270
#define BitUnion8(name)
Definition: bitunion.hh:377
struct FXSave M5_ATTR_PACKED
FeatureBits guestFeatures
Feature set accepted by the guest.
Definition: base.hh:637
const uint16_t _size
Queue size in terms of number of descriptors.
Definition: base.hh:433
void updateChain()
Populate this descriptor chain with data from the guest.
Definition: base.cc:93
This object is a proxy for a port or other object which implements the functional response protocol...
Definition: port_proxy.hh:80
FeatureBits getGuestFeatures() const
Get features accepted by the guest driver.
Definition: base.hh:837
Bitfield< 29 > c
Bitfield< 25, 21 > bo
Definition: types.hh:62
uint16_t DeviceId
Device Type (sometimes known as subsystem ID)
Definition: base.hh:570
const FeatureBits deviceFeatures
Feature set offered by the device.
Definition: base.hh:846
Addr getAddress() const
Get the guest physical address of this queue.
Definition: base.hh:315
std::ostream CheckpointOut
Definition: serialize.hh:63
bool isOutgoing() const
Check if this is a write-only descriptor (outgoing data).
Definition: base.hh:221
uint16_t Index
Descriptor index in virtqueue.
Definition: base.hh:110
vring_used_elem swap_byte(vring_used_elem v)
Definition: base.hh:68
void update()
Populate this descriptor with data from the guest.
Definition: base.cc:74
VirtRing< struct vring_used_elem > used
Ring of used (outgoing) descriptors.
Definition: base.hh:537
VirtIO ring buffer wrapper.
Definition: base.hh:448
uint16_t flags
Definition: virtio_ring.h:68
std::vector< T > ring
Elements in ring in host byte order.
Definition: base.hh:520
ByteOrder byteOrder
Byte order in this queue.
Definition: base.hh:427
void unserialize(ThreadContext &tc, CheckpointIn &cp)
void registerKickCallback(Callback *c)
Register a callback to kick the guest through the transport interface.
Definition: base.hh:730
virtual ~VirtQueue()
Definition: base.hh:293
Base wrapper around a virtqueue.
Definition: base.hh:291
QueueID getQueueSelect() const
Get the currently active queue.
Definition: base.hh:767
bool hasNext() const
Is this descriptor chained to another descriptor?
Definition: base.hh:209
uint16_t _last_avail
Offset of last consumed descriptor in the VirtQueue::avail ring.
Definition: base.hh:541
vring_desc desc
Underlying descriptor.
Definition: base.hh:279
Abstract superclass for simulation objects.
Definition: sim_object.hh:92
uint32_t len
Definition: virtio_ring.h:66
const size_t configSize
Size of the device&#39;s configuration space.
Definition: base.hh:843
Addr _base
Guest physical base address of the ring buffer.
Definition: base.hh:529
uint32_t FeatureBits
Definition: base.hh:562
uint16_t QueueID
Definition: base.hh:561
virtual void onNotifyDescriptor(VirtDescriptor *desc)
Notify queue of pending incoming descriptor.
Definition: base.hh:395
PortProxy * memProxy
Pointer to memory proxy.
Definition: base.hh:268

Generated on Mon Jun 8 2020 15:45:07 for gem5 by doxygen 1.8.13