genode/repos/base/src/core/main.cc

314 lines
8.6 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Core main program
* \author Norman Feske
* \date 2006-07-12
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2006-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.
*/
/* Genode includes */
#include <base/snprintf.h>
#include <base/sleep.h>
#include <base/service.h>
#include <base/child.h>
#include <base/log.h>
#include <rm_session/connection.h>
#include <pd_session/connection.h>
2011-12-22 16:19:25 +01:00
#include <rom_session/connection.h>
#include <cpu_session/connection.h>
/* base-internal includes */
#include <base/internal/globals.h>
2011-12-22 16:19:25 +01:00
/* core includes */
#include <platform.h>
#include <core_env.h>
#include <ram_root.h>
#include <rom_root.h>
#include <rm_root.h>
#include <cpu_root.h>
#include <pd_root.h>
#include <log_root.h>
#include <io_mem_root.h>
#include <irq_root.h>
#include <trace/root.h>
#include <platform_services.h>
2011-12-22 16:19:25 +01:00
using namespace Genode;
/***************************************
** Core environment/platform support **
***************************************/
Core_env * Genode::core_env()
{
/*
* Make sure to initialize the platform before constructing the core
* environment.
*/
platform();
/*
* By placing the environment as static object here, we ensure that its
* constructor gets called when this function is used the first time.
*/
static Core_env _env;
return &_env;
}
Env_deprecated * Genode::env() {
2011-12-22 16:19:25 +01:00
return core_env(); }
Platform *Genode::platform_specific()
{
static Platform _platform;
return &_platform;
}
Platform_generic *Genode::platform() { return platform_specific(); }
/*************************
** Core parent support **
*************************/
Session_capability Core_parent::session(Parent::Client::Id id,
Parent::Service_name const &name,
Parent::Session_args const &args,
Affinity const &affinity)
2011-12-22 16:19:25 +01:00
{
Session_capability cap;
_services.for_each([&] (Service &service) {
if ((service.name() != name.string()) || cap.valid())
return;
Session_state &session = *new (_alloc)
Session_state(service, _id_space, id, args.string(), affinity);
service.initiate_request(session);
cap = session.cap;
});
2011-12-22 16:19:25 +01:00
if (!cap.valid())
error("unexpected core-parent ", name.string(), " session request");
2011-12-22 16:19:25 +01:00
return cap;
2011-12-22 16:19:25 +01:00
}
/****************
** Core child **
****************/
class Core_child : public Child_policy
{
private:
/*
* Entry point used for serving the parent interface
*/
Rpc_entrypoint _entrypoint;
enum { STACK_SIZE = 2 * 1024 * sizeof(Genode::addr_t)};
2011-12-22 16:19:25 +01:00
Registry<Service> &_services;
2011-12-22 16:19:25 +01:00
Capability<Ram_session> _core_ram_cap;
Ram_session &_core_ram;
Capability<Cpu_session> _core_cpu_cap;
Cpu_session &_core_cpu;
size_t const _ram_quota;
Child _child;
2011-12-22 16:19:25 +01:00
public:
/**
* Constructor
*/
Core_child(Registry<Service> &services, Ram_session &core_ram,
Capability<Ram_session> core_ram_cap, size_t ram_quota,
Cpu_session &core_cpu, Capability<Cpu_session> core_cpu_cap)
2011-12-22 16:19:25 +01:00
:
_entrypoint(nullptr, STACK_SIZE, "init_child", false),
_services(services),
_core_ram_cap(core_ram_cap), _core_ram(core_ram),
_core_cpu_cap(core_cpu_cap), _core_cpu(core_cpu),
_ram_quota(Child::effective_ram_quota(ram_quota)),
_child(*env()->rm_session(), _entrypoint, *this)
2011-12-22 16:19:25 +01:00
{
_entrypoint.activate();
}
/****************************
** Child-policy interface **
****************************/
Name name() const { return "init"; }
Service &resolve_session_request(Service::Name const &name,
Session_state::Args const &args) override
{
Service *service = nullptr;
_services.for_each([&] (Service &s) {
if (!service && s.name() == name)
service = &s; });
if (!service)
throw Parent::Service_denied();
return *service;
}
void init(Ram_session &session, Capability<Ram_session> cap) override
{
session.ref_account(_core_ram_cap);
_core_ram.transfer_quota(cap, _ram_quota);
}
2011-12-22 16:19:25 +01:00
void init(Cpu_session &session, Capability<Cpu_session> cap) override
2011-12-22 16:19:25 +01:00
{
session.ref_account(_core_cpu_cap);
_core_cpu.transfer_quota(cap, Cpu_session::quota_lim_upscale(100, 100));
2011-12-22 16:19:25 +01:00
}
Ram_session &ref_ram() { return _core_ram; }
Ram_session_capability ref_ram_cap() const { return _core_ram_cap; }
2011-12-22 16:19:25 +01:00
};
/****************
** Signal API **
****************/
/*
* In contrast to the 'Platform_env' used by non-core components, core disables
* the signal thread but overriding 'Genode::init_signal_thread' with a dummy.
* Within core, the signal thread is not needed as core is never supposed to
* receive any signals. Otherwise, the signal thread would be the only
* non-entrypoint thread within core, which would be a problem on NOVA where
* the creation of regular threads within core is unsupported.
*/
namespace Genode { void init_signal_thread(Env &) { } }
/*******************
** Trace support **
*******************/
Trace::Source_registry &Trace::sources()
{
static Trace::Source_registry inst;
return inst;
}
2011-12-22 16:19:25 +01:00
/***************
** Core main **
***************/
namespace Genode {
extern bool inhibit_tracing;
extern char const *version_string;
}
2013-08-09 20:48:32 +02:00
2011-12-22 16:19:25 +01:00
int main()
{
2013-08-09 20:48:32 +02:00
/**
* Disable tracing within core because it is currently not fully implemented.
*/
inhibit_tracing = true;
log("Genode ", Genode::version_string);
static Trace::Policy_registry trace_policies;
2011-12-22 16:19:25 +01:00
/*
* Initialize root interfaces for our services
*/
Rpc_entrypoint *e = core_env()->entrypoint();
Registry<Service> &services = core_env()->services();
2011-12-22 16:19:25 +01:00
/*
* Allocate session meta data on distinct dataspaces to enable independent
* destruction (to enable quota trading) of session component objects.
*/
static Sliced_heap sliced_heap(env()->ram_session(), env()->rm_session());
/*
* Factory for creating RPC capabilities within core
*/
static Rpc_cap_factory rpc_cap_factory(sliced_heap);
static Pager_entrypoint pager_ep(rpc_cap_factory);
2011-12-22 16:19:25 +01:00
static Ram_root ram_root (e, e, platform()->ram_alloc(), &sliced_heap);
static Rom_root rom_root (e, e, platform()->rom_fs(), &sliced_heap);
static Rm_root rm_root (e, &sliced_heap, pager_ep);
static Cpu_root cpu_root (e, e, &pager_ep, &sliced_heap,
Trace::sources());
static Pd_root pd_root (e, e, pager_ep, &sliced_heap);
2011-12-22 16:19:25 +01:00
static Log_root log_root (e, &sliced_heap);
static Io_mem_root io_mem_root (e, e, platform()->io_mem_alloc(),
platform()->ram_alloc(), &sliced_heap);
static Irq_root irq_root (core_env()->pd_session(),
platform()->irq_alloc(), &sliced_heap);
static Trace::Root trace_root (e, &sliced_heap, Trace::sources(), trace_policies);
2011-12-22 16:19:25 +01:00
static Core_service<Rom_session_component> rom_service (services, rom_root);
static Core_service<Ram_session_component> ram_service (services, ram_root);
static Core_service<Rm_session_component> rm_service (services, rm_root);
static Core_service<Cpu_session_component> cpu_service (services, cpu_root);
static Core_service<Pd_session_component> pd_service (services, pd_root);
static Core_service<Log_session_component> log_service (services, log_root);
static Core_service<Io_mem_session_component> io_mem_service (services, io_mem_root);
static Core_service<Irq_session_component> irq_service (services, irq_root);
static Core_service<Trace::Session_component> trace_service (services, trace_root);
2011-12-22 16:19:25 +01:00
/* make platform-specific services known to service pool */
platform_add_local_services(e, &sliced_heap, &services);
/* create CPU session representing core */
thread API & CPU session: accounting of CPU quota In the init configuration one can configure the donation of CPU time via 'resource' tags that have the attribute 'name' set to "CPU" and the attribute 'quantum' set to the percentage of CPU quota that init shall donate. The pattern is the same as when donating RAM quota. ! <start name="test"> ! <resource name="CPU" quantum="75"/> ! </start> This would cause init to try donating 75% of its CPU quota to the child "test". Init and core do not preserve CPU quota for their own requirements by default as it is done with RAM quota. The CPU quota that a process owns can be applied through the thread constructor. The constructor has been enhanced by an argument that indicates the percentage of the programs CPU quota that shall be granted to the new thread. So 'Thread(33, "test")' would cause the backing CPU session to try to grant 33% of the programs CPU quota to the thread "test". By now, the CPU quota of a thread can't be altered after construction. Constructing a thread with CPU quota 0 doesn't mean the thread gets never scheduled but that the thread has no guaranty to receive CPU time. Such threads have to live with excess CPU time. Threads that already existed in the official repositories of Genode were adapted in the way that they receive a quota of 0. This commit also provides a run test 'cpu_quota' in base-hw (the only kernel that applies the CPU-quota scheme currently). The test basically runs three threads with different physical CPU quota. The threads simply count for 30 seconds each and the test then checks wether the counter values relate to the CPU-quota distribution. fix #1275
2014-10-16 11:15:46 +02:00
static Cpu_session_component
core_cpu(e, e, &pager_ep, &sliced_heap, Trace::sources(),
"label=\"core\"", Affinity(), Cpu_session::QUOTA_LIMIT);
Cpu_session_capability core_cpu_cap = core_env()->entrypoint()->manage(&core_cpu);
2011-12-22 16:19:25 +01:00
/*
* Transfer all left memory to init, but leave some memory left for core
*
* NOTE: exception objects thrown in core components are currently
* allocated on core's heap and not accounted by the component's meta data
* allocator
*/
Genode::size_t const ram_quota = platform()->ram_alloc()->avail() - 224*1024;
log("", ram_quota / (1024*1024), " MiB RAM assigned to init");
2011-12-22 16:19:25 +01:00
static Volatile_object<Core_child>
init(services, *env()->ram_session(), env()->ram_session_cap(),
ram_quota, core_cpu, core_cpu_cap);
2011-12-22 16:19:25 +01:00
platform()->wait_for_exit();
2011-12-22 16:19:25 +01:00
init.destruct();
2011-12-22 16:19:25 +01:00
return 0;
}