genode/repos/os/src/server/tz_vmm/include/block_driver.h

158 lines
3.8 KiB
C
Raw Normal View History

/*
* \brief Paravirtualized access to block devices for VMs
* \author Martin Stein
* \date 2015-10-23
*/
/*
* Copyright (C) 2015-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _BLOCK_DRIVER_H_
#define _BLOCK_DRIVER_H_
/* Genode includes */
#include <block_session/connection.h>
#include <base/allocator_avl.h>
/* local includes */
#include <vm_base.h>
namespace Genode {
class Xml_node;
class Block_driver;
}
class Genode::Block_driver
{
private:
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
/*
* Noncopyable
*/
Block_driver(Block_driver const &);
Block_driver &operator = (Block_driver const &);
using Packet_descriptor = Block::Packet_descriptor;
struct Device_function_failed : Exception { };
class Request_cache
{
private:
enum { NR_OF_ENTRIES = Block::Session::TX_QUEUE_SIZE };
struct No_matching_entry : Exception { };
struct Entry { void *pkt; void *req; } _entries[NR_OF_ENTRIES];
unsigned _find(void *packet);
void _free(unsigned id) { _entries[id].pkt = 0; }
public:
struct Full : Exception { };
Request_cache() {
for (unsigned i = 0; i < NR_OF_ENTRIES; i++) { _free(i); } }
void insert(void *pkt, void *req);
void remove(void *pkt, void **req);
};
class Device
{
public:
enum { TX_BUF_SIZE = 5 * 1024 * 1024 };
using Id = Id_space<Device>::Id;
using Name = String<64>;
private:
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Request_cache _cache { };
Vm_base &_vm;
Name const _name;
unsigned const _irq;
Signal_handler<Device> _irq_handler;
Block::Connection _session;
Id_space<Device>::Element _id_space_elem;
Block::Session::Info const _info { _session.info() };
public:
void _handle_irq() { _vm.inject_irq(_irq); }
public:
struct Invalid : Exception { };
Device(Env &env,
Xml_node node,
Range_allocator &alloc,
Id_space<Device> &id_space,
Id id,
Vm_base &vm);
void start_irq_handling();
Request_cache &cache() { return _cache; }
Block::Connection &session() { return _session; }
size_t block_size() const { return _info.block_size; }
size_t block_count() const { return _info.block_count; }
bool writeable() const { return _info.writeable; }
Name const &name() const { return _name; }
unsigned irq() const { return _irq; }
};
void *_buf = nullptr;
size_t _buf_size = 0;
Id_space<Device> _devs;
unsigned _dev_count = 0;
Allocator_avl _dev_alloc;
void _buf_to_pkt(void *dst, size_t sz);
void _name(Vm_base &vm);
void _block_count(Vm_base &vm);
void _block_size(Vm_base &vm);
void _queue_size(Vm_base &vm);
void _writeable(Vm_base &vm);
void _irq(Vm_base &vm);
void _buffer(Vm_base &vm);
void _device_count(Vm_base &vm);
void _new_request(Vm_base &vm);
void _submit_request(Vm_base &vm);
void _collect_reply(Vm_base &vm);
template <typename DEV_FUNC, typename ERR_FUNC>
void _dev_apply(Device::Id id,
DEV_FUNC const &dev_func,
ERR_FUNC const &err_func)
{
try { _devs.apply<Device>(id, [&] (Device &dev) { dev_func(dev); }); }
catch (Id_space<Device>::Unknown_id) {
error("unknown block device ", id);
err_func();
}
catch (Device_function_failed) { err_func(); }
}
public:
void handle_smc(Vm_base &vm);
Block_driver(Env &env,
Xml_node config,
Allocator &alloc,
Vm_base &vm);
};
#endif /* _BLOCK_DRIVER_H_ */