From b4891fb7a735de0f07e263505387db9d6fe08d21 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 26 Feb 2013 07:57:58 -0800 Subject: [PATCH 1/7] Error checking in sceKernelAllocPartitionMemory(). Before, it was still returning a uid on fail, oops. --- Core/HLE/FunctionWrappers.h | 5 +++++ Core/HLE/sceKernelMemory.cpp | 43 +++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Core/HLE/FunctionWrappers.h b/Core/HLE/FunctionWrappers.h index 85468754b8..e4e80b6631 100644 --- a/Core/HLE/FunctionWrappers.h +++ b/Core/HLE/FunctionWrappers.h @@ -313,6 +313,11 @@ template void WrapI_IIII() { RETURN(retval); } +template void WrapI_ICIUU() { + int retval = func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2), PARAM(3), PARAM(4)); + RETURN(retval); +} + template void WrapI_IIU() { int retval = func(PARAM(0), PARAM(1), PARAM(2)); RETURN(retval); diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index b4d8f9d9d9..da8676d664 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -449,29 +449,50 @@ void sceKernelTotalFreeMemSize() RETURN(retVal); } -void sceKernelAllocPartitionMemory() +int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 size, u32 addr) { - int partid = PARAM(0); - const char *name = Memory::GetCharPointer(PARAM(1)); - int type = PARAM(2); - u32 size = PARAM(3); - int addr = PARAM(4); //only if type includes ADDR + if (name == NULL) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid name", SCE_KERNEL_ERROR_ERROR); + return SCE_KERNEL_ERROR_ERROR; + } + if (size == 0) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid size %x", SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED, size); + return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED; + } + if (partition < 1 || partition > 9 || partition == 7) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid partition %x", SCE_KERNEL_ERROR_ILLEGAL_ARGUMENT, partition); + return SCE_KERNEL_ERROR_ILLEGAL_ARGUMENT; + } + // We only support user right now. + if (partition != 2 && partition != 5 && partition != 6) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid partition %x", SCE_KERNEL_ERROR_ILLEGAL_PARTITION, partition); + return SCE_KERNEL_ERROR_ILLEGAL_PARTITION; + } + if (type < 0 || type > 4) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid type %x", SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE, type); + return SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE; + } - PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, type==1); + PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, type == 1 || type == 4); if (!block->IsValid()) { ERROR_LOG(HLE, "ARGH! sceKernelAllocPartitionMemory failed"); - RETURN(-1); + return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED; } SceUID uid = kernelObjects.Create(block); strncpy(block->name, name, 32); DEBUG_LOG(HLE,"%i = sceKernelAllocPartitionMemory(partition = %i, %s, type= %i, size= %i, addr= %08x)", - uid, partid,name,type,size,addr); + uid, partition, name, type, size, addr); if (type == 2) ERROR_LOG(HLE, "ARGH! sceKernelAllocPartitionMemory wants a specific address"); - RETURN(uid); //for now + return uid; } void sceKernelFreePartitionMemory() @@ -1135,7 +1156,7 @@ const HLEFunction SysMemUserForUser[] = { {0xA291F107,sceKernelMaxFreeMemSize,"sceKernelMaxFreeMemSize"}, {0xF919F628,sceKernelTotalFreeMemSize,"sceKernelTotalFreeMemSize"}, {0x3FC9AE6A,WrapU_V,"sceKernelDevkitVersion"}, - {0x237DBD4F,sceKernelAllocPartitionMemory,"sceKernelAllocPartitionMemory"}, //(int size) ? + {0x237DBD4F,WrapI_ICIUU,"sceKernelAllocPartitionMemory"}, //(int size) ? {0xB6D61D02,sceKernelFreePartitionMemory,"sceKernelFreePartitionMemory"}, //(void *ptr) ? {0x9D9A5BA1,sceKernelGetBlockHeadAddr,"sceKernelGetBlockHeadAddr"}, //(void *ptr) ? {0x13a5abef,sceKernelPrintf,"sceKernelPrintf 0x13a5abef"}, From 9eeb3d6ffda9997c8c8eae487574229b5dd0074b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 26 Feb 2013 08:09:40 -0800 Subject: [PATCH 2/7] Support the other allocation types. --- Core/HLE/sceKernelMemory.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index da8676d664..1b6243f6c5 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -390,6 +390,14 @@ void sceKernelReferFplStatus() ////////////////////////////////////////////////////////////////////////// //00:49:12 ector, well the partitions are 1 = kernel, 2 = user, 3 = me, 4 = kernel mirror :) +enum MemblockType +{ + PSP_SMEM_Low = 0, + PSP_SMEM_High = 1, + PSP_SMEM_Addr = 2, + PSP_SMEM_LowAligned = 3, + PSP_SMEM_HighAligned = 4, +}; class PartitionMemoryBlock : public KernelObject { @@ -404,14 +412,20 @@ public: static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_MPPID; } /// ???? int GetIDType() const { return PPSSPP_KERNEL_TMID_PMB; } - PartitionMemoryBlock(BlockAllocator *_alloc, u32 size, bool fromEnd) + PartitionMemoryBlock(BlockAllocator *_alloc, u32 size, MemblockType type, u32 alignment) { alloc = _alloc; // 0 is used for save states to wake up. if (size != 0) { - address = alloc->Alloc(size, fromEnd, "PMB"); + if (type == PSP_SMEM_Addr) + address = alloc->AllocAt(alignment, size, "PMB"); + // TODO: Check if size is upaligned? + else if (type == PSP_SMEM_LowAligned || type == PSP_SMEM_HighAligned) + address = alloc->AllocAligned(size, alignment, type == PSP_SMEM_HighAligned, "PMB"); + else + address = alloc->Alloc(size, type == PSP_SMEM_High, "PMB"); alloc->ListBlocks(); } } @@ -478,7 +492,7 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 return SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE; } - PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, type == 1 || type == 4); + PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, (MemblockType)type, addr); if (!block->IsValid()) { ERROR_LOG(HLE, "ARGH! sceKernelAllocPartitionMemory failed"); @@ -740,7 +754,7 @@ KernelObject *__KernelMemoryVPLObject() KernelObject *__KernelMemoryPMBObject() { // TODO: We could theoretically handle kernelMemory too, but we don't support that now anyway. - return new PartitionMemoryBlock(&userMemory, 0, true); + return new PartitionMemoryBlock(&userMemory, 0, PSP_SMEM_Low, 0); } // VPL = variable length memory pool From d02879dbafc05b817095db063ae9ed3843e9bec2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Feb 2013 22:35:08 -0800 Subject: [PATCH 3/7] Fix PMB alignment and cleanup free/get. --- Core/HLE/sceKernel.h | 1 + Core/HLE/sceKernelMemory.cpp | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Core/HLE/sceKernel.h b/Core/HLE/sceKernel.h index 17911153d9..65df209f35 100644 --- a/Core/HLE/sceKernel.h +++ b/Core/HLE/sceKernel.h @@ -87,6 +87,7 @@ enum SCE_KERNEL_ERROR_ILLEGAL_CHUNK_ID = 0x800200de, SCE_KERNEL_ERROR_NOCHUNK = 0x800200df, SCE_KERNEL_ERROR_NO_FREECHUNK = 0x800200e0, + SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE = 0x800200e4, SCE_KERNEL_ERROR_LINKERR = 0x8002012c, SCE_KERNEL_ERROR_ILLEGAL_OBJECT = 0x8002012d, SCE_KERNEL_ERROR_UNKNOWN_MODULE = 0x8002012e, diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 1b6243f6c5..77c35dbd7f 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -409,7 +409,7 @@ public: int sz = alloc->GetBlockSizeFromAddress(address); sprintf(ptr, "MemPart: %08x - %08x size: %08x", address, address + sz, sz); } - static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_MPPID; } /// ???? + static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_UID; } int GetIDType() const { return PPSSPP_KERNEL_TMID_PMB; } PartitionMemoryBlock(BlockAllocator *_alloc, u32 size, MemblockType type, u32 alignment) @@ -420,8 +420,10 @@ public: if (size != 0) { if (type == PSP_SMEM_Addr) + { + alignment &= ~0xFF; address = alloc->AllocAt(alignment, size, "PMB"); - // TODO: Check if size is upaligned? + } else if (type == PSP_SMEM_LowAligned || type == PSP_SMEM_HighAligned) address = alloc->AllocAligned(size, alignment, type == PSP_SMEM_HighAligned, "PMB"); else @@ -431,7 +433,8 @@ public: } ~PartitionMemoryBlock() { - alloc->Free(address); + if (address != (u32)-1) + alloc->Free(address); } bool IsValid() {return address != (u32)-1;} BlockAllocator *alloc; @@ -491,6 +494,12 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid type %x", SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE, type); return SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE; } + // Alignment is only allowed for powers of 2. + if ((type == 3 || type == 4) && ((addr & (addr - 1)) != 0 || addr == 0)) + { + WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid alignment %x", SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE, addr); + return SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE; + } PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, (MemblockType)type, addr); if (!block->IsValid()) @@ -509,29 +518,26 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 return uid; } -void sceKernelFreePartitionMemory() +int sceKernelFreePartitionMemory(SceUID id) { - SceUID id = PARAM(0); DEBUG_LOG(HLE,"sceKernelFreePartitionMemory(%d)",id); - RETURN(kernelObjects.Destroy(id)); + return kernelObjects.Destroy(id); } -void sceKernelGetBlockHeadAddr() +u32 sceKernelGetBlockHeadAddr(SceUID id) { - SceUID id = PARAM(0); - u32 error; PartitionMemoryBlock *block = kernelObjects.Get(id, error); if (block) { DEBUG_LOG(HLE,"%08x = sceKernelGetBlockHeadAddr(%i)", block->address, id); - RETURN(block->address); + return block->address; } else { ERROR_LOG(HLE,"sceKernelGetBlockHeadAddr failed(%i)", id); - RETURN(error); + return 0; } } @@ -1171,8 +1177,8 @@ const HLEFunction SysMemUserForUser[] = { {0xF919F628,sceKernelTotalFreeMemSize,"sceKernelTotalFreeMemSize"}, {0x3FC9AE6A,WrapU_V,"sceKernelDevkitVersion"}, {0x237DBD4F,WrapI_ICIUU,"sceKernelAllocPartitionMemory"}, //(int size) ? - {0xB6D61D02,sceKernelFreePartitionMemory,"sceKernelFreePartitionMemory"}, //(void *ptr) ? - {0x9D9A5BA1,sceKernelGetBlockHeadAddr,"sceKernelGetBlockHeadAddr"}, //(void *ptr) ? + {0xB6D61D02,WrapI_I,"sceKernelFreePartitionMemory"}, //(void *ptr) ? + {0x9D9A5BA1,WrapU_I,"sceKernelGetBlockHeadAddr"}, //(void *ptr) ? {0x13a5abef,sceKernelPrintf,"sceKernelPrintf 0x13a5abef"}, {0x7591c7db,&WrapV_I,"sceKernelSetCompiledSdkVersion"}, {0x342061E5,&WrapV_I,"sceKernelSetCompiledSdkVersion370"}, From 4631fd7c6fdbb24412a4ae817792000d27b7180f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Feb 2013 22:35:53 -0800 Subject: [PATCH 4/7] Keep track of the PMB name like other objects. --- Core/HLE/sceKernelMemory.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 77c35dbd7f..69e221234a 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -412,9 +412,11 @@ public: static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_UID; } int GetIDType() const { return PPSSPP_KERNEL_TMID_PMB; } - PartitionMemoryBlock(BlockAllocator *_alloc, u32 size, MemblockType type, u32 alignment) + PartitionMemoryBlock(BlockAllocator *_alloc, const char *_name, u32 size, MemblockType type, u32 alignment) { alloc = _alloc; + strncpy(name, _name, 32); + name[31] = '\0'; // 0 is used for save states to wake up. if (size != 0) @@ -422,12 +424,12 @@ public: if (type == PSP_SMEM_Addr) { alignment &= ~0xFF; - address = alloc->AllocAt(alignment, size, "PMB"); + address = alloc->AllocAt(alignment, size, name); } else if (type == PSP_SMEM_LowAligned || type == PSP_SMEM_HighAligned) - address = alloc->AllocAligned(size, alignment, type == PSP_SMEM_HighAligned, "PMB"); + address = alloc->AllocAligned(size, alignment, type == PSP_SMEM_HighAligned, name); else - address = alloc->Alloc(size, type == PSP_SMEM_High, "PMB"); + address = alloc->Alloc(size, type == PSP_SMEM_High, name); alloc->ListBlocks(); } } @@ -501,7 +503,7 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 return SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE; } - PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, size, (MemblockType)type, addr); + PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, name, size, (MemblockType)type, addr); if (!block->IsValid()) { ERROR_LOG(HLE, "ARGH! sceKernelAllocPartitionMemory failed"); @@ -760,7 +762,7 @@ KernelObject *__KernelMemoryVPLObject() KernelObject *__KernelMemoryPMBObject() { // TODO: We could theoretically handle kernelMemory too, but we don't support that now anyway. - return new PartitionMemoryBlock(&userMemory, 0, PSP_SMEM_Low, 0); + return new PartitionMemoryBlock(&userMemory, "", 0, PSP_SMEM_Low, 0); } // VPL = variable length memory pool From 3bde874bf86e41e3af13b45f651b39a01a591d3a Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Feb 2013 22:54:41 -0800 Subject: [PATCH 5/7] Test update. --- pspautotests | 2 +- test.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pspautotests b/pspautotests index 24620b2ae1..7211f79d7d 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 24620b2ae15d640bdb74cb2f455152855629d2b0 +Subproject commit 7211f79d7df7dc8efc59d3d568a2cbd41772136c diff --git a/test.py b/test.py index f2f4abbe2b..d2836664b5 100755 --- a/test.py +++ b/test.py @@ -162,6 +162,7 @@ tests_next = [ "io/iodrv/iodrv", "modules/loadexec/loader", "rtc/rtc", + "sysmem/partition", "sysmem/sysmem", "umd/io/umd_io", "umd/raw_access/raw_access", From eab7777ec33b471fd80b5a5541eb201c81e2ba2f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Feb 2013 23:01:44 -0800 Subject: [PATCH 6/7] Clean up some magic numbers, oops. --- Core/HLE/sceKernelMemory.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 69e221234a..6d77dec450 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -491,13 +491,13 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid partition %x", SCE_KERNEL_ERROR_ILLEGAL_PARTITION, partition); return SCE_KERNEL_ERROR_ILLEGAL_PARTITION; } - if (type < 0 || type > 4) + if (type < PSP_SMEM_Low || type > PSP_SMEM_HighAligned) { WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid type %x", SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE, type); return SCE_KERNEL_ERROR_ILLEGAL_MEMBLOCKTYPE; } // Alignment is only allowed for powers of 2. - if ((type == 3 || type == 4) && ((addr & (addr - 1)) != 0 || addr == 0)) + if ((type == PSP_SMEM_LowAligned || type == PSP_SMEM_HighAligned) && ((addr & (addr - 1)) != 0 || addr == 0)) { WARN_LOG(HLE, "%08x=sceKernelAllocPartitionMemory(): invalid alignment %x", SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE, addr); return SCE_ERROR_KERNEL_ILLEGAL_ALIGNMENT_SIZE; From 1e3f5477c30fe0bbc58976e6a036e5475f45895a Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 28 Feb 2013 00:19:07 -0800 Subject: [PATCH 7/7] Don't leak block on allocation failure. --- Core/HLE/sceKernelMemory.cpp | 1 + pspautotests | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 6d77dec450..ab44bad4fc 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -506,6 +506,7 @@ int sceKernelAllocPartitionMemory(int partition, const char *name, int type, u32 PartitionMemoryBlock *block = new PartitionMemoryBlock(&userMemory, name, size, (MemblockType)type, addr); if (!block->IsValid()) { + delete block; ERROR_LOG(HLE, "ARGH! sceKernelAllocPartitionMemory failed"); return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED; } diff --git a/pspautotests b/pspautotests index 7211f79d7d..a9c949b87d 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 7211f79d7df7dc8efc59d3d568a2cbd41772136c +Subproject commit a9c949b87dc69d4d6cde854b73455a170a2aaff4