genode/ports-foc/src/lib/l4lx/include/dataspace.h

170 lines
4.3 KiB
C
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Dataspace abstraction between Genode and L4Linux
* \author Stefan Kalkowski
* \date 2011-03-20
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2011-2013 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _L4LX__DATASPACE_H_
#define _L4LX__DATASPACE_H_
/* Genode includes */
#include <dataspace/client.h>
Fiasco.OC: introduce Cap_index (fixes #149, #112) This commit introduces a Cap_index class for Fiasco.OC's capabilities. A Cap_index is a combination of the global capability id, that is used by Genode to correctly identify a kernel-object, and a corresponding entry in a protection-domain's (kernel-)capability-space. The cap-indices are non-copyable, unique objects, that are held in a Cap_map. The Cap_map is used to re-find capabilities already present in the protection-domain, when a capability is received via IPC. The retrieval of capabilities effectively fixes issue #112, meaning the waste of capability-space entries. Because Cap_index objects are non-copyable (their address indicates the position in the capability-space of the pd), they are inappropriate to use as Native_capability. Therefore, Native_capability is implemented as a reference to Cap_index objects. This design seems to be a good pre-condition to implement smart-pointers for entries in the capability-space, and thereby closing existing leaks (please refer to issue #32). Cap_index, Cap_map, and the allocator for Cap_index objects are designed in a way, that it should be relatively easy to apply the same concept to NOVA also. By now, these classes are located in the `base-foc` repository, but they intentionally contain no Fiasco.OC specific elements. The previously explained changes had extensive impact on the whole Fiasco.OC platform implementation, due to various dependencies. The following things had to be changed: * The Thread object's startup and destruction routine is re-arranged, to enable another thread (that calls the Thread destructor) gaining the capability id of the thread's gate to remove it from the Cap_map, the thread's UTCB had to be made available to the caller, because there is the current location of that id. After having the UTCB available in the Thread object for that reason, the whole thread bootstrapping could be simplified. * In the course of changing the Native_capability's semantic, a new Cap_mapping class was introduced in core, that facilitates the establishment and destruction of capability mappings between core and it's client's, especially mappings related to Platform_thread and Platform_task, that are relevant to task and thread creation and destruction. Thereby, the destruction of threads had to be reworked, which effectively removed a bug (issue #149) where some threads weren't destroyed properly. * In the quick fix for issue #112, something similar to the Cap_map was introduced available in all processes. Moreover, some kind of a capability map already existed in core, to handle cap-session request properly. The introduction of the Cap_map unified both structures, so that the cap-session component code in core had to be reworked too. * The platform initialization code had to be changed sligthly due to the changes in Native_capability * The vcpu initialization in the L4Linux support library had to be adapted according to the already mentioned changes in the Thread object's bootstrap code.
2012-03-15 12:41:24 +01:00
#include <base/cap_map.h>
2013-09-17 14:44:10 +02:00
#include <base/env.h>
2011-12-22 16:19:25 +01:00
#include <util/avl_tree.h>
2013-09-17 14:44:10 +02:00
#include <rm_session/connection.h>
#include <platform_env.h>
2011-12-22 16:19:25 +01:00
namespace Fiasco {
#include <l4/sys/types.h>
}
namespace L4lx {
class Dataspace : public Genode::Avl_node<Dataspace>
{
private:
2013-09-17 14:44:10 +02:00
const char* _name;
Genode::size_t _size;
Fiasco::l4_cap_idx_t _ref;
2011-12-22 16:19:25 +01:00
public:
/******************
** Constructors **
******************/
2013-09-17 14:44:10 +02:00
Dataspace(const char* name,
Genode::size_t size,
Fiasco::l4_cap_idx_t ref)
: _name(name), _size(size), _ref(ref) {}
2011-12-22 16:19:25 +01:00
/***************
** Accessors **
***************/
const char* name() const { return _name; }
Genode::size_t size() { return _size; }
Fiasco::l4_cap_idx_t ref() { return _ref; }
2013-09-17 14:44:10 +02:00
virtual Genode::Dataspace_capability cap() = 0;
virtual void map(Genode::size_t offset, bool greedy = false) = 0;
virtual bool free(Genode::size_t offset) = 0;
2011-12-22 16:19:25 +01:00
/************************
** Avl_node interface **
************************/
bool higher(Dataspace *n) { return n->_ref > _ref; }
Dataspace *find_by_ref(Fiasco::l4_cap_idx_t ref)
{
if (_ref == ref) return this;
Dataspace *n = Genode::Avl_node<Dataspace>::child(ref > _ref);
return n ? n->find_by_ref(ref) : 0;
}
};
2013-09-17 14:44:10 +02:00
class Single_dataspace : public Dataspace
{
private:
Genode::Dataspace_capability _cap;
public:
Single_dataspace(const char* name,
Genode::size_t size,
Genode::Dataspace_capability ds,
Fiasco::l4_cap_idx_t ref =
Genode::cap_idx_alloc()->alloc_range(1)->kcap())
: Dataspace(name, size, ref), _cap(ds) {}
Genode::Dataspace_capability cap() { return _cap; }
void map(Genode::size_t offset, bool greedy) { }
bool free(Genode::size_t offset) { return false; }
2013-09-17 14:44:10 +02:00
};
class Chunked_dataspace : public Dataspace
{
private:
Genode::Rm_connection _rm_con;
Genode::Expanding_rm_session_client _rm;
Genode::Ram_dataspace_capability *_chunks;
2013-09-17 14:44:10 +02:00
public:
2013-09-17 14:44:10 +02:00
enum {
CHUNK_SIZE_LOG2 = 20,
CHUNK_SIZE = 1 << CHUNK_SIZE_LOG2,
2013-09-17 14:44:10 +02:00
};
Chunked_dataspace(const char* name,
Genode::size_t size,
Fiasco::l4_cap_idx_t ref)
: Dataspace(name, size, ref), _rm_con(0, size), _rm(_rm_con.cap())
{
_chunks = (Genode::Ram_dataspace_capability*) Genode::env()->heap()->alloc(
sizeof(Genode::Ram_dataspace_capability) * (size/CHUNK_SIZE));
}
2013-09-17 14:44:10 +02:00
Genode::Dataspace_capability cap() { return _rm_con.dataspace(); }
2013-09-17 14:44:10 +02:00
void map(Genode::size_t off, bool greedy)
2013-09-17 14:44:10 +02:00
{
off = Genode::align_addr((off-(CHUNK_SIZE-1)), CHUNK_SIZE_LOG2);
int i = off / CHUNK_SIZE;
if (_chunks[i].valid()) return;
Genode::size_t ram_avail = Genode::env()->ram_session()->avail();
if (greedy && ram_avail < 4*CHUNK_SIZE) {
char buf[128];
Genode::snprintf(buf, sizeof(buf), "ram_quota=%zd",
4*CHUNK_SIZE - ram_avail);
Genode::env()->parent()->resource_request(buf);
2013-09-17 14:44:10 +02:00
}
_chunks[i] = Genode::env()->ram_session()->alloc(CHUNK_SIZE);
_rm.attach(_chunks[i], 0, 0, true, off, false);
}
bool free(Genode::size_t off)
{
off = Genode::align_addr((off-(CHUNK_SIZE-1)), CHUNK_SIZE_LOG2);
int i = off / CHUNK_SIZE;
if (!_chunks[i].valid()) return false;
Genode::env()->ram_session()->free(_chunks[i]);
_chunks[i] = Genode::Ram_dataspace_capability();
return true;
2013-09-17 14:44:10 +02:00
}
};
2011-12-22 16:19:25 +01:00
class Dataspace_tree : public Genode::Avl_tree<Dataspace>
{
public:
Dataspace* find_by_ref(Fiasco::l4_cap_idx_t ref) {
return this->first() ? this->first()->find_by_ref(ref) : 0; }
Dataspace* insert(const char* name, Genode::Dataspace_capability cap);
void insert(Dataspace *ds) { Genode::Avl_tree<Dataspace>::insert(ds); }
};
}
#endif /* _L4LX__DATASPACE_H_ */