base/core: use references instead of pointers

This patch replaces the former prominent use of pointers by references
wherever feasible. This has the following benefits:

* The contract between caller and callee becomes more obvious. When
  passing a reference, the contract says that the argument cannot be
  a null pointer. The caller is responsible to ensure that. Therefore,
  the use of reference eliminates the need to add defensive null-pointer
  checks at the callee site, which sometimes merely exist to be on the
  safe side. The bottom line is that the code becomes easier to follow.

* Reference members must be initialized via an object initializer,
  which promotes a programming style that avoids intermediate object-
  construction states. Within core, there are still a few pointers
  as member variables left though. E.g., caused by the late association
  of 'Platform_thread' objects with their 'Platform_pd' objects.

* If no pointers are present as member variables, we don't need to
  manually provide declarations of a private copy constructor and
  an assignment operator to avoid -Weffc++ errors "class ... has
  pointer data members [-Werror=effc++]".

This patch also changes a few system bindings on NOVA and Fiasco.OC,
e.g., the return value of the global 'cap_map' accessor has become a
reference. Hence, the patch touches a few places outside of core.

Fixes #3135
This commit is contained in:
Norman Feske
2019-01-24 22:00:01 +01:00
parent f9373b4430
commit 6b289a1423
242 changed files with 2390 additions and 2434 deletions

View File

