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-11-18 15:31:54 +01: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()
|
|
|
|
{
|
2013-08-30 13:30:19 +02:00
|
|
|
/* detach UTCB */
|
2012-10-09 15:41:40 +02:00
|
|
|
if (!_attaches_utcb_by_itself()) {
|
2013-09-18 13:16:17 +02:00
|
|
|
|
|
|
|
/* the RM client may be destructed before platform thread */
|
|
|
|
if (_rm_client) {
|
|
|
|
Rm_session_component * const rm = _rm_client->member_rm_session();
|
2013-11-18 15:31:54 +01:00
|
|
|
rm->detach(_utcb_virt);
|
2013-09-18 13:16:17 +02:00
|
|
|
}
|
2012-10-09 15:41:40 +02:00
|
|
|
}
|
|
|
|
/* free UTCB */
|
|
|
|
if (_pd_id == Kernel::core_id()) {
|
|
|
|
Range_allocator * const ram = platform()->ram_alloc();
|
2013-11-18 15:31:54 +01:00
|
|
|
ram->free(_utcb_phys, sizeof(Native_utcb));
|
2012-10-09 15:41:40 +02:00
|
|
|
} else {
|
|
|
|
Ram_session_component * const ram =
|
|
|
|
dynamic_cast<Ram_session_component *>(core_env()->ram_session());
|
|
|
|
assert(ram);
|
|
|
|
ram->free(_utcb);
|
|
|
|
}
|
2013-09-18 23:40:38 +02:00
|
|
|
/* release from pager */
|
|
|
|
if (_rm_client) {
|
|
|
|
Pager_object * const object = dynamic_cast<Pager_object *>(_rm_client);
|
|
|
|
assert(object);
|
|
|
|
Rm_session_component * const rm = _rm_client->member_rm_session();
|
|
|
|
assert(rm);
|
2013-11-14 13:29:47 +01:00
|
|
|
Pager_capability cap = reinterpret_cap_cast<Pager_object>(object->Object_pool<Pager_object>::Entry::cap());
|
2013-09-18 23:40:38 +02:00
|
|
|
rm->remove_client(cap);
|
|
|
|
}
|
2012-10-09 15:41:40 +02:00
|
|
|
/* destroy object at the kernel */
|
2013-11-18 17:00:33 +01:00
|
|
|
Kernel::kill_thread(_id);
|
2012-10-09 15:41:40 +02:00
|
|
|
}
|
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
|
2013-11-20 23:35:02 +01:00
|
|
|
Platform_thread::Platform_thread(size_t const stack_size,
|
|
|
|
unsigned const pd_id,
|
|
|
|
const char * const label)
|
2012-05-30 20:13:09 +02:00
|
|
|
:
|
2013-11-18 15:31:54 +01:00
|
|
|
_stack_size(stack_size),
|
|
|
|
_pd_id(pd_id),
|
|
|
|
_rm_client(0),
|
|
|
|
_utcb_virt(0),
|
2013-11-11 16:55:30 +01:00
|
|
|
_main_thread(0)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
2013-11-18 15:31:54 +01:00
|
|
|
strncpy(_label, label, LABEL_MAX_LEN);
|
2012-05-30 20:13:09 +02:00
|
|
|
|
|
|
|
/* create UTCB for a core thread */
|
|
|
|
Range_allocator * const ram = platform()->ram_alloc();
|
2013-11-18 12:47:14 +01:00
|
|
|
if (!ram->alloc_aligned(sizeof(Native_utcb), (void **)&_utcb_phys,
|
2013-08-30 13:30:19 +02:00
|
|
|
MIN_MAPPING_SIZE_LOG2).is_ok())
|
|
|
|
{
|
|
|
|
PERR("failed to allocate UTCB");
|
|
|
|
throw Cpu_session::Out_of_metadata();
|
|
|
|
}
|
2013-11-18 12:47:14 +01:00
|
|
|
_utcb_virt = _utcb_phys;
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
/* create kernel object */
|
|
|
|
_id = Kernel::new_thread(_kernel_thread, Kernel::Priority::MAX, _label);
|
|
|
|
if (!_id) {
|
|
|
|
PERR("failed to create kernel object");
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
|
|
|
}
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
Platform_thread::Platform_thread(const char * const label,
|
2013-11-20 23:35:02 +01:00
|
|
|
unsigned const virt_prio,
|
2013-11-18 15:31:54 +01:00
|
|
|
addr_t const utcb)
|
2012-05-30 20:13:09 +02:00
|
|
|
:
|
2013-11-18 15:31:54 +01:00
|
|
|
_stack_size(0),
|
|
|
|
_pd_id(0),
|
|
|
|
_rm_client(0),
|
2013-11-18 12:47:14 +01:00
|
|
|
_utcb_virt((Native_utcb *)utcb),
|
2013-11-11 16:55:30 +01:00
|
|
|
_main_thread(0)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
2013-11-18 15:31:54 +01:00
|
|
|
strncpy(_label, label, LABEL_MAX_LEN);
|
2012-05-30 20:13:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-08-30 13:30:19 +02:00
|
|
|
Ram_session_component * const ram =
|
|
|
|
dynamic_cast<Ram_session_component *>(core_env()->ram_session());
|
|
|
|
assert(ram);
|
|
|
|
try { _utcb = ram->alloc(sizeof(Native_utcb), 1); }
|
|
|
|
catch (...) {
|
|
|
|
PERR("failed to allocate UTCB");
|
|
|
|
throw Cpu_session::Out_of_metadata();
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
2013-11-18 12:47:14 +01:00
|
|
|
_utcb_phys = (Native_utcb *)ram->phys_addr(_utcb);
|
2012-05-30 20:13:09 +02:00
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
/* create kernel object */
|
2013-11-20 23:35:02 +01:00
|
|
|
enum { MAX_PRIO = Kernel::Priority::MAX };
|
|
|
|
auto const phys_prio = Cpu_session::scale_priority(MAX_PRIO, virt_prio);
|
|
|
|
_id = Kernel::new_thread(_kernel_thread, phys_prio, _label);
|
2013-11-18 15:31:54 +01:00
|
|
|
if (!_id) {
|
|
|
|
PERR("failed to create kernel object");
|
|
|
|
throw Cpu_session::Thread_creation_failed();
|
|
|
|
}
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-11-18 15:31:54 +01:00
|
|
|
/* check if thread is already in another protection domain */
|
2013-08-30 13:30:19 +02:00
|
|
|
if (_pd_id && _pd_id != pd_id) {
|
2013-11-18 15:31:54 +01:00
|
|
|
PERR("thread already in another protection domain");
|
2013-08-30 13:30:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-11-18 15:31:54 +01:00
|
|
|
/* join protection domain */
|
|
|
|
_pd_id = pd_id;
|
|
|
|
_main_thread = main_thread;
|
2013-03-08 11:54:12 +01:00
|
|
|
_address_space = address_space;
|
2012-05-30 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Platform_thread::_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 12:47:14 +01:00
|
|
|
int Platform_thread::start(void * const ip, void * const sp,
|
|
|
|
unsigned int const cpu_id)
|
2012-05-30 20:13:09 +02:00
|
|
|
{
|
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
|
|
|
{
|
2013-11-18 12:47:14 +01:00
|
|
|
/* declare page aligned virtual UTCB outside the context area */
|
|
|
|
_utcb_virt = (Native_utcb *)((platform()->vm_start()
|
2013-09-18 23:40:38 +02:00
|
|
|
+ platform()->vm_size() - sizeof(Native_utcb))
|
|
|
|
& ~((1<<MIN_MAPPING_SIZE_LOG2)-1));
|
2012-05-30 20:13:09 +02:00
|
|
|
|
|
|
|
/* attach UTCB */
|
2013-08-30 13:30:19 +02:00
|
|
|
if (!_rm_client) {
|
|
|
|
PERR("invalid RM client");
|
|
|
|
return -1;
|
|
|
|
};
|
2012-05-30 20:13:09 +02:00
|
|
|
Rm_session_component * const rm = _rm_client->member_rm_session();
|
2013-11-18 12:47:14 +01:00
|
|
|
try { rm->attach(_utcb, 0, 0, true, _utcb_virt, 0); }
|
2013-08-30 13:30:19 +02:00
|
|
|
catch (...) {
|
|
|
|
PERR("failed to attach UTCB");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
2013-11-18 12:47:14 +01:00
|
|
|
/* initialize thread regisers */
|
|
|
|
typedef Kernel::Thread_reg_id Reg_id;
|
|
|
|
enum { WRITES = 2 };
|
|
|
|
addr_t * write_regs = (addr_t *)Thread_base::myself()->utcb()->base();
|
|
|
|
write_regs[0] = Reg_id::IP;
|
|
|
|
write_regs[1] = Reg_id::SP;
|
2013-11-19 15:13:24 +01:00
|
|
|
addr_t write_values[] = {
|
2013-11-18 12:47:14 +01:00
|
|
|
(addr_t)ip,
|
2013-11-18 15:31:54 +01:00
|
|
|
_main_thread ? (addr_t)_utcb_virt : (addr_t)sp
|
2013-11-18 12:47:14 +01:00
|
|
|
};
|
|
|
|
if (Kernel::access_thread_regs(id(), 0, WRITES, 0, write_values)) {
|
|
|
|
PERR("failed to initialize thread registers");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-19 15:13:24 +01:00
|
|
|
/* start executing new thread */
|
|
|
|
_utcb_phys->startup_msg.init(_id);
|
|
|
|
_tlb = Kernel::start_thread(_id, cpu_id, _pd_id, _utcb_phys);
|
2013-08-30 13:30:19 +02:00
|
|
|
if (!_tlb) {
|
|
|
|
PERR("failed to start thread");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Platform_thread::pager(Pager_object * const pager)
|
|
|
|
{
|
2013-11-14 13:29:47 +01:00
|
|
|
typedef Kernel::Thread_event_id Event_id;
|
2013-09-18 12:57:01 +02:00
|
|
|
if (pager) {
|
2013-11-14 13:29:47 +01:00
|
|
|
unsigned const sc_id = pager->signal_context_id();
|
|
|
|
if (sc_id) {
|
|
|
|
if (!Kernel::route_thread_event(id(), Event_id::FAULT, sc_id)) {
|
|
|
|
_rm_client = dynamic_cast<Rm_client *>(pager);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PERR("failed to attach signal context to fault");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!Kernel::route_thread_event(id(), Event_id::FAULT, 0)) {
|
|
|
|
_rm_client = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PERR("failed to detach signal context from fault");
|
2013-09-18 12:57:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
2012-05-30 20:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-30 13:30:19 +02:00
|
|
|
Genode::Pager_object * Platform_thread::pager()
|
|
|
|
{
|
|
|
|
return _rm_client ? static_cast<Pager_object *>(_rm_client) : 0;
|
|
|
|
}
|
2013-11-11 13:03:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
addr_t const * cpu_state_regs();
|
|
|
|
|
|
|
|
size_t cpu_state_regs_length();
|
|
|
|
|
|
|
|
|
|
|
|
Thread_state Platform_thread::state()
|
|
|
|
{
|
|
|
|
static addr_t const * const src = cpu_state_regs();
|
|
|
|
static size_t const length = cpu_state_regs_length();
|
|
|
|
static size_t const size = length * sizeof(src[0]);
|
|
|
|
void * dst = Thread_base::myself()->utcb()->base();
|
|
|
|
Genode::memcpy(dst, src, size);
|
|
|
|
Thread_state thread_state;
|
|
|
|
Cpu_state * const cpu_state = static_cast<Cpu_state *>(&thread_state);
|
|
|
|
if (Kernel::access_thread_regs(id(), length, 0, (addr_t *)cpu_state, 0)) {
|
|
|
|
throw Cpu_session::State_access_failed();
|
|
|
|
}
|
|
|
|
return thread_state;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void Platform_thread::state(Thread_state thread_state)
|
|
|
|
{
|
|
|
|
static addr_t const * const src = cpu_state_regs();
|
|
|
|
static size_t const length = cpu_state_regs_length();
|
|
|
|
static size_t const size = length * sizeof(src[0]);
|
|
|
|
void * dst = Thread_base::myself()->utcb()->base();
|
|
|
|
Genode::memcpy(dst, src, size);
|
|
|
|
Cpu_state * const cpu_state = static_cast<Cpu_state *>(&thread_state);
|
|
|
|
if (Kernel::access_thread_regs(id(), 0, length, 0, (addr_t *)cpu_state)) {
|
|
|
|
throw Cpu_session::State_access_failed();
|
|
|
|
}
|
|
|
|
};
|