genode/repos/os/src/server/ram_block/main.cc

216 lines
5.3 KiB
C++
Raw Normal View History

/*
* \brief Provide a RAM dataspace as writable block device
* \author Stefan Kalkowski
* \author Sebastian Sumpf
* \author Josef Soentgen
* \date 2010-07-07
*/
/*
* Copyright (C) 2010-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.
*/
/* Genode includes */
#include <base/attached_ram_dataspace.h>
#include <base/attached_rom_dataspace.h>
#include <base/component.h>
#include <base/exception.h>
#include <base/heap.h>
#include <base/log.h>
#include <block/component.h>
#include <block/driver.h>
using namespace Genode;
class Ram_block : public 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
*/
Ram_block(Ram_block const &);
Ram_block &operator = (Ram_block const &);
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
Env &_env;
Allocator *_alloc { nullptr };
Attached_rom_dataspace *_rom_ds { nullptr };
size_t _size;
size_t _block_size;
size_t _block_count;
Attached_ram_dataspace _ram_ds;
addr_t _ram_addr;
void _io(Block::sector_t block_number,
size_t block_count,
char* buffer,
Block::Packet_descriptor &packet,
bool read)
{
/* sanity check block number */
if (block_number + block_count > _block_count) {
Genode::warning("requested blocks ", block_number, "-",
block_number + block_count," out of range!");
return;
}
size_t offset = (size_t) block_number * _block_size;
size_t size = block_count * _block_size;
void *src = read ? (void *)(_ram_addr + offset) : (void *)buffer;
void *dst = read ? (void *)buffer : (void *)(_ram_addr + offset);
/* copy file content to packet payload */
memcpy(dst, src, size);
ack_packet(packet);
}
public:
/**
* Construct populated RAM dataspace
*/
Ram_block(Env &env, Allocator &alloc,
const char *name, size_t block_size)
:
Block::Driver(env.ram()),
_env(env), _alloc(&alloc),
_rom_ds(new (_alloc) Attached_rom_dataspace(_env, name)),
_size(_rom_ds->size()),
_block_size(block_size),
_block_count(_size/_block_size),
_ram_ds(_env.ram(), _env.rm(), _size),
_ram_addr((addr_t)_ram_ds.local_addr<addr_t>())
{
/* populate backing store from file */
memcpy(_ram_ds.local_addr<void>(), _rom_ds->local_addr<void>(), _size);
}
/**
* Construct empty RAM dataspace
*/
Ram_block(Env &env, size_t size, size_t block_size)
: Block::Driver(env.ram()),
_env(env),
_size(size),
_block_size(block_size),
_block_count(_size/_block_size),
_ram_ds(_env.ram(), _env.rm(), _size),
_ram_addr((addr_t)_ram_ds.local_addr<addr_t>())
{ }
~Ram_block() { destroy(_alloc, _rom_ds); }
/****************************
** Block-driver interface **
****************************/
size_t block_size() { return _block_size; }
Block::sector_t block_count() { return _block_count; }
Block::Session::Operations ops()
{
Block::Session::Operations o;
o.set_operation(Block::Packet_descriptor::READ);
o.set_operation(Block::Packet_descriptor::WRITE);
return o;
}
void read(Block::sector_t block_number,
size_t block_count,
char* buffer,
Block::Packet_descriptor &packet)
{
_io(block_number, block_count, buffer, packet, true);
}
void write(Block::sector_t block_number,
size_t block_count,
const char * buffer,
Block::Packet_descriptor &packet)
{
_io(block_number, block_count, const_cast<char *>(buffer), packet, false);
}
};
struct Main
{
Env &env;
Heap heap { env.ram(), env.rm() };
Attached_rom_dataspace config_rom { env, "config" };
struct Factory : Block::Driver_factory
{
Env &env;
Allocator &alloc;
bool use_file { false };
typedef String<64> File;
File file { };
size_t size { 0 };
size_t block_size { 512 };
Factory(Env &env, Allocator &alloc, Xml_node config)
: env(env), alloc(alloc)
{
use_file = config.has_attribute("file");
if (use_file) {
file = config.attribute_value("file", File());
} else {
if (!config.has_attribute("size")) {
error("neither file nor size attribute specified");
throw Exception();
}
size = config.attribute_value("size", Number_of_bytes());
}
block_size = config.attribute_value("block_size", block_size);
}
Block::Driver *create()
{
try {
if (use_file) {
Genode::log("Creating RAM-basd block device populated by file='",
file, "' with block size ", block_size);
return new (&alloc)
Ram_block(env, alloc, file.string(), block_size);
} else {
Genode::log("Creating RAM-based block device with size ",
size, " and block size ", block_size);
return new (&alloc) Ram_block(env, size, block_size);
}
}
catch (...) { throw Service_denied(); }
}
void destroy(Block::Driver *driver) {
Genode::destroy(&alloc, driver); }
} factory { env, heap, config_rom.xml() };
enum { WRITEABLE = true };
Block::Root root { env.ep(), heap, env.rm(), factory, WRITEABLE };
Main(Env &env) : env(env)
{
env.parent().announce(env.ep().manage(root));
}
};
void Component::construct(Genode::Env &env) { static Main server(env); }