@@ -44,7 +44,7 @@ namespace Genode {
inline static bool map_local(addr_t from_addr, addr_t to_addr, size_t num_pages)
{
Pistachio::L4_ThreadId_t core_pager = platform_specific()->core_pager()->native_thread_id();
Pistachio::L4_ThreadId_t core_pager = platform_specific().core_pager().native_thread_id();
addr_t offset = 0;
size_t page_size = get_page_size();

View File

@@ -34,12 +34,6 @@ namespace Genode {
{
private:
/*
* Noncopyable
*/
Platform(Platform const &);
Platform &operator = (Platform const &);
/*
* Shortcut for the type of allocator instances for physical resources
*/
@@ -113,7 +107,7 @@ namespace Genode {
/**
* Return singleton instance of Sigma0 pager object
*/
static Sigma0 *sigma0();
static Sigma0 &sigma0();
/**
* Core pager thread that handles core-internal page-faults
@@ -123,7 +117,7 @@ namespace Genode {
/**
* Constructor
*/
Core_pager(Platform_pd *core_pd);
Core_pager(Platform_pd &core_pd);
int pager(Ipc_pager &) { /* never called */ return -1; }
};
@@ -131,7 +125,7 @@ namespace Genode {
/**
* Return singleton instance of core pager object
*/
Core_pager *core_pager();
Core_pager &core_pager();
/**
* Constructor
@@ -141,22 +135,22 @@ namespace Genode {
/**
* Return singleton instance of core PD object
*/
Platform_pd *core_pd();
Platform_pd &core_pd();
/********************************
** Generic platform interface **
********************************/
Range_allocator *core_mem_alloc() override { return &_ram_alloc; }
Range_allocator *ram_alloc() override { return &_ram_alloc; }
Range_allocator *io_mem_alloc() override { return &_io_mem_alloc; }
Range_allocator *io_port_alloc() override { return &_io_port_alloc; }
Range_allocator *irq_alloc() override { return &_irq_alloc; }
Range_allocator *region_alloc() override { return &_region_alloc; }
Range_allocator &core_mem_alloc() override { return _ram_alloc; }
Range_allocator &ram_alloc() override { return _ram_alloc; }
Range_allocator &io_mem_alloc() override { return _io_mem_alloc; }
Range_allocator &io_port_alloc() override { return _io_port_alloc; }
Range_allocator &irq_alloc() override { return _irq_alloc; }
Range_allocator &region_alloc() override { return _region_alloc; }
addr_t vm_start() const override { return _vm_start; }
size_t vm_size() const override { return _vm_size; }
Rom_fs *rom_fs() override { return &_rom_fs; }
Rom_fs &rom_fs() override { return _rom_fs; }
size_t max_caps() const override { return Capability_space::max_caps(); }
void wait_for_exit();

View File

@@ -97,7 +97,7 @@ namespace Genode {
*
* Again a special case for Core thread0.
*/
int _alloc_thread(int thread_id, Platform_thread *thread);
int _alloc_thread(int thread_id, Platform_thread &thread);
/**
* Thread deallocation
@@ -190,7 +190,7 @@ namespace Genode {
* Constructors
*/
Platform_pd(bool core);
Platform_pd(Allocator * md_alloc, char const *,
Platform_pd(Allocator &md_alloc, char const *,
signed pd_id = PD_INVALID, bool create = true);
/**
@@ -211,16 +211,16 @@ namespace Genode {
*
* This function allocates the physical L4 thread ID.
*/
bool bind_thread(Platform_thread *thread);
bool bind_thread(Platform_thread &thread);
int bind_initial_thread(Platform_thread *thread);
int bind_initial_thread(Platform_thread &thread);
/**
* Unbind thread from protection domain
*
* Free the thread's slot and update thread object.
*/
void unbind_thread(Platform_thread *thread);
void unbind_thread(Platform_thread &thread);
/**
* Assign parent interface to protection domain

View File

@@ -21,6 +21,7 @@
/* core includes */
#include <pager.h>
#include <platform_pd.h>
#include <assertion.h>
/* Pistachio includes */
namespace Pistachio {
@@ -53,12 +54,14 @@ namespace Genode {
Platform_thread(Platform_thread const &);
Platform_thread &operator = (Platform_thread const &);
int _thread_id;
L4_ThreadId_t _l4_thread_id;
char _name[32]; /* thread name at kernel debugger */
typedef String<32> Name;
int _thread_id = THREAD_INVALID;
L4_ThreadId_t _l4_thread_id = L4_nilthread;
Name const _name; /* thread name at kernel debugger */
Platform_pd *_platform_pd = nullptr;
unsigned _priority;
Pager_object *_pager;
unsigned _priority = 0;
Pager_object *_pager = nullptr;
Affinity::Location _location { };
public:
@@ -69,9 +72,16 @@ namespace Genode {
/**
* Constructor
*/
Platform_thread(size_t, const char *name = 0, unsigned priority = 0,
Affinity::Location = Affinity::Location(),
addr_t utcb = 0, int thread_id = THREAD_INVALID);
Platform_thread(size_t, char const *name, unsigned priority,
Affinity::Location, addr_t)
:
_name(name), _priority(priority)
{ }
/**
* Constructor used for core-internal threads
*/
Platform_thread(char const *name) : _name(name) { }
/**
* Destructor
@@ -117,7 +127,7 @@ namespace Genode {
* \param pd platform pd, thread is bound to
*/
void bind(int thread_id, Pistachio::L4_ThreadId_t l4_thread_id,
Platform_pd *pd);
Platform_pd &pd);
/**
* Unbind this thread
@@ -144,8 +154,15 @@ namespace Genode {
/**
* Return/set pager
*/
Pager_object *pager() const { return _pager; }
void pager(Pager_object *pager) { _pager = pager; }
Pager_object &pager() const
{
if (_pager)
return *_pager;
ASSERT_NEVER_CALLED;
}
void pager(Pager_object &pager) { _pager = &pager; }
/**
* Return identification of thread when faulting
@@ -180,7 +197,7 @@ namespace Genode {
int thread_id() const { return _thread_id; }
Pistachio::L4_ThreadId_t native_thread_id() const { return _l4_thread_id; }
const char *name() const { return _name; }
Name name() const { return _name; }
/* use only for core... */
void set_l4_thread_id(Pistachio::L4_ThreadId_t id) { _l4_thread_id = id; }

View File

@@ -24,7 +24,7 @@ class Genode::Rpc_cap_factory
{
private:
static Native_capability _alloc(Rpc_cap_factory *owner,
static Native_capability _alloc(Rpc_cap_factory &owner,
Native_capability ep);
public:

View File

@@ -76,7 +76,7 @@ addr_t Io_mem_session_component::_map_local(addr_t base, size_t size)
/* find appropriate region for mapping */
void *result = 0;
if (platform()->region_alloc()->alloc_aligned(size, &result, alignment).error())
if (platform().region_alloc().alloc_aligned(size, &result, alignment).error())
error(__func__, ": alloc_aligned failed!");
local_base = (addr_t)result;

View File

@@ -126,7 +126,7 @@ Irq_object::Irq_object(unsigned irq)
***************************/
Irq_session_component::Irq_session_component(Range_allocator *irq_alloc,
Irq_session_component::Irq_session_component(Range_allocator &irq_alloc,
const char *args)
:
_irq_number(Arg_string::find_arg(args, "irq_number").long_value(-1)),
@@ -137,7 +137,7 @@ Irq_session_component::Irq_session_component(Range_allocator *irq_alloc,
if (msi)
throw Service_denied();
if (!irq_alloc || irq_alloc->alloc_addr(1, _irq_number).error()) {
if (irq_alloc.alloc_addr(1, _irq_number).error()) {
error("unavailable IRQ ", Hex(_irq_number), " requested");
throw Service_denied();
}

View File

@@ -212,23 +212,23 @@ Platform::Sigma0::Sigma0()
}
Platform::Sigma0 *Platform::sigma0()
Platform::Sigma0 &Platform::sigma0()
{
static Sigma0 _sigma0;
return &_sigma0;
return _sigma0;
}
Platform::Core_pager::Core_pager(Platform_pd *core_pd)
Platform::Core_pager::Core_pager(Platform_pd &core_pd)
:
Platform_thread(0, "core.pager"),
Platform_thread("core.pager"),
Pager_object(Cpu_session_capability(), Thread_capability(),
0, Affinity::Location(),
Session_label(), Cpu_session::Name(name()))
{
Platform_thread::pager(sigma0());
core_pd->bind_thread(this);
core_pd.bind_thread(*this);
cap(Capability_space::import(native_thread_id(), Rpc_obj_key()));
/* stack begins at the top end of the '_core_pager_stack' array */
@@ -240,10 +240,10 @@ Platform::Core_pager::Core_pager(Platform_pd *core_pd)
}
Platform::Core_pager *Platform::core_pager()
Platform::Core_pager &Platform::core_pager()
{
static Core_pager _core_pager(core_pd());
return &_core_pager;
return _core_pager;
}
@@ -495,8 +495,6 @@ void Platform::_setup_basics()
/* add KIP as ROM module */
_rom_fs.insert(&_kip_rom);
// Get virtual bootinfo address.
L4_Fpage_t bipage = L4_Sigma0_GetPage(get_sigma0(),
L4_Fpage(kip->BootInfo,
get_page_size()));
@@ -567,18 +565,19 @@ void Platform::_setup_basics()
}
Platform_pd *Platform::core_pd()
Platform_pd &Platform::core_pd()
{
/* on first call, setup task object for core task */
static Platform_pd _core_pd(true);
return &_core_pd;
return _core_pd;
}
Platform::Platform() :
_ram_alloc(nullptr), _io_mem_alloc(core_mem_alloc()),
_io_port_alloc(core_mem_alloc()), _irq_alloc(core_mem_alloc()),
_region_alloc(core_mem_alloc()),
Platform::Platform()
:
_ram_alloc(nullptr), _io_mem_alloc(&core_mem_alloc()),
_io_port_alloc(&core_mem_alloc()), _irq_alloc(&core_mem_alloc()),
_region_alloc(&core_mem_alloc()),
_kip_rom((addr_t)Pistachio::get_kip(),
sizeof(Pistachio::L4_KernelInterfacePage_t), "pistachio_kip")
{
@@ -605,12 +604,12 @@ Platform::Platform() :
* thread_id of first task. But since we do not destroy this
* task, it should be no problem.
*/
static Platform_thread core_thread(0, "core.main");
static Platform_thread core_thread("core.main");
core_thread.set_l4_thread_id(Pistachio::L4_MyGlobalId());
core_thread.pager(sigma0());
core_pd()->bind_thread(&core_thread);
core_pd().bind_thread(core_thread);
/* core log as ROM module */
{
@@ -618,14 +617,14 @@ Platform::Platform() :
unsigned const pages = 1;
size_t const log_size = pages << get_page_size_log2();
ram_alloc()->alloc_aligned(log_size, &phys_ptr, get_page_size_log2());
ram_alloc().alloc_aligned(log_size, &phys_ptr, get_page_size_log2());
addr_t const phys_addr = reinterpret_cast<addr_t>(phys_ptr);
void * const core_local_ptr = phys_ptr;
addr_t const core_local_addr = phys_addr;
/* let one page free after the log buffer */
region_alloc()->remove_range(core_local_addr, log_size + get_page_size());
region_alloc().remove_range(core_local_addr, log_size + get_page_size());
memset(core_local_ptr, 0, log_size);

View File

@@ -147,7 +147,7 @@ Platform_thread* Platform_pd::_next_thread()
}
int Platform_pd::_alloc_thread(int thread_id, Platform_thread *thread)
int Platform_pd::_alloc_thread(int thread_id, Platform_thread &thread)
{
int i = thread_id;
@@ -164,7 +164,7 @@ int Platform_pd::_alloc_thread(int thread_id, Platform_thread *thread)
if (_threads[i]) return -2;
}
_threads[i] = thread;
_threads[i] = &thread;
return i;
}
@@ -183,12 +183,12 @@ void Platform_pd::_free_thread(int thread_id)
** Public object members **
***************************/
bool Platform_pd::bind_thread(Platform_thread *thread)
bool Platform_pd::bind_thread(Platform_thread &thread)
{
using namespace Pistachio;
/* thread_id is THREAD_INVALID by default - only core is the special case */
int thread_id = thread->thread_id();
int thread_id = thread.thread_id();
L4_ThreadId_t l4_thread_id;
int t = _alloc_thread(thread_id, thread);
@@ -200,18 +200,18 @@ bool Platform_pd::bind_thread(Platform_thread *thread)
l4_thread_id = make_l4_id(_pd_id, thread_id, _version);
/* finally inform thread about binding */
thread->bind(thread_id, l4_thread_id, this);
thread.bind(thread_id, l4_thread_id, *this);
return true;
}
void Platform_pd::unbind_thread(Platform_thread *thread)
void Platform_pd::unbind_thread(Platform_thread &thread)
{
int thread_id = thread->thread_id();
int thread_id = thread.thread_id();
/* unbind thread before proceeding */
thread->unbind();
thread.unbind();
_free_thread(thread_id);
@@ -326,7 +326,7 @@ Platform_pd::Platform_pd(bool) : _l4_task_id(L4_MyGlobalId())
}
Platform_pd::Platform_pd(Allocator *, char const *, signed pd_id, bool create)
Platform_pd::Platform_pd(Allocator &, char const *, signed pd_id, bool create)
{
if (!create)
panic("create must be true.");
@@ -348,7 +348,7 @@ Platform_pd::Platform_pd(Allocator *, char const *, signed pd_id, bool create)
Platform_pd::~Platform_pd()
{
/* unbind all threads */
while (Platform_thread *t = _next_thread()) unbind_thread(t);
while (Platform_thread *t = _next_thread()) unbind_thread(*t);
_destroy_pd();
_free_pd();

View File

@@ -137,11 +137,11 @@ void Platform_thread::resume()
void Platform_thread::bind(int thread_id, L4_ThreadId_t l4_thread_id,
Platform_pd *pd)
Platform_pd &pd)
{
_thread_id = thread_id;
_l4_thread_id = l4_thread_id;
_platform_pd = pd;
_platform_pd = &pd;
}
@@ -223,14 +223,6 @@ void Platform_thread::cancel_blocking()
}
Platform_thread::Platform_thread(size_t, const char *name, unsigned prio,
Affinity::Location, addr_t, int id)
: _thread_id(id), _l4_thread_id(L4_nilthread), _priority(prio), _pager(0)
{
strncpy(_name, name, sizeof(_name));
}
Platform_thread::~Platform_thread()
{
/*
@@ -238,5 +230,5 @@ Platform_thread::~Platform_thread()
* Thread::unbind()
*/
if (_platform_pd)
_platform_pd->unbind_thread(this);
_platform_pd->unbind_thread(*this);
}

View File

@@ -18,10 +18,10 @@
using namespace Genode;
void Ram_dataspace_factory::_export_ram_ds(Dataspace_component *) { }
void Ram_dataspace_factory::_revoke_ram_ds(Dataspace_component *) { }
void Ram_dataspace_factory::_export_ram_ds(Dataspace_component &) { }
void Ram_dataspace_factory::_revoke_ram_ds(Dataspace_component &) { }
void Ram_dataspace_factory::_clear_ds(Dataspace_component *ds)
void Ram_dataspace_factory::_clear_ds(Dataspace_component &ds)
{
memset((void *)ds->phys_addr(), 0, ds->size());
memset((void *)ds.phys_addr(), 0, ds.size());
}

View File

@@ -37,12 +37,12 @@ void Thread::_thread_start()
void Thread::start()
{
/* create and start platform thread */
native_thread().pt = new(platform()->core_mem_alloc())
Platform_thread(0, _stack->name().string());
native_thread().pt = new (platform().core_mem_alloc())
Platform_thread(_stack->name().string());
platform_specific()->core_pd()->bind_thread(native_thread().pt);
platform_specific().core_pd().bind_thread(*native_thread().pt);
native_thread().pt->pager(platform_specific()->core_pager());
native_thread().pt->pager(platform_specific().core_pager());
native_thread().l4id = native_thread().pt->native_thread_id();
native_thread().pt->start((void *)_thread_start, stack_top());
@@ -60,5 +60,5 @@ void Thread::cancel_blocking()
void Thread::_deinit_platform_thread()
{
/* destruct platform thread */
destroy(platform()->core_mem_alloc(), native_thread().pt);
destroy(platform().core_mem_alloc(), native_thread().pt);
}