2012-05-30 20:13:09 +02:00
|
|
|
/**
|
2014-07-09 12:03:17 +02:00
|
|
|
* \brief Implementations for the start of a thread
|
2012-05-30 20:13:09 +02:00
|
|
|
* \author Martin Stein
|
2014-04-28 21:31:57 +02:00
|
|
|
* \author Stefan Kalkowski
|
2012-05-30 20:13:09 +02:00
|
|
|
* \date 2012-02-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
2012-05-30 20:13:09 +02: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/thread.h>
|
|
|
|
#include <base/printf.h>
|
|
|
|
#include <base/sleep.h>
|
|
|
|
#include <base/env.h>
|
|
|
|
|
2016-01-23 14:42:55 +01:00
|
|
|
/* base-internal includes */
|
|
|
|
#include <base/internal/stack_allocator.h>
|
2016-03-08 16:59:43 +01:00
|
|
|
#include <base/internal/native_utcb.h>
|
2016-01-23 14:42:55 +01:00
|
|
|
|
2012-05-30 20:13:09 +02:00
|
|
|
using namespace Genode;
|
|
|
|
|
2016-01-23 14:42:55 +01:00
|
|
|
namespace Genode { Rm_session * env_stack_area_rm_session(); }
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2015-05-19 14:18:40 +02:00
|
|
|
namespace Hw {
|
|
|
|
extern Ram_dataspace_capability _main_thread_utcb_ds;
|
|
|
|
extern Untyped_capability _main_thread_cap;
|
|
|
|
}
|
2014-04-28 21:31:57 +02:00
|
|
|
|
2012-05-30 20:13:09 +02:00
|
|
|
/*****************
|
|
|
|
** Thread_base **
|
|
|
|
*****************/
|
|
|
|
|
2015-03-27 14:05:55 +01:00
|
|
|
void Thread_base::_init_platform_thread(size_t weight, Type type)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
2014-05-21 17:59:12 +02:00
|
|
|
if (!_cpu_session) { _cpu_session = env()->cpu_session(); }
|
|
|
|
if (type == NORMAL) {
|
|
|
|
|
|
|
|
/* create server object */
|
|
|
|
char buf[48];
|
|
|
|
name(buf, sizeof(buf));
|
2016-01-23 14:42:55 +01:00
|
|
|
addr_t const utcb = (addr_t)&_stack->utcb();
|
2015-03-27 14:05:55 +01:00
|
|
|
_thread_cap = _cpu_session->create_thread(weight, buf, utcb);
|
2014-05-21 17:59:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-04-28 21:31:57 +02:00
|
|
|
/* if we got reinitialized we have to get rid of the old UTCB */
|
2016-01-23 14:42:55 +01:00
|
|
|
size_t const utcb_size = sizeof(Native_utcb);
|
2016-03-03 17:57:29 +01:00
|
|
|
addr_t const stack_area = stack_area_virtual_base();
|
2016-01-23 14:42:55 +01:00
|
|
|
addr_t const utcb_new = (addr_t)&_stack->utcb() - stack_area;
|
|
|
|
Rm_session * const rm = env_stack_area_rm_session();
|
|
|
|
|
2014-04-28 21:31:57 +02:00
|
|
|
if (type == REINITIALIZED_MAIN) { rm->detach(utcb_new); }
|
|
|
|
|
2016-01-23 14:42:55 +01:00
|
|
|
/* remap initial main-thread UTCB according to stack-area spec */
|
2015-05-19 14:18:40 +02:00
|
|
|
try { rm->attach_at(Hw::_main_thread_utcb_ds, utcb_new, utcb_size); }
|
2014-04-28 21:31:57 +02:00
|
|
|
catch(...) {
|
|
|
|
PERR("failed to re-map UTCB");
|
|
|
|
while (1) ;
|
|
|
|
}
|
|
|
|
/* adjust initial object state in case of a main thread */
|
2015-05-19 14:18:40 +02:00
|
|
|
tid().cap = Hw::_main_thread_cap;
|
|
|
|
_thread_cap = env()->parent()->main_thread_cap();
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 21:31:57 +02:00
|
|
|
void Thread_base::_deinit_platform_thread()
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
2014-04-28 21:31:57 +02:00
|
|
|
if (!_cpu_session)
|
|
|
|
_cpu_session = env()->cpu_session();
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2014-04-28 21:31:57 +02:00
|
|
|
_cpu_session->kill_thread(_thread_cap);
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2016-01-23 14:42:55 +01:00
|
|
|
/* detach userland stack */
|
|
|
|
size_t const size = sizeof(_stack->utcb());
|
|
|
|
addr_t utcb = Stack_allocator::addr_to_base(_stack) +
|
2016-03-03 17:57:29 +01:00
|
|
|
stack_virtual_size() - size - stack_area_virtual_base();
|
2016-01-23 14:42:55 +01:00
|
|
|
env_stack_area_rm_session()->detach(utcb);
|
2012-10-09 15:41:40 +02:00
|
|
|
|
2013-09-18 23:40:38 +02:00
|
|
|
if (_pager_cap.valid()) {
|
|
|
|
env()->rm_session()->remove_client(_pager_cap);
|
|
|
|
}
|
2012-10-09 15:41:40 +02:00
|
|
|
}
|
2012-05-30 20:13:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
void Thread_base::start()
|
|
|
|
{
|
|
|
|
/* assign thread to protection domain */
|
|
|
|
env()->pd_session()->bind_thread(_thread_cap);
|
|
|
|
|
2013-11-19 15:13:24 +01:00
|
|
|
/* create pager object and assign it to the thread */
|
2013-01-18 09:38:19 +01:00
|
|
|
_pager_cap = env()->rm_session()->add_client(_thread_cap);
|
2014-04-03 14:18:52 +02:00
|
|
|
_cpu_session->set_pager(_thread_cap, _pager_cap);
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2016-01-23 14:42:55 +01:00
|
|
|
/* attach userland stack */
|
2012-05-30 20:13:09 +02:00
|
|
|
try {
|
2014-04-03 14:18:52 +02:00
|
|
|
Ram_dataspace_capability ds = _cpu_session->utcb(_thread_cap);
|
2016-01-23 14:42:55 +01:00
|
|
|
size_t const size = sizeof(_stack->utcb());
|
|
|
|
addr_t dst = Stack_allocator::addr_to_base(_stack) +
|
2016-03-03 17:57:29 +01:00
|
|
|
stack_virtual_size() - size - stack_area_virtual_base();
|
2016-01-23 14:42:55 +01:00
|
|
|
env_stack_area_rm_session()->attach_at(ds, dst, size);
|
2012-05-30 20:13:09 +02:00
|
|
|
} catch (...) {
|
2016-01-23 14:42:55 +01:00
|
|
|
PERR("failed to attach userland stack");
|
2012-05-30 20:13:09 +02:00
|
|
|
sleep_forever();
|
|
|
|
}
|
|
|
|
/* start thread with its initial IP and aligned SP */
|
2016-01-23 14:42:55 +01:00
|
|
|
_cpu_session->start(_thread_cap, (addr_t)_thread_start, _stack->top());
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-18 23:40:38 +02:00
|
|
|
void Thread_base::cancel_blocking()
|
|
|
|
{
|
2014-04-03 14:18:52 +02:00
|
|
|
_cpu_session->cancel_blocking(_thread_cap);
|
2013-09-18 23:40:38 +02:00
|
|
|
}
|