L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
l4virtio
1// vi:ft=cpp
2/* SPDX-License-Identifier: MIT */
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
185 { return _nqueues; }
186
198 l4_uint32_t guest_features(unsigned idx) const
199 { return _config->driver_features_map[idx]; }
200
213 { return _config->driver_features_map[idx] & _host_features[idx]; }
214
222 Status status() const { return _status; }
223
231 {
232 return hdr()->cmd;
233 }
234
242 {
243 const_cast<l4_uint32_t volatile &>(hdr()->cmd) = 0;
244 }
245
254 {
255 _status = status;
256 const_cast<l4_uint32_t volatile &>(hdr()->status) = status.raw;
257 }
258
266 {
267 const_cast<l4_uint32_t volatile &>(hdr()->irq_status) |= status;
268 }
269
277 {
278 _status.device_needs_reset() = 1;
279 const_cast<l4_uint32_t volatile &>(hdr()->status) = _status.raw;
280 }
281
287 {
288 if (sizeof(l4virtio_config_queue_t) * num_queues + _qoffset > L4_PAGESIZE)
289 // too many queues does not fit into our page
290 return false;
291
292 _nqueues = num_queues;
293 reset_hdr(true);
294 return true;
295 }
296
303 l4virtio_config_queue_t volatile const *qconfig(unsigned index) const
304 {
305 if (L4_UNLIKELY(_qoffset < sizeof (l4virtio_config_hdr_t)))
306 return 0;
307
308 if (L4_UNLIKELY(index >= _nqueues))
309 return 0;
310
311 return reinterpret_cast<l4virtio_config_queue_t const *>
312 (reinterpret_cast<char *>(_config.get()) + _qoffset) + index;
313 }
314
318 void reset_hdr(bool inc_generation = false) const
319 {
320 _config->magic = L4VIRTIO_MAGIC;
321 _config->version = 2;
322 _config->device = _device;
323 _config->vendor = _vendor;
324 _config->status = 0;
325 _config->irq_status = 0;
326 _config->num_queues = _nqueues;
327 _config->queues_offset = _qoffset;
328
329 memcpy(_config->dev_features_map, _host_features,
330 sizeof(_config->dev_features_map));
331 wmb();
332 if (inc_generation)
333 ++_config->generation;
334
335 }
336
345 bool reset_queue(unsigned index, unsigned num_max,
346 bool inc_generation = false) const
347 {
348 l4virtio_config_queue_t volatile *qc;
349 // this function is allowed to write to the device config
350 qc = const_cast<l4virtio_config_queue_t volatile *>(qconfig(index));
351 if (L4_UNLIKELY(qc == 0))
352 return false;
353
354 qc->num_max = num_max;
355 qc->num = 0;
356 qc->ready = 0;
357 wmb();
358 if (inc_generation)
359 ++_config->generation;
360
361 return true;
362 }
363
368 l4virtio_config_hdr_t const volatile *hdr() const
369 { return _config.get(); }
370
375 L4::Cap<L4Re::Dataspace> ds() const { return _ds.get(); }
376
382 { return _ds_offset; }
383};
384
385
386template<typename PRIV_CONFIG>
387class Dev_config_t : public Dev_config
388{
389public:
391 typedef PRIV_CONFIG Priv_config;
392
404 Dev_config_t(l4_uint32_t vendor, l4_uint32_t device,
406 : Dev_config(vendor, device, sizeof(PRIV_CONFIG), num_queues)
407 {}
408
419 Dev_config_t(L4Re::Util::Shared_cap<L4Re::Dataspace> const &cfg,
420 l4_addr_t cfg_offset, l4_uint32_t vendor, l4_uint32_t device,
422 : Dev_config(cfg, cfg_offset, vendor, device, sizeof(PRIV_CONFIG),
424 {}
425
435 Priv_config volatile *priv_config() const
436 {
437 return static_cast<Priv_config volatile *>(get_priv_config());
438 }
439
440};
441
442struct No_custom_data {};
443
449template <typename DATA>
450class Driver_mem_region_t : public DATA
451{
452public:
453 struct Flags
454 {
455 Flags() = default;
456 explicit Flags(l4_uint32_t raw) : raw(raw) {}
457
458 l4_uint32_t raw;
459 CXX_BITFIELD_MEMBER(0, 0, rw, raw);
460 };
461
462private:
465
466 l4_uint64_t _drv_base;
467 l4_uint64_t _trans_offset;
468 l4_umword_t _size;
469 Flags _flags;
470
471 Ds_cap _ds;
472 l4_addr_t _ds_offset;
473
476
477 template<typename T>
478 T _local(l4_uint64_t addr) const
479 {
480 return reinterpret_cast<T>(addr - _trans_offset);
481 }
482
483public:
485 Driver_mem_region_t() : _size(0) {}
486
499 l4_addr_t offset, Ds_cap &&ds)
500 : _drv_base(l4_trunc_page(drv_base)), _size(0), _flags(0),
501 _ds_offset(l4_trunc_page(offset))
502 {
503 using L4Re::chksys;
504 using L4Re::Env;
505
507 // Sometimes we work with dataspaces that do not implement all dataspace
508 // methods and return an error instead. An example of such a dataspace is
509 // io's Vi::System_bus. We detect this case when the info method returns
510 // -L4_ENOSYS and simply assume the dataspace is good for us.
511 long err = ds->info(&ds_info);
512 if (err >= 0)
513 {
514 l4_addr_t ds_size = l4_round_page(ds_info.size);
515
516 if (ds_size < L4_PAGESIZE)
517 chksys(-L4_EINVAL, "DS too small");
518
519 if (_ds_offset >= ds_size)
520 chksys(-L4_ERANGE, "offset larger than DS size");
521
523 if (size > ds_size)
524 chksys(-L4_EINVAL, "size larger than DS size");
525
526 if (_ds_offset > ds_size - size)
527 chksys(-L4_EINVAL, "invalid offset or size");
528
529 // overflow check
530 if ((ULLONG_MAX - size) < _drv_base)
531 chksys(-L4_EINVAL, "invalid size");
532
533 _flags.rw() = (ds_info.flags & L4Re::Dataspace::F::W).raw != 0;
534 }
535 else if (err == -L4_ENOSYS)
536 {
537 _flags.rw() = true;
538 }
539 else
540 {
541 chksys(err, "getting data-space infos");
542 }
543
545 if (_flags.rw())
546 f |= L4Re::Rm::F::W;
547
548 // use a big alignment to save PT/TLB entries and kernel memory resources!
549 chksys(Env::env()->rm()->attach(&_local_base, size, f,
550 L4::Ipc::make_cap(ds.get(), _flags.rw()
553 _ds_offset, L4_SUPERPAGESHIFT));
554
555 _size = size;
556 _ds = cxx::move(ds);
557 _trans_offset = _drv_base - _local_base.get();
558 }
559
561 bool is_writable() const { return _flags.rw(); }
562
564 Flags flags() const { return _flags; }
565
567 bool empty() const
568 { return _size == 0; }
569
571 l4_uint64_t drv_base() const { return _drv_base; }
572
574 l4_addr_t local_base() const { return _local_base.get(); }
575
577 l4_umword_t size() const { return _size; }
578
580 l4_addr_t ds_offset() const { return _ds_offset; }
581
583 L4::Cap<L4Re::Dataspace> ds() const { return _ds.get(); }
584
593 {
594 if (base < _drv_base)
595 return false;
596
597 if (base > _drv_base + _size - 1)
598 return false;
599
600 if (size > _size)
601 return false;
602
603 if (base - _drv_base > _size - size)
604 return false;
605
606 return true;
607 }
608
615 template<typename T>
616 T *local(Ptr<T> p) const
617 { return _local<T*>(p.get()); }
618};
619
620typedef Driver_mem_region_t<No_custom_data> Driver_mem_region;
621
628template <typename DATA>
630{
631public:
632 typedef Driver_mem_region_t<DATA> Mem_region;
633
634private:
635 cxx::unique_ptr<Mem_region[]> _l;
636 unsigned _max;
637 unsigned _free;
638
639public:
642
644 Driver_mem_list_t() : _max(0), _free(0) {}
645
650 void init(unsigned max)
651 {
652 _l = cxx::make_unique<Driver_mem_region_t<DATA>[]>(max);
653 _max = max;
654 _free = 0;
655 }
656
658 bool full() const
659 { return _free == _max; }
660
669 Mem_region const *add(l4_uint64_t drv_base, l4_umword_t size,
670 l4_addr_t offset, Ds_cap &&ds)
671 {
672 if (full())
674
675 _l[_free++] = Mem_region(drv_base, size, offset, cxx::move(ds));
676 return &_l[_free - 1];
677 }
678
683 void remove(Mem_region const *r)
684 {
685 if (r < &_l[0] || r >= &_l[_free])
687
688 unsigned idx = r - &_l[0];
689
690 for (unsigned i = idx + 1; i < _free - 1; ++i)
691 _l[i] = cxx::move(_l[i + 1]);
692
693 _l[--_free] = Mem_region();
694 }
695
703 Mem_region *find(l4_uint64_t base, l4_umword_t size) const
704 {
705 return _find(base, size);
706 }
707
717 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
718 Virtqueue::Desc const **table) const
719 {
720 Mem_region const *r = find(desc.addr.get(), desc.len);
721 if (L4_UNLIKELY(!r))
723
724 *table = static_cast<Virtqueue::Desc const *>(r->local(desc.addr));
725 }
726
737 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
738 Mem_region const **data) const
739 {
740 Mem_region const *r = find(desc.addr.get(), desc.len);
741 if (L4_UNLIKELY(!r))
743
744 *data = r;
745 }
746
763 template<typename ARG>
764 void load_desc(Virtqueue::Desc const &desc, Request_processor const *p,
765 ARG *data) const
766 {
767 Mem_region *r = find(desc.addr.get(), desc.len);
768 if (L4_UNLIKELY(!r))
770
771 *data = ARG(r, desc, p);
772 }
773
774private:
775 Mem_region *_find(l4_uint64_t base, l4_umword_t size) const
776 {
777 for (unsigned i = 0; i < _free; ++i)
778 if (_l[i].contains(base, size))
779 return &_l[i];
780 return 0;
781 }
782
783
784};
785
786typedef Driver_mem_list_t<No_custom_data> Driver_mem_list;
787
794template<typename DATA>
796{
797public:
798 typedef Driver_mem_list_t<DATA> Mem_list;
799
800protected:
801 Mem_list _mem_info;
802
803private:
804 Dev_config *_device_config;
805
806 using Ds_vector = std::vector<L4::Cap<L4Re::Dataspace>>;
808 std::shared_ptr<Ds_vector const> _trusted_ds_caps;
809
811 bool _trusted_ds_validation_enabled = false;
812
813public:
814 L4_RPC_LEGACY_DISPATCH(L4virtio::Device);
815 template<typename IOS> int virtio_dispatch(unsigned r, IOS &ios)
816 { return dispatch(r, ios); }
817
819 virtual void reset() = 0;
820
822 virtual bool check_features()
823 { return true; }
824
826 virtual bool check_queues() = 0;
827
829 virtual int reconfig_queue(unsigned idx) = 0;
830
832 virtual void cfg_changed(unsigned /* reg */) {};
833
836 { L4Re::chksys(-L4_ENOSYS, "Legacy single IRQ interface not implemented."); }
837
839 virtual void trigger_driver_config_irq() = 0;
840
843 {
844 L4Re::chksys(-L4_ENOSYS, "Legacy single IRQ interface not implemented.");
845 return L4::Cap<L4::Irq>();
846 }
847
854 virtual void register_driver_irq(unsigned idx)
855 {
856 if (idx != 0)
857 L4Re::chksys(-L4_ENOSYS, "Multi IRQ interface not implemented.");
858
860 }
861
869 {
870 if (idx != 0)
871 L4Re::chksys(-L4_ENOSYS, "Multi IRQ interface not implemented.");
872
873 return device_notify_irq();
874 }
875
877 virtual unsigned num_events_supported() const
878 { return 1; }
879
880 virtual L4::Ipc_svr::Server_iface *server_iface() const = 0;
881
885 Device_t(Dev_config *dev_config)
886 : _device_config(dev_config)
887 {}
888
892 Mem_list const *mem_info() const
893 { return &_mem_info; };
894
895 long op_set_status(L4virtio::Device::Rights, unsigned status)
896 { return _set_status(status); }
897
898 long op_config_queue(L4virtio::Device::Rights, unsigned queue)
899 {
900 Dev_config::Status status = _device_config->status();
901 if (status.fail_state() || !status.acked() || !status.driver())
902 return -L4_EIO;
903
904 return reconfig_queue(queue);
905 }
906
907 long op_register_ds(L4virtio::Device::Rights,
908 L4::Ipc::Snd_fpage ds_cap_fp, l4_uint64_t ds_base,
909 l4_umword_t offset, l4_umword_t sz)
910 {
911 L4Re::Util::Dbg()
912 .printf("Registering dataspace from 0x%llx with %lu KiB, offset 0x%lx\n",
913 ds_base, sz >> 10, offset);
914
915 _check_n_init_shm(ds_cap_fp, ds_base, sz, offset);
916
917 return 0;
918 }
919
920 long op_device_config(L4virtio::Device::Rights,
922 l4_addr_t &ds_offset)
923 {
924 L4Re::Util::Dbg()
925 .printf("register client: host IRQ: %lx config DS: %lx\n",
926 device_notify_irq().cap(), _device_config->ds().cap());
927
928 config_ds = L4::Ipc::make_cap(_device_config->ds(), L4_CAP_FPAGE_RW);
929 ds_offset = _device_config->ds_offset();
930 return 0;
931 }
932
933 long op_device_notification_irq(L4virtio::Device::Rights,
934 unsigned idx,
936 {
937 auto cap = device_notify_irq(idx);
938
939 if (!cap.is_valid())
940 return -L4_EINVAL;
941
943 return L4_EOK;
944 }
945
946 int op_bind(L4::Icu::Rights, l4_umword_t idx, L4::Ipc::Snd_fpage irq_cap_fp)
947 {
948 if (idx >= num_events_supported())
949 return -L4_ERANGE;
950
951 if (!irq_cap_fp.cap_received())
952 return -L4_EINVAL;
953
955
956 return L4_EOK;
957 }
958
959 int op_unbind(L4::Icu::Rights, l4_umword_t, L4::Ipc::Snd_fpage)
960 {
961 return -L4_ENOSYS;
962 }
963
964 int op_info(L4::Icu::Rights, L4::Icu::_Info &info)
965 {
966 info.features = 0;
967 info.nr_irqs = num_events_supported();
968 info.nr_msis = 0;
969
970 return L4_EOK;
971 }
972
973 int op_msi_info(L4::Icu::Rights, l4_umword_t, l4_uint64_t, l4_icu_msi_info_t &)
974 { return -L4_ENOSYS; }
975
976 int op_mask(L4::Icu::Rights, l4_umword_t)
977 { return -L4_ENOSYS; }
978
979 int op_unmask(L4::Icu::Rights, l4_umword_t)
980 { return -L4_ENOREPLY; }
981
982 int op_set_mode(L4::Icu::Rights, l4_umword_t, l4_umword_t)
983 { return -L4_ENOSYS; }
984
996 void reset_queue_config(unsigned idx, unsigned num_max,
997 bool inc_generation = false)
998 {
999 _device_config->reset_queue(idx, num_max, inc_generation);
1000 }
1001
1006 void init_mem_info(unsigned num)
1007 {
1008 _mem_info.init(num);
1009 }
1010
1019 {
1020 reset();
1021 _device_config->set_device_needs_reset();
1022
1023 // the device MUST NOT notify the driver before DRIVER_OK.
1024 if (_device_config->status().driver_ok())
1026 }
1027
1041 bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
1042 {
1043 l4virtio_config_queue_t volatile const *qc;
1044 qc = _device_config->qconfig(qn);
1045 if (L4_UNLIKELY(qc == 0))
1046 return false;
1047
1048 if (!qc->ready)
1049 {
1050 q->disable();
1051 return true;
1052 }
1053
1054 // read to local variables before check
1055 l4_uint32_t num = qc->num;
1056 l4_uint64_t desc = qc->desc_addr;
1057 l4_uint64_t avail = qc->avail_addr;
1058 l4_uint64_t used = qc->used_addr;
1059
1060 if (0)
1061 printf("%p: setup queue: num=0x%x max_num=0x%x desc=0x%llx avail=0x%llx used=0x%llx\n",
1062 this, num, num_max, desc, avail, used);
1063
1064 if (!num || num > num_max)
1065 return false;
1066
1067 // num must be power of two
1068 if (num & (num - 1))
1069 return false;
1070
1071 if (desc & 0xf)
1072 return false;
1073
1074 if (avail & 0x1)
1075 return false;
1076
1077 if (used & 0x3)
1078 return false;
1079
1080 auto const *desc_info = _mem_info.find(desc, Virtqueue::desc_size(num));
1081 if (L4_UNLIKELY(!desc_info))
1082 return false;
1083
1084 auto const *avail_info = _mem_info.find(avail, Virtqueue::avail_size(num));
1085 if (L4_UNLIKELY(!avail_info))
1086 return false;
1087
1088 auto const *used_info = _mem_info.find(used, Virtqueue::used_size(num));
1089 if (L4_UNLIKELY(!used_info || !used_info->is_writable()))
1090 return false;
1091
1092 L4Re::Util::Dbg()
1093 .printf("shm=[%llx-%llx] local=[%lx-%lx] desc=[%llx-%llx] (%p-%p)\n",
1094 desc_info->drv_base(), desc_info->drv_base() + desc_info->size() - 1,
1095 desc_info->local_base(),
1096 desc_info->local_base() + desc_info->size() - 1,
1097 desc, desc + Virtqueue::desc_size(num),
1098 desc_info->local(Ptr<char>(desc)),
1099 desc_info->local(Ptr<char>(desc)) + Virtqueue::desc_size(num));
1100
1101 L4Re::Util::Dbg()
1102 .printf("shm=[%llx-%llx] local=[%lx-%lx] avail=[%llx-%llx] (%p-%p)\n",
1103 avail_info->drv_base(), avail_info->drv_base() + avail_info->size() - 1,
1104 avail_info->local_base(),
1105 avail_info->local_base() + avail_info->size() - 1,
1106 avail, avail + Virtqueue::avail_size(num),
1107 avail_info->local(Ptr<char>(avail)),
1108 avail_info->local(Ptr<char>(avail)) + Virtqueue::avail_size(num));
1109
1110 L4Re::Util::Dbg()
1111 .printf("shm=[%llx-%llx] local=[%lx-%lx] used=[%llx-%llx] (%p-%p)\n",
1112 used_info->drv_base(), used_info->drv_base() + used_info->size() - 1,
1113 used_info->local_base(),
1114 used_info->local_base() + used_info->size() - 1,
1115 used, used + Virtqueue::used_size(num),
1116 used_info->local(Ptr<char>(used)),
1117 used_info->local(Ptr<char>(used)) + Virtqueue::used_size(num));
1118
1119 q->setup(num, desc_info->local(Ptr<void>(desc)),
1120 avail_info->local(Ptr<void>(avail)),
1121 used_info->local(Ptr<void>(used)));
1122 return true;
1123 }
1124
1125 void check_n_init_shm(L4Re::Util::Unique_cap<L4Re::Dataspace> &&shm,
1126 l4_uint64_t base, l4_umword_t size, l4_addr_t offset)
1127 {
1128 if (_mem_info.full())
1130
1131 auto const *i = _mem_info.add(base, size, offset, cxx::move(shm));
1132 L4Re::Util::Dbg()
1133 .printf("PORT[%p]: DMA guest [%llx-%llx] local [%lx-%lx] offset %lx\n",
1134 this, i->drv_base(), i->drv_base() + i->size() - 1,
1135 i->local_base(),
1136 i->local_base() + i->size() - 1,
1137 i->ds_offset());
1138 }
1139
1149 {
1150 l4_uint32_t cmd = _device_config->get_cmd();
1151 if (L4_LIKELY(!(cmd & L4VIRTIO_CMD_MASK)))
1152 return false;
1153
1154 switch (cmd & L4VIRTIO_CMD_MASK)
1155 {
1157 _set_status(cmd & ~L4VIRTIO_CMD_MASK);
1158 break;
1159
1162 break;
1163
1166 break;
1167
1168 default:
1169 // unknown command
1170 break;
1171 }
1172
1173 _device_config->reset_cmd();
1174
1175 return true;
1176 }
1177
1182 {
1183 _trusted_ds_validation_enabled = true;
1184 }
1185
1191 void
1192 add_trusted_dataspaces(std::shared_ptr<Ds_vector const> ds)
1193 {
1194 _trusted_ds_caps = ds;
1195 }
1196
1197
1198private:
1210 long validate_ds(L4::Cap<L4Re::Dataspace> ds)
1211 {
1212 if (!_trusted_ds_caps)
1213 return -L4_EINVAL;
1214 if (std::any_of(_trusted_ds_caps->cbegin(), _trusted_ds_caps->cend(),
1215 [&ds](L4::Cap<L4Re::Dataspace> cap)
1216 {
1217 return L4Re::Env::env()->task()
1218 ->cap_equal(ds, cap).label() == 1;
1219 }
1220 ))
1221 {
1222 return L4_EOK;
1223 }
1224 return -L4_EINVAL;
1225 }
1226
1227 void _check_n_init_shm(L4::Ipc::Snd_fpage shm_cap_fp,
1228 l4_uint64_t base, l4_umword_t size, l4_addr_t offset)
1229 {
1230 if (!shm_cap_fp.cap_received())
1232
1234 L4Re::chkcap(server_iface()->template rcv_cap<L4Re::Dataspace>(0)));
1235 L4Re::chksys(server_iface()->realloc_rcv_cap(0));
1236
1237 if (_trusted_ds_validation_enabled)
1238 L4Re::chksys(validate_ds(ds.get()), "Validating the dataspace.");
1239
1240 check_n_init_shm(cxx::move(ds), base, size, offset);
1241 }
1242
1243 bool check_features_internal()
1244 {
1245 static_assert(sizeof(l4virtio_config_hdr_t::driver_features_map)
1246 == sizeof(l4virtio_config_hdr_t::dev_features_map),
1247 "Driver and device feature maps must be of the same size");
1248
1249 // From the Virtio 1.0 specification 6.1 Driver Requirements and 6.2 Device
1250 // Requirements: A driver MUST accept VIRTIO_F_VERSION_1 if it is offered.
1251 // A device MUST offer VIRTIO_F_VERSION_1. A device MAY fail to operate
1252 // further if VIRTIO_F_VERSION_1 is not accepted.
1253 //
1254 // The L4virtio implementation does not support legacy interfaces so we
1255 // fail here if the Virtio 1.0 feature was not accepted.
1256 if (!_device_config->get_guest_feature(L4VIRTIO_FEATURE_VERSION_1))
1257 return false;
1258
1259 for (auto i = 0u;
1260 i < sizeof(l4virtio_config_hdr_t::driver_features_map)
1261 / sizeof(l4virtio_config_hdr_t::driver_features_map[0]);
1262 i++)
1263 {
1264 // Driver must not accept features that were not offered by device
1265 if (_device_config->guest_features(i)
1266 & ~_device_config->host_features(i))
1267 return false;
1268 }
1269 return check_features();
1270 }
1271
1293 void check_and_update_status(Dev_config::Status status)
1294 {
1295 // snapshot of current status
1296 Dev_config::Status current_status = _device_config->status();
1297
1298 // handle reset
1299 if (!status.raw)
1300 {
1301 _device_config->set_status(status);
1302 return;
1303 }
1304
1305 // Do no further processing in case of driver or device failure. If FAILED
1306 // or DEVICE_NEEDS_RESET are set only these fail_state bits will be set in
1307 // addition to the current status bits already set.
1308 if (current_status.fail_state() || status.fail_state())
1309 {
1310 if (current_status.fail_state() != status.fail_state())
1311 {
1312 current_status.fail_state() =
1313 current_status.fail_state() | status.fail_state();
1314 _device_config->set_status(current_status);
1315 }
1316 return;
1317 }
1318
1319 // Enforce init sequence ACKNOWLEDGE, DRIVER, FEATURES_OK, DRIVER_OK.
1320 // We do not enforce that only one additional new bit is set per call.
1321 if ((!status.acked() && status.driver())
1322 || (!status.driver() && status.features_ok())
1323 || (!status.features_ok() && status.driver_ok()))
1324 {
1325 current_status.device_needs_reset() = 1;
1326 _device_config->set_status(current_status);
1327 return;
1328 }
1329
1330 // only check feature compatibility before DRIVER_OK is set
1331 if (status.features_ok() && !status.driver_ok()
1332 && !check_features_internal())
1333 status.features_ok() = 0;
1334
1335 // Note that if FEATURES_OK and DRIVER_OK are both updated to being set
1336 // at the same time the above check_features_internal() is skipped; this is
1337 // considered undefined behaviour but it is not prevented.
1338 if (status.running() && !check_queues())
1339 {
1340 current_status.device_needs_reset() = 1;
1341 _device_config->set_status(current_status);
1342 return;
1343 }
1344
1345 _device_config->set_status(status);
1346 }
1347
1360 int _set_status(unsigned new_status)
1361 {
1362 if (new_status == 0)
1363 {
1364 L4Re::Util::Dbg().printf("Resetting device\n");
1365 reset();
1366 _device_config->reset_hdr(true);
1367 }
1368
1369 Dev_config::Status status(new_status);
1370 check_and_update_status(status);
1371
1372 return 0;
1373 }
1374
1375};
1376
1377typedef Device_t<No_custom_data> Device;
1378
1379} // namespace Svr
1380
1381}
Interface for memory-like objects.
Definition dataspace:64
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:435
T get() const noexcept
Return the address.
Definition rm:508
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
C++ interface for capabilities.
Definition capability.h:219
Capability type for RPC interfaces (see L4::Cap<T>).
Definition ipc_types:563
bool cap_received() const noexcept
Check if at least one capability has been mapped.
Definition ipc_types:365
Send flex-page.
Definition ipc_types:408
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:55
l4_uint64_t get() const
Definition virtqueue:69
Abstraction for L4-Virtio device config memory.
Definition l4virtio:53
void set_status(Status status)
Set device status register.
Definition l4virtio:253
bool reset_queue(unsigned index, unsigned num_max, bool inc_generation=false) const
Reset queue config for the given queue.
Definition l4virtio:345
l4virtio_config_hdr_t const volatile * hdr() const
Get a read-only pointer to the config header.
Definition l4virtio:368
l4_uint32_t get_cmd() const
Get the value from the cmd register.
Definition l4virtio:230
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:286
void add_irq_status(l4_uint32_t status)
Adds irq status bit.
Definition l4virtio:265
L4::Cap< L4Re::Dataspace > ds() const
Get data-space capability for the shared config data space.
Definition l4virtio:375
Status status() const
Get current device status (trusted).
Definition l4virtio:222
void reset_cmd()
Reset the cmd register after execution of a command.
Definition l4virtio:241
l4_uint32_t num_queues() const
Return the number of queues currently usable.
Definition l4virtio:184
l4virtio_config_queue_t volatile const * qconfig(unsigned index) const
Get queue read-only config data for queue with the given index.
Definition l4virtio:303
void reset_hdr(bool inc_generation=false) const
Reset the config header to the initial contents.
Definition l4virtio:318
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:198
l4_uint32_t negotiated_features(unsigned idx) const
Compute a specific set of negotiated features.
Definition l4virtio:212
l4_addr_t ds_offset() const
Return the offset into the config dataspace where the device configuration starts.
Definition l4virtio:381
void set_device_needs_reset()
Set DEVICE_NEEDS_RESET bit in device status register.
Definition l4virtio:276
Server-side L4-VIRTIO device stub.
Definition l4virtio:796
void device_error()
Transition device into DEVICE_NEEDS_RESET state.
Definition l4virtio:1018
virtual void cfg_changed(unsigned)
callback for client device configuration changes
Definition l4virtio:832
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:1192
virtual void reset()=0
reset callback, called for doing a device reset
virtual void trigger_driver_config_irq()=0
callback for triggering configuration change notification IRQ
bool handle_mem_cmd_write()
Check for a value in the cmd register and handle a write.
Definition l4virtio:1148
Mem_list _mem_info
Memory region list.
Definition l4virtio:801
virtual void register_driver_irq(unsigned idx)
Callback for registering an notification IRQ (multi IRQ).
Definition l4virtio:854
Device_t(Dev_config *dev_config)
Make a device for the given config.
Definition l4virtio:885
virtual void register_single_driver_irq()
callback for registering a single guest IRQ for all queues (old-style)
Definition l4virtio:835
virtual bool check_features()
callback for checking the subset of accepted features
Definition l4virtio:822
virtual unsigned num_events_supported() const
Return the highest notification index supported.
Definition l4virtio:877
bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
Enable/disable the specified queue.
Definition l4virtio:1041
void init_mem_info(unsigned num)
Initialize the memory region list to the given maximum.
Definition l4virtio:1006
Mem_list const * mem_info() const
Get the memory region list used for this device.
Definition l4virtio:892
virtual int reconfig_queue(unsigned idx)=0
callback for client queue-config request
void enable_trusted_ds_validation()
Enable trusted dataspace validation.
Definition l4virtio:1181
virtual L4::Cap< L4::Irq > device_notify_irq(unsigned idx)
Callback to gather the device notification IRQ (multi IRQ).
Definition l4virtio:868
virtual L4::Cap< L4::Irq > device_notify_irq() const
callback to gather the device notification IRQ (old-style)
Definition l4virtio:842
void reset_queue_config(unsigned idx, unsigned num_max, bool inc_generation=false)
Trigger reset for the configuration space for queue idx.
Definition l4virtio:996
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:630
Mem_region * find(l4_uint64_t base, l4_umword_t size) const
Find memory region containing the given driver address region.
Definition l4virtio:703
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:717
void remove(Mem_region const *r)
Remove the given region from the list.
Definition l4virtio:683
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:669
L4Re::Util::Unique_cap< L4Re::Dataspace > Ds_cap
type for storing a data-space capability internally
Definition l4virtio:641
void init(unsigned max)
Make a fresh list with capacity max.
Definition l4virtio:650
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:737
void load_desc(Virtqueue::Desc const &desc, Request_processor const *p, ARG *data) const
Default implementation returning generic information.
Definition l4virtio:764
Driver_mem_list_t()
Make an empty, zero capacity list.
Definition l4virtio:644
Region of driver memory, that shall be managed locally.
Definition l4virtio:451
bool contains(l4_uint64_t base, l4_umword_t size) const
Test if the given driver address range is within this region.
Definition l4virtio:592
l4_addr_t ds_offset() const
Definition l4virtio:580
Driver_mem_region_t()
Make default empty memory region.
Definition l4virtio:485
l4_umword_t size() const
Definition l4virtio:577
l4_addr_t local_base() const
Definition l4virtio:574
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:498
L4::Cap< L4Re::Dataspace > ds() const
Definition l4virtio:583
l4_uint64_t drv_base() const
Definition l4virtio:571
T * local(Ptr< T > p) const
Get the local address for driver address p.
Definition l4virtio:616
Encapsulate the state for processing a VIRTIO request.
Definition virtio:454
Virtqueue implementation for the device.
Definition virtio:88
Descriptor in the descriptor table.
Definition virtqueue:94
l4_uint32_t len
Length of described buffer.
Definition virtqueue:116
Ptr< void > addr
Address stored in descriptor.
Definition virtqueue:115
void disable()
Completely disable the queue.
Definition virtqueue:230
void setup(unsigned num, void *desc, void *avail, void *used)
Enable this queue.
Definition virtqueue:355
static unsigned long desc_size(unsigned num)
Calculate the size of the descriptor table for num entries.
Definition virtqueue:267
static unsigned long used_size(unsigned num)
Calculate the size of the used ring for num entries.
Definition virtqueue:304
static unsigned long avail_size(unsigned num)
Calculate the size of the available ring for num entries.
Definition virtqueue:285
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:59
@ L4_ENOSYS
No sys.
Definition err.h:61
@ L4_EINVAL
Invalid argument.
Definition err.h:57
@ L4_ENOREPLY
No reply.
Definition err.h:66
@ 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:296
#define L4_LIKELY(x)
Expression is likely to execute.
Definition compiler.h:295
void * l4virtio_device_config(l4virtio_config_hdr_t const *cfg)
Get the pointer to the device configuration.
Definition virtio.h:260
void l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
Clear the given feature bit in a feature map.
Definition virtio.h:281
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
Definition virtio.h:269
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:293
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition virtio.h:98
@ L4VIRTIO_CMD_SET_STATUS
Set the status register.
Definition virtio.h:119
@ L4VIRTIO_CMD_CFG_QUEUE
Configure a queue.
Definition virtio.h:120
@ L4VIRTIO_CMD_MASK
Mask to get command bits.
Definition virtio.h:123
@ L4VIRTIO_CMD_CFG_CHANGED
Device config changed.
Definition virtio.h:121
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:649
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:659
L4-VIRTIO Transport C++ API.
Definition l4virtio:26
Region mapper interface.
@ W
Request write-only mapping.
Definition dataspace:90
Information about the dataspace.
Definition dataspace:137
@ RW
Readable and writable region.
Definition rm:150
@ R
Readable region.
Definition rm:144
@ W
Writable region.
Definition rm:146
@ Search_addr
Search for a suitable address range.
Definition rm:125
Exception used by Queue to indicate descriptor errors.
Definition virtio:379
@ Bad_address
Address cannot be translated.
Definition virtio:383
Type for device feature bitmap.
Definition virtio:67
Type of the device status register.
Definition virtio:33
constexpr device_needs_reset_bfm_t::Val device_needs_reset() const
Get the device_needs_reset bits ( 6 to 6 ) of raw.
Definition virtio:45
constexpr 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:130
l4_uint32_t vendor
vendor ID
Definition virtio.h:135
l4_uint32_t magic
magic value (must be 'virt').
Definition virtio.h:132
l4_uint32_t queues_offset
offset of virtqueue config array
Definition virtio.h:148
l4_uint32_t device
device ID
Definition virtio.h:134
l4_uint32_t num_queues
number of virtqueues
Definition virtio.h:147
l4_uint32_t version
VIRTIO version.
Definition virtio.h:133
Queue configuration entry.
Definition virtio.h:221
l4_uint64_t avail_addr
W: address of available ring.
Definition virtio.h:234
l4_uint64_t desc_addr
W: address of descriptor table.
Definition virtio.h:233
l4_uint64_t used_addr
W: address of used ring.
Definition virtio.h:235
l4_uint16_t ready
RW: queue ready flag (read-write)
Definition virtio.h:228
l4_uint16_t num_max
R: maximum number of descriptors supported by this queue.
Definition virtio.h:223
l4_uint16_t num
RW: number of descriptors configured for this queue.
Definition virtio.h:225
Capability allocator.
Shared_cap / Shared_del_cap.
Unique_cap / Unique_del_cap.