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

173 lines
4.4 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);
}
},
[&] () { upgrade_capability_slab(); });
return Rpc_exception_code(utcb.exception_code());
}
/****************
** IPC server **
****************/
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
void Genode::ipc_reply(Native_capability, 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;
}
},
[&] () { upgrade_capability_slab(); });
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() { }