gem5 v24.0.0.0
Loading...
Searching...
No Matches
base.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, 2016-2017, 2021 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 <cstdint>
42#include <functional>
43#include <vector>
44
45#include "base/bitunion.hh"
46#include "base/compiler.hh"
47#include "base/types.hh"
49#include "mem/port_proxy.hh"
50#include "sim/serialize.hh"
51#include "sim/sim_object.hh"
52
53namespace gem5
54{
55
56struct VirtIODeviceBaseParams;
57struct VirtIODummyDeviceParams;
58
59class VirtQueue;
60
74template <typename T>
75inline std::enable_if_t<std::is_same_v<T, vring_used_elem>, T>
77{
78 v.id = swap_byte(v.id);
79 v.len = swap_byte(v.len);
80 return v;
81}
82
83template <typename T>
84inline std::enable_if_t<std::is_same_v<T, vring_desc>, T>
86{
87 v.addr = swap_byte(v.addr);
88 v.len = swap_byte(v.len);
89 v.flags = swap_byte(v.flags);
90 v.next = swap_byte(v.next);
91 return v;
92}
93
118{
119 public:
121 typedef uint16_t Index;
122
135 // WORKAROUND: The noexcept declaration works around a bug where
136 // gcc 4.7 tries to call the wrong constructor when emplacing
137 // something into a vector.
138 VirtDescriptor(VirtDescriptor &&other) noexcept;
139 ~VirtDescriptor() noexcept;
140
141 VirtDescriptor &operator=(VirtDescriptor &&rhs) noexcept;
142
144 Index index() const { return _index; }
145
147 void update();
148
150 void updateChain();
159 void dump() const;
164 void dumpChain() const;
185 void read(size_t offset, uint8_t *dst, size_t size) const;
200 void write(size_t offset, const uint8_t *src, size_t size);
213 size_t size() const { return desc.len; }
214
220 bool hasNext() const { return desc.flags & VRING_DESC_F_NEXT; }
227 VirtDescriptor *next() const;
228
230 bool isIncoming() const { return !isOutgoing(); }
232 bool isOutgoing() const { return desc.flags & VRING_DESC_F_WRITE; }
233
234
247 void chainRead(size_t offset, uint8_t *dst, size_t size) const;
260 void chainWrite(size_t offset, const uint8_t *src, size_t size);
269 size_t chainSize() const;
272 private:
273 // Remove default constructor
275 // Prevent copying
277
282
284 ByteOrder byteOrder;
285
288
291};
292
303{
304 public:
305 virtual ~VirtQueue() {};
306
310 void serialize(CheckpointOut &cp) const override;
311 void unserialize(CheckpointIn &cp) override;
312
322 void reset();
323
329 void setAddress(Addr address);
335 Addr getAddress() const { return _address; }
336
342 uint16_t getSize() const { return _size; }
343
386 void produceDescriptor(VirtDescriptor *desc, uint32_t len);
404 virtual void onNotify();
415 virtual void onNotifyDescriptor(VirtDescriptor *desc) {};
422 void dump() const;
430 static const Addr ALIGN_BITS = 12;
431 static const Addr ALIGN_SIZE = 1 << ALIGN_BITS;
434 protected:
444 VirtQueue(PortProxy &proxy, ByteOrder bo, uint16_t size);
445
447 ByteOrder byteOrder;
448
449 private:
451
453 const uint16_t _size;
458
459 private:
467 template<typename T>
469 {
470 public:
471 typedef uint16_t Flags;
472 typedef uint16_t Index;
473
475 {
478 };
479
480 VirtRing<T>(PortProxy &proxy, ByteOrder bo, uint16_t size) :
481 header{0, 0}, ring(size), _proxy(proxy), _base(0), byteOrder(bo)
482 {}
483
485 void
487 {
488 header = {0, 0};
489 _base = 0;
490 };
491
498
500 void
502 {
503 assert(_base != 0);
504 _proxy.readBlob(_base, &header, sizeof(header));
507 }
508
509 void
511 {
512 Header out;
513 assert(_base != 0);
516 _proxy.writeBlob(_base, &out, sizeof(out));
517 }
518
519 void
521 {
522 readHeader();
523
524 /* Read and byte-swap the elements in the ring */
525 T temp[ring.size()];
526 _proxy.readBlob(_base + sizeof(header),
527 temp, sizeof(T) * ring.size());
528 for (int i = 0; i < ring.size(); ++i)
529 ring[i] = gtoh(temp[i], byteOrder);
530 }
531
532 void
534 {
535 assert(_base != 0);
536 /* Create a byte-swapped copy of the ring and write it to
537 * guest memory. */
538 T temp[ring.size()];
539 for (int i = 0; i < ring.size(); ++i)
540 temp[i] = htog(ring[i], byteOrder);
541 _proxy.writeBlob(_base + sizeof(header),
542 temp, sizeof(T) * ring.size());
543 writeHeader();
544 }
545
550
551 private:
552 // Remove default constructor
553 VirtRing<T>();
554
560 ByteOrder byteOrder;
561 };
562
567
570 uint16_t _last_avail;
571
575};
576
588{
589 public:
590 typedef uint16_t QueueID;
591 typedef uint32_t FeatureBits;
597 typedef uint16_t VirtAddress;
599 typedef uint16_t DeviceId;
600
601 BitUnion8(DeviceStatus)
602 Bitfield<7> failed;
603 Bitfield<2> driver_ok;
604 Bitfield<1> driver;
605 Bitfield<0> acknowledge;
606 EndBitUnion(DeviceStatus)
607
608 typedef VirtIODeviceBaseParams Params;
609 VirtIODeviceBase(const Params &params, DeviceId id, size_t config_size,
610 FeatureBits features);
611 virtual ~VirtIODeviceBase();
612
613 public:
617 void serialize(CheckpointOut &cp) const override;
618 void unserialize(CheckpointIn &cp) override;
622 protected:
637 void
639 {
640 assert(transKick);
641 transKick();
642 };
643
655 void registerQueue(VirtQueue &queue);
656
657
671 public:
689 virtual void readConfig(PacketPtr pkt, Addr cfgOffset);
704 virtual void writeConfig(PacketPtr pkt, Addr cfgOffset);
705
718 virtual void reset();
721 protected:
733 void readConfigBlob(PacketPtr pkt, Addr cfgOffset, const uint8_t *cfg);
734
742 void writeConfigBlob(PacketPtr pkt, Addr cfgOffset, uint8_t *cfg);
743
747 ByteOrder byteOrder;
748
751 public:
761 void
762 registerKickCallback(const std::function<void()> &callback)
763 {
764 assert(!transKick);
765 transKick = callback;
766 }
767
768
778 void onNotify(QueueID index);
779
780
801
816 void setQueueAddress(uint32_t address);
830 uint32_t getQueueAddress() const;
831
838 uint16_t getQueueSize() const { return getCurrentQueue().getSize(); }
839
848 void setDeviceStatus(DeviceStatus status);
849
855 DeviceStatus getDeviceStatus() const { return _deviceStatus; }
856
863 void setGuestFeatures(FeatureBits features);
864
871
874
876 const size_t configSize;
877
882 private:
884 const VirtQueue &getCurrentQueue() const;
887
894 DeviceStatus _deviceStatus;
895
898
901
903 std::function<void()> transKick;
904};
905
907{
908 public:
909 VirtIODummyDevice(const VirtIODummyDeviceParams &params);
910
911 protected:
913 static const DeviceId ID_INVALID = 0x00;
914};
915
916} // namespace gem5
917
918#endif // __DEV_VIRTIO_BASE_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition port_proxy.hh:87
void readBlob(Addr addr, void *p, uint64_t size) const
Higher level interfaces based on the above.
void writeBlob(Addr addr, const void *p, uint64_t size) const
Same as tryWriteBlob, but insists on success.
Basic support for object serialization.
Definition serialize.hh:170
Abstract superclass for simulation objects.
SimObjectParams Params
VirtIO descriptor (chain) wrapper.
Definition base.hh:118
bool isOutgoing() const
Check if this is a write-only descriptor (outgoing data).
Definition base.hh:232
void update()
Populate this descriptor with data from the guest.
Definition base.cc:78
~VirtDescriptor() noexcept
Definition base.cc:61
bool hasNext() const
Is this descriptor chained to another descriptor?
Definition base.hh:220
PortProxy * memProxy
Pointer to memory proxy.
Definition base.hh:279
size_t size() const
Retrieve the size of this descriptor.
Definition base.hh:213
void dumpChain() const
Dump the contents of a descriptor chain starting at this descriptor.
Definition base.cc:126
ByteOrder byteOrder
The byte order the descriptor is stored in.
Definition base.hh:284
Index _index
Index in virtqueue.
Definition base.hh:287
VirtDescriptor(const VirtDescriptor &other)
void chainRead(size_t offset, uint8_t *dst, size_t size) const
Read the contents of a descriptor chain.
Definition base.cc:172
void dump() const
Dump the contents of a descriptor.
Definition base.cc:109
vring_desc desc
Underlying descriptor.
Definition base.hh:290
void read(size_t offset, uint8_t *dst, size_t size) const
Read the contents of a descriptor.
Definition base.cc:148
uint16_t Index
Descriptor index in virtqueue.
Definition base.hh:121
VirtDescriptor * next() const
Get the pointer to the next descriptor in a chain.
Definition base.cc:138
Index index() const
Get the descriptor's index into the virtqueue.
Definition base.hh:144
void chainWrite(size_t offset, const uint8_t *src, size_t size)
Write to a descriptor chain.
Definition base.cc:195
void write(size_t offset, const uint8_t *src, size_t size)
Write to the contents of a descriptor.
Definition base.cc:160
size_t chainSize() const
Retrieve the size of this descriptor chain.
Definition base.cc:218
VirtQueue * queue
Pointer to virtqueue owning this descriptor.
Definition base.hh:281
bool isIncoming() const
Check if this is a read-only descriptor (incoming data).
Definition base.hh:230
void updateChain()
Populate this descriptor chain with data from the guest.
Definition base.cc:97
Base class for all VirtIO-based devices.
Definition base.hh:588
Bitfield< 2 > driver_ok
Definition base.hh:603
void setDeviceStatus(DeviceStatus status)
Update device status and optionally reset device.
Definition base.cc:412
void kick()
Inform the guest of available buffers.
Definition base.hh:638
ByteOrder byteOrder
The byte order of the queues, descriptors, etc.
Definition base.hh:747
void readConfigBlob(PacketPtr pkt, Addr cfgOffset, const uint8_t *cfg)
Read configuration data from a device structure.
Definition base.cc:434
virtual ~VirtIODeviceBase()
Definition base.cc:350
const VirtQueue & getCurrentQueue() const
Convenience method to get the currently selected queue.
Definition base.cc:459
uint16_t VirtAddress
This is a VirtQueue address as exposed through the low-level interface. The address needs to be multi...
Definition base.hh:597
FeatureBits guestFeatures
Feature set accepted by the guest.
Definition base.hh:668
Bitfield< 0 > acknowledge
Definition base.hh:605
Bitfield< 1 > driver
Definition base.hh:604
const size_t configSize
Size of the device's configuration space.
Definition base.hh:876
VirtIODeviceBase(const Params &params, DeviceId id, size_t config_size, FeatureBits features)
Definition base.cc:339
virtual void writeConfig(PacketPtr pkt, Addr cfgOffset)
Write to the configuration space of a device.
Definition base.cc:428
FeatureBits getGuestFeatures() const
Get features accepted by the guest driver.
Definition base.hh:870
QueueID getQueueSelect() const
Get the currently active queue.
Definition base.hh:800
DeviceStatus getDeviceStatus() const
Retrieve the device status.
Definition base.hh:855
uint32_t getQueueAddress() const
Get the host physical address of the currently active queue.
Definition base.cc:483
void writeConfigBlob(PacketPtr pkt, Addr cfgOffset, uint8_t *cfg)
Write configuration data to a device structure.
Definition base.cc:446
QueueID _queueSelect
Queue select register (set by guest)
Definition base.hh:897
const FeatureBits deviceFeatures
Feature set offered by the device.
Definition base.hh:879
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition base.cc:365
std::vector< VirtQueue * > _queues
List of virtual queues supported by this device.
Definition base.hh:900
virtual void reset()
Driver-request device reset.
Definition base.cc:375
std::function< void()> transKick
Callbacks to kick the guest through the transport layer
Definition base.hh:903
void onNotify(QueueID index)
Driver is requesting service.
Definition base.cc:386
void setQueueSelect(QueueID idx)
Change currently active queue.
Definition base.hh:790
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition base.cc:355
uint16_t getQueueSize() const
Get the size (descriptors) of the currently active queue.
Definition base.hh:838
BitUnion8(DeviceStatus) Bitfield< 7 > failed
uint32_t FeatureBits
Definition base.hh:591
void registerKickCallback(const std::function< void()> &callback)
Register a callback to kick the guest through the transport interface.
Definition base.hh:762
virtual void readConfig(PacketPtr pkt, Addr cfgOffset)
Read from the configuration space of a device.
Definition base.cc:422
uint16_t DeviceId
Device Type (sometimes known as subsystem ID)
Definition base.hh:599
void setQueueAddress(uint32_t address)
Change the host physical address of the currently active queue.
Definition base.cc:477
EndBitUnion(DeviceStatus) typedef VirtIODeviceBaseParams Params
const DeviceId deviceId
Device ID (sometimes known as subsystem ID)
Definition base.hh:873
void setGuestFeatures(FeatureBits features)
Set feature bits accepted by the guest driver.
Definition base.cc:398
void registerQueue(VirtQueue &queue)
Register a new VirtQueue with the device model.
Definition base.cc:491
DeviceStatus _deviceStatus
Status of the device.
Definition base.hh:894
static const DeviceId ID_INVALID
VirtIO device ID.
Definition base.hh:913
VirtIODummyDevice(const VirtIODummyDeviceParams &params)
Definition base.cc:497
VirtIO ring buffer wrapper.
Definition base.hh:469
void reset()
Reset any state in the ring buffer.
Definition base.hh:486
Addr _base
Guest physical base address of the ring buffer.
Definition base.hh:558
PortProxy & _proxy
Guest physical memory proxy.
Definition base.hh:556
ByteOrder byteOrder
Byte order in the ring.
Definition base.hh:560
void readHeader()
Update the ring buffer header with data from the guest.
Definition base.hh:501
Header header
Ring buffer header in host byte order.
Definition base.hh:547
std::vector< T > ring
Elements in ring in host byte order.
Definition base.hh:549
void setAddress(Addr addr)
Set the base address of the VirtIO ring buffer.
Definition base.hh:497
Base wrapper around a virtqueue.
Definition base.hh:303
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition base.cc:249
static const Addr ALIGN_BITS
Page size used by VirtIO. It's hard-coded to 4096 bytes in the spec for historical reasons.
Definition base.hh:430
virtual ~VirtQueue()
Definition base.hh:305
void setAddress(Addr address)
Set the base address of this queue.
Definition base.cc:272
void produceDescriptor(VirtDescriptor *desc, uint32_t len)
Send a descriptor chain to the guest.
Definition base.cc:304
Addr getAddress() const
Get the guest physical address of this queue.
Definition base.hh:335
VirtRing< struct vring_used_elem > used
Ring of used (outgoing) descriptors.
Definition base.hh:566
std::vector< VirtDescriptor > descriptors
Vector of pre-created descriptors indexed by their index into the queue.
Definition base.hh:574
static const Addr ALIGN_SIZE
Definition base.hh:431
uint16_t getSize() const
Get the number of descriptors available in this queue.
Definition base.hh:342
VirtRing< VirtDescriptor::Index > avail
Ring of available (incoming) descriptors.
Definition base.hh:564
virtual void onNotify()
Notify queue of pending events.
Definition base.cc:328
uint16_t _last_avail
Offset of last consumed descriptor in the VirtQueue::avail ring.
Definition base.hh:570
void reset()
Reset cached state in this queue and in the associated ring buffers.
Definition base.cc:262
const uint16_t _size
Queue size in terms of number of descriptors.
Definition base.hh:453
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition base.cc:242
PortProxy & memProxy
Guest physical memory proxy.
Definition base.hh:457
Addr _address
Base address of the queue.
Definition base.hh:455
ByteOrder byteOrder
Byte order in this queue.
Definition base.hh:447
VirtDescriptor * consumeDescriptor()
Get an incoming descriptor chain from the queue.
Definition base.cc:285
VirtDescriptor * getDescriptor(VirtDescriptor::Index index)
Get a pointer to a specific descriptor in the queue.
Definition base.hh:353
void dump() const
Dump the contents of a queue.
Definition base.cc:318
virtual void onNotifyDescriptor(VirtDescriptor *desc)
Notify queue of pending incoming descriptor.
Definition base.hh:415
STL vector class.
Definition stl.hh:37
const Params & params() const
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 18, 16 > len
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 23, 0 > offset
Definition types.hh:144
Bitfield< 5, 0 > status
Bitfield< 30, 0 > index
Bitfield< 25, 21 > bo
Definition types.hh:82
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::enable_if_t< std::is_same_v< T, vring_used_elem >, T > swap_byte(T v)
Definition base.hh:76
std::ostream CheckpointOut
Definition serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
T gtoh(T value, ByteOrder guest_byte_order)
Definition byteswap.hh:194
T htog(T value, ByteOrder guest_byte_order)
Definition byteswap.hh:187
PortProxy Object Declaration.
PM4 packets.
uint16_t flags
Definition virtio_ring.h:69
uint32_t len
Definition virtio_ring.h:67
#define VRING_DESC_F_WRITE
Definition virtio_ring.h:39
#define VRING_DESC_F_NEXT
Definition virtio_ring.h:37

Generated on Tue Jun 18 2024 16:24:02 for gem5 by doxygen 1.11.0