From f649364b2de6f02c3bfa936b4caf942c19f84beb Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 12:05:02 -0700 Subject: [PATCH 1/8] Add a null check to scePsmfPlayerGetAudioData(). Crimson Gem Saga was passing a NULL, maybe a glitch, but we do NULL checks elsewhere. --- Core/HLE/scePsmf.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/HLE/scePsmf.cpp b/Core/HLE/scePsmf.cpp index 0550d2fed0..634666d8aa 100644 --- a/Core/HLE/scePsmf.cpp +++ b/Core/HLE/scePsmf.cpp @@ -779,7 +779,8 @@ int scePsmfPlayerGetAudioData(u32 psmfPlayer, u32 audioDataAddr) return ERROR_PSMF_NOT_FOUND; } - Memory::Memset(audioDataAddr, 0, audioSamplesBytes); + if (Memory::IsValidAddress(audioDataAddr)) + Memory::Memset(audioDataAddr, 0, audioSamplesBytes); return 0; } From ac9e936ba8a3d83afec6076f1fc6ad40f2310fe0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 13:46:50 -0700 Subject: [PATCH 2/8] Fix sema and event flag refer to check size. If it's zero, write nothing, just like the others. --- Core/HLE/sceKernelEventFlag.cpp | 10 ++++++++-- Core/HLE/sceKernelSemaphore.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceKernelEventFlag.cpp b/Core/HLE/sceKernelEventFlag.cpp index 34e44ec9bf..ec07532af9 100644 --- a/Core/HLE/sceKernelEventFlag.cpp +++ b/Core/HLE/sceKernelEventFlag.cpp @@ -661,16 +661,22 @@ int sceKernelPollEventFlag(SceUID id, u32 bits, u32 wait, u32 outBitsPtr, u32 ti //int sceKernelReferEventFlagStatus(SceUID event, SceKernelEventFlagInfo *status); u32 sceKernelReferEventFlagStatus(SceUID id, u32 statusPtr) { - DEBUG_LOG(HLE, "sceKernelReferEventFlagStatus(%i, %08x)", id, statusPtr); u32 error; EventFlag *e = kernelObjects.Get(id, error); if (e) { - Memory::WriteStruct(statusPtr, &e->nef); + DEBUG_LOG(HLE, "sceKernelReferEventFlagStatus(%i, %08x)", id, statusPtr); + + if (!Memory::IsValidAddress(statusPtr)) + return -1; + + if (Memory::Read_U32(statusPtr) != 0) + Memory::WriteStruct(statusPtr, &e->nef); return 0; } else { + ERROR_LOG(HLE, "sceKernelReferEventFlagStatus(%i, %08x): invalid event flag", id, statusPtr); return error; } } diff --git a/Core/HLE/sceKernelSemaphore.cpp b/Core/HLE/sceKernelSemaphore.cpp index f709874eca..89a6c92c04 100644 --- a/Core/HLE/sceKernelSemaphore.cpp +++ b/Core/HLE/sceKernelSemaphore.cpp @@ -328,7 +328,12 @@ int sceKernelReferSemaStatus(SceUID id, u32 infoPtr) if (s) { DEBUG_LOG(HLE, "sceKernelReferSemaStatus(%i, %08x)", id, infoPtr); - Memory::WriteStruct(infoPtr, &s->ns); + + if (!Memory::IsValidAddress(infoPtr)) + return -1; + + if (Memory::Read_U32(infoPtr) != 0) + Memory::WriteStruct(infoPtr, &s->ns); return 0; } else From 3fbc3bd14a8c2ca122d714df6b3feaf6be9b7522 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 15:30:10 -0700 Subject: [PATCH 3/8] Don't allow attr 0x400 for mutexes, report less. Seems like 0 is ignored afaict, which makes sense from other funcs. Not sure if anyone calls it with a value. --- Core/HLE/sceKernelMutex.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceKernelMutex.cpp b/Core/HLE/sceKernelMutex.cpp index a08d7ee009..b8d08ea2b2 100644 --- a/Core/HLE/sceKernelMutex.cpp +++ b/Core/HLE/sceKernelMutex.cpp @@ -360,7 +360,7 @@ int sceKernelCreateMutex(const char *name, u32 attr, int initialCount, u32 optio WARN_LOG_REPORT(HLE, "%08x=sceKernelCreateMutex(): invalid name", SCE_KERNEL_ERROR_ERROR); return SCE_KERNEL_ERROR_ERROR; } - if (attr >= 0xC00) + if (attr & ~0xBFF) { WARN_LOG_REPORT(HLE, "%08x=sceKernelCreateMutex(): invalid attr parameter: %08x", SCE_KERNEL_ERROR_ILLEGAL_ATTR, attr); return SCE_KERNEL_ERROR_ILLEGAL_ATTR; @@ -390,7 +390,11 @@ int sceKernelCreateMutex(const char *name, u32 attr, int initialCount, u32 optio DEBUG_LOG(HLE, "%i=sceKernelCreateMutex(%s, %08x, %d, %08x)", id, name, attr, initialCount, optionsPtr); if (optionsPtr != 0) - WARN_LOG_REPORT(HLE, "sceKernelCreateMutex(%s) unsupported options parameter: %08x", name, optionsPtr); + { + u32 size = Memory::Read_U32(optionsPtr); + if (size != 0) + WARN_LOG_REPORT(HLE, "sceKernelCreateMutex(%s) unsupported options parameter, size = %d", name, size); + } if ((attr & ~PSP_MUTEX_ATTR_KNOWN) != 0) WARN_LOG_REPORT(HLE, "sceKernelCreateMutex(%s) unsupported attr parameter: %08x", name, attr); @@ -735,7 +739,11 @@ int sceKernelCreateLwMutex(u32 workareaPtr, const char *name, u32 attr, int init DEBUG_LOG(HLE, "sceKernelCreateLwMutex(%08x, %s, %08x, %d, %08x)", workareaPtr, name, attr, initialCount, optionsPtr); if (optionsPtr != 0) - WARN_LOG_REPORT(HLE, "sceKernelCreateLwMutex(%s) unsupported options parameter: %08x", name, optionsPtr); + { + u32 size = Memory::Read_U32(optionsPtr); + if (size != 0) + WARN_LOG_REPORT(HLE, "sceKernelCreateLwMutex(%s) unsupported options parameter, size = %d", name, size); + } if ((attr & ~PSP_MUTEX_ATTR_KNOWN) != 0) WARN_LOG_REPORT(HLE, "sceKernelCreateLwMutex(%s) unsupported attr parameter: %08x", name, attr); From 47ffd3b14e5517837560eb8f020b28bede5254e1 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 16:34:35 -0700 Subject: [PATCH 4/8] Report less for semaphores options with no size. --- Core/HLE/sceKernelSemaphore.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceKernelSemaphore.cpp b/Core/HLE/sceKernelSemaphore.cpp index 89a6c92c04..712fb4c5e5 100644 --- a/Core/HLE/sceKernelSemaphore.cpp +++ b/Core/HLE/sceKernelSemaphore.cpp @@ -293,7 +293,11 @@ int sceKernelCreateSema(const char* name, u32 attr, int initVal, int maxVal, u32 DEBUG_LOG(HLE, "%i=sceKernelCreateSema(%s, %08x, %i, %i, %08x)", id, s->ns.name, s->ns.attr, s->ns.initCount, s->ns.maxCount, optionPtr); if (optionPtr != 0) - WARN_LOG_REPORT(HLE, "sceKernelCreateSema(%s) unsupported options parameter: %08x", name, optionPtr); + { + u32 size = Memory::Read_U32(optionPtr); + if (size != 0) + WARN_LOG_REPORT(HLE, "sceKernelCreateSema(%s) unsupported options parameter, size = %d", name, size); + } if ((attr & ~PSP_SEMA_ATTR_PRIORITY) != 0) WARN_LOG_REPORT(HLE, "sceKernelCreateSema(%s) unsupported attr parameter: %08x", name, attr); From 0525906c709a658cbe0eb31e13828dd8899905f2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 17:36:53 -0700 Subject: [PATCH 5/8] Report unimplemented but known HLE functions. --- Core/HLE/HLE.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index 92c0cea3e2..ae997d17dc 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -470,7 +470,7 @@ void CallSyscall(u32 op) } else { - ERROR_LOG(HLE,"Unimplemented HLE function %s", moduleDB[modulenum].funcTable[funcnum].name); + ERROR_LOG_REPORT(HLE, "Unimplemented HLE function %s", moduleDB[modulenum].funcTable[funcnum].name); } if (g_Config.bShowDebugStats) { From 0fb5877d7526921683e2aa13e8924bd876698f21 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 20:16:01 -0700 Subject: [PATCH 6/8] Implement sceKernelExtendThreadStack(). Fixes #1833. Implemented using fake nids instead of callbacks since it's simpler to get them (and waits and recursion) right. --- Core/HLE/HLETables.cpp | 1 + Core/HLE/HLETables.h | 1 + Core/HLE/sceKernelThread.cpp | 190 +++++++++++++++++++++++++++-------- Core/HLE/sceKernelThread.h | 1 + 4 files changed, 151 insertions(+), 42 deletions(-) diff --git a/Core/HLE/HLETables.cpp b/Core/HLE/HLETables.cpp index dd8fe77c69..7c1628abdc 100644 --- a/Core/HLE/HLETables.cpp +++ b/Core/HLE/HLETables.cpp @@ -79,6 +79,7 @@ const HLEFunction FakeSysCalls[] = {NID_THREADRETURN, __KernelReturnFromThread, "__KernelReturnFromThread"}, {NID_CALLBACKRETURN, __KernelReturnFromMipsCall, "__KernelReturnFromMipsCall"}, {NID_INTERRUPTRETURN, __KernelReturnFromInterrupt, "__KernelReturnFromInterrupt"}, + {NID_EXTENDRETURN, __KernelReturnFromExtendStack, "__KernelReturnFromExtendStack"}, {NID_IDLE, __KernelIdle, "_sceKernelIdle"}, }; diff --git a/Core/HLE/HLETables.h b/Core/HLE/HLETables.h index 5f73715f10..956f2383f6 100644 --- a/Core/HLE/HLETables.h +++ b/Core/HLE/HLETables.h @@ -22,6 +22,7 @@ #define NID_THREADRETURN 0xc0debabe #define NID_CALLBACKRETURN 0xbadc0fee #define NID_INTERRUPTRETURN 0xbadd00d5 +#define NID_EXTENDRETURN 0xbad0b0c9 #define NID_IDLE 0x1d7e1d7e void RegisterAllModules(); \ No newline at end of file diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 21b47cfe07..103fef1879 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -306,29 +306,29 @@ public: if (nt.attr & PSP_THREAD_ATTR_KERNEL) { // Allocate stacks for kernel threads (idle) in kernel RAM - stackBlock = kernelMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); + currentStack.start = kernelMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); } else { - stackBlock = userMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); + currentStack.start = userMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); } - if (stackBlock == (u32)-1) + if (currentStack.start == (u32)-1) { - stackBlock = 0; + currentStack.start = 0; ERROR_LOG(HLE, "Failed to allocate stack for thread"); return false; } - nt.initialStack = stackBlock; + nt.initialStack = currentStack.start; nt.stackSize = stackSize; return true; } bool FillStack() { // Fill the stack. - Memory::Memset(stackBlock, 0xFF, nt.stackSize); - context.r[MIPS_REG_SP] = stackBlock + nt.stackSize; - stackEnd = context.r[MIPS_REG_SP]; + Memory::Memset(currentStack.start, 0xFF, nt.stackSize); + context.r[MIPS_REG_SP] = currentStack.start + nt.stackSize; + currentStack.end = context.r[MIPS_REG_SP]; // The k0 section is 256 bytes at the top of the stack. context.r[MIPS_REG_SP] -= 256; context.r[MIPS_REG_K0] = context.r[MIPS_REG_SP]; @@ -345,19 +345,51 @@ public: } void FreeStack() { - if (stackBlock != 0) { + if (currentStack.start != 0) { DEBUG_LOG(HLE, "Freeing thread stack %s", nt.name); if (nt.attr & PSP_THREAD_ATTR_KERNEL) { - kernelMemory.Free(stackBlock); + kernelMemory.Free(currentStack.start); } else { - userMemory.Free(stackBlock); + userMemory.Free(currentStack.start); } - stackBlock = 0; + currentStack.start = 0; } } - Thread() : stackBlock(0) + bool PushExtendedStack(u32 size) { + u32 stack = userMemory.Alloc(size, true, (std::string("extended/") + nt.name).c_str()); + if (stack == (u32)-1) + return false; + + pushedStacks.push_back(currentStack); + currentStack.start = stack; + currentStack.end = stack + size; + nt.initialStack = currentStack.start; + nt.stackSize = currentStack.end - currentStack.start; + + // We still drop the threadID at the bottom and fill it, but there's no k0. + Memory::Memset(currentStack.start, 0xFF, nt.stackSize); + Memory::Write_U32(GetUID(), nt.initialStack); + return true; + } + + bool PopExtendedStack() + { + if (pushedStacks.size() == 0) + return false; + + userMemory.Free(currentStack.start); + currentStack = pushedStacks.back(); + pushedStacks.pop_back(); + nt.initialStack = currentStack.start; + nt.stackSize = currentStack.end - currentStack.start; + return true; + } + + Thread() + { + currentStack.start = 0; } ~Thread() @@ -406,7 +438,8 @@ public: } p.Do(pendingMipsCalls); - p.Do(stackBlock); + p.Do(pushedStacks); + p.Do(currentStack); p.DoMarker("Thread"); } @@ -427,8 +460,15 @@ public: std::list pendingMipsCalls; - u32 stackBlock; - u32 stackEnd; + struct StackInfo { + u32 start; + u32 end; + }; + // This is a stack of... stacks, since sceKernelExtendThreadStack() can recurse. + // These are stacks that aren't "active" right now, but will pop off once the func returns. + std::vector pushedStacks; + + StackInfo currentStack; }; struct ThreadQueueList @@ -700,6 +740,7 @@ u32 idleThreadHackAddr; u32 threadReturnHackAddr; u32 cbReturnHackAddr; u32 intReturnHackAddr; +u32 extendReturnHackAddr; std::vector threadEndListeners; // Lists all thread ids that aren't deleted/etc. @@ -801,9 +842,25 @@ u32 __KernelInterruptReturnAddress() void hleScheduledWakeup(u64 userdata, int cyclesLate); void hleThreadEndTimeout(u64 userdata, int cyclesLate); +void __KernelWriteFakeSysCall(u32 nid, u32 &ptr, u32 &pos) +{ + ptr = pos; + pos += 8; + WriteSyscall("FakeSysCalls", nid, ptr); +} + void __KernelThreadingInit() { - u32 blockSize = 4 * 4 + 4 * 2 * 3; // One 16-byte thread plus 3 8-byte "hacks" + // Yeah, this is straight out of JPCSP, I should be ashamed. + const static u32 idleThreadCode[] = { + MIPS_MAKE_ADDIU(MIPS_REG_A0, MIPS_REG_ZERO, 0), + MIPS_MAKE_LUI(MIPS_REG_RA, 0x0800), + MIPS_MAKE_JR_RA(), + //MIPS_MAKE_SYSCALL("ThreadManForUser", "sceKernelDelayThread"), + MIPS_MAKE_SYSCALL("FakeSysCalls", "_sceKernelIdle"), + MIPS_MAKE_BREAK(), + }; + u32 blockSize = sizeof(idleThreadCode) + 4 * 2 * 4; // The thread code above plus 4 8-byte "hacks" dispatchEnabled = true; memset(waitTypeFuncs, 0, sizeof(waitTypeFuncs)); @@ -813,25 +870,15 @@ void __KernelThreadingInit() currentCallbackThreadID = 0; readyCallbacksCount = 0; idleThreadHackAddr = kernelMemory.Alloc(blockSize, false, "threadrethack"); - // Make sure it got allocated where we expect it... at the very start of kernel RAM - //CHECK_EQ(idleThreadHackAddr & 0x3FFFFFFF, 0x08000000); - // Yeah, this is straight out of JPCSP, I should be ashamed. - Memory::Write_U32(MIPS_MAKE_ADDIU(MIPS_REG_A0, MIPS_REG_ZERO, 0), idleThreadHackAddr); - Memory::Write_U32(MIPS_MAKE_LUI(MIPS_REG_RA, 0x0800), idleThreadHackAddr + 4); - Memory::Write_U32(MIPS_MAKE_JR_RA(), idleThreadHackAddr + 8); - //Memory::Write_U32(MIPS_MAKE_SYSCALL("ThreadManForUser", "sceKernelDelayThread"), idleThreadHackAddr + 12); - Memory::Write_U32(MIPS_MAKE_SYSCALL("FakeSysCalls", "_sceKernelIdle"), idleThreadHackAddr + 12); - Memory::Write_U32(MIPS_MAKE_BREAK(), idleThreadHackAddr + 16); + Memory::Memcpy(idleThreadHackAddr, idleThreadCode, sizeof(idleThreadCode)); - threadReturnHackAddr = idleThreadHackAddr + 20; - WriteSyscall("FakeSysCalls", NID_THREADRETURN, threadReturnHackAddr); - - cbReturnHackAddr = threadReturnHackAddr + 8; - WriteSyscall("FakeSysCalls", NID_CALLBACKRETURN, cbReturnHackAddr); - - intReturnHackAddr = cbReturnHackAddr + 8; - WriteSyscall("FakeSysCalls", NID_INTERRUPTRETURN, intReturnHackAddr); + u32 pos = idleThreadHackAddr + sizeof(idleThreadCode); + // IF you add another func here, add it to the allocation above, and also to DoState below. + __KernelWriteFakeSysCall(NID_THREADRETURN, threadReturnHackAddr, pos); + __KernelWriteFakeSysCall(NID_CALLBACKRETURN, cbReturnHackAddr, pos); + __KernelWriteFakeSysCall(NID_INTERRUPTRETURN, intReturnHackAddr, pos); + __KernelWriteFakeSysCall(NID_EXTENDRETURN, extendReturnHackAddr, pos); eventScheduledWakeup = CoreTiming::RegisterEvent("ScheduledWakeup", &hleScheduledWakeup); eventThreadEndTimeout = CoreTiming::RegisterEvent("ThreadEndTimeout", &hleThreadEndTimeout); @@ -857,6 +904,7 @@ void __KernelThreadingDoState(PointerWrap &p) p.Do(threadReturnHackAddr); p.Do(cbReturnHackAddr); p.Do(intReturnHackAddr); + p.Do(extendReturnHackAddr); p.Do(currentThread); SceUID dv = 0; @@ -1611,7 +1659,7 @@ int sceKernelCheckThreadStack() u32 error; Thread *t = kernelObjects.Get(__KernelGetCurThread(), error); if (t) { - u32 diff = labs((long)((s64)t->stackEnd - (s64)currentMIPS->r[MIPS_REG_SP])); + u32 diff = labs((long)((s64)t->currentStack.end - (s64)currentMIPS->r[MIPS_REG_SP])); WARN_LOG(HLE, "%i=sceKernelCheckThreadStack()", diff); return diff; } else { @@ -1726,7 +1774,7 @@ void __KernelSetupRootThread(SceUID moduleID, int args, const char *argp, int pr //grab mips regs SceUID id; Thread *thread = __KernelCreateThread(id, moduleID, "root", currentMIPS->pc, prio, stacksize, attr); - if (thread->stackBlock == 0) + if (thread->currentStack.start == 0) ERROR_LOG_REPORT(HLE, "Unable to allocate stack for root thread."); __KernelResetThread(thread); @@ -1781,7 +1829,7 @@ int __KernelCreateThread(const char *threadName, SceUID moduleID, u32 entry, u32 SceUID id; Thread *newThread = __KernelCreateThread(id, moduleID, threadName, entry, prio, stacksize, attr); - if (newThread->stackBlock == 0) + if (newThread->currentStack.start == 0) { ERROR_LOG_REPORT(HLE, "sceKernelCreateThread(name=%s): out of memory, %08x stack requested", threadName, stacksize); return SCE_KERNEL_ERROR_NO_MEMORY; @@ -1900,7 +1948,7 @@ void sceKernelGetThreadStackFreeSize() // Scan the stack for 0xFF int sz = 0; - for (u32 addr = thread->stackBlock; addr < thread->stackBlock + thread->nt.stackSize; addr++) + for (u32 addr = thread->currentStack.start; addr < thread->currentStack.start + thread->nt.stackSize; addr++) { if (Memory::Read_U8(addr) != 0xFF) break; @@ -2140,7 +2188,7 @@ u32 __KernelGetCurThreadStack() { Thread *t = __GetCurrentThread(); if (t) - return t->stackEnd; + return t->currentStack.end; return 0; } @@ -2557,12 +2605,70 @@ int sceKernelReferCallbackStatus(SceUID cbId, u32 statusAddr) u32 sceKernelExtendThreadStack(u32 size, u32 entryAddr, u32 entryParameter) { - ERROR_LOG_REPORT(HLE,"sceKernelExtendThreadStack(%08x, %08x, %08x) - Not fully supported", size, entryAddr, entryParameter); - u32 args[1] = { entryParameter }; - __KernelDirectMipsCall(entryAddr, 0, args, 1, false); + if (size < 512) + { + ERROR_LOG_REPORT(HLE, "sceKernelExtendThreadStack(%08x, %08x, %08x) - stack size too small", size, entryAddr, entryParameter); + return SCE_KERNEL_ERROR_ILLEGAL_STACK_SIZE; + } + + Thread *thread = __GetCurrentThread(); + if (!thread) + { + ERROR_LOG_REPORT(HLE, "sceKernelExtendThreadStack(%08x, %08x, %08x) - not on a thread?", size, entryAddr, entryParameter); + return -1; + } + + if (!thread->PushExtendedStack(size)) + { + ERROR_LOG_REPORT(HLE, "sceKernelExtendThreadStack(%08x, %08x, %08x) - could not allocate new stack", size, entryAddr, entryParameter); + return SCE_KERNEL_ERROR_NO_MEMORY; + } + + // The stack has been changed now, so it's do or die time. + DEBUG_LOG(HLE, "sceKernelExtendThreadStack(%08x, %08x, %08x)", size, entryAddr, entryParameter); + + // Push the old SP, RA, and PC onto the stack (so we can restore them later.) + Memory::Write_U32(currentMIPS->r[MIPS_REG_RA], thread->currentStack.end - 4); + Memory::Write_U32(currentMIPS->r[MIPS_REG_SP], thread->currentStack.end - 8); + Memory::Write_U32(currentMIPS->pc, thread->currentStack.end - 12); + + currentMIPS->pc = entryAddr; + currentMIPS->r[MIPS_REG_A0] = entryParameter; + currentMIPS->r[MIPS_REG_RA] = extendReturnHackAddr; + // Stack should stay aligned even though we saved only 3 regs. + currentMIPS->r[MIPS_REG_SP] = thread->currentStack.end - 0x10; + return 0; } +void __KernelReturnFromExtendStack() +{ + Thread *thread = __GetCurrentThread(); + if (!thread) + { + ERROR_LOG_REPORT(HLE, "__KernelReturnFromExtendStack() - not on a thread?"); + return; + } + + // Grab the saved regs at the top of the stack. + u32 restoreRA = Memory::Read_U32(thread->currentStack.end - 4); + u32 restoreSP = Memory::Read_U32(thread->currentStack.end - 8); + u32 restorePC = Memory::Read_U32(thread->currentStack.end - 12); + + if (!thread->PopExtendedStack()) + { + ERROR_LOG_REPORT(HLE, "__KernelReturnFromExtendStack() - no stack to restore?"); + return; + } + + DEBUG_LOG(HLE, "__KernelReturnFromExtendStack()"); + currentMIPS->r[MIPS_REG_RA] = restoreRA; + currentMIPS->r[MIPS_REG_SP] = restoreSP; + currentMIPS->pc = restorePC; + + // We retain whatever is in v0/v1, it gets passed on to the caller of sceKernelExtendThreadStack(). +} + void ActionAfterMipsCall::run(MipsCall &call) { u32 error; Thread *thread = kernelObjects.Get(threadID, error); diff --git a/Core/HLE/sceKernelThread.h b/Core/HLE/sceKernelThread.h index 18b72d6675..2101fcba42 100644 --- a/Core/HLE/sceKernelThread.h +++ b/Core/HLE/sceKernelThread.h @@ -190,6 +190,7 @@ void __KernelReturnFromThread(); // Called as HLE function u32 __KernelGetThreadPrio(SceUID id); bool __KernelThreadSortPriority(SceUID thread1, SceUID thread2); bool __KernelIsDispatchEnabled(); +void __KernelReturnFromExtendStack(); void __KernelIdle(); From ead70f56d9584184a2ff528e02fa9a4bac13b419 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 20:20:41 -0700 Subject: [PATCH 7/8] Add a safety check to prevent leaks. --- Core/HLE/sceKernelThread.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 103fef1879..e1536d81ca 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -394,6 +394,12 @@ public: ~Thread() { + if (pushedStacks.size() != 0) + { + WARN_LOG_REPORT(HLE, "Thread ended within an extended stack"); + for (size_t i = 0; i < pushedStacks.size(); ++i) + userMemory.Free(pushedStacks[i].start); + } FreeStack(); } From eaeeb86f1bade4db03cf5c99064daf1120322362 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 18 May 2013 17:35:36 -0700 Subject: [PATCH 8/8] Update tests. --- pspautotests | 2 +- test.py | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pspautotests b/pspautotests index 8063a43c71..35e00795c6 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 8063a43c71d8c937705e7d88959521b16724fde3 +Subproject commit 35e00795c6c0150d4a70f9cab6a4254b820fed03 diff --git a/test.py b/test.py index 3bdc3f736b..1a4560b0dd 100755 --- a/test.py +++ b/test.py @@ -128,14 +128,15 @@ tests_good = [ "threads/mutex/try", "threads/mutex/unlock", "threads/semaphores/semaphores", - "threads/semaphores/cancel/cancel", - "threads/semaphores/create/create", - "threads/semaphores/delete/delete", - "threads/semaphores/poll/poll", - "threads/semaphores/priority/priority", - "threads/semaphores/refer/refer", - "threads/semaphores/signal/signal", - "threads/semaphores/wait/wait", + "threads/semaphores/cancel", + "threads/semaphores/create", + "threads/semaphores/delete", + "threads/semaphores/poll", + "threads/semaphores/priority", + "threads/semaphores/refer", + "threads/semaphores/signal", + "threads/semaphores/wait", + "threads/threads/extend", "threads/threads/release", "threads/threads/rotate", "threads/threads/threadend", @@ -162,6 +163,7 @@ tests_next = [ "audio/sascore/sascore", "threads/fpl/fpl", "threads/msgpipe/msgpipe", + "threads/mutex/cancel", "threads/scheduling/dispatch", "threads/scheduling/scheduling", "threads/threads/create",