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/cap_sel_alloc.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()
|
|
|
|
{
|
|
|
|
Genode::Thread_base::myself()->entry();
|
2012-11-16 13:53:37 +01:00
|
|
|
Thread_base::myself()->_join_lock.unlock();
|
2011-12-22 16:19:25 +01:00
|
|
|
Genode::sleep_forever();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
** Thread base **
|
|
|
|
*****************/
|
|
|
|
|
|
|
|
void Thread_base::_init_platform_thread()
|
|
|
|
{
|
|
|
|
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
|
|
|
*/
|
2012-08-08 14:23:13 +02:00
|
|
|
_tid.ec_sel = ~0UL;
|
2011-12-22 16:19:25 +01:00
|
|
|
_tid.exc_pt_sel = cap_selector_allocator()->alloc(NUM_INITIAL_PT_LOG2);
|
|
|
|
|
|
|
|
/* create thread at core */
|
|
|
|
char buf[48];
|
|
|
|
name(buf, sizeof(buf));
|
2012-08-02 12:20:00 +02:00
|
|
|
|
2012-07-13 12:48:27 +02:00
|
|
|
_thread_cap = env()->cpu_session()->create_thread(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;
|
|
|
|
|
|
|
|
if (_tid.ec_sel != ~0UL) {
|
|
|
|
revoke(Obj_crd(_tid.ec_sel, 0));
|
|
|
|
cap_selector_allocator()->free(_tid.ec_sel, 0);
|
|
|
|
}
|
2012-06-29 14:59:32 +02:00
|
|
|
|
2012-08-01 16:16:51 +02:00
|
|
|
revoke(Obj_crd(_tid.exc_pt_sel, NUM_INITIAL_PT_LOG2));
|
|
|
|
cap_selector_allocator()->free(_tid.exc_pt_sel, NUM_INITIAL_PT_LOG2);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* revoke utcb */
|
2012-08-01 16:16:51 +02:00
|
|
|
Rights rwx(true, true, true);
|
2012-06-19 15:54:41 +02:00
|
|
|
addr_t utcb = reinterpret_cast<addr_t>(&_context->utcb);
|
2012-08-01 16:16:51 +02:00
|
|
|
revoke(Mem_crd(utcb >> 12, 0, rwx));
|
2012-07-13 12:48:27 +02:00
|
|
|
|
|
|
|
/* de-announce thread */
|
2013-01-18 09:38:19 +01:00
|
|
|
if (_thread_cap.valid()) {
|
|
|
|
env()->cpu_session()->kill_thread(_thread_cap);
|
|
|
|
revoke(_thread_cap.local_name(), 0);
|
|
|
|
cap_selector_allocator()->free(_thread_cap.local_name(), 0);
|
|
|
|
}
|
2012-07-30 10:56:07 +02:00
|
|
|
|
2013-01-18 09:38:19 +01:00
|
|
|
if (_pager_cap.valid()) {
|
|
|
|
env()->rm_session()->remove_client(_pager_cap);
|
|
|
|
revoke(_pager_cap.local_name(), 0);
|
|
|
|
cap_selector_allocator()->free(_pager_cap.local_name(), 0);
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread_base::start()
|
|
|
|
{
|
2012-08-08 14:23:13 +02:00
|
|
|
if (_tid.ec_sel != ~0UL)
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2013-01-18 09:38:19 +01:00
|
|
|
if (env()->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 */
|
|
|
|
addr_t thread_sp = reinterpret_cast<addr_t>(&_context->stack[-4]);
|
|
|
|
|
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;
|
|
|
|
|
2012-11-12 17:48:18 +01:00
|
|
|
try { env()->cpu_session()->state(_thread_cap, state); }
|
|
|
|
catch (...) { throw Cpu_session::Thread_creation_failed(); }
|
|
|
|
if (env()->cpu_session()->start(_thread_cap, (addr_t)_thread_start,
|
2012-08-22 12:10:04 +02:00
|
|
|
thread_sp))
|
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 */
|
2012-10-10 13:59:32 +02:00
|
|
|
Genode::Cpu_session_client cpu(env()->cpu_session_cap());
|
2012-08-08 14:23:13 +02:00
|
|
|
Native_capability ec_cap = cpu.native_cap(_thread_cap);
|
2012-08-22 12:10:04 +02:00
|
|
|
if (!ec_cap.valid())
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
2012-07-30 10:56:07 +02:00
|
|
|
_tid.ec_sel = ec_cap.local_name();
|
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) {
|
2012-08-23 11:02:31 +02:00
|
|
|
for (unsigned i = 0; i < PT_SEL_PARENT; i++)
|
2013-01-18 09:38:19 +01:00
|
|
|
request_event_portal(_pager_cap, _tid.exc_pt_sel, i);
|
2012-08-23 11:02:31 +02:00
|
|
|
|
2013-01-18 09:38:19 +01:00
|
|
|
request_event_portal(_pager_cap, _tid.exc_pt_sel, PT_SEL_STARTUP);
|
|
|
|
request_event_portal(_pager_cap, _tid.exc_pt_sel, SM_SEL_EC);
|
|
|
|
request_event_portal(_pager_cap, _tid.exc_pt_sel, PT_SEL_RECALL);
|
2012-08-08 17:12:39 +02:00
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2013-02-06 10:25:45 +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();
|
|
|
|
|
2012-08-08 14:23:13 +02:00
|
|
|
/* request creation of SC to let thread run*/
|
|
|
|
env()->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
|
|
|
}
|