2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief NOVA-specific implementation of the Thread API
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Sebastian Sumpf
|
2012-08-01 16:16:51 +02:00
|
|
|
* \author Alexander Boettcher
|
2011-12-22 16:19:25 +01:00
|
|
|
* \date 2010-01-19
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2010-2013 Genode Labs GmbH
|
2011-12-22 16:19:25 +01: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>
|
|
|
|
|
2012-08-08 14:23:13 +02:00
|
|
|
#include <base/rpc_client.h>
|
|
|
|
#include <session/session.h>
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/* NOVA includes */
|
|
|
|
#include <nova/syscalls.h>
|
2012-08-03 10:56:08 +02:00
|
|
|
#include <nova/util.h>
|
2012-08-08 14:23:13 +02:00
|
|
|
#include <nova_cpu_session/connection.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point entered by new threads
|
|
|
|
*/
|
|
|
|
void Thread_base::_thread_start()
|
|
|
|
{
|
2013-07-09 17:06:02 +02:00
|
|
|
using namespace Genode;
|
|
|
|
|
2014-05-16 10:27:24 +02:00
|
|
|
/* catch any exception at this point and try to print an error message */
|
2013-07-09 17:06:02 +02:00
|
|
|
try {
|
2014-05-16 10:27:24 +02:00
|
|
|
Thread_base::myself()->entry();
|
|
|
|
} catch (...) {
|
|
|
|
char thread_name[48];
|
|
|
|
Thread_base::myself()->name(thread_name, sizeof(thread_name));
|
2013-07-09 17:06:02 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
PERR("Thread '%s' died because of an uncaught exception", thread_name);
|
2014-05-16 10:27:24 +02:00
|
|
|
} catch (...) {
|
|
|
|
/* die in a noisy way */
|
|
|
|
nova_die();
|
2013-07-09 17:06:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:27:24 +02:00
|
|
|
throw;
|
2013-07-09 17:06:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:27:24 +02:00
|
|
|
Thread_base::myself()->_join_lock.unlock();
|
|
|
|
|
2013-07-09 17:06:02 +02:00
|
|
|
/* sleep silently */
|
2011-12-22 16:19:25 +01:00
|
|
|
Genode::sleep_forever();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
** Thread base **
|
|
|
|
*****************/
|
|
|
|
|
2015-03-27 14:05:55 +01:00
|
|
|
void Thread_base::_init_platform_thread(size_t weight, Type type)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
using namespace Nova;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate capability selectors for the thread's execution context,
|
2012-08-08 14:23:13 +02:00
|
|
|
* running semaphore and exception handler portals.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
2014-01-28 14:30:36 +01:00
|
|
|
_tid.ec_sel = Native_thread::INVALID_INDEX;
|
|
|
|
|
|
|
|
/* for main threads the member initialization differs */
|
|
|
|
if (type == MAIN || type == REINITIALIZED_MAIN) {
|
|
|
|
_thread_cap = env()->parent()->main_thread_cap();
|
2014-02-12 16:22:22 +01:00
|
|
|
|
|
|
|
Genode::Native_capability pager_cap(Nova::PT_SEL_MAIN_PAGER);
|
|
|
|
_pager_cap = reinterpret_cap_cast<Pager_object>(pager_cap);
|
|
|
|
|
2014-01-28 14:30:36 +01:00
|
|
|
_tid.exc_pt_sel = 0;
|
2014-02-12 16:22:22 +01:00
|
|
|
_tid.ec_sel = Nova::PT_SEL_MAIN_EC;
|
|
|
|
|
2014-12-12 20:15:02 +01:00
|
|
|
request_native_ec_cap(_pager_cap, _tid.ec_sel);
|
2014-01-28 14:30:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-30 15:28:25 +02:00
|
|
|
/*
|
|
|
|
* Revoke possible left-over UTCB of a previously destroyed thread
|
|
|
|
* which used this context location.
|
|
|
|
*
|
|
|
|
* This cannot be done in '_deinit_platform_thread()', because a
|
|
|
|
* self-destructing thread needs its UTCB to call
|
|
|
|
* 'Cpu_session::kill_thread()' and is not able to revoke the UTCB
|
|
|
|
* afterwards.
|
|
|
|
*/
|
|
|
|
Rights rwx(true, true, true);
|
|
|
|
addr_t utcb = reinterpret_cast<addr_t>(&_context->utcb);
|
|
|
|
revoke(Mem_crd(utcb >> 12, 0, rwx));
|
|
|
|
|
2013-09-11 10:45:23 +02:00
|
|
|
_tid.exc_pt_sel = cap_map()->insert(NUM_INITIAL_PT_LOG2);
|
2013-09-23 09:07:33 +02:00
|
|
|
if (_tid.exc_pt_sel == Native_thread::INVALID_INDEX)
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2014-04-03 14:18:52 +02:00
|
|
|
/* if no cpu session is given, use it from the environment */
|
|
|
|
if (!_cpu_session)
|
|
|
|
_cpu_session = env()->cpu_session();
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/* create thread at core */
|
|
|
|
char buf[48];
|
|
|
|
name(buf, sizeof(buf));
|
2012-08-02 12:20:00 +02:00
|
|
|
|
2015-03-27 14:05:55 +01:00
|
|
|
_thread_cap = _cpu_session->create_thread(weight, buf);
|
2012-08-02 12:20:00 +02:00
|
|
|
if (!_thread_cap.valid())
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2012-08-08 14:23:13 +02:00
|
|
|
/* assign thread to protection domain */
|
2012-08-02 12:20:00 +02:00
|
|
|
if (env()->pd_session()->bind_thread(_thread_cap))
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread_base::_deinit_platform_thread()
|
|
|
|
{
|
2012-08-01 16:16:51 +02:00
|
|
|
using namespace Nova;
|
|
|
|
|
2013-09-11 10:45:23 +02:00
|
|
|
if (_tid.ec_sel != Native_thread::INVALID_INDEX) {
|
2013-09-26 14:31:56 +02:00
|
|
|
revoke(Obj_crd(_tid.ec_sel, 1));
|
2013-09-11 10:45:23 +02:00
|
|
|
cap_map()->remove(_tid.ec_sel, 1, false);
|
2012-08-01 16:16:51 +02:00
|
|
|
}
|
2012-06-29 14:59:32 +02:00
|
|
|
|
2012-07-13 12:48:27 +02:00
|
|
|
/* de-announce thread */
|
2013-09-11 10:45:23 +02:00
|
|
|
if (_thread_cap.valid())
|
2014-04-03 14:18:52 +02:00
|
|
|
_cpu_session->kill_thread(_thread_cap);
|
2012-07-30 10:56:07 +02:00
|
|
|
|
2015-09-22 14:39:48 +02:00
|
|
|
cap_map()->remove(_tid.exc_pt_sel, NUM_INITIAL_PT_LOG2);
|
|
|
|
|
2013-09-11 10:45:23 +02:00
|
|
|
if (_pager_cap.valid())
|
2013-01-18 09:38:19 +01:00
|
|
|
env()->rm_session()->remove_client(_pager_cap);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread_base::start()
|
|
|
|
{
|
2013-07-22 09:14:40 +02:00
|
|
|
if (_tid.ec_sel < Native_thread::INVALID_INDEX - 1)
|
2012-08-08 14:23:13 +02:00
|
|
|
throw Cpu_session::Thread_creation_failed();
|
|
|
|
|
2013-07-22 09:14:40 +02:00
|
|
|
/*
|
|
|
|
* Default: create global thread - ec.sel == INVALID_INDEX
|
|
|
|
* create local thread - ec.sel == INVALID_INDEX - 1
|
|
|
|
*/
|
|
|
|
bool global = _tid.ec_sel == Native_thread::INVALID_INDEX;
|
|
|
|
|
2012-08-08 14:23:13 +02:00
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
/* create new pager object and assign it to the new thread */
|
2013-01-18 09:38:19 +01:00
|
|
|
_pager_cap = env()->rm_session()->add_client(_thread_cap);
|
|
|
|
if (!_pager_cap.valid())
|
2012-08-02 12:20:00 +02:00
|
|
|
throw Cpu_session::Thread_creation_failed();
|
|
|
|
|
2014-04-03 14:18:52 +02:00
|
|
|
if (_cpu_session->set_pager(_thread_cap, _pager_cap))
|
2012-08-02 12:20:00 +02:00
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2012-08-08 14:23:13 +02:00
|
|
|
|
|
|
|
/* create EC at core */
|
2012-11-12 17:48:18 +01:00
|
|
|
Thread_state state;
|
2012-08-22 12:10:04 +02:00
|
|
|
state.sel_exc_base = _tid.exc_pt_sel;
|
|
|
|
state.is_vcpu = _tid.is_vcpu;
|
|
|
|
|
2013-07-22 09:14:40 +02:00
|
|
|
/* local thread have no start instruction pointer - set via portal entry */
|
|
|
|
addr_t thread_ip = global ? reinterpret_cast<addr_t>(_thread_start) : 0;
|
|
|
|
|
2014-04-03 14:18:52 +02:00
|
|
|
try { _cpu_session->state(_thread_cap, state); }
|
2012-11-12 17:48:18 +01:00
|
|
|
catch (...) { throw Cpu_session::Thread_creation_failed(); }
|
2013-07-22 09:14:40 +02:00
|
|
|
|
2014-04-03 14:18:52 +02:00
|
|
|
if (_cpu_session->start(_thread_cap, thread_ip, _context->stack_top()))
|
2012-08-08 14:23:13 +02:00
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2012-08-22 12:10:04 +02:00
|
|
|
|
2012-08-08 14:23:13 +02:00
|
|
|
/* request native EC thread cap */
|
2013-09-11 10:45:23 +02:00
|
|
|
_tid.ec_sel = cap_map()->insert(1);
|
2013-09-25 11:53:49 +02:00
|
|
|
if (_tid.ec_sel == Native_thread::INVALID_INDEX)
|
2012-08-22 12:10:04 +02:00
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2013-09-25 11:53:49 +02:00
|
|
|
|
2014-12-12 20:15:02 +01:00
|
|
|
/* requested pager cap used by request_native_ec_cap in Signal_source_client */
|
|
|
|
enum { MAP_PAGER_CAP = 1 };
|
|
|
|
request_native_ec_cap(_pager_cap, _tid.ec_sel, MAP_PAGER_CAP);
|
2012-08-08 14:23:13 +02:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
using namespace Nova;
|
|
|
|
|
2012-08-08 17:12:39 +02:00
|
|
|
/* request exception portals for normal threads */
|
|
|
|
if (!_tid.is_vcpu) {
|
2013-09-23 10:44:50 +02:00
|
|
|
request_event_portal(_pager_cap, _tid.exc_pt_sel, 0, NUM_INITIAL_PT_LOG2);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2013-02-27 10:42:39 +01:00
|
|
|
/* default: we don't accept any mappings or translations */
|
|
|
|
Utcb * utcb_obj = reinterpret_cast<Utcb *>(utcb());
|
|
|
|
utcb_obj->crd_rcv = Obj_crd();
|
|
|
|
utcb_obj->crd_xlt = Obj_crd();
|
|
|
|
}
|
2013-02-06 10:25:45 +01:00
|
|
|
|
2013-07-22 09:14:40 +02:00
|
|
|
if (global)
|
|
|
|
/* request creation of SC to let thread run*/
|
2014-04-03 14:18:52 +02:00
|
|
|
_cpu_session->resume(_thread_cap);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread_base::cancel_blocking()
|
|
|
|
{
|
2012-08-03 10:56:08 +02:00
|
|
|
using namespace Nova;
|
|
|
|
|
|
|
|
if (sm_ctrl(_tid.exc_pt_sel + SM_SEL_EC, SEMAPHORE_UP))
|
|
|
|
nova_die();
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|