2014-04-09 13:24:55 +00:00
|
|
|
/*
|
|
|
|
* \brief Virtualbox adjusted pthread_create implementation
|
|
|
|
* \author Alexander Boettcher
|
|
|
|
* \date 2014-04-09
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-04-09 13:24:55 +00:00
|
|
|
*
|
|
|
|
* This file is distributed under the terms of the GNU General Public License
|
|
|
|
* version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode */
|
2019-05-22 08:41:30 +00:00
|
|
|
#include <base/attached_rom_dataspace.h>
|
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 17:07:09 +00:00
|
|
|
#include <base/log.h>
|
2014-04-09 13:24:55 +00:00
|
|
|
#include <base/thread.h>
|
2014-05-23 07:26:57 +00:00
|
|
|
#include <cpu_session/connection.h>
|
2014-04-09 13:24:55 +00:00
|
|
|
|
|
|
|
/* Genode libc pthread binding */
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 18:19:10 +00:00
|
|
|
#include <internal/thread_create.h>
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2014-07-16 19:43:41 +00:00
|
|
|
#include "sup.h"
|
2017-01-05 12:45:37 +00:00
|
|
|
#include "vmm.h"
|
2014-07-16 19:43:41 +00:00
|
|
|
|
2014-04-09 13:24:55 +00:00
|
|
|
/* libc */
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2015-07-24 16:38:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-04-09 13:24:55 +00:00
|
|
|
|
|
|
|
/* vbox */
|
|
|
|
#include <internal/thread.h>
|
|
|
|
|
2019-05-22 08:41:30 +00:00
|
|
|
static bool use_priorities()
|
|
|
|
{
|
|
|
|
Genode::Attached_rom_dataspace const platform(genode_env(), "platform_info");
|
|
|
|
Genode::Xml_node const kernel = platform.xml().sub_node("kernel");
|
|
|
|
return kernel.attribute_value("name", Genode::String<16>("unknown")) == "nova";
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:34:47 +00:00
|
|
|
static long prio_class(RTTHREADTYPE const type)
|
|
|
|
{
|
|
|
|
unsigned const VIRTUAL_GENODE_VBOX_LEVELS = 16;
|
|
|
|
static_assert (RTTHREADTYPE_END < VIRTUAL_GENODE_VBOX_LEVELS,
|
|
|
|
"prio levels exceeds VIRTUAL_GENODE_VBOX_LEVELS");
|
|
|
|
|
2019-05-22 08:41:30 +00:00
|
|
|
/* evaluate once */
|
|
|
|
static bool const priorities = use_priorities();
|
|
|
|
|
|
|
|
if (!priorities)
|
|
|
|
return Genode::Cpu_session::DEFAULT_PRIORITY;
|
|
|
|
|
2019-05-06 08:34:47 +00:00
|
|
|
return (VIRTUAL_GENODE_VBOX_LEVELS - type) *
|
|
|
|
Genode::Cpu_session::PRIORITY_LIMIT / VIRTUAL_GENODE_VBOX_LEVELS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Genode::Cpu_connection * cpu_connection(RTTHREADTYPE type)
|
|
|
|
{
|
2014-05-23 07:26:57 +00:00
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
static Cpu_connection * con[RTTHREADTYPE_END - 1];
|
|
|
|
static Lock lock;
|
|
|
|
|
|
|
|
Assert(type && type < RTTHREADTYPE_END);
|
|
|
|
|
|
|
|
Lock::Guard guard(lock);
|
|
|
|
|
|
|
|
if (con[type - 1])
|
|
|
|
return con[type - 1];
|
|
|
|
|
2017-01-05 12:45:37 +00:00
|
|
|
char * data = new (vmm_heap()) char[16];
|
2014-05-23 07:26:57 +00:00
|
|
|
|
2015-07-24 16:38:34 +00:00
|
|
|
Genode::snprintf(data, 16, "vbox %u", type);
|
2014-05-23 07:26:57 +00:00
|
|
|
|
2019-05-06 08:34:47 +00:00
|
|
|
con[type - 1] = new (vmm_heap()) Cpu_connection(genode_env(), data,
|
|
|
|
prio_class(type));
|
2014-05-23 07:26:57 +00:00
|
|
|
|
|
|
|
return con[type - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 12:53:12 +00:00
|
|
|
static int create_thread(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*start_routine) (void *), void *arg)
|
2014-09-23 11:01:47 +00:00
|
|
|
{
|
|
|
|
PRTTHREADINT rtthread = reinterpret_cast<PRTTHREADINT>(arg);
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2014-09-23 11:01:47 +00:00
|
|
|
Assert(rtthread);
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2016-03-08 15:59:43 +00:00
|
|
|
size_t const utcb_size = 4096;
|
|
|
|
|
2016-05-04 10:27:17 +00:00
|
|
|
size_t stack_size = Genode::Thread::stack_virtual_size() -
|
2016-03-08 15:59:43 +00:00
|
|
|
utcb_size - 2 * (1UL << 12);
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2014-09-23 11:01:47 +00:00
|
|
|
if (rtthread->cbStack < stack_size)
|
|
|
|
stack_size = rtthread->cbStack;
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2014-09-23 11:01:47 +00:00
|
|
|
/* sanity check - emt and vcpu thread have to have same prio class */
|
2015-07-24 16:38:34 +00:00
|
|
|
if (strstr(rtthread->szName, "EMT") == rtthread->szName)
|
2014-09-23 11:01:47 +00:00
|
|
|
Assert(rtthread->enmType == RTTHREADTYPE_EMULATION);
|
2014-05-23 07:26:57 +00:00
|
|
|
|
2014-09-23 11:01:47 +00:00
|
|
|
if (rtthread->enmType == RTTHREADTYPE_EMULATION) {
|
2015-07-24 16:38:34 +00:00
|
|
|
|
|
|
|
unsigned int cpu_id = 0;
|
|
|
|
sscanf(rtthread->szName, "EMT-%u", &cpu_id);
|
|
|
|
|
2015-06-16 12:53:12 +00:00
|
|
|
Genode::Cpu_session * cpu_session = cpu_connection(RTTHREADTYPE_EMULATION);
|
2015-07-24 16:38:34 +00:00
|
|
|
Genode::Affinity::Space space = cpu_session->affinity_space();
|
|
|
|
Genode::Affinity::Location location(space.location_of_index(cpu_id));
|
|
|
|
|
2018-04-26 12:58:57 +00:00
|
|
|
if (create_emt_vcpu(thread, stack_size, start_routine, arg,
|
2019-05-06 08:34:47 +00:00
|
|
|
cpu_session, location, cpu_id, rtthread->szName,
|
|
|
|
prio_class(rtthread->enmType)))
|
2014-09-23 11:01:47 +00:00
|
|
|
return 0;
|
2015-06-16 12:53:12 +00:00
|
|
|
/*
|
|
|
|
* The virtualization layer had no need to setup the EMT
|
|
|
|
* specially, so create it as a ordinary pthread.
|
|
|
|
*/
|
2014-09-23 11:01:47 +00:00
|
|
|
}
|
2014-05-23 07:26:57 +00:00
|
|
|
|
2018-11-21 16:17:25 +00:00
|
|
|
return Libc::pthread_create(thread, start_routine, arg,
|
|
|
|
stack_size, rtthread->szName,
|
|
|
|
cpu_connection(rtthread->enmType),
|
|
|
|
Genode::Affinity::Location());
|
2014-09-23 11:01:47 +00:00
|
|
|
}
|
2014-04-09 13:24:55 +00:00
|
|
|
|
2015-06-16 12:53:12 +00:00
|
|
|
extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*start_routine) (void *), void *arg)
|
|
|
|
{
|
|
|
|
PRTTHREADINT rtthread = reinterpret_cast<PRTTHREADINT>(arg);
|
|
|
|
|
|
|
|
/* retry thread creation once after CPU session upgrade */
|
|
|
|
for (unsigned i = 0; i < 2; i++) {
|
|
|
|
using namespace Genode;
|
|
|
|
|
Streamline exception types
This patch reduces the number of exception types by facilitating
globally defined exceptions for common usage patterns shared by most
services. In particular, RPC functions that demand a session-resource
upgrade not longer reflect this condition via a session-specific
exception but via the 'Out_of_ram' or 'Out_of_caps' types.
Furthermore, the 'Parent::Service_denied', 'Parent::Unavailable',
'Root::Invalid_args', 'Root::Unavailable', 'Service::Invalid_args',
'Service::Unavailable', and 'Local_service::Factory::Denied' types have
been replaced by the single 'Service_denied' exception type defined in
'session/session.h'.
This consolidation eases the error handling (there are fewer exceptions
to handle), alleviates the need to convert exceptions along the
session-creation call chain, and avoids possible aliasing problems
(catching the wrong type with the same name but living in a different
scope).
2017-05-07 20:03:22 +00:00
|
|
|
try { return create_thread(thread, attr, start_routine, arg); }
|
|
|
|
catch (Out_of_ram) {
|
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 17:07:09 +00:00
|
|
|
log("Upgrading memory for creation of "
|
|
|
|
"thread '", Cstring(rtthread->szName), "'");
|
2016-11-23 16:07:49 +00:00
|
|
|
cpu_connection(rtthread->enmType)->upgrade_ram(4096);
|
2019-05-22 08:41:30 +00:00
|
|
|
} catch (Genode::Signal_receiver::Signal_not_pending) {
|
|
|
|
error("signal not pending ?");
|
|
|
|
} catch (Out_of_caps) {
|
|
|
|
error("out of caps ...");
|
Streamline exception types
This patch reduces the number of exception types by facilitating
globally defined exceptions for common usage patterns shared by most
services. In particular, RPC functions that demand a session-resource
upgrade not longer reflect this condition via a session-specific
exception but via the 'Out_of_ram' or 'Out_of_caps' types.
Furthermore, the 'Parent::Service_denied', 'Parent::Unavailable',
'Root::Invalid_args', 'Root::Unavailable', 'Service::Invalid_args',
'Service::Unavailable', and 'Local_service::Factory::Denied' types have
been replaced by the single 'Service_denied' exception type defined in
'session/session.h'.
This consolidation eases the error handling (there are fewer exceptions
to handle), alleviates the need to convert exceptions along the
session-creation call chain, and avoids possible aliasing problems
(catching the wrong type with the same name but living in a different
scope).
2017-05-07 20:03:22 +00:00
|
|
|
}
|
|
|
|
catch (...) { break; }
|
2015-06-16 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
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 17:07:09 +00:00
|
|
|
Genode::error("could not create vbox pthread - halt");
|
2015-06-16 12:53:12 +00:00
|
|
|
Genode::Lock lock(Genode::Lock::LOCKED);
|
|
|
|
lock.lock();
|
|
|
|
return EAGAIN;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:01:47 +00:00
|
|
|
extern "C" int pthread_attr_setdetachstate(pthread_attr_t *, int)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int pthread_attr_setstacksize(pthread_attr_t *, size_t)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int pthread_atfork(void (*)(void), void (*)(void), void (*)(void))
|
|
|
|
{
|
|
|
|
return 0;
|
2014-04-09 13:24:55 +00:00
|
|
|
}
|