From 6b264518a50ce21cb1be55ff3eac4e1c85582cfe Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 May 2014 22:11:18 -0400 Subject: added initial kernel/thread modules --- src/core/hle/kernel/kernel.h | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/core/hle/kernel/kernel.h (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h new file mode 100644 index 000000000..2381ca7f7 --- /dev/null +++ b/src/core/hle/kernel/kernel.h @@ -0,0 +1,121 @@ +// Copyright 2014 Citra Emulator Project / PPSSPP Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +typedef u32 UID; + +class KernelObjectPool; + +class KernelObject { + friend class KernelObjectPool; + u32 uid; +public: + virtual ~KernelObject() {} + UID GetUID() const { return uid; } + virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; } + virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; } + virtual int GetIDType() const = 0; + //virtual void GetQuickInfo(char *ptr, int size); +}; + +class KernelObjectPool { +public: + KernelObjectPool(); + ~KernelObjectPool() {} + + // Allocates a UID within the range and inserts the object into the map. + UID Create(KernelObject *obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF); + + static KernelObject *CreateByIDType(int type); + + template + u32 Destroy(UID handle) { + u32 error; + if (Get(handle, error)) { + occupied[handle - handleOffset] = false; + delete pool[handle - handleOffset]; + } + return error; + }; + + bool IsValid(UID handle); + + template + T* Get(UID handle, u32& outError) { + if (handle < handleOffset || handle >= handleOffset + maxCount || !occupied[handle - handleOffset]) { + // Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP + if (handle != 0 && (u32)handle != 0x80020001) { + WARN_LOG(SCEKERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); + } + outError = T::GetMissingErrorCode(); + 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. + T* t = static_cast(pool[handle - handleOffset]); + if (t == 0 || t->GetIDType() != T::GetStaticIDType()) { + WARN_LOG(SCEKERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle); + outError = T::GetMissingErrorCode(); + return 0; + } + outError = SCE_KERNEL_ERROR_OK; + return t; + } + } + + // ONLY use this when you know the handle is valid. + template + T *GetFast(UID handle) { + const UID realHandle = handle - handleOffset; + _dbg_assert_(SCEKERNEL, realHandle >= 0 && realHandle < maxCount && occupied[realHandle]); + return static_cast(pool[realHandle]); + } + + template + void Iterate(bool func(T *, ArgT), ArgT arg) { + int type = T::GetStaticIDType(); + for (int i = 0; i < maxCount; i++) + { + if (!occupied[i]) + continue; + T *t = static_cast(pool[i]); + if (t->GetIDType() == type) { + if (!func(t, arg)) + break; + } + } + } + + bool GetIDType(UID handle, int *type) const { + 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; + } + + KernelObject *&operator [](UID handle); + 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; -- cgit v1.2.3 From 1583d2b6f32ec6f558646284a8130c0759e93e12 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 13 May 2014 21:57:12 -0400 Subject: - added __KernelLoadExec function - fixed some logging --- src/core/hle/kernel/kernel.h | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 2381ca7f7..0eb58210c 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -8,6 +8,15 @@ typedef u32 UID; +enum KernelIDType { + KERNEL_ID_TYPE_THREAD = 1, + KERNEL_ID_TYPE_SEMAPHORE = 2, + KERNEL_ID_TYPE_MUTEX = 3, + KERNEL_ID_TYPE_EVENT = 4, +}; + +#define KERNELOBJECT_MAX_NAME_LENGTH 31 + class KernelObjectPool; class KernelObject { @@ -18,7 +27,7 @@ public: UID GetUID() const { return uid; } virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; } virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; } - virtual int GetIDType() const = 0; + virtual KernelIDType GetIDType() const = 0; //virtual void GetQuickInfo(char *ptr, int size); }; @@ -36,8 +45,8 @@ public: u32 Destroy(UID handle) { u32 error; if (Get(handle, error)) { - occupied[handle - handleOffset] = false; - delete pool[handle - handleOffset]; + occupied[handle - HANDLE_OFFSET] = false; + delete pool[handle - HANDLE_OFFSET]; } return error; }; @@ -46,24 +55,24 @@ public: template T* Get(UID handle, u32& outError) { - if (handle < handleOffset || handle >= handleOffset + maxCount || !occupied[handle - handleOffset]) { + if (handle < HANDLE_OFFSET || handle >= HANDLE_OFFSET + MAX_COUNT || !occupied[handle - HANDLE_OFFSET]) { // Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP if (handle != 0 && (u32)handle != 0x80020001) { - WARN_LOG(SCEKERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); + WARN_LOG(KERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); } - outError = T::GetMissingErrorCode(); + outError = 0;//T::GetMissingErrorCode(); 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. - T* t = static_cast(pool[handle - handleOffset]); + T* t = static_cast(pool[handle - HANDLE_OFFSET]); if (t == 0 || t->GetIDType() != T::GetStaticIDType()) { - WARN_LOG(SCEKERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle); - outError = T::GetMissingErrorCode(); + WARN_LOG(KERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle); + outError = 0;//T::GetMissingErrorCode(); return 0; } - outError = SCE_KERNEL_ERROR_OK; + outError = 0;//SCE_KERNEL_ERROR_OK; return t; } } @@ -71,15 +80,15 @@ public: // ONLY use this when you know the handle is valid. template T *GetFast(UID handle) { - const UID realHandle = handle - handleOffset; - _dbg_assert_(SCEKERNEL, realHandle >= 0 && realHandle < maxCount && occupied[realHandle]); + const UID realHandle = handle - HANDLE_OFFSET; + _dbg_assert_(KERNEL, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]); return static_cast(pool[realHandle]); } template void Iterate(bool func(T *, ArgT), ArgT arg) { int type = T::GetStaticIDType(); - for (int i = 0; i < maxCount; i++) + for (int i = 0; i < MAX_COUNT; i++) { if (!occupied[i]) continue; @@ -119,3 +128,5 @@ private: }; extern KernelObjectPool g_kernel_objects; + +bool __KernelLoadExec(u32 entry_point); -- cgit v1.2.3 From a7cc430aa4da2962dcf08db2f6009fc272bdda70 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 15 May 2014 18:26:28 -0400 Subject: changed "UID" to "Handle" to be a little more consistent with CTR naming --- src/core/hle/kernel/kernel.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 0eb58210c..24d422682 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -6,7 +6,7 @@ #include "common/common_types.h" -typedef u32 UID; +typedef s32 Handle; enum KernelIDType { KERNEL_ID_TYPE_THREAD = 1, @@ -15,20 +15,23 @@ enum KernelIDType { KERNEL_ID_TYPE_EVENT = 4, }; +enum { + KERNELOBJECT_MAX_NAME_LENGTH = 255, +}; + #define KERNELOBJECT_MAX_NAME_LENGTH 31 class KernelObjectPool; class KernelObject { friend class KernelObjectPool; - u32 uid; + u32 handle; public: virtual ~KernelObject() {} - UID GetUID() const { return uid; } + Handle GetHandle() const { return handle; } virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; } virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; } virtual KernelIDType GetIDType() const = 0; - //virtual void GetQuickInfo(char *ptr, int size); }; class KernelObjectPool { @@ -36,13 +39,13 @@ public: KernelObjectPool(); ~KernelObjectPool() {} - // Allocates a UID within the range and inserts the object into the map. - UID Create(KernelObject *obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF); + // 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); static KernelObject *CreateByIDType(int type); template - u32 Destroy(UID handle) { + u32 Destroy(Handle handle) { u32 error; if (Get(handle, error)) { occupied[handle - HANDLE_OFFSET] = false; @@ -51,10 +54,10 @@ public: return error; }; - bool IsValid(UID handle); + bool IsValid(Handle handle); template - T* Get(UID handle, u32& outError) { + T* Get(Handle handle, u32& outError) { if (handle < HANDLE_OFFSET || handle >= HANDLE_OFFSET + MAX_COUNT || !occupied[handle - HANDLE_OFFSET]) { // Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP if (handle != 0 && (u32)handle != 0x80020001) { @@ -79,8 +82,8 @@ public: // ONLY use this when you know the handle is valid. template - T *GetFast(UID handle) { - const UID realHandle = handle - HANDLE_OFFSET; + T *GetFast(Handle handle) { + const Handle realHandle = handle - HANDLE_OFFSET; _dbg_assert_(KERNEL, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]); return static_cast(pool[realHandle]); } @@ -100,7 +103,7 @@ public: } } - bool GetIDType(UID handle, int *type) const { + bool GetIDType(Handle handle, int *type) const { if ((handle < HANDLE_OFFSET) || (handle >= HANDLE_OFFSET + MAX_COUNT) || !occupied[handle - HANDLE_OFFSET]) { ERROR_LOG(KERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); @@ -111,7 +114,7 @@ public: return true; } - KernelObject *&operator [](UID handle); + KernelObject *&operator [](Handle handle); void List(); void Clear(); int GetCount(); -- cgit v1.2.3 From 7cdb70505944b2ed456d7f5376594e05f3b3357f Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 16 May 2014 23:48:15 -0400 Subject: - replaced KERNELOBJECT_MAX_NAME_LENGTH with KERNEL_MAX_NAME_LENGTH - added KERNEL_DEFAULT_STACK_SIZE definition (0x4000) --- src/core/hle/kernel/kernel.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 24d422682..2608eecc9 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -16,11 +16,10 @@ enum KernelIDType { }; enum { - KERNELOBJECT_MAX_NAME_LENGTH = 255, + KERNEL_MAX_NAME_LENGTH = 0x100, + KERNEL_DEFAULT_STACK_SIZE = 0x4000, }; -#define KERNELOBJECT_MAX_NAME_LENGTH 31 - class KernelObjectPool; class KernelObject { -- cgit v1.2.3 From 772abad77803809d8ee857efc0d7e29c36c6b2cb Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 18 May 2014 18:12:29 -0400 Subject: - moved Handle/Result definitions to kernel.h - added ResetType enum --- src/core/hle/kernel/kernel.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 2608eecc9..d4bb28c72 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -6,7 +6,8 @@ #include "common/common_types.h" -typedef s32 Handle; +typedef u32 Handle; +typedef s32 Result; enum KernelIDType { KERNEL_ID_TYPE_THREAD = 1, -- cgit v1.2.3 From eab6fd01d7d2e9b7434a8c5654d424cb563c3784 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 18 May 2014 21:43:29 -0400 Subject: - updated service(s) to be KernelObject's - various cleanups --- src/core/hle/kernel/kernel.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index d4bb28c72..8f2b7b36d 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -10,10 +10,11 @@ typedef u32 Handle; typedef s32 Result; enum KernelIDType { - KERNEL_ID_TYPE_THREAD = 1, - KERNEL_ID_TYPE_SEMAPHORE = 2, - KERNEL_ID_TYPE_MUTEX = 3, - KERNEL_ID_TYPE_EVENT = 4, + KERNEL_ID_TYPE_THREAD, + KERNEL_ID_TYPE_SEMAPHORE, + KERNEL_ID_TYPE_MUTEX, + KERNEL_ID_TYPE_EVENT, + KERNEL_ID_TYPE_SERVICE, }; enum { -- cgit v1.2.3 From 44336329eddd7dbe1f76144e9a1e95e5f76ed372 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 May 2014 18:13:25 -0400 Subject: - created a Kernel namespace - cleaned up Kernel code a bit (moved stuff into namespace, fixed whitespace issues) - added handle types for all different CTROS handles --- src/core/hle/kernel/kernel.h | 75 ++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 31 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 8f2b7b36d..19edb7b57 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -9,41 +9,50 @@ typedef u32 Handle; typedef s32 Result; -enum KernelIDType { - KERNEL_ID_TYPE_THREAD, - KERNEL_ID_TYPE_SEMAPHORE, - KERNEL_ID_TYPE_MUTEX, - KERNEL_ID_TYPE_EVENT, - KERNEL_ID_TYPE_SERVICE, +namespace Kernel { + +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, }; - + enum { - KERNEL_MAX_NAME_LENGTH = 0x100, - KERNEL_DEFAULT_STACK_SIZE = 0x4000, + MAX_NAME_LENGTH = 0x100, + DEFAULT_STACK_SIZE = 0x4000, }; -class KernelObjectPool; +class ObjectPool; -class KernelObject { - friend class KernelObjectPool; +class Object : NonCopyable { + friend class ObjectPool; u32 handle; public: - virtual ~KernelObject() {} + virtual ~Object() {} Handle GetHandle() const { return handle; } virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; } virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; } - virtual KernelIDType GetIDType() const = 0; + virtual Kernel::HandleType GetHandleType() const = 0; }; -class KernelObjectPool { +class ObjectPool : NonCopyable { public: - KernelObjectPool(); - ~KernelObjectPool() {} + ObjectPool(); + ~ObjectPool() {} // 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); + Handle Create(Object* obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF); - static KernelObject *CreateByIDType(int type); + static Object* CreateByIDType(int type); template u32 Destroy(Handle handle) { @@ -71,7 +80,7 @@ public: // 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. T* t = static_cast(pool[handle - HANDLE_OFFSET]); - if (t == 0 || t->GetIDType() != T::GetStaticIDType()) { + if (t == 0 || t->GetHandleType() != T::GetStaticHandleType()) { WARN_LOG(KERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle); outError = 0;//T::GetMissingErrorCode(); return 0; @@ -86,17 +95,17 @@ public: T *GetFast(Handle handle) { const Handle realHandle = handle - HANDLE_OFFSET; _dbg_assert_(KERNEL, realHandle >= 0 && realHandle < MAX_COUNT && occupied[realHandle]); - return static_cast(pool[realHandle]); + return static_cast(pool[realHandle]); } template - void Iterate(bool func(T *, ArgT), ArgT arg) { + 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(pool[i]); + T* t = static_cast(pool[i]); if (t->GetIDType() == type) { if (!func(t, arg)) break; @@ -104,33 +113,37 @@ public: } } - bool GetIDType(Handle handle, int *type) const { + bool GetIDType(Handle handle, HandleType* type) const { 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(); + Object* t = pool[handle - HANDLE_OFFSET]; + *type = t->GetHandleType(); return true; } - KernelObject *&operator [](Handle handle); + Object* &operator [](Handle handle); 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; + + Object* pool[MAX_COUNT]; + bool occupied[MAX_COUNT]; + int next_id; }; -extern KernelObjectPool g_kernel_objects; +extern ObjectPool g_object_pool; + +} // namespace bool __KernelLoadExec(u32 entry_point); -- cgit v1.2.3 From 1c5802c35a668cbb326cb497708bde341cd63f6d Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 May 2014 22:27:46 -0400 Subject: kernel: fixed include, in general include "common.h" not "common_types.h" --- src/core/hle/kernel/kernel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 19edb7b57..180339914 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -4,7 +4,7 @@ #pragma once -#include "common/common_types.h" +#include "common/common.h" typedef u32 Handle; typedef s32 Result; -- cgit v1.2.3 From d26f3d4c1ff27f740fe7185e1bca7dcfc5851919 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 22 May 2014 19:06:12 -0400 Subject: kernel: refactored function naming to remove "__" prefix --- src/core/hle/kernel/kernel.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/kernel.h') diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 180339914..7cd79c2c4 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -144,6 +144,11 @@ private: extern ObjectPool g_object_pool; -} // namespace +/** + * 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); -bool __KernelLoadExec(u32 entry_point); +} // namespace -- cgit v1.2.3