L4Re - L4 Runtime Environment
virtqueue
1 // vi:ft=cpp
2 /*
3  * (c) 2014 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction. Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License. This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 
19 #include <l4/re/util/debug>
20 #include <l4/sys/types.h>
21 #include <l4/sys/err.h>
22 #include <l4/cxx/bitfield>
23 #include <l4/cxx/exceptions>
24 #include <cstdint>
25 
26 #pragma once
27 
28 namespace L4virtio {
29 
30 #ifdef __ARM_ARCH_7A__
31 static inline void wmb() { asm volatile ("dmb" : : : "memory"); }
32 static inline void rmb() { asm volatile ("dmb" : : : "memory"); }
33 #elif defined(__ARM_ARCH_8A)
34 static inline void wmb() { asm volatile ("dsb ishst" : : : "memory"); }
35 static inline void rmb() { asm volatile ("dsb ishld" : : : "memory"); }
36 #elif defined(__mips__)
37 static inline void wmb() { asm volatile ("sync" : : : "memory"); }
38 static inline void rmb() { asm volatile ("sync" : : : "memory"); }
39 #elif defined(__amd64__) || defined (__i686__)
40 static inline void wmb() { asm volatile ("sfence" : : : "memory"); }
41 static inline void rmb() { asm volatile ("lfence" : : : "memory"); }
42 #else
43 #warning Missing proper memory write barrier
44 static inline void wmb() { asm volatile ("" : : : "memory"); }
45 static inline void rmb() { asm volatile ("" : : : "memory"); }
46 #endif
47 
48 
55 template< typename T >
56 class Ptr
57 {
58 public:
61 
62  Ptr() = default;
63 
65  Ptr(Invalid_type) : _p(~0ULL) {}
66 
68  explicit Ptr(l4_uint64_t vm_addr) : _p(vm_addr) {}
69 
71  l4_uint64_t get() const { return _p; }
72 
74  bool is_valid() const { return _p != ~0ULL; }
75 
76 private:
77  l4_uint64_t _p;
78 };
79 
80 
87 class Virtqueue
88 {
89 public:
93  class Desc
94  {
95  public:
99  struct Flags
100  {
102  Flags() = default;
103 
105  explicit Flags(l4_uint16_t v) : raw(v) {}
106 
108  CXX_BITFIELD_MEMBER( 0, 0, next, raw);
110  CXX_BITFIELD_MEMBER( 1, 1, write, raw);
112  CXX_BITFIELD_MEMBER( 2, 2, indirect, raw);
113  };
114 
119 
123  void dump(unsigned idx) const
124  {
125  L4Re::Util::Dbg().printf("D[%04x]: %08llx (%x) f=%04x n=%04x\n",
126  idx, addr.get(),
127  len, (unsigned)flags.raw, (unsigned)next);
128  }
129  };
130 
134  class Avail
135  {
136  public:
140  struct Flags
141  {
143  Flags() = default;
144 
146  explicit Flags(l4_uint16_t v) : raw(v) {}
147 
149  CXX_BITFIELD_MEMBER( 0, 0, no_irq, raw);
150  };
151 
154  l4_uint16_t ring[];
155  };
156 
160  struct Used_elem
161  {
162  Used_elem() = default;
163 
171  Used_elem(l4_uint16_t id, l4_uint32_t len) : id(id), len(len) {}
174  };
175 
179  class Used
180  {
181  public:
185  struct Flags
186  {
188  Flags() = default;
189 
191  explicit Flags(l4_uint16_t v) : raw(v) {}
192 
194  CXX_BITFIELD_MEMBER( 0, 0, no_notify, raw);
195  };
196 
199  Used_elem ring[];
200  };
201 
202 protected:
206 
209 
215 
219  Virtqueue() : _desc(0), _idx_mask(0) {}
220  Virtqueue(Virtqueue const &) = delete;
221 
222 public:
228  void disable()
229  { _desc = 0; }
230 
234  enum
235  {
236  Desc_align = 4, //< Alignment of the descriptor table.
237  Avail_align = 1, //< Alignment of the available ring.
238  Used_align = 2, //< Alignment of the used ring.
239  };
240 
249  static unsigned long total_size(unsigned num)
250  {
251  static_assert(Desc_align >= Avail_align,
252  "virtqueue alignment assumptions broken");
253  return l4_round_size(desc_size(num) + avail_size(num), Used_align)
254  + used_size(num);
255  }
256 
265  static unsigned long desc_size(unsigned num)
266  { return num * 16; }
267 
273  static unsigned long desc_align()
274  { return Desc_align; }
275 
283  static unsigned long avail_size(unsigned num)
284  { return 2 * num + 6; }
285 
291  static unsigned long avail_align()
292  { return Avail_align; }
293 
302  static unsigned long used_size(unsigned num)
303  { return 8 * num + 6; }
304 
310  static unsigned long used_align()
311  { return Used_align; }
312 
318  unsigned long total_size() const
319  {
320  return ((char *) _used - (char *) _desc)
321  + used_size(num());
322  }
323 
327  unsigned long avail_offset() const
328  { return (char const *)_avail - (char const *)_desc; }
329 
333  unsigned long used_offset() const
334  { return (char const *)_used - (char const *)_desc; }
335 
353  void setup(unsigned num, void *desc, void *avail, void *used)
354  {
355  if (num > 0x10000)
356  throw L4::Runtime_error(-L4_EINVAL, "Queue too large.");
357 
358  _idx_mask = num - 1;
359  _desc = (Desc*)desc;
360  _avail = (Avail*)avail;
361  _used = (Used*)used;
362 
363  _current_avail = 0;
364 
365  L4Re::Util::Dbg().printf("VQ[%p]: num=%d d:%p a:%p u:%p\n",
366  this, num, _desc, _avail, _used);
367  }
368 
382  void setup_simple(unsigned num, void *ring)
383  {
384  l4_addr_t desc = reinterpret_cast<l4_addr_t>(ring);
385  l4_addr_t avail = l4_round_size(desc + desc_size(num), Avail_align);
386  l4_addr_t used = l4_round_size(avail + avail_size(num), Used_align);
387  setup(num, (void *)desc, (void *)avail, (void *)used);
388  }
389 
395  void dump(Desc const *d) const
396  { d->dump(d - _desc); }
397 
403  bool ready() const
404  { return L4_LIKELY(_desc != 0); }
405 
407  unsigned num() const
408  { return _idx_mask + 1; }
409 
417  bool no_notify_guest() const
418  {
419  return _avail->flags.no_irq();
420  }
421 
429  bool no_notify_host() const
430  {
431  return _used->flags.no_notify();
432  }
433 
439  void no_notify_host(bool value)
440  {
441  _used->flags.no_notify() = value;
442  }
443 
452  l4_uint16_t get_avail_idx() const { return _avail->idx; }
453 
459  l4_uint16_t get_tail_avail_idx() const { return _current_avail; }
460 
461 };
462 
463 namespace Driver {
464 
472 {
473 private:
475  l4_uint16_t _next_free;
476 
477 public:
478  enum End_of_queue
479  {
480  // Indicates the end of the queue.
481  Eoq = 0xFFFF
482  };
483 
484  Virtqueue() : _next_free(Eoq) {}
485 
495  void initialize_rings(unsigned num)
496  {
497  _used->idx = 0;
498  _avail->idx = 0;
499 
500  // setup the freelist
501  for (l4_uint16_t d = 0; d < num - 1; ++d)
502  _desc[d].next = d + 1;
503  _desc[num - 1].next = Eoq;
504  _next_free = 0;
505  }
506 
523  void init_queue(unsigned num, void *desc, void *avail, void *used)
524  {
525  setup(num, desc, avail, used);
526  initialize_rings(num);
527  }
528 
538  void init_queue(unsigned num, void *base)
539  {
540  setup_simple(num, base);
541  initialize_rings(num);
542  }
543 
544 
560  {
561  // XXX needs to be locked
562  l4_uint16_t idx = _next_free;
563  if (idx == Eoq)
564  return Eoq;
565 
566  _next_free = _desc[idx].next;
567 
568  return idx;
569  }
570 
577  {
578  if (descno > _idx_mask)
579  throw L4::Bounds_error();
580 
581  // TODO lock required
582  _avail->ring[_avail->idx & _idx_mask] = descno; // _avail->idx expected to wrap
583  wmb();
584  ++_avail->idx;
585  }
586 
594  {
595  if (descno > _idx_mask)
596  throw L4::Bounds_error();
597 
598  return _desc[descno];
599  }
600 
613  {
614  // TODO lock required
615  if (_current_avail == _used->idx)
616  return Eoq;
617 
618  auto elem = _used->ring[_current_avail++ & _idx_mask];
619 
620  if (len)
621  *len = elem.len;
622 
623  return elem.id;
624  }
625 
636  {
637  if (head > _idx_mask || tail > _idx_mask)
638  throw L4::Bounds_error();
639 
640  // TODO lock required
641  _desc[tail].next = _next_free;
642  _next_free = head;
643  }
644 };
645 
646 }
647 } // namespace L4virtio
static unsigned long desc_align()
Get the alignment in zero LSBs needed for the descriptor table.
Definition: virtqueue:273
Descriptor in the descriptor table.
Definition: virtqueue:93
Pointer used in virtio descriptors.
Definition: virtqueue:56
Used * _used
pointer to used ring.
Definition: virtqueue:205
Invalid argument.
Definition: err.h:56
static unsigned long avail_align()
Get the alignment in zero LSBs needed for the available ring.
Definition: virtqueue:291
void dump(Desc const *d) const
Dump descriptors for this queue.
Definition: virtqueue:395
l4_uint16_t raw
raw 16bit flags value of the available ring.
Definition: virtqueue:142
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition: l4int.h:38
void dump(unsigned idx) const
Dump a single descriptor.
Definition: virtqueue:123
Low-level Virtqueue.
Definition: virtqueue:87
Common L4 ABI Data Types.
l4_uint16_t raw
raw flags value as specified by virtio.
Definition: virtqueue:187
l4_uint16_t next
Index of the next chained descriptor.
Definition: virtqueue:118
bool no_notify_host() const
Get the no notify flag of this queue.
Definition: virtqueue:429
l4_addr_t l4_round_size(l4_umword_t value, unsigned char bits) L4_NOTHROW
Round value up to the next alignment with bits size.
Definition: consts.h:400
void init_queue(unsigned num, void *desc, void *avail, void *used)
Initialize this virtqueue.
Definition: virtqueue:523
static unsigned long total_size(unsigned num)
Calculate the total size for a virtqueue of the given dimensions.
Definition: virtqueue:249
Base exceptions.
no_notify_bfm_t::Val no_notify() const
Get the no_notify bits ( 0 to 0 ) of raw .
Definition: virtqueue:194
Invalid_type
Type for making an invalid (NULL) Ptr.
Definition: virtqueue:60
Avail * _avail
pointer to available ring.
Definition: virtqueue:204
bool ready() const
Test if this queue is in working state.
Definition: virtqueue:403
Ptr(Invalid_type)
Make and invalid Ptr.
Definition: virtqueue:65
l4_uint16_t _current_avail
The life counter for the queue.
Definition: virtqueue:208
void disable()
Completely disable the queue.
Definition: virtqueue:228
void no_notify_host(bool value)
Set the no-notify flag for this queue.
Definition: virtqueue:439
Flags(l4_uint16_t v)
Make Flags from raw 16bit value.
Definition: virtqueue:105
flags for the used ring.
Definition: virtqueue:185
Desc & desc(l4_uint16_t descno)
Return a reference to a descriptor in the descriptor table.
Definition: virtqueue:593
Exception for an abstract runtime error.
Definition: exceptions:139
void initialize_rings(unsigned num)
Initialize the descriptor table and the index structures of this queue.
Definition: virtqueue:495
Access out of bounds.
Definition: exceptions:289
bool is_valid() const
Definition: virtqueue:74
void free_descriptor(l4_uint16_t head, l4_uint16_t tail)
Free a chained list of descriptors in the descriptor queue.
Definition: virtqueue:635
Flags flags
flags of available ring
Definition: virtqueue:152
Type of available ring, this is read-only for the host.
Definition: virtqueue:134
l4_uint16_t raw
raw flags value of a virtio descriptor.
Definition: virtqueue:101
Virtqueue()
Create a disabled virtqueue.
Definition: virtqueue:219
Error codes.
static unsigned long used_align()
Get the alignment in zero LSBs needed for the used ring.
Definition: virtqueue:310
unsigned long used_offset() const
Get the offset of the used ring from the descriptor table.
Definition: virtqueue:333
Ptr< void > addr
Address stored in descriptor.
Definition: virtqueue:115
Ptr(l4_uint64_t vm_addr)
Make a Ptr from a raw 64bit address.
Definition: virtqueue:68
unsigned long total_size() const
Calculate the total size of this virtqueue.
Definition: virtqueue:318
void enqueue_descriptor(l4_uint16_t descno)
Enqueue a descriptor in the available ring.
Definition: virtqueue:576
Driver-side implementation of a Virtqueue.
Definition: virtqueue:471
l4_uint16_t get_tail_avail_idx() const
Get tail-available index stored in local state (for debugging).
Definition: virtqueue:459
l4_uint16_t get_avail_idx() const
Get available index from available ring (for debugging).
Definition: virtqueue:452
#define L4_LIKELY(x)
Expression is likely to execute.
Definition: compiler.h:233
l4_uint32_t id
descriptor index
Definition: virtqueue:172
l4_uint64_t get() const
Definition: virtqueue:71
l4_uint16_t idx
index of the last entry in the ring.
Definition: virtqueue:198
void setup(unsigned num, void *desc, void *avail, void *used)
Enable this queue.
Definition: virtqueue:353
Type for descriptor flags.
Definition: virtqueue:99
Used_elem(l4_uint16_t id, l4_uint32_t len)
Initialize a used ring element.
Definition: virtqueue:171
void setup_simple(unsigned num, void *ring)
Enable this queue.
Definition: virtqueue:382
Flags flags
Descriptor flags.
Definition: virtqueue:117
static unsigned long avail_size(unsigned num)
Calculate the size of the available ring for num entries.
Definition: virtqueue:283
Desc * _desc
pointer to descriptor table, NULL if queue is off.
Definition: virtqueue:203
no_irq_bfm_t::Val no_irq() const
Get the no_irq bits ( 0 to 0 ) of raw .
Definition: virtqueue:149
Type of an element of the used ring.
Definition: virtqueue:160
unsigned long avail_offset() const
Get the offset of the available ring from the descriptor table.
Definition: virtqueue:327
l4_uint32_t len
length field
Definition: virtqueue:173
Use to set a Ptr to invalid (NULL)
Definition: virtqueue:60
l4_uint16_t idx
available index written by guest
Definition: virtqueue:153
bool no_notify_guest() const
Get the no IRQ flag of this queue.
Definition: virtqueue:417
void init_queue(unsigned num, void *base)
Initialize this virtqueue.
Definition: virtqueue:538
static unsigned long desc_size(unsigned num)
Calculate the size of the descriptor table for num entries.
Definition: virtqueue:265
Flags(l4_uint16_t v)
make Flags from raw value
Definition: virtqueue:191
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition: l4int.h:42
l4_uint16_t find_next_used(l4_uint32_t *len=nullptr)
Return the next finished block.
Definition: virtqueue:612
l4_uint16_t alloc_descriptor()
Allocate and return an unused descriptor from the descriptor table.
Definition: virtqueue:559
L4-VIRTIO Transport C++ API.
Definition: l4virtio:29
l4_uint32_t len
Length of described buffer.
Definition: virtqueue:116
Flags flags
flags of the used ring.
Definition: virtqueue:197
unsigned long l4_addr_t
Address type.
Definition: l4int.h:45
Flags(l4_uint16_t v)
Make Flags from the raw value.
Definition: virtqueue:146
unsigned num() const
Definition: virtqueue:407
Flags of the available ring.
Definition: virtqueue:140
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition: l4int.h:40
static unsigned long used_size(unsigned num)
Calculate the size of the used ring for num entries.
Definition: virtqueue:302
l4_uint16_t _idx_mask
mask used for indexing into the descriptor table and the rings.
Definition: virtqueue:214