L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
l4virtio
1// vi:ft=cpp
2/* SPDX-License-Identifier: GPL-2.0-only or License-Ref-kk-custom */
3/*
4 * Copyright (C) 2014-2022 Kernkonzept GmbH.
5 * Author(s): Alexander Warg <alexander.warg@kernkonzept.com>
6 * Manuel von Oltersdorff-Kalettka <manuel.kalettka@kernkonzept.com>
7 *
8 */
9#pragma once
10
11#include <algorithm>
12#include <limits.h>
13#include <memory>
14#include <vector>
15
16#include <l4/re/dataspace>
17#include <l4/re/util/debug>
18#include <l4/re/env>
19#include <l4/re/error_helper>
20#include <l4/re/rm>
21#include <l4/re/util/cap_alloc>
22#include <l4/re/util/shared_cap>
23#include <l4/re/util/unique_cap>
24
25#include <l4/sys/types.h>
26#include <l4/re/util/meta>
27
28#include <l4/cxx/bitfield>
29#include <l4/cxx/utils>
30#include <l4/cxx/unique_ptr>
31
32#include <l4/sys/cxx/ipc_legacy>
33
34#include "../l4virtio"
35#include "virtio"
36
40namespace L4virtio {
41namespace Svr {
42
53{
54public:
55 typedef Dev_status Status;
56 typedef Dev_features Features;
57
58private:
61
62 l4_uint32_t _vendor, _device, _qoffset, _nqueues;
63 l4_uint32_t _host_features[sizeof(l4virtio_config_hdr_t::dev_features_map)
64 / sizeof(l4_uint32_t)];
65 Cfg_cap _ds;
66 Cfg_region _config;
67 l4_addr_t _ds_offset = 0;
68
69 Status _status{0}; // status shadow, can be trusted by the device model
70
71 static l4_uint32_t align(l4_uint32_t x)
72 { return (x + 0xfU) & ~0xfU; }
73
74 void attach_n_init_cfg(Cfg_cap const &cfg, l4_addr_t offset)
75 {
76 L4Re::chksys(L4Re::Env::env()->rm()->attach(&_config, L4_PAGESIZE,
78 L4::Ipc::make_cap_rw(cfg.get()),
79 offset),
80 "Attach config space to local address space.");
81
82 _config->generation = 0;
83 memset(_config->driver_features_map, 0, sizeof(_config->driver_features_map));
84 memset(_host_features, 0, sizeof(_host_features));
85 set_host_feature(L4VIRTIO_FEATURE_VERSION_1);
86 reset_hdr();
87
88 _ds = cfg;
89 _ds_offset = offset;
90 }
91
92protected:
93 void volatile *get_priv_config() const
94 {
95 return l4virtio_device_config(_config.get());
96 }
97
98public:
99
113 unsigned cfg_size, l4_uint32_t num_queues = 0)
114 : _vendor(vendor), _device(device),
115 _qoffset(0x100 + align(cfg_size)),
116 _nqueues(num_queues)
117 {
118 using L4Re::Dataspace;
119 using L4Re::chkcap;
120 using L4Re::chksys;
121
122 if (sizeof(l4virtio_config_queue_t) * _nqueues + _qoffset > L4_PAGESIZE)
123 {
124 // too many queues does not fit into our page
125 _qoffset = 0;
126 _nqueues = 0;
127 }
128
129 auto cfg = chkcap(L4Re::Util::make_shared_cap<Dataspace>());
130 chksys(L4Re::Env::env()->mem_alloc()->alloc(L4_PAGESIZE, cfg.get()));
131
132 attach_n_init_cfg(cfg, 0);
133 }
134
146 Dev_config(Cfg_cap const &cfg, l4_addr_t cfg_offset,
147 l4_uint32_t vendor, l4_uint32_t device,
148 unsigned cfg_size, l4_uint32_t num_queues = 0)
149 : _vendor(vendor), _device(device),
150 _qoffset(0x100 + align(cfg_size)),
151 _nqueues(num_queues)
152 {
153 if (sizeof(l4virtio_config_queue_t) * _nqueues + _qoffset > L4_PAGESIZE)
154 {
155 // too many queues does not fit into our page
156 _qoffset = 0;
157 _nqueues = 0;
158 }
159
160 attach_n_init_cfg(cfg, cfg_offset);
161 }
162
163 void set_host_feature(unsigned feature)
164 { l4virtio_set_feature(_host_features, feature); }
165
166 void clear_host_feature(unsigned feature)
167 { l4virtio_clear_feature(_host_features, feature); }
168
169 bool get_host_feature(unsigned feature)
170 { return l4virtio_get_feature(_host_features, feature); }
171
172 bool get_guest_feature(unsigned feature)
173 { return l4virtio_get_feature(_config->driver_features_map, feature); }
174
175 l4_uint32_t &host_features(unsigned idx)
176 { return _host_features[idx]; }
177
178 l4_uint32_t host_features(unsigned idx) const
179 { return _host_features[idx]; }
180
192 l4_uint32_t guest_features(unsigned idx) const
193 { return _config->driver_features_map[idx]; }
194
207 { return _config->driver_features_map[idx] & _host_features[idx]; }
208
216 Status status() const { return _status; }
217
225 {
226 return hdr()->cmd;
227 }
228
236 {
237 const_cast<l4_uint32_t volatile &>(hdr()->cmd) = 0;
238 }
239
248 {
249 _status = status;
250 const_cast<l4_uint32_t volatile &>(hdr()->status) = status.raw;
251 }
252
260 {
261 _status.device_needs_reset() = 1;
262 const_cast<l4_uint32_t volatile &>(hdr()->status) = _status.raw;
263 }
264
270 {
271 if (sizeof(l4virtio_config_queue_t) * num_queues + _qoffset > L4_PAGESIZE)
272 // too many queues does not fit into our page
273 return false;
274
275 _nqueues = num_queues;
276 reset_hdr(true);
277 return true;
278 }
279
286 l4virtio_config_queue_t volatile const *qconfig(unsigned index) const
287 {
288 if (L4_UNLIKELY(_qoffset < sizeof (l4virtio_config_hdr_t)))
289 return 0;
290
291 if (L4_UNLIKELY(index >= _nqueues))
292 return 0;
293
294 return reinterpret_cast<l4virtio_config_queue_t const *>
295 (reinterpret_cast<char *>(_config.get()) + _qoffset) + index;
296 }
297
301 void reset_hdr(bool inc_generation = false) const
302 {
303 _config->magic = L4VIRTIO_MAGIC;
304 _config->version = 2;
305 _config->device = _device;
306 _config->vendor = _vendor;
307 _config->status = 0;
308 _config->irq_status = 0;
309 _config->num_queues = _nqueues;
310 _config->queues_offset = _qoffset;
311
312 memcpy(_config->dev_features_map, _host_features,
313 sizeof(_config->dev_features_map));
314 wmb();
315 if (inc_generation)
316 ++_config->generation;
317
318 }
319
328 bool reset_queue(unsigned index, unsigned num_max,
329 bool inc_generation = false) const
330 {
331 l4virtio_config_queue_t volatile *qc;
332 // this function is allowed to write to the device config
333 qc = const_cast<l4virtio_config_queue_t volatile *>(qconfig(index));
334 if (L4_UNLIKELY(qc == 0))
335 return false;
336
337 qc->num_max = num_max;
338 qc->num = 0;
339 qc->ready = 0;
340 wmb();
341 if (inc_generation)
342 ++_config->generation;
343
344 return true;
345 }
346
351 l4virtio_config_hdr_t const volatile *hdr() const
352 { return _config.get(); }
353
358 L4::Cap<L4Re::Dataspace> ds() const { return _ds.get(); }
359
365 { return _ds_offset; }
366};
367
368
369template<typename PRIV_CONFIG>
370class Dev_config_t : public Dev_config
371{
372public:
374 typedef PRIV_CONFIG Priv_config;
375
387 Dev_config_t(l4_uint32_t vendor, l4_uint32_t device,
388 l4_uint32_t num_queues = 0)
389 : Dev_config(vendor, device, sizeof(PRIV_CONFIG), num_queues)
390 {}
391
402 Dev_config_t(L4Re::Util::Shared_cap<L4Re::Dataspace> const &cfg,
403 l4_addr_t cfg_offset, l4_uint32_t vendor, l4_uint32_t device,
404 l4_uint32_t num_queues = 0)
405 : Dev_config(cfg, cfg_offset, vendor, device, sizeof(PRIV_CONFIG),
406 num_queues)
407 {}
408
418 Priv_config volatile *priv_config() const
419 {
420 return static_cast<Priv_config volatile *>(get_priv_config());
421 }
422
423};
424
425struct No_custom_data {};
426
432template <typename DATA>
433class Driver_mem_region_t : public DATA
434{
435public:
436 struct Flags
437 {
438 l4_uint32_t raw;
439 CXX_BITFIELD_MEMBER(0, 0, rw, raw);
440 };
441
442private:
445
446 l4_uint64_t _drv_base;
447 l4_uint64_t _trans_offset;
448 l4_umword_t _size;
449 Flags _flags;
450
451 Ds_cap _ds;
452 l4_addr_t _ds_offset;
453
456
457 template<typename T>
458 T _local(l4_uint64_t addr) const
459 {
460 return (T)(addr - _trans_offset);
461 }
462
463public:
465 Driver_mem_region_t() : _size(0) {}
466
479 l4_addr_t offset, Ds_cap &&ds)
480 : _drv_base(l4_trunc_page(drv_base)), _size(0),
481 _ds_offset(l4_trunc_page(offset))
482 {
483 using L4Re::chksys;
484 using L4Re::Env;
485
487 // Sometimes we work with dataspaces that do not implement all dataspace
488 // methods and return an error instead. An example of such a dataspace is
489 // io's Vi::System_bus. We detect this case when the info method returns
490 // -L4_ENOSYS and simply assume the dataspace is good for us.
491 long err = ds->info(&ds_info);
492 if (err >= 0)
493 {
494 l4_addr_t ds_size = l4_round_page(ds_info.size);
495
496 if (ds_size < L4_PAGESIZE)
497 chksys(-L4_EINVAL, "DS too small");
498
499 if (_ds_offset >= ds_size)
500 chksys(-L4_ERANGE, "offset larger than DS size");
501
503 if (size > ds_size)
504 chksys(-L4_EINVAL, "size larger than DS size");
505
506 if (_ds_offset > ds_size - size)
507 chksys(-L4_EINVAL, "invalid offset or size");
508
509 // overflow check
510 if ((ULLONG_MAX - size) < _drv_base)
511 chksys(-L4_EINVAL, "invalid size");
512
513 _flags.rw() = (ds_info.flags & L4Re::Dataspace::F::W).raw != 0;
514 }
515 else if (err == -L4_ENOSYS)
516 {
517 _flags.rw() = true;
518 }
519 else
520 {
521 chksys(err, "getting data-space infos");
522 }
523
525 if (_flags.rw())
526 f |= L4Re::Rm::F::W;
527
528 // use a big alignment to save PT/TLB entries and kernel memory resources!
529 chksys(Env::env()->rm()->attach(&_local_base, size, f,
530 L4::Ipc::make_cap(ds.get(), _flags.rw()
533 _ds_offset, L4_SUPERPAGESHIFT));
534
535 _size = size;
536 _ds = cxx::move(ds);
537 _trans_offset = _drv_base - _local_base.get();
538 }
539
541 bool is_writable() const { return _flags.rw(); }
542
544 Flags flags() const { return _flags; }
545
547 bool empty() const
548 { return _size == 0; }
549
551 l4_uint64_t drv_base() const { return _drv_base; }
552
554 void *local_base() const { return (void*)_local_base.get(); }
555
557 l4_umword_t size() const { return _size; }
558
560 l4_addr_t ds_offset() const { return _ds_offset; }
561
563 L4::Cap<L4Re::Dataspace> ds() const { return _ds.get(); }
564
573 {
574 if (base < _drv_base)
575 return false;
576
577 if (base > _drv_base + _size - 1)
578 return false;
579
580 if (size > _size)
581 return false;
582
583 if (base - _drv_base > _size - size)
584 return false;
585
586 return true;
587 }
588
595 template<typename T>
596 T *local(Ptr<T> p) const
597 { return _local<T*>(p.get()); }
598};
599
600typedef Driver_mem_region_t<No_custom_data> Driver_mem_region;
601
608template <typename DATA>
610{
611public:
612 typedef Driver_mem_region_t<DATA> Mem_region;
613
614private:
615 cxx::unique_ptr<Mem_region[]> _l;
616 unsigned _max;
617 unsigned _free;
618
619public:
622
624 Driver_mem_list_t() : _max(0), _free(0) {}
625
630 void init(unsigned max)
631 {
632 _l = cxx::make_unique<Driver_mem_region_t<DATA>[]>(max);
633 _max = max;
634 _free = 0;
635 }
636
638 bool full() const
639 { return _free == _max; }
640
649 Mem_region const *add(l4_uint64_t drv_base, l4_umword_t size,
650 l4_addr_t offset, Ds_cap &&ds)
651 {
652 if (full())
654
655 _l[_free++] = Mem_region(drv_base, size, offset, cxx::move(ds));
656 return &_l[_free - 1];
657 }
658
663 void remove(Mem_region const *r)
664 {
665 if (r < &_l[0] || r >= &_l[_free])
667
668 unsigned idx = r - &_l[0];
669
670 for (unsigned i = idx + 1; i < _free - 1; ++i)
671 _l[i] = cxx::move(_l[i + 1]);
672
673 _l[--_free] = Mem_region();
674 }
675
683 Mem_region *find(l4_uint64_t base, l4_umword_t size) const
684 {
685 return _find(base, size);
686 }
687
697 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
698 Virtqueue::Desc const **table) const
699 {
700 Mem_region const *r = find(desc.addr.get(), desc.len);
701 if (L4_UNLIKELY(!r))
703
704 *table = static_cast<Virtqueue::Desc const *>(r->local(desc.addr));
705 }
706
717 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
718 Mem_region const **data) const
719 {
720 Mem_region const *r = find(desc.addr.get(), desc.len);
721 if (L4_UNLIKELY(!r))
723
724 *data = r;
725 }
726
743 template<typename ARG>
744 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
745 ARG *data) const
746 {
747 Mem_region *r = find(desc.addr.get(), desc.len);
748 if (L4_UNLIKELY(!r))
750
751 *data = ARG(r, desc, p);
752 }
753
754private:
755 Mem_region *_find(l4_uint64_t base, l4_umword_t size) const
756 {
757 for (unsigned i = 0; i < _free; ++i)
758 if (_l[i].contains(base, size))
759 return &_l[i];
760 return 0;
761 }
762
763
764};
765
766typedef Driver_mem_list_t<No_custom_data> Driver_mem_list;
767
774template<typename DATA>
776{
777public:
778 typedef Driver_mem_list_t<DATA> Mem_list;
779
780protected:
781 Mem_list _mem_info;
782
783private:
784 Dev_config *_device_config;
785
786 using Ds_vector = std::vector<L4::Cap<L4Re::Dataspace>>;
788 std::shared_ptr<Ds_vector const> _trusted_ds_caps;
789
791 bool _trusted_ds_validation_enabled = false;
792
793public:
794 L4_RPC_LEGACY_DISPATCH(L4virtio::Device);
795 template<typename IOS> int virtio_dispatch(unsigned r, IOS &ios)
796 { return dispatch(r, ios); }
797
799 virtual void reset() = 0;
800
802 virtual bool check_features()
803 { return true; }
804
806 virtual bool check_queues() = 0;
807
809 virtual int reconfig_queue(unsigned idx) = 0;
810
813 { L4Re::chksys(-L4_ENOSYS, "Legacy single IRQ interface not implemented."); }
814
816 virtual void trigger_driver_config_irq() const = 0;
817
820 {
821 L4Re::chksys(-L4_ENOSYS, "Legacy single IRQ interface not implemented.");
822 return L4::Cap<L4::Irq>();
823 }
824
831 virtual void register_driver_irq(unsigned idx)
832 {
833 if (idx != 0)
834 L4Re::chksys(-L4_ENOSYS, "Multi IRQ interface not implemented.");
835
837 }
838
846 {
847 if (idx != 0)
848 L4Re::chksys(-L4_ENOSYS, "Multi IRQ interface not implemented.");
849
850 return device_notify_irq();
851 }
852
854 virtual unsigned num_events_supported() const
855 { return 1; }
856
857 virtual L4::Ipc_svr::Server_iface *server_iface() const = 0;
858
862 Device_t(Dev_config *dev_config)
863 : _device_config(dev_config)
864 {}
865
869 Mem_list const *mem_info() const
870 { return &_mem_info; };
871
872 long op_set_status(L4virtio::Device::Rights, unsigned status)
873 { return _set_status(status); }
874
875 long op_config_queue(L4virtio::Device::Rights, unsigned queue)
876 {
877 Dev_config::Status status = _device_config->status();
878 if (status.fail_state() || !status.acked() || !status.driver())
879 return -L4_EIO;
880
881 return reconfig_queue(queue);
882 }
883
884 long op_register_ds(L4virtio::Device::Rights,
885 L4::Ipc::Snd_fpage ds_cap_fp, l4_uint64_t ds_base,
886 l4_umword_t offset, l4_umword_t sz)
887 {
888 L4Re::Util::Dbg()
889 .printf("Registering dataspace from 0x%llx with %lu KiB, offset 0x%lx\n",
890 ds_base, sz >> 10, offset);
891
892 _check_n_init_shm(ds_cap_fp, ds_base, sz, offset);
893
894 return 0;
895 }
896
897 long op_device_config(L4virtio::Device::Rights,
899 l4_addr_t &ds_offset)
900 {
901 L4Re::Util::Dbg()
902 .printf("register client: host IRQ: %lx config DS: %lx\n",
903 device_notify_irq().cap(), _device_config->ds().cap());
904
905 config_ds = L4::Ipc::make_cap(_device_config->ds(), L4_CAP_FPAGE_RW);
906 ds_offset = _device_config->ds_offset();
907 return 0;
908 }
909
910 long op_device_notification_irq(L4virtio::Device::Rights,
911 unsigned idx,
913 {
914 auto cap = device_notify_irq(idx);
915
916 if (!cap.is_valid())
917 return -L4_EINVAL;
918
920 return L4_EOK;
921 }
922
923 int op_bind(L4::Icu::Rights, l4_umword_t idx, L4::Ipc::Snd_fpage irq_cap_fp)
924 {
925 if (idx >= num_events_supported())
926 return -L4_ERANGE;
927
928 if (!irq_cap_fp.cap_received())
929 return -L4_EINVAL;
930
932
933 return L4_EOK;
934 }
935
936 int op_unbind(L4::Icu::Rights, l4_umword_t, L4::Ipc::Snd_fpage)
937 {
938 return -L4_ENOSYS;
939 }
940
941 int op_info(L4::Icu::Rights, L4::Icu::_Info &info)
942 {
943 info.features = 0;
944 info.nr_irqs = num_events_supported();
945 info.nr_msis = 0;
946
947 return L4_EOK;
948 }
949
950 int op_msi_info(L4::Icu::Rights, l4_umword_t, l4_uint64_t, l4_icu_msi_info_t &)
951 { return -L4_ENOSYS; }
952
953 int op_mask(L4::Icu::Rights, l4_umword_t)
954 { return -L4_ENOSYS; }
955
956 int op_unmask(L4::Icu::Rights, l4_umword_t)
957 { return -L4_ENOREPLY; }
958
959 int op_set_mode(L4::Icu::Rights, l4_umword_t, l4_umword_t)
960 { return -L4_ENOSYS; }
961
973 void reset_queue_config(unsigned idx, unsigned num_max,
974 bool inc_generation = false)
975 {
976 _device_config->reset_queue(idx, num_max, inc_generation);
977 }
978
983 void init_mem_info(unsigned num)
984 {
985 _mem_info.init(num);
986 }
987
996 {
997 reset();
998 _device_config->set_device_needs_reset();
999
1000 // the device MUST NOT notify the driver before DRIVER_OK.
1001 if (_device_config->status().driver_ok())
1002 {
1003 // we do not care about this anywhere, so skip
1004 // _dev_config->irq_status |= 1;
1006 }
1007 }
1008
1022 bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
1023 {
1024 l4virtio_config_queue_t volatile const *qc;
1025 qc = _device_config->qconfig(qn);
1026 if (L4_UNLIKELY(qc == 0))
1027 return false;
1028
1029 if (!qc->ready)
1030 {
1031 q->disable();
1032 return true;
1033 }
1034
1035 // read to local variables before check
1036 l4_uint32_t num = qc->num;
1037 l4_uint64_t desc = qc->desc_addr;
1038 l4_uint64_t avail = qc->avail_addr;
1039 l4_uint64_t used = qc->used_addr;
1040
1041 if (0)
1042 printf("%p: setup queue: num=0x%x max_num=0x%x desc=0x%llx avail=0x%llx used=0x%llx\n",
1043 this, num, num_max, desc, avail, used);
1044
1045 if (!num || num > num_max)
1046 return false;
1047
1048 // num must be power of two
1049 if (num & (num - 1))
1050 return false;
1051
1052 if (desc & 0xf)
1053 return false;
1054
1055 if (avail & 0x1)
1056 return false;
1057
1058 if (used & 0x3)
1059 return false;
1060
1061 auto const *desc_info = _mem_info.find(desc, Virtqueue::desc_size(num));
1062 if (L4_UNLIKELY(!desc_info))
1063 return false;
1064
1065 auto const *avail_info = _mem_info.find(avail, Virtqueue::avail_size(num));
1066 if (L4_UNLIKELY(!avail_info))
1067 return false;
1068
1069 auto const *used_info = _mem_info.find(used, Virtqueue::used_size(num));
1070 if (L4_UNLIKELY(!used_info || !used_info->is_writable()))
1071 return false;
1072
1073 L4Re::Util::Dbg()
1074 .printf("shm=[%llx-%llx] local=[%lx-%lx] desc=[%llx-%llx] (%p-%p)\n",
1075 desc_info->drv_base(), desc_info->drv_base() + desc_info->size() - 1,
1076 (unsigned long)desc_info->local_base(),
1077 (unsigned long)desc_info->local_base() + desc_info->size() - 1,
1078 desc, desc + Virtqueue::desc_size(num),
1079 desc_info->local(Ptr<char>(desc)),
1080 desc_info->local(Ptr<char>(desc)) + Virtqueue::desc_size(num));
1081
1082 L4Re::Util::Dbg()
1083 .printf("shm=[%llx-%llx] local=[%lx-%lx] avail=[%llx-%llx] (%p-%p)\n",
1084 avail_info->drv_base(), avail_info->drv_base() + avail_info->size() - 1,
1085 (unsigned long)avail_info->local_base(),
1086 (unsigned long)avail_info->local_base() + avail_info->size() - 1,
1087 avail, avail + Virtqueue::avail_size(num),
1088 avail_info->local(Ptr<char>(avail)),
1089 avail_info->local(Ptr<char>(avail)) + Virtqueue::avail_size(num));
1090
1091 L4Re::Util::Dbg()
1092 .printf("shm=[%llx-%llx] local=[%lx-%lx] used=[%llx-%llx] (%p-%p)\n",
1093 used_info->drv_base(), used_info->drv_base() + used_info->size() - 1,
1094 (unsigned long)used_info->local_base(),
1095 (unsigned long)used_info->local_base() + used_info->size() - 1,
1096 used, used + Virtqueue::used_size(num),
1097 used_info->local(Ptr<char>(used)),
1098 used_info->local(Ptr<char>(used)) + Virtqueue::used_size(num));
1099
1100 q->setup(num, desc_info->local(Ptr<void>(desc)),
1101 avail_info->local(Ptr<void>(avail)),
1102 used_info->local(Ptr<void>(used)));
1103 return true;
1104 }
1105
1106 void check_n_init_shm(L4Re::Util::Unique_cap<L4Re::Dataspace> &&shm,
1107 l4_uint64_t base, l4_umword_t size, l4_addr_t offset)
1108 {
1109 if (_mem_info.full())
1111
1112 auto const *i = _mem_info.add(base, size, offset, cxx::move(shm));
1113 L4Re::Util::Dbg()
1114 .printf("PORT[%p]: DMA guest [%llx-%llx] local [%lx-%lx] offset %lx\n",
1115 this, i->drv_base(), i->drv_base() + i->size() - 1,
1116 (unsigned long)i->local_base(),
1117 (unsigned long)i->local_base() + i->size() - 1,
1118 i->ds_offset());
1119 }
1120
1130 {
1131 l4_uint32_t cmd = _device_config->get_cmd();
1132 if (L4_LIKELY(!(cmd & L4VIRTIO_CMD_MASK)))
1133 return false;
1134
1135 switch (cmd & L4VIRTIO_CMD_MASK)
1136 {
1138 _set_status(cmd & ~L4VIRTIO_CMD_MASK);
1139 break;
1140
1143 break;
1144
1145 default:
1146 // unknown command
1147 break;
1148 }
1149
1150 _device_config->reset_cmd();
1151
1152 return true;
1153 }
1154
1159 {
1160 _trusted_ds_validation_enabled = true;
1161 }
1162
1168 void
1169 add_trusted_dataspaces(std::shared_ptr<Ds_vector const> ds)
1170 {
1171 _trusted_ds_caps = ds;
1172 }
1173
1174
1175private:
1187 long validate_ds(L4::Cap<L4Re::Dataspace> ds)
1188 {
1189 if (!_trusted_ds_caps)
1190 return -L4_EINVAL;
1191 if (std::any_of(_trusted_ds_caps->cbegin(), _trusted_ds_caps->cend(),
1192 [&ds](L4::Cap<L4Re::Dataspace> cap)
1193 {
1194 return L4Re::Env::env()->task()
1195 ->cap_equal(ds, cap).label() == 1;
1196 }
1197 ))
1198 {
1199 return L4_EOK;
1200 }
1201 return -L4_EINVAL;
1202 }
1203
1204 void _check_n_init_shm(L4::Ipc::Snd_fpage shm_cap_fp,
1205 l4_uint64_t base, l4_umword_t size, l4_addr_t offset)
1206 {
1207 if (!shm_cap_fp.cap_received())
1209
1211 L4Re::chkcap(server_iface()->template rcv_cap<L4Re::Dataspace>(0)));
1212 L4Re::chksys(server_iface()->realloc_rcv_cap(0));
1213
1214 if (_trusted_ds_validation_enabled)
1215 L4Re::chksys(validate_ds(ds.get()), "Validating the dataspace.");
1216
1217 check_n_init_shm(cxx::move(ds), base, size, offset);
1218 }
1219
1220 bool check_features_internal()
1221 {
1222 static_assert(sizeof(l4virtio_config_hdr_t::driver_features_map)
1223 == sizeof(l4virtio_config_hdr_t::dev_features_map),
1224 "Driver and device feature maps must be of the same size");
1225
1226 // From the Virtio 1.0 specification 6.1 Driver Requirements and 6.2 Device
1227 // Requirements: A driver MUST accept VIRTIO_F_VERSION_1 if it is offered.
1228 // A device MUST offer VIRTIO_F_VERSION_1. A device MAY fail to operate
1229 // further if VIRTIO_F_VERSION_1 is not accepted.
1230 //
1231 // The L4virtio implementation does not support legacy interfaces so we
1232 // fail here if the Virtio 1.0 feature was not accepted.
1233 if (!_device_config->get_guest_feature(L4VIRTIO_FEATURE_VERSION_1))
1234 return false;
1235
1236 for (auto i = 0u;
1237 i < sizeof(l4virtio_config_hdr_t::driver_features_map)
1238 / sizeof(l4virtio_config_hdr_t::driver_features_map[0]);
1239 i++)
1240 {
1241 // Driver must not accept features that were not offered by device
1242 if (_device_config->guest_features(i)
1243 & ~_device_config->host_features(i))
1244 return false;
1245 }
1246 return check_features();
1247 }
1248
1270 void check_and_update_status(Dev_config::Status status)
1271 {
1272 // snapshot of current status
1273 Dev_config::Status current_status = _device_config->status();
1274
1275 // handle reset
1276 if (!status.raw)
1277 {
1278 _device_config->set_status(status);
1279 return;
1280 }
1281
1282 // Do no further processing in case of driver or device failure. If FAILED
1283 // or DEVICE_NEEDS_RESET are set only these fail_state bits will be set in
1284 // addition to the current status bits already set.
1285 if (current_status.fail_state() || status.fail_state())
1286 {
1287 if (current_status.fail_state() != status.fail_state())
1288 {
1289 current_status.fail_state() =
1290 current_status.fail_state() | status.fail_state();
1291 _device_config->set_status(current_status);
1292 }
1293 return;
1294 }
1295
1296 // Enforce init sequence ACKNOWLEDGE, DRIVER, FEATURES_OK, DRIVER_OK.
1297 // We do not enforce that only one additional new bit is set per call.
1298 if ((!status.acked() && status.driver())
1299 || (!status.driver() && status.features_ok())
1300 || (!status.features_ok() && status.driver_ok()))
1301 {
1302 current_status.device_needs_reset() = 1;
1303 _device_config->set_status(current_status);
1304 return;
1305 }
1306
1307 // only check feature compatibility before DRIVER_OK is set
1308 if (status.features_ok() && !status.driver_ok()
1309 && !check_features_internal())
1310 status.features_ok() = 0;
1311
1312 // Note that if FEATURES_OK and DRIVER_OK are both updated to being set
1313 // at the same time the above check_features_internal() is skipped; this is
1314 // considered undefined behaviour but it is not prevented.
1315 if (status.running() && !check_queues())
1316 {
1317 current_status.device_needs_reset() = 1;
1318 _device_config->set_status(current_status);
1319 return;
1320 }
1321
1322 _device_config->set_status(status);
1323 }
1324
1337 int _set_status(unsigned new_status)
1338 {
1339 if (new_status == 0)
1340 {
1341 L4Re::Util::Dbg().printf("Resetting device\n");
1342 reset();
1343 _device_config->reset_hdr(true);
1344 }
1345
1346 Dev_config::Status status(new_status);
1347 check_and_update_status(status);
1348
1349 return 0;
1350 }
1351
1352};
1353
1354typedef Device_t<No_custom_data> Device;
1355
1356} // namespace Svr
1357
1358}
1359
Interface for memory-like objects.
Definition dataspace:63
long info(Stats *stats)
Get information on the dataspace.
C++ interface of the initial environment that is provided to an L4 task.
Definition env:86
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:103
Unique region.
Definition rm:413
T get() const noexcept
Return the address.
Definition rm:486
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:52
C++ interface for capabilities.
Definition capability.h:222
Capability type for RPC interfaces (see L4::Cap<T>).
Definition ipc_types:546
Generic RPC wrapper for L4 flex-pages.
Definition ipc_types:322
bool cap_received() const noexcept
Check if at least one capability has been mapped.
Definition ipc_types:442
Interface for server-loop related functions.
Definition ipc_epiface:48
IPC interface for virtio over L4 IPC.
Definition l4virtio:42
Pointer used in virtio descriptors.
Definition virtqueue:64
l4_uint64_t get() const
Definition virtqueue:78
Abstraction for L4-Virtio device config memory.
Definition l4virtio:53
void set_status(Status status)
Set device status register.
Definition l4virtio:247
bool reset_queue(unsigned index, unsigned num_max, bool inc_generation=false) const
Reset queue config for the given queue.
Definition l4virtio:328
l4virtio_config_hdr_t const volatile * hdr() const
Get a read-only pointer to the config header.
Definition l4virtio:351
l4_uint32_t get_cmd() const
Get the value from the cmd register.
Definition l4virtio:224
Dev_config(l4_uint32_t vendor, l4_uint32_t device, unsigned cfg_size, l4_uint32_t num_queues=0)
Create a L4-Virtio config data space.
Definition l4virtio:112
bool change_queue_config(l4_uint32_t num_queues)
Setup new queue configuration.
Definition l4virtio:269
L4::Cap< L4Re::Dataspace > ds() const
Get data-space capability for the shared config data space.
Definition l4virtio:358
Status status() const
Get current device status (trusted).
Definition l4virtio:216
void reset_cmd()
Reset the cmd register after execution of a command.
Definition l4virtio:235
l4virtio_config_queue_t volatile const * qconfig(unsigned index) const
Get queue read-only config data for queue with the given index.
Definition l4virtio:286
void reset_hdr(bool inc_generation=false) const
Reset the config header to the initial contents.
Definition l4virtio:301
Dev_config(Cfg_cap const &cfg, l4_addr_t cfg_offset, l4_uint32_t vendor, l4_uint32_t device, unsigned cfg_size, l4_uint32_t num_queues=0)
Setup an L4-Virtio config space in an existing data space.
Definition l4virtio:146
l4_uint32_t guest_features(unsigned idx) const
Return a specific set of guest features.
Definition l4virtio:192
l4_uint32_t negotiated_features(unsigned idx) const
Compute a specific set of negotiated features.
Definition l4virtio:206
l4_addr_t ds_offset() const
Return the offset into the config dataspace where the device configuration starts.
Definition l4virtio:364
void set_device_needs_reset()
Set DEVICE_NEEDS_RESET bit in device status register.
Definition l4virtio:259
Server-side L4-VIRTIO device stub.
Definition l4virtio:776
void device_error()
Transition device into DEVICE_NEEDS_RESET state.
Definition l4virtio:995
void add_trusted_dataspaces(std::shared_ptr< Ds_vector const > ds)
Provide a list of trusted dataspaces that can be used for validation.
Definition l4virtio:1169
virtual void reset()=0
reset callback, called for doing a device reset
bool handle_mem_cmd_write()
Check for a value in the cmd register and handle a write.
Definition l4virtio:1129
Mem_list _mem_info
Memory region list.
Definition l4virtio:781
virtual void trigger_driver_config_irq() const =0
callback for triggering configuration change notification IRQ
virtual void register_driver_irq(unsigned idx)
Callback for registering an notification IRQ (multi IRQ).
Definition l4virtio:831
Device_t(Dev_config *dev_config)
Make a device for the given config.
Definition l4virtio:862
virtual void register_single_driver_irq()
callback for registering a single guest IRQ for all queues (old-style)
Definition l4virtio:812
virtual bool check_features()
callback for checking the subset of accepted features
Definition l4virtio:802
virtual unsigned num_events_supported() const
Return the highest notification index supported.
Definition l4virtio:854
bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
Enable/disable the specified queue.
Definition l4virtio:1022
void init_mem_info(unsigned num)
Initialize the memory region list to the given maximum.
Definition l4virtio:983
Mem_list const * mem_info() const
Get the memory region list used for this device.
Definition l4virtio:869
virtual int reconfig_queue(unsigned idx)=0
callback for client queue-config request
void enable_trusted_ds_validation()
Enable trusted dataspace validation.
Definition l4virtio:1158
virtual L4::Cap< L4::Irq > device_notify_irq(unsigned idx)
Callback to gather the device notification IRQ (multi IRQ).
Definition l4virtio:845
virtual L4::Cap< L4::Irq > device_notify_irq() const
callback to gather the device notification IRQ (old-style)
Definition l4virtio:819
void reset_queue_config(unsigned idx, unsigned num_max, bool inc_generation=false)
Trigger reset for the configuration space for queue idx.
Definition l4virtio:973
virtual bool check_queues()=0
callback for checking if the queues at DRIVER_OK transition
List of driver memory regions assigned to a single L4-VIRTIO transport instance.
Definition l4virtio:610
Mem_region * find(l4_uint64_t base, l4_umword_t size) const
Find memory region containing the given driver address region.
Definition l4virtio:683
void load_desc(Virtqueue::Desc const &desc, Request_processor const *p, Virtqueue::Desc const **table) const
Default implementation for loading an indirect descriptor.
Definition l4virtio:697
void remove(Mem_region const *r)
Remove the given region from the list.
Definition l4virtio:663
Mem_region const * add(l4_uint64_t drv_base, l4_umword_t size, l4_addr_t offset, Ds_cap &&ds)
Add a new region to the list.
Definition l4virtio:649
L4Re::Util::Unique_cap< L4Re::Dataspace > Ds_cap
type for storing a data-space capability internally
Definition l4virtio:621
void init(unsigned max)
Make a fresh list with capacity max.
Definition l4virtio:630
void load_desc(Virtqueue::Desc const &desc, Request_processor const *p, Mem_region const **data) const
Default implementation returning the Driver_mem_region.
Definition l4virtio:717
void load_desc(Virtqueue::Desc const &desc, Request_processor const *p, ARG *data) const
Default implementation returning generic information.
Definition l4virtio:744
Driver_mem_list_t()
Make an empty, zero capacity list.
Definition l4virtio:624
Region of driver memory, that shall be managed locally.
Definition l4virtio:434
bool contains(l4_uint64_t base, l4_umword_t size) const
Test if the given driver address range is within this region.
Definition l4virtio:572
l4_addr_t ds_offset() const
Definition l4virtio:560
Driver_mem_region_t()
Make default empty memory region.
Definition l4virtio:465
l4_umword_t size() const
Definition l4virtio:557
Driver_mem_region_t(l4_uint64_t drv_base, l4_umword_t size, l4_addr_t offset, Ds_cap &&ds)
Make a local memory region for the given driver values.
Definition l4virtio:478
L4::Cap< L4Re::Dataspace > ds() const
Definition l4virtio:563
l4_uint64_t drv_base() const
Definition l4virtio:551
T * local(Ptr< T > p) const
Get the local address for driver address p.
Definition l4virtio:596
Encapsulate the state for processing a VIRTIO request.
Definition virtio:400
Virtqueue implementation for the device.
Definition virtio:88
Descriptor in the descriptor table.
Definition virtqueue:103
l4_uint32_t len
Length of described buffer.
Definition virtqueue:125
Ptr< void > addr
Address stored in descriptor.
Definition virtqueue:124
void disable()
Completely disable the queue.
Definition virtqueue:237
void setup(unsigned num, void *desc, void *avail, void *used)
Enable this queue.
Definition virtqueue:362
static unsigned long desc_size(unsigned num)
Calculate the size of the descriptor table for num entries.
Definition virtqueue:274
static unsigned long used_size(unsigned num)
Calculate the size of the used ring for num entries.
Definition virtqueue:311
static unsigned long avail_size(unsigned num)
Calculate the size of the available ring for num entries.
Definition virtqueue:292
Dataspace interface.
Environment interface.
Error helper.
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:51
unsigned long l4_addr_t
Address type.
Definition l4int.h:45
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:40
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:42
@ L4_ERANGE
Range error.
Definition err.h:58
@ L4_ENOSYS
No sys.
Definition err.h:60
@ L4_EINVAL
Invalid argument.
Definition err.h:56
@ L4_ENOREPLY
No reply.
Definition err.h:65
@ L4_EIO
I/O error.
Definition err.h:46
@ L4_EOK
Ok.
Definition err.h:43
@ L4_ENOMEM
No memory.
Definition err.h:50
@ L4_CAP_FPAGE_RO
Read right for capability flex-pages.
Definition __l4_fpage.h:179
@ L4_CAP_FPAGE_RW
Read and interface specific 'W' right for capability flex-pages.
Definition __l4_fpage.h:195
#define L4_SUPERPAGESHIFT
Size of a large page, log2-based.
Definition consts.h:42
l4_addr_t l4_trunc_page(l4_addr_t address) L4_NOTHROW
Round an address down to the next lower page boundary.
Definition consts.h:437
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
Definition consts.h:462
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition consts.h:380
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:285
#define L4_LIKELY(x)
Expression is likely to execute.
Definition compiler.h:284
void * l4virtio_device_config(l4virtio_config_hdr_t const *cfg)
Get the pointer to the device configuration.
Definition virtio.h:246
void l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
Clear the given feature bit in a feature map.
Definition virtio.h:267
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
Definition virtio.h:255
unsigned l4virtio_get_feature(l4_uint32_t *feature_map, unsigned feat)
Check if the given bit in a feature map is set.
Definition virtio.h:279
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition virtio.h:96
@ L4VIRTIO_CMD_SET_STATUS
Set the status register.
Definition virtio.h:117
@ L4VIRTIO_CMD_CFG_QUEUE
Configure a queue.
Definition virtio.h:118
@ L4VIRTIO_CMD_MASK
Mask to get command bits.
Definition virtio.h:119
Common L4 ABI Data Types.
L4::Detail::Shared_cap_impl< T, Smart_count_cap< L4_FP_ALL_SPACES > > Shared_cap
Shared capability that implements automatic free and unmap of the capability selector.
Definition shared_cap:59
L4::Detail::Unique_cap_impl< T, Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability selector.
Definition unique_cap:54
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition error_helper:68
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition error_helper:145
Cap< T > make_cap(L4::Cap< T > cap, unsigned rights) noexcept
Make an L4::Ipc::Cap<T> for the given capability and rights.
Definition ipc_types:628
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
Definition ipc_types:638
L4-VIRTIO Transport C++ API.
Definition l4virtio:26
Region mapper interface.
@ W
Request write-only mapping.
Definition dataspace:89
Information about the dataspace.
Definition dataspace:136
@ RW
Readable and writable region.
Definition rm:140
@ R
Readable region.
Definition rm:134
@ W
Writable region.
Definition rm:136
@ Search_addr
Search for a suitable address range.
Definition rm:118
Exception used by Queue to indicate descriptor errors.
Definition virtio:326
@ Bad_address
Address cannot be translated.
Definition virtio:330
Type for device feature bitmap.
Definition virtio:67
Type of the device status register.
Definition virtio:33
device_needs_reset_bfm_t::Val device_needs_reset() const
Get the device_needs_reset bits ( 6 to 6 ) of raw.
Definition virtio:45
driver_ok_bfm_t::Val driver_ok() const
Get the driver_ok bits ( 2 to 2 ) of raw.
Definition virtio:42
unsigned char raw
Raw value of the VIRTIO device status register.
Definition virtio:34
Info to use for a specific MSI.
Definition icu.h:194
L4-VIRTIO config header, provided in shared data space.
Definition virtio.h:126
l4_uint32_t vendor
vendor ID
Definition virtio.h:130
l4_uint32_t magic
magic value (must be 'virt').
Definition virtio.h:127
l4_uint32_t queues_offset
offset of virtqueue config array
Definition virtio.h:141
l4_uint32_t device
device ID
Definition virtio.h:129
l4_uint32_t cmd
L4 specific command register polled by the driver iff supported.
Definition virtio.h:185
l4_uint32_t status
Device status register (read-only).
Definition virtio.h:164
l4_uint32_t num_queues
number of virtqueues
Definition virtio.h:140
l4_uint32_t version
VIRTIO version.
Definition virtio.h:128
Queue configuration entry.
Definition virtio.h:207
l4_uint64_t avail_addr
W: address of available ring.
Definition virtio.h:220
l4_uint64_t desc_addr
W: address of descriptor table.
Definition virtio.h:219
l4_uint64_t used_addr
W: address of used ring.
Definition virtio.h:221
l4_uint16_t ready
RW: queue ready flag (read-write)
Definition virtio.h:214
l4_uint16_t num_max
R: maximum number of descriptors supported by this queue.
Definition virtio.h:209
l4_uint16_t num
RW: number of descriptors configured for this queue.
Definition virtio.h:211
Capability allocator.
Shared_cap / Shared_del_cap.
Unique_cap / Unique_del_cap.