diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index c9fc08f45b..7853dba24d 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -103,6 +103,7 @@ void __KernelInit() __KernelMbxInit(); __KernelMutexInit(); __KernelSemaInit(); + __KernelMsgPipeInit(); __IoInit(); __JpegInit(); __AudioInit(); @@ -190,6 +191,7 @@ void __KernelDoState(PointerWrap &p) __KernelEventFlagDoState(p); __KernelMbxDoState(p); __KernelModuleDoState(p); + __KernelMsgPipeDoState(p); __KernelMutexDoState(p); __KernelSemaDoState(p); __KernelTimeDoState(p); diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 852855be8a..7bce80373d 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -1070,7 +1070,7 @@ void __KernelVplTimeout(u64 userdata, int cyclesLate) if (vpl) { // This thread isn't waiting anymore, but we'll remove it from waitingThreads later. - // The reason is, if it times out, but what it was waiting on is DELETED prior to it + // The reason is, if it times out, but while it was waiting on is DELETED prior to it // actually running, it will get a DELETE result instead of a TIMEOUT. // So, we need to remember it or we won't be able to mark it DELETE instead later. __KernelResumeThreadFromWait(threadID, SCE_KERNEL_ERROR_WAIT_TIMEOUT); diff --git a/Core/HLE/sceKernelMsgPipe.cpp b/Core/HLE/sceKernelMsgPipe.cpp index bc48d135e1..9fc8e51a79 100644 --- a/Core/HLE/sceKernelMsgPipe.cpp +++ b/Core/HLE/sceKernelMsgPipe.cpp @@ -34,6 +34,9 @@ #define SCE_KERNEL_MPW_FULL 0 #define SCE_KERNEL_MPW_ASAP 1 +// State: the timer for MsgPipe timeouts. +static int waitTimer = -1; + struct NativeMsgPipe { SceSize_le size; @@ -60,6 +63,14 @@ struct MsgPipeWaitingThread int actualWaitID = __KernelGetWaitID(id, WAITTYPE_MSGPIPE, error); if (actualWaitID == waitID) { + u32 timeoutPtr = __KernelGetWaitTimeoutPtr(id, error); + if (timeoutPtr != 0 && waitTimer != -1) + { + // Remove any event for this thread. + s64 cyclesLeft = CoreTiming::UnscheduleEvent(waitTimer, id); + Memory::Write_U32((u32) cyclesToUs(cyclesLeft), timeoutPtr); + } + *transferredBytes = 0; __KernelResumeThreadFromWait(id, result); } @@ -195,6 +206,59 @@ KernelObject *__KernelMsgPipeObject() return new MsgPipe; } +void __KernelMsgPipeTimeout(u64 userdata, int cyclesLate) +{ + SceUID threadID = (SceUID) (userdata & 0xFFFFFFFF); + + u32 error; + u32 timeoutPtr = __KernelGetWaitTimeoutPtr(threadID, error); + if (timeoutPtr != 0) + Memory::Write_U32(0, timeoutPtr); + + SceUID uid = __KernelGetWaitID(threadID, WAITTYPE_MSGPIPE, error); + MsgPipe *m = kernelObjects.Get(uid, error); + if (m) + { + // This thread isn't waiting anymore, but we'll remove it from waitingThreads later. + // The reason is, if it times out, but whhile it was waiting on is DELETED prior to it + // actually running, it will get a DELETE result instead of a TIMEOUT. + // So, we need to remember it or we won't be able to mark it DELETE instead later. + __KernelResumeThreadFromWait(threadID, SCE_KERNEL_ERROR_WAIT_TIMEOUT); + } +} + +void __KernelSetMsgPipeTimeout(u32 timeoutPtr) +{ + if (timeoutPtr == 0 || waitTimer == -1) + return; + + int micro = (int) Memory::Read_U32(timeoutPtr); + + // TODO: Correct. + // This happens to be how the hardware seems to time things. + if (micro <= 5) + micro = 10; + // Yes, this 7 is reproducible. 6 is (a lot) longer than 7. + else if (micro == 7) + micro = 15; + else if (micro <= 215) + micro = 250; + + CoreTiming::ScheduleEvent(usToCycles(micro), waitTimer, __KernelGetCurThread()); +} + +void __KernelMsgPipeInit() +{ + waitTimer = CoreTiming::RegisterEvent("MsgPipeTimeout", __KernelMsgPipeTimeout); +} + +void __KernelMsgPipeDoState(PointerWrap &p) +{ + p.Do(waitTimer); + CoreTiming::RestoreRegisterEvent(waitTimer, "MsgPipeTimeout", __KernelMsgPipeTimeout); + p.DoMarker("sceKernelMsgPipe"); +} + int sceKernelCreateMsgPipe(const char *name, int partition, u32 attr, u32 size, u32 optionsPtr) { if (!name) @@ -327,7 +391,8 @@ void __KernelSendMsgPipe(MsgPipe *m, u32 sendBufAddr, u32 sendSize, int waitMode { m->AddSendWaitingThread(__KernelGetCurThread(), curSendAddr, sendSize, waitMode, resultAddr); RETURN(0); - __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, 0, cbEnabled, "msgpipe waited"); + __KernelSetMsgPipeTimeout(timeoutPtr); + __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, timeoutPtr, cbEnabled, "msgpipe waited"); return; } } @@ -359,7 +424,8 @@ void __KernelSendMsgPipe(MsgPipe *m, u32 sendBufAddr, u32 sendSize, int waitMode { m->AddSendWaitingThread(__KernelGetCurThread(), curSendAddr, sendSize, waitMode, resultAddr); RETURN(0); - __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, 0, cbEnabled, "msgpipe waited"); + __KernelSetMsgPipeTimeout(timeoutPtr); + __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, timeoutPtr, cbEnabled, "msgpipe waited"); return; } } @@ -499,7 +565,8 @@ void __KernelReceiveMsgPipe(MsgPipe *m, u32 receiveBufAddr, u32 receiveSize, int { m->AddReceiveWaitingThread(__KernelGetCurThread(), curReceiveAddr, receiveSize, waitMode, resultAddr); RETURN(0); - __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, 0, cbEnabled, "msgpipe waited"); + __KernelSetMsgPipeTimeout(timeoutPtr); + __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, timeoutPtr, cbEnabled, "msgpipe waited"); return; } } @@ -535,7 +602,8 @@ void __KernelReceiveMsgPipe(MsgPipe *m, u32 receiveBufAddr, u32 receiveSize, int { m->AddReceiveWaitingThread(__KernelGetCurThread(), curReceiveAddr, receiveSize, waitMode, resultAddr); RETURN(0); - __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, 0, cbEnabled, "msgpipe waited"); + __KernelSetMsgPipeTimeout(timeoutPtr); + __KernelWaitCurThread(WAITTYPE_MSGPIPE, m->GetUID(), 0, timeoutPtr, cbEnabled, "msgpipe waited"); return; } } diff --git a/Core/HLE/sceKernelMsgPipe.h b/Core/HLE/sceKernelMsgPipe.h index fb873fa3c1..8c6a33d271 100644 --- a/Core/HLE/sceKernelMsgPipe.h +++ b/Core/HLE/sceKernelMsgPipe.h @@ -17,6 +17,8 @@ #pragma once +class PointerWrap; + int sceKernelCreateMsgPipe(const char *name, int partition, u32 attr, u32 size, u32 optionsPtr); int sceKernelDeleteMsgPipe(SceUID uid); void sceKernelSendMsgPipe(); @@ -28,4 +30,6 @@ void sceKernelTryReceiveMsgPipe(); int sceKernelCancelMsgPipe(SceUID uid, u32 numSendThreadsAddr, u32 numReceiveThreadsAddr); void sceKernelReferMsgPipeStatus(); +void __KernelMsgPipeInit(); +void __KernelMsgPipeDoState(PointerWrap &p); KernelObject *__KernelMsgPipeObject();