genode/repos/base-hw/src/include/platform_thread.h

232 lines
5.5 KiB
C
Raw Normal View History

/*
* \brief Userland interface for the management of kernel thread-objects
* \author Martin Stein
* \author Stefan Kalkowski
* \date 2012-02-02
*/
/*
* Copyright (C) 2012-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _CORE__PLATFORM_THREAD_H_
#define _CORE__PLATFORM_THREAD_H_
/* Genode includes */
#include <base/ram_allocator.h>
#include <base/thread.h>
#include <base/trace/types.h>
/* base-internal includes */
#include <base/internal/native_utcb.h>
/* core includes */
#include <address_space.h>
#include "../core/object.h"
/* kernel includes */
#include "../core/kernel/core_interface.h"
#include "../core/kernel/thread.h"
namespace Genode {
class Pager_object;
class Thread_state;
class Rm_client;
class Platform_thread;
class Platform_pd;
/**
* Userland interface for the management of kernel thread-objects
*/
class Platform_thread : Noncopyable
{
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
/*
* Noncopyable
*/
Platform_thread(Platform_thread const &);
Platform_thread &operator = (Platform_thread const &);
typedef String<32> Label;
Label const _label;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Platform_pd * _pd;
Weak_ptr<Address_space> _address_space { };
Pager_object * _pager;
Native_utcb * _utcb_core_addr { }; /* UTCB addr in core */
Native_utcb * _utcb_pd_addr; /* UTCB addr in pd */
Ram_dataspace_capability _utcb { }; /* UTCB dataspace */
unsigned _priority {0};
unsigned _quota {0};
/*
* Wether this thread is the main thread of a program.
* This should be used only after 'join_pd' was called
* or if this is a core thread. For core threads its save
* also without 'join_pd' because '_main_thread' is initialized
* with 0 wich is always true as cores main thread has no
* 'Platform_thread'.
*/
bool _main_thread;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Affinity::Location _location { };
Kernel_object<Kernel::Thread> _kobj;
/**
* Common construction part
*/
void _init();
2012-10-09 15:41:40 +02:00
/*
* Check if this thread will attach its UTCB by itself
*/
bool _attaches_utcb_by_itself();
unsigned _scale_priority(unsigned virt_prio)
{
return Cpu_session::scale_priority(Kernel::Cpu_priority::MAX,
virt_prio);
}
public:
/**
* Constructor for core threads
*
* \param label debugging label
* \param utcb virtual address of UTCB within core
*/
Platform_thread(Label const &label, Native_utcb &utcb);
/**
* Constructor for threads outside of core
*
thread API & CPU session: accounting of CPU quota In the init configuration one can configure the donation of CPU time via 'resource' tags that have the attribute 'name' set to "CPU" and the attribute 'quantum' set to the percentage of CPU quota that init shall donate. The pattern is the same as when donating RAM quota. ! <start name="test"> ! <resource name="CPU" quantum="75"/> ! </start> This would cause init to try donating 75% of its CPU quota to the child "test". Init and core do not preserve CPU quota for their own requirements by default as it is done with RAM quota. The CPU quota that a process owns can be applied through the thread constructor. The constructor has been enhanced by an argument that indicates the percentage of the programs CPU quota that shall be granted to the new thread. So 'Thread(33, "test")' would cause the backing CPU session to try to grant 33% of the programs CPU quota to the thread "test". By now, the CPU quota of a thread can't be altered after construction. Constructing a thread with CPU quota 0 doesn't mean the thread gets never scheduled but that the thread has no guaranty to receive CPU time. Such threads have to live with excess CPU time. Threads that already existed in the official repositories of Genode were adapted in the way that they receive a quota of 0. This commit also provides a run test 'cpu_quota' in base-hw (the only kernel that applies the CPU-quota scheme currently). The test basically runs three threads with different physical CPU quota. The threads simply count for 30 seconds each and the test then checks wether the counter values relate to the CPU-quota distribution. fix #1275
2014-10-16 11:15:46 +02:00
* \param quota CPU quota that shall be granted to the thread
* \param label debugging label
* \param virt_prio unscaled processor-scheduling priority
* \param utcb core local pointer to userland stack
*/
Platform_thread(size_t const quota, Label const &label,
unsigned const virt_prio, Affinity::Location,
addr_t const utcb);
2012-10-09 15:41:40 +02:00
/**
* Destructor
*/
~Platform_thread();
/**
* Return information about current fault
*/
Kernel::Thread_fault fault_info() { return _kobj.kernel_object()->fault(); }
/**
* Join a protection domain
*
* \param pd platform pd object pointer
* \param main_thread wether thread is the first in protection domain
* \param address_space corresponding Genode address space
*
* This function has no effect when called more twice for a
* given thread.
*/
void join_pd(Platform_pd *const pd, bool const main_thread,
Weak_ptr<Address_space> address_space);
/**
* Run this thread
*
* \param ip initial instruction pointer
* \param sp initial stack pointer
*/
int start(void * const ip, void * const sp);
void restart();
/**
* Pause this thread
*/
void pause() { Kernel::pause_thread(_kobj.kernel_object()); }
/**
* Enable/disable single stepping
*/
void single_step(bool) { }
/**
* Resume this thread
*/
void resume() { Kernel::resume_thread(_kobj.kernel_object()); }
/**
* Cancel currently blocking operation
*/
hw: clean up scheduling-readiness syscalls This cleans up the syscalls that are mainly used to control the scheduling readiness of a thread. The different use cases and requirements were somehow mixed together in the previous interface. The new syscall set is: 1) pause_thread and resume_thread They don't affect the state of the thread (IPC, signalling, etc.) but merely decide wether the thread is allowed for scheduling or not, the so-called pause state. The pause state is orthogonal to the thread state and masks it when it comes to scheduling. In contrast to the stopped state, which is described in "stop_thread and restart_thread", the thread state and the UTCB content of a thread may change while in the paused state. However, the register state of a thread doesn't change while paused. The "pause" and "resume" syscalls are both core-restricted and may target any thread. They are used as back end for the CPU session calls "pause" and "resume". The "pause/resume" feature is made for applications like the GDB monitor that transparently want to stop and continue the execution of a thread no matter what state the thread is in. 2) stop_thread and restart_thread The stop syscall can only be used on a thread in the non-blocking ("active") thread state. The thread then switches to the "stopped" thread state in wich it explicitely waits for a restart. The restart syscall can only be used on a thread in the "stopped" or the "active" thread state. The thread then switches back to the "active" thread state and the syscall returns whether the thread was stopped. Both syscalls are not core-restricted. "Stop" always targets the calling thread while "restart" may target any thread in the same PD as the caller. Thread state and UTCB content of a thread don't change while in the stopped state. The "stop/restart" feature is used when an active thread wants to wait for an event that is not known to the kernel. Actually the syscalls are used when waiting for locks and on thread exit. 3) cancel_thread_blocking Does cleanly cancel a cancelable blocking thread state (IPC, signalling, stopped). The thread whose blocking was cancelled goes back to the "active" thread state. It may receive a syscall return value that reflects the cancellation. This syscall doesn't affect the pause state of the thread which means that it may still not get scheduled. The syscall is core-restricted and may target any thread. 4) yield_thread Does its best that a thread is scheduled as few as possible in the current scheduling super-period without touching the thread or pause state. In the next superperiod, however, the thread is scheduled "normal" again. The syscall is not core-restricted and always targets the caller. Fixes #2104
2016-09-15 17:23:06 +02:00
void cancel_blocking() {
Kernel::cancel_thread_blocking(_kobj.kernel_object()); }
/**
* Set CPU quota of the thread to 'quota'
*/
void quota(size_t const quota);
/**
* Get raw thread state
*/
Thread_state state();
/**
* Override raw thread state
*/
void state(Thread_state s);
/**
* Return unique identification of this thread as faulter
*/
2015-02-06 17:23:41 +01:00
unsigned long pager_object_badge() { return (unsigned long)this; }
/**
* Set the executing CPU for this thread
*
* \param location targeted location in affinity space
*/
void affinity(Affinity::Location const & location);
/**
* Get the executing CPU for this thread
*/
Affinity::Location affinity() const;
/**
* Return the address space to which the thread is bound
*/
Weak_ptr<Address_space>& address_space();
/**
* Return execution time consumed by the thread
*/
Trace::Execution_time execution_time() const {
return { 0, 0, _quota, _priority }; }
/***************
** Accessors **
***************/
Label label() const { return _label; };
void pager(Pager_object &pager);
Pager_object &pager();
Platform_pd * pd() const { return _pd; }
Ram_dataspace_capability utcb() const { return _utcb; }
};
}
#endif /* _CORE__PLATFORM_THREAD_H_ */