mirror of
https://github.com/citra-mirror/citra.git
synced 2024-08-24 13:17:19 +02:00
commit
77363d9590
@ -20,8 +20,8 @@ public:
|
||||
std::string GetTypeName() const override { return "Arbiter"; }
|
||||
std::string GetName() const override { return name; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return HandleType::AddressArbiter; }
|
||||
Kernel::HandleType GetHandleType() const override { return HandleType::AddressArbiter; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::AddressArbiter;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
std::string name; ///< Name of address arbiter object (optional)
|
||||
};
|
||||
@ -62,7 +62,8 @@ ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s3
|
||||
/// Create an address arbiter
|
||||
AddressArbiter* CreateAddressArbiter(Handle& handle, const std::string& name) {
|
||||
AddressArbiter* address_arbiter = new AddressArbiter;
|
||||
handle = Kernel::g_object_pool.Create(address_arbiter);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(address_arbiter).ValueOr(INVALID_HANDLE);
|
||||
address_arbiter->name = name;
|
||||
return address_arbiter;
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ public:
|
||||
std::string GetTypeName() const override { return "Event"; }
|
||||
std::string GetName() const override { return name; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Event; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Event; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::Event;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
ResetType intitial_reset_type; ///< ResetType specified at Event initialization
|
||||
ResetType reset_type; ///< Current ResetType
|
||||
@ -53,7 +53,7 @@ public:
|
||||
* @return Result of operation, 0 on success, otherwise error code
|
||||
*/
|
||||
ResultCode SetPermanentLock(Handle handle, const bool permanent_locked) {
|
||||
Event* evt = g_object_pool.Get<Event>(handle);
|
||||
Event* evt = g_handle_table.Get<Event>(handle);
|
||||
if (evt == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
evt->permanent_locked = permanent_locked;
|
||||
@ -67,7 +67,7 @@ ResultCode SetPermanentLock(Handle handle, const bool permanent_locked) {
|
||||
* @return Result of operation, 0 on success, otherwise error code
|
||||
*/
|
||||
ResultCode SetEventLocked(const Handle handle, const bool locked) {
|
||||
Event* evt = g_object_pool.Get<Event>(handle);
|
||||
Event* evt = g_handle_table.Get<Event>(handle);
|
||||
if (evt == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
if (!evt->permanent_locked) {
|
||||
@ -82,7 +82,7 @@ ResultCode SetEventLocked(const Handle handle, const bool locked) {
|
||||
* @return Result of operation, 0 on success, otherwise error code
|
||||
*/
|
||||
ResultCode SignalEvent(const Handle handle) {
|
||||
Event* evt = g_object_pool.Get<Event>(handle);
|
||||
Event* evt = g_handle_table.Get<Event>(handle);
|
||||
if (evt == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
// Resume threads waiting for event to signal
|
||||
@ -110,7 +110,7 @@ ResultCode SignalEvent(const Handle handle) {
|
||||
* @return Result of operation, 0 on success, otherwise error code
|
||||
*/
|
||||
ResultCode ClearEvent(Handle handle) {
|
||||
Event* evt = g_object_pool.Get<Event>(handle);
|
||||
Event* evt = g_handle_table.Get<Event>(handle);
|
||||
if (evt == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
if (!evt->permanent_locked) {
|
||||
@ -129,7 +129,8 @@ ResultCode ClearEvent(Handle handle) {
|
||||
Event* CreateEvent(Handle& handle, const ResetType reset_type, const std::string& name) {
|
||||
Event* evt = new Event;
|
||||
|
||||
handle = Kernel::g_object_pool.Create(evt);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(evt).ValueOr(INVALID_HANDLE);
|
||||
|
||||
evt->locked = true;
|
||||
evt->permanent_locked = false;
|
||||
|
@ -13,79 +13,95 @@
|
||||
namespace Kernel {
|
||||
|
||||
Handle g_main_thread = 0;
|
||||
ObjectPool g_object_pool;
|
||||
HandleTable g_handle_table;
|
||||
u64 g_program_id = 0;
|
||||
|
||||
ObjectPool::ObjectPool() {
|
||||
next_id = INITIAL_NEXT_ID;
|
||||
HandleTable::HandleTable() {
|
||||
next_generation = 1;
|
||||
Clear();
|
||||
}
|
||||
|
||||
Handle ObjectPool::Create(Object* obj, int range_bottom, int range_top) {
|
||||
if (range_top > MAX_COUNT) {
|
||||
range_top = MAX_COUNT;
|
||||
ResultVal<Handle> HandleTable::Create(Object* obj) {
|
||||
_dbg_assert_(Kernel, obj != nullptr);
|
||||
|
||||
u16 slot = next_free_slot;
|
||||
if (slot >= generations.size()) {
|
||||
LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
|
||||
return ERR_OUT_OF_HANDLES;
|
||||
}
|
||||
if (next_id >= range_bottom && next_id < range_top) {
|
||||
range_bottom = next_id++;
|
||||
}
|
||||
for (int i = range_bottom; i < range_top; i++) {
|
||||
if (!occupied[i]) {
|
||||
occupied[i] = true;
|
||||
pool[i] = obj;
|
||||
pool[i]->handle = i + HANDLE_OFFSET;
|
||||
return i + HANDLE_OFFSET;
|
||||
}
|
||||
}
|
||||
LOG_ERROR(Kernel, "Unable to allocate kernel object, too many objects slots in use.");
|
||||
return 0;
|
||||
next_free_slot = generations[slot];
|
||||
|
||||
u16 generation = next_generation++;
|
||||
|
||||
// Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
|
||||
// CTR-OS doesn't use generation 0, so skip straight to 1.
|
||||
if (next_generation >= (1 << 15)) next_generation = 1;
|
||||
|
||||
generations[slot] = generation;
|
||||
intrusive_ptr_add_ref(obj);
|
||||
objects[slot] = obj;
|
||||
|
||||
Handle handle = generation | (slot << 15);
|
||||
obj->handle = handle;
|
||||
return MakeResult<Handle>(handle);
|
||||
}
|
||||
|
||||
bool ObjectPool::IsValid(Handle handle) const {
|
||||
int index = handle - HANDLE_OFFSET;
|
||||
if (index < 0)
|
||||
return false;
|
||||
if (index >= MAX_COUNT)
|
||||
return false;
|
||||
|
||||
return occupied[index];
|
||||
ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
|
||||
Object* object = GetGeneric(handle);
|
||||
if (object == nullptr) {
|
||||
LOG_ERROR(Kernel, "Tried to duplicate invalid handle: %08X", handle);
|
||||
return ERR_INVALID_HANDLE;
|
||||
}
|
||||
return Create(object);
|
||||
}
|
||||
|
||||
void ObjectPool::Clear() {
|
||||
for (int i = 0; i < MAX_COUNT; i++) {
|
||||
//brutally clear everything, no validation
|
||||
if (occupied[i])
|
||||
delete pool[i];
|
||||
occupied[i] = false;
|
||||
}
|
||||
pool.fill(nullptr);
|
||||
next_id = INITIAL_NEXT_ID;
|
||||
ResultCode HandleTable::Close(Handle handle) {
|
||||
if (!IsValid(handle))
|
||||
return ERR_INVALID_HANDLE;
|
||||
|
||||
size_t slot = GetSlot(handle);
|
||||
u16 generation = GetGeneration(handle);
|
||||
|
||||
intrusive_ptr_release(objects[slot]);
|
||||
objects[slot] = nullptr;
|
||||
|
||||
generations[generation] = next_free_slot;
|
||||
next_free_slot = slot;
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
Object* &ObjectPool::operator [](Handle handle)
|
||||
{
|
||||
_dbg_assert_msg_(Kernel, IsValid(handle), "GRABBING UNALLOCED KERNEL OBJ");
|
||||
return pool[handle - HANDLE_OFFSET];
|
||||
bool HandleTable::IsValid(Handle handle) const {
|
||||
size_t slot = GetSlot(handle);
|
||||
u16 generation = GetGeneration(handle);
|
||||
|
||||
return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
|
||||
}
|
||||
|
||||
void ObjectPool::List() {
|
||||
for (int i = 0; i < MAX_COUNT; i++) {
|
||||
if (occupied[i]) {
|
||||
if (pool[i]) {
|
||||
LOG_DEBUG(Kernel, "KO %i: %s \"%s\"", i + HANDLE_OFFSET, pool[i]->GetTypeName().c_str(),
|
||||
pool[i]->GetName().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ObjectPool::GetCount() const {
|
||||
return std::count(occupied.begin(), occupied.end(), true);
|
||||
}
|
||||
|
||||
Object* ObjectPool::CreateByIDType(int type) {
|
||||
LOG_ERROR(Kernel, "Unimplemented: %d.", type);
|
||||
Object* HandleTable::GetGeneric(Handle handle) const {
|
||||
if (handle == CurrentThread) {
|
||||
// TODO(yuriks) Directly return the pointer once this is possible.
|
||||
handle = GetCurrentThreadHandle();
|
||||
} else if (handle == CurrentProcess) {
|
||||
LOG_ERROR(Kernel, "Current process (%08X) pseudo-handle not supported", CurrentProcess);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!IsValid(handle)) {
|
||||
return nullptr;
|
||||
}
|
||||
return objects[GetSlot(handle)];
|
||||
}
|
||||
|
||||
void HandleTable::Clear() {
|
||||
for (size_t i = 0; i < MAX_COUNT; ++i) {
|
||||
generations[i] = i + 1;
|
||||
if (objects[i] != nullptr)
|
||||
intrusive_ptr_release(objects[i]);
|
||||
objects[i] = nullptr;
|
||||
}
|
||||
next_free_slot = 0;
|
||||
}
|
||||
|
||||
/// Initialize the kernel
|
||||
void Init() {
|
||||
Kernel::ThreadingInit();
|
||||
@ -95,7 +111,7 @@ void Init() {
|
||||
void Shutdown() {
|
||||
Kernel::ThreadingShutdown();
|
||||
|
||||
g_object_pool.Clear(); // Free all kernel objects
|
||||
g_handle_table.Clear(); // Free all kernel objects
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,13 +12,17 @@
|
||||
typedef u32 Handle;
|
||||
typedef s32 Result;
|
||||
|
||||
const Handle INVALID_HANDLE = 0;
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
// From kernel.h. Declarations duplicated here to avoid a circular header dependency.
|
||||
class Thread;
|
||||
Thread* GetCurrentThread();
|
||||
// TODO: Verify code
|
||||
const ResultCode ERR_OUT_OF_HANDLES(ErrorDescription::OutOfMemory, ErrorModule::Kernel,
|
||||
ErrorSummary::OutOfResource, ErrorLevel::Temporary);
|
||||
// TOOD: Verify code
|
||||
const ResultCode ERR_INVALID_HANDLE = InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
enum KernelHandle {
|
||||
enum KernelHandle : Handle {
|
||||
CurrentThread = 0xFFFF8000,
|
||||
CurrentProcess = 0xFFFF8001,
|
||||
};
|
||||
@ -41,10 +45,10 @@ enum {
|
||||
DEFAULT_STACK_SIZE = 0x4000,
|
||||
};
|
||||
|
||||
class ObjectPool;
|
||||
class HandleTable;
|
||||
|
||||
class Object : NonCopyable {
|
||||
friend class ObjectPool;
|
||||
friend class HandleTable;
|
||||
u32 handle;
|
||||
public:
|
||||
virtual ~Object() {}
|
||||
@ -61,106 +65,130 @@ public:
|
||||
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
|
||||
return UnimplementedFunction(ErrorModule::Kernel);
|
||||
}
|
||||
};
|
||||
|
||||
class ObjectPool : NonCopyable {
|
||||
public:
|
||||
ObjectPool();
|
||||
~ObjectPool() {}
|
||||
|
||||
// Allocates a handle within the range and inserts the object into the map.
|
||||
Handle Create(Object* obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF);
|
||||
|
||||
static Object* CreateByIDType(int type);
|
||||
|
||||
template <class T>
|
||||
void Destroy(Handle handle) {
|
||||
if (Get<T>(handle)) {
|
||||
occupied[handle - HANDLE_OFFSET] = false;
|
||||
delete pool[handle - HANDLE_OFFSET];
|
||||
}
|
||||
}
|
||||
|
||||
bool IsValid(Handle handle) const;
|
||||
|
||||
template <class T>
|
||||
T* Get(Handle handle) {
|
||||
if (handle == CurrentThread) {
|
||||
return reinterpret_cast<T*>(GetCurrentThread());
|
||||
}
|
||||
|
||||
if (handle < HANDLE_OFFSET || handle >= HANDLE_OFFSET + MAX_COUNT || !occupied[handle - HANDLE_OFFSET]) {
|
||||
if (handle != 0) {
|
||||
LOG_ERROR(Kernel, "Bad object handle %08x", handle);
|
||||
}
|
||||
return nullptr;
|
||||
} else {
|
||||
Object* t = pool[handle - HANDLE_OFFSET];
|
||||
if (t->GetHandleType() != T::GetStaticHandleType()) {
|
||||
LOG_ERROR(Kernel, "Wrong object type for %08x", handle);
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<T*>(t);
|
||||
}
|
||||
}
|
||||
|
||||
// ONLY use this when you know the handle is valid.
|
||||
template <class T>
|
||||
T *GetFast(Handle handle) {
|
||||
if (handle == CurrentThread) {
|
||||
return reinterpret_cast<T*>(GetCurrentThread());
|
||||
}
|
||||
|
||||
const Handle realHandle = handle - HANDLE_OFFSET;
|
||||
_dbg_assert_(Kernel, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]);
|
||||
return static_cast<T*>(pool[realHandle]);
|
||||
}
|
||||
|
||||
template <class T, typename ArgT>
|
||||
void Iterate(bool func(T*, ArgT), ArgT arg) {
|
||||
int type = T::GetStaticIDType();
|
||||
for (int i = 0; i < MAX_COUNT; i++)
|
||||
{
|
||||
if (!occupied[i])
|
||||
continue;
|
||||
T* t = static_cast<T*>(pool[i]);
|
||||
if (t->GetIDType() == type) {
|
||||
if (!func(t, arg))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool GetIDType(Handle handle, HandleType* type) const {
|
||||
if ((handle < HANDLE_OFFSET) || (handle >= HANDLE_OFFSET + MAX_COUNT) ||
|
||||
!occupied[handle - HANDLE_OFFSET]) {
|
||||
LOG_ERROR(Kernel, "Bad object handle %08X", handle);
|
||||
return false;
|
||||
}
|
||||
Object* t = pool[handle - HANDLE_OFFSET];
|
||||
*type = t->GetHandleType();
|
||||
return true;
|
||||
}
|
||||
|
||||
Object* &operator [](Handle handle);
|
||||
void List();
|
||||
void Clear();
|
||||
int GetCount() const;
|
||||
|
||||
private:
|
||||
friend void intrusive_ptr_add_ref(Object*);
|
||||
friend void intrusive_ptr_release(Object*);
|
||||
|
||||
enum {
|
||||
MAX_COUNT = 0x1000,
|
||||
HANDLE_OFFSET = 0x100,
|
||||
INITIAL_NEXT_ID = 0x10,
|
||||
unsigned int ref_count = 0;
|
||||
};
|
||||
|
||||
std::array<Object*, MAX_COUNT> pool;
|
||||
std::array<bool, MAX_COUNT> occupied;
|
||||
int next_id;
|
||||
// Special functions that will later be used by boost::instrusive_ptr to do automatic ref-counting
|
||||
inline void intrusive_ptr_add_ref(Object* object) {
|
||||
++object->ref_count;
|
||||
}
|
||||
|
||||
inline void intrusive_ptr_release(Object* object) {
|
||||
if (--object->ref_count == 0) {
|
||||
delete object;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class allows the creation of Handles, which are references to objects that can be tested
|
||||
* for validity and looked up. Here they are used to pass references to kernel objects to/from the
|
||||
* emulated process. it has been designed so that it follows the same handle format and has
|
||||
* approximately the same restrictions as the handle manager in the CTR-OS.
|
||||
*
|
||||
* Handles contain two sub-fields: a slot index (bits 31:15) and a generation value (bits 14:0).
|
||||
* The slot index is used to index into the arrays in this class to access the data corresponding
|
||||
* to the Handle.
|
||||
*
|
||||
* To prevent accidental use of a freed Handle whose slot has already been reused, a global counter
|
||||
* is kept and incremented every time a Handle is created. This is the Handle's "generation". The
|
||||
* value of the counter is stored into the Handle as well as in the handle table (in the
|
||||
* "generations" array). When looking up a handle, the Handle's generation must match with the
|
||||
* value stored on the class, otherwise the Handle is considered invalid.
|
||||
*
|
||||
* To find free slots when allocating a Handle without needing to scan the entire object array, the
|
||||
* generations field of unallocated slots is re-purposed as a linked list of indices to free slots.
|
||||
* When a Handle is created, an index is popped off the list and used for the new Handle. When it
|
||||
* is destroyed, it is again pushed onto the list to be re-used by the next allocation. It is
|
||||
* likely that this allocation strategy differs from the one used in CTR-OS, but this hasn't been
|
||||
* verified and isn't likely to cause any problems.
|
||||
*/
|
||||
class HandleTable final : NonCopyable {
|
||||
public:
|
||||
HandleTable();
|
||||
|
||||
/**
|
||||
* Allocates a handle for the given object.
|
||||
* @return The created Handle or one of the following errors:
|
||||
* - `ERR_OUT_OF_HANDLES`: the maximum number of handles has been exceeded.
|
||||
*/
|
||||
ResultVal<Handle> Create(Object* obj);
|
||||
|
||||
/**
|
||||
* Returns a new handle that points to the same object as the passed in handle.
|
||||
* @return The duplicated Handle or one of the following errors:
|
||||
* - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
|
||||
* - Any errors returned by `Create()`.
|
||||
*/
|
||||
ResultVal<Handle> Duplicate(Handle handle);
|
||||
|
||||
/**
|
||||
* Closes a handle, removing it from the table and decreasing the object's ref-count.
|
||||
* @return `RESULT_SUCCESS` or one of the following errors:
|
||||
* - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
|
||||
*/
|
||||
ResultCode Close(Handle handle);
|
||||
|
||||
/// Checks if a handle is valid and points to an existing object.
|
||||
bool IsValid(Handle handle) const;
|
||||
|
||||
/**
|
||||
* Looks up a handle.
|
||||
* @returns Pointer to the looked-up object, or `nullptr` if the handle is not valid.
|
||||
*/
|
||||
Object* GetGeneric(Handle handle) const;
|
||||
|
||||
/**
|
||||
* Looks up a handle while verifying its type.
|
||||
* @returns Pointer to the looked-up object, or `nullptr` if the handle is not valid or its
|
||||
* type differs from the handle type `T::HANDLE_TYPE`.
|
||||
*/
|
||||
template <class T>
|
||||
T* Get(Handle handle) const {
|
||||
Object* object = GetGeneric(handle);
|
||||
if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
|
||||
return static_cast<T*>(object);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/// Closes all handles held in this table.
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
/**
|
||||
* This is the maximum limit of handles allowed per process in CTR-OS. It can be further
|
||||
* reduced by ExHeader values, but this is not emulated here.
|
||||
*/
|
||||
static const size_t MAX_COUNT = 4096;
|
||||
|
||||
static size_t GetSlot(Handle handle) { return handle >> 15; }
|
||||
static u16 GetGeneration(Handle handle) { return handle & 0x7FFF; }
|
||||
|
||||
/// Stores the Object referenced by the handle or null if the slot is empty.
|
||||
std::array<Object*, MAX_COUNT> objects;
|
||||
|
||||
/**
|
||||
* The value of `next_generation` when the handle was created, used to check for validity. For
|
||||
* empty slots, contains the index of the next free slot in the list.
|
||||
*/
|
||||
std::array<u16, MAX_COUNT> generations;
|
||||
|
||||
/**
|
||||
* Global counter of the number of created handles. Stored in `generations` when a handle is
|
||||
* created, and wraps around to 1 when it hits 0x8000.
|
||||
*/
|
||||
u16 next_generation;
|
||||
|
||||
/// Head of the free slots linked list.
|
||||
u16 next_free_slot;
|
||||
};
|
||||
|
||||
extern ObjectPool g_object_pool;
|
||||
extern HandleTable g_handle_table;
|
||||
extern Handle g_main_thread;
|
||||
|
||||
/// The ID code of the currently running game
|
||||
|
@ -18,8 +18,8 @@ public:
|
||||
std::string GetTypeName() const override { return "Mutex"; }
|
||||
std::string GetName() const override { return name; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Mutex; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Mutex; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::Mutex;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
bool initial_locked; ///< Initial lock state when mutex was created
|
||||
bool locked; ///< Current locked state
|
||||
@ -87,7 +87,7 @@ void ReleaseThreadMutexes(Handle thread) {
|
||||
|
||||
// Release every mutex that the thread holds, and resume execution on the waiting threads
|
||||
for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) {
|
||||
Mutex* mutex = g_object_pool.GetFast<Mutex>(iter->second);
|
||||
Mutex* mutex = g_handle_table.Get<Mutex>(iter->second);
|
||||
ResumeWaitingThread(mutex);
|
||||
}
|
||||
|
||||
@ -115,7 +115,7 @@ bool ReleaseMutex(Mutex* mutex) {
|
||||
* @param handle Handle to mutex to release
|
||||
*/
|
||||
ResultCode ReleaseMutex(Handle handle) {
|
||||
Mutex* mutex = Kernel::g_object_pool.Get<Mutex>(handle);
|
||||
Mutex* mutex = Kernel::g_handle_table.Get<Mutex>(handle);
|
||||
if (mutex == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
if (!ReleaseMutex(mutex)) {
|
||||
@ -136,7 +136,8 @@ ResultCode ReleaseMutex(Handle handle) {
|
||||
*/
|
||||
Mutex* CreateMutex(Handle& handle, bool initial_locked, const std::string& name) {
|
||||
Mutex* mutex = new Mutex;
|
||||
handle = Kernel::g_object_pool.Create(mutex);
|
||||
// TODO(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(mutex).ValueOr(INVALID_HANDLE);
|
||||
|
||||
mutex->locked = mutex->initial_locked = initial_locked;
|
||||
mutex->name = name;
|
||||
|
@ -17,8 +17,8 @@ public:
|
||||
std::string GetTypeName() const override { return "Semaphore"; }
|
||||
std::string GetName() const override { return name; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::Semaphore;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
s32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
|
||||
s32 available_count; ///< Number of free slots left in the semaphore
|
||||
@ -57,7 +57,8 @@ ResultCode CreateSemaphore(Handle* handle, s32 initial_count,
|
||||
ErrorSummary::WrongArgument, ErrorLevel::Permanent);
|
||||
|
||||
Semaphore* semaphore = new Semaphore;
|
||||
*handle = g_object_pool.Create(semaphore);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
*handle = g_handle_table.Create(semaphore).ValueOr(INVALID_HANDLE);
|
||||
|
||||
// When the semaphore is created, some slots are reserved for other threads,
|
||||
// and the rest is reserved for the caller thread
|
||||
@ -69,7 +70,7 @@ ResultCode CreateSemaphore(Handle* handle, s32 initial_count,
|
||||
}
|
||||
|
||||
ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
|
||||
Semaphore* semaphore = g_object_pool.Get<Semaphore>(handle);
|
||||
Semaphore* semaphore = g_handle_table.Get<Semaphore>(handle);
|
||||
if (semaphore == nullptr)
|
||||
return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
|
@ -45,8 +45,8 @@ class Session : public Object {
|
||||
public:
|
||||
std::string GetTypeName() const override { return "Session"; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Session; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Session; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::Session;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
/**
|
||||
* Handles a synchronous call to this session using HLE emulation. Emulated <-> emulated calls
|
||||
|
@ -13,8 +13,8 @@ class SharedMemory : public Object {
|
||||
public:
|
||||
std::string GetTypeName() const override { return "SharedMemory"; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::SharedMemory; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::SharedMemory; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::SharedMemory;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
u32 base_address; ///< Address of shared memory block in RAM
|
||||
MemoryPermission permissions; ///< Permissions of shared memory block (SVC field)
|
||||
@ -32,7 +32,8 @@ public:
|
||||
*/
|
||||
SharedMemory* CreateSharedMemory(Handle& handle, const std::string& name) {
|
||||
SharedMemory* shared_memory = new SharedMemory;
|
||||
handle = Kernel::g_object_pool.Create(shared_memory);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(shared_memory).ValueOr(INVALID_HANDLE);
|
||||
shared_memory->name = name;
|
||||
return shared_memory;
|
||||
}
|
||||
@ -60,7 +61,7 @@ ResultCode MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions
|
||||
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
||||
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
||||
}
|
||||
SharedMemory* shared_memory = Kernel::g_object_pool.Get<SharedMemory>(handle);
|
||||
SharedMemory* shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
|
||||
if (shared_memory == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
shared_memory->base_address = address;
|
||||
@ -71,7 +72,7 @@ ResultCode MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions
|
||||
}
|
||||
|
||||
ResultVal<u8*> GetSharedMemoryPointer(Handle handle, u32 offset) {
|
||||
SharedMemory* shared_memory = Kernel::g_object_pool.Get<SharedMemory>(handle);
|
||||
SharedMemory* shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
|
||||
if (shared_memory == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
if (0 != shared_memory->base_address)
|
||||
|
@ -26,8 +26,8 @@ public:
|
||||
std::string GetName() const override { return name; }
|
||||
std::string GetTypeName() const override { return "Thread"; }
|
||||
|
||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; }
|
||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Thread; }
|
||||
static const HandleType HANDLE_TYPE = HandleType::Thread;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
|
||||
inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
|
||||
@ -164,7 +164,7 @@ static bool CheckWaitType(const Thread* thread, WaitType type, Handle wait_handl
|
||||
|
||||
/// Stops the current thread
|
||||
ResultCode StopThread(Handle handle, const char* reason) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
if (thread == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
// Release all the mutexes that this thread holds
|
||||
@ -173,7 +173,7 @@ ResultCode StopThread(Handle handle, const char* reason) {
|
||||
ChangeReadyState(thread, false);
|
||||
thread->status = THREADSTATUS_DORMANT;
|
||||
for (Handle waiting_handle : thread->waiting_threads) {
|
||||
Thread* waiting_thread = g_object_pool.Get<Thread>(waiting_handle);
|
||||
Thread* waiting_thread = g_handle_table.Get<Thread>(waiting_handle);
|
||||
|
||||
if (CheckWaitType(waiting_thread, WAITTYPE_THREADEND, handle))
|
||||
ResumeThreadFromWait(waiting_handle);
|
||||
@ -210,7 +210,7 @@ Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) {
|
||||
|
||||
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
||||
for (Handle handle : thread_queue) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
|
||||
if (!CheckWaitType(thread, WAITTYPE_ARB, arbiter, address))
|
||||
continue;
|
||||
@ -235,7 +235,7 @@ void ArbitrateAllThreads(u32 arbiter, u32 address) {
|
||||
|
||||
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
||||
for (Handle handle : thread_queue) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
|
||||
if (CheckWaitType(thread, WAITTYPE_ARB, arbiter, address))
|
||||
ResumeThreadFromWait(handle);
|
||||
@ -288,7 +288,7 @@ Thread* NextThread() {
|
||||
if (next == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
return Kernel::g_object_pool.Get<Thread>(next);
|
||||
return Kernel::g_handle_table.Get<Thread>(next);
|
||||
}
|
||||
|
||||
void WaitCurrentThread(WaitType wait_type, Handle wait_handle) {
|
||||
@ -305,7 +305,7 @@ void WaitCurrentThread(WaitType wait_type, Handle wait_handle, VAddr wait_addres
|
||||
|
||||
/// Resumes a thread from waiting by marking it as "ready"
|
||||
void ResumeThreadFromWait(Handle handle) {
|
||||
Thread* thread = Kernel::g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = Kernel::g_handle_table.Get<Thread>(handle);
|
||||
if (thread) {
|
||||
thread->status &= ~THREADSTATUS_WAIT;
|
||||
thread->wait_handle = 0;
|
||||
@ -341,7 +341,8 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio
|
||||
|
||||
Thread* thread = new Thread;
|
||||
|
||||
handle = Kernel::g_object_pool.Create(thread);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(thread).ValueOr(INVALID_HANDLE);
|
||||
|
||||
thread_queue.push_back(handle);
|
||||
thread_ready_queue.prepare(priority);
|
||||
@ -398,7 +399,7 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3
|
||||
|
||||
/// Get the priority of the thread specified by handle
|
||||
ResultVal<u32> GetThreadPriority(const Handle handle) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
if (thread == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
return MakeResult<u32>(thread->current_priority);
|
||||
@ -410,7 +411,7 @@ ResultCode SetThreadPriority(Handle handle, s32 priority) {
|
||||
if (!handle) {
|
||||
thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior?
|
||||
} else {
|
||||
thread = g_object_pool.Get<Thread>(handle);
|
||||
thread = g_handle_table.Get<Thread>(handle);
|
||||
if (thread == nullptr) {
|
||||
return InvalidHandle(ErrorModule::Kernel);
|
||||
}
|
||||
@ -481,7 +482,7 @@ void Reschedule() {
|
||||
LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle());
|
||||
|
||||
for (Handle handle : thread_queue) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X wait_type=0x%08X wait_handle=0x%08X",
|
||||
thread->GetHandle(), thread->current_priority, thread->status, thread->wait_type, thread->wait_handle);
|
||||
}
|
||||
@ -497,7 +498,7 @@ void Reschedule() {
|
||||
}
|
||||
|
||||
ResultCode GetThreadId(u32* thread_id, Handle handle) {
|
||||
Thread* thread = g_object_pool.Get<Thread>(handle);
|
||||
Thread* thread = g_handle_table.Get<Thread>(handle);
|
||||
if (thread == nullptr)
|
||||
return ResultCode(ErrorDescription::InvalidHandle, ErrorModule::OS,
|
||||
ErrorSummary::WrongArgument, ErrorLevel::Permanent);
|
||||
|
@ -77,9 +77,6 @@ Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address);
|
||||
/// Arbitrate all threads currently waiting...
|
||||
void ArbitrateAllThreads(u32 arbiter, u32 address);
|
||||
|
||||
/// Gets the current thread
|
||||
Thread* GetCurrentThread();
|
||||
|
||||
/// Gets the current thread handle
|
||||
Handle GetCurrentThreadHandle();
|
||||
|
||||
|
@ -133,7 +133,7 @@ public:
|
||||
case FileCommand::Close:
|
||||
{
|
||||
LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
|
||||
Kernel::g_object_pool.Destroy<File>(GetHandle());
|
||||
backend->Close();
|
||||
break;
|
||||
}
|
||||
|
||||
@ -189,7 +189,7 @@ public:
|
||||
case DirectoryCommand::Close:
|
||||
{
|
||||
LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
|
||||
Kernel::g_object_pool.Destroy<Directory>(GetHandle());
|
||||
backend->Close();
|
||||
break;
|
||||
}
|
||||
|
||||
@ -283,7 +283,8 @@ ResultVal<Handle> OpenFileFromArchive(ArchiveHandle archive_handle, const FileSy
|
||||
}
|
||||
|
||||
auto file = Common::make_unique<File>(std::move(backend), path);
|
||||
Handle handle = Kernel::g_object_pool.Create(file.release());
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
Handle handle = Kernel::g_handle_table.Create(file.release()).ValueOr(INVALID_HANDLE);
|
||||
return MakeResult<Handle>(handle);
|
||||
}
|
||||
|
||||
@ -388,7 +389,8 @@ ResultVal<Handle> OpenDirectoryFromArchive(ArchiveHandle archive_handle, const F
|
||||
}
|
||||
|
||||
auto directory = Common::make_unique<Directory>(std::move(backend), path);
|
||||
Handle handle = Kernel::g_object_pool.Create(directory.release());
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
Handle handle = Kernel::g_handle_table.Create(directory.release()).ValueOr(INVALID_HANDLE);
|
||||
return MakeResult<Handle>(handle);
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,8 @@ Manager::~Manager() {
|
||||
|
||||
/// Add a service to the manager (does not create it though)
|
||||
void Manager::AddService(Interface* service) {
|
||||
m_port_map[service->GetPortName()] = Kernel::g_object_pool.Create(service);
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
m_port_map[service->GetPortName()] = Kernel::g_handle_table.Create(service).ValueOr(INVALID_HANDLE);
|
||||
m_services.push_back(service);
|
||||
}
|
||||
|
||||
@ -70,7 +71,7 @@ void Manager::DeleteService(const std::string& port_name) {
|
||||
|
||||
/// Get a Service Interface from its Handle
|
||||
Interface* Manager::FetchFromHandle(Handle handle) {
|
||||
return Kernel::g_object_pool.Get<Interface>(handle);
|
||||
return Kernel::g_handle_table.Get<Interface>(handle);
|
||||
}
|
||||
|
||||
/// Get a Service Interface from its port
|
||||
|
@ -54,7 +54,8 @@ public:
|
||||
|
||||
/// Allocates a new handle for the service
|
||||
Handle CreateHandle(Kernel::Object *obj) {
|
||||
Handle handle = Kernel::g_object_pool.Create(obj);
|
||||
// TODO(yuriks): Fix error reporting
|
||||
Handle handle = Kernel::g_handle_table.Create(obj).ValueOr(INVALID_HANDLE);
|
||||
m_handles.push_back(handle);
|
||||
return handle;
|
||||
}
|
||||
@ -62,7 +63,7 @@ public:
|
||||
/// Frees a handle from the service
|
||||
template <class T>
|
||||
void DeleteHandle(const Handle handle) {
|
||||
Kernel::g_object_pool.Destroy<T>(handle);
|
||||
Kernel::g_handle_table.Close(handle);
|
||||
m_handles.erase(std::remove(m_handles.begin(), m_handles.end(), handle), m_handles.end());
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ static Result ConnectToPort(Handle* out, const char* port_name) {
|
||||
|
||||
/// Synchronize to an OS service
|
||||
static Result SendSyncRequest(Handle handle) {
|
||||
Kernel::Session* session = Kernel::g_object_pool.Get<Kernel::Session>(handle);
|
||||
Kernel::Session* session = Kernel::g_handle_table.Get<Kernel::Session>(handle);
|
||||
if (session == nullptr) {
|
||||
return InvalidHandle(ErrorModule::Kernel).raw;
|
||||
}
|
||||
@ -119,11 +119,9 @@ static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
|
||||
// TODO(bunnei): Do something with nano_seconds, currently ignoring this
|
||||
bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated
|
||||
|
||||
if (!Kernel::g_object_pool.IsValid(handle)) {
|
||||
Kernel::Object* object = Kernel::g_handle_table.GetGeneric(handle);
|
||||
if (object == nullptr)
|
||||
return InvalidHandle(ErrorModule::Kernel).raw;
|
||||
}
|
||||
Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle);
|
||||
_dbg_assert_(Kernel, object != nullptr);
|
||||
|
||||
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle, object->GetTypeName().c_str(),
|
||||
object->GetName().c_str(), nano_seconds);
|
||||
@ -150,10 +148,9 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
|
||||
|
||||
// Iterate through each handle, synchronize kernel object
|
||||
for (s32 i = 0; i < handle_count; i++) {
|
||||
if (!Kernel::g_object_pool.IsValid(handles[i])) {
|
||||
Kernel::Object* object = Kernel::g_handle_table.GetGeneric(handles[i]);
|
||||
if (object == nullptr)
|
||||
return InvalidHandle(ErrorModule::Kernel).raw;
|
||||
}
|
||||
Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handles[i]);
|
||||
|
||||
LOG_TRACE(Kernel_SVC, "\thandle[%d] = 0x%08X(%s:%s)", i, handles[i], object->GetTypeName().c_str(),
|
||||
object->GetName().c_str());
|
||||
@ -321,19 +318,12 @@ static Result CreateEvent(Handle* evt, u32 reset_type) {
|
||||
|
||||
/// Duplicates a kernel handle
|
||||
static Result DuplicateHandle(Handle* out, Handle handle) {
|
||||
LOG_WARNING(Kernel_SVC, "(STUBBED) called handle=0x%08X", handle);
|
||||
|
||||
// Translate kernel handles -> real handles
|
||||
if (handle == Kernel::CurrentThread) {
|
||||
handle = Kernel::GetCurrentThreadHandle();
|
||||
ResultVal<Handle> out_h = Kernel::g_handle_table.Duplicate(handle);
|
||||
if (out_h.Succeeded()) {
|
||||
*out = *out_h;
|
||||
LOG_TRACE(Kernel_SVC, "duplicated 0x%08X to 0x%08X", handle, *out);
|
||||
}
|
||||
_assert_msg_(KERNEL, (handle != Kernel::CurrentProcess),
|
||||
"(UNIMPLEMENTED) process handle duplication!");
|
||||
|
||||
// TODO(bunnei): FixMe - This is a hack to return the handle that we were asked to duplicate.
|
||||
*out = handle;
|
||||
|
||||
return 0;
|
||||
return out_h.Code().raw;
|
||||
}
|
||||
|
||||
/// Signals an event
|
||||
|
Loading…
Reference in New Issue
Block a user