2012-05-30 20:13:09 +02:00
|
|
|
/*
|
|
|
|
* \brief Thread facility
|
|
|
|
* \author Martin Stein
|
|
|
|
* \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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* core includes */
|
|
|
|
#include <platform_thread.h>
|
|
|
|
#include <core_env.h>
|
|
|
|
#include <rm_session_component.h>
|
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
namespace Kernel { unsigned core_id(); }
|
|
|
|
|
|
|
|
|
2012-10-09 15:41:40 +02:00
|
|
|
bool Platform_thread::_attaches_utcb_by_itself()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If this is a main thread outside of core it'll not manage its
|
|
|
|
* virtual context area by itself, as it is done for other threads
|
|
|
|
* through a sub RM-session.
|
|
|
|
*/
|
2013-06-06 14:18:36 +02:00
|
|
|
return _pd_id == Kernel::core_id() || !main_thread();
|
2012-10-09 15:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 11:54:12 +01:00
|
|
|
Weak_ptr<Address_space> Platform_thread::address_space()
|
|
|
|
{
|
|
|
|
return _address_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-09 15:41:40 +02:00
|
|
|
Platform_thread::~Platform_thread()
|
|
|
|
{
|
|
|
|
/* detach UTCB if main thread outside core */
|
|
|
|
if (!_attaches_utcb_by_itself()) {
|
|
|
|
assert(_rm_client);
|
|
|
|
Rm_session_component * const rm = _rm_client->member_rm_session();
|
|
|
|
rm->detach(_virt_utcb);
|
|
|
|
}
|
|
|
|
/* free UTCB */
|
|
|
|
if (_pd_id == Kernel::core_id()) {
|
|
|
|
Range_allocator * const ram = platform()->ram_alloc();
|
|
|
|
ram->free((void *)_phys_utcb, sizeof(Native_utcb));
|
|
|
|
} else {
|
|
|
|
Ram_session_component * const ram =
|
|
|
|
dynamic_cast<Ram_session_component *>(core_env()->ram_session());
|
|
|
|
assert(ram);
|
|
|
|
ram->free(_utcb);
|
|
|
|
}
|
|
|
|
/* destroy object at the kernel */
|
|
|
|
Kernel::delete_thread(_id);
|
|
|
|
}
|
|
|
|
|
2012-05-30 20:13:09 +02:00
|
|
|
Platform_thread::Platform_thread(const char * name,
|
|
|
|
Thread_base * const thread_base,
|
2012-12-19 14:46:48 +01:00
|
|
|
size_t const stack_size, unsigned const pd_id)
|
2012-05-30 20:13:09 +02:00
|
|
|
:
|
|
|
|
_thread_base(thread_base), _stack_size(stack_size),
|
2013-06-06 17:02:22 +02:00
|
|
|
_pd_id(pd_id), _rm_client(0), _virt_utcb(0), _main_thread(0)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
|
|
|
strncpy(_name, name, NAME_MAX_LEN);
|
|
|
|
|
|
|
|
/* create UTCB for a core thread */
|
|
|
|
Range_allocator * const ram = platform()->ram_alloc();
|
|
|
|
assert(ram->alloc_aligned(sizeof(Native_utcb), (void **)&_phys_utcb,
|
2012-11-28 22:50:08 +01:00
|
|
|
MIN_MAPPING_SIZE_LOG2).is_ok());
|
2012-05-30 20:13:09 +02:00
|
|
|
_virt_utcb = _phys_utcb;
|
|
|
|
|
|
|
|
/* common constructor parts */
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Platform_thread::Platform_thread(const char * name, unsigned int priority,
|
|
|
|
addr_t utcb)
|
|
|
|
:
|
|
|
|
_thread_base(0), _stack_size(0), _pd_id(0), _rm_client(0),
|
2013-06-06 17:02:22 +02:00
|
|
|
_virt_utcb((Native_utcb *)utcb), _main_thread(0)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
|
|
|
strncpy(_name, name, NAME_MAX_LEN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate UTCB backing store for a thread outside of core. Page alignment
|
|
|
|
* is done by RAM session by default. It's save to use core env because
|
|
|
|
* this cannot be its server activation thread.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
Ram_session_component * const ram =
|
|
|
|
dynamic_cast<Ram_session_component *>(core_env()->ram_session());
|
|
|
|
assert(ram);
|
|
|
|
_utcb = ram->alloc(sizeof(Native_utcb), 1);
|
|
|
|
_phys_utcb = (Native_utcb *)ram->phys_addr(_utcb);
|
|
|
|
}
|
|
|
|
catch (...) { assert(0); }
|
|
|
|
|
|
|
|
/* common constructor parts */
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 11:54:12 +01:00
|
|
|
int Platform_thread::join_pd(unsigned const pd_id, bool const main_thread,
|
|
|
|
Weak_ptr<Address_space> address_space)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
|
|
|
/* check if we're already in another PD */
|
|
|
|
if (_pd_id && _pd_id != pd_id) return -1;
|
|
|
|
|
|
|
|
/* denote configuration for start method */
|
2013-03-08 11:54:12 +01:00
|
|
|
_pd_id = pd_id;
|
|
|
|
_main_thread = main_thread;
|
|
|
|
_address_space = address_space;
|
2012-05-30 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Platform_thread::_init()
|
|
|
|
{
|
2012-10-09 15:41:40 +02:00
|
|
|
_id = Kernel::new_thread(_kernel_thread, this);
|
2012-05-30 20:13:09 +02:00
|
|
|
assert(_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Platform_thread::start(void * ip, void * sp, unsigned int cpu_no)
|
|
|
|
{
|
|
|
|
/* check thread attributes */
|
|
|
|
assert(_pd_id);
|
|
|
|
|
2012-10-09 15:41:40 +02:00
|
|
|
/* attach UTCB if the thread can't do this by itself */
|
|
|
|
if (!_attaches_utcb_by_itself())
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Declare page aligned virtual UTCB outside the context area.
|
|
|
|
* Kernel afterwards offers this as bootstrap argument to the thread.
|
|
|
|
*/
|
|
|
|
_virt_utcb = (Native_utcb *)((platform()->vm_start()
|
|
|
|
+ platform()->vm_size() - sizeof(Native_utcb))
|
|
|
|
& ~((1<<MIN_MAPPING_SIZE_LOG2)-1));
|
|
|
|
|
|
|
|
/* attach UTCB */
|
|
|
|
assert(_rm_client);
|
|
|
|
Rm_session_component * const rm = _rm_client->member_rm_session();
|
|
|
|
try { rm->attach(_utcb, 0, 0, true, _virt_utcb, 0); }
|
|
|
|
catch (...) { assert(0); }
|
|
|
|
}
|
|
|
|
/* let thread participate in CPU scheduling */
|
2012-11-07 07:56:22 +01:00
|
|
|
_tlb = Kernel::start_thread(this, ip, sp, cpu_no);
|
|
|
|
return _tlb ? 0 : -1;
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Platform_thread::pager(Pager_object * const pager)
|
|
|
|
{
|
|
|
|
/* announce pager thread to kernel */
|
|
|
|
Kernel::set_pager(pager->cap().dst(), _id);
|
|
|
|
|
|
|
|
/* get RM client from pager pointer */
|
|
|
|
_rm_client = dynamic_cast<Rm_client *>(pager);
|
|
|
|
assert(_rm_client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-05 15:38:06 +01:00
|
|
|
Genode::Pager_object * Platform_thread::pager() {
|
|
|
|
return _rm_client ? static_cast<Pager_object *>(_rm_client) : 0; }
|
2012-05-30 20:13:09 +02:00
|
|
|
|