2014-05-09 19:11:18 -07:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
typedef s32 Handle;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-13 18:57:12 -07:00
|
|
|
enum KernelIDType {
|
|
|
|
KERNEL_ID_TYPE_THREAD = 1,
|
|
|
|
KERNEL_ID_TYPE_SEMAPHORE = 2,
|
|
|
|
KERNEL_ID_TYPE_MUTEX = 3,
|
|
|
|
KERNEL_ID_TYPE_EVENT = 4,
|
|
|
|
};
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
enum {
|
|
|
|
KERNELOBJECT_MAX_NAME_LENGTH = 255,
|
|
|
|
};
|
|
|
|
|
2014-05-13 18:57:12 -07:00
|
|
|
#define KERNELOBJECT_MAX_NAME_LENGTH 31
|
|
|
|
|
2014-05-09 19:11:18 -07:00
|
|
|
class KernelObjectPool;
|
|
|
|
|
|
|
|
class KernelObject {
|
|
|
|
friend class KernelObjectPool;
|
2014-05-15 15:26:28 -07:00
|
|
|
u32 handle;
|
2014-05-09 19:11:18 -07:00
|
|
|
public:
|
|
|
|
virtual ~KernelObject() {}
|
2014-05-15 15:26:28 -07:00
|
|
|
Handle GetHandle() const { return handle; }
|
2014-05-09 19:11:18 -07:00
|
|
|
virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; }
|
|
|
|
virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; }
|
2014-05-13 18:57:12 -07:00
|
|
|
virtual KernelIDType GetIDType() const = 0;
|
2014-05-09 19:11:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class KernelObjectPool {
|
|
|
|
public:
|
|
|
|
KernelObjectPool();
|
|
|
|
~KernelObjectPool() {}
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
// Allocates a handle within the range and inserts the object into the map.
|
|
|
|
Handle Create(KernelObject *obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF);
|
2014-05-09 19:11:18 -07:00
|
|
|
|
|
|
|
static KernelObject *CreateByIDType(int type);
|
|
|
|
|
|
|
|
template <class T>
|
2014-05-15 15:26:28 -07:00
|
|
|
u32 Destroy(Handle handle) {
|
2014-05-09 19:11:18 -07:00
|
|
|
u32 error;
|
|
|
|
if (Get<T>(handle, error)) {
|
2014-05-13 18:57:12 -07:00
|
|
|
occupied[handle - HANDLE_OFFSET] = false;
|
|
|
|
delete pool[handle - HANDLE_OFFSET];
|
2014-05-09 19:11:18 -07:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
};
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
bool IsValid(Handle handle);
|
2014-05-09 19:11:18 -07:00
|
|
|
|
|
|
|
template <class T>
|
2014-05-15 15:26:28 -07:00
|
|
|
T* Get(Handle handle, u32& outError) {
|
2014-05-13 18:57:12 -07:00
|
|
|
if (handle < HANDLE_OFFSET || handle >= HANDLE_OFFSET + MAX_COUNT || !occupied[handle - HANDLE_OFFSET]) {
|
2014-05-09 19:11:18 -07:00
|
|
|
// Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP
|
|
|
|
if (handle != 0 && (u32)handle != 0x80020001) {
|
2014-05-13 18:57:12 -07:00
|
|
|
WARN_LOG(KERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle);
|
2014-05-09 19:11:18 -07:00
|
|
|
}
|
2014-05-13 18:57:12 -07:00
|
|
|
outError = 0;//T::GetMissingErrorCode();
|
2014-05-09 19:11:18 -07:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// Previously we had a dynamic_cast here, but since RTTI was disabled traditionally,
|
|
|
|
// it just acted as a static case and everything worked. This means that we will never
|
|
|
|
// see the Wrong type object error below, but we'll just have to live with that danger.
|
2014-05-13 18:57:12 -07:00
|
|
|
T* t = static_cast<T*>(pool[handle - HANDLE_OFFSET]);
|
2014-05-09 19:11:18 -07:00
|
|
|
if (t == 0 || t->GetIDType() != T::GetStaticIDType()) {
|
2014-05-13 18:57:12 -07:00
|
|
|
WARN_LOG(KERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle);
|
|
|
|
outError = 0;//T::GetMissingErrorCode();
|
2014-05-09 19:11:18 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-13 18:57:12 -07:00
|
|
|
outError = 0;//SCE_KERNEL_ERROR_OK;
|
2014-05-09 19:11:18 -07:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ONLY use this when you know the handle is valid.
|
|
|
|
template <class T>
|
2014-05-15 15:26:28 -07:00
|
|
|
T *GetFast(Handle handle) {
|
|
|
|
const Handle realHandle = handle - HANDLE_OFFSET;
|
2014-05-13 18:57:12 -07:00
|
|
|
_dbg_assert_(KERNEL, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]);
|
2014-05-09 19:11:18 -07:00
|
|
|
return static_cast<T *>(pool[realHandle]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, typename ArgT>
|
|
|
|
void Iterate(bool func(T *, ArgT), ArgT arg) {
|
|
|
|
int type = T::GetStaticIDType();
|
2014-05-13 18:57:12 -07:00
|
|
|
for (int i = 0; i < MAX_COUNT; i++)
|
2014-05-09 19:11:18 -07:00
|
|
|
{
|
|
|
|
if (!occupied[i])
|
|
|
|
continue;
|
|
|
|
T *t = static_cast<T *>(pool[i]);
|
|
|
|
if (t->GetIDType() == type) {
|
|
|
|
if (!func(t, arg))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
bool GetIDType(Handle handle, int *type) const {
|
2014-05-09 19:11:18 -07:00
|
|
|
if ((handle < HANDLE_OFFSET) || (handle >= HANDLE_OFFSET + MAX_COUNT) ||
|
|
|
|
!occupied[handle - HANDLE_OFFSET]) {
|
|
|
|
ERROR_LOG(KERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
KernelObject *t = pool[handle - HANDLE_OFFSET];
|
|
|
|
*type = t->GetIDType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
KernelObject *&operator [](Handle handle);
|
2014-05-09 19:11:18 -07:00
|
|
|
void List();
|
|
|
|
void Clear();
|
|
|
|
int GetCount();
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
MAX_COUNT = 0x1000,
|
|
|
|
HANDLE_OFFSET = 0x100,
|
|
|
|
INITIAL_NEXT_ID = 0x10,
|
|
|
|
};
|
|
|
|
KernelObject *pool[MAX_COUNT];
|
|
|
|
bool occupied[MAX_COUNT];
|
|
|
|
int next_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern KernelObjectPool g_kernel_objects;
|
2014-05-13 18:57:12 -07:00
|
|
|
|
|
|
|
bool __KernelLoadExec(u32 entry_point);
|