2012-11-30 14:08:42 +01:00
|
|
|
/*
|
2013-10-07 14:56:31 +02:00
|
|
|
* \brief Kernel backend for execution contexts in userland
|
2012-11-30 14:08:42 +01:00
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2012-11-30
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
2012-11-30 14:08:42 +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.
|
|
|
|
*/
|
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
#ifndef _KERNEL__THREAD_H_
|
|
|
|
#define _KERNEL__THREAD_H_
|
2012-11-30 14:08:42 +01:00
|
|
|
|
|
|
|
/* core includes */
|
2013-09-02 13:37:55 +02:00
|
|
|
#include <kernel/configuration.h>
|
2013-09-03 14:20:43 +02:00
|
|
|
#include <kernel/scheduler.h>
|
2013-09-09 15:20:30 +02:00
|
|
|
#include <kernel/signal_receiver.h>
|
|
|
|
#include <kernel/ipc_node.h>
|
2013-11-14 13:29:47 +01:00
|
|
|
#include <cpu_support.h>
|
2013-09-05 13:40:54 +02:00
|
|
|
#include <cpu.h>
|
2013-05-14 22:40:30 +02:00
|
|
|
|
2012-11-30 14:08:42 +01:00
|
|
|
namespace Kernel
|
|
|
|
{
|
2013-09-09 15:20:30 +02:00
|
|
|
class Thread;
|
2013-10-16 20:53:59 +02:00
|
|
|
class Pd;
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-18 17:01:51 +02:00
|
|
|
typedef Genode::Cpu Cpu;
|
|
|
|
typedef Genode::Native_utcb Native_utcb;
|
2013-09-09 15:20:30 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void reset_lap_time();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
|
|
|
/**
|
2013-09-09 15:20:30 +02:00
|
|
|
* Kernel backend for userland execution-contexts
|
2012-11-30 14:08:42 +01:00
|
|
|
*/
|
2013-09-09 15:20:30 +02:00
|
|
|
class Thread;
|
2013-10-07 14:56:31 +02:00
|
|
|
|
2013-10-11 12:29:40 +02:00
|
|
|
class Thread_ids : public Id_allocator<MAX_THREADS> { };
|
|
|
|
typedef Object_pool<Thread> Thread_pool;
|
2013-10-07 14:56:31 +02:00
|
|
|
|
|
|
|
Thread_ids * thread_ids();
|
|
|
|
Thread_pool * thread_pool();
|
2013-09-09 15:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Kernel::Thread
|
|
|
|
:
|
|
|
|
public Cpu::User_context,
|
2013-10-11 12:29:40 +02:00
|
|
|
public Object<Thread, MAX_THREADS, Thread_ids, thread_ids, thread_pool>,
|
2013-09-09 15:20:30 +02:00
|
|
|
public Execution_context,
|
|
|
|
public Ipc_node,
|
2013-09-12 00:48:27 +02:00
|
|
|
public Signal_context_killer,
|
|
|
|
public Signal_receiver_killer,
|
2013-11-14 13:29:47 +01:00
|
|
|
public Signal_handler,
|
|
|
|
public Thread_cpu_support
|
2013-09-09 15:20:30 +02:00
|
|
|
{
|
2013-11-14 13:29:47 +01:00
|
|
|
friend class Thread_event;
|
|
|
|
|
2013-09-09 15:20:30 +02:00
|
|
|
private:
|
|
|
|
|
2013-10-07 15:37:58 +02:00
|
|
|
enum { START_VERBOSE = 0 };
|
|
|
|
|
2013-05-22 14:41:47 +02:00
|
|
|
enum State
|
|
|
|
{
|
2013-10-16 13:10:54 +02:00
|
|
|
SCHEDULED = 1,
|
|
|
|
AWAITS_START = 2,
|
|
|
|
AWAITS_IPC = 3,
|
|
|
|
AWAITS_RESUME = 4,
|
2013-11-14 13:29:47 +01:00
|
|
|
AWAITS_SIGNAL = 5,
|
|
|
|
AWAITS_SIGNAL_CONTEXT_KILL = 6,
|
|
|
|
AWAITS_SIGNAL_RECEIVER_KILL = 7,
|
|
|
|
STOPPED = 8,
|
2013-05-22 14:41:47 +02:00
|
|
|
};
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
State _state;
|
|
|
|
Pd * _pd;
|
|
|
|
Native_utcb * _utcb_phys;
|
|
|
|
Signal_receiver * _signal_receiver;
|
|
|
|
char const * const _label;
|
2012-11-30 14:08:42 +01:00
|
|
|
|
|
|
|
/**
|
2013-10-16 20:53:59 +02:00
|
|
|
* Notice that another thread yielded the CPU to this thread
|
2012-11-30 14:08:42 +01:00
|
|
|
*/
|
2013-10-16 20:53:59 +02:00
|
|
|
void _receive_yielded_cpu();
|
2013-09-12 00:48:27 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
2013-11-14 13:29:47 +01:00
|
|
|
* Attach or detach the handler of a thread-triggered event
|
|
|
|
*
|
|
|
|
* \param event_id kernel name of the thread event
|
|
|
|
* \param signal_context_id kernel name signal context or 0 to detach
|
|
|
|
*
|
|
|
|
* \retval 0 succeeded
|
|
|
|
* \retval -1 failed
|
|
|
|
*/
|
|
|
|
int _route_event(unsigned const event_id,
|
|
|
|
unsigned const signal_context_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map kernel name of thread event to the corresponding member
|
|
|
|
*
|
|
|
|
* \param id kernel name of targeted thread event
|
|
|
|
*
|
|
|
|
* \retval 0 failed
|
|
|
|
* \retval >0 targeted member pointer
|
2013-10-16 20:53:59 +02:00
|
|
|
*/
|
2013-11-14 13:29:47 +01:00
|
|
|
Thread_event Thread::* _event(unsigned const id) const;
|
2013-09-12 00:48:27 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return wether this is a core thread
|
|
|
|
*/
|
|
|
|
bool _core() const;
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Resume execution rawly
|
|
|
|
*/
|
|
|
|
void _schedule();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Pause execution
|
|
|
|
*/
|
|
|
|
void _pause();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Suspend unrecoverably from execution
|
|
|
|
*/
|
|
|
|
void _stop();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Try to escape from blocking state, if in any, and resume execution
|
|
|
|
*
|
|
|
|
* \retval -1 failed
|
|
|
|
* \retval 0 succeeded, execution was paused
|
|
|
|
* \retval 1 succeeded, execution was not paused
|
|
|
|
*/
|
|
|
|
int _resume();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**
|
|
|
|
* Handle an exception thrown by the memory management unit
|
|
|
|
*/
|
|
|
|
void _mmu_exception();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 13:10:54 +02:00
|
|
|
/**
|
2013-11-14 17:29:34 +01:00
|
|
|
* Handle kernel-call request of this thread
|
2013-10-16 13:10:54 +02:00
|
|
|
*/
|
2013-11-14 17:29:34 +01:00
|
|
|
void _call();
|
2013-10-16 13:10:54 +02:00
|
|
|
|
2013-11-11 13:03:07 +01:00
|
|
|
/**
|
|
|
|
* Read a thread register
|
|
|
|
*
|
|
|
|
* \param id kernel name of targeted thread register
|
|
|
|
* \param value read-value buffer
|
|
|
|
*
|
|
|
|
* \retval 0 succeeded
|
|
|
|
* \retval -1 failed
|
|
|
|
*/
|
|
|
|
int _read_reg(addr_t const id, addr_t & value) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override a thread register
|
|
|
|
*
|
|
|
|
* \param id kernel name of targeted thread register
|
|
|
|
* \param value write-value buffer
|
|
|
|
*
|
|
|
|
* \retval 0 succeeded
|
|
|
|
* \retval -1 failed
|
|
|
|
*/
|
|
|
|
int _write_reg(addr_t const id, addr_t const value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map kernel names of thread registers to the corresponding data
|
|
|
|
*
|
|
|
|
* \param id kernel name of thread register
|
|
|
|
*
|
|
|
|
* \retval 0 failed
|
|
|
|
* \retval >0 pointer to register content
|
|
|
|
*/
|
2013-11-14 13:29:47 +01:00
|
|
|
addr_t Thread::* _reg(addr_t const id) const;
|
2013-11-11 13:03:07 +01:00
|
|
|
|
2013-10-16 13:10:54 +02:00
|
|
|
|
2013-11-14 17:29:34 +01:00
|
|
|
/****************************************************************
|
|
|
|
** Kernel-call backends, for details see 'kernel/interface.h' **
|
|
|
|
****************************************************************/
|
2013-10-16 13:10:54 +02:00
|
|
|
|
2013-11-14 13:29:47 +01:00
|
|
|
void _call_new_pd();
|
|
|
|
void _call_kill_pd();
|
|
|
|
void _call_new_thread();
|
|
|
|
void _call_delete_thread();
|
|
|
|
void _call_start_thread();
|
|
|
|
void _call_pause_thread();
|
|
|
|
void _call_resume_thread();
|
|
|
|
void _call_yield_thread();
|
|
|
|
void _call_current_thread_id();
|
|
|
|
void _call_wait_for_request();
|
|
|
|
void _call_request_and_wait();
|
|
|
|
void _call_reply();
|
|
|
|
void _call_update_pd();
|
|
|
|
void _call_update_region();
|
|
|
|
void _call_print_char();
|
|
|
|
void _call_new_signal_receiver();
|
|
|
|
void _call_new_signal_context();
|
|
|
|
void _call_await_signal();
|
|
|
|
void _call_signal_pending();
|
|
|
|
void _call_submit_signal();
|
|
|
|
void _call_ack_signal();
|
|
|
|
void _call_kill_signal_context();
|
|
|
|
void _call_kill_signal_receiver();
|
|
|
|
void _call_new_vm();
|
|
|
|
void _call_run_vm();
|
|
|
|
void _call_pause_vm();
|
|
|
|
void _call_access_thread_regs();
|
|
|
|
void _call_route_thread_event();
|
2013-10-16 13:10:54 +02:00
|
|
|
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/***************************
|
|
|
|
** Signal_context_killer **
|
|
|
|
***************************/
|
2013-09-09 15:20:30 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void _signal_context_kill_pending();
|
|
|
|
void _signal_context_kill_done();
|
2013-10-07 15:37:58 +02:00
|
|
|
|
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/****************************
|
|
|
|
** Signal_receiver_killer **
|
|
|
|
****************************/
|
2013-10-07 15:37:58 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void _signal_receiver_kill_pending();
|
|
|
|
void _signal_receiver_kill_done();
|
2013-10-07 15:37:58 +02:00
|
|
|
|
2013-09-09 15:20:30 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/********************
|
|
|
|
** Signal_handler **
|
|
|
|
********************/
|
2013-09-09 15:20:30 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void _await_signal(Signal_receiver * const receiver);
|
|
|
|
void _receive_signal(void * const base, size_t const size);
|
2013-09-09 15:20:30 +02:00
|
|
|
|
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
/**************
|
|
|
|
** Ipc_node **
|
|
|
|
**************/
|
2013-09-06 01:36:03 +02:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void _received_ipc_request(size_t const s);
|
|
|
|
void _await_ipc();
|
2013-10-17 13:51:17 +02:00
|
|
|
void _await_ipc_succeeded(size_t const s);
|
|
|
|
void _await_ipc_failed();
|
2012-11-30 14:08:42 +01:00
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
public:
|
2013-09-09 15:20:30 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-16 20:53:59 +02:00
|
|
|
* Constructor
|
|
|
|
*
|
2013-11-18 15:31:54 +01:00
|
|
|
* \param priority scheduling priority
|
|
|
|
* \param label debugging label
|
2013-09-09 15:20:30 +02:00
|
|
|
*/
|
2013-11-18 15:31:54 +01:00
|
|
|
Thread(unsigned const priority, char const * const label);
|
2013-09-09 15:20:30 +02:00
|
|
|
|
2013-09-19 16:47:38 +02:00
|
|
|
/**
|
2013-10-16 20:53:59 +02:00
|
|
|
* Prepare thread to get scheduled the first time
|
|
|
|
*
|
2013-11-18 12:47:14 +01:00
|
|
|
* \param cpu_id kernel name of targeted processor
|
|
|
|
* \param pd_id kernel name of target protection domain
|
|
|
|
* \param utcb core local pointer to userland thread-context
|
|
|
|
* \param start wether to start executing the thread
|
2013-09-19 16:47:38 +02:00
|
|
|
*/
|
2013-11-18 12:47:14 +01:00
|
|
|
void init(unsigned const cpu_id, unsigned const pd_id,
|
|
|
|
Native_utcb * const utcb, bool const start);
|
2013-09-19 16:47:38 +02:00
|
|
|
|
2013-09-09 15:20:30 +02:00
|
|
|
|
|
|
|
/***********************
|
|
|
|
** Execution_context **
|
|
|
|
***********************/
|
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
void handle_exception();
|
|
|
|
void proceed();
|
2013-09-09 15:20:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***************
|
|
|
|
** Accessors **
|
|
|
|
***************/
|
|
|
|
|
2013-11-18 15:31:54 +01:00
|
|
|
unsigned id() const { return Object::id(); }
|
|
|
|
char const * label() const { return _label; };
|
|
|
|
unsigned pd_id() const;
|
|
|
|
char const * pd_label() const;
|
2013-09-09 15:20:30 +02:00
|
|
|
};
|
|
|
|
|
2013-10-16 20:53:59 +02:00
|
|
|
#endif /* _KERNEL__THREAD_H_ */
|