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
|
|
|
|
|
2014-05-20 19:27:46 -07:00
|
|
|
#include "common/common.h"
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-18 15:12:29 -07:00
|
|
|
typedef u32 Handle;
|
|
|
|
typedef s32 Result;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
namespace Kernel {
|
|
|
|
|
2014-05-26 18:56:23 -07:00
|
|
|
enum KernelHandle {
|
|
|
|
CurrentThread = 0xFFFF8000,
|
|
|
|
CurrentProcess = 0xFFFF8001,
|
|
|
|
};
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
enum class HandleType : u32 {
|
|
|
|
Unknown = 0,
|
|
|
|
Port = 1,
|
|
|
|
Service = 2,
|
|
|
|
Event = 3,
|
|
|
|
Mutex = 4,
|
|
|
|
SharedMemory = 5,
|
|
|
|
Redirection = 6,
|
|
|
|
Thread = 7,
|
|
|
|
Process = 8,
|
|
|
|
Arbiter = 9,
|
|
|
|
File = 10,
|
|
|
|
Semaphore = 11,
|
2014-06-25 15:15:35 -07:00
|
|
|
Archive = 12,
|
2014-05-13 18:57:12 -07:00
|
|
|
};
|
2014-05-20 15:13:25 -07:00
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
enum {
|
2014-05-20 15:13:25 -07:00
|
|
|
MAX_NAME_LENGTH = 0x100,
|
|
|
|
DEFAULT_STACK_SIZE = 0x4000,
|
2014-05-15 15:26:28 -07:00
|
|
|
};
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
class ObjectPool;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
class Object : NonCopyable {
|
|
|
|
friend class ObjectPool;
|
2014-05-15 15:26:28 -07:00
|
|
|
u32 handle;
|
2014-05-09 19:11:18 -07:00
|
|
|
public:
|
2014-05-20 15:13:25 -07:00
|
|
|
virtual ~Object() {}
|
2014-05-15 15:26:28 -07:00
|
|
|
Handle GetHandle() const { return handle; }
|
2014-06-05 21:10:50 -07:00
|
|
|
virtual const char* GetTypeName() const { return "[BAD KERNEL OBJECT TYPE]"; }
|
|
|
|
virtual const char* GetName() const { return "[UNKNOWN KERNEL OBJECT]"; }
|
2014-05-20 15:13:25 -07:00
|
|
|
virtual Kernel::HandleType GetHandleType() const = 0;
|
2014-05-26 19:12:46 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronize kernel object
|
|
|
|
* @param wait Boolean wait set if current thread should wait as a result of sync operation
|
|
|
|
* @return Result of operation, 0 on success, otherwise error code
|
|
|
|
*/
|
2014-06-05 20:13:28 -07:00
|
|
|
virtual Result SyncRequest(bool* wait) {
|
|
|
|
ERROR_LOG(KERNEL, "(UNIMPLEMENTED)");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 19:12:46 -07:00
|
|
|
|
2014-05-26 19:17:49 -07:00
|
|
|
/**
|
|
|
|
* Wait for kernel object to synchronize
|
|
|
|
* @param wait Boolean wait set if current thread should wait as a result of sync operation
|
|
|
|
* @return Result of operation, 0 on success, otherwise error code
|
|
|
|
*/
|
|
|
|
virtual Result WaitSynchronization(bool* wait) = 0;
|
2014-05-09 19:11:18 -07:00
|
|
|
};
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
class ObjectPool : NonCopyable {
|
2014-05-09 19:11:18 -07:00
|
|
|
public:
|
2014-05-20 15:13:25 -07:00
|
|
|
ObjectPool();
|
|
|
|
~ObjectPool() {}
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-15 15:26:28 -07:00
|
|
|
// Allocates a handle within the range and inserts the object into the map.
|
2014-05-20 15:13:25 -07:00
|
|
|
Handle Create(Object* obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF);
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
static Object* CreateByIDType(int type);
|
2014-05-09 19:11:18 -07:00
|
|
|
|
|
|
|
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-20 15:13:25 -07:00
|
|
|
if (t == 0 || t->GetHandleType() != T::GetStaticHandleType()) {
|
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-20 15:13:25 -07:00
|
|
|
return static_cast<T*>(pool[realHandle]);
|
2014-05-09 19:11:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, typename ArgT>
|
2014-05-20 15:13:25 -07:00
|
|
|
void Iterate(bool func(T*, ArgT), ArgT arg) {
|
2014-05-09 19:11:18 -07:00
|
|
|
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;
|
2014-05-20 15:13:25 -07:00
|
|
|
T* t = static_cast<T*>(pool[i]);
|
2014-05-09 19:11:18 -07:00
|
|
|
if (t->GetIDType() == type) {
|
|
|
|
if (!func(t, arg))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
bool GetIDType(Handle handle, HandleType* 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;
|
|
|
|
}
|
2014-05-20 15:13:25 -07:00
|
|
|
Object* t = pool[handle - HANDLE_OFFSET];
|
|
|
|
*type = t->GetHandleType();
|
2014-05-09 19:11:18 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
Object* &operator [](Handle handle);
|
2014-05-09 19:11:18 -07:00
|
|
|
void List();
|
|
|
|
void Clear();
|
|
|
|
int GetCount();
|
|
|
|
|
|
|
|
private:
|
2014-05-20 15:13:25 -07:00
|
|
|
|
2014-05-09 19:11:18 -07:00
|
|
|
enum {
|
|
|
|
MAX_COUNT = 0x1000,
|
|
|
|
HANDLE_OFFSET = 0x100,
|
|
|
|
INITIAL_NEXT_ID = 0x10,
|
|
|
|
};
|
2014-05-20 15:13:25 -07:00
|
|
|
|
|
|
|
Object* pool[MAX_COUNT];
|
|
|
|
bool occupied[MAX_COUNT];
|
|
|
|
int next_id;
|
2014-05-09 19:11:18 -07:00
|
|
|
};
|
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
extern ObjectPool g_object_pool;
|
2014-06-01 18:42:50 -07:00
|
|
|
extern Handle g_main_thread;
|
2014-05-20 15:13:25 -07:00
|
|
|
|
2014-06-10 19:43:50 -07:00
|
|
|
/// Initialize the kernel
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
/// Shutdown the kernel
|
|
|
|
void Shutdown();
|
|
|
|
|
2014-05-22 16:06:12 -07:00
|
|
|
/**
|
|
|
|
* Loads executable stored at specified address
|
|
|
|
* @entry_point Entry point in memory of loaded executable
|
|
|
|
* @return True on success, otherwise false
|
|
|
|
*/
|
|
|
|
bool LoadExec(u32 entry_point);
|
2014-05-13 18:57:12 -07:00
|
|
|
|
2014-05-22 16:06:12 -07:00
|
|
|
} // namespace
|