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) 2015-2020, 2022 Kernkonzept GmbH.
5 * Author(s): Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
6 *
7 */
8#pragma once
9
10#include <l4/sys/factory>
11#include <l4/sys/semaphore>
12#include <l4/re/dataspace>
13#include <l4/re/env>
14#include <l4/re/util/unique_cap>
15#include <l4/re/util/object_registry>
16#include <l4/re/error_helper>
17
18#include <l4/util/atomic.h>
19#include <l4/util/bitops.h>
20#include <l4/l4virtio/l4virtio>
21#include <l4/l4virtio/virtqueue>
22#include <l4/sys/consts.h>
23
24#include <cstring>
25
26namespace L4virtio { namespace Driver {
27
31class Device
32{
33public:
56 void driver_connect(L4::Cap<L4virtio::Device> srvcap, bool manage_notify = true)
57 {
58 _device = srvcap;
59
60 _next_devaddr = L4_SUPERPAGESIZE;
61
62 auto *e = L4Re::Env::env();
63
64 // Set up the virtio configuration page.
65
66 _config_cap = L4Re::chkcap(L4Re::Util::make_unique_cap<L4Re::Dataspace>(),
67 "Allocate config dataspace capability");
68
69 l4_addr_t ds_offset;
70 L4Re::chksys(_device->device_config(_config_cap.get(), &ds_offset),
71 "Request virtio config page");
72
73 if (ds_offset & ~L4_PAGEMASK)
74 L4Re::chksys(-L4_EINVAL, "Virtio config page is page aligned.");
75
76 L4Re::chksys(e->rm()->attach(&_config, L4_PAGESIZE,
78 L4::Ipc::make_cap_rw(_config_cap.get()), ds_offset,
80 "Attach config dataspace");
81
82 if (memcmp(&_config->magic, "virt", 4) != 0)
83 L4Re::chksys(-L4_ENODEV, "Device config has wrong magic value");
84
85 if (_config->version != 2)
86 L4Re::chksys(-L4_ENODEV, "Invalid virtio version, must be 2");
87
88 _device->set_status(0); // reset
89 int status = L4VIRTIO_STATUS_ACKNOWLEDGE;
90 _device->set_status(status);
91
92 status |= L4VIRTIO_STATUS_DRIVER;
93 _device->set_status(status);
94
95 if (_config->fail_state())
96 L4Re::chksys(-L4_EIO, "Device failure during initialisation.");
97
98 // Set up the interrupt used to notify the device about events.
99 // (only supporting one interrupt with index 0 at the moment)
100
101 _host_irq = L4Re::chkcap(L4Re::Util::make_unique_cap<L4::Irq>(),
102 "Allocate host IRQ capability");
103
104 L4Re::chksys(_device->device_notification_irq(0, _host_irq.get()),
105 "Request device notification interrupt.");
106
107 // Set up the interrupt to get notifications from the device.
108 // (only supporting one interrupt with index 0 at the moment)
109 if (manage_notify)
110 {
111 _driver_notification =
112 L4Re::chkcap(L4Re::Util::make_unique_cap<L4::Semaphore>(),
113 "Allocate notification capability");
114
115 L4Re::chksys(l4_error(e->factory()->create(_driver_notification.get())),
116 "Create semaphore for notifications from device");
117
118 L4Re::chksys(_device->bind(0, _driver_notification.get()),
119 "Bind driver notification interrupt");
120 }
121 }
122
129 int bind_notification_irq(unsigned index, L4::Cap<L4::Triggerable> irq) const
130 { return l4_error(_device->bind(index, irq)); }
131
133 bool fail_state() const { return _config->fail_state(); }
134
145 bool feature_negotiated(unsigned int feat) const
146 { return l4virtio_get_feature(_config->driver_features_map, feat); }
147
157 {
158 if (!l4virtio_get_feature(_config->dev_features_map,
161 "Require Virtio 1.0 device; Legacy device not supported.");
162
163 _config->driver_features_map[0] &= _config->dev_features_map[0];
164 _config->driver_features_map[1] &= _config->dev_features_map[1];
165
166 _device->set_status(_config->status | L4VIRTIO_STATUS_FEATURES_OK);
167
168 if (!(_config->status & L4VIRTIO_STATUS_FEATURES_OK))
169 L4Re::chksys(-L4_EINVAL, "Negotiation of device features.");
170
171 _device->set_status(_config->status | L4VIRTIO_STATUS_DRIVER_OK);
172
173 if (_config->fail_state())
174 return -L4_EIO;
175
176 return L4_EOK;
177 }
178
197 l4_umword_t size, l4_uint64_t *devaddr)
198 {
199 *devaddr = next_device_address(size);
200 return _device->register_ds(L4::Ipc::make_cap_rw(ds), *devaddr, offset, size);
201 }
202
212 int config_queue(int num, unsigned size, l4_uint64_t desc_addr,
213 l4_uint64_t avail_addr, l4_uint64_t used_addr)
214 {
215 auto *queueconf = &_config->queues()[num];
216 queueconf->num = size;
217 queueconf->desc_addr = desc_addr;
218 queueconf->avail_addr = avail_addr;
219 queueconf->used_addr = used_addr;
220 queueconf->ready = 1;
221
222 return _device->config_queue(num);
223 }
224
230 int max_queue_size(int num) const
231 {
232 return _config->queues()[num].num_max;
233 }
234
248 {
249 send(queue, descno);
250
251 // wait for a reply, we assume that no other
252 // request will get in the way.
253 auto head = wait_for_next_used(queue);
254
255 if (head < 0)
256 return head;
257
258 return (head == descno) ? L4_EOK : -L4_EINVAL;
259 }
260
268 int wait(int index) const
269 {
270 if (index != 0)
271 return -L4_EEXIST;
272
273 return l4_ipc_error(_driver_notification->down(), l4_utcb());
274 }
275
291 int wait_for_next_used(Virtqueue &queue, l4_uint32_t *len = nullptr) const
292 {
293 while (true)
294 {
295 int err = wait(0);
296
297 if (err < 0)
298 return err;
299
300 auto head = queue.find_next_used(len);
301 if (head != Virtqueue::Eoq) // spurious interrupt?
302 return head;
303 }
304 }
305
312 void send(Virtqueue &queue, l4_uint16_t descno)
313 {
314 queue.enqueue_descriptor(descno);
315 notify(queue);
316 }
317
318 void notify(Virtqueue &queue)
319 {
320 if (!queue.no_notify_host())
321 _host_irq->trigger();
322 }
323
324private:
335 l4_uint64_t next_device_address(l4_umword_t size)
336 {
337 l4_umword_t ret;
338 size = l4_round_page(size);
339 do
340 {
341 ret = _next_devaddr;
342 if (l4_umword_t(~0) - ret < size)
343 L4Re::chksys(-L4_ENOMEM, "Out of device address space.");
344 }
345 while (!l4util_cmpxchg(&_next_devaddr, ret, ret + size));
346
347 return ret;
348 }
349
350protected:
353 l4_umword_t _next_devaddr;
354 L4Re::Util::Unique_cap<L4::Semaphore> _driver_notification;
355
356private:
359};
360
361} }
bit manipulation functions
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:103
Unique region.
Definition rm:435
C++ interface for capabilities.
Definition capability.h:219
l4_msgtag_t bind(unsigned irqnum, L4::Cap< Triggerable > irq, l4_utcb_t *utcb=l4_utcb()) noexcept
Bind an interrupt line of an interrupt controller to an interrupt object.
Definition irq:316
long register_ds(L4::Ipc::Cap< L4Re::Dataspace > ds_cap, l4_uint64_t base, l4_umword_t offset, l4_umword_t size)
Register a shared data space with VIRTIO host.
long device_notification_irq(unsigned index, L4::Ipc::Out< L4::Cap< L4::Triggerable > > irq)
Get the notification interrupt corresponding to the given index.
long config_queue(unsigned queue)
Trigger queue configuration of the given queue.
long device_config(L4::Ipc::Out< L4::Cap< L4Re::Dataspace > > config_ds, l4_addr_t *ds_offset)
Get the dataspace with the L4virtio configuration page.
long set_status(unsigned status)
Write the VIRTIO status register.
Client-side implementation for a general virtio device.
Definition l4virtio:32
int wait(int index) const
Wait for a notification from the device.
Definition l4virtio:268
int max_queue_size(int num) const
Maximum queue size allowed by the device.
Definition l4virtio:230
int bind_notification_irq(unsigned index, L4::Cap< L4::Triggerable > irq) const
Register a triggerable to receive notifications from the device.
Definition l4virtio:129
int wait_for_next_used(Virtqueue &queue, l4_uint32_t *len=nullptr) const
Wait for the next item to arrive in the used queue and return it.
Definition l4virtio:291
void driver_connect(L4::Cap< L4virtio::Device > srvcap, bool manage_notify=true)
Contacts the device and starts the initial handshake.
Definition l4virtio:56
void send(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device.
Definition l4virtio:312
bool fail_state() const
Return true if the device is in a fail state.
Definition l4virtio:133
int register_ds(L4::Cap< L4Re::Dataspace > ds, l4_umword_t offset, l4_umword_t size, l4_uint64_t *devaddr)
Share a dataspace with the device.
Definition l4virtio:196
int config_queue(int num, unsigned size, l4_uint64_t desc_addr, l4_uint64_t avail_addr, l4_uint64_t used_addr)
Send the virtqueue configuration to the device.
Definition l4virtio:212
bool feature_negotiated(unsigned int feat) const
Check if a particular feature bit was negotiated with the device.
Definition l4virtio:145
int driver_acknowledge()
Finalize handshake with the device.
Definition l4virtio:156
int send_and_wait(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device and wait for it to be processed.
Definition l4virtio:247
Driver-side implementation of a Virtqueue.
Definition virtqueue:477
void enqueue_descriptor(l4_uint16_t descno)
Enqueue a descriptor in the available ring.
Definition virtqueue:580
l4_uint16_t find_next_used(l4_uint32_t *len=nullptr)
Return the next finished block.
Definition virtqueue:615
bool no_notify_host() const
Get the no notify flag of this queue.
Definition virtqueue:432
Dataspace interface.
Environment interface.
Error helper.
Common factory related definitions.
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 short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:38
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:42
@ L4_EEXIST
Already exists.
Definition err.h:54
@ L4_EINVAL
Invalid argument.
Definition err.h:57
@ L4_ENODEV
No such thing.
Definition err.h:55
@ L4_EIO
I/O error.
Definition err.h:46
@ L4_EOK
Ok.
Definition err.h:43
@ L4_ENOMEM
No memory.
Definition err.h:50
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
Definition ipc.h:657
l4_umword_t l4_ipc_error(l4_msgtag_t tag, l4_utcb_t *utcb) L4_NOTHROW
Get the IPC error code for an IPC operation.
Definition ipc.h:640
#define L4_SUPERPAGESIZE
Size of a large page.
Definition consts.h:407
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_PAGESHIFT
Size of a page, log2-based.
Definition consts.h:37
#define L4_PAGEMASK
Mask for the page number.
Definition consts.h:389
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition utcb.h:340
int l4util_cmpxchg(volatile l4_umword_t *dest, l4_umword_t cmp_val, l4_umword_t new_val)
Atomic compare and exchange (machine wide fields)
Definition atomic.h:381
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_STATUS_DRIVER
Guest OS knows how to drive device.
Definition virtio.h:87
@ L4VIRTIO_STATUS_ACKNOWLEDGE
Guest OS has found device.
Definition virtio.h:86
@ L4VIRTIO_STATUS_FEATURES_OK
Driver has acknowledged feature set.
Definition virtio.h:89
@ L4VIRTIO_STATUS_DRIVER_OK
Driver is set up.
Definition virtio.h:88
atomic operations header and generic implementations
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_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
Semaphore class definition.
@ RW
Readable and writable region.
Definition rm:150
@ Search_addr
Search for a suitable address range.
Definition rm:125
Unique_cap / Unique_del_cap.