genode/base-nova/src/base/thread/thread_nova.cc

144 lines
3.7 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief NOVA-specific implementation of the Thread API
* \author Norman Feske
* \author Sebastian Sumpf
* \date 2010-01-19
*/
/*
2012-01-03 15:35:05 +01:00
* Copyright (C) 2010-2012 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>
#include <base/rpc_client.h>
#include <session/session.h>
2011-12-22 16:19:25 +01:00
/* NOVA includes */
#include <nova/syscalls.h>
#include <base/nova_util.h>
#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();
Genode::sleep_forever();
}
/*****************
** Thread base **
*****************/
void Thread_base::_init_platform_thread()
{
using namespace Nova;
/*
* Allocate capability selectors for the thread's execution context,
* running semaphore and exception handler portals.
2011-12-22 16:19:25 +01:00
*/
_tid.ec_sel = ~0UL;
2011-12-22 16:19:25 +01:00
_tid.rs_sel = cap_selector_allocator()->alloc();
_tid.pd_sel = cap_selector_allocator()->pd_sel();
_tid.exc_pt_sel = cap_selector_allocator()->alloc(NUM_INITIAL_PT_LOG2);
/* create thread at core */
char buf[48];
name(buf, sizeof(buf));
_thread_cap = env()->cpu_session()->create_thread(buf);
2011-12-22 16:19:25 +01:00
/* assign thread to protection domain */
env()->pd_session()->bind_thread(_thread_cap);
2011-12-22 16:19:25 +01:00
/* create new pager object and assign it to the new thread */
Pager_capability pager_cap = env()->rm_session()->add_client(_thread_cap);
env()->cpu_session()->set_pager(_thread_cap, pager_cap);
2011-12-22 16:19:25 +01:00
/* create running semaphore required for locking */
uint8_t res = create_sm(_tid.rs_sel, _tid.pd_sel, 0);
if (res != NOVA_OK) {
PERR("create_sm returned %u", res);
throw Cpu_session::Thread_creation_failed();
}
2011-12-22 16:19:25 +01:00
}
void Thread_base::_deinit_platform_thread()
{
// Nova::revoke(Nova::Obj_crd(_tid.ec_sel, 0));
2011-12-22 16:19:25 +01:00
Nova::revoke(Nova::Obj_crd(_tid.rs_sel, 0));
Nova::revoke(Nova::Obj_crd(_tid.exc_pt_sel, Nova::NUM_INITIAL_PT_LOG2));
// cap_selector_allocator()->free(_tid.ec_sel, 0);
cap_selector_allocator()->free(_tid.rs_sel, 0);
cap_selector_allocator()->free(_tid.exc_pt_sel, Nova::NUM_INITIAL_PT_LOG2);
2011-12-22 16:19:25 +01:00
/* revoke utcb */
Nova::Rights rwx(true, true, true);
addr_t utcb = reinterpret_cast<addr_t>(&_context->utcb);
Nova::revoke(Nova::Mem_crd(utcb >> 12, 0, rwx));
/* de-announce thread */
env()->cpu_session()->kill_thread(_thread_cap);
revoke(_thread_cap.local_name(), 0);
cap_selector_allocator()->free(_thread_cap.local_name(), 0);
2011-12-22 16:19:25 +01:00
}
void Thread_base::start()
{
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 */
Pager_capability pager_cap = env()->rm_session()->add_client(_thread_cap);
env()->cpu_session()->set_pager(_thread_cap, pager_cap);
/* create EC at core */
addr_t thread_sp = reinterpret_cast<addr_t>(&_context->stack[-4]);
Genode::Nova_cpu_connection cpu;
if (cpu.start_exc_base_vcpu(_thread_cap, (addr_t)_thread_start,
thread_sp, _tid.exc_pt_sel))
throw Cpu_session::Thread_creation_failed();
/* request native EC thread cap */
Native_capability ec_cap = cpu.native_cap(_thread_cap);
_tid.ec_sel = ec_cap.local_name();
2011-12-22 16:19:25 +01:00
using namespace Nova;
/* request exception portals */
request_event_portal(pager_cap, _tid.exc_pt_sel, PT_SEL_STARTUP);
request_event_portal(pager_cap, _tid.exc_pt_sel, PT_SEL_PAGE_FAULT);
2011-12-22 16:19:25 +01: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()
{
Nova::sm_ctrl(_tid.rs_sel, Nova::SEMAPHORE_UP);
}