2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2013-09-02 22:11:49 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2013-09-03 00:59:47 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
#include "Core/HLE/sceKernel.h"
|
|
|
|
#include "Core/HLE/sceKernelThread.h"
|
|
|
|
#include "Core/HLE/sceKernelMbx.h"
|
|
|
|
#include "Core/HLE/HLE.h"
|
2013-02-04 04:31:46 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
2015-04-06 01:09:35 +00:00
|
|
|
#include "Core/MemMapHelpers.h"
|
2013-03-26 07:54:00 +00:00
|
|
|
#include "Core/Reporting.h"
|
2013-09-03 00:59:47 +00:00
|
|
|
#include "Core/HLE/KernelWaitHelpers.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
#define SCE_KERNEL_MBA_THPRI 0x100
|
|
|
|
#define SCE_KERNEL_MBA_MSPRI 0x400
|
2012-12-16 02:57:15 +00:00
|
|
|
#define SCE_KERNEL_MBA_ATTR_KNOWN (SCE_KERNEL_MBA_THPRI | SCE_KERNEL_MBA_MSPRI)
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
const int PSP_MBX_ERROR_DUPLICATE_MSG = 0x800201C9;
|
|
|
|
|
2013-02-04 09:31:02 +00:00
|
|
|
struct MbxWaitingThread
|
|
|
|
{
|
2013-09-02 22:11:49 +00:00
|
|
|
SceUID threadID;
|
|
|
|
u32 packetAddr;
|
|
|
|
u64 pausedTimeout;
|
2013-09-09 04:41:54 +00:00
|
|
|
|
|
|
|
bool operator ==(const SceUID &otherThreadID) const
|
|
|
|
{
|
|
|
|
return threadID == otherThreadID;
|
|
|
|
}
|
2013-02-04 09:31:02 +00:00
|
|
|
};
|
2012-12-16 02:57:15 +00:00
|
|
|
void __KernelMbxTimeout(u64 userdata, int cyclesLate);
|
|
|
|
|
2013-01-18 06:04:52 +00:00
|
|
|
static int mbxWaitTimer = -1;
|
2012-12-16 02:06:55 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
struct NativeMbx
|
|
|
|
{
|
2013-07-25 06:58:45 +00:00
|
|
|
SceSize_le size;
|
2012-12-17 00:32:18 +00:00
|
|
|
char name[KERNELOBJECT_MAX_NAME_LENGTH + 1];
|
2013-07-25 06:58:45 +00:00
|
|
|
SceUInt_le attr;
|
|
|
|
s32_le numWaitThreads;
|
|
|
|
s32_le numMessages;
|
|
|
|
u32_le packetListHead;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2012-12-06 18:03:12 +00:00
|
|
|
struct Mbx : public KernelObject
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-12-08 20:14:35 +00:00
|
|
|
const char *GetName() override { return nmb.name; }
|
|
|
|
const char *GetTypeName() override { return "Mbx"; }
|
2012-11-01 15:19:01 +00:00
|
|
|
static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_MBXID; }
|
2013-06-19 06:54:29 +00:00
|
|
|
static int GetStaticIDType() { return SCE_KERNEL_TMID_Mbox; }
|
2014-12-08 20:14:35 +00:00
|
|
|
int GetIDType() const override { return SCE_KERNEL_TMID_Mbox; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
void AddWaitingThread(SceUID id, u32 addr)
|
|
|
|
{
|
2012-12-17 02:40:47 +00:00
|
|
|
bool inserted = false;
|
2012-11-08 13:24:51 +00:00
|
|
|
if (nmb.attr & SCE_KERNEL_MBA_THPRI)
|
|
|
|
{
|
2014-12-25 03:20:21 +00:00
|
|
|
for (auto it = waitingThreads.begin(); it != waitingThreads.end(); ++it)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-02 22:11:49 +00:00
|
|
|
if (__KernelGetThreadPrio(id) < __KernelGetThreadPrio(it->threadID))
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-02-04 09:31:02 +00:00
|
|
|
MbxWaitingThread waiting = {id, addr};
|
|
|
|
waitingThreads.insert(it, waiting);
|
2012-12-17 02:40:47 +00:00
|
|
|
inserted = true;
|
2012-11-08 13:24:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
if (!inserted)
|
2013-02-04 09:31:02 +00:00
|
|
|
{
|
|
|
|
MbxWaitingThread waiting = {id, addr};
|
|
|
|
waitingThreads.push_back(waiting);
|
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
inline void AddInitialMessage(u32 ptr)
|
2012-12-17 02:40:47 +00:00
|
|
|
{
|
|
|
|
nmb.numMessages++;
|
2012-12-17 05:32:52 +00:00
|
|
|
Memory::Write_U32(ptr, ptr);
|
|
|
|
nmb.packetListHead = ptr;
|
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
inline void AddFirstMessage(u32 endPtr, u32 ptr)
|
|
|
|
{
|
|
|
|
nmb.numMessages++;
|
|
|
|
Memory::Write_U32(nmb.packetListHead, ptr);
|
|
|
|
Memory::Write_U32(ptr, endPtr);
|
|
|
|
nmb.packetListHead = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void AddLastMessage(u32 endPtr, u32 ptr)
|
|
|
|
{
|
|
|
|
nmb.numMessages++;
|
|
|
|
Memory::Write_U32(ptr, endPtr);
|
|
|
|
Memory::Write_U32(nmb.packetListHead, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void AddMessage(u32 beforePtr, u32 afterPtr, u32 ptr)
|
|
|
|
{
|
|
|
|
nmb.numMessages++;
|
|
|
|
Memory::Write_U32(afterPtr, ptr);
|
|
|
|
Memory::Write_U32(ptr, beforePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ReceiveMessage(u32 receivePtr)
|
|
|
|
{
|
|
|
|
u32 ptr = nmb.packetListHead;
|
|
|
|
|
2014-01-05 16:10:36 +00:00
|
|
|
// Check over the linked list and reset the head.
|
|
|
|
int c = 0;
|
|
|
|
while (true)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2012-12-17 05:32:52 +00:00
|
|
|
u32 next = Memory::Read_U32(nmb.packetListHead);
|
2014-01-05 16:10:36 +00:00
|
|
|
if (!Memory::IsValidAddress(next))
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_ADDR;
|
|
|
|
if (next == ptr)
|
2012-12-17 05:32:52 +00:00
|
|
|
{
|
2014-01-05 16:10:36 +00:00
|
|
|
if (nmb.packetListHead != ptr)
|
2012-12-17 05:32:52 +00:00
|
|
|
{
|
2014-01-05 16:10:36 +00:00
|
|
|
next = Memory::Read_U32(next);
|
|
|
|
Memory::Write_U32(next, nmb.packetListHead);
|
|
|
|
nmb.packetListHead = next;
|
|
|
|
break;
|
2012-12-17 05:32:52 +00:00
|
|
|
}
|
2014-01-05 16:10:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c < nmb.numMessages - 1)
|
|
|
|
return PSP_MBX_ERROR_DUPLICATE_MSG;
|
2012-12-17 02:40:47 +00:00
|
|
|
|
2014-01-05 16:10:36 +00:00
|
|
|
nmb.packetListHead = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-17 05:32:52 +00:00
|
|
|
}
|
2014-01-05 16:10:36 +00:00
|
|
|
|
|
|
|
nmb.packetListHead = next;
|
|
|
|
c++;
|
2012-12-17 05:32:52 +00:00
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
|
|
|
|
// Tell the receiver about the message.
|
|
|
|
Memory::Write_U32(ptr, receivePtr);
|
2012-12-17 05:32:52 +00:00
|
|
|
nmb.numMessages--;
|
2012-12-17 02:40:47 +00:00
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
return 0;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 20:14:35 +00:00
|
|
|
void DoState(PointerWrap &p) override
|
2012-12-27 04:34:10 +00:00
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("Mbx", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2012-12-27 04:34:10 +00:00
|
|
|
p.Do(nmb);
|
2013-02-04 09:31:02 +00:00
|
|
|
MbxWaitingThread mwt = {0};
|
2012-12-28 22:33:00 +00:00
|
|
|
p.Do(waitingThreads, mwt);
|
2013-09-02 22:11:49 +00:00
|
|
|
p.Do(pausedWaits);
|
2012-12-27 04:34:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
NativeMbx nmb;
|
|
|
|
|
2012-12-16 02:06:55 +00:00
|
|
|
std::vector<MbxWaitingThread> waitingThreads;
|
2013-09-02 22:11:49 +00:00
|
|
|
// Key is the callback id it was for, or if no callback, the thread id.
|
|
|
|
std::map<SceUID, MbxWaitingThread> pausedWaits;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-09-02 22:11:49 +00:00
|
|
|
void __KernelMbxBeginCallback(SceUID threadID, SceUID prevCallbackId);
|
2013-09-02 23:29:00 +00:00
|
|
|
void __KernelMbxEndCallback(SceUID threadID, SceUID prevCallbackId);
|
2013-09-02 22:11:49 +00:00
|
|
|
|
2012-12-16 02:57:15 +00:00
|
|
|
void __KernelMbxInit()
|
|
|
|
{
|
2012-12-27 22:21:39 +00:00
|
|
|
mbxWaitTimer = CoreTiming::RegisterEvent("MbxTimeout", __KernelMbxTimeout);
|
2013-09-02 22:11:49 +00:00
|
|
|
__KernelRegisterWaitTypeFuncs(WAITTYPE_MBX, __KernelMbxBeginCallback, __KernelMbxEndCallback);
|
2012-12-27 22:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __KernelMbxDoState(PointerWrap &p)
|
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("sceKernelMbx", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2012-12-27 22:21:39 +00:00
|
|
|
p.Do(mbxWaitTimer);
|
|
|
|
CoreTiming::RestoreRegisterEvent(mbxWaitTimer, "MbxTimeout", __KernelMbxTimeout);
|
2012-12-16 02:57:15 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:45:19 +00:00
|
|
|
KernelObject *__KernelMbxObject()
|
|
|
|
{
|
|
|
|
return new Mbx;
|
|
|
|
}
|
|
|
|
|
2014-12-08 09:40:08 +00:00
|
|
|
static bool __KernelUnlockMbxForThread(Mbx *m, MbxWaitingThread &th, u32 &error, int result, bool &wokeThreads)
|
2012-12-16 17:21:22 +00:00
|
|
|
{
|
2013-09-07 17:51:11 +00:00
|
|
|
if (!HLEKernel::VerifyWait(th.threadID, WAITTYPE_MBX, m->GetUID()))
|
2012-12-16 17:21:22 +00:00
|
|
|
return true;
|
|
|
|
|
2013-09-07 17:51:11 +00:00
|
|
|
u32 timeoutPtr = __KernelGetWaitTimeoutPtr(th.threadID, error);
|
2013-01-18 06:04:52 +00:00
|
|
|
if (timeoutPtr != 0 && mbxWaitTimer != -1)
|
2012-12-16 17:21:22 +00:00
|
|
|
{
|
|
|
|
// Remove any event for this thread.
|
2013-09-02 22:11:49 +00:00
|
|
|
s64 cyclesLeft = CoreTiming::UnscheduleEvent(mbxWaitTimer, th.threadID);
|
2012-12-16 17:21:22 +00:00
|
|
|
Memory::Write_U32((u32) cyclesToUs(cyclesLeft), timeoutPtr);
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:11:49 +00:00
|
|
|
__KernelResumeThreadFromWait(th.threadID, result);
|
2012-12-16 17:21:22 +00:00
|
|
|
wokeThreads = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-08 09:40:08 +00:00
|
|
|
static bool __KernelUnlockMbxForThreadCheck(Mbx *m, MbxWaitingThread &waitData, u32 &error, int result, bool &wokeThreads)
|
2013-09-02 22:11:49 +00:00
|
|
|
{
|
2013-09-03 00:59:47 +00:00
|
|
|
if (m->nmb.numMessages > 0 && __KernelUnlockMbxForThread(m, waitData, error, 0, wokeThreads))
|
2013-09-02 22:11:49 +00:00
|
|
|
{
|
2013-09-03 00:59:47 +00:00
|
|
|
m->ReceiveMessage(waitData.packetAddr);
|
|
|
|
return true;
|
2013-09-02 22:11:49 +00:00
|
|
|
}
|
2013-09-03 00:59:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __KernelMbxBeginCallback(SceUID threadID, SceUID prevCallbackId)
|
|
|
|
{
|
2015-01-18 02:14:58 +00:00
|
|
|
auto result = HLEKernel::WaitBeginCallback<Mbx, WAITTYPE_MBX, MbxWaitingThread>(threadID, prevCallbackId, mbxWaitTimer);
|
2013-09-03 00:59:47 +00:00
|
|
|
if (result == HLEKernel::WAIT_CB_SUCCESS)
|
2014-06-30 02:03:35 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbxCB: Suspending mbx wait for callback");
|
2013-09-03 00:59:47 +00:00
|
|
|
else if (result == HLEKernel::WAIT_CB_BAD_WAIT_DATA)
|
2014-06-30 02:03:35 +00:00
|
|
|
ERROR_LOG_REPORT(SCEKERNEL, "sceKernelReceiveMbxCB: wait not found to pause for callback");
|
2013-09-02 22:11:49 +00:00
|
|
|
else
|
2013-09-07 20:02:55 +00:00
|
|
|
WARN_LOG_REPORT(SCEKERNEL, "sceKernelReceiveMbxCB: beginning callback with bad wait id?");
|
2013-09-02 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 23:29:00 +00:00
|
|
|
void __KernelMbxEndCallback(SceUID threadID, SceUID prevCallbackId)
|
2013-09-02 22:11:49 +00:00
|
|
|
{
|
2015-01-18 02:14:58 +00:00
|
|
|
auto result = HLEKernel::WaitEndCallback<Mbx, WAITTYPE_MBX, MbxWaitingThread>(threadID, prevCallbackId, mbxWaitTimer, __KernelUnlockMbxForThreadCheck);
|
2013-09-03 00:59:47 +00:00
|
|
|
if (result == HLEKernel::WAIT_CB_RESUMED_WAIT)
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbxCB: Resuming mbx wait from callback");
|
2013-09-02 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 02:57:15 +00:00
|
|
|
void __KernelMbxTimeout(u64 userdata, int cyclesLate)
|
|
|
|
{
|
|
|
|
SceUID threadID = (SceUID)userdata;
|
2013-09-04 05:27:28 +00:00
|
|
|
HLEKernel::WaitExecTimeout<Mbx, WAITTYPE_MBX>(threadID);
|
2012-12-16 02:57:15 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 09:40:08 +00:00
|
|
|
static void __KernelWaitMbx(Mbx *m, u32 timeoutPtr)
|
2012-12-16 02:57:15 +00:00
|
|
|
{
|
2013-01-18 06:04:52 +00:00
|
|
|
if (timeoutPtr == 0 || mbxWaitTimer == -1)
|
2012-12-16 02:57:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int micro = (int) Memory::Read_U32(timeoutPtr);
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
// This seems to match the actual timing.
|
|
|
|
if (micro <= 2)
|
2014-06-27 07:58:59 +00:00
|
|
|
micro = 20;
|
2012-12-17 02:40:47 +00:00
|
|
|
else if (micro <= 209)
|
2012-12-16 02:57:15 +00:00
|
|
|
micro = 250;
|
|
|
|
|
|
|
|
// This should call __KernelMbxTimeout() later, unless we cancel it.
|
|
|
|
CoreTiming::ScheduleEvent(usToCycles(micro), mbxWaitTimer, __KernelGetCurThread());
|
|
|
|
}
|
|
|
|
|
2014-12-08 09:40:08 +00:00
|
|
|
static std::vector<MbxWaitingThread>::iterator __KernelMbxFindPriority(std::vector<MbxWaitingThread> &waiting)
|
2012-12-17 02:40:47 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
_dbg_assert_msg_(SCEKERNEL, !waiting.empty(), "__KernelMutexFindPriority: Trying to find best of no threads.");
|
2012-12-17 02:40:47 +00:00
|
|
|
|
|
|
|
std::vector<MbxWaitingThread>::iterator iter, end, best = waiting.end();
|
|
|
|
u32 best_prio = 0xFFFFFFFF;
|
|
|
|
for (iter = waiting.begin(), end = waiting.end(); iter != end; ++iter)
|
|
|
|
{
|
2013-09-02 22:11:49 +00:00
|
|
|
u32 iter_prio = __KernelGetThreadPrio(iter->threadID);
|
2012-12-17 02:40:47 +00:00
|
|
|
if (iter_prio < best_prio)
|
|
|
|
{
|
|
|
|
best = iter;
|
|
|
|
best_prio = iter_prio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
_dbg_assert_msg_(SCEKERNEL, best != waiting.end(), "__KernelMutexFindPriority: Returning invalid best thread.");
|
2012-12-17 02:40:47 +00:00
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2012-12-16 02:06:55 +00:00
|
|
|
SceUID sceKernelCreateMbx(const char *name, u32 attr, u32 optAddr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-12-16 02:06:55 +00:00
|
|
|
if (!name)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
WARN_LOG_REPORT(SCEKERNEL, "%08x=sceKernelCreateMbx(): invalid name", SCE_KERNEL_ERROR_ERROR);
|
2012-12-16 02:06:55 +00:00
|
|
|
return SCE_KERNEL_ERROR_ERROR;
|
|
|
|
}
|
2012-12-16 02:57:15 +00:00
|
|
|
// Accepts 0x000 - 0x0FF, 0x100 - 0x1FF, and 0x400 - 0x4FF.
|
|
|
|
if (((attr & ~SCE_KERNEL_MBA_ATTR_KNOWN) & ~0xFF) != 0)
|
2012-12-16 02:06:55 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
WARN_LOG_REPORT(SCEKERNEL, "%08x=sceKernelCreateMbx(): invalid attr parameter: %08x", SCE_KERNEL_ERROR_ILLEGAL_ATTR, attr);
|
2012-12-16 02:06:55 +00:00
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_ATTR;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
Mbx *m = new Mbx();
|
|
|
|
SceUID id = kernelObjects.Create(m);
|
|
|
|
|
|
|
|
m->nmb.size = sizeof(NativeMbx);
|
2012-12-17 00:32:18 +00:00
|
|
|
strncpy(m->nmb.name, name, KERNELOBJECT_MAX_NAME_LENGTH);
|
|
|
|
m->nmb.name[KERNELOBJECT_MAX_NAME_LENGTH] = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
m->nmb.attr = attr;
|
|
|
|
m->nmb.numWaitThreads = 0;
|
|
|
|
m->nmb.numMessages = 0;
|
|
|
|
m->nmb.packetListHead = 0;
|
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "%i=sceKernelCreateMbx(%s, %08x, %08x)", id, name, attr, optAddr);
|
2012-12-16 02:06:55 +00:00
|
|
|
|
|
|
|
if (optAddr != 0)
|
2013-08-25 23:43:47 +00:00
|
|
|
{
|
|
|
|
u32 size = Memory::Read_U32(optAddr);
|
|
|
|
if (size > 4)
|
2013-09-07 20:02:55 +00:00
|
|
|
WARN_LOG_REPORT(SCEKERNEL, "sceKernelCreateMbx(%s) unsupported options parameter, size = %d", name, size);
|
2013-08-25 23:43:47 +00:00
|
|
|
}
|
2012-12-16 02:57:15 +00:00
|
|
|
if ((attr & ~SCE_KERNEL_MBA_ATTR_KNOWN) != 0)
|
2013-09-07 20:02:55 +00:00
|
|
|
WARN_LOG_REPORT(SCEKERNEL, "sceKernelCreateMbx(%s) unsupported attr parameter: %08x", name, attr);
|
2012-12-16 02:06:55 +00:00
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
return id;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
int sceKernelDeleteMbx(SceUID id)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 20:49:56 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
|
|
|
if (m)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelDeleteMbx(%i)", id);
|
2012-12-16 17:21:22 +00:00
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
bool wokeThreads = false;
|
2012-11-08 20:49:56 +00:00
|
|
|
for (size_t i = 0; i < m->waitingThreads.size(); i++)
|
2012-12-16 17:21:22 +00:00
|
|
|
__KernelUnlockMbxForThread(m, m->waitingThreads[i], error, SCE_KERNEL_ERROR_WAIT_DELETE, wokeThreads);
|
|
|
|
m->waitingThreads.clear();
|
2012-12-16 02:06:55 +00:00
|
|
|
|
2012-12-16 17:21:22 +00:00
|
|
|
if (wokeThreads)
|
2012-12-16 02:06:55 +00:00
|
|
|
hleReSchedule("mbx deleted");
|
2012-11-08 20:49:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelDeleteMbx(%i): invalid mbx id", id);
|
2012-11-08 20:49:56 +00:00
|
|
|
}
|
2012-11-08 13:24:51 +00:00
|
|
|
return kernelObjects.Destroy<Mbx>(id);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
int sceKernelSendMbx(SceUID id, u32 packetAddr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 13:24:51 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
2012-12-17 02:40:47 +00:00
|
|
|
if (!m)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelSendMbx(%i, %08x): invalid mbx id", id, packetAddr);
|
2012-12-17 02:40:47 +00:00
|
|
|
return error;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
NativeMbxPacket *addPacket = (NativeMbxPacket*)Memory::GetPointer(packetAddr);
|
|
|
|
if (addPacket == 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelSendMbx(%i, %08x): invalid packet address", id, packetAddr);
|
2012-12-17 02:40:47 +00:00
|
|
|
return -1;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
// If the queue is empty, maybe someone is waiting.
|
|
|
|
// We have to check them first, they might've timed out.
|
2012-12-17 05:32:52 +00:00
|
|
|
if (m->nmb.numMessages == 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2012-12-17 02:40:47 +00:00
|
|
|
bool wokeThreads = false;
|
|
|
|
std::vector<MbxWaitingThread>::iterator iter;
|
|
|
|
while (!wokeThreads && !m->waitingThreads.empty())
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2012-12-17 02:40:47 +00:00
|
|
|
if ((m->nmb.attr & SCE_KERNEL_MBA_THPRI) != 0)
|
|
|
|
iter = __KernelMbxFindPriority(m->waitingThreads);
|
|
|
|
else
|
|
|
|
iter = m->waitingThreads.begin();
|
|
|
|
|
|
|
|
MbxWaitingThread t = *iter;
|
|
|
|
__KernelUnlockMbxForThread(m, t, error, 0, wokeThreads);
|
|
|
|
m->waitingThreads.erase(iter);
|
|
|
|
|
|
|
|
if (wokeThreads)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelSendMbx(%i, %08x): threads waiting, resuming %d", id, packetAddr, t.threadID);
|
2013-09-02 22:11:49 +00:00
|
|
|
Memory::Write_U32(packetAddr, t.packetAddr);
|
2012-12-17 02:40:47 +00:00
|
|
|
hleReSchedule("mbx sent");
|
|
|
|
|
|
|
|
// We don't need to do anything else, finish here.
|
|
|
|
return 0;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelSendMbx(%i, %08x): no threads currently waiting, adding message to queue", id, packetAddr);
|
2012-12-17 02:40:47 +00:00
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
if (m->nmb.numMessages == 0)
|
|
|
|
m->AddInitialMessage(packetAddr);
|
|
|
|
else
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-07-08 01:20:17 +00:00
|
|
|
u32 next = m->nmb.packetListHead, prev = 0;
|
2012-12-17 05:32:52 +00:00
|
|
|
for (int i = 0, n = m->nmb.numMessages; i < n; i++)
|
2012-12-17 02:40:47 +00:00
|
|
|
{
|
2012-12-17 05:32:52 +00:00
|
|
|
if (next == packetAddr)
|
|
|
|
return PSP_MBX_ERROR_DUPLICATE_MSG;
|
|
|
|
if (!Memory::IsValidAddress(next))
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_ADDR;
|
|
|
|
|
|
|
|
prev = next;
|
|
|
|
next = Memory::Read_U32(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inserted = false;
|
|
|
|
if (m->nmb.attr & SCE_KERNEL_MBA_MSPRI)
|
|
|
|
{
|
|
|
|
NativeMbxPacket p;
|
|
|
|
for (int i = 0, n = m->nmb.numMessages; i < n; i++)
|
2012-12-17 02:40:47 +00:00
|
|
|
{
|
2015-09-21 18:16:03 +00:00
|
|
|
Memory::ReadStructUnchecked<NativeMbxPacket>(next, &p);
|
2012-12-17 05:32:52 +00:00
|
|
|
if (addPacket->priority < p.priority)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
m->AddFirstMessage(prev, packetAddr);
|
|
|
|
else
|
|
|
|
m->AddMessage(prev, next, packetAddr);
|
|
|
|
inserted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = next;
|
|
|
|
next = Memory::Read_U32(next);
|
2012-12-17 02:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-17 05:32:52 +00:00
|
|
|
if (!inserted)
|
|
|
|
m->AddLastMessage(prev, packetAddr);
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
2012-12-17 02:40:47 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
int sceKernelReceiveMbx(SceUID id, u32 packetAddrPtr, u32 timeoutPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 13:24:51 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelReceiveMbx(%i, %08x, %08x): invalid mbx id", id, packetAddrPtr, timeoutPtr);
|
2012-12-17 02:40:47 +00:00
|
|
|
return error;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
if (m->nmb.numMessages > 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbx(%i, %08x, %08x): sending first queue message", id, packetAddrPtr, timeoutPtr);
|
2012-12-17 05:32:52 +00:00
|
|
|
return m->ReceiveMessage(packetAddrPtr);
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbx(%i, %08x, %08x): no message in queue, waiting", id, packetAddrPtr, timeoutPtr);
|
2013-09-09 04:41:54 +00:00
|
|
|
HLEKernel::RemoveWaitingThread(m->waitingThreads, __KernelGetCurThread());
|
2012-11-08 13:24:51 +00:00
|
|
|
m->AddWaitingThread(__KernelGetCurThread(), packetAddrPtr);
|
2012-12-16 02:57:15 +00:00
|
|
|
__KernelWaitMbx(m, timeoutPtr);
|
2013-01-26 18:44:04 +00:00
|
|
|
__KernelWaitCurThread(WAITTYPE_MBX, id, 0, timeoutPtr, false, "mbx waited");
|
2012-12-17 02:40:47 +00:00
|
|
|
return 0;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
int sceKernelReceiveMbxCB(SceUID id, u32 packetAddrPtr, u32 timeoutPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 20:49:56 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
if (!m)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelReceiveMbxCB(%i, %08x, %08x): invalid mbx id", id, packetAddrPtr, timeoutPtr);
|
2012-12-17 02:40:47 +00:00
|
|
|
return error;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
if (m->nmb.numMessages > 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbxCB(%i, %08x, %08x): sending first queue message", id, packetAddrPtr, timeoutPtr);
|
2012-12-16 17:21:22 +00:00
|
|
|
hleCheckCurrentCallbacks();
|
2012-12-17 05:32:52 +00:00
|
|
|
return m->ReceiveMessage(packetAddrPtr);
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelReceiveMbxCB(%i, %08x, %08x): no message in queue, waiting", id, packetAddrPtr, timeoutPtr);
|
2013-09-09 04:41:54 +00:00
|
|
|
HLEKernel::RemoveWaitingThread(m->waitingThreads, __KernelGetCurThread());
|
2012-12-16 02:06:55 +00:00
|
|
|
m->AddWaitingThread(__KernelGetCurThread(), packetAddrPtr);
|
2012-12-16 02:57:15 +00:00
|
|
|
__KernelWaitMbx(m, timeoutPtr);
|
2013-01-26 18:44:04 +00:00
|
|
|
__KernelWaitCurThread(WAITTYPE_MBX, id, 0, timeoutPtr, true, "mbx waited");
|
2012-12-17 02:40:47 +00:00
|
|
|
return 0;
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
int sceKernelPollMbx(SceUID id, u32 packetAddrPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 20:49:56 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
2012-11-08 13:24:51 +00:00
|
|
|
|
|
|
|
if (!m)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelPollMbx(%i, %08x): invalid mbx id", id, packetAddrPtr);
|
2012-11-08 13:24:51 +00:00
|
|
|
return error;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
if (m->nmb.numMessages > 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelPollMbx(%i, %08x): sending first queue message", id, packetAddrPtr);
|
2012-12-17 05:32:52 +00:00
|
|
|
return m->ReceiveMessage(packetAddrPtr);
|
2012-11-08 13:24:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "SCE_KERNEL_ERROR_MBOX_NOMSG=sceKernelPollMbx(%i, %08x): no message in queue", id, packetAddrPtr);
|
2012-11-08 13:24:51 +00:00
|
|
|
return SCE_KERNEL_ERROR_MBOX_NOMSG;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
int sceKernelCancelReceiveMbx(SceUID id, u32 numWaitingThreadsAddr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-08 13:24:51 +00:00
|
|
|
u32 error;
|
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
if (!m)
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelCancelReceiveMbx(%i, %08x): invalid mbx id", id, numWaitingThreadsAddr);
|
2012-11-08 13:24:51 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-01-19 21:48:20 +00:00
|
|
|
u32 count = (u32) m->waitingThreads.size();
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelCancelReceiveMbx(%i, %08x): cancelling %d threads", id, numWaitingThreadsAddr, count);
|
2012-12-16 17:21:22 +00:00
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
bool wokeThreads = false;
|
2012-11-08 13:24:51 +00:00
|
|
|
for (size_t i = 0; i < m->waitingThreads.size(); i++)
|
2012-12-16 17:21:22 +00:00
|
|
|
__KernelUnlockMbxForThread(m, m->waitingThreads[i], error, SCE_KERNEL_ERROR_WAIT_CANCEL, wokeThreads);
|
2012-11-08 13:24:51 +00:00
|
|
|
m->waitingThreads.clear();
|
2012-11-27 09:18:36 +00:00
|
|
|
|
2012-12-16 17:21:22 +00:00
|
|
|
if (wokeThreads)
|
|
|
|
hleReSchedule("mbx canceled");
|
|
|
|
|
2012-11-27 09:18:36 +00:00
|
|
|
if (numWaitingThreadsAddr)
|
|
|
|
Memory::Write_U32(count, numWaitingThreadsAddr);
|
2012-11-08 13:24:51 +00:00
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 13:24:51 +00:00
|
|
|
int sceKernelReferMbxStatus(SceUID id, u32 infoAddr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
u32 error;
|
2012-11-08 13:24:51 +00:00
|
|
|
Mbx *m = kernelObjects.Get<Mbx>(id, error);
|
|
|
|
if (!m)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "sceKernelReferMbxStatus(%i, %08x): invalid mbx id", id, infoAddr);
|
2012-11-08 13:24:51 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
// Should we crash the thread somehow?
|
2012-12-16 17:21:22 +00:00
|
|
|
if (!Memory::IsValidAddress(infoAddr))
|
|
|
|
return -1;
|
|
|
|
|
2012-12-17 05:32:52 +00:00
|
|
|
for (int i = 0, n = m->nmb.numMessages; i < n; ++i)
|
|
|
|
m->nmb.packetListHead = Memory::Read_U32(m->nmb.packetListHead);
|
|
|
|
|
2013-09-09 04:29:39 +00:00
|
|
|
HLEKernel::CleanupWaitingThreads(WAITTYPE_MBX, id, m->waitingThreads);
|
2013-09-02 22:11:49 +00:00
|
|
|
|
2012-12-17 02:40:47 +00:00
|
|
|
// For whatever reason, it won't write if the size (first member) is 0.
|
|
|
|
if (Memory::Read_U32(infoAddr) != 0)
|
2012-11-08 13:24:51 +00:00
|
|
|
{
|
2013-01-19 21:48:20 +00:00
|
|
|
m->nmb.numWaitThreads = (int) m->waitingThreads.size();
|
2012-12-17 02:40:47 +00:00
|
|
|
Memory::WriteStruct<NativeMbx>(infoAddr, &m->nmb);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-08 13:24:51 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|