L4Re - L4 Runtime Environment
virtio-block
1 // vi:ft=cpp
2 /*
3  * Copyright (C) 2017 Kernkonzept GmbH.
4  * Author(s): Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
5  *
6  * This file is distributed under the terms of the GNU General Public
7  * License, version 2. Please see the COPYING-GPL-2 file for details.
8  */
9 #pragma once
10 
11 #include <l4/cxx/unique_ptr>
12 
13 #include <climits>
14 
15 #include <l4/l4virtio/virtio.h>
16 #include <l4/l4virtio/virtio_block.h>
17 #include <l4/l4virtio/server/l4virtio>
18 #include <l4/sys/cxx/ipc_epiface>
19 
20 namespace L4virtio { namespace Svr {
21 
22 template <typename Ds_data> class Block_dev;
23 
27 template<typename Ds_data>
29 {
30  friend Block_dev<Ds_data>;
31  enum { Header_size = sizeof(l4virtio_block_header_t) };
32 
33 public:
34  struct Data_block
35  {
39  void *addr;
41  l4_uint32_t len;
42 
43  Data_block() = default;
44 
45  Data_block(Driver_mem_region_t<Ds_data> *m, Virtqueue::Desc const &desc,
46  Request_processor const *)
47  : mem(m), addr(m->local(desc.addr)), len(desc.len)
48  {}
49  };
50 
51 
52 
63  unsigned data_size() const
64  {
66  Data_block data;
67 
68  rp.start(_mem_list, _request, &data);
69 
70  unsigned total = data.len;
71 
72  try
73  {
74  while (rp.has_more())
75  {
76  rp.next(_mem_list, &data);
77  total += data.len;
78  }
79  }
80  catch (Bad_descriptor const &e)
81  {
82  // need to convert the exception because e contains a raw pointer to rp
83  throw L4::Runtime_error(-L4_EIO, "bad virtio descriptor");
84  }
85 
86  if (total < Header_size + 1)
87  throw L4::Runtime_error(-L4_EIO, "virtio request too short");
88 
89  return total - Header_size - 1;
90  }
91 
95  bool has_more()
96  {
97  // peek into the remaining data
98  while (_data.len == 0 && _rp.has_more())
99  _rp.next(_mem_list, &_data);
100 
101  // there always must be one byte left for status
102  return (_data.len > 1 || _rp.has_more());
103  }
104 
113  Data_block next_block()
114  {
115  Data_block out;
116 
117  if (_data.len == 0)
118  {
119  if (!_rp.has_more())
121  "No more data blocks in virtio request");
122 
123  if (_todo_blocks == 0)
125  --_todo_blocks;
126 
127  _rp.next(_mem_list, &_data);
128  }
129 
130  if (_data.len > _max_block_size)
132 
133  out = _data;
134 
135  if (!_rp.has_more())
136  {
137  --(out.len);
138  _data.len = 1;
139  _data.addr = static_cast<char *>(_data.addr) + out.len;
140  }
141  else
142  _data.len = 0; // is consumed
143 
144  return out;
145  }
146 
149  { return _header; }
150 
151 private:
152  Block_request(Virtqueue::Request req, Driver_mem_list_t<Ds_data> *mem_list,
153  unsigned max_blocks, l4_uint32_t max_block_size)
154  : _mem_list(mem_list),
155  _request(req),
156  _todo_blocks(max_blocks),
157  _max_block_size(max_block_size)
158  {
159  // read header which should be in the first block
160  _rp.start(mem_list, _request, &_data);
161  --_todo_blocks;
162 
163  if (_data.len < Header_size)
165 
166  _header = *(static_cast<l4virtio_block_header_t *>(_data.addr));
167 
168  _data.addr = static_cast<char *>(_data.addr) + Header_size;
169  _data.len -= Header_size;
170 
171  // if there is no space for status bit we cannot really recover
172  if (!_rp.has_more() && _data.len == 0)
174  }
175 
176  int release_request(Virtqueue *queue, l4_uint8_t status, unsigned sz)
177  {
178  // write back status
179  // If there was an error on the way or the status byte is in its
180  // own block, fast-forward to the last block.
181  if (_rp.has_more())
182  {
183  while (_rp.next(_mem_list, &_data) && _todo_blocks > 0)
184  --_todo_blocks;
185 
186  if (_todo_blocks > 0 && _data.len > 0)
187  *(static_cast<l4_uint8_t *>(_data.addr) + _data.len - 1) = status;
188  else
189  return -L4_EIO; // too many data blocks
190  }
191  else if (_data.len > 0)
192  *(static_cast<l4_uint8_t *>(_data.addr)) = status;
193  else
194  return -L4_EIO; // no space for final status byte
195 
196  // now release the head
197  queue->consumed(_request, sz);
198 
199  return L4_EOK;
200  }
201 
207  Driver_mem_list_t<Ds_data> *_mem_list;
209  l4virtio_block_header_t _header;
211  Request_processor _rp;
213  Data_block _data;
214 
216  Virtqueue::Request _request;
218  unsigned _todo_blocks;
220  l4_uint32_t _max_block_size;
221 };
222 
223 struct Block_features : public Dev_config::Features
224 {
225  Block_features() = default;
226  Block_features(l4_uint32_t raw) : Dev_config::Features(raw) {}
227 
229  CXX_BITFIELD_MEMBER( 1, 1, size_max, raw);
231  CXX_BITFIELD_MEMBER( 2, 2, seg_max, raw);
233  CXX_BITFIELD_MEMBER( 4, 4, geometry, raw);
235  CXX_BITFIELD_MEMBER( 5, 5, ro, raw);
237  CXX_BITFIELD_MEMBER( 6, 6, blk_size, raw);
239  CXX_BITFIELD_MEMBER(10, 10, topology, raw);
240 };
241 
242 
248 template <typename Ds_data>
249 class Block_dev
250 : public L4virtio::Svr::Device_t<Ds_data>,
251  public L4::Epiface_t<Block_dev<Ds_data>, L4virtio::Device>
252 {
253 private:
254  class Irq_object : public L4::Irqep_t<Irq_object>
255  {
256  public:
257  Irq_object(Block_dev<Ds_data> *parent) : _parent(parent) {}
258 
259  void handle_irq()
260  {
261  _parent->kick();
262  }
263 
264  private:
265  Block_dev<Ds_data> *_parent;
266  };
267  Irq_object _irq_handler;
268 
269  L4Re::Util::Auto_cap<L4::Irq>::Cap _kick_guest_irq;
270  Virtqueue _queue;
271  unsigned _vq_max;
272  l4_uint32_t _max_block_size = UINT_MAX;
273  Dev_config_t<l4virtio_block_config_t> _dev_config;
274 
275 public:
276  typedef Block_request<Ds_data> Request;
277 
278 protected:
279  Block_features device_features() const
280  { return _dev_config.host_features(0); }
281 
282  void set_device_features(Block_features df)
283  { _dev_config.host_features(0) = df.raw; }
284 
295  {
296  _dev_config.priv_config()->size_max = sz;
297  Block_features df = device_features();
298  df.size_max() = true;
299  set_device_features(df);
300 
301  _max_block_size = sz;
302  }
303 
309  {
310  _dev_config.priv_config()->seg_max = sz;
311  Block_features df = device_features();
312  df.seg_max() = true;
313  set_device_features(df);
314  }
315 
319  void set_geometry(l4_uint16_t cylinders, l4_uint8_t heads, l4_uint8_t sectors)
320  {
321  l4virtio_block_config_t volatile *pc = _dev_config.priv_config();
322  pc->geometry.cylinders = cylinders;
323  pc->geometry.heads = heads;
324  pc->geometry.sectors = sectors;
325  Block_features df = device_features();
326  df.geometry() = true;
327  set_device_features(df);
328  }
329 
337  {
338  _dev_config.priv_config()->blk_size = sz;
339  Block_features df = device_features();
340  df.blk_size() = true;
341  set_device_features(df);
342  }
343 
352  void set_topology(l4_uint8_t physical_block_exp,
353  l4_uint8_t alignment_offset,
354  l4_uint32_t min_io_size,
355  l4_uint32_t opt_io_size)
356  {
357  l4virtio_block_config_t volatile *pc = _dev_config.priv_config();
358  pc->topology.physical_block_exp = physical_block_exp;
359  pc->topology.alignment_offset = alignment_offset;
360  pc->topology.min_io_size = min_io_size;
361  pc->topology.opt_io_size = opt_io_size;
362  Block_features df = device_features();
363  df.topology() = true;
364  set_device_features(df);
365  }
366 
367 
368 public:
377  Block_dev(l4_uint32_t vendor, unsigned queue_size,
378  l4_uint64_t capacity, bool read_only)
379  : L4virtio::Svr::Device_t<Ds_data>(&_dev_config),
380  _irq_handler(this), _vq_max(queue_size),
381  _dev_config(vendor, L4VIRTIO_ID_BLOCK, 1)
382  {
383  _kick_guest_irq = L4Re::chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>());
384  this->reset_queue_config(0, queue_size);
385 
386  Block_features df(0);
387  df.ring_indirect_desc() = true;
388  df.ro() = read_only;
389  set_device_features(df);
390 
391  _dev_config.priv_config()->capacity = capacity;
392  _dev_config.reset_hdr(); // to publish hardware features
393  }
394 
395 
410  virtual bool process_request(cxx::unique_ptr<Request> &&req) = 0;
411 
415  virtual void reset_device() = 0;
416 
420  virtual bool queue_stopped() = 0;
421 
433  void finalize_request(cxx::unique_ptr<Request> req, unsigned sz,
435  {
436  if (_dev_config.status().failed())
437  return;
438 
439  if (req->release_request(&_queue, status, sz) < 0)
440  this->device_error();
441 
442  // XXX not implemented
443  // _dev_config->irq_status |= 1;
444  _kick_guest_irq->trigger();
445 
446  // Request can be dropped here.
447  }
448 
449  int reconfig_queue(unsigned idx)
450  {
451  if (idx == 0 && this->setup_queue(&_queue, 0, _vq_max))
452  return 0;
453 
454  return -L4_EINVAL;
455  }
456 
468  char const *service = 0)
469  {
470  L4Re::chkcap(registry->register_irq_obj(&_irq_handler));
471  L4::Cap<void> ret;
472  if (service)
473  ret = registry->register_obj(this, service);
474  else
475  ret = registry->register_obj(this);
476  L4Re::chkcap(ret);
477 
478  return ret;
479  }
480 
481 protected:
482  L4::Ipc_svr::Server_iface *server_iface() const
483  {
484  return L4::Epiface::server_iface();
485  }
486 
487  void kick()
488  {
489  if (queue_stopped())
490  return;
491 
492  while (!_dev_config.status().failed())
493  {
494  auto r = _queue.next_avail();
495  if (!r)
496  return;
497 
498  try
499  {
500  cxx::unique_ptr<Request>
501  cur{new Request(r, &(this->_mem_info), _vq_max, _max_block_size)};
502 
503  if (!process_request(cxx::move(cur)))
504  return;
505  }
506  catch (Bad_descriptor const &e)
507  {
508  this->device_error();
509  return;
510  }
511  }
512  }
513 
514 private:
515  L4::Cap<L4::Irq> device_notify_irq() const
516  {
517  return L4::cap_cast<L4::Irq>(_irq_handler.obj_cap());
518  }
519 
520 
521  void register_single_driver_irq()
522  {
523  _kick_guest_irq = L4Re::chkcap(server_iface()->template rcv_cap<L4::Irq>(0));
524  L4Re::chksys(server_iface()->realloc_rcv_cap(0));
525  }
526 
527 
528  void reset()
529  {
530  _queue.disable();
531  reset_device();
532  }
533 
534  bool check_queues()
535  {
536  if (!_queue.ready())
537  {
538  reset();
539  return false;
540  }
541 
542  return true;
543  }
544 };
545 
546 } }
Abstract interface for object registries.
Definition: ipc_epiface:285
bool has_more() const
Are there more chained descriptors ?
Definition: virtio:452
Descriptor in the descriptor table.
Definition: virtqueue:93
L4::Cap< void > register_obj(L4::Registry_iface *registry, char const *service=0)
Attach device to an object registry.
Definition: virtio-block:467
Smart capability class.
Virtqueue implementation for the device.
Definition: virtio:93
void set_size_max(l4_uint32_t sz)
Sets the maximum size of any single segment reported to client.
Definition: virtio-block:294
Invalid argument.
Definition: err.h:56
Server-side L4-VIRTIO device stub.
Definition: l4virtio:652
General block device.
Definition: virtio.h:67
l4virtio_block_header_t const & header() const
Return the block request header.
Definition: virtio-block:148
Interface for server-loop related functions.
Definition: ipc_epiface:45
Request finished successfully.
Definition: virtio_block.h:44
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition: l4int.h:38
Block_dev(l4_uint32_t vendor, unsigned queue_size, l4_uint64_t capacity, bool read_only)
Create a new virtio block device.
Definition: virtio-block:377
void set_seg_max(l4_uint32_t sz)
Sets the maximum number of segments in a request that is reported to client.
Definition: virtio-block:308
C++ Irq interface.
Definition: irq:111
Exception used by Queue to indicate descriptor errors.
Definition: virtio:329
void set_geometry(l4_uint16_t cylinders, l4_uint8_t heads, l4_uint8_t sectors)
Set disk geometry that is reported to the client.
Definition: virtio-block:319
int reconfig_queue(unsigned idx)
callback for client queue-config request
Definition: virtio-block:449
Header structure of a request for a block device.
Definition: virtio_block.h:52
bool ready() const
Test if this queue is in working state.
Definition: virtqueue:403
Device configuration for block devices.
Definition: virtio_block.h:63
void start(DESC_MAN *dm, Virtqueue *ring, Virtqueue::Head_desc const &request, ARGS... args)
Start processing a new request.
Definition: virtio:404
unsigned data_size() const
Compute the total size of the data in the request.
Definition: virtio-block:63
void consumed(Head_desc const &r, l4_uint32_t len=0)
Put the given descriptor into the used ring.
Definition: virtio:172
void disable()
Completely disable the queue.
Definition: virtqueue:228
Request next_avail()
Get the next available descriptor from the available ring.
Definition: virtio:143
Encapsulate the state for processing a VIRTIO request.
Definition: virtio:381
void finalize_request(cxx::unique_ptr< Request > req, unsigned sz, l4_uint8_t status=L4VIRTIO_BLOCK_S_OK)
Releases resources related to a request and notifies the client.
Definition: virtio-block:433
Exception for an abstract runtime error.
Definition: exceptions:139
Base class for virtio block devices.
Definition: virtio-block:22
Data_block next_block()
Return next block in scatter-gather list.
Definition: virtio-block:113
Ok.
Definition: err.h:43
_Cap_alloc & cap_alloc
Capability allocator.
bool next(DESC_MAN *dm, ARGS... args)
Switch to the next descriptor in a descriptor chain.
Definition: virtio:463
Ptr< void > addr
Address stored in descriptor.
Definition: virtqueue:115
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition: error_helper:139
I/O error.
Definition: err.h:46
A request to read or write data.
Definition: virtio-block:28
T * local(Ptr< T > p) const
Get the local address for driver address p.
Definition: l4virtio:486
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition: error_helper:62
unsigned char l4_uint8_t
Unsigned 8bit value.
Definition: l4int.h:36
Type for device feature bitmap.
Definition: virtio:74
virtual L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o)=0
Register o as server-side object for asynchronous IRQs.
bool has_more()
Check if the request contains more data blocks.
Definition: virtio-block:95
Invalid size of memory block.
Definition: virtio:338
Cap< T > cap_cast(Cap< F > const &c)
static_cast for capabilities.
Definition: capability.h:376
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition: l4int.h:42
L4-VIRTIO Transport C++ API.
Definition: l4virtio:29
C++ interface for capabilities.
Definition: capability.h:13
void set_blk_size(l4_uint32_t sz)
Sets block disk size to be reported to the client.
Definition: virtio-block:336
l4_uint32_t len
Length of described buffer.
Definition: virtqueue:116
virtual L4::Cap< void > register_obj(L4::Epiface *o, char const *service)=0
Register an L4::Epiface for an IPC gate available in the applications environment under the name serv...
struct l4virtio_block_header_t l4virtio_block_header_t
Header structure of a request for a block device.
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition: l4int.h:40
Already exists.
Definition: err.h:54
void set_topology(l4_uint8_t physical_block_exp, l4_uint8_t alignment_offset, l4_uint32_t min_io_size, l4_uint32_t opt_io_size)
Sets the I/O alignment information reported back to the client.
Definition: virtio-block:352