2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Linux platform interface implementation
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-06-13
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/lock.h>
|
2012-12-04 16:51:24 +01:00
|
|
|
#include <linux_dataspace/client.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2016-03-11 17:32:43 +01:00
|
|
|
/* base-internal includes */
|
|
|
|
#include <base/internal/native_thread.h>
|
2016-06-13 13:53:58 +02:00
|
|
|
#include <base/internal/parent_socket_handle.h>
|
2016-06-15 15:04:54 +02:00
|
|
|
#include <base/internal/capability_space_tpl.h>
|
2016-03-11 17:32:43 +01:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/* local includes */
|
|
|
|
#include "platform.h"
|
|
|
|
#include "core_env.h"
|
2012-08-10 16:35:57 +02:00
|
|
|
#include "server_socket_pair.h"
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* Linux includes */
|
2012-08-10 16:35:57 +02:00
|
|
|
#include <core_linux_syscalls.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
2012-12-04 16:51:24 +01:00
|
|
|
/**
|
|
|
|
* Memory pool used for for core-local meta data
|
|
|
|
*/
|
|
|
|
static char _core_mem[80*1024*1024];
|
2013-01-03 16:23:11 +01:00
|
|
|
|
|
|
|
|
2013-02-25 21:18:26 +01:00
|
|
|
/*
|
|
|
|
* Basic semaphore implementation based on the 'pipe' syscall.
|
|
|
|
*
|
|
|
|
* This alternative implementation is needed to be able to wake up the
|
|
|
|
* blocked main thread from a signal handler executed by the same thread.
|
|
|
|
*/
|
|
|
|
class Pipe_semaphore
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
int _pipefd[2];
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Pipe_semaphore()
|
|
|
|
{
|
|
|
|
lx_pipe(_pipefd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void down()
|
|
|
|
{
|
|
|
|
char dummy;
|
|
|
|
while(lx_read(_pipefd[0], &dummy, 1) != 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void up()
|
|
|
|
{
|
|
|
|
char dummy;
|
|
|
|
while (lx_write(_pipefd[1], &dummy, 1) != 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Pipe_semaphore _wait_for_exit_sem; /* wakeup of '_wait_for_exit' */
|
|
|
|
static bool _do_exit = false; /* exit condition */
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
static void sigint_handler(int)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2013-01-03 13:20:24 +01:00
|
|
|
_do_exit = true;
|
2013-02-25 21:18:26 +01:00
|
|
|
_wait_for_exit_sem.up();
|
2013-01-03 13:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
static void sigchld_handler(int)
|
2013-01-03 13:20:24 +01:00
|
|
|
{
|
2013-02-25 21:18:26 +01:00
|
|
|
_wait_for_exit_sem.up();
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Platform::Platform()
|
2015-09-17 14:16:59 +02:00
|
|
|
: _core_mem_alloc(nullptr)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
/* catch control-c */
|
2016-06-22 16:14:18 +02:00
|
|
|
lx_sigaction(LX_SIGINT, sigint_handler, false);
|
2013-01-03 13:20:24 +01:00
|
|
|
|
|
|
|
/* catch SIGCHLD */
|
2016-06-22 16:14:18 +02:00
|
|
|
lx_sigaction(LX_SIGCHLD, sigchld_handler, false);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* create resource directory under /tmp */
|
2012-08-10 16:35:57 +02:00
|
|
|
lx_mkdir(resource_path(), S_IRWXU);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2012-12-04 16:51:24 +01:00
|
|
|
_core_mem_alloc.add_range((addr_t)_core_mem, sizeof(_core_mem));
|
2012-11-26 20:30:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Occupy the socket handle that will be used to propagate the parent
|
|
|
|
* capability new processes. Otherwise, there may be the chance that the
|
|
|
|
* parent capability as supplied by the process creator will be assigned to
|
|
|
|
* this handle, which would result in a 'dup2' syscall taking
|
|
|
|
* PARENT_SOCKET_HANDLE as both source and target descriptor.
|
|
|
|
*/
|
|
|
|
lx_dup2(0, PARENT_SOCKET_HANDLE);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-22 16:58:21 +01:00
|
|
|
void Platform::wait_for_exit()
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2013-01-03 13:20:24 +01:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block until a signal occurs.
|
|
|
|
*/
|
2013-02-25 21:18:26 +01:00
|
|
|
_wait_for_exit_sem.down();
|
2013-01-03 13:20:24 +01:00
|
|
|
|
|
|
|
/*
|
2013-02-25 21:18:26 +01:00
|
|
|
* Each time, the '_wait_for_exit_sem' gets unlocked, we could have
|
2013-01-03 13:20:24 +01:00
|
|
|
* received either a SIGINT or SIGCHLD. If a SIGINT was received, the
|
|
|
|
* '_exit' condition will be set.
|
|
|
|
*/
|
|
|
|
if (_do_exit)
|
2017-05-11 00:07:54 +02:00
|
|
|
break;
|
2013-01-03 13:20:24 +01:00
|
|
|
|
|
|
|
/*
|
2013-01-03 16:23:11 +01:00
|
|
|
* Reflect SIGCHLD as exception signal to the signal context of the CPU
|
|
|
|
* session of the process. Because multiple children could have been
|
2013-01-18 07:10:47 +01:00
|
|
|
* terminated, we iterate until 'pollpid' (wrapper around 'wait4')
|
2013-01-03 16:23:11 +01:00
|
|
|
* returns -1.
|
2013-01-03 13:20:24 +01:00
|
|
|
*/
|
2013-01-03 16:23:11 +01:00
|
|
|
for (;;) {
|
|
|
|
int const pid = lx_pollpid();
|
|
|
|
|
2013-02-21 15:53:18 +01:00
|
|
|
if (pid <= 0)
|
2013-01-03 16:23:11 +01:00
|
|
|
break;
|
2013-01-03 13:20:24 +01:00
|
|
|
|
2013-01-03 16:23:11 +01:00
|
|
|
Platform_thread::submit_exception(pid);
|
|
|
|
}
|
2013-01-03 13:20:24 +01:00
|
|
|
}
|
2020-01-22 16:58:21 +01:00
|
|
|
lx_exit_group(0);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
2012-08-09 16:52:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************
|
|
|
|
** Support for IPC library **
|
|
|
|
*****************************/
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
2016-03-18 22:53:25 +01:00
|
|
|
Socket_pair server_socket_pair()
|
2012-08-09 16:52:47 +02:00
|
|
|
{
|
2016-05-04 12:27:17 +02:00
|
|
|
return create_server_socket_pair(Thread::myself()->native_thread().tid);
|
2012-08-09 16:52:47 +02:00
|
|
|
}
|
2012-11-15 12:53:32 +01:00
|
|
|
|
2016-03-18 22:53:25 +01:00
|
|
|
void destroy_server_socket_pair(Socket_pair socket_pair)
|
2012-11-15 12:53:32 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* As entrypoints in core are never destructed, this function is only
|
|
|
|
* called on IPC-client destruction. In this case, it's a no-op in core
|
|
|
|
* as well as in Genode processes.
|
|
|
|
*/
|
2016-03-18 22:53:25 +01:00
|
|
|
if (socket_pair.server_sd != -1 || socket_pair.client_sd != -1)
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error(__func__, " called for IPC server which should never happen");
|
2012-11-15 12:53:32 +01:00
|
|
|
}
|
2012-08-09 16:52:47 +02:00
|
|
|
}
|
|
|
|
|
2012-12-04 16:51:24 +01:00
|
|
|
|
|
|
|
/****************************************************
|
2016-04-15 15:19:22 +02:00
|
|
|
** Support for Platform_env_base::Region_map_mmap **
|
2012-12-04 16:51:24 +01:00
|
|
|
****************************************************/
|
|
|
|
|
2016-04-29 22:58:59 +02:00
|
|
|
size_t Region_map_mmap::_dataspace_size(Capability<Dataspace> ds_cap)
|
2012-12-04 16:51:24 +01:00
|
|
|
{
|
|
|
|
if (!ds_cap.valid())
|
2015-03-17 11:47:25 +01:00
|
|
|
return Local_capability<Dataspace>::deref(ds_cap)->size();
|
2012-12-04 16:51:24 +01:00
|
|
|
|
|
|
|
/* use local function call if called from the entrypoint */
|
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
2019-01-24 22:00:01 +01:00
|
|
|
return core_env().entrypoint().apply(ds_cap, [] (Dataspace *ds) {
|
2015-08-10 13:34:16 +02:00
|
|
|
return ds ? ds->size() : 0; });
|
2012-12-04 16:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-29 22:58:59 +02:00
|
|
|
int Region_map_mmap::_dataspace_fd(Capability<Dataspace> ds_cap)
|
2012-12-04 16:51:24 +01:00
|
|
|
{
|
|
|
|
Capability<Linux_dataspace> lx_ds_cap = static_cap_cast<Linux_dataspace>(ds_cap);
|
|
|
|
|
2013-04-24 14:10:15 +02:00
|
|
|
/*
|
|
|
|
* Return a duplicate of the dataspace file descriptor, which will be freed
|
2016-04-15 15:19:22 +02:00
|
|
|
* immediately after mmap'ing the file (see 'Region_map_mmap').
|
2013-04-24 14:10:15 +02:00
|
|
|
*
|
|
|
|
* Handing out the original file descriptor would result in the premature
|
|
|
|
* release of the descriptor. So the descriptor could be reused (i.e., as a
|
|
|
|
* socket descriptor during the RPC handling). When later destroying the
|
|
|
|
* dataspace, the descriptor would unexpectedly be closed again.
|
|
|
|
*/
|
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
2019-01-24 22:00:01 +01:00
|
|
|
return core_env().entrypoint().apply(lx_ds_cap, [] (Linux_dataspace *ds) {
|
2016-06-15 15:04:54 +02:00
|
|
|
return ds ? lx_dup(Capability_space::ipc_cap_data(ds->fd()).dst.socket) : -1; });
|
2012-12-04 16:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-29 22:58:59 +02:00
|
|
|
bool Region_map_mmap::_dataspace_writable(Dataspace_capability ds_cap)
|
2012-12-04 16:51:24 +01:00
|
|
|
{
|
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
2019-01-24 22:00:01 +01:00
|
|
|
return core_env().entrypoint().apply(ds_cap, [] (Dataspace *ds) {
|
2015-08-10 13:34:16 +02:00
|
|
|
return ds ? ds->writable() : false; });
|
2012-12-04 16:51:24 +01:00
|
|
|
}
|