mirror of
https://github.com/ran-j/PS2Recomp.git
synced 2026-09-28 01:15:04 -04:00
refactor: from guest threads to EE scheduler
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -573,7 +573,7 @@ namespace ps2_stubs
|
||||
}
|
||||
if (hitStreamEnd || g_cdStreamingLbn == g_cdStreamingEndLbn)
|
||||
{
|
||||
notifyMpegCdStreamEof();
|
||||
notifyMpegCdStreamEof(runtime);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -582,7 +582,7 @@ namespace ps2_stubs
|
||||
{
|
||||
std::memset(rdram + offset, 0, requestedBytes);
|
||||
}
|
||||
notifyMpegCdStreamEof();
|
||||
notifyMpegCdStreamEof(runtime);
|
||||
}
|
||||
|
||||
if (int32_t *err = reinterpret_cast<int32_t *>(getMemPtr(rdram, errAddr)); err)
|
||||
@@ -635,7 +635,7 @@ namespace ps2_stubs
|
||||
g_cdStreamingEndLbn = cdStreamingEndLbnForStart(g_cdStreamingLbn);
|
||||
g_cdStReadTraceCount = 0u;
|
||||
|
||||
notifyMpegCdStreamStart();
|
||||
notifyMpegCdStreamStart(runtime);
|
||||
|
||||
std::cerr << "[sceCdStStart] lbn=0x" << std::hex << g_cdStreamingLbn
|
||||
<< " endLbn=0x" << g_cdStreamingEndLbn << std::dec << std::endl;
|
||||
@@ -649,7 +649,7 @@ namespace ps2_stubs
|
||||
|
||||
void sceCdStStop(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
notifyMpegCdStreamEof();
|
||||
notifyMpegCdStreamEof(runtime);
|
||||
setReturnS32(ctx, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,20 +3,12 @@
|
||||
#include "ps2_log.h"
|
||||
#include "runtime/ps2_gs_common.h"
|
||||
#include "runtime/ps2_gs_psmct16.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
|
||||
namespace ps2_stubs
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::mutex g_gs_sync_v_mutex;
|
||||
uint64_t g_gs_sync_v_base_tick = 0u;
|
||||
std::mutex g_gs_sync_v_callback_mutex;
|
||||
uint32_t g_gs_sync_v_callback_func = 0u;
|
||||
uint32_t g_gs_sync_v_callback_gp = 0u;
|
||||
uint32_t g_gs_sync_v_callback_sp = 0u;
|
||||
uint32_t g_gs_sync_v_callback_stack_base = 0u;
|
||||
uint32_t g_gs_sync_v_callback_stack_top = 0u;
|
||||
uint32_t g_gs_sync_v_callback_bad_pc_logs = 0u;
|
||||
uint64_t makeClearPrim(bool useContext2)
|
||||
{
|
||||
return static_cast<uint64_t>(GS_PRIM_SPRITE) |
|
||||
@@ -598,175 +590,6 @@ namespace ps2_stubs
|
||||
setReturnU32(ctx, terminatePacketBuilderState(rdram, ctx, runtime));
|
||||
}
|
||||
|
||||
static void resetGsSyncVState()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_mutex);
|
||||
g_gs_sync_v_base_tick = ps2_syscalls::GetCurrentVSyncTick();
|
||||
}
|
||||
|
||||
static int32_t getGsSyncVFieldForTick(uint64_t tick)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_mutex);
|
||||
if (tick <= g_gs_sync_v_base_tick)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<int32_t>((tick - g_gs_sync_v_base_tick - 1u) & 1u);
|
||||
}
|
||||
|
||||
void resetGsSyncVCallbackState()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_callback_mutex);
|
||||
g_gs_sync_v_callback_func = 0u;
|
||||
g_gs_sync_v_callback_gp = 0u;
|
||||
g_gs_sync_v_callback_sp = 0u;
|
||||
g_gs_sync_v_callback_stack_base = 0u;
|
||||
g_gs_sync_v_callback_stack_top = 0u;
|
||||
g_gs_sync_v_callback_bad_pc_logs = 0u;
|
||||
}
|
||||
resetGsSyncVState();
|
||||
}
|
||||
|
||||
void dispatchGsSyncVCallback(uint8_t *rdram, PS2Runtime *runtime, uint64_t tick)
|
||||
{
|
||||
if (!rdram || !runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t callback = 0u;
|
||||
uint32_t gp = 0u;
|
||||
uint32_t callbackStackTop = 0u;
|
||||
const uint64_t callbackTick = (tick != 0u) ? tick : ps2_syscalls::GetCurrentVSyncTick();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_callback_mutex);
|
||||
callback = g_gs_sync_v_callback_func;
|
||||
gp = g_gs_sync_v_callback_gp;
|
||||
callbackStackTop = g_gs_sync_v_callback_stack_top;
|
||||
if (callback == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!runtime->hasFunction(callback))
|
||||
{
|
||||
static uint32_t s_missingCallbackLogCount = 0u;
|
||||
if (s_missingCallbackLogCount < 32u)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[sceGsSyncVCallback:missing] cb=0x" << std::hex << callback
|
||||
<< " gp=0x" << gp
|
||||
<< " tick=0x" << callbackTick
|
||||
<< std::dec << std::endl;
|
||||
});
|
||||
++s_missingCallbackLogCount;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (callbackStackTop == 0u)
|
||||
{
|
||||
constexpr uint32_t kCallbackStackSize = 0x4000u;
|
||||
const uint32_t stackTop = runtime->reserveAsyncCallbackStack(kCallbackStackSize, 16u);
|
||||
if (stackTop != 0u)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_callback_mutex);
|
||||
if (g_gs_sync_v_callback_stack_top == 0u)
|
||||
{
|
||||
g_gs_sync_v_callback_stack_base = stackTop - (kCallbackStackSize - 0x10u);
|
||||
g_gs_sync_v_callback_stack_top = stackTop;
|
||||
}
|
||||
callbackStackTop = g_gs_sync_v_callback_stack_top;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
R5900Context callbackCtx{};
|
||||
SET_GPR_U32(&callbackCtx, 28, gp);
|
||||
SET_GPR_U32(&callbackCtx, 29, (callbackStackTop != 0u) ? callbackStackTop : (PS2_RAM_SIZE - 0x10u));
|
||||
SET_GPR_U32(&callbackCtx, 31, 0u);
|
||||
SET_GPR_U32(&callbackCtx, 4, static_cast<uint32_t>(callbackTick));
|
||||
callbackCtx.pc = callback;
|
||||
|
||||
static uint32_t s_dispatchLogCount = 0u;
|
||||
const bool shouldLogDispatch = (s_dispatchLogCount < 64u);
|
||||
if (shouldLogDispatch)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
RUNTIME_LOG("[sceGsSyncVCallback:dispatch] cb=0x" << std::hex << callback
|
||||
<< " gp=0x" << gp
|
||||
<< " sp=0x" << getRegU32(&callbackCtx, 29)
|
||||
<< " tick=0x" << callbackTick
|
||||
<< std::dec << std::endl);
|
||||
});
|
||||
}
|
||||
|
||||
uint32_t steps = 0u;
|
||||
bool reschedulePending = false;
|
||||
uint64_t handoffBaseline = 0u;
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
PS2Runtime::DeferredGuestYieldScope deferYield(reschedulePending);
|
||||
|
||||
while (callbackCtx.pc != 0u && !runtime->isStopRequested() && steps < 1024u)
|
||||
{
|
||||
if (!runtime->hasFunction(callbackCtx.pc))
|
||||
{
|
||||
if (g_gs_sync_v_callback_bad_pc_logs < 16u)
|
||||
{
|
||||
std::cerr << "[sceGsSyncVCallback:bad-pc] pc=0x" << std::hex << callbackCtx.pc
|
||||
<< " ra=0x" << getRegU32(&callbackCtx, 31)
|
||||
<< " sp=0x" << getRegU32(&callbackCtx, 29)
|
||||
<< " gp=0x" << getRegU32(&callbackCtx, 28)
|
||||
<< std::dec << std::endl;
|
||||
++g_gs_sync_v_callback_bad_pc_logs;
|
||||
}
|
||||
callbackCtx.pc = 0u;
|
||||
break;
|
||||
}
|
||||
|
||||
auto step = runtime->lookupFunction(callbackCtx.pc);
|
||||
if (!step)
|
||||
{
|
||||
break;
|
||||
}
|
||||
++steps;
|
||||
step(rdram, &callbackCtx, runtime);
|
||||
}
|
||||
handoffBaseline = runtime->guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
if (reschedulePending && !runtime->isStopRequested())
|
||||
{
|
||||
runtime->waitForGuestExecutionHandoff(handoffBaseline);
|
||||
}
|
||||
|
||||
if (shouldLogDispatch)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
RUNTIME_LOG("[sceGsSyncVCallback:return] cb=0x" << std::hex << callback
|
||||
<< " finalPc=0x" << callbackCtx.pc
|
||||
<< " ra=0x" << getRegU32(&callbackCtx, 31)
|
||||
<< " steps=0x" << steps
|
||||
<< std::dec << std::endl);
|
||||
});
|
||||
++s_dispatchLogCount;
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
static uint32_t warnCount = 0u;
|
||||
if (warnCount < 8u)
|
||||
{
|
||||
std::cerr << "[sceGsSyncVCallback] callback exception: " << e.what() << std::endl;
|
||||
++warnCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sceGsExecLoadImage(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
uint32_t imgAddr = getRegU32(ctx, 4);
|
||||
@@ -979,8 +802,6 @@ namespace ps2_stubs
|
||||
g_gparam.omode = static_cast<uint8_t>(omode & 0xFF);
|
||||
g_gparam.ffmode = static_cast<uint8_t>(ffmode & 0x1);
|
||||
writeGsGParamToScratch(runtime);
|
||||
resetGsSyncVState();
|
||||
|
||||
uint64_t pmode = makePmode(1, 0, 0, 0, 0, 0x80);
|
||||
uint64_t smode2 = (interlace & 0x1) | ((ffmode & 0x1) << 1);
|
||||
uint64_t dispfb = makeDispFb(0, 10, 0, 0, 0);
|
||||
@@ -1459,14 +1280,10 @@ namespace ps2_stubs
|
||||
|
||||
void sceGsSyncV(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint64_t tick = ps2_syscalls::WaitForNextVSyncTick(rdram, runtime);
|
||||
if (g_gparam.interlace != 0u)
|
||||
{
|
||||
setReturnS32(ctx, getGsSyncVFieldForTick(tick));
|
||||
return;
|
||||
}
|
||||
|
||||
setReturnS32(ctx, 1);
|
||||
ps2_syscalls::WaitVSyncTick(rdram,
|
||||
ctx,
|
||||
runtime,
|
||||
g_gparam.interlace != 0u ? -1 : 1);
|
||||
}
|
||||
|
||||
void sceGsSyncVCallback(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -1477,17 +1294,9 @@ namespace ps2_stubs
|
||||
const uint32_t gp = getRegU32(ctx, 28);
|
||||
const uint32_t sp = getRegU32(ctx, 29);
|
||||
|
||||
uint32_t oldCallback = 0u;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_gs_sync_v_callback_mutex);
|
||||
oldCallback = g_gs_sync_v_callback_func;
|
||||
g_gs_sync_v_callback_func = newCallback;
|
||||
if (newCallback != 0u)
|
||||
{
|
||||
g_gs_sync_v_callback_gp = gp;
|
||||
g_gs_sync_v_callback_sp = sp;
|
||||
}
|
||||
}
|
||||
EeScheduler &ee = runtime->eeScheduler();
|
||||
ee.bindMainContextForSyscall(*ctx, rdram);
|
||||
const uint32_t oldCallback = ee.setGsVSyncCallback(newCallback, gp, sp);
|
||||
|
||||
static uint32_t s_syncVCallbackLogCount = 0u;
|
||||
if (s_syncVCallbackLogCount < 128u)
|
||||
@@ -1504,11 +1313,6 @@ namespace ps2_stubs
|
||||
++s_syncVCallbackLogCount;
|
||||
}
|
||||
|
||||
if (newCallback != 0u)
|
||||
{
|
||||
ps2_syscalls::EnsureVSyncWorkerRunning(rdram, runtime);
|
||||
}
|
||||
|
||||
setReturnU32(ctx, oldCallback);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
namespace ps2_stubs
|
||||
{
|
||||
void resetGsSyncVCallbackState();
|
||||
void dispatchGsSyncVCallback(uint8_t *rdram, PS2Runtime *runtime, uint64_t tick);
|
||||
void sceGifPkAddGsAD(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void sceGifPkAddGsData(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void sceGifPkCloseGifTag(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
|
||||
@@ -1,47 +1,19 @@
|
||||
#include "Common.h"
|
||||
#include "IPU.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
|
||||
namespace ps2_stubs
|
||||
namespace
|
||||
{
|
||||
void sceIpuInit(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
constexpr uint32_t REG_IPU_CTRL = 0x10002010u;
|
||||
constexpr uint32_t REG_IPU_CMD = 0x10002000u;
|
||||
constexpr uint32_t REG_IPU_IN_FIFO = 0x10007010u;
|
||||
constexpr uint32_t IQVAL_BASE = 0x1721e0u;
|
||||
constexpr uint32_t VQVAL_BASE = 0x172230u;
|
||||
constexpr uint32_t SETD4_CHCR_ENTRY = 0x126428u;
|
||||
|
||||
void completeIpuInit(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
static constexpr uint32_t REG_IPU_CTRL = 0x10002010u;
|
||||
static constexpr uint32_t REG_IPU_CMD = 0x10002000u;
|
||||
static constexpr uint32_t REG_IPU_IN_FIFO = 0x10007010u;
|
||||
static constexpr uint32_t IQVAL_BASE = 0x1721e0u;
|
||||
static constexpr uint32_t VQVAL_BASE = 0x172230u;
|
||||
static constexpr uint32_t SETD4_CHCR_ENTRY = 0x126428u;
|
||||
|
||||
if (!runtime)
|
||||
return;
|
||||
|
||||
if (!runtime->memory().getRDRAM())
|
||||
{
|
||||
if (!runtime->memory().initialize())
|
||||
{
|
||||
setReturnS32(ctx, -1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!runtime->syncCoreSubsystems())
|
||||
{
|
||||
setReturnS32(ctx, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
PS2Memory &mem = runtime->memory();
|
||||
|
||||
if (runtime->hasFunction(SETD4_CHCR_ENTRY))
|
||||
{
|
||||
auto setD4 = runtime->lookupFunction(SETD4_CHCR_ENTRY);
|
||||
ctx->r[4] = _mm_set_epi64x(0, 1);
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
setD4(rdram, ctx, runtime);
|
||||
}
|
||||
}
|
||||
|
||||
mem.write32(REG_IPU_CTRL, 0x40000000u);
|
||||
mem.write32(REG_IPU_CMD, 0u);
|
||||
|
||||
@@ -70,12 +42,54 @@ namespace ps2_stubs
|
||||
|
||||
mem.write32(REG_IPU_CMD, 0x60000000u);
|
||||
mem.write32(REG_IPU_CMD, 0x90000000u);
|
||||
|
||||
mem.write32(REG_IPU_CTRL, 0x40000000u);
|
||||
mem.write32(REG_IPU_CMD, 0u);
|
||||
|
||||
setReturnS32(ctx, 0);
|
||||
}
|
||||
}
|
||||
|
||||
namespace ps2_stubs
|
||||
{
|
||||
void sceIpuInit(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
if (!runtime)
|
||||
return;
|
||||
|
||||
if (!runtime->memory().getRDRAM())
|
||||
{
|
||||
if (!runtime->memory().initialize())
|
||||
{
|
||||
setReturnS32(ctx, -1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!runtime->syncCoreSubsystems())
|
||||
{
|
||||
setReturnS32(ctx, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (runtime->hasFunction(SETD4_CHCR_ENTRY))
|
||||
{
|
||||
EeScheduler &scheduler = runtime->eeScheduler();
|
||||
scheduler.bindMainContextForSyscall(*ctx, rdram);
|
||||
GuestInvocation invocation{};
|
||||
invocation.kind = GuestInvocationKind::HleCall;
|
||||
invocation.context = *ctx;
|
||||
invocation.context.pc = SETD4_CHCR_ENTRY;
|
||||
SET_GPR_U32(&invocation.context, 4, 1u);
|
||||
SET_GPR_U32(&invocation.context, 29, 0u);
|
||||
SET_GPR_U32(&invocation.context, 31, 0u);
|
||||
invocation.onComplete = [rdram, runtime](const R5900Context &, R5900Context &parent)
|
||||
{
|
||||
completeIpuInit(rdram, &parent, runtime);
|
||||
};
|
||||
scheduler.invokeCurrent(std::move(invocation));
|
||||
}
|
||||
|
||||
completeIpuInit(rdram, ctx, runtime);
|
||||
}
|
||||
|
||||
void sceIpuRestartDMA(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Common.h"
|
||||
#include "MPEG.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
|
||||
#if !defined(PS2X_HAS_FFMPEG)
|
||||
#define PS2X_HAS_FFMPEG 1
|
||||
@@ -15,8 +16,6 @@ extern "C"
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
|
||||
@@ -472,16 +471,11 @@ namespace ps2_stubs
|
||||
bool streamEnded = false;
|
||||
bool decoderFailed = false;
|
||||
uint64_t cdStreamGeneration = 0u;
|
||||
bool noFrameStallArmed = false;
|
||||
std::chrono::steady_clock::time_point noFrameStallStart{};
|
||||
uint32_t consecutiveEmptyGetPicture = 0u;
|
||||
bool waitingForVideoSequenceHeader = true;
|
||||
std::vector<uint8_t> videoSequenceSyncBuffer;
|
||||
std::vector<uint8_t> pssBuffer;
|
||||
std::vector<uint32_t> pssGuestAddrs;
|
||||
std::deque<MpegDecodedFrame> decodedFrames;
|
||||
bool hasLastFrame = false;
|
||||
MpegDecodedFrame lastFrame;
|
||||
std::unique_ptr<MpegFfmpegDecoder> decoder;
|
||||
};
|
||||
|
||||
@@ -513,7 +507,7 @@ namespace ps2_stubs
|
||||
};
|
||||
|
||||
std::mutex g_mpeg_stub_mutex;
|
||||
std::condition_variable g_mpeg_cv;
|
||||
constexpr uint32_t kMpegPictureWaitType = 1u;
|
||||
MpegStubState g_mpeg_stub_state;
|
||||
|
||||
// TODO this resolution should follow runtime resolution
|
||||
@@ -529,10 +523,6 @@ namespace ps2_stubs
|
||||
constexpr uint8_t kMpegPrivateStream1 = 0xBDu;
|
||||
constexpr size_t kStartCodeNotFound = std::numeric_limits<size_t>::max();
|
||||
constexpr uint32_t kMpegCallbackDataSize = 0x20u;
|
||||
constexpr uint32_t kMpegCallbackMaxSteps = 0x4000u;
|
||||
constexpr std::chrono::milliseconds kMpegGetPictureNoFrameWaitTimeout{64};
|
||||
constexpr std::chrono::milliseconds kMpegNoFrameEndTimeout{500};
|
||||
constexpr uint32_t kMpegMaxConsecutiveEmptyGetPicture = 60u;
|
||||
|
||||
uint32_t align16(uint32_t value)
|
||||
{
|
||||
@@ -732,77 +722,6 @@ namespace ps2_stubs
|
||||
}
|
||||
}
|
||||
|
||||
void clearNoFrameStall(MpegPlaybackState &playback)
|
||||
{
|
||||
playback.noFrameStallArmed = false;
|
||||
playback.noFrameStallStart = {};
|
||||
}
|
||||
|
||||
void finishPlaybackStream(uint32_t mpegAddr, MpegPlaybackState &playback);
|
||||
|
||||
bool maybeFinishNoFrameStall(uint32_t mpegAddr, MpegPlaybackState &playback)
|
||||
{
|
||||
if (playback.streamEnded || playback.decoderFailed || !playback.decodedFrames.empty())
|
||||
{
|
||||
clearNoFrameStall(playback);
|
||||
playback.consecutiveEmptyGetPicture = 0u;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!playback.sawInput || playback.picturesServed == 0u)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const bool cdStreamEofSeen = g_mpeg_stub_state.currentCdStreamEofSeen;
|
||||
|
||||
bool stallByNoFrame = false;
|
||||
if (cdStreamEofSeen)
|
||||
{
|
||||
if (!playback.noFrameStallArmed)
|
||||
{
|
||||
playback.noFrameStallArmed = true;
|
||||
playback.noFrameStallStart = now;
|
||||
}
|
||||
else if (now - playback.noFrameStallStart >= kMpegNoFrameEndTimeout)
|
||||
{
|
||||
stallByNoFrame = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
clearNoFrameStall(playback);
|
||||
}
|
||||
|
||||
const bool stallByConsecutive =
|
||||
cdStreamEofSeen && (playback.consecutiveEmptyGetPicture >= kMpegMaxConsecutiveEmptyGetPicture);
|
||||
|
||||
if (!stallByNoFrame && !stallByConsecutive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
finishPlaybackStream(mpegAddr, playback);
|
||||
|
||||
static uint32_t s_noFrameEofLogCount = 0u;
|
||||
if (s_noFrameEofLogCount < 16u)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[MPEG:no-frame-eof] mpeg=0x" << std::hex << mpegAddr
|
||||
<< std::dec << " served=" << playback.picturesServed
|
||||
<< " sawInput=" << playback.sawInput
|
||||
<< " cdEof=" << cdStreamEofSeen
|
||||
<< " reason="
|
||||
<< (stallByNoFrame ? "no-frame" : "consecutive")
|
||||
<< " consecutiveEmpty=" << playback.consecutiveEmptyGetPicture
|
||||
<< std::endl;
|
||||
});
|
||||
++s_noFrameEofLogCount;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void feedElementaryStream(MpegPlaybackState &playback, const uint8_t *data, size_t size)
|
||||
{
|
||||
if (!data || size == 0)
|
||||
@@ -890,10 +809,6 @@ namespace ps2_stubs
|
||||
|
||||
playback.videoSequenceSyncBuffer.clear();
|
||||
flushDecoderIfEnded(playback);
|
||||
if (!playback.decodedFrames.empty())
|
||||
{
|
||||
clearNoFrameStall(playback);
|
||||
}
|
||||
}
|
||||
|
||||
void erasePssPrefix(MpegPlaybackState &playback, size_t count)
|
||||
@@ -1190,10 +1105,6 @@ namespace ps2_stubs
|
||||
playback.pssGuestAddrs.push_back(guestAddr + static_cast<uint32_t>(i));
|
||||
}
|
||||
processPssBuffer(mpegAddr, playback, callbackEvents);
|
||||
if (!playback.decodedFrames.empty())
|
||||
{
|
||||
clearNoFrameStall(playback);
|
||||
}
|
||||
}
|
||||
|
||||
size_t appendGuestBytes(uint32_t mpegAddr,
|
||||
@@ -1317,15 +1228,6 @@ namespace ps2_stubs
|
||||
return;
|
||||
}
|
||||
|
||||
thread_local PS2Runtime *s_callbackStackRuntime = nullptr;
|
||||
thread_local uint32_t s_callbackStackTop = 0u;
|
||||
if (s_callbackStackRuntime != runtime || s_callbackStackTop == 0u)
|
||||
{
|
||||
constexpr uint32_t kCallbackStackSize = 0x4000u;
|
||||
s_callbackStackRuntime = runtime;
|
||||
s_callbackStackTop = runtime->reserveAsyncCallbackStack(kCallbackStackSize, 16u);
|
||||
}
|
||||
|
||||
const uint32_t cbDataAddr = runtime->guestMalloc(kMpegCallbackDataSize, 16u);
|
||||
if (cbDataAddr == 0u)
|
||||
{
|
||||
@@ -1342,61 +1244,18 @@ namespace ps2_stubs
|
||||
SET_GPR_U32(&callbackCtx, 5, cbDataAddr);
|
||||
SET_GPR_U32(&callbackCtx, 6, callback.data);
|
||||
SET_GPR_U32(&callbackCtx, 7, 0u);
|
||||
SET_GPR_U32(&callbackCtx, 29, (s_callbackStackTop != 0u) ? s_callbackStackTop : (PS2_RAM_SIZE - 0x10u));
|
||||
SET_GPR_U32(&callbackCtx, 29, 0u);
|
||||
SET_GPR_U32(&callbackCtx, 31, 0u);
|
||||
callbackCtx.pc = callback.func;
|
||||
|
||||
uint32_t steps = 0u;
|
||||
bool reschedulePending = false;
|
||||
uint64_t handoffBaseline = 0u;
|
||||
GuestInvocation invocation{};
|
||||
invocation.kind = GuestInvocationKind::RpcCallback;
|
||||
invocation.context = callbackCtx;
|
||||
invocation.onComplete = [runtime, cbDataAddr](const R5900Context &, R5900Context &)
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
PS2Runtime::DeferredGuestYieldScope deferYield(reschedulePending);
|
||||
|
||||
while (callbackCtx.pc != 0u && !runtime->isStopRequested() && steps < kMpegCallbackMaxSteps)
|
||||
{
|
||||
if (!runtime->hasFunction(callbackCtx.pc))
|
||||
{
|
||||
static uint32_t badPcLogCount = 0u;
|
||||
if (badPcLogCount < 16u)
|
||||
{
|
||||
std::cerr << "[MPEG:callback:bad-pc] cb=0x" << std::hex << callback.func
|
||||
<< " pc=0x" << callbackCtx.pc
|
||||
<< " ra=0x" << getRegU32(&callbackCtx, 31)
|
||||
<< std::dec << std::endl;
|
||||
++badPcLogCount;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
PS2Runtime::RecompiledFunction step = runtime->lookupFunction(callbackCtx.pc);
|
||||
if (!step)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
step(rdram, &callbackCtx, runtime);
|
||||
++steps;
|
||||
}
|
||||
handoffBaseline = runtime->guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
if (reschedulePending && !runtime->isStopRequested())
|
||||
{
|
||||
runtime->waitForGuestExecutionHandoff(handoffBaseline);
|
||||
}
|
||||
|
||||
if (steps >= kMpegCallbackMaxSteps)
|
||||
{
|
||||
static uint32_t stepLimitLogCount = 0u;
|
||||
if (stepLimitLogCount < 16u)
|
||||
{
|
||||
std::cerr << "[MPEG:callback:step-limit] cb=0x" << std::hex << callback.func
|
||||
<< " pc=0x" << callbackCtx.pc << std::dec << std::endl;
|
||||
++stepLimitLogCount;
|
||||
}
|
||||
}
|
||||
|
||||
runtime->guestFree(cbDataAddr);
|
||||
runtime->guestFree(cbDataAddr);
|
||||
};
|
||||
runtime->eeScheduler().queueInvocation(std::move(invocation));
|
||||
}
|
||||
|
||||
void dispatchStreamCallbacks(uint8_t *rdram,
|
||||
@@ -1428,7 +1287,6 @@ namespace ps2_stubs
|
||||
return;
|
||||
}
|
||||
|
||||
PS2Runtime::GuestExecutionReleaseScope releaseGuestExecution(runtime);
|
||||
dispatchStreamCallbacks(rdram, ctx, runtime, events);
|
||||
}
|
||||
|
||||
@@ -1543,11 +1401,27 @@ namespace ps2_stubs
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
resetMpegStubStateUnlocked();
|
||||
g_mpeg_cv.notify_all();
|
||||
}
|
||||
|
||||
void notifyMpegCdStreamStart()
|
||||
void enqueueMpegDecodedFrameForTesting(uint32_t mpegAddr)
|
||||
{
|
||||
constexpr int kTestFrameWidth = 16;
|
||||
constexpr int kTestFrameHeight = 16;
|
||||
|
||||
MpegDecodedFrame frame;
|
||||
frame.width = kTestFrameWidth;
|
||||
frame.height = kTestFrameHeight;
|
||||
frame.rgba.resize(static_cast<size_t>(kTestFrameWidth * kTestFrameHeight * 4), 0x80u);
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
playback.sawInput = true;
|
||||
playback.decodedFrames.push_back(std::move(frame));
|
||||
}
|
||||
|
||||
void notifyMpegCdStreamStart(PS2Runtime *runtime)
|
||||
{
|
||||
(void)runtime;
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
++g_mpeg_stub_state.cdStreamGeneration;
|
||||
g_mpeg_stub_state.currentCdStreamEofSeen = false;
|
||||
@@ -1566,23 +1440,26 @@ namespace ps2_stubs
|
||||
std::cerr << "[MPEG:CdStreamStart] generation=" << g_mpeg_stub_state.cdStreamGeneration
|
||||
<< " reopened=" << g_mpeg_stub_state.playbackByMpeg.size() << std::endl;
|
||||
});
|
||||
g_mpeg_cv.notify_all();
|
||||
}
|
||||
|
||||
void notifyMpegCdStreamEof()
|
||||
void notifyMpegCdStreamEof(PS2Runtime *runtime)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
g_mpeg_stub_state.currentCdStreamEofSeen = true;
|
||||
std::vector<uint32_t> completedMpegIds;
|
||||
bool changed = false;
|
||||
for (auto &[mpegAddr, playback] : g_mpeg_stub_state.playbackByMpeg)
|
||||
{
|
||||
if (!playback.sawInput || playback.streamEnded)
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
g_mpeg_stub_state.currentCdStreamEofSeen = true;
|
||||
for (auto &[mpegAddr, playback] : g_mpeg_stub_state.playbackByMpeg)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
completedMpegIds.push_back(mpegAddr);
|
||||
if (!playback.sawInput || playback.streamEnded)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
finishPlaybackStream(mpegAddr, playback);
|
||||
changed = true;
|
||||
finishPlaybackStream(mpegAddr, playback);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
@@ -1595,52 +1472,58 @@ namespace ps2_stubs
|
||||
});
|
||||
++s_eofLogCount;
|
||||
}
|
||||
g_mpeg_cv.notify_all();
|
||||
}
|
||||
if (runtime)
|
||||
{
|
||||
for (const uint32_t mpegAddr : completedMpegIds)
|
||||
{
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sceMpegFlush(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)rdram;
|
||||
(void)runtime;
|
||||
|
||||
const uint32_t mpegAddr = getRegU32(ctx, 4);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
if (playback.decoder)
|
||||
{
|
||||
playback.decoder->flush(playback.decodedFrames);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
if (playback.decoder)
|
||||
{
|
||||
playback.decoder->flush(playback.decodedFrames);
|
||||
}
|
||||
}
|
||||
g_mpeg_cv.notify_all();
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_OK);
|
||||
setReturnS32(ctx, 0);
|
||||
}
|
||||
|
||||
void sceMpegAddBs(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)runtime;
|
||||
|
||||
const uint32_t mpegAddr = getRegU32(ctx, 4);
|
||||
const uint32_t dataAddr = getRegU32(ctx, 5);
|
||||
const uint32_t byteCount = getRegU32(ctx, 6);
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
size_t copied = 0u;
|
||||
while (copied < byteCount)
|
||||
{
|
||||
const uint32_t curAddr = dataAddr + static_cast<uint32_t>(copied);
|
||||
const uint32_t offset = curAddr & PS2_RAM_MASK;
|
||||
const size_t chunk = std::min<size_t>(static_cast<size_t>(byteCount) - copied, PS2_RAM_SIZE - offset);
|
||||
const uint8_t *src = getConstMemPtr(rdram, curAddr);
|
||||
if (!src || chunk == 0u)
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
while (copied < byteCount)
|
||||
{
|
||||
break;
|
||||
const uint32_t curAddr = dataAddr + static_cast<uint32_t>(copied);
|
||||
const uint32_t offset = curAddr & PS2_RAM_MASK;
|
||||
const size_t chunk = std::min<size_t>(static_cast<size_t>(byteCount) - copied, PS2_RAM_SIZE - offset);
|
||||
const uint8_t *src = getConstMemPtr(rdram, curAddr);
|
||||
if (!src || chunk == 0u)
|
||||
{
|
||||
break;
|
||||
}
|
||||
feedElementaryStream(playback, src, chunk);
|
||||
copied += chunk;
|
||||
}
|
||||
feedElementaryStream(playback, src, chunk);
|
||||
copied += chunk;
|
||||
}
|
||||
|
||||
g_mpeg_cv.notify_all();
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_OK);
|
||||
setReturnS32(ctx, static_cast<int32_t>(copied));
|
||||
}
|
||||
|
||||
@@ -1801,12 +1684,14 @@ namespace ps2_stubs
|
||||
void sceMpegDelete(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)rdram;
|
||||
(void)runtime;
|
||||
|
||||
const uint32_t mpegAddr = getRegU32(ctx, 4);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
g_mpeg_stub_state.callbacksByMpeg.erase(mpegAddr);
|
||||
g_mpeg_stub_state.playbackByMpeg.erase(mpegAddr);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
g_mpeg_stub_state.callbacksByMpeg.erase(mpegAddr);
|
||||
g_mpeg_stub_state.playbackByMpeg.erase(mpegAddr);
|
||||
}
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_WAIT_DELETE);
|
||||
setReturnU32(ctx, 0u);
|
||||
}
|
||||
|
||||
@@ -1821,14 +1706,13 @@ namespace ps2_stubs
|
||||
size_t decodedCount = 0u;
|
||||
uint32_t traceIdx = 0u;
|
||||
{
|
||||
PS2Runtime::GuestExecutionReleaseScope releaseGuestExecution(runtime);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
consumed = appendGuestBytes(mpegAddr, playback, rdram, dataAddr, byteCount, callbackEvents);
|
||||
decodedCount = playback.decodedFrames.size();
|
||||
traceIdx = g_mpeg_stub_state.demuxPssTraceCount++;
|
||||
}
|
||||
g_mpeg_cv.notify_all();
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_OK);
|
||||
|
||||
if (traceIdx < 32u)
|
||||
{
|
||||
@@ -1872,8 +1756,6 @@ namespace ps2_stubs
|
||||
size_t decodedCount = 0u;
|
||||
uint32_t traceIdx = 0u;
|
||||
{
|
||||
// This prevents an ABBA deadlock with sceMpegGetPicture on thread 5, need investigation on other games
|
||||
PS2Runtime::GuestExecutionReleaseScope releaseGuestExecution(runtime);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
consumed = appendGuestRingBytes(
|
||||
@@ -1888,7 +1770,7 @@ namespace ps2_stubs
|
||||
decodedCount = playback.decodedFrames.size();
|
||||
traceIdx = g_mpeg_stub_state.demuxRingTraceCount++;
|
||||
}
|
||||
g_mpeg_cv.notify_all();
|
||||
runtime->eeScheduler().completeExternalWait(kMpegPictureWaitType, mpegAddr, KE_OK);
|
||||
|
||||
if (traceIdx < 32u)
|
||||
{
|
||||
@@ -1959,84 +1841,36 @@ namespace ps2_stubs
|
||||
bool haveFrame = false;
|
||||
MpegDecodedFrame frame;
|
||||
{
|
||||
PS2Runtime::GuestExecutionReleaseScope releaseGuestExecution(runtime);
|
||||
std::unique_lock<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
const uint64_t waitCdStreamGeneration = g_mpeg_stub_state.cdStreamGeneration;
|
||||
|
||||
if (playback.decodedFrames.empty())
|
||||
if (playback.decodedFrames.empty() &&
|
||||
!g_mpeg_stub_state.currentCdStreamEofSeen &&
|
||||
!playback.streamEnded &&
|
||||
!playback.decoderFailed)
|
||||
{
|
||||
playback.consecutiveEmptyGetPicture++;
|
||||
if (g_mpeg_stub_state.getPictureWaitTraceCount < 32u)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[MPEG:GetPicture] waiting for frames, mpeg=0x" << std::hex << mpegAddr
|
||||
<< std::dec << " ended=" << playback.streamEnded
|
||||
<< " failed=" << playback.decoderFailed
|
||||
<< " sawInput=" << playback.sawInput
|
||||
<< " consec=" << playback.consecutiveEmptyGetPicture << std::endl;
|
||||
<< " sawInput=" << playback.sawInput << std::endl;
|
||||
});
|
||||
++g_mpeg_stub_state.getPictureWaitTraceCount;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<ThreadInfo> currentThreadInfo = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> mapLock(g_thread_map_mutex);
|
||||
auto it = g_threads.find(g_currentThreadId);
|
||||
if (it != g_threads.end())
|
||||
currentThreadInfo = it->second;
|
||||
}
|
||||
|
||||
const auto noFrameWaitStart = std::chrono::steady_clock::now();
|
||||
while (runtime &&
|
||||
g_mpeg_stub_state.playbackByMpeg.find(mpegAddr) != g_mpeg_stub_state.playbackByMpeg.end() &&
|
||||
getPlaybackState(mpegAddr).decodedFrames.empty() &&
|
||||
!getPlaybackState(mpegAddr).streamEnded &&
|
||||
!getPlaybackState(mpegAddr).decoderFailed &&
|
||||
g_mpeg_stub_state.cdStreamGeneration == waitCdStreamGeneration &&
|
||||
!runtime->isStopRequested() &&
|
||||
(!currentThreadInfo || !currentThreadInfo->terminated.load(std::memory_order_relaxed)))
|
||||
{
|
||||
g_mpeg_cv.wait_for(lock, std::chrono::milliseconds(8));
|
||||
|
||||
auto playbackIt = g_mpeg_stub_state.playbackByMpeg.find(mpegAddr);
|
||||
if (playbackIt == g_mpeg_stub_state.playbackByMpeg.end())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
MpegPlaybackState &waitPlayback = playbackIt->second;
|
||||
if (maybeFinishNoFrameStall(mpegAddr, waitPlayback))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!g_mpeg_stub_state.currentCdStreamEofSeen &&
|
||||
std::chrono::steady_clock::now() - noFrameWaitStart >= kMpegGetPictureNoFrameWaitTimeout)
|
||||
{
|
||||
static uint32_t s_noFrameYieldLogCount = 0u;
|
||||
if (s_noFrameYieldLogCount < 16u)
|
||||
lock.unlock();
|
||||
runtime->eeScheduler().waitExternal(
|
||||
EeWaitReason::Mpeg,
|
||||
kMpegPictureWaitType,
|
||||
mpegAddr,
|
||||
[rdram, runtime](R5900Context &resumeContext)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[MPEG:GetPicture:yield] mpeg=0x" << std::hex << mpegAddr
|
||||
<< std::dec << " generation=" << g_mpeg_stub_state.cdStreamGeneration
|
||||
<< " sawInput=" << waitPlayback.sawInput
|
||||
<< " served=" << waitPlayback.picturesServed
|
||||
<< " cdEof=" << g_mpeg_stub_state.currentCdStreamEofSeen
|
||||
<< std::endl;
|
||||
});
|
||||
++s_noFrameYieldLogCount;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_mpeg_stub_state.playbackByMpeg.find(mpegAddr) == g_mpeg_stub_state.playbackByMpeg.end())
|
||||
{
|
||||
// The MPEG decoder was deleted while we were waiting.
|
||||
setReturnS32(ctx, -1);
|
||||
return;
|
||||
if (static_cast<int32_t>(getRegU32(&resumeContext, 2)) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
sceMpegGetPicture(rdram, &resumeContext, runtime);
|
||||
});
|
||||
}
|
||||
|
||||
if (!playback.decodedFrames.empty())
|
||||
@@ -2049,9 +1883,6 @@ namespace ps2_stubs
|
||||
height = playback.height;
|
||||
frameCount = playback.picturesServed;
|
||||
playback.picturesServed += 1u;
|
||||
playback.consecutiveEmptyGetPicture = 0u;
|
||||
playback.lastFrame = frame;
|
||||
playback.hasLastFrame = true;
|
||||
haveFrame = true;
|
||||
if (g_mpeg_stub_state.pictureTraceCount < 32u)
|
||||
{
|
||||
@@ -2064,41 +1895,9 @@ namespace ps2_stubs
|
||||
});
|
||||
++g_mpeg_stub_state.pictureTraceCount;
|
||||
}
|
||||
if (!playback.decodedFrames.empty())
|
||||
{
|
||||
clearNoFrameStall(playback);
|
||||
}
|
||||
}
|
||||
else if (!g_mpeg_stub_state.currentCdStreamEofSeen &&
|
||||
playback.sawInput &&
|
||||
playback.hasLastFrame &&
|
||||
playback.picturesServed > 0u &&
|
||||
!playback.streamEnded &&
|
||||
!playback.decoderFailed)
|
||||
{
|
||||
frame = playback.lastFrame;
|
||||
width = static_cast<uint32_t>(frame.width);
|
||||
height = static_cast<uint32_t>(frame.height);
|
||||
frameCount = playback.picturesServed;
|
||||
playback.picturesServed += 1u;
|
||||
playback.consecutiveEmptyGetPicture = 0u;
|
||||
haveFrame = true;
|
||||
|
||||
static uint32_t s_duplicateFrameLogCount = 0u;
|
||||
if (s_duplicateFrameLogCount < 16u)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[MPEG:GetPicture:DUP] mpeg=0x" << std::hex << mpegAddr
|
||||
<< std::dec << " generation=" << g_mpeg_stub_state.cdStreamGeneration
|
||||
<< " frame=" << frameCount
|
||||
<< " size=" << width << "x" << height << std::endl;
|
||||
});
|
||||
++s_duplicateFrameLogCount;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
maybeFinishNoFrameStall(mpegAddr, playback);
|
||||
width = playback.width;
|
||||
height = playback.height;
|
||||
frameCount = playback.picturesServed;
|
||||
@@ -2147,8 +1946,6 @@ namespace ps2_stubs
|
||||
void sceMpegInit(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)rdram;
|
||||
(void)runtime;
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
const uint64_t cdStreamGeneration = g_mpeg_stub_state.cdStreamGeneration;
|
||||
const bool currentCdStreamEofSeen = g_mpeg_stub_state.currentCdStreamEofSeen;
|
||||
@@ -2156,21 +1953,17 @@ namespace ps2_stubs
|
||||
g_mpeg_stub_state.initialized = true;
|
||||
g_mpeg_stub_state.cdStreamGeneration = cdStreamGeneration;
|
||||
g_mpeg_stub_state.currentCdStreamEofSeen = currentCdStreamEofSeen;
|
||||
g_mpeg_cv.notify_all();
|
||||
setReturnU32(ctx, 0u);
|
||||
}
|
||||
|
||||
void sceMpegIsEnd(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)rdram;
|
||||
// runtime used below for GuestExecutionReleaseScope
|
||||
const uint32_t mpegAddr = getRegU32(ctx, 4);
|
||||
|
||||
PS2Runtime::GuestExecutionReleaseScope releaseGuestExecution(runtime);
|
||||
std::lock_guard<std::mutex> lock(g_mpeg_stub_mutex);
|
||||
g_mpeg_stub_state.initialized = true;
|
||||
MpegPlaybackState &playback = getPlaybackState(mpegAddr);
|
||||
maybeFinishNoFrameStall(mpegAddr, playback);
|
||||
const bool ended = playback.streamEnded || (playback.decoderFailed && playback.sawInput);
|
||||
|
||||
if (g_mpeg_stub_state.isEndTraceCount < 16u)
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
namespace ps2_stubs
|
||||
{
|
||||
void resetMpegStubState();
|
||||
void notifyMpegCdStreamStart();
|
||||
void notifyMpegCdStreamEof();
|
||||
void enqueueMpegDecodedFrameForTesting(uint32_t mpegAddr);
|
||||
void notifyMpegCdStreamStart(PS2Runtime *runtime = nullptr);
|
||||
void notifyMpegCdStreamEof(PS2Runtime *runtime = nullptr);
|
||||
void sceMpegFlush(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void sceMpegAddBs(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void sceMpegAddCallback(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "ps2_syscalls.h"
|
||||
#include "ps2_log.h"
|
||||
#include "ps2_runtime.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
#include "ps2_runtime_macros.h"
|
||||
#include "ps2_stubs.h"
|
||||
#include <iostream>
|
||||
@@ -14,7 +15,6 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
#include <filesystem>
|
||||
#include <chrono>
|
||||
@@ -35,10 +35,51 @@ std::string translatePs2Path(const char *ps2Path);
|
||||
#include "Helpers/Loader.h"
|
||||
#include "Helpers/Runtime.h"
|
||||
|
||||
namespace ps2_syscalls
|
||||
inline bool resolveEeGuestRange(uint32_t address, size_t size, uint32_t &offset, bool &scratch)
|
||||
{
|
||||
inline void yieldGuestExecutionAfterWake(PS2Runtime *runtime)
|
||||
scratch = ps2IsScratchpadAddress(address);
|
||||
if (scratch)
|
||||
{
|
||||
runtime->yieldGuestExecutionAfterWake();
|
||||
offset = ps2ScratchpadOffset(address);
|
||||
return size <= PS2_SCRATCHPAD_SIZE && offset <= PS2_SCRATCHPAD_SIZE - size;
|
||||
}
|
||||
|
||||
if (address < PS2_RAM_SIZE)
|
||||
{
|
||||
offset = address;
|
||||
}
|
||||
else if ((address >= 0x20000000u && address < 0x40000000u) ||
|
||||
(address >= 0x80000000u && address < 0xC0000000u))
|
||||
{
|
||||
offset = address & 0x1FFFFFFFu;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return size <= PS2_RAM_SIZE && offset <= PS2_RAM_SIZE - size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const T *getEeGuestStruct(const uint8_t *rdram, uint32_t address)
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
bool scratch = false;
|
||||
if (!rdram || (address & (alignof(T) - 1u)) != 0u ||
|
||||
!resolveEeGuestRange(address, sizeof(T), offset, scratch))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
if (scratch)
|
||||
{
|
||||
const uint8_t *base = ps2GetScratchpadHostPtr();
|
||||
return base ? reinterpret_cast<const T *>(base + offset) : nullptr;
|
||||
}
|
||||
return reinterpret_cast<const T *>(rdram + offset);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T *getEeGuestStruct(uint8_t *rdram, uint32_t address)
|
||||
{
|
||||
return const_cast<T *>(getEeGuestStruct<T>(static_cast<const uint8_t *>(rdram), address));
|
||||
}
|
||||
|
||||
@@ -1,153 +1,3 @@
|
||||
struct ThreadExitException final : public std::exception
|
||||
{
|
||||
const char *what() const noexcept override
|
||||
{
|
||||
return "PS2 Thread Exit";
|
||||
}
|
||||
};
|
||||
|
||||
static void throwIfTerminated(const std::shared_ptr<ThreadInfo> &info)
|
||||
{
|
||||
if (info && info->terminated.load())
|
||||
{
|
||||
throw ThreadExitException();
|
||||
}
|
||||
}
|
||||
|
||||
// Condition-variable waits in the EE runtime must release the global guest
|
||||
// execution mutex, but must not reacquire it while still holding the local
|
||||
// wait-object mutex. Reacquiring guest execution while holding a semaphore,
|
||||
// thread, event-flag, or vsync mutex can create an ABBA deadlock:
|
||||
//
|
||||
// awakened thread: local wait mutex -> waiting for GuestExecutionScope
|
||||
// running thread: GuestExecutionScope -> waiting for local wait mutex
|
||||
//
|
||||
// This helper keeps guest execution released for the whole host wait and for
|
||||
// any post-wake bookkeeping that needs the local mutex, then unlocks the local
|
||||
// mutex before the GuestExecutionReleaseScope destructor reacquires guest code.
|
||||
template <typename Lock, typename WaitFn, typename FinishFn>
|
||||
static void waitWithGuestExecutionReleasedUntilUnlocked(PS2Runtime *runtime,
|
||||
Lock &lock,
|
||||
WaitFn waitFn,
|
||||
FinishFn finishFn)
|
||||
{
|
||||
auto releaseGuestExecution = std::make_unique<PS2Runtime::GuestExecutionReleaseScope>(runtime);
|
||||
|
||||
waitFn();
|
||||
finishFn();
|
||||
|
||||
if (lock.owns_lock())
|
||||
{
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
releaseGuestExecution.reset();
|
||||
}
|
||||
|
||||
template <typename Lock, typename WaitFn>
|
||||
static void waitWithGuestExecutionReleasedUntilUnlocked(PS2Runtime *runtime, Lock &lock, WaitFn waitFn)
|
||||
{
|
||||
waitWithGuestExecutionReleasedUntilUnlocked(runtime, lock, waitFn, []() {});
|
||||
}
|
||||
|
||||
static void waitWhileSuspended(const std::shared_ptr<ThreadInfo> &info, PS2Runtime *runtime = nullptr)
|
||||
{
|
||||
if (!info)
|
||||
return;
|
||||
|
||||
std::unique_lock<std::mutex> lock(info->m);
|
||||
if (info->suspendCount > 0)
|
||||
{
|
||||
info->status = THS_SUSPEND;
|
||||
info->waitType = TSW_NONE;
|
||||
info->waitId = 0;
|
||||
|
||||
bool terminated = false;
|
||||
waitWithGuestExecutionReleasedUntilUnlocked(
|
||||
runtime,
|
||||
lock,
|
||||
[&]()
|
||||
{
|
||||
info->cv.wait(lock, [&]()
|
||||
{ return info->suspendCount == 0 || info->terminated.load(); });
|
||||
},
|
||||
[&]()
|
||||
{
|
||||
terminated = info->terminated.load();
|
||||
if (!terminated)
|
||||
{
|
||||
info->status = THS_RUN;
|
||||
}
|
||||
});
|
||||
|
||||
if (terminated)
|
||||
{
|
||||
throw ThreadExitException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static std::shared_ptr<ThreadInfo> lookupThreadInfo(int tid)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_thread_map_mutex);
|
||||
auto it = g_threads.find(tid);
|
||||
if (it != g_threads.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::shared_ptr<ThreadInfo> ensureCurrentThreadInfo(R5900Context *ctx)
|
||||
{
|
||||
const int tid = g_currentThreadId;
|
||||
std::lock_guard<std::mutex> lock(g_thread_map_mutex);
|
||||
auto it = g_threads.find(tid);
|
||||
if (it != g_threads.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto info = std::make_shared<ThreadInfo>();
|
||||
info->started = true;
|
||||
info->status = THS_RUN;
|
||||
info->currentPriority = info->priority;
|
||||
info->suspendCount = 0;
|
||||
if (ctx)
|
||||
{
|
||||
info->entry = ctx->pc;
|
||||
info->stack = getRegU32(ctx, 29);
|
||||
info->gp = getRegU32(ctx, 28);
|
||||
}
|
||||
info->waitType = TSW_NONE;
|
||||
info->waitId = 0;
|
||||
|
||||
g_threads.emplace(tid, info);
|
||||
return info;
|
||||
}
|
||||
|
||||
static std::shared_ptr<SemaInfo> lookupSemaInfo(int sid)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_sema_map_mutex);
|
||||
auto it = g_semas.find(sid);
|
||||
if (it != g_semas.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::shared_ptr<EventFlagInfo> lookupEventFlagInfo(int eid)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_event_flag_map_mutex);
|
||||
auto it = g_eventFlags.find(eid);
|
||||
if (it != g_eventFlags.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void setRegU32(R5900Context *ctx, int reg, uint32_t value)
|
||||
{
|
||||
if (!ctx || reg < 0 || reg > 31)
|
||||
@@ -155,104 +5,6 @@ static void setRegU32(R5900Context *ctx, int reg, uint32_t value)
|
||||
SET_GPR_U32(ctx, reg, value);
|
||||
}
|
||||
|
||||
static std::chrono::microseconds alarmTicksToDuration(uint16_t ticks)
|
||||
{
|
||||
constexpr uint64_t kAlarmTickUsec = 64u; // Approximate EE H-SYNC tick period.
|
||||
const uint64_t clampedTicks = (ticks == 0u) ? 1u : static_cast<uint64_t>(ticks);
|
||||
return std::chrono::microseconds(clampedTicks * kAlarmTickUsec);
|
||||
}
|
||||
|
||||
static void ensureAlarmWorkerRunning()
|
||||
{
|
||||
std::call_once(g_alarm_worker_once, []()
|
||||
{ std::thread([]()
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
std::shared_ptr<AlarmInfo> readyAlarm;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_alarm_mutex);
|
||||
while (!readyAlarm)
|
||||
{
|
||||
if (g_alarms.empty())
|
||||
{
|
||||
g_alarm_cv.wait(lock);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto nextIt = std::min_element(g_alarms.begin(), g_alarms.end(),
|
||||
[](const auto &a, const auto &b)
|
||||
{
|
||||
return a.second->dueAt < b.second->dueAt;
|
||||
});
|
||||
if (nextIt == g_alarms.end())
|
||||
{
|
||||
g_alarm_cv.wait(lock);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (nextIt->second->dueAt > now)
|
||||
{
|
||||
g_alarm_cv.wait_until(lock, nextIt->second->dueAt);
|
||||
continue;
|
||||
}
|
||||
|
||||
readyAlarm = nextIt->second;
|
||||
g_alarms.erase(nextIt);
|
||||
}
|
||||
}
|
||||
|
||||
if (!readyAlarm || !readyAlarm->runtime || !readyAlarm->rdram || !readyAlarm->handler)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!readyAlarm->runtime->hasFunction(readyAlarm->handler))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
constexpr uint32_t kAlarmCallbackStackSize = 0x4000u;
|
||||
thread_local PS2Runtime *s_alarmStackRuntime = nullptr;
|
||||
thread_local uint32_t s_alarmStackTop = 0u;
|
||||
if (s_alarmStackRuntime != readyAlarm->runtime || s_alarmStackTop == 0u)
|
||||
{
|
||||
s_alarmStackRuntime = readyAlarm->runtime;
|
||||
s_alarmStackTop = readyAlarm->runtime->reserveAsyncCallbackStack(kAlarmCallbackStackSize, 16u);
|
||||
}
|
||||
|
||||
R5900Context callbackCtx{};
|
||||
setRegU32(&callbackCtx, 28, readyAlarm->gp);
|
||||
setRegU32(&callbackCtx, 29,
|
||||
(s_alarmStackTop != 0u) ? s_alarmStackTop : (PS2_RAM_SIZE - 0x10u));
|
||||
setRegU32(&callbackCtx, 31, 0);
|
||||
setRegU32(&callbackCtx, 4, static_cast<uint32_t>(readyAlarm->id));
|
||||
setRegU32(&callbackCtx, 5, static_cast<uint32_t>(readyAlarm->ticks));
|
||||
setRegU32(&callbackCtx, 6, readyAlarm->commonArg);
|
||||
setRegU32(&callbackCtx, 7, 0);
|
||||
callbackCtx.pc = readyAlarm->handler;
|
||||
|
||||
PS2Runtime::RecompiledFunction func = readyAlarm->runtime->lookupFunction(readyAlarm->handler);
|
||||
func(readyAlarm->rdram, &callbackCtx, readyAlarm->runtime);
|
||||
}
|
||||
catch (const ThreadExitException &)
|
||||
{
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
static int alarmExceptionLogs = 0;
|
||||
if (alarmExceptionLogs < 8)
|
||||
{
|
||||
std::cerr << "[SetAlarm] callback exception: " << e.what() << std::endl;
|
||||
++alarmExceptionLogs;
|
||||
}
|
||||
}
|
||||
} })
|
||||
.detach(); });
|
||||
}
|
||||
|
||||
static void rpcCopyToRdram(uint8_t *rdram, uint32_t dst, uint32_t src, size_t size)
|
||||
{
|
||||
if (!rdram || size == 0)
|
||||
@@ -331,146 +83,6 @@ static bool readStackU32(uint8_t *rdram, uint32_t sp, uint32_t offset, uint32_t
|
||||
return true;
|
||||
}
|
||||
|
||||
enum class RpcInvokeExitReason
|
||||
{
|
||||
Returned,
|
||||
NullPc,
|
||||
MissingFunction,
|
||||
StepLimit,
|
||||
SamePcLimit
|
||||
};
|
||||
|
||||
static const char *rpcInvokeExitReasonName(RpcInvokeExitReason reason)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
case RpcInvokeExitReason::Returned:
|
||||
return "returned";
|
||||
case RpcInvokeExitReason::NullPc:
|
||||
return "null-pc";
|
||||
case RpcInvokeExitReason::MissingFunction:
|
||||
return "missing-function";
|
||||
case RpcInvokeExitReason::StepLimit:
|
||||
return "step-limit";
|
||||
case RpcInvokeExitReason::SamePcLimit:
|
||||
return "same-pc-limit";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static bool rpcInvokeFunction(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime,
|
||||
uint32_t funcAddr, uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t *outV0)
|
||||
{
|
||||
if (!runtime || !ctx || !funcAddr || !runtime->hasFunction(funcAddr))
|
||||
return false;
|
||||
|
||||
constexpr uint32_t kRpcInvokeStackSize = 0x4000u;
|
||||
constexpr uint32_t kRpcInvokeReturnSentinel = 0x00FFF000u;
|
||||
constexpr uint32_t kRpcInvokeMaxSteps = 0x8000u;
|
||||
|
||||
R5900Context tmp = *ctx;
|
||||
setRegU32(&tmp, 4, a0);
|
||||
setRegU32(&tmp, 5, a1);
|
||||
setRegU32(&tmp, 6, a2);
|
||||
setRegU32(&tmp, 7, a3);
|
||||
|
||||
thread_local uint32_t s_rpcInvokeStackBase = 0u;
|
||||
thread_local uint32_t s_rpcInvokeStackTop = 0u;
|
||||
if (s_rpcInvokeStackTop == 0u)
|
||||
{
|
||||
const uint32_t stackBase = runtime->guestMalloc(kRpcInvokeStackSize, 16u);
|
||||
if (stackBase != 0u)
|
||||
{
|
||||
s_rpcInvokeStackBase = stackBase;
|
||||
s_rpcInvokeStackTop = (stackBase + kRpcInvokeStackSize) & ~0xFu;
|
||||
}
|
||||
}
|
||||
if (s_rpcInvokeStackTop != 0u)
|
||||
{
|
||||
setRegU32(&tmp, 29, s_rpcInvokeStackTop);
|
||||
}
|
||||
(void)s_rpcInvokeStackBase;
|
||||
|
||||
setRegU32(&tmp, 31, kRpcInvokeReturnSentinel);
|
||||
tmp.pc = funcAddr;
|
||||
|
||||
uint32_t steps = 0u;
|
||||
uint32_t lastPc = 0xFFFFFFFFu;
|
||||
uint32_t samePcCount = 0u;
|
||||
RpcInvokeExitReason exitReason = RpcInvokeExitReason::MissingFunction;
|
||||
while (tmp.pc != 0u &&
|
||||
tmp.pc != kRpcInvokeReturnSentinel &&
|
||||
runtime->hasFunction(tmp.pc) &&
|
||||
steps < kRpcInvokeMaxSteps)
|
||||
{
|
||||
const uint32_t pc = tmp.pc;
|
||||
if (pc == lastPc)
|
||||
{
|
||||
++samePcCount;
|
||||
if (samePcCount > 0x2000u)
|
||||
{
|
||||
exitReason = RpcInvokeExitReason::SamePcLimit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lastPc = pc;
|
||||
samePcCount = 0u;
|
||||
}
|
||||
|
||||
PS2Runtime::RecompiledFunction func = runtime->lookupFunction(pc);
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
func(rdram, &tmp, runtime);
|
||||
}
|
||||
++steps;
|
||||
}
|
||||
|
||||
if (outV0)
|
||||
{
|
||||
*outV0 = getRegU32(&tmp, 2);
|
||||
}
|
||||
|
||||
if (tmp.pc == kRpcInvokeReturnSentinel)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (tmp.pc == 0u)
|
||||
{
|
||||
exitReason = RpcInvokeExitReason::NullPc;
|
||||
}
|
||||
else if (steps >= kRpcInvokeMaxSteps)
|
||||
{
|
||||
exitReason = RpcInvokeExitReason::StepLimit;
|
||||
}
|
||||
else if (!runtime->hasFunction(tmp.pc))
|
||||
{
|
||||
exitReason = RpcInvokeExitReason::MissingFunction;
|
||||
}
|
||||
|
||||
static std::atomic<uint32_t> s_rpcInvokeFailureLogs{0u};
|
||||
constexpr uint32_t kMaxRpcInvokeFailureLogs = 64u;
|
||||
const uint32_t logIndex = s_rpcInvokeFailureLogs.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < kMaxRpcInvokeFailureLogs)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[SyscallOverride:invoke-failed]"
|
||||
<< " func=0x" << std::hex << funcAddr
|
||||
<< " exitPc=0x" << tmp.pc
|
||||
<< " ra=0x" << getRegU32(&tmp, 31)
|
||||
<< std::dec
|
||||
<< " steps=" << steps
|
||||
<< " reason=" << rpcInvokeExitReasonName(exitReason)
|
||||
<< std::endl;
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint32_t rpcAllocPacketAddr(uint8_t *rdram)
|
||||
{
|
||||
if (kRpcPacketPoolCount == 0)
|
||||
@@ -493,28 +105,6 @@ static uint32_t rpcAllocServerAddr(uint8_t *rdram)
|
||||
return addr;
|
||||
}
|
||||
|
||||
struct IrqHandlerInfo
|
||||
{
|
||||
int id = 0;
|
||||
uint32_t cause = 0;
|
||||
uint32_t handler = 0;
|
||||
uint32_t arg = 0;
|
||||
uint32_t gp = 0;
|
||||
uint32_t sp = 0;
|
||||
bool enabled = true;
|
||||
int order = 0;
|
||||
};
|
||||
|
||||
static std::unordered_map<int, IrqHandlerInfo> g_intcHandlers;
|
||||
static std::unordered_map<int, IrqHandlerInfo> g_dmacHandlers;
|
||||
static int g_nextIntcHandlerId = 1;
|
||||
static int g_nextDmacHandlerId = 1;
|
||||
|
||||
static int g_intc_head_order = 0;
|
||||
static int g_intc_tail_order = 1000;
|
||||
static int g_dmac_head_order = 0;
|
||||
static int g_dmac_tail_order = 1000;
|
||||
|
||||
inline std::string translatePs2Path(const char *ps2Path)
|
||||
{
|
||||
if (!ps2Path || !*ps2Path)
|
||||
|
||||
@@ -2,40 +2,10 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
|
||||
inline std::unordered_map<int, FILE *> g_fileDescriptors;
|
||||
inline int g_nextFd = 3; // Start after stdin, stdout, stderr
|
||||
|
||||
struct ThreadInfo
|
||||
{
|
||||
uint32_t entry = 0;
|
||||
uint32_t stack = 0;
|
||||
uint32_t stackSize = 0;
|
||||
uint32_t gp = 0;
|
||||
uint32_t priority = 0;
|
||||
uint32_t attr = 0;
|
||||
uint32_t option = 0;
|
||||
uint32_t arg = 0;
|
||||
bool started = false;
|
||||
bool ownsStack = false;
|
||||
uint32_t tlsBase = 0;
|
||||
|
||||
// Thread Status
|
||||
int status = 0x10; // THS_DORMANT
|
||||
int waitType = 0; // TSW_NONE
|
||||
int waitId = 0;
|
||||
int wakeupCount = 0;
|
||||
int currentPriority = 0;
|
||||
int suspendCount = 0;
|
||||
std::atomic<uint32_t> currentPc{0};
|
||||
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
std::atomic<bool> forceRelease{false};
|
||||
std::atomic<bool> terminated{false};
|
||||
};
|
||||
|
||||
// Thread status
|
||||
#define THS_RUN 0x01
|
||||
#define THS_READY 0x02
|
||||
@@ -138,6 +108,24 @@ struct ee_thread_status_t
|
||||
uint32_t wakeupCount; // 0x2C
|
||||
};
|
||||
|
||||
// PS2SDK EE kernel.h t_ee_thread. CreateThread consumes this full 0x24-byte
|
||||
// descriptor; it is not the attr-first IOP thread descriptor.
|
||||
struct ee_thread_t
|
||||
{
|
||||
int status;
|
||||
uint32_t func;
|
||||
uint32_t stack;
|
||||
int stack_size;
|
||||
uint32_t gp_reg;
|
||||
int initial_priority;
|
||||
int current_priority;
|
||||
uint32_t attr;
|
||||
uint32_t option;
|
||||
};
|
||||
|
||||
static_assert(sizeof(ee_thread_t) == 0x24u);
|
||||
static_assert(sizeof(ee_thread_status_t) == 0x30u);
|
||||
|
||||
struct ee_sema_t
|
||||
{
|
||||
int count;
|
||||
@@ -148,43 +136,7 @@ struct ee_sema_t
|
||||
uint32_t option;
|
||||
};
|
||||
|
||||
struct SemaInfo
|
||||
{
|
||||
int count = 0;
|
||||
int maxCount = 0;
|
||||
int initCount = 0;
|
||||
uint32_t attr = 0;
|
||||
uint32_t option = 0;
|
||||
int waiters = 0;
|
||||
bool deleted = false;
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
};
|
||||
|
||||
struct EventFlagInfo
|
||||
{
|
||||
uint32_t attr = 0;
|
||||
uint32_t option = 0;
|
||||
uint32_t initBits = 0;
|
||||
uint32_t bits = 0;
|
||||
int waiters = 0;
|
||||
bool deleted = false;
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
};
|
||||
|
||||
struct AlarmInfo
|
||||
{
|
||||
int id = 0;
|
||||
uint16_t ticks = 0;
|
||||
uint32_t handler = 0;
|
||||
uint32_t commonArg = 0;
|
||||
uint32_t gp = 0;
|
||||
uint32_t sp = 0;
|
||||
uint8_t *rdram = nullptr;
|
||||
PS2Runtime *runtime = nullptr;
|
||||
std::chrono::steady_clock::time_point dueAt;
|
||||
};
|
||||
static_assert(sizeof(ee_sema_t) == 0x18u);
|
||||
|
||||
struct io_stat_t
|
||||
{
|
||||
@@ -204,136 +156,8 @@ static constexpr uint32_t kFioSoIROth = 0x0004;
|
||||
static constexpr uint32_t kFioSoIWOth = 0x0002;
|
||||
static constexpr uint32_t kFioSoIXOth = 0x0001;
|
||||
|
||||
inline std::unordered_map<int, std::shared_ptr<ThreadInfo>> g_threads;
|
||||
inline int g_nextThreadId = 2; // Reserve 1 for the main thread
|
||||
inline thread_local int g_currentThreadId = 1;
|
||||
inline std::mutex g_thread_map_mutex;
|
||||
inline std::unordered_map<int, std::thread> g_hostThreads;
|
||||
inline std::mutex g_host_thread_mutex;
|
||||
|
||||
inline std::unordered_map<int, std::shared_ptr<SemaInfo>> g_semas;
|
||||
inline int g_nextSemaId = 1;
|
||||
inline std::mutex g_sema_map_mutex;
|
||||
inline std::unordered_map<int, std::shared_ptr<EventFlagInfo>> g_eventFlags;
|
||||
inline int g_nextEventFlagId = 1;
|
||||
inline std::mutex g_event_flag_map_mutex;
|
||||
inline std::unordered_map<int, std::shared_ptr<AlarmInfo>> g_alarms;
|
||||
inline int g_nextAlarmId = 1;
|
||||
inline std::mutex g_alarm_mutex;
|
||||
inline std::condition_variable g_alarm_cv;
|
||||
inline std::once_flag g_alarm_worker_once;
|
||||
inline std::atomic<int> g_activeThreads{0};
|
||||
inline std::mutex g_fd_mutex;
|
||||
|
||||
static void registerHostThread(int tid, std::thread worker)
|
||||
{
|
||||
std::thread stale;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_host_thread_mutex);
|
||||
auto it = g_hostThreads.find(tid);
|
||||
if (it != g_hostThreads.end())
|
||||
{
|
||||
stale = std::move(it->second);
|
||||
g_hostThreads.erase(it);
|
||||
}
|
||||
g_hostThreads.emplace(tid, std::move(worker));
|
||||
}
|
||||
|
||||
if (stale.joinable())
|
||||
{
|
||||
if (stale.get_id() == std::this_thread::get_id())
|
||||
{
|
||||
stale.detach();
|
||||
}
|
||||
else
|
||||
{
|
||||
stale.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void joinHostThreadById(int tid)
|
||||
{
|
||||
std::thread worker;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_host_thread_mutex);
|
||||
auto it = g_hostThreads.find(tid);
|
||||
if (it != g_hostThreads.end())
|
||||
{
|
||||
worker = std::move(it->second);
|
||||
g_hostThreads.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
if (!worker.joinable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (worker.get_id() == std::this_thread::get_id())
|
||||
{
|
||||
worker.detach();
|
||||
}
|
||||
else
|
||||
{
|
||||
worker.join();
|
||||
}
|
||||
}
|
||||
|
||||
static void joinAllHostThreads()
|
||||
{
|
||||
std::vector<std::thread> workers;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_host_thread_mutex);
|
||||
workers.reserve(g_hostThreads.size());
|
||||
const std::thread::id selfId = std::this_thread::get_id();
|
||||
for (auto it = g_hostThreads.begin(); it != g_hostThreads.end();)
|
||||
{
|
||||
std::thread &worker = it->second;
|
||||
if (worker.joinable() && worker.get_id() == selfId)
|
||||
{
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
|
||||
workers.push_back(std::move(worker));
|
||||
it = g_hostThreads.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &worker : workers)
|
||||
{
|
||||
if (!worker.joinable())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
worker.join();
|
||||
}
|
||||
}
|
||||
|
||||
static void detachAllHostThreads()
|
||||
{
|
||||
std::vector<std::thread> workers;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_host_thread_mutex);
|
||||
workers.reserve(g_hostThreads.size());
|
||||
for (auto &entry : g_hostThreads)
|
||||
{
|
||||
workers.push_back(std::move(entry.second));
|
||||
}
|
||||
g_hostThreads.clear();
|
||||
}
|
||||
|
||||
for (auto &worker : workers)
|
||||
{
|
||||
if (!worker.joinable())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
worker.detach();
|
||||
}
|
||||
}
|
||||
|
||||
struct RpcServerState
|
||||
{
|
||||
uint32_t sid = 0;
|
||||
@@ -397,24 +221,11 @@ inline uint32_t g_rpc_next_id = 1;
|
||||
inline uint32_t g_rpc_packet_index = 0;
|
||||
inline uint32_t g_rpc_server_index = 0;
|
||||
inline uint32_t g_rpc_active_queue = 0;
|
||||
struct ExitHandlerEntry
|
||||
{
|
||||
uint32_t func = 0;
|
||||
uint32_t arg = 0;
|
||||
};
|
||||
|
||||
inline std::mutex g_exit_handler_mutex;
|
||||
inline std::unordered_map<int, std::vector<ExitHandlerEntry>> g_exit_handlers;
|
||||
|
||||
inline std::mutex g_bootmode_mutex;
|
||||
inline bool g_bootmode_initialized = false;
|
||||
inline uint32_t g_bootmode_pool_offset = 0;
|
||||
inline std::unordered_map<uint8_t, uint32_t> g_bootmode_addresses;
|
||||
|
||||
inline std::mutex g_syscall_override_mutex;
|
||||
inline std::unordered_map<uint32_t, uint32_t> g_syscall_overrides;
|
||||
inline std::unordered_set<uint32_t> g_syscall_mirror_addrs;
|
||||
|
||||
static constexpr uint32_t kGuestSyscallTableGuestBase = 0x80011F80u;
|
||||
static constexpr uint32_t kGuestSyscallTablePhysBase = kGuestSyscallTableGuestBase & 0x1FFFFFFFu;
|
||||
static constexpr uint32_t kGuestSyscallMirrorLimit = 0x00080000u;
|
||||
|
||||
@@ -1,538 +1,103 @@
|
||||
#include "Common.h"
|
||||
#include "Interrupt.h"
|
||||
#include "ps2_log.h"
|
||||
#include "Stubs/GS.h"
|
||||
|
||||
namespace ps2_syscalls
|
||||
{
|
||||
namespace interrupt_state
|
||||
namespace
|
||||
{
|
||||
constexpr uint32_t kIntcVblankStart = 2u;
|
||||
constexpr uint32_t kIntcVblankEnd = 3u;
|
||||
constexpr auto kVblankPeriod = std::chrono::microseconds(16667);
|
||||
constexpr int kMaxCatchupTicks = 4;
|
||||
constexpr uint32_t kMaxIrqHandlerSteps = 4096u;
|
||||
|
||||
std::mutex g_irq_handler_mutex;
|
||||
std::mutex g_irq_worker_mutex;
|
||||
std::condition_variable g_irq_worker_cv;
|
||||
std::mutex g_vsync_flag_mutex;
|
||||
std::condition_variable g_vsync_cv;
|
||||
std::atomic<bool> g_irq_worker_stop{false};
|
||||
std::atomic<bool> g_irq_worker_running{false};
|
||||
uint32_t g_enabled_intc_mask = 0xFFFFFFFFu;
|
||||
uint32_t g_enabled_dmac_mask = 0xFFFFFFFFu;
|
||||
uint64_t g_vsync_tick_counter = 0u;
|
||||
VSyncFlagRegistration g_vsync_registration{};
|
||||
}
|
||||
|
||||
using namespace interrupt_state;
|
||||
|
||||
static void writeGuestU32NoThrow(uint8_t *rdram, uint32_t addr, uint32_t value)
|
||||
{
|
||||
if (addr == 0u)
|
||||
EeScheduler &scheduler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
return;
|
||||
EeScheduler &result = runtime->eeScheduler();
|
||||
result.bindMainContextForSyscall(*ctx, rdram);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t *dst = getMemPtr(rdram, addr);
|
||||
if (!dst)
|
||||
void setCauseEnabled(uint8_t *rdram,
|
||||
R5900Context *ctx,
|
||||
PS2Runtime *runtime,
|
||||
bool dmac,
|
||||
bool enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
std::memcpy(dst, &value, sizeof(value));
|
||||
}
|
||||
|
||||
static void writeGuestU64NoThrow(uint8_t *rdram, uint32_t addr, uint64_t value)
|
||||
{
|
||||
if (addr == 0u)
|
||||
{
|
||||
return;
|
||||
setReturnS32(ctx,
|
||||
scheduler(rdram, ctx, runtime)
|
||||
.setIrqCauseEnabled(dmac, getRegU32(ctx, 4), enabled));
|
||||
}
|
||||
|
||||
uint8_t *dst = getMemPtr(rdram, addr);
|
||||
if (!dst)
|
||||
void addHandler(uint8_t *rdram,
|
||||
R5900Context *ctx,
|
||||
PS2Runtime *runtime,
|
||||
bool dmac)
|
||||
{
|
||||
return;
|
||||
}
|
||||
std::memcpy(dst, &value, sizeof(value));
|
||||
}
|
||||
|
||||
static uint32_t readGuestU32NoThrow(uint8_t *rdram, uint32_t addr)
|
||||
{
|
||||
if (addr == 0u)
|
||||
{
|
||||
return 0u;
|
||||
const int id = scheduler(rdram, ctx, runtime)
|
||||
.addIrqHandler(dmac,
|
||||
getRegU32(ctx, 4),
|
||||
getRegU32(ctx, 5),
|
||||
getRegU32(ctx, 6) != 0u,
|
||||
getRegU32(ctx, 7),
|
||||
getRegU32(ctx, 28),
|
||||
getRegU32(ctx, 29));
|
||||
setReturnS32(ctx, id);
|
||||
}
|
||||
|
||||
uint8_t *src = getMemPtr(rdram, addr);
|
||||
if (!src)
|
||||
void removeHandler(uint8_t *rdram,
|
||||
R5900Context *ctx,
|
||||
PS2Runtime *runtime,
|
||||
bool dmac)
|
||||
{
|
||||
return 0u;
|
||||
setReturnS32(ctx,
|
||||
scheduler(rdram, ctx, runtime)
|
||||
.removeIrqHandler(dmac,
|
||||
getRegU32(ctx, 4),
|
||||
static_cast<int>(getRegU32(ctx, 5))));
|
||||
}
|
||||
|
||||
uint32_t value = 0u;
|
||||
std::memcpy(&value, src, sizeof(value));
|
||||
return value;
|
||||
}
|
||||
|
||||
static uint32_t getAsyncHandlerStackTop(PS2Runtime *runtime)
|
||||
{
|
||||
constexpr uint32_t kAsyncHandlerStackSize = 0x4000u;
|
||||
thread_local PS2Runtime *s_cachedRuntime = nullptr;
|
||||
thread_local uint32_t s_cachedStackTop = 0u;
|
||||
|
||||
if (runtime == nullptr)
|
||||
void setHandlerEnabled(uint8_t *rdram,
|
||||
R5900Context *ctx,
|
||||
PS2Runtime *runtime,
|
||||
bool dmac,
|
||||
bool enabled)
|
||||
{
|
||||
return PS2_RAM_SIZE - 0x10u;
|
||||
}
|
||||
|
||||
if (s_cachedRuntime != runtime || s_cachedStackTop == 0u)
|
||||
{
|
||||
s_cachedRuntime = runtime;
|
||||
s_cachedStackTop = runtime->reserveAsyncCallbackStack(kAsyncHandlerStackSize, 16u);
|
||||
}
|
||||
|
||||
return (s_cachedStackTop != 0u) ? s_cachedStackTop : (PS2_RAM_SIZE - 0x10u);
|
||||
}
|
||||
|
||||
static void dispatchIntcHandlersForCause(uint8_t *rdram, PS2Runtime *runtime, uint32_t cause)
|
||||
{
|
||||
if (!rdram || !runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<IrqHandlerInfo> handlers;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (cause < 32u && (g_enabled_intc_mask & (1u << cause)) == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handlers.reserve(g_intcHandlers.size());
|
||||
for (const auto &[id, info] : g_intcHandlers)
|
||||
{
|
||||
(void)id;
|
||||
if (!info.enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (info.cause != cause)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (info.handler == 0u)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
handlers.push_back(info);
|
||||
}
|
||||
std::sort(handlers.begin(), handlers.end(), [](const IrqHandlerInfo &a, const IrqHandlerInfo &b)
|
||||
{ return a.order < b.order; });
|
||||
}
|
||||
|
||||
for (const IrqHandlerInfo &info : handlers)
|
||||
{
|
||||
if (!runtime->hasFunction(info.handler))
|
||||
{
|
||||
if (cause == kIntcVblankStart)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
static std::atomic<uint32_t> s_missingHandlerLogCount{0u};
|
||||
const uint32_t logIndex = s_missingHandlerLogCount.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < 32u)
|
||||
{
|
||||
auto flags = std::cout.flags();
|
||||
std::cout << "[INTC:missing] cause=" << cause
|
||||
<< " handler=0x" << std::hex << info.handler
|
||||
<< std::dec
|
||||
<< " id=" << info.id
|
||||
<< std::endl;
|
||||
std::cout.flags(flags);
|
||||
}
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
R5900Context irqCtx{};
|
||||
SET_GPR_U32(&irqCtx, 28, info.gp);
|
||||
SET_GPR_U32(&irqCtx, 29, getAsyncHandlerStackTop(runtime));
|
||||
SET_GPR_U32(&irqCtx, 31, 0u);
|
||||
SET_GPR_U32(&irqCtx, 4, cause);
|
||||
SET_GPR_U32(&irqCtx, 5, info.arg);
|
||||
SET_GPR_U32(&irqCtx, 6, 0u);
|
||||
SET_GPR_U32(&irqCtx, 7, 0u);
|
||||
irqCtx.pc = info.handler;
|
||||
|
||||
bool reschedulePending = false;
|
||||
uint64_t handoffBaseline = 0u;
|
||||
uint32_t steps = 0u;
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
PS2Runtime::DeferredGuestYieldScope deferYield(reschedulePending);
|
||||
|
||||
while (irqCtx.pc != 0u && runtime && !runtime->isStopRequested() && steps < kMaxIrqHandlerSteps)
|
||||
{
|
||||
PS2Runtime::RecompiledFunction step = runtime->lookupFunction(irqCtx.pc);
|
||||
if (!step)
|
||||
{
|
||||
break;
|
||||
}
|
||||
step(rdram, &irqCtx, runtime);
|
||||
++steps;
|
||||
}
|
||||
handoffBaseline = runtime->guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
if (steps >= kMaxIrqHandlerSteps)
|
||||
{
|
||||
static uint32_t s_stepLimitLogCount = 0u;
|
||||
if (s_stepLimitLogCount < 16u)
|
||||
{
|
||||
std::cerr << "[INTC:step-limit] handler=0x" << std::hex << info.handler << " pc=0x" << irqCtx.pc << std::dec << std::endl;
|
||||
++s_stepLimitLogCount;
|
||||
}
|
||||
}
|
||||
if (reschedulePending && !runtime->isStopRequested())
|
||||
{
|
||||
runtime->waitForGuestExecutionHandoff(handoffBaseline);
|
||||
}
|
||||
}
|
||||
catch (const ThreadExitException &)
|
||||
{
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
static uint32_t warnCount = 0;
|
||||
if (warnCount < 8u)
|
||||
{
|
||||
std::cerr << "[INTC] handler 0x" << std::hex << info.handler
|
||||
<< " threw exception: " << e.what() << std::dec << std::endl;
|
||||
++warnCount;
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx,
|
||||
scheduler(rdram, ctx, runtime)
|
||||
.setIrqHandlerEnabled(dmac,
|
||||
static_cast<int>(getRegU32(ctx, 5)),
|
||||
enabled));
|
||||
}
|
||||
}
|
||||
|
||||
void dispatchDmacHandlersForCause(uint8_t *rdram, PS2Runtime *runtime, uint32_t cause)
|
||||
void dispatchDmacHandlersForCause(uint8_t *, PS2Runtime *runtime, uint32_t cause)
|
||||
{
|
||||
if (!rdram || !runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<IrqHandlerInfo> handlers;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (cause < 32u && (g_enabled_dmac_mask & (1u << cause)) == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handlers.reserve(g_dmacHandlers.size());
|
||||
for (const auto &[id, info] : g_dmacHandlers)
|
||||
{
|
||||
(void)id;
|
||||
if (!info.enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (info.cause != cause)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (info.handler == 0u)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
handlers.push_back(info);
|
||||
}
|
||||
std::sort(handlers.begin(), handlers.end(), [](const IrqHandlerInfo &a, const IrqHandlerInfo &b)
|
||||
{ return a.order < b.order; });
|
||||
}
|
||||
|
||||
for (const IrqHandlerInfo &info : handlers)
|
||||
{
|
||||
if (!runtime->hasFunction(info.handler))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
R5900Context irqCtx{};
|
||||
SET_GPR_U32(&irqCtx, 28, info.gp);
|
||||
SET_GPR_U32(&irqCtx, 29, getAsyncHandlerStackTop(runtime));
|
||||
SET_GPR_U32(&irqCtx, 31, 0u);
|
||||
SET_GPR_U32(&irqCtx, 4, cause);
|
||||
SET_GPR_U32(&irqCtx, 5, info.arg);
|
||||
SET_GPR_U32(&irqCtx, 6, 0u);
|
||||
SET_GPR_U32(&irqCtx, 7, 0u);
|
||||
irqCtx.pc = info.handler;
|
||||
|
||||
bool reschedulePending = false;
|
||||
uint64_t handoffBaseline = 0u;
|
||||
uint32_t steps = 0u;
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
PS2Runtime::DeferredGuestYieldScope deferYield(reschedulePending);
|
||||
|
||||
while (irqCtx.pc != 0u && runtime && !runtime->isStopRequested() &&
|
||||
steps < kMaxIrqHandlerSteps)
|
||||
{
|
||||
PS2Runtime::RecompiledFunction step = runtime->lookupFunction(irqCtx.pc);
|
||||
if (!step)
|
||||
{
|
||||
break;
|
||||
}
|
||||
step(rdram, &irqCtx, runtime);
|
||||
++steps;
|
||||
}
|
||||
handoffBaseline = runtime->guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
if (steps >= kMaxIrqHandlerSteps)
|
||||
{
|
||||
static uint32_t s_stepLimitLogCount = 0u;
|
||||
if (s_stepLimitLogCount < 16u)
|
||||
{
|
||||
std::cerr << "[DMAC:step-limit] handler=0x" << std::hex << info.handler
|
||||
<< " pc=0x" << irqCtx.pc << std::dec << std::endl;
|
||||
++s_stepLimitLogCount;
|
||||
}
|
||||
}
|
||||
if (reschedulePending && !runtime->isStopRequested())
|
||||
{
|
||||
runtime->waitForGuestExecutionHandoff(handoffBaseline);
|
||||
}
|
||||
}
|
||||
catch (const ThreadExitException &)
|
||||
{
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
static uint32_t warnCount = 0;
|
||||
if (warnCount < 8u)
|
||||
{
|
||||
std::cerr << "[DMAC] handler 0x" << std::hex << info.handler
|
||||
<< " threw exception: " << e.what() << std::dec << std::endl;
|
||||
++warnCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
runtime->eeScheduler().dispatchIrq(true, cause);
|
||||
}
|
||||
|
||||
static void updateGsCsrFieldForVSync(PS2Runtime *runtime, uint64_t tickValue)
|
||||
uint64_t GetCurrentVSyncTick(PS2Runtime *runtime)
|
||||
{
|
||||
if (!runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr uint64_t kGsCsrFieldMask = 0x2000ull;
|
||||
std::atomic<uint64_t> &csr = runtime->memory().gs().csr;
|
||||
if (tickValue & 1ull)
|
||||
{
|
||||
csr.fetch_or(kGsCsrFieldMask);
|
||||
}
|
||||
else
|
||||
{
|
||||
csr.fetch_and(~kGsCsrFieldMask);
|
||||
}
|
||||
return runtime->eeScheduler().currentVSyncTick();
|
||||
}
|
||||
|
||||
static uint64_t signalVSyncFlag(uint8_t *rdram, PS2Runtime *runtime)
|
||||
void WaitVSyncTick(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime, int fixedResult)
|
||||
{
|
||||
VSyncFlagRegistration reg{};
|
||||
uint64_t tickValue = 0u;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_vsync_flag_mutex);
|
||||
reg = g_vsync_registration;
|
||||
g_vsync_registration = {};
|
||||
tickValue = ++g_vsync_tick_counter;
|
||||
}
|
||||
|
||||
g_vsync_cv.notify_all();
|
||||
updateGsCsrFieldForVSync(runtime, tickValue);
|
||||
|
||||
if (reg.flagAddr != 0u)
|
||||
{
|
||||
writeGuestU32NoThrow(rdram, reg.flagAddr, 1u);
|
||||
}
|
||||
if (reg.tickAddr != 0u)
|
||||
{
|
||||
writeGuestU64NoThrow(rdram, reg.tickAddr, tickValue);
|
||||
}
|
||||
return tickValue;
|
||||
}
|
||||
|
||||
static void interruptWorkerMain(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
g_currentThreadId = -1;
|
||||
|
||||
using clock = std::chrono::steady_clock;
|
||||
auto nextTick = clock::now() + kVblankPeriod;
|
||||
|
||||
while (runtime != nullptr && !runtime->isStopRequested())
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_irq_worker_mutex);
|
||||
if (g_irq_worker_cv.wait_until(lock, nextTick, []()
|
||||
{ return g_irq_worker_stop.load(std::memory_order_acquire); }))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const auto now = clock::now();
|
||||
int ticksToProcess = 0;
|
||||
while (now >= nextTick && ticksToProcess < kMaxCatchupTicks)
|
||||
{
|
||||
++ticksToProcess;
|
||||
nextTick += kVblankPeriod;
|
||||
}
|
||||
if (ticksToProcess == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ticksToProcess; ++i)
|
||||
{
|
||||
bool reschedulePending = false;
|
||||
uint64_t handoffBaseline = 0u;
|
||||
{
|
||||
PS2Runtime::GuestExecutionScope guestExecution(runtime);
|
||||
PS2Runtime::DeferredGuestYieldScope deferYield(reschedulePending);
|
||||
const uint64_t tickValue = signalVSyncFlag(rdram, runtime);
|
||||
ps2_stubs::dispatchGsSyncVCallback(rdram, runtime, tickValue);
|
||||
dispatchIntcHandlersForCause(rdram, runtime, kIntcVblankStart);
|
||||
handoffBaseline = runtime->guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
if (reschedulePending && !runtime->isStopRequested())
|
||||
{
|
||||
runtime->waitForGuestExecutionHandoff(handoffBaseline);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(500));
|
||||
dispatchIntcHandlersForCause(rdram, runtime, kIntcVblankEnd);
|
||||
}
|
||||
}
|
||||
|
||||
g_irq_worker_running.store(false, std::memory_order_release);
|
||||
g_irq_worker_cv.notify_all();
|
||||
}
|
||||
|
||||
static void ensureInterruptWorkerRunning(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
if (!rdram || !runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_irq_worker_mutex);
|
||||
if (g_irq_worker_running.load(std::memory_order_acquire))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
g_irq_worker_stop.store(false, std::memory_order_release);
|
||||
g_irq_worker_running.store(true, std::memory_order_release);
|
||||
try
|
||||
{
|
||||
std::thread(interruptWorkerMain, rdram, runtime).detach();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
g_irq_worker_running.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void EnsureVSyncWorkerRunning(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
ensureInterruptWorkerRunning(rdram, runtime);
|
||||
}
|
||||
|
||||
uint64_t GetCurrentVSyncTick()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_vsync_flag_mutex);
|
||||
return g_vsync_tick_counter;
|
||||
}
|
||||
|
||||
void stopInterruptWorker()
|
||||
{
|
||||
g_irq_worker_stop.store(true, std::memory_order_release);
|
||||
g_irq_worker_cv.notify_all();
|
||||
std::unique_lock<std::mutex> lock(g_irq_worker_mutex);
|
||||
g_irq_worker_cv.wait_for(lock, std::chrono::milliseconds(500), []()
|
||||
{ return !g_irq_worker_running.load(std::memory_order_acquire); });
|
||||
g_vsync_cv.notify_all();
|
||||
}
|
||||
|
||||
uint64_t WaitForNextVSyncTick(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
ensureInterruptWorkerRunning(rdram, runtime);
|
||||
std::unique_lock<std::mutex> lock(g_vsync_flag_mutex);
|
||||
uint64_t current = g_vsync_tick_counter;
|
||||
uint64_t result = current;
|
||||
waitWithGuestExecutionReleasedUntilUnlocked(
|
||||
runtime,
|
||||
lock,
|
||||
[&]()
|
||||
{
|
||||
g_vsync_cv.wait(lock, [current, runtime]()
|
||||
{ return g_vsync_tick_counter > current || (runtime != nullptr && runtime->isStopRequested()); });
|
||||
},
|
||||
[&]()
|
||||
{
|
||||
result = g_vsync_tick_counter;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
void WaitVSyncTick(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
(void)WaitForNextVSyncTick(rdram, runtime);
|
||||
EeScheduler &ee = scheduler(rdram, ctx, runtime);
|
||||
ee.waitVSync(ee.currentVSyncTick(), fixedResult);
|
||||
}
|
||||
|
||||
void SetVSyncFlag(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t flagAddr = getRegU32(ctx, 4);
|
||||
const uint32_t tickAddr = getRegU32(ctx, 5);
|
||||
|
||||
const uint32_t flagAddress = getRegU32(ctx, 4);
|
||||
const uint32_t tickAddress = getRegU32(ctx, 5);
|
||||
if ((flagAddress != 0u && !getEeGuestStruct<uint32_t>(rdram, flagAddress)) ||
|
||||
(tickAddress != 0u && !getEeGuestStruct<uint64_t>(rdram, tickAddress)))
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_vsync_flag_mutex);
|
||||
g_vsync_registration.flagAddr = flagAddr;
|
||||
g_vsync_registration.tickAddr = tickAddr;
|
||||
setReturnS32(ctx, KE_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
writeGuestU32NoThrow(rdram, flagAddr, 0u);
|
||||
writeGuestU64NoThrow(rdram, tickAddr, 0u);
|
||||
ensureInterruptWorkerRunning(rdram, runtime);
|
||||
scheduler(rdram, ctx, runtime).setVSyncFlag(flagAddress, tickAddress);
|
||||
setReturnS32(ctx, KE_OK);
|
||||
}
|
||||
|
||||
void EnableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
if (cause < 32u)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
g_enabled_intc_mask |= (1u << cause);
|
||||
}
|
||||
if (cause == kIntcVblankStart || cause == kIntcVblankEnd)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
static std::atomic<uint32_t> s_enableLogCount{0u};
|
||||
const uint32_t logIndex = s_enableLogCount.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < 32u)
|
||||
{
|
||||
RUNTIME_LOG("[EnableIntc] cause=" << cause);
|
||||
}
|
||||
});
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setCauseEnabled(rdram, ctx, runtime, false, true);
|
||||
}
|
||||
|
||||
void iEnableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -542,24 +107,7 @@ namespace ps2_syscalls
|
||||
|
||||
void DisableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
if (cause < 32u)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
g_enabled_intc_mask &= ~(1u << cause);
|
||||
}
|
||||
if (cause == kIntcVblankStart || cause == kIntcVblankEnd)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
static std::atomic<uint32_t> s_disableLogCount{0u};
|
||||
const uint32_t logIndex = s_disableLogCount.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < 32u)
|
||||
{
|
||||
RUNTIME_LOG("[DisableIntc] cause=" << cause);
|
||||
}
|
||||
});
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setCauseEnabled(rdram, ctx, runtime, false, false);
|
||||
}
|
||||
|
||||
void iDisableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -569,47 +117,7 @@ namespace ps2_syscalls
|
||||
|
||||
void AddIntcHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
IrqHandlerInfo info{};
|
||||
info.cause = getRegU32(ctx, 4);
|
||||
info.handler = getRegU32(ctx, 5);
|
||||
uint32_t next = getRegU32(ctx, 6);
|
||||
info.arg = getRegU32(ctx, 7);
|
||||
info.gp = getRegU32(ctx, 28);
|
||||
info.sp = getRegU32(ctx, 29);
|
||||
info.enabled = true;
|
||||
|
||||
int handlerId = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
info.order = (next == 0) ? --g_intc_head_order : ++g_intc_tail_order;
|
||||
handlerId = g_nextIntcHandlerId++;
|
||||
info.id = handlerId;
|
||||
g_intcHandlers[handlerId] = info;
|
||||
}
|
||||
|
||||
if (info.cause == kIntcVblankStart)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
static std::atomic<uint32_t> s_addHandlerLogCount{0u};
|
||||
const uint32_t logIndex = s_addHandlerLogCount.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < 32u)
|
||||
{
|
||||
auto flags = std::cout.flags();
|
||||
std::cout << "[AddIntcHandler] cause=" << info.cause
|
||||
<< " handler=0x" << std::hex << info.handler
|
||||
<< " arg=0x" << info.arg
|
||||
<< " gp=0x" << info.gp
|
||||
<< " sp=0x" << info.sp
|
||||
<< std::dec
|
||||
<< " id=" << handlerId
|
||||
<< std::endl;
|
||||
std::cout.flags(flags);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ensureInterruptWorkerRunning(rdram, runtime);
|
||||
setReturnS32(ctx, handlerId);
|
||||
addHandler(rdram, ctx, runtime, false);
|
||||
}
|
||||
|
||||
void AddIntcHandler2(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -619,40 +127,12 @@ namespace ps2_syscalls
|
||||
|
||||
void RemoveIntcHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
if (handlerId > 0)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
auto it = g_intcHandlers.find(handlerId);
|
||||
if (it != g_intcHandlers.end() && it->second.cause == cause)
|
||||
{
|
||||
g_intcHandlers.erase(it);
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
removeHandler(rdram, ctx, runtime, false);
|
||||
}
|
||||
|
||||
void AddDmacHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
IrqHandlerInfo info{};
|
||||
info.cause = getRegU32(ctx, 4);
|
||||
info.handler = getRegU32(ctx, 5);
|
||||
uint32_t next = getRegU32(ctx, 6);
|
||||
info.arg = getRegU32(ctx, 7);
|
||||
info.gp = getRegU32(ctx, 28);
|
||||
info.sp = getRegU32(ctx, 29);
|
||||
info.enabled = true;
|
||||
|
||||
int handlerId = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
info.order = (next == 0) ? --g_dmac_head_order : ++g_dmac_tail_order;
|
||||
handlerId = g_nextDmacHandlerId++;
|
||||
info.id = handlerId;
|
||||
g_dmacHandlers[handlerId] = info;
|
||||
}
|
||||
setReturnS32(ctx, handlerId);
|
||||
addHandler(rdram, ctx, runtime, true);
|
||||
}
|
||||
|
||||
void AddDmacHandler2(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -662,81 +142,32 @@ namespace ps2_syscalls
|
||||
|
||||
void RemoveDmacHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
if (handlerId > 0)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
auto it = g_dmacHandlers.find(handlerId);
|
||||
if (it != g_dmacHandlers.end() && it->second.cause == cause)
|
||||
{
|
||||
g_dmacHandlers.erase(it);
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
removeHandler(rdram, ctx, runtime, true);
|
||||
}
|
||||
|
||||
void EnableIntcHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (auto it = g_intcHandlers.find(handlerId); it != g_intcHandlers.end())
|
||||
{
|
||||
it->second.enabled = true;
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setHandlerEnabled(rdram, ctx, runtime, false, true);
|
||||
}
|
||||
|
||||
void DisableIntcHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (auto it = g_intcHandlers.find(handlerId); it != g_intcHandlers.end())
|
||||
{
|
||||
it->second.enabled = false;
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setHandlerEnabled(rdram, ctx, runtime, false, false);
|
||||
}
|
||||
|
||||
void EnableDmacHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (auto it = g_dmacHandlers.find(handlerId); it != g_dmacHandlers.end())
|
||||
{
|
||||
it->second.enabled = true;
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setHandlerEnabled(rdram, ctx, runtime, true, true);
|
||||
}
|
||||
|
||||
void DisableDmacHandler(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const int handlerId = static_cast<int>(getRegU32(ctx, 5));
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
if (auto it = g_dmacHandlers.find(handlerId); it != g_dmacHandlers.end())
|
||||
{
|
||||
it->second.enabled = false;
|
||||
}
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setHandlerEnabled(rdram, ctx, runtime, true, false);
|
||||
}
|
||||
|
||||
void EnableDmac(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
if (cause < 32u)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
g_enabled_dmac_mask |= (1u << cause);
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setCauseEnabled(rdram, ctx, runtime, true, true);
|
||||
}
|
||||
|
||||
void iEnableDmac(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -746,13 +177,7 @@ namespace ps2_syscalls
|
||||
|
||||
void DisableDmac(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
const uint32_t cause = getRegU32(ctx, 4);
|
||||
if (cause < 32u)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
g_enabled_dmac_mask &= ~(1u << cause);
|
||||
}
|
||||
setReturnS32(ctx, KE_OK);
|
||||
setCauseEnabled(rdram, ctx, runtime, true, false);
|
||||
}
|
||||
|
||||
void iDisableDmac(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
|
||||
@@ -1,37 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include "ps2_syscalls.h"
|
||||
|
||||
namespace ps2_syscalls
|
||||
{
|
||||
namespace interrupt_state
|
||||
{
|
||||
struct VSyncFlagRegistration
|
||||
{
|
||||
uint32_t flagAddr;
|
||||
uint32_t tickAddr;
|
||||
};
|
||||
|
||||
extern std::mutex g_irq_handler_mutex;
|
||||
extern std::mutex g_irq_worker_mutex;
|
||||
extern std::condition_variable g_irq_worker_cv;
|
||||
extern std::mutex g_vsync_flag_mutex;
|
||||
extern std::condition_variable g_vsync_cv;
|
||||
extern std::atomic<bool> g_irq_worker_stop;
|
||||
extern std::atomic<bool> g_irq_worker_running;
|
||||
extern uint32_t g_enabled_intc_mask;
|
||||
extern uint32_t g_enabled_dmac_mask;
|
||||
extern uint64_t g_vsync_tick_counter;
|
||||
extern VSyncFlagRegistration g_vsync_registration;
|
||||
}
|
||||
|
||||
void dispatchDmacHandlersForCause(uint8_t *rdram, PS2Runtime *runtime, uint32_t cause);
|
||||
void EnsureVSyncWorkerRunning(uint8_t *rdram, PS2Runtime *runtime);
|
||||
uint64_t GetCurrentVSyncTick();
|
||||
void stopInterruptWorker();
|
||||
uint64_t WaitForNextVSyncTick(uint8_t *rdram, PS2Runtime *runtime);
|
||||
void WaitVSyncTick(uint8_t *rdram, PS2Runtime *runtime);
|
||||
uint64_t GetCurrentVSyncTick(PS2Runtime *runtime);
|
||||
void WaitVSyncTick(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime, int fixedResult);
|
||||
void SetVSyncFlag(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void EnableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void iEnableIntc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
#include "Common.h"
|
||||
#include "Interrupt.h"
|
||||
#include "Lifecycle.h"
|
||||
|
||||
namespace ps2_syscalls
|
||||
{
|
||||
using namespace interrupt_state;
|
||||
|
||||
void notifyRuntimeStop()
|
||||
{
|
||||
stopInterruptWorker();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_irq_handler_mutex);
|
||||
g_intcHandlers.clear();
|
||||
g_dmacHandlers.clear();
|
||||
g_nextIntcHandlerId = 1;
|
||||
g_nextDmacHandlerId = 1;
|
||||
g_enabled_intc_mask = 0xFFFFFFFFu;
|
||||
g_enabled_dmac_mask = 0xFFFFFFFFu;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_vsync_flag_mutex);
|
||||
g_vsync_registration = {};
|
||||
g_vsync_tick_counter = 0u;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ThreadInfo>> threads;
|
||||
threads.reserve(32);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_thread_map_mutex);
|
||||
for (const auto &entry : g_threads)
|
||||
{
|
||||
if (entry.second)
|
||||
{
|
||||
threads.push_back(entry.second);
|
||||
}
|
||||
}
|
||||
g_threads.clear();
|
||||
g_nextThreadId = 2; // Reserve id 1 for main thread.
|
||||
}
|
||||
g_currentThreadId = 1;
|
||||
|
||||
for (const auto &threadInfo : threads)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(threadInfo->m);
|
||||
threadInfo->forceRelease = true;
|
||||
threadInfo->terminated = true;
|
||||
}
|
||||
threadInfo->cv.notify_all();
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<SemaInfo>> semas;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_sema_map_mutex);
|
||||
semas.reserve(g_semas.size());
|
||||
for (const auto &entry : g_semas)
|
||||
{
|
||||
if (entry.second)
|
||||
{
|
||||
semas.push_back(entry.second);
|
||||
}
|
||||
}
|
||||
g_semas.clear();
|
||||
g_nextSemaId = 1;
|
||||
}
|
||||
for (const auto &sema : semas)
|
||||
{
|
||||
sema->cv.notify_all();
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<EventFlagInfo>> eventFlags;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_event_flag_map_mutex);
|
||||
eventFlags.reserve(g_eventFlags.size());
|
||||
for (const auto &entry : g_eventFlags)
|
||||
{
|
||||
if (entry.second)
|
||||
{
|
||||
eventFlags.push_back(entry.second);
|
||||
}
|
||||
}
|
||||
g_eventFlags.clear();
|
||||
g_nextEventFlagId = 1;
|
||||
}
|
||||
for (const auto &eventFlag : eventFlags)
|
||||
{
|
||||
eventFlag->cv.notify_all();
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_alarm_mutex);
|
||||
g_alarms.clear();
|
||||
}
|
||||
g_alarm_cv.notify_all();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_exit_handler_mutex);
|
||||
g_exit_handlers.clear();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_syscall_override_mutex);
|
||||
g_syscall_overrides.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void joinAllGuestHostThreads()
|
||||
{
|
||||
joinAllHostThreads();
|
||||
}
|
||||
|
||||
void detachAllGuestHostThreads()
|
||||
{
|
||||
detachAllHostThreads();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ps2_syscalls.h"
|
||||
|
||||
namespace ps2_syscalls
|
||||
{
|
||||
void notifyRuntimeStop();
|
||||
void joinAllGuestHostThreads();
|
||||
void detachAllGuestHostThreads();
|
||||
}
|
||||
@@ -6,13 +6,13 @@ namespace ps2_syscalls
|
||||
{
|
||||
namespace
|
||||
{
|
||||
SifRpcDebugEvent makeRpcDebugEvent(const char *op, R5900Context *ctx)
|
||||
SifRpcDebugEvent makeRpcDebugEvent(const char *op, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
SifRpcDebugEvent event{};
|
||||
event.op = op;
|
||||
event.pc = ctx ? ctx->pc : 0u;
|
||||
event.ra = ctx ? getRegU32(ctx, 31) : 0u;
|
||||
event.threadId = static_cast<uint32_t>(g_currentThreadId);
|
||||
event.threadId = runtime ? static_cast<uint32_t>(runtime->eeScheduler().currentThreadId()) : 0u;
|
||||
return event;
|
||||
}
|
||||
|
||||
@@ -139,34 +139,13 @@ namespace ps2_syscalls
|
||||
}
|
||||
#endif
|
||||
|
||||
bool signalRpcCompletionSema(uint32_t semaId)
|
||||
bool signalRpcCompletionSema(PS2Runtime *runtime, uint32_t semaId)
|
||||
{
|
||||
if (semaId == 0u || semaId > 0xFFFFu)
|
||||
if (!runtime || semaId == 0u || semaId > 0xFFFFu)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto sema = lookupSemaInfo(static_cast<int>(semaId));
|
||||
if (!sema)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool signaled = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(sema->m);
|
||||
if (!sema->deleted && sema->count < sema->maxCount)
|
||||
{
|
||||
sema->count++;
|
||||
signaled = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (signaled)
|
||||
{
|
||||
sema->cv.notify_one();
|
||||
}
|
||||
return signaled;
|
||||
return runtime->eeScheduler().signalSemaphore(static_cast<int>(semaId), true) >= 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -261,7 +240,7 @@ namespace ps2_syscalls
|
||||
RUNTIME_LOG("[SifInitRpc] Initialized");
|
||||
}
|
||||
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("InitRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("InitRpc", ctx, runtime);
|
||||
event.result = 0;
|
||||
pushSifRpcDebugEventLocked(event);
|
||||
setReturnS32(ctx, 0);
|
||||
@@ -277,7 +256,7 @@ namespace ps2_syscalls
|
||||
|
||||
if (!client)
|
||||
{
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("BindRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("BindRpc", ctx, runtime);
|
||||
event.clientPtr = clientPtr;
|
||||
event.sid = rpcId;
|
||||
event.mode = mode;
|
||||
@@ -342,7 +321,7 @@ namespace ps2_syscalls
|
||||
client->cbuf = 0;
|
||||
}
|
||||
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("BindRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("BindRpc", ctx, runtime);
|
||||
event.clientPtr = clientPtr;
|
||||
event.serverPtr = serverPtr;
|
||||
event.sid = rpcId;
|
||||
@@ -473,7 +452,7 @@ namespace ps2_syscalls
|
||||
auto *client = reinterpret_cast<t_SifRpcClientData *>(getMemPtr(rdram, clientPtr));
|
||||
if (!client)
|
||||
{
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("CallRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("CallRpc", ctx, runtime);
|
||||
event.clientPtr = clientPtr;
|
||||
event.sid = sidHint;
|
||||
event.rpcNum = rpcNum;
|
||||
@@ -535,24 +514,12 @@ namespace ps2_syscalls
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t resultPointer = 0u;
|
||||
bool handled = false;
|
||||
bool handledByIop = false;
|
||||
bool serverDispatched = false;
|
||||
bool callbackCompleted = false;
|
||||
bool copiedFallback = false;
|
||||
bool zeroedFallback = false;
|
||||
ps2x::iop::RpcResult iopResult{};
|
||||
|
||||
const auto completionSemaphore = [&]()
|
||||
uint32_t completionSemaphore = static_cast<uint32_t>(client->hdr.sema_id);
|
||||
if (completionSemaphore == 0xFFFFFFFFu || completionSemaphore == 0u)
|
||||
{
|
||||
uint32_t semaphore = static_cast<uint32_t>(client->hdr.sema_id);
|
||||
if (semaphore == 0xFFFFFFFFu || semaphore == 0u)
|
||||
{
|
||||
semaphore = endParameter;
|
||||
}
|
||||
return semaphore;
|
||||
};
|
||||
completionSemaphore = endParameter;
|
||||
}
|
||||
|
||||
{
|
||||
ps2x::iop::RpcRequest request{};
|
||||
@@ -569,171 +536,162 @@ namespace ps2_syscalls
|
||||
request.endParameter = endParameter;
|
||||
|
||||
iopResult = PS2IopTransport::handleRpc(runtime, rdram, ctx, request);
|
||||
handled = iopResult.handled;
|
||||
handledByIop = iopResult.handled;
|
||||
resultPointer = iopResult.resultAddress;
|
||||
|
||||
if (iopResult.signalNowaitCompletion &&
|
||||
(mode & kSifRpcModeNowait) != 0u)
|
||||
{
|
||||
(void)signalRpcCompletionSema(completionSemaphore());
|
||||
(void)signalRpcCompletionSema(runtime, completionSemaphore);
|
||||
}
|
||||
if (iopResult.signalCompletion)
|
||||
{
|
||||
(void)signalRpcCompletionSema(completionSemaphore());
|
||||
(void)signalRpcCompletionSema(runtime, completionSemaphore);
|
||||
}
|
||||
}
|
||||
|
||||
if (server && server->func != 0u && iopResult.serverDispatchPolicy != ps2x::iop::ServerDispatchPolicy::Suppress)
|
||||
uint32_t guestFunction = iopResult.guestFunction;
|
||||
uint32_t guestA0 = iopResult.guestArguments[0];
|
||||
uint32_t guestA1 = iopResult.guestArguments[1];
|
||||
uint32_t guestA2 = iopResult.guestArguments[2];
|
||||
uint32_t guestA3 = iopResult.guestArguments[3];
|
||||
uint32_t guestDefaultResult = iopResult.guestDefaultResultAddress;
|
||||
if (guestFunction == 0u && server && server->func != 0u &&
|
||||
iopResult.serverDispatchPolicy != ps2x::iop::ServerDispatchPolicy::Suppress)
|
||||
{
|
||||
uint32_t serverResult = 0u;
|
||||
serverDispatched = rpcInvokeFunction(rdram,
|
||||
ctx,
|
||||
runtime,
|
||||
server->func,
|
||||
rpcNum,
|
||||
server->buf,
|
||||
sendSize,
|
||||
0u,
|
||||
&serverResult);
|
||||
if (serverDispatched)
|
||||
guestFunction = server->func;
|
||||
guestA0 = rpcNum;
|
||||
guestA1 = server->buf;
|
||||
guestA2 = sendSize;
|
||||
guestDefaultResult = server->buf != 0u ? server->buf : receiveBuffer;
|
||||
}
|
||||
const bool serverDispatched = guestFunction != 0u && runtime->hasFunction(guestFunction);
|
||||
|
||||
auto finishCall = [=](const R5900Context *guestResult, R5900Context &parent)
|
||||
{
|
||||
bool handled = iopResult.handled;
|
||||
uint32_t resultPointer = iopResult.resultAddress;
|
||||
bool copiedFallback = false;
|
||||
bool zeroedFallback = false;
|
||||
if (guestResult)
|
||||
{
|
||||
handled = true;
|
||||
resultPointer = serverResult;
|
||||
if (resultPointer == 0u && server->buf != 0u)
|
||||
{
|
||||
resultPointer = server->buf;
|
||||
}
|
||||
resultPointer = getRegU32(guestResult, 2);
|
||||
if (resultPointer == 0u)
|
||||
{
|
||||
resultPointer = receiveBuffer;
|
||||
resultPointer = guestDefaultResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (receiveBuffer != 0u && receiveSize != 0u)
|
||||
{
|
||||
if (handled && resultPointer != 0u && resultPointer != receiveBuffer)
|
||||
if (receiveBuffer != 0u && receiveSize != 0u)
|
||||
{
|
||||
rpcCopyToRdram(rdram,
|
||||
receiveBuffer,
|
||||
resultPointer,
|
||||
receiveSize);
|
||||
}
|
||||
else if (!handled && sendBuf != 0u && sendSize != 0u && sendBuf != receiveBuffer)
|
||||
{
|
||||
const uint32_t copySize = std::min(sendSize, receiveSize);
|
||||
rpcCopyToRdram(rdram, receiveBuffer, sendBuf, copySize);
|
||||
copiedFallback = true;
|
||||
}
|
||||
else if (!handled)
|
||||
{
|
||||
rpcZeroRdram(rdram, receiveBuffer, receiveSize);
|
||||
zeroedFallback = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (endFunction != 0u)
|
||||
{
|
||||
if (iopResult.callbackPolicy == ps2x::iop::CallbackPolicy::Suppress)
|
||||
{
|
||||
callbackCompleted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
callbackCompleted = rpcInvokeFunction(rdram,
|
||||
ctx,
|
||||
runtime,
|
||||
endFunction,
|
||||
endParameter,
|
||||
0u,
|
||||
0u,
|
||||
0u,
|
||||
nullptr);
|
||||
if (!callbackCompleted && endFunction >= 0x10000u)
|
||||
if (handled && resultPointer != 0u && resultPointer != receiveBuffer)
|
||||
{
|
||||
const uint32_t normalizedEndFunction = endFunction - 0x10000u;
|
||||
if (runtime->hasFunction(normalizedEndFunction))
|
||||
{
|
||||
callbackCompleted = rpcInvokeFunction(
|
||||
rdram,
|
||||
ctx,
|
||||
runtime,
|
||||
normalizedEndFunction,
|
||||
endParameter,
|
||||
0u,
|
||||
0u,
|
||||
0u,
|
||||
nullptr);
|
||||
}
|
||||
rpcCopyToRdram(rdram, receiveBuffer, resultPointer, receiveSize);
|
||||
}
|
||||
}
|
||||
|
||||
if (!callbackCompleted)
|
||||
{
|
||||
const bool signaled = signalRpcCompletionSema(completionSemaphore());
|
||||
static uint32_t unresolvedCallbackWarnings = 0u;
|
||||
if (unresolvedCallbackWarnings < 32u)
|
||||
else if (!handled && sendBuf != 0u && sendSize != 0u && sendBuf != receiveBuffer)
|
||||
{
|
||||
std::cerr
|
||||
<< "[SifCallRpc] unresolved end callback endFunc=0x"
|
||||
<< std::hex << endFunction
|
||||
<< " semaId=0x" << completionSemaphore()
|
||||
<< " fallbackSignal=" << std::dec
|
||||
<< (signaled ? 1 : 0) << std::endl;
|
||||
++unresolvedCallbackWarnings;
|
||||
rpcCopyToRdram(rdram, receiveBuffer, sendBuf, std::min(sendSize, receiveSize));
|
||||
copiedFallback = true;
|
||||
}
|
||||
else if (!handled)
|
||||
{
|
||||
rpcZeroRdram(rdram, receiveBuffer, receiveSize);
|
||||
zeroedFallback = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_rpc_mutex);
|
||||
g_rpc_clients[clientPtr].busy = false;
|
||||
}
|
||||
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("CallRpc", ctx);
|
||||
event.clientPtr = clientPtr;
|
||||
event.serverPtr = serverPtr;
|
||||
event.sid = sid;
|
||||
event.rpcNum = rpcNum;
|
||||
event.mode = mode;
|
||||
event.sendBuf = sendBuf;
|
||||
event.sendSize = sendSize;
|
||||
event.recvBuf = receiveBuffer;
|
||||
event.recvSize = receiveSize;
|
||||
event.resultPtr = resultPointer;
|
||||
event.endFunc = endFunction;
|
||||
event.endParam = endParameter;
|
||||
event.semaId = static_cast<uint32_t>(client->hdr.sema_id);
|
||||
event.flags =
|
||||
((mode & kSifRpcModeNowait) ? kSifRpcDebugFlagNowait : 0u) |
|
||||
(handledByIop ? kSifRpcDebugFlagHandledByHle : 0u) |
|
||||
(callbackCompleted ? kSifRpcDebugFlagCallback : 0u) |
|
||||
(serverDispatched ? kSifRpcDebugFlagServerDispatch : 0u) |
|
||||
(!handled ? kSifRpcDebugFlagUnhandled : 0u) |
|
||||
(copiedFallback ? kSifRpcDebugFlagFallbackCopy : 0u) |
|
||||
(zeroedFallback ? kSifRpcDebugFlagFallbackZero : 0u);
|
||||
fillRpcDebugPreview(rdram,
|
||||
sendBuf,
|
||||
sendSize,
|
||||
event.sendPreview,
|
||||
event.sendPreviewSize);
|
||||
fillRpcDebugPreview(rdram,
|
||||
receiveBuffer,
|
||||
receiveSize,
|
||||
event.recvPreview,
|
||||
event.recvPreviewSize);
|
||||
event.result = 0;
|
||||
|
||||
auto completeClient = [=](R5900Context &base, bool callbackCompleted)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_rpc_mutex);
|
||||
g_rpc_clients[clientPtr].busy = false;
|
||||
}
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("CallRpc", &base, runtime);
|
||||
event.clientPtr = clientPtr;
|
||||
event.serverPtr = serverPtr;
|
||||
event.sid = sid;
|
||||
event.rpcNum = rpcNum;
|
||||
event.mode = mode;
|
||||
event.sendBuf = sendBuf;
|
||||
event.sendSize = sendSize;
|
||||
event.recvBuf = receiveBuffer;
|
||||
event.recvSize = receiveSize;
|
||||
event.resultPtr = resultPointer;
|
||||
event.endFunc = endFunction;
|
||||
event.endParam = endParameter;
|
||||
event.semaId = completionSemaphore;
|
||||
event.flags =
|
||||
((mode & kSifRpcModeNowait) ? kSifRpcDebugFlagNowait : 0u) |
|
||||
(iopResult.handled ? kSifRpcDebugFlagHandledByHle : 0u) |
|
||||
(callbackCompleted ? kSifRpcDebugFlagCallback : 0u) |
|
||||
(serverDispatched ? kSifRpcDebugFlagServerDispatch : 0u) |
|
||||
(!handled ? kSifRpcDebugFlagUnhandled : 0u) |
|
||||
(copiedFallback ? kSifRpcDebugFlagFallbackCopy : 0u) |
|
||||
(zeroedFallback ? kSifRpcDebugFlagFallbackZero : 0u);
|
||||
fillRpcDebugPreview(rdram, sendBuf, sendSize, event.sendPreview, event.sendPreviewSize);
|
||||
fillRpcDebugPreview(rdram, receiveBuffer, receiveSize, event.recvPreview, event.recvPreviewSize);
|
||||
event.result = 0;
|
||||
#if PS2X_ENABLE_IOP_RPC_TRACE
|
||||
if ((event.flags & kSifRpcDebugFlagUnhandled) != 0u)
|
||||
{
|
||||
logUnhandledRpcTrace(event);
|
||||
}
|
||||
if ((event.flags & kSifRpcDebugFlagUnhandled) != 0u)
|
||||
{
|
||||
logUnhandledRpcTrace(event);
|
||||
}
|
||||
#endif
|
||||
pushSifRpcDebugEvent(event);
|
||||
pushSifRpcDebugEvent(event);
|
||||
};
|
||||
|
||||
setReturnS32(ctx, 0);
|
||||
setReturnS32(&parent, 0);
|
||||
if (endFunction == 0u || iopResult.callbackPolicy == ps2x::iop::CallbackPolicy::Suppress)
|
||||
{
|
||||
completeClient(parent, endFunction != 0u);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t callbackFunction = endFunction;
|
||||
if (!runtime->hasFunction(callbackFunction) && callbackFunction >= 0x10000u &&
|
||||
runtime->hasFunction(callbackFunction - 0x10000u))
|
||||
{
|
||||
callbackFunction -= 0x10000u;
|
||||
}
|
||||
if (!runtime->hasFunction(callbackFunction))
|
||||
{
|
||||
(void)signalRpcCompletionSema(runtime, completionSemaphore);
|
||||
completeClient(parent, false);
|
||||
return;
|
||||
}
|
||||
|
||||
GuestInvocation callback{};
|
||||
callback.kind = GuestInvocationKind::RpcCallback;
|
||||
callback.context = parent;
|
||||
callback.context.pc = callbackFunction;
|
||||
SET_GPR_U32(&callback.context, 4, endParameter);
|
||||
SET_GPR_U32(&callback.context, 29, runtime->eeScheduler().invocationStackTop());
|
||||
SET_GPR_U32(&callback.context, 31, 0u);
|
||||
callback.onComplete = [completeClient](const R5900Context &, R5900Context &base)
|
||||
{
|
||||
completeClient(base, true);
|
||||
};
|
||||
runtime->eeScheduler().invokeCurrent(std::move(callback));
|
||||
};
|
||||
|
||||
if (serverDispatched)
|
||||
{
|
||||
GuestInvocation invocation{};
|
||||
invocation.kind = GuestInvocationKind::RpcCallback;
|
||||
invocation.context = *ctx;
|
||||
invocation.context.pc = guestFunction;
|
||||
SET_GPR_U32(&invocation.context, 4, guestA0);
|
||||
SET_GPR_U32(&invocation.context, 5, guestA1);
|
||||
SET_GPR_U32(&invocation.context, 6, guestA2);
|
||||
SET_GPR_U32(&invocation.context, 7, guestA3);
|
||||
SET_GPR_U32(&invocation.context, 29, runtime->eeScheduler().invocationStackTop());
|
||||
SET_GPR_U32(&invocation.context, 31, 0u);
|
||||
invocation.onComplete = [finishCall](const R5900Context &completed, R5900Context &parent)
|
||||
{
|
||||
finishCall(&completed, parent);
|
||||
};
|
||||
runtime->eeScheduler().invokeCurrent(std::move(invocation));
|
||||
}
|
||||
finishCall(nullptr, *ctx);
|
||||
}
|
||||
|
||||
void SifRegisterRpc(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
@@ -754,7 +712,7 @@ namespace ps2_syscalls
|
||||
t_SifRpcServerData *sd = reinterpret_cast<t_SifRpcServerData *>(getMemPtr(rdram, sdPtr));
|
||||
if (!sd)
|
||||
{
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("RegisterRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("RegisterRpc", ctx, runtime);
|
||||
event.serverPtr = sdPtr;
|
||||
event.sid = sid;
|
||||
event.sendBuf = buf;
|
||||
@@ -836,7 +794,7 @@ namespace ps2_syscalls
|
||||
}
|
||||
|
||||
RUNTIME_LOG("[SifRegisterRpc] sid=0x" << std::hex << sid << " sd=0x" << sdPtr << std::dec);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("RegisterRpc", ctx);
|
||||
SifRpcDebugEvent event = makeRpcDebugEvent("RegisterRpc", ctx, runtime);
|
||||
event.serverPtr = sdPtr;
|
||||
event.sid = sid;
|
||||
event.sendBuf = buf;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -405,145 +405,41 @@ namespace ps2_syscalls
|
||||
setReturnS32(ctx, 0);
|
||||
}
|
||||
|
||||
static uint32_t computeBuiltinFindAddressResult(uint8_t *rdram,
|
||||
uint32_t originalStart,
|
||||
uint32_t originalEnd,
|
||||
uint32_t target);
|
||||
|
||||
bool dispatchSyscallOverride(uint32_t syscallNumber, uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
uint32_t handler = 0u;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_syscall_override_mutex);
|
||||
auto it = g_syscall_overrides.find(syscallNumber);
|
||||
if (it == g_syscall_overrides.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
handler = it->second;
|
||||
}
|
||||
|
||||
if (!runtime || !ctx || handler == 0u)
|
||||
if (!runtime || !ctx ||
|
||||
!runtime->findEeSyscallOverride(syscallNumber, handler) ||
|
||||
handler == 0u)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t overrideA0 = getRegU32(ctx, 4);
|
||||
const uint32_t overrideA1 = getRegU32(ctx, 5);
|
||||
const uint32_t overrideA2 = getRegU32(ctx, 6);
|
||||
const uint32_t overrideA3 = getRegU32(ctx, 7);
|
||||
const uint32_t overridePc = ctx->pc;
|
||||
const uint32_t overrideRa = getRegU32(ctx, 31);
|
||||
|
||||
thread_local std::vector<uint32_t> s_activeSyscallOverrides;
|
||||
if (std::find(s_activeSyscallOverrides.begin(), s_activeSyscallOverrides.end(), syscallNumber) != s_activeSyscallOverrides.end())
|
||||
EeScheduler &scheduler = runtime->eeScheduler();
|
||||
scheduler.bindMainContextForSyscall(*ctx, rdram);
|
||||
if (scheduler.hasInvocation(GuestInvocationKind::SyscallOverride, syscallNumber))
|
||||
{
|
||||
static std::atomic<uint32_t> s_reentrantLogs{0u};
|
||||
constexpr uint32_t kMaxReentrantLogs = 32u;
|
||||
const uint32_t logIndex = s_reentrantLogs.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < kMaxReentrantLogs)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[SyscallOverride:reentrant]"
|
||||
<< " syscall=0x" << std::hex << syscallNumber
|
||||
<< " handler=0x" << handler
|
||||
<< " pc=0x" << ctx->pc
|
||||
<< " ra=0x" << getRegU32(ctx, 31)
|
||||
<< std::dec << std::endl;
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
s_activeSyscallOverrides.push_back(syscallNumber);
|
||||
struct ScopedActiveOverride
|
||||
if (!runtime->hasFunction(handler))
|
||||
{
|
||||
std::vector<uint32_t> &active;
|
||||
~ScopedActiveOverride()
|
||||
{
|
||||
if (!active.empty())
|
||||
{
|
||||
active.pop_back();
|
||||
}
|
||||
}
|
||||
} scopedActiveOverride{s_activeSyscallOverrides};
|
||||
|
||||
uint32_t retV0 = 0u;
|
||||
const bool invoked = rpcInvokeFunction(rdram,
|
||||
ctx,
|
||||
runtime,
|
||||
handler,
|
||||
getRegU32(ctx, 4),
|
||||
getRegU32(ctx, 5),
|
||||
getRegU32(ctx, 6),
|
||||
getRegU32(ctx, 7),
|
||||
&retV0);
|
||||
|
||||
if (syscallNumber == 0x83u)
|
||||
{
|
||||
const uint32_t builtinRet = computeBuiltinFindAddressResult(rdram, overrideA0, overrideA1, overrideA2);
|
||||
const bool mismatch = (retV0 != builtinRet);
|
||||
|
||||
static std::atomic<uint32_t> s_findAddressOverrideLogs{0u};
|
||||
static std::atomic<uint32_t> s_findAddressOverrideMismatchLogs{0u};
|
||||
constexpr uint32_t kMaxFindAddressOverrideLogs = 64u;
|
||||
constexpr uint32_t kMaxFindAddressOverrideMismatchLogs = 128u;
|
||||
|
||||
const uint32_t logIndex = s_findAddressOverrideLogs.fetch_add(1u, std::memory_order_relaxed);
|
||||
const uint32_t mismatchIndex = mismatch
|
||||
? s_findAddressOverrideMismatchLogs.fetch_add(1u, std::memory_order_relaxed)
|
||||
: 0u;
|
||||
if (logIndex < kMaxFindAddressOverrideLogs ||
|
||||
(mismatch && mismatchIndex < kMaxFindAddressOverrideMismatchLogs))
|
||||
{
|
||||
const uint32_t guestMinus20c = (retV0 != 0u) ? (retV0 - 0x20Cu) : 0u;
|
||||
const uint32_t guestMinus168 = (retV0 != 0u) ? (retV0 - 0x168u) : 0u;
|
||||
const uint32_t builtinMinus20c = (builtinRet != 0u) ? (builtinRet - 0x20Cu) : 0u;
|
||||
const uint32_t builtinMinus168 = (builtinRet != 0u) ? (builtinRet - 0x168u) : 0u;
|
||||
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[Syscall83:override]"
|
||||
<< " handler=0x" << std::hex << handler
|
||||
<< " invoked=" << (invoked ? "true" : "false")
|
||||
<< " pc=0x" << overridePc
|
||||
<< " ra=0x" << overrideRa
|
||||
<< " a0=0x" << overrideA0
|
||||
<< " a1=0x" << overrideA1
|
||||
<< " a2=0x" << overrideA2
|
||||
<< " a3=0x" << overrideA3
|
||||
<< " guestRet=0x" << retV0
|
||||
<< " builtinRet=0x" << builtinRet
|
||||
<< " guest-20c=0x" << guestMinus20c
|
||||
<< " builtin-20c=0x" << builtinMinus20c
|
||||
<< " guest-168=0x" << guestMinus168
|
||||
<< " builtin-168=0x" << builtinMinus168
|
||||
<< " match=" << (mismatch ? "false" : "true")
|
||||
<< std::dec << std::endl;
|
||||
});
|
||||
}
|
||||
setReturnS32(ctx, KE_ERROR);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!invoked)
|
||||
GuestInvocation invocation{};
|
||||
invocation.kind = GuestInvocationKind::SyscallOverride;
|
||||
invocation.tag = syscallNumber;
|
||||
invocation.context = *ctx;
|
||||
invocation.context.pc = handler;
|
||||
SET_GPR_U32(&invocation.context, 29, scheduler.invocationStackTop());
|
||||
SET_GPR_U32(&invocation.context, 31, 0u);
|
||||
invocation.onComplete = [](const R5900Context &completed, R5900Context &parent)
|
||||
{
|
||||
static std::atomic<uint32_t> s_fallbackLogs{0u};
|
||||
constexpr uint32_t kMaxFallbackLogs = 64u;
|
||||
const uint32_t logIndex = s_fallbackLogs.fetch_add(1u, std::memory_order_relaxed);
|
||||
if (logIndex < kMaxFallbackLogs)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[SyscallOverride:fallback]"
|
||||
<< " syscall=0x" << std::hex << syscallNumber
|
||||
<< " handler=0x" << handler
|
||||
<< " pc=0x" << ctx->pc
|
||||
<< " ra=0x" << getRegU32(ctx, 31)
|
||||
<< std::dec << std::endl;
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
setReturnU32(ctx, retV0);
|
||||
return true;
|
||||
parent.r[2] = completed.r[2];
|
||||
};
|
||||
scheduler.invokeCurrent(std::move(invocation));
|
||||
}
|
||||
|
||||
static bool tryResolveGuestSyscallMirrorAddr(uint32_t syscallIndex, uint32_t &guestAddr)
|
||||
@@ -573,73 +469,20 @@ namespace ps2_syscalls
|
||||
}
|
||||
}
|
||||
|
||||
static void seedGuestSyscallTableProbeLocked(uint8_t *rdram)
|
||||
void initializeGuestKernelState(uint8_t *rdram, PS2Runtime *runtime)
|
||||
{
|
||||
writeGuestKernelWord(rdram, kGuestSyscallTableProbeBase + 0u, kGuestSyscallTableGuestBase >> 16);
|
||||
writeGuestKernelWord(rdram, kGuestSyscallTableProbeBase + 8u, kGuestSyscallTableGuestBase & 0xFFFFu);
|
||||
g_syscall_mirror_addrs.insert(kGuestSyscallTableProbeBase + 0u);
|
||||
g_syscall_mirror_addrs.insert(kGuestSyscallTableProbeBase + 8u);
|
||||
}
|
||||
|
||||
static void mirrorGuestSyscallEntryLocked(uint8_t *rdram, uint32_t syscallIndex, uint32_t handler)
|
||||
{
|
||||
uint32_t guestAddr = 0u;
|
||||
if (!tryResolveGuestSyscallMirrorAddr(syscallIndex, guestAddr))
|
||||
if (!runtime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writeGuestKernelWord(rdram, guestAddr, handler);
|
||||
if (handler == 0u)
|
||||
{
|
||||
g_syscall_mirror_addrs.erase(guestAddr);
|
||||
return;
|
||||
}
|
||||
|
||||
g_syscall_mirror_addrs.insert(guestAddr);
|
||||
}
|
||||
|
||||
void initializeGuestKernelState(uint8_t *rdram)
|
||||
{
|
||||
if (!rdram)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_syscall_override_mutex);
|
||||
for (uint32_t guestAddr : g_syscall_mirror_addrs)
|
||||
{
|
||||
writeGuestKernelWord(rdram, guestAddr, 0u);
|
||||
}
|
||||
g_syscall_mirror_addrs.clear();
|
||||
|
||||
seedGuestSyscallTableProbeLocked(rdram);
|
||||
|
||||
for (const auto &entry : g_syscall_overrides)
|
||||
{
|
||||
mirrorGuestSyscallEntryLocked(rdram, entry.first, entry.second);
|
||||
}
|
||||
runtime->initializeEeKernelState(rdram);
|
||||
}
|
||||
|
||||
void SetSyscall(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
(void)runtime;
|
||||
const uint32_t syscallIndex = getRegU32(ctx, 4);
|
||||
const uint32_t handler = getRegU32(ctx, 5);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_syscall_override_mutex);
|
||||
if (handler == 0u)
|
||||
{
|
||||
g_syscall_overrides.erase(syscallIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_syscall_overrides[syscallIndex] = handler;
|
||||
}
|
||||
|
||||
mirrorGuestSyscallEntryLocked(rdram, syscallIndex, handler);
|
||||
}
|
||||
runtime->setEeSyscallOverride(rdram, syscallIndex, handler);
|
||||
|
||||
setReturnS32(ctx, 0);
|
||||
}
|
||||
@@ -1091,7 +934,9 @@ namespace ps2_syscalls
|
||||
// GetThreadTLS (stub): return 0
|
||||
void GetThreadTLS(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
|
||||
{
|
||||
auto info = ensureCurrentThreadInfo(ctx);
|
||||
EeScheduler &ee = runtime->eeScheduler();
|
||||
ee.bindMainContextForSyscall(*ctx, rdram);
|
||||
GuestThread *info = ee.currentThread();
|
||||
if (!info)
|
||||
{
|
||||
setReturnU32(ctx, 0);
|
||||
@@ -1149,11 +994,10 @@ namespace ps2_syscalls
|
||||
return;
|
||||
}
|
||||
|
||||
int tid = g_currentThreadId;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_exit_handler_mutex);
|
||||
g_exit_handlers[tid].push_back({func, arg});
|
||||
}
|
||||
EeScheduler &ee = runtime->eeScheduler();
|
||||
ee.bindMainContextForSyscall(*ctx, rdram);
|
||||
const int tid = ee.currentThreadId();
|
||||
runtime->addEeExitHandler(tid, func, arg);
|
||||
|
||||
setReturnS32(ctx, 0);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace ps2_syscalls
|
||||
void sceSifLoadModule(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void sceSifLoadModuleBuffer(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void TODO(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime, uint32_t encodedSyscallId);
|
||||
void initializeGuestKernelState(uint8_t *rdram);
|
||||
void initializeGuestKernelState(uint8_t *rdram, PS2Runtime *runtime);
|
||||
void SetSyscall(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void SetupThread(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
void SetupHeap(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@
|
||||
#include "ps2_runtime.h"
|
||||
#include "ps2_runtime_macros.h"
|
||||
#include "ps2_log.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
|
||||
#include <unordered_set>
|
||||
#include "Kernel/Syscalls/Helpers/State.h"
|
||||
@@ -32,39 +33,45 @@
|
||||
namespace
|
||||
{
|
||||
#if defined(PS2X_ENABLE_DEBUG_UI) && !defined(PLATFORM_VITA)
|
||||
const char *threadStatusName(int status)
|
||||
const char *threadStatusName(EeThreadStatus status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case THS_RUN:
|
||||
case EeThreadStatus::Running:
|
||||
return "RUN";
|
||||
case THS_READY:
|
||||
case EeThreadStatus::Ready:
|
||||
return "READY";
|
||||
case THS_WAIT:
|
||||
case EeThreadStatus::Waiting:
|
||||
return "WAIT";
|
||||
case THS_SUSPEND:
|
||||
case EeThreadStatus::Suspended:
|
||||
return "SUSPEND";
|
||||
case THS_WAITSUSPEND:
|
||||
case EeThreadStatus::WaitingSuspended:
|
||||
return "WAITSUSP";
|
||||
case THS_DORMANT:
|
||||
case EeThreadStatus::Dormant:
|
||||
return "DORMANT";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
const char *waitTypeName(int waitType)
|
||||
const char *waitTypeName(EeWaitReason waitType)
|
||||
{
|
||||
switch (waitType)
|
||||
{
|
||||
case TSW_NONE:
|
||||
case EeWaitReason::None:
|
||||
return "NONE";
|
||||
case TSW_SLEEP:
|
||||
case EeWaitReason::Sleep:
|
||||
return "SLEEP";
|
||||
case TSW_SEMA:
|
||||
case EeWaitReason::Semaphore:
|
||||
return "SEMA";
|
||||
case TSW_EVENT:
|
||||
case EeWaitReason::EventFlag:
|
||||
return "EVENT";
|
||||
case EeWaitReason::VSync:
|
||||
return "VSYNC";
|
||||
case EeWaitReason::External:
|
||||
return "EXTERNAL";
|
||||
case EeWaitReason::Mpeg:
|
||||
return "MPEG";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
@@ -698,7 +705,7 @@ namespace
|
||||
const uint32_t gp = runtime.m_debugGp.load(std::memory_order_relaxed);
|
||||
|
||||
ImGui::Text("Runtime: %s", runtime.isStopRequested() ? "stop requested" : "running");
|
||||
ImGui::Text("Guest execution waiters: %u", runtime.guestExecutionWaiterCountForTesting());
|
||||
ImGui::Text("EE executor: %s", runtime.eeScheduler().isExecutingGuest() ? "guest" : "scheduler");
|
||||
ImGui::Separator();
|
||||
textHex32("PC", pc);
|
||||
ImGui::SameLine();
|
||||
@@ -756,60 +763,11 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
void drawThreadsTab()
|
||||
void drawThreadsTab(PS2Runtime &runtime)
|
||||
{
|
||||
struct ThreadRow
|
||||
{
|
||||
int id = 0;
|
||||
uint32_t entry = 0;
|
||||
uint32_t stack = 0;
|
||||
uint32_t stackSize = 0;
|
||||
uint32_t gp = 0;
|
||||
uint32_t priority = 0;
|
||||
int status = 0;
|
||||
int waitType = 0;
|
||||
int waitId = 0;
|
||||
int currentPriority = 0;
|
||||
int wakeupCount = 0;
|
||||
int suspendCount = 0;
|
||||
uint32_t currentPc = 0u;
|
||||
bool terminated = false;
|
||||
};
|
||||
|
||||
std::vector<ThreadRow> rows;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_thread_map_mutex);
|
||||
rows.reserve(g_threads.size());
|
||||
for (const auto &[id, ptr] : g_threads)
|
||||
{
|
||||
if (!ptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
ThreadRow row{};
|
||||
row.id = id;
|
||||
{
|
||||
std::lock_guard<std::mutex> threadLock(ptr->m);
|
||||
row.entry = ptr->entry;
|
||||
row.stack = ptr->stack;
|
||||
row.stackSize = ptr->stackSize;
|
||||
row.gp = ptr->gp;
|
||||
row.priority = ptr->priority;
|
||||
row.currentPriority = ptr->currentPriority;
|
||||
row.status = ptr->status;
|
||||
row.waitType = ptr->waitType;
|
||||
row.waitId = ptr->waitId;
|
||||
row.wakeupCount = ptr->wakeupCount;
|
||||
row.suspendCount = ptr->suspendCount;
|
||||
}
|
||||
row.currentPc = ptr->currentPc.load(std::memory_order_relaxed);
|
||||
row.terminated = ptr->terminated.load(std::memory_order_relaxed);
|
||||
rows.push_back(row);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Text("Threads: %zu activeThreads=%d", rows.size(), g_activeThreads.load(std::memory_order_relaxed));
|
||||
if (ImGui::BeginTable("threads", 12, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY, ImVec2(0, 320)))
|
||||
const EeKernelSnapshot snapshot = runtime.eeScheduler().snapshot();
|
||||
ImGui::Text("Threads: %zu running=%d", snapshot.threads.size(), snapshot.runningThreadId);
|
||||
if (ImGui::BeginTable("threads", 11, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY, ImVec2(0, 320)))
|
||||
{
|
||||
ImGui::TableSetupColumn("ID");
|
||||
ImGui::TableSetupColumn("Status");
|
||||
@@ -822,9 +780,8 @@ namespace
|
||||
ImGui::TableSetupColumn("Prio");
|
||||
ImGui::TableSetupColumn("Wake");
|
||||
ImGui::TableSetupColumn("Susp");
|
||||
ImGui::TableSetupColumn("Term");
|
||||
ImGui::TableHeadersRow();
|
||||
for (const ThreadRow &row : rows)
|
||||
for (const EeThreadSnapshot &row : snapshot.threads)
|
||||
{
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
@@ -832,11 +789,11 @@ namespace
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", threadStatusName(row.status));
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", waitTypeName(row.waitType));
|
||||
ImGui::Text("%s", waitTypeName(row.waitReason));
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", row.waitId);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", row.currentPc);
|
||||
ImGui::Text("0x%08X", row.pc);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", row.entry);
|
||||
ImGui::TableNextColumn();
|
||||
@@ -844,23 +801,21 @@ namespace
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", row.gp);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d/%u", row.currentPriority, row.priority);
|
||||
ImGui::Text("%d/%d", row.currentPriority, row.initialPriority);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", row.wakeupCount);
|
||||
ImGui::Text("%u", row.wakeupCount);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", row.suspendCount);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%u", row.terminated ? 1u : 0u);
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
}
|
||||
|
||||
void drawKernelTab()
|
||||
void drawKernelTab(PS2Runtime &runtime)
|
||||
{
|
||||
const EeKernelSnapshot snapshot = runtime.eeScheduler().snapshot();
|
||||
ImGui::SeparatorText("Semaphores");
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_sema_map_mutex);
|
||||
if (ImGui::BeginTable("semas", 7, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable))
|
||||
{
|
||||
ImGui::TableSetupColumn("ID");
|
||||
@@ -871,26 +826,23 @@ namespace
|
||||
ImGui::TableSetupColumn("Attr");
|
||||
ImGui::TableSetupColumn("Deleted");
|
||||
ImGui::TableHeadersRow();
|
||||
for (const auto &[id, sema] : g_semas)
|
||||
for (const EeSemaphoreSnapshot &sema : snapshot.semaphores)
|
||||
{
|
||||
if (!sema)
|
||||
continue;
|
||||
std::lock_guard<std::mutex> semaLock(sema->m);
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", id);
|
||||
ImGui::Text("%d", sema.id);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", sema->count);
|
||||
ImGui::Text("%d", sema.count);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", sema->maxCount);
|
||||
ImGui::Text("%d", sema.maxCount);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", sema->initCount);
|
||||
ImGui::Text("-");
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", sema->waiters);
|
||||
ImGui::Text("%u", sema.waiters);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", sema->attr);
|
||||
ImGui::Text("-");
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%u", sema->deleted ? 1u : 0u);
|
||||
ImGui::Text("0");
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
@@ -898,7 +850,6 @@ namespace
|
||||
|
||||
ImGui::SeparatorText("Event flags");
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_event_flag_map_mutex);
|
||||
if (ImGui::BeginTable("evf", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable))
|
||||
{
|
||||
ImGui::TableSetupColumn("ID");
|
||||
@@ -908,24 +859,21 @@ namespace
|
||||
ImGui::TableSetupColumn("Attr");
|
||||
ImGui::TableSetupColumn("Deleted");
|
||||
ImGui::TableHeadersRow();
|
||||
for (const auto &[id, evf] : g_eventFlags)
|
||||
for (const EeEventFlagSnapshot &evf : snapshot.eventFlags)
|
||||
{
|
||||
if (!evf)
|
||||
continue;
|
||||
std::lock_guard<std::mutex> evfLock(evf->m);
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", id);
|
||||
ImGui::Text("%d", evf.id);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", evf->bits);
|
||||
ImGui::Text("0x%08X", evf.bits);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", evf->initBits);
|
||||
ImGui::Text("0x%08X", evf.initBits);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", evf->waiters);
|
||||
ImGui::Text("%u", evf.waiters);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("0x%08X", evf->attr);
|
||||
ImGui::Text("0x%08X", evf.attr);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%u", evf->deleted ? 1u : 0u);
|
||||
ImGui::Text("0");
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
@@ -2249,12 +2197,12 @@ void PS2DebugPanel::draw(PS2Runtime &runtime)
|
||||
}
|
||||
if (ImGui::BeginTabItem("Threads"))
|
||||
{
|
||||
drawThreadsTab();
|
||||
drawThreadsTab(runtime);
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
if (ImGui::BeginTabItem("Kernel"))
|
||||
{
|
||||
drawKernelTab();
|
||||
drawKernelTab(runtime);
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
if (ImGui::BeginTabItem("IOP/SIF"))
|
||||
|
||||
@@ -617,7 +617,7 @@ void GS::recordDebugEventUnlocked(GSDebugHistoryEntry entry)
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t tick = ps2_syscalls::GetCurrentVSyncTick();
|
||||
const uint64_t tick = m_privRegs ? m_privRegs->vsyncTick.load(std::memory_order_acquire) : 0u;
|
||||
if (m_debugLastVsyncTick == UINT64_MAX)
|
||||
{
|
||||
m_debugLastVsyncTick = tick;
|
||||
@@ -960,7 +960,7 @@ void GS::latchHostPresentationFrameUnlocked()
|
||||
const GSPmodeState pmode = decodePmode(m_privRegs->pmode);
|
||||
const GSSmode2State smode2 = decodeSMode2(m_privRegs->smode2);
|
||||
const bool applyFieldMode = smode2.interlaced && !smode2.frameMode;
|
||||
const bool oddField = (ps2_syscalls::GetCurrentVSyncTick() & 1ull) != 0ull;
|
||||
const bool oddField = (m_privRegs->vsyncTick.load(std::memory_order_acquire) & 1ull) != 0ull;
|
||||
const GSFrameReg displayFrame1 = decodeDisplayFrame(m_privRegs->dispfb1);
|
||||
const GSFrameReg displayFrame2 = decodeDisplayFrame(m_privRegs->dispfb2);
|
||||
const GSDisplayReadOrigin displayOrigin1 = decodeDisplayReadOrigin(m_privRegs->dispfb1);
|
||||
|
||||
@@ -458,21 +458,17 @@ bool PS2IopHostAdapter::invokeGuestFunction(uint64_t callToken,
|
||||
uint32_t a3,
|
||||
uint32_t *resultAddress)
|
||||
{
|
||||
if (!m_activeContext || callToken == 0 || callToken != m_activeToken)
|
||||
(void)callToken;
|
||||
(void)address;
|
||||
(void)a0;
|
||||
(void)a1;
|
||||
(void)a2;
|
||||
(void)a3;
|
||||
if (resultAddress)
|
||||
{
|
||||
return false;
|
||||
*resultAddress = 0u;
|
||||
}
|
||||
return rpcInvokeFunction(m_activeRdram
|
||||
? m_activeRdram
|
||||
: m_runtime.memory().getRDRAM(),
|
||||
m_activeContext,
|
||||
&m_runtime,
|
||||
address,
|
||||
a0,
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
resultAddress);
|
||||
return false;
|
||||
}
|
||||
|
||||
void PS2IopHostAdapter::log(ps2x::iop::LogLevel level, std::string_view message)
|
||||
|
||||
+143
-346
@@ -5,6 +5,7 @@
|
||||
#include "game_overrides.h"
|
||||
#include "ps2_runtime_macros.h"
|
||||
#include "runtime/ps2_gs_gpu.h"
|
||||
#include "runtime/ee_scheduler.h"
|
||||
#include "ThreadNaming.h"
|
||||
#include "Kernel/Stubs/Audio.h"
|
||||
#include "Kernel/Stubs/GS.h"
|
||||
@@ -107,9 +108,6 @@ namespace
|
||||
};
|
||||
|
||||
thread_local DispatchHistory g_dispatchHistory;
|
||||
thread_local std::unordered_map<PS2Runtime *, uint32_t> g_guestExecutionDepths;
|
||||
thread_local uint32_t g_deferredGuestYieldDepth = 0u;
|
||||
thread_local bool g_deferredGuestYieldPending = false;
|
||||
|
||||
bool computeFileCrc32(const std::string &path, uint32_t &crcOut)
|
||||
{
|
||||
@@ -368,40 +366,6 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
PS2Runtime::GuestExecutionScope::GuestExecutionScope(PS2Runtime *runtime) noexcept
|
||||
: m_runtime(runtime)
|
||||
{
|
||||
if (m_runtime)
|
||||
{
|
||||
m_runtime->enterGuestExecution();
|
||||
}
|
||||
}
|
||||
|
||||
PS2Runtime::GuestExecutionScope::~GuestExecutionScope()
|
||||
{
|
||||
if (m_runtime)
|
||||
{
|
||||
m_runtime->leaveGuestExecution();
|
||||
}
|
||||
}
|
||||
|
||||
PS2Runtime::GuestExecutionReleaseScope::GuestExecutionReleaseScope(PS2Runtime *runtime) noexcept
|
||||
: m_runtime(runtime)
|
||||
{
|
||||
if (m_runtime)
|
||||
{
|
||||
m_depth = m_runtime->releaseGuestExecution();
|
||||
}
|
||||
}
|
||||
|
||||
PS2Runtime::GuestExecutionReleaseScope::~GuestExecutionReleaseScope()
|
||||
{
|
||||
if (m_runtime && m_depth != 0u)
|
||||
{
|
||||
m_runtime->reacquireGuestExecution(m_depth);
|
||||
}
|
||||
}
|
||||
|
||||
static void UploadFrame(Texture2D &tex, PS2Runtime *rt, uint32_t &outWidth, uint32_t &outHeight)
|
||||
{
|
||||
static uint64_t s_lastPresentationTick = std::numeric_limits<uint64_t>::max();
|
||||
@@ -415,7 +379,7 @@ static void UploadFrame(Texture2D &tex, PS2Runtime *rt, uint32_t &outWidth, uint
|
||||
static std::vector<uint8_t> s_scratch;
|
||||
static std::vector<uint8_t> s_uploadBuffer(DEFAULT_FB_SIZE, 0u);
|
||||
|
||||
const uint64_t currentTick = ps2_syscalls::GetCurrentVSyncTick();
|
||||
const uint64_t currentTick = rt->eeScheduler().currentVSyncTick();
|
||||
const bool needsLatch = !s_hasLatchedInitialFrame || currentTick != s_lastPresentationTick;
|
||||
if (needsLatch)
|
||||
{
|
||||
@@ -510,6 +474,7 @@ PS2Runtime::PS2Runtime()
|
||||
{
|
||||
m_iopHost = std::make_unique<PS2IopHostAdapter>(*this);
|
||||
m_iopSubsystem = std::make_unique<ps2x::iop::IopSubsystem>(*m_iopHost);
|
||||
m_eeScheduler = std::make_unique<EeScheduler>(*this);
|
||||
#if defined(PS2X_IOP_ENABLE_PLUGINS) && PS2X_IOP_ENABLE_PLUGINS && \
|
||||
!defined(PLATFORM_VITA) && (defined(_WIN32) || defined(__linux__))
|
||||
if (const char *applicationDirectory = GetApplicationDirectory();
|
||||
@@ -559,7 +524,6 @@ PS2Runtime::~PS2Runtime()
|
||||
try
|
||||
{
|
||||
requestStop();
|
||||
ps2_syscalls::detachAllGuestHostThreads();
|
||||
m_iopSubsystem.reset();
|
||||
m_iopHost.reset();
|
||||
#if defined(PLATFORM_VITA)
|
||||
@@ -1928,249 +1892,6 @@ uint32_t PS2Runtime::reserveAsyncCallbackStack(uint32_t size, uint32_t alignment
|
||||
return top - 0x10u;
|
||||
}
|
||||
|
||||
void PS2Runtime::dispatchLoop(uint8_t *rdram, R5900Context *ctx)
|
||||
{
|
||||
uint32_t lastPc = std::numeric_limits<uint32_t>::max();
|
||||
uint32_t samePcCount = 0;
|
||||
constexpr uint32_t kSamePcYieldInterval = 0x4000u;
|
||||
|
||||
while (!isStopRequested())
|
||||
{
|
||||
const uint32_t pc = ctx->pc;
|
||||
|
||||
if (pc == lastPc)
|
||||
{
|
||||
++samePcCount;
|
||||
if ((samePcCount % kSamePcYieldInterval) == 0u)
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
RUNTIME_LOG("CPU is doing some work at PC 0x" << std::hex << pc << ". PC not updating.");
|
||||
});
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
samePcCount = 0;
|
||||
lastPc = pc;
|
||||
}
|
||||
|
||||
m_debugPc.store(pc, std::memory_order_relaxed);
|
||||
m_debugRa.store(static_cast<uint32_t>(_mm_extract_epi32(ctx->r[31], 0)), std::memory_order_relaxed);
|
||||
m_debugSp.store(static_cast<uint32_t>(_mm_extract_epi32(ctx->r[29], 0)), std::memory_order_relaxed);
|
||||
m_debugGp.store(static_cast<uint32_t>(_mm_extract_epi32(ctx->r[28], 0)), std::memory_order_relaxed);
|
||||
|
||||
RecompiledFunction fn = lookupFunction(pc);
|
||||
const uint32_t dispatchedPc = pc;
|
||||
const uint32_t dispatchedRa = static_cast<uint32_t>(_mm_extract_epi32(ctx->r[31], 0));
|
||||
|
||||
uint64_t handoffBaseline = 0u;
|
||||
{
|
||||
GuestExecutionScope guestExecution(this);
|
||||
fn(rdram, ctx, this);
|
||||
handoffBaseline = guestExecutionHandoffEpochSnapshot();
|
||||
}
|
||||
|
||||
waitForGuestExecutionHandoff(handoffBaseline);
|
||||
|
||||
if (ctx->pc == 0u)
|
||||
{
|
||||
const uint32_t ra = static_cast<uint32_t>(_mm_extract_epi32(ctx->r[31], 0));
|
||||
const uint32_t sp = static_cast<uint32_t>(_mm_extract_epi32(ctx->r[29], 0));
|
||||
const uint32_t gp = static_cast<uint32_t>(_mm_extract_epi32(ctx->r[28], 0));
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
std::cerr << "[dispatch:pc-zero] from=0x" << std::hex << dispatchedPc
|
||||
<< " fromRa=0x" << dispatchedRa
|
||||
<< " ra=0x" << ra
|
||||
<< " sp=0x" << sp
|
||||
<< " gp=0x" << gp
|
||||
<< " trace=" << formatDispatchHistory()
|
||||
<< std::dec << std::endl;
|
||||
});
|
||||
|
||||
// PC=0 means this guest thread returned (usually via jr $ra with RA=0).
|
||||
// Do not request a global runtime stop here: other guest threads may still run.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PS2Runtime::enterGuestExecution()
|
||||
{
|
||||
uint32_t &depth = g_guestExecutionDepths[this];
|
||||
|
||||
if (depth != 0u)
|
||||
{
|
||||
m_guestExecutionMutex.lock();
|
||||
++depth;
|
||||
return;
|
||||
}
|
||||
|
||||
m_guestExecutionWaiters.fetch_add(1u, std::memory_order_acq_rel);
|
||||
m_guestExecutionMutex.lock();
|
||||
m_guestExecutionWaiters.fetch_sub(1u, std::memory_order_acq_rel);
|
||||
depth = 1u;
|
||||
markGuestExecutionAcquired();
|
||||
}
|
||||
|
||||
void PS2Runtime::leaveGuestExecution()
|
||||
{
|
||||
auto it = g_guestExecutionDepths.find(this);
|
||||
if (it == g_guestExecutionDepths.end() || it->second == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
--it->second;
|
||||
m_guestExecutionMutex.unlock();
|
||||
if (it->second == 0u)
|
||||
{
|
||||
g_guestExecutionDepths.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t PS2Runtime::releaseGuestExecution()
|
||||
{
|
||||
auto it = g_guestExecutionDepths.find(this);
|
||||
if (it == g_guestExecutionDepths.end() || it->second == 0u)
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
|
||||
const uint32_t depth = it->second;
|
||||
for (uint32_t i = 0; i < depth; ++i)
|
||||
{
|
||||
m_guestExecutionMutex.unlock();
|
||||
}
|
||||
g_guestExecutionDepths.erase(it);
|
||||
return depth;
|
||||
}
|
||||
|
||||
void PS2Runtime::reacquireGuestExecution(uint32_t depth)
|
||||
{
|
||||
if (depth == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t &heldDepth = g_guestExecutionDepths[this];
|
||||
uint32_t remaining = depth;
|
||||
|
||||
if (heldDepth == 0u)
|
||||
{
|
||||
m_guestExecutionWaiters.fetch_add(1u, std::memory_order_acq_rel);
|
||||
m_guestExecutionMutex.lock();
|
||||
m_guestExecutionWaiters.fetch_sub(1u, std::memory_order_acq_rel);
|
||||
heldDepth = 1u;
|
||||
markGuestExecutionAcquired();
|
||||
--remaining;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < remaining; ++i)
|
||||
{
|
||||
m_guestExecutionMutex.lock();
|
||||
++heldDepth;
|
||||
}
|
||||
}
|
||||
|
||||
void PS2Runtime::markGuestExecutionAcquired()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_guestExecutionHandoffMutex);
|
||||
m_guestExecutionHandoffEpoch.fetch_add(1u, std::memory_order_acq_rel);
|
||||
}
|
||||
m_guestExecutionHandoffCv.notify_all();
|
||||
}
|
||||
|
||||
void PS2Runtime::waitForGuestExecutionHandoff()
|
||||
{
|
||||
waitForGuestExecutionHandoff(guestExecutionHandoffEpochSnapshot());
|
||||
}
|
||||
|
||||
void PS2Runtime::waitForGuestExecutionHandoff(uint64_t baselineEpoch)
|
||||
{
|
||||
// Lock-free fast path
|
||||
if (m_guestExecutionWaiters.load(std::memory_order_acquire) == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_guestExecutionHandoffMutex);
|
||||
|
||||
if (m_guestExecutionWaiters.load(std::memory_order_acquire) == 0u)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const bool handedOff = m_guestExecutionHandoffCv.wait_for(
|
||||
lock,
|
||||
std::chrono::milliseconds(2),
|
||||
[&]()
|
||||
{
|
||||
return m_guestExecutionWaiters.load(std::memory_order_acquire) == 0u ||
|
||||
m_guestExecutionHandoffEpoch.load(std::memory_order_relaxed) != baselineEpoch ||
|
||||
isStopRequested();
|
||||
});
|
||||
|
||||
if (!handedOff)
|
||||
{
|
||||
m_guestExecutionHandoffTimeouts.fetch_add(1u, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
PS2Runtime::DeferredGuestYieldScope::DeferredGuestYieldScope(bool &pendingOut) noexcept
|
||||
: m_pendingOut(pendingOut)
|
||||
{
|
||||
++g_deferredGuestYieldDepth;
|
||||
}
|
||||
|
||||
PS2Runtime::DeferredGuestYieldScope::~DeferredGuestYieldScope()
|
||||
{
|
||||
if (--g_deferredGuestYieldDepth == 0u && g_deferredGuestYieldPending)
|
||||
{
|
||||
g_deferredGuestYieldPending = false;
|
||||
m_pendingOut = true;
|
||||
}
|
||||
}
|
||||
|
||||
void PS2Runtime::yieldGuestExecutionAfterWake()
|
||||
{
|
||||
if (g_deferredGuestYieldDepth != 0u)
|
||||
{
|
||||
g_deferredGuestYieldPending = true;
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = g_guestExecutionDepths.find(this);
|
||||
if (it == g_guestExecutionDepths.end() || it->second == 0u)
|
||||
{
|
||||
std::this_thread::yield();
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t handoffEpoch = m_guestExecutionHandoffEpoch.load(std::memory_order_acquire);
|
||||
{
|
||||
GuestExecutionReleaseScope releaseGuestExecution(this);
|
||||
std::unique_lock<std::mutex> lock(m_guestExecutionHandoffMutex);
|
||||
m_guestExecutionHandoffCv.wait_for(lock, std::chrono::milliseconds(2), [&]()
|
||||
{ return m_guestExecutionHandoffEpoch.load(std::memory_order_acquire) != handoffEpoch; });
|
||||
}
|
||||
}
|
||||
|
||||
bool PS2Runtime::shouldPreemptGuestExecution()
|
||||
{
|
||||
thread_local uint32_t s_backEdgeYieldCounter = 0u;
|
||||
const uint32_t waiterCount = m_guestExecutionWaiters.load(std::memory_order_acquire);
|
||||
const uint32_t yieldInterval = (waiterCount != 0u) ? 64u : 100u;
|
||||
if (++s_backEdgeYieldCounter < yieldInterval)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
s_backEdgeYieldCounter = 0u;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t PS2Runtime::Load8(uint8_t *rdram, R5900Context *ctx, uint32_t vaddr)
|
||||
{
|
||||
try
|
||||
@@ -2341,7 +2062,10 @@ void PS2Runtime::kickGifDmaChainFromMMIO(uint8_t *rdram,
|
||||
void PS2Runtime::requestStop()
|
||||
{
|
||||
m_stopRequested.store(true, std::memory_order_relaxed);
|
||||
ps2_syscalls::notifyRuntimeStop();
|
||||
if (m_eeScheduler)
|
||||
{
|
||||
m_eeScheduler->requestStop();
|
||||
}
|
||||
}
|
||||
|
||||
bool PS2Runtime::isStopRequested() const
|
||||
@@ -2349,6 +2073,134 @@ bool PS2Runtime::isStopRequested() const
|
||||
return m_stopRequested.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
EeScheduler &PS2Runtime::eeScheduler()
|
||||
{
|
||||
return *m_eeScheduler;
|
||||
}
|
||||
|
||||
const EeScheduler &PS2Runtime::eeScheduler() const
|
||||
{
|
||||
return *m_eeScheduler;
|
||||
}
|
||||
|
||||
void PS2Runtime::postEeEvent(EeEvent event)
|
||||
{
|
||||
m_eeScheduler->postEvent(event);
|
||||
}
|
||||
|
||||
bool PS2Runtime::eeCheckpointDue() const noexcept
|
||||
{
|
||||
return m_eeScheduler->checkpointDue();
|
||||
}
|
||||
|
||||
void PS2Runtime::addEeExitHandler(int threadId, uint32_t function, uint32_t argument)
|
||||
{
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
m_eeExitHandlers[threadId].push_back({function, argument});
|
||||
}
|
||||
|
||||
std::vector<PS2Runtime::EeExitHandlerRegistration> PS2Runtime::takeEeExitHandlers(int threadId)
|
||||
{
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
auto it = m_eeExitHandlers.find(threadId);
|
||||
if (it == m_eeExitHandlers.end())
|
||||
{
|
||||
return {};
|
||||
}
|
||||
auto handlers = std::move(it->second);
|
||||
m_eeExitHandlers.erase(it);
|
||||
return handlers;
|
||||
}
|
||||
|
||||
void PS2Runtime::removeEeExitHandlers(int threadId)
|
||||
{
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
m_eeExitHandlers.erase(threadId);
|
||||
}
|
||||
|
||||
bool PS2Runtime::findEeSyscallOverride(uint32_t syscallNumber, uint32_t &handler) const
|
||||
{
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
const auto it = m_eeSyscallOverrides.find(syscallNumber);
|
||||
if (it == m_eeSyscallOverrides.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
handler = it->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PS2Runtime::setEeSyscallOverride(uint8_t *rdram, uint32_t syscallNumber, uint32_t handler)
|
||||
{
|
||||
constexpr uint32_t kTableBase = 0x80011F80u & 0x1FFFFFFFu;
|
||||
constexpr uint32_t kMirrorLimit = 0x00080000u;
|
||||
const int64_t offset = static_cast<int64_t>(static_cast<int32_t>(syscallNumber)) * 4;
|
||||
const int64_t address = static_cast<int64_t>(kTableBase) + offset;
|
||||
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
if (handler == 0u)
|
||||
{
|
||||
m_eeSyscallOverrides.erase(syscallNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_eeSyscallOverrides[syscallNumber] = handler;
|
||||
}
|
||||
if (!rdram || address < 0 || address + 4 > kMirrorLimit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
const uint32_t guestAddress = static_cast<uint32_t>(address);
|
||||
std::memcpy(rdram + guestAddress, &handler, sizeof(handler));
|
||||
if (handler == 0u)
|
||||
{
|
||||
m_eeSyscallMirrorAddresses.erase(guestAddress);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_eeSyscallMirrorAddresses.insert(guestAddress);
|
||||
}
|
||||
}
|
||||
|
||||
void PS2Runtime::initializeEeKernelState(uint8_t *rdram)
|
||||
{
|
||||
if (!rdram)
|
||||
{
|
||||
return;
|
||||
}
|
||||
constexpr uint32_t kTableGuestBase = 0x80011F80u;
|
||||
constexpr uint32_t kTableBase = kTableGuestBase & 0x1FFFFFFFu;
|
||||
constexpr uint32_t kMirrorLimit = 0x00080000u;
|
||||
constexpr uint32_t kProbeBase = 0x000002F0u;
|
||||
|
||||
std::lock_guard lock(m_eeKernelStateMutex);
|
||||
for (const uint32_t address : m_eeSyscallMirrorAddresses)
|
||||
{
|
||||
const uint32_t zero = 0u;
|
||||
std::memcpy(rdram + address, &zero, sizeof(zero));
|
||||
}
|
||||
m_eeSyscallMirrorAddresses.clear();
|
||||
const uint32_t high = kTableGuestBase >> 16;
|
||||
const uint32_t low = kTableGuestBase & 0xFFFFu;
|
||||
std::memcpy(rdram + kProbeBase, &high, sizeof(high));
|
||||
std::memcpy(rdram + kProbeBase + 8u, &low, sizeof(low));
|
||||
m_eeSyscallMirrorAddresses.insert(kProbeBase);
|
||||
m_eeSyscallMirrorAddresses.insert(kProbeBase + 8u);
|
||||
|
||||
for (const auto &[syscallNumber, handler] : m_eeSyscallOverrides)
|
||||
{
|
||||
const int64_t offset = static_cast<int64_t>(static_cast<int32_t>(syscallNumber)) * 4;
|
||||
const int64_t address = static_cast<int64_t>(kTableBase) + offset;
|
||||
if (address < 0 || address + 4 > kMirrorLimit)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const uint32_t guestAddress = static_cast<uint32_t>(address);
|
||||
std::memcpy(rdram + guestAddress, &handler, sizeof(handler));
|
||||
m_eeSyscallMirrorAddresses.insert(guestAddress);
|
||||
}
|
||||
}
|
||||
|
||||
void PS2Runtime::HandleIntegerOverflow(R5900Context *ctx)
|
||||
{
|
||||
raiseCop0Exception(ctx, EXCEPTION_INTEGER_OVERFLOW);
|
||||
@@ -2360,9 +2212,8 @@ void PS2Runtime::run()
|
||||
ps2_stubs::resetSifState();
|
||||
resetIop();
|
||||
ps2_stubs::resetAudioStubState();
|
||||
ps2_stubs::resetGsSyncVCallbackState();
|
||||
ps2_stubs::resetMpegStubState();
|
||||
ps2_syscalls::initializeGuestKernelState(m_memory.getRDRAM());
|
||||
initializeEeKernelState(m_memory.getRDRAM());
|
||||
m_cpuContext.r[4] = _mm_setzero_si128();
|
||||
m_cpuContext.r[5] = _mm_setzero_si128();
|
||||
m_cpuContext.r[29] = _mm_set_epi64x(0, static_cast<int64_t>(PS2_RAM_SIZE - 0x10u));
|
||||
@@ -2378,7 +2229,6 @@ void PS2Runtime::run()
|
||||
Texture2D frameTex = LoadTextureFromImage(blank);
|
||||
UnloadImage(blank);
|
||||
|
||||
g_activeThreads.store(1, std::memory_order_relaxed);
|
||||
std::atomic<bool> gameThreadFinished{false};
|
||||
|
||||
std::thread gameThread([&]()
|
||||
@@ -2386,7 +2236,8 @@ void PS2Runtime::run()
|
||||
ThreadNaming::SetCurrentThreadName("GameThread");
|
||||
try
|
||||
{
|
||||
dispatchLoop(m_memory.getRDRAM(), &m_cpuContext);
|
||||
m_eeScheduler->reset(m_memory.getRDRAM(), m_cpuContext);
|
||||
m_eeScheduler->run();
|
||||
uint32_t pc = m_debugPc.load(std::memory_order_relaxed);
|
||||
RUNTIME_LOG("Game thread returned. PC=0x" << std::hex << pc
|
||||
<< " RA=0x" << static_cast<uint32_t>(_mm_extract_epi32(m_cpuContext.r[31], 0)) << std::dec << std::endl);
|
||||
@@ -2399,13 +2250,10 @@ void PS2Runtime::run()
|
||||
{
|
||||
std::cerr << "Error during program execution: unknown exception" << std::endl;
|
||||
}
|
||||
g_activeThreads.fetch_sub(1, std::memory_order_relaxed);
|
||||
gameThreadFinished.store(true, std::memory_order_release); });
|
||||
|
||||
ps2_syscalls::EnsureVSyncWorkerRunning(m_memory.getRDRAM(), this);
|
||||
|
||||
uint64_t tick = 0;
|
||||
while (!isStopRequested() && g_activeThreads.load(std::memory_order_relaxed) > 0)
|
||||
while (!isStopRequested() && !gameThreadFinished.load(std::memory_order_acquire))
|
||||
{
|
||||
PS2_IF_AGRESSIVE_LOGS({
|
||||
tick++;
|
||||
@@ -2420,7 +2268,7 @@ void PS2Runtime::run()
|
||||
const uint32_t dbgRa = m_debugRa.load(std::memory_order_relaxed);
|
||||
const uint32_t dbgSp = m_debugSp.load(std::memory_order_relaxed);
|
||||
const uint32_t dbgGp = m_debugGp.load(std::memory_order_relaxed);
|
||||
const int activeThreads = g_activeThreads.load(std::memory_order_relaxed);
|
||||
const auto eeSnapshot = m_eeScheduler->snapshot();
|
||||
|
||||
RUNTIME_LOG("[run:tick] tick=" << tick
|
||||
<< " pc=0x" << std::hex << dbgPc
|
||||
@@ -2430,7 +2278,7 @@ void PS2Runtime::run()
|
||||
<< " dispfb1=0x" << gs.dispfb1
|
||||
<< " display1=0x" << gs.display1
|
||||
<< std::dec
|
||||
<< " activeThreads=" << activeThreads
|
||||
<< " activeThreads=" << eeSnapshot.threads.size()
|
||||
<< " dma=" << curDma
|
||||
<< " gif=" << curGif
|
||||
<< " gsw=" << curGs
|
||||
@@ -2473,54 +2321,9 @@ void PS2Runtime::run()
|
||||
}
|
||||
|
||||
requestStop();
|
||||
|
||||
const auto joinDeadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
|
||||
while (!gameThreadFinished.load(std::memory_order_acquire) &&
|
||||
std::chrono::steady_clock::now() < joinDeadline)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
if (gameThread.joinable())
|
||||
{
|
||||
if (gameThreadFinished.load(std::memory_order_acquire))
|
||||
{
|
||||
gameThread.join();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "[run] game thread did not stop within timeout; detaching" << std::endl;
|
||||
gameThread.detach();
|
||||
}
|
||||
}
|
||||
|
||||
const auto workerDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(1000);
|
||||
while (g_activeThreads.load(std::memory_order_relaxed) > 0 &&
|
||||
std::chrono::steady_clock::now() < workerDeadline)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
if (g_activeThreads.load(std::memory_order_relaxed) > 0)
|
||||
{
|
||||
requestStop();
|
||||
const auto finalWorkerDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(1000);
|
||||
while (g_activeThreads.load(std::memory_order_relaxed) > 0 &&
|
||||
std::chrono::steady_clock::now() < finalWorkerDeadline)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
if (g_activeThreads.load(std::memory_order_relaxed) == 0)
|
||||
{
|
||||
ps2_syscalls::joinAllGuestHostThreads();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "[run] guest host threads did not stop within timeout; detaching remaining worker threads"
|
||||
<< std::endl;
|
||||
ps2_syscalls::detachAllGuestHostThreads();
|
||||
gameThread.join();
|
||||
}
|
||||
|
||||
if (m_debugUiInitialized && m_debugUiShutdownCallback)
|
||||
@@ -2531,11 +2334,5 @@ void PS2Runtime::run()
|
||||
UnloadTexture(frameTex);
|
||||
CloseWindow();
|
||||
|
||||
const int remainingThreads = g_activeThreads.load(std::memory_order_relaxed);
|
||||
RUNTIME_LOG("[run] exiting loop, activeThreads=" << remainingThreads);
|
||||
if (remainingThreads > 0)
|
||||
{
|
||||
std::cerr << "[run] warning: " << remainingThreads
|
||||
<< " guest worker thread(s) still active during shutdown." << std::endl;
|
||||
}
|
||||
RUNTIME_LOG("[run] exiting loop");
|
||||
}
|
||||
|
||||
+54
-23
@@ -26,38 +26,69 @@
|
||||
namespace
|
||||
{
|
||||
#if defined(__ANDROID__)
|
||||
int g_logcatPipeFds[2]{-1, -1};
|
||||
std::thread g_logcatThread;
|
||||
|
||||
void stopLogcatRedirect()
|
||||
{
|
||||
std::fflush(stdout);
|
||||
std::fflush(stderr);
|
||||
close(STDOUT_FILENO);
|
||||
close(STDERR_FILENO);
|
||||
if (g_logcatPipeFds[1] >= 0)
|
||||
{
|
||||
close(g_logcatPipeFds[1]);
|
||||
g_logcatPipeFds[1] = -1;
|
||||
}
|
||||
if (g_logcatThread.joinable())
|
||||
{
|
||||
g_logcatThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void redirectStdioToLogcat()
|
||||
{
|
||||
static int pipeFds[2];
|
||||
if (pipe(pipeFds) != 0)
|
||||
if (pipe(g_logcatPipeFds) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
setvbuf(stdout, nullptr, _IOLBF, 0);
|
||||
setvbuf(stderr, nullptr, _IONBF, 0);
|
||||
dup2(pipeFds[1], STDOUT_FILENO);
|
||||
dup2(pipeFds[1], STDERR_FILENO);
|
||||
dup2(g_logcatPipeFds[1], STDOUT_FILENO);
|
||||
dup2(g_logcatPipeFds[1], STDERR_FILENO);
|
||||
|
||||
std::thread([]()
|
||||
{
|
||||
FILE *reader = fdopen(pipeFds[0], "r");
|
||||
if (!reader)
|
||||
{
|
||||
return;
|
||||
}
|
||||
char line[1024];
|
||||
while (fgets(line, sizeof(line), reader))
|
||||
{
|
||||
size_t len = std::strlen(line);
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
{
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
__android_log_write(ANDROID_LOG_INFO, "ps2x", line);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
g_logcatThread = std::thread([]()
|
||||
{
|
||||
FILE *reader = fdopen(g_logcatPipeFds[0], "r");
|
||||
if (!reader)
|
||||
{
|
||||
return;
|
||||
}
|
||||
char line[1024];
|
||||
while (fgets(line, sizeof(line), reader))
|
||||
{
|
||||
size_t len = std::strlen(line);
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
{
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
__android_log_write(ANDROID_LOG_INFO, "ps2x", line);
|
||||
}
|
||||
fclose(reader);
|
||||
g_logcatPipeFds[0] = -1;
|
||||
});
|
||||
if (std::atexit(stopLogcatRedirect) != 0)
|
||||
{
|
||||
close(STDOUT_FILENO);
|
||||
close(STDERR_FILENO);
|
||||
close(g_logcatPipeFds[1]);
|
||||
g_logcatPipeFds[1] = -1;
|
||||
if (g_logcatThread.joinable())
|
||||
{
|
||||
g_logcatThread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user