parent
0c8a4f9f3a
commit
1256b01867
|
@ -13,265 +13,140 @@
|
||||||
|
|
||||||
#include <base/exception.h>
|
#include <base/exception.h>
|
||||||
#include <base/printf.h>
|
#include <base/printf.h>
|
||||||
#include <base/sleep.h>
|
|
||||||
#include <base/semaphore.h>
|
|
||||||
#include <base/allocator_avl.h>
|
|
||||||
#include <root/component.h>
|
|
||||||
#include <os/config.h>
|
#include <os/config.h>
|
||||||
#include <cap_session/connection.h>
|
#include <cap_session/connection.h>
|
||||||
#include <rom_session/connection.h>
|
#include <rom_session/connection.h>
|
||||||
#include <block_session/rpc_object.h>
|
#include <block/component.h>
|
||||||
|
#include <block/driver.h>
|
||||||
|
|
||||||
using namespace Genode;
|
using namespace Genode;
|
||||||
|
|
||||||
namespace Block {
|
class Rom_blk : public Block::Driver
|
||||||
|
|
||||||
class Session_component : public Session_rpc_object
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/**
|
Rom_connection _rom;
|
||||||
* Thread handling the requests of an open block session.
|
Dataspace_capability _file_cap; /* rom-file capability */
|
||||||
*/
|
addr_t _file_addr; /* start address of attached file */
|
||||||
class Tx_thread : public Thread<8192>
|
size_t _file_sz; /* file size */
|
||||||
{
|
size_t _blk_sz; /* block size */
|
||||||
private:
|
size_t _blk_cnt; /* block count */
|
||||||
|
|
||||||
Session_component *_session; /* corresponding session object */
|
|
||||||
addr_t _dev_addr; /* rom-file address */
|
|
||||||
size_t _dev_size; /* rom-file size */
|
|
||||||
size_t _blk_size; /* block size */
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
Rom_blk(const char *name, size_t blk_sz)
|
||||||
* Constructor
|
: _rom(name),
|
||||||
*/
|
_file_cap(_rom.dataspace()),
|
||||||
Tx_thread(Session_component *session,
|
_file_addr(env()->rm_session()->attach(_file_cap)),
|
||||||
addr_t dev_addr,
|
_file_sz(Dataspace_client(_file_cap).size()),
|
||||||
size_t dev_size,
|
_blk_sz(blk_sz),
|
||||||
size_t blk_size)
|
_blk_cnt(_file_sz/_blk_sz) { }
|
||||||
: Thread("block_session_tx"),
|
|
||||||
_session(session),
|
|
||||||
_dev_addr(dev_addr),
|
|
||||||
_dev_size(dev_size),
|
|
||||||
_blk_size(blk_size) { }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread's entry function.
|
/****************************
|
||||||
*/
|
** Block-driver interface **
|
||||||
void entry()
|
****************************/
|
||||||
|
|
||||||
|
Genode::size_t block_size() { return _blk_sz; }
|
||||||
|
Genode::size_t block_count() { return _blk_cnt; }
|
||||||
|
|
||||||
|
Block::Session::Operations ops()
|
||||||
{
|
{
|
||||||
Session_component::Tx::Sink *tx_sink =
|
Block::Session::Operations o;
|
||||||
_session->tx_sink();
|
o.set_operation(Block::Packet_descriptor::READ);
|
||||||
Block::Packet_descriptor packet;
|
return o;
|
||||||
|
|
||||||
/* signal preparedness to server activation */
|
|
||||||
_session->tx_ready();
|
|
||||||
|
|
||||||
/* handle requests */
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
/* blocking-get packet from client */
|
|
||||||
packet = tx_sink->get_packet();
|
|
||||||
if (!packet.valid()) {
|
|
||||||
PWRN("received invalid packet");
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read(Genode::size_t block_number,
|
||||||
|
Genode::size_t block_count,
|
||||||
|
char *out_buffer)
|
||||||
|
{
|
||||||
/* sanity check block number */
|
/* sanity check block number */
|
||||||
if ((packet.block_number() + packet.block_count()
|
if ((block_number + block_count > _file_sz / _blk_sz)
|
||||||
> _dev_size / _blk_size)
|
|| block_number < 0) {
|
||||||
|| packet.block_number() < 0) {
|
|
||||||
PWRN("requested blocks %zd-%zd out of range!",
|
PWRN("requested blocks %zd-%zd out of range!",
|
||||||
packet.block_number(),
|
block_number, block_number + block_count);
|
||||||
packet.block_number() + packet.block_count());
|
return;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t offset = packet.block_number() * _blk_size;
|
size_t offset = block_number * _blk_sz;
|
||||||
size_t size = packet.block_count() * _blk_size;
|
size_t size = block_count * _blk_sz;
|
||||||
switch (packet.operation()) {
|
|
||||||
case Block::Packet_descriptor::READ:
|
|
||||||
{
|
|
||||||
/* copy file content to packet payload */
|
/* copy file content to packet payload */
|
||||||
memcpy(tx_sink->packet_content(packet),
|
memcpy(out_buffer, (void*)(_file_addr + offset), size);
|
||||||
(void*)(_dev_addr + offset),
|
|
||||||
size);
|
|
||||||
packet.succeeded(true);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Block::Packet_descriptor::WRITE:
|
|
||||||
|
void write(Genode::size_t block_number,
|
||||||
|
Genode::size_t block_count,
|
||||||
|
char const *buffer)
|
||||||
{
|
{
|
||||||
PWRN("write attempt on read-only device");
|
PWRN("write attempt on read-only device");
|
||||||
packet.succeeded(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
PWRN("unsupported operation");
|
|
||||||
packet.succeeded(false);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* acknowledge packet to the client */
|
/*
|
||||||
if (!tx_sink->ready_to_ack())
|
* This driver does not support DMA operation, currently.
|
||||||
PDBG("need to wait until ready-for-ack");
|
|
||||||
tx_sink->acknowledge_packet(packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
friend class Session_component;
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
Dataspace_capability _tx_ds; /* buffer for tx channel */
|
|
||||||
Semaphore _startup_sema; /* thread startup sync */
|
|
||||||
Tx_thread _tx_thread; /* thread handling block requests */
|
|
||||||
size_t _block_count; /* count of blocks of this session */
|
|
||||||
size_t _block_size; /* size of a block */
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* \param tx_buf_size buffer size for tx channel
|
|
||||||
* \param dev_addr address of attached file
|
|
||||||
* \param dev_size size of attached file
|
|
||||||
*/
|
*/
|
||||||
Session_component(size_t tx_buf_size,
|
|
||||||
addr_t dev_addr,
|
|
||||||
size_t dev_size,
|
|
||||||
size_t blk_size,
|
|
||||||
Genode::Rpc_entrypoint &ep)
|
|
||||||
: Session_rpc_object(env()->ram_session()->alloc(tx_buf_size), ep),
|
|
||||||
_startup_sema(0),
|
|
||||||
_tx_thread(this, dev_addr, dev_size, blk_size),
|
|
||||||
_block_count(dev_size / blk_size),
|
|
||||||
_block_size(blk_size)
|
|
||||||
{
|
|
||||||
_tx_thread.start();
|
|
||||||
|
|
||||||
/* block until thread is ready to handle requests */
|
void read_dma(Genode::size_t, Genode::size_t, Genode::addr_t) {
|
||||||
_startup_sema.down();
|
throw Io_error(); }
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
void write_dma(Genode::size_t, Genode::size_t, Genode::addr_t) {
|
||||||
* Signal indicating that transmit thread is ready
|
throw Io_error(); }
|
||||||
*/
|
|
||||||
void tx_ready() { _startup_sema.up(); }
|
|
||||||
|
|
||||||
|
bool dma_enabled() { return false; }
|
||||||
|
|
||||||
/*****************************
|
Genode::Ram_dataspace_capability alloc_dma_buffer(Genode::size_t size) {
|
||||||
** Block session interface **
|
return Genode::env()->ram_session()->alloc(size, false); }
|
||||||
*****************************/
|
|
||||||
|
|
||||||
void info(size_t *blk_count, size_t *blk_size, Operations *ops)
|
|
||||||
{
|
|
||||||
*blk_count = _block_count;
|
|
||||||
*blk_size = _block_size;
|
|
||||||
ops->set_operation(Block::Packet_descriptor::READ);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sync() {}
|
void sync() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
struct Factory : Block::Driver_factory
|
||||||
* Root component, handling new session requests
|
|
||||||
*/
|
|
||||||
class Root : public Root_component<Session_component>
|
|
||||||
{
|
{
|
||||||
private:
|
Block::Driver *create()
|
||||||
|
|
||||||
Dataspace_capability _file_cap; /* rom-file capability */
|
|
||||||
addr_t _file_addr; /* start address of attached file */
|
|
||||||
size_t _file_sz; /* file size */
|
|
||||||
size_t _blk_sz; /* block size */
|
|
||||||
Genode::Rpc_entrypoint &_channel_ep;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
Session_component *_create_session(const char *args)
|
|
||||||
{
|
{
|
||||||
size_t ram_quota =
|
char file[64];
|
||||||
Arg_string::find_arg(args, "ram_quota" ).ulong_value(0);
|
size_t blk_sz = 512;
|
||||||
size_t tx_buf_size =
|
|
||||||
Arg_string::find_arg(args, "tx_buf_size").ulong_value(0);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Check if donated ram quota suffices for session data,
|
|
||||||
* and communication buffer.
|
|
||||||
*/
|
|
||||||
size_t session_size = sizeof(Session_component) + tx_buf_size;
|
|
||||||
if (max((size_t)4096, session_size) > ram_quota) {
|
|
||||||
PERR("insufficient 'ram_quota', got %zd, need %zd",
|
|
||||||
ram_quota, session_size);
|
|
||||||
throw Root::Quota_exceeded();
|
|
||||||
}
|
|
||||||
return new (md_alloc())
|
|
||||||
Session_component(tx_buf_size, _file_addr, _file_sz, _blk_sz, _channel_ep);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* \param session_ep session entrypoint
|
|
||||||
* \param md_alloc meta-data allocator
|
|
||||||
* \param file_cap capabilty of file to use as block-device
|
|
||||||
*/
|
|
||||||
Root(Rpc_entrypoint *session_ep, Allocator *md_alloc,
|
|
||||||
Dataspace_capability file_cap, size_t blk_size)
|
|
||||||
: Root_component<Session_component>(session_ep, md_alloc),
|
|
||||||
_file_cap(file_cap),
|
|
||||||
_file_addr(env()->rm_session()->attach(file_cap)),
|
|
||||||
_file_sz(Dataspace_client(file_cap).size()),
|
|
||||||
_blk_sz(blk_size),
|
|
||||||
_channel_ep(*session_ep) { }
|
|
||||||
|
|
||||||
~Root() { env()->rm_session()->detach((void*)_file_addr); }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get name of the file we want to provide as block device,
|
|
||||||
* and the block_size.
|
|
||||||
*/
|
|
||||||
static void process_config(char *file, size_t size, size_t *blk_size)
|
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
config()->xml_node().attribute("file").value(file, size);
|
config()->xml_node().attribute("file").value(file, sizeof(file));
|
||||||
config()->xml_node().attribute("block_size").value(blk_size);
|
config()->xml_node().attribute("block_size").value(&blk_sz);
|
||||||
}
|
}
|
||||||
catch (...) { }
|
catch (...) { }
|
||||||
|
|
||||||
|
PINF("Using file=%s as device with block size %zx.", file, blk_sz);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Rom_blk *driver = new (Genode::env()->heap()) Rom_blk(file, blk_sz);
|
||||||
|
return driver;
|
||||||
|
} catch(Rom_connection::Rom_connection_failed) {
|
||||||
|
PERR("Cannot open file %s.", file);
|
||||||
}
|
}
|
||||||
|
throw Root::Unavailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy(Block::Driver *driver) {
|
||||||
|
Genode::destroy(env()->heap(), driver); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
static char file[64];
|
|
||||||
static size_t block_size = 512;
|
|
||||||
|
|
||||||
process_config(file, sizeof(file), &block_size);
|
|
||||||
|
|
||||||
PINF("Using file=%s as device with block size %zx.", file, block_size);
|
|
||||||
|
|
||||||
try {
|
|
||||||
enum { STACK_SIZE = 8192 };
|
enum { STACK_SIZE = 8192 };
|
||||||
static Cap_connection cap;
|
static Cap_connection cap;
|
||||||
static Rpc_entrypoint ep(&cap, STACK_SIZE, "rom_loop_ep");
|
static Rpc_entrypoint ep(&cap, STACK_SIZE, "rom_blk_ep");
|
||||||
static Rom_connection rom(file);
|
|
||||||
static Block::Root blk_root(&ep, env()->heap(), rom.dataspace(), block_size);
|
|
||||||
|
|
||||||
env()->parent()->announce(ep.manage(&blk_root));
|
static Signal_receiver receiver;
|
||||||
|
static Factory driver_factory;
|
||||||
|
static Block::Root block_root(&ep, env()->heap(), driver_factory, receiver);
|
||||||
|
|
||||||
sleep_forever();
|
env()->parent()->announce(ep.manage(&block_root));
|
||||||
} catch (Rom_connection::Rom_connection_failed) {
|
|
||||||
PERR("Cannot open file %s.", file);
|
while (true) {
|
||||||
|
Signal s = receiver.wait_for_signal();
|
||||||
|
static_cast<Signal_dispatcher_base *>(s.context())->dispatch(s.num());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user