genode/repos/base-hw/src/lib/base/ipc.cc

175 lines
4.6 KiB
C++
Raw Normal View History

/*
* \brief Implementation of the Genode IPC-framework
* \author Martin Stein
* \author Norman Feske
* \date 2012-02-12
*/
/*
* Copyright (C) 2012-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/env.h>
#include <base/ipc.h>
#include <base/allocator.h>
#include <base/thread.h>
#include <util/construct_at.h>
#include <util/retry.h>
/* base-internal includes */
#include <base/internal/native_utcb.h>
#include <base/internal/native_thread.h>
#include <base/internal/ipc_server.h>
#include <base/internal/native_env.h>
#include <base/internal/capability_space.h>
/* base-hw includes */
#include <kernel/interface.h>
namespace Hw { extern Genode::Untyped_capability _main_thread_cap; }
using namespace Genode;
/**
* Copy data from the message buffer to UTCB
*/
static inline void copy_msg_to_utcb(Msgbuf_base const &snd_msg, Native_utcb &utcb)
{
/* copy capabilities */
size_t const num_caps = min((size_t)Msgbuf_base::MAX_CAPS_PER_MSG,
snd_msg.used_caps());
for (unsigned i = 0; i < num_caps; i++)
utcb.cap_set(i, Capability_space::capid(snd_msg.cap(i)));
utcb.cap_cnt(num_caps);
/* copy data payload */
size_t const data_size = min(snd_msg.data_size(),
min(snd_msg.capacity(), utcb.capacity()));
memcpy(utcb.data(), snd_msg.data(), data_size);
utcb.data_size(data_size);
}
/**
* Copy data from UTCB to the message buffer
*/
static inline void copy_utcb_to_msg(Native_utcb const &utcb, Msgbuf_base &rcv_msg)
{
/* copy capabilities */
size_t const num_caps = min((size_t)Msgbuf_base::MAX_CAPS_PER_MSG,
utcb.cap_cnt());
for (unsigned i = 0; i < num_caps; i++) {
rcv_msg.cap(i) = Capability_space::import(utcb.cap_get(i));
if (rcv_msg.cap(i).valid())
Kernel::ack_cap(Capability_space::capid(rcv_msg.cap(i)));
}
rcv_msg.used_caps(num_caps);
/* copy data payload */
size_t const data_size = min(utcb.data_size(),
min(utcb.capacity(), rcv_msg.capacity()));
memcpy(rcv_msg.data(), utcb.data(), data_size);
rcv_msg.data_size(data_size);
}
/****************
** IPC client **
****************/
Rpc_exception_code Genode::ipc_call(Native_capability dst,
Msgbuf_base &snd_msg, Msgbuf_base &rcv_msg,
size_t rcv_caps)
{
Native_utcb &utcb = *Thread::myself()->utcb();
retry<Genode::Allocator::Out_of_memory>(
[&] () {
copy_msg_to_utcb(snd_msg, *Thread::myself()->utcb());
switch (Kernel::send_request_msg(Capability_space::capid(dst), rcv_caps)) {
case -1: throw Blocking_canceled();
case -2: throw Allocator::Out_of_memory();
default:
copy_utcb_to_msg(utcb, rcv_msg);
}
},
Capability quota accounting and trading This patch mirrors the accounting and trading scheme that Genode employs for physical memory to the accounting of capability allocations. Capability quotas must now be explicitly assigned to subsystems by specifying a 'caps=<amount>' attribute to init's start nodes. Analogously to RAM quotas, cap quotas can be traded between clients and servers as part of the session protocol. The capability budget of each component is maintained by the component's corresponding PD session at core. At the current stage, the accounting is applied to RPC capabilities, signal-context capabilities, and dataspace capabilities. Capabilities that are dynamically allocated via core's CPU and TRACE service are not yet covered. Also, the capabilities allocated by resource multiplexers outside of core (like nitpicker) must be accounted by the respective servers, which is not covered yet. If a component runs out of capabilities, core's PD service prints a warning to the log. To observe the consumption of capabilities per component in detail, the PD service is equipped with a diagnostic mode, which can be enabled via the 'diag' attribute in the target node of init's routing rules. E.g., the following route enables the diagnostic mode for the PD session of the "timer" component: <default-route> <service name="PD" unscoped_label="timer"> <parent diag="yes"/> </service> ... </default-route> For subsystems based on a sub-init instance, init can be configured to report the capability-quota information of its subsystems by adding the attribute 'child_caps="yes"' to init's '<report>' config node. Init's own capability quota can be reported by adding the attribute 'init_caps="yes"'. Fixes #2398
2017-05-08 21:35:43 +02:00
[&] () { upgrade_pd_quota_non_blocking(Ram_quota{3 * 1024 * sizeof(addr_t)},
Cap_quota{0}); });
return Rpc_exception_code(utcb.exception_code());
}
/****************
** IPC server **
****************/
void Genode::ipc_reply(Native_capability caller, Rpc_exception_code exc,
Msgbuf_base &snd_msg)
{
Native_utcb &utcb = *Thread::myself()->utcb();
copy_msg_to_utcb(snd_msg, utcb);
utcb.exception_code(exc.value);
snd_msg.reset();
Kernel::send_reply_msg(0, false);
}
Genode::Rpc_request Genode::ipc_reply_wait(Reply_capability const &,
Rpc_exception_code exc,
Msgbuf_base &reply_msg,
Msgbuf_base &request_msg)
{
Native_utcb &utcb = *Thread::myself()->utcb();
retry<Genode::Allocator::Out_of_memory>(
[&] () {
int ret = 0;
if (exc.value != Rpc_exception_code::INVALID_OBJECT) {
copy_msg_to_utcb(reply_msg, utcb);
utcb.exception_code(exc.value);
ret = Kernel::send_reply_msg(Msgbuf_base::MAX_CAPS_PER_MSG, true);
} else {
ret = Kernel::await_request_msg(Msgbuf_base::MAX_CAPS_PER_MSG);
}
switch (ret) {
case -1: throw Blocking_canceled();
case -2: throw Allocator::Out_of_memory();
default: break;
}
},
Capability quota accounting and trading This patch mirrors the accounting and trading scheme that Genode employs for physical memory to the accounting of capability allocations. Capability quotas must now be explicitly assigned to subsystems by specifying a 'caps=<amount>' attribute to init's start nodes. Analogously to RAM quotas, cap quotas can be traded between clients and servers as part of the session protocol. The capability budget of each component is maintained by the component's corresponding PD session at core. At the current stage, the accounting is applied to RPC capabilities, signal-context capabilities, and dataspace capabilities. Capabilities that are dynamically allocated via core's CPU and TRACE service are not yet covered. Also, the capabilities allocated by resource multiplexers outside of core (like nitpicker) must be accounted by the respective servers, which is not covered yet. If a component runs out of capabilities, core's PD service prints a warning to the log. To observe the consumption of capabilities per component in detail, the PD service is equipped with a diagnostic mode, which can be enabled via the 'diag' attribute in the target node of init's routing rules. E.g., the following route enables the diagnostic mode for the PD session of the "timer" component: <default-route> <service name="PD" unscoped_label="timer"> <parent diag="yes"/> </service> ... </default-route> For subsystems based on a sub-init instance, init can be configured to report the capability-quota information of its subsystems by adding the attribute 'child_caps="yes"' to init's '<report>' config node. Init's own capability quota can be reported by adding the attribute 'init_caps="yes"'. Fixes #2398
2017-05-08 21:35:43 +02:00
[&] () { upgrade_pd_quota_non_blocking(Ram_quota{3 * 1024 * sizeof(addr_t)},
Cap_quota{0}); });
copy_utcb_to_msg(utcb, request_msg);
return Rpc_request(Native_capability(), utcb.destination());
}
Ipc_server::Ipc_server()
:
Native_capability(Thread::myself() ? Thread::myself()->native_thread().cap
: Hw::_main_thread_cap)
{ }
Ipc_server::~Ipc_server() { }