2014-05-10 07:41:18 +05:30
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
|
|
|
// Licensed under GPLv2
|
2014-11-19 14:19:13 +05:30
|
|
|
// Refer to the license.txt file included.
|
2014-05-10 07:41:18 +05:30
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-08-19 09:50:56 +05:30
|
|
|
#include <array>
|
2014-08-18 08:33:22 +05:30
|
|
|
#include <string>
|
2014-05-21 07:57:46 +05:30
|
|
|
#include "common/common.h"
|
2014-10-23 08:50:01 +05:30
|
|
|
#include "core/hle/result.h"
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-19 03:42:29 +05:30
|
|
|
typedef u32 Handle;
|
|
|
|
typedef s32 Result;
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
namespace Kernel {
|
|
|
|
|
2014-05-27 07:26:23 +05:30
|
|
|
enum KernelHandle {
|
|
|
|
CurrentThread = 0xFFFF8000,
|
|
|
|
CurrentProcess = 0xFFFF8001,
|
|
|
|
};
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
enum class HandleType : u32 {
|
|
|
|
Unknown = 0,
|
|
|
|
Port = 1,
|
|
|
|
Service = 2,
|
|
|
|
Event = 3,
|
|
|
|
Mutex = 4,
|
|
|
|
SharedMemory = 5,
|
|
|
|
Redirection = 6,
|
|
|
|
Thread = 7,
|
|
|
|
Process = 8,
|
2014-07-07 08:45:40 +05:30
|
|
|
AddressArbiter = 9,
|
2014-05-21 03:43:25 +05:30
|
|
|
File = 10,
|
|
|
|
Semaphore = 11,
|
2014-06-26 03:45:35 +05:30
|
|
|
Archive = 12,
|
2014-09-12 04:18:04 +05:30
|
|
|
Directory = 13,
|
2014-05-14 07:27:12 +05:30
|
|
|
};
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-16 03:56:28 +05:30
|
|
|
enum {
|
2014-05-21 03:43:25 +05:30
|
|
|
DEFAULT_STACK_SIZE = 0x4000,
|
2014-05-16 03:56:28 +05:30
|
|
|
};
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
class ObjectPool;
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
class Object : NonCopyable {
|
|
|
|
friend class ObjectPool;
|
2014-05-16 03:56:28 +05:30
|
|
|
u32 handle;
|
2014-05-10 07:41:18 +05:30
|
|
|
public:
|
2014-05-21 03:43:25 +05:30
|
|
|
virtual ~Object() {}
|
2014-05-16 03:56:28 +05:30
|
|
|
Handle GetHandle() const { return handle; }
|
2014-08-18 08:33:22 +05:30
|
|
|
virtual std::string GetTypeName() const { return "[BAD KERNEL OBJECT TYPE]"; }
|
|
|
|
virtual std::string GetName() const { return "[UNKNOWN KERNEL OBJECT]"; }
|
2014-05-21 03:43:25 +05:30
|
|
|
virtual Kernel::HandleType GetHandleType() const = 0;
|
2014-05-27 07:42:46 +05:30
|
|
|
|
|
|
|
/**
|
2014-10-23 08:50:01 +05:30
|
|
|
* Synchronize kernel object.
|
|
|
|
* @return True if the current thread should wait as a result of the sync
|
2014-05-27 07:42:46 +05:30
|
|
|
*/
|
2014-10-23 08:50:01 +05:30
|
|
|
virtual ResultVal<bool> SyncRequest() {
|
2014-12-06 07:23:49 +05:30
|
|
|
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
|
2014-10-23 08:50:01 +05:30
|
|
|
return UnimplementedFunction(ErrorModule::Kernel);
|
2014-06-06 08:43:28 +05:30
|
|
|
}
|
2014-05-27 07:42:46 +05:30
|
|
|
|
2014-05-27 07:47:49 +05:30
|
|
|
/**
|
2014-10-23 08:50:01 +05:30
|
|
|
* Wait for kernel object to synchronize.
|
|
|
|
* @return True if the current thread should wait as a result of the wait
|
2014-05-27 07:47:49 +05:30
|
|
|
*/
|
2014-12-06 07:23:49 +05:30
|
|
|
virtual ResultVal<bool> WaitSynchronization() {
|
|
|
|
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
|
|
|
|
return UnimplementedFunction(ErrorModule::Kernel);
|
|
|
|
}
|
2014-05-10 07:41:18 +05:30
|
|
|
};
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
class ObjectPool : NonCopyable {
|
2014-05-10 07:41:18 +05:30
|
|
|
public:
|
2014-05-21 03:43:25 +05:30
|
|
|
ObjectPool();
|
|
|
|
~ObjectPool() {}
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-16 03:56:28 +05:30
|
|
|
// Allocates a handle within the range and inserts the object into the map.
|
2014-05-21 03:43:25 +05:30
|
|
|
Handle Create(Object* obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF);
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
static Object* CreateByIDType(int type);
|
2014-05-10 07:41:18 +05:30
|
|
|
|
|
|
|
template <class T>
|
2014-10-23 08:50:01 +05:30
|
|
|
void Destroy(Handle handle) {
|
|
|
|
if (Get<T>(handle)) {
|
2014-05-14 07:27:12 +05:30
|
|
|
occupied[handle - HANDLE_OFFSET] = false;
|
|
|
|
delete pool[handle - HANDLE_OFFSET];
|
2014-05-10 07:41:18 +05:30
|
|
|
}
|
2014-11-18 18:57:16 +05:30
|
|
|
}
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-12-04 06:18:34 +05:30
|
|
|
bool IsValid(Handle handle) const;
|
2014-05-10 07:41:18 +05:30
|
|
|
|
|
|
|
template <class T>
|
2014-10-23 08:50:01 +05:30
|
|
|
T* Get(Handle handle) {
|
2014-05-14 07:27:12 +05:30
|
|
|
if (handle < HANDLE_OFFSET || handle >= HANDLE_OFFSET + MAX_COUNT || !occupied[handle - HANDLE_OFFSET]) {
|
2014-10-23 08:50:01 +05:30
|
|
|
if (handle != 0) {
|
2014-12-13 20:45:58 +05:30
|
|
|
LOG_ERROR(Kernel, "Bad object handle %08x", handle);
|
2014-05-10 07:41:18 +05:30
|
|
|
}
|
2014-10-23 08:50:01 +05:30
|
|
|
return nullptr;
|
2014-05-10 07:41:18 +05:30
|
|
|
} else {
|
2014-10-23 08:50:01 +05:30
|
|
|
Object* t = pool[handle - HANDLE_OFFSET];
|
|
|
|
if (t->GetHandleType() != T::GetStaticHandleType()) {
|
2014-12-13 20:45:58 +05:30
|
|
|
LOG_ERROR(Kernel, "Wrong object type for %08x", handle);
|
2014-10-23 08:50:01 +05:30
|
|
|
return nullptr;
|
2014-05-10 07:41:18 +05:30
|
|
|
}
|
2014-10-23 08:50:01 +05:30
|
|
|
return static_cast<T*>(t);
|
2014-05-10 07:41:18 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ONLY use this when you know the handle is valid.
|
|
|
|
template <class T>
|
2014-05-16 03:56:28 +05:30
|
|
|
T *GetFast(Handle handle) {
|
|
|
|
const Handle realHandle = handle - HANDLE_OFFSET;
|
2014-12-06 07:23:49 +05:30
|
|
|
_dbg_assert_(Kernel, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]);
|
2014-05-21 03:43:25 +05:30
|
|
|
return static_cast<T*>(pool[realHandle]);
|
2014-05-10 07:41:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, typename ArgT>
|
2014-05-21 03:43:25 +05:30
|
|
|
void Iterate(bool func(T*, ArgT), ArgT arg) {
|
2014-05-10 07:41:18 +05:30
|
|
|
int type = T::GetStaticIDType();
|
2014-05-14 07:27:12 +05:30
|
|
|
for (int i = 0; i < MAX_COUNT; i++)
|
2014-05-10 07:41:18 +05:30
|
|
|
{
|
|
|
|
if (!occupied[i])
|
|
|
|
continue;
|
2014-05-21 03:43:25 +05:30
|
|
|
T* t = static_cast<T*>(pool[i]);
|
2014-05-10 07:41:18 +05:30
|
|
|
if (t->GetIDType() == type) {
|
|
|
|
if (!func(t, arg))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
bool GetIDType(Handle handle, HandleType* type) const {
|
2014-11-19 14:19:13 +05:30
|
|
|
if ((handle < HANDLE_OFFSET) || (handle >= HANDLE_OFFSET + MAX_COUNT) ||
|
2014-12-06 07:23:49 +05:30
|
|
|
!occupied[handle - HANDLE_OFFSET]) {
|
2014-12-13 20:45:58 +05:30
|
|
|
LOG_ERROR(Kernel, "Bad object handle %08X", handle);
|
2014-05-10 07:41:18 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-05-21 03:43:25 +05:30
|
|
|
Object* t = pool[handle - HANDLE_OFFSET];
|
|
|
|
*type = t->GetHandleType();
|
2014-05-10 07:41:18 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
Object* &operator [](Handle handle);
|
2014-05-10 07:41:18 +05:30
|
|
|
void List();
|
|
|
|
void Clear();
|
2014-12-04 06:18:34 +05:30
|
|
|
int GetCount() const;
|
2014-05-10 07:41:18 +05:30
|
|
|
|
|
|
|
private:
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-10 07:41:18 +05:30
|
|
|
enum {
|
|
|
|
MAX_COUNT = 0x1000,
|
|
|
|
HANDLE_OFFSET = 0x100,
|
|
|
|
INITIAL_NEXT_ID = 0x10,
|
|
|
|
};
|
2014-05-21 03:43:25 +05:30
|
|
|
|
2014-08-19 09:50:56 +05:30
|
|
|
std::array<Object*, MAX_COUNT> pool;
|
|
|
|
std::array<bool, MAX_COUNT> occupied;
|
|
|
|
int next_id;
|
2014-05-10 07:41:18 +05:30
|
|
|
};
|
|
|
|
|
2014-05-21 03:43:25 +05:30
|
|
|
extern ObjectPool g_object_pool;
|
2014-06-02 07:12:50 +05:30
|
|
|
extern Handle g_main_thread;
|
2014-05-21 03:43:25 +05:30
|
|
|
|
2014-06-11 08:13:50 +05:30
|
|
|
/// Initialize the kernel
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
/// Shutdown the kernel
|
|
|
|
void Shutdown();
|
|
|
|
|
2014-05-23 04:36:12 +05:30
|
|
|
/**
|
|
|
|
* 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-14 07:27:12 +05:30
|
|
|
|
2014-05-23 04:36:12 +05:30
|
|
|
} // namespace
|