mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-26 12:48:39 +08:00
cdee77521e
* [agc] WAIT_REG_MEM suspend/resume, draw packet fixes, new HLE exports, debug cleanup
Rebased onto upstream 79a7437 (par274/sharpemu, rewritten history).
- GpuWaitRegistry: DCBs suspended on unsatisfied WAIT_REG_MEM are re-polled
against guest memory on every submit; fixed 64-bit and standard packet parse
offsets, apply the mask, treat PM4 compare function 0 as "always".
- TryReadSubmittedDrawCount: accept the 5-dword ItDrawIndex2 form emitted by
DcbDrawIndex (count at +4); menu draws were silently discarded before.
- sceAgcDriverSubmitMultiDcbs: reversed ABI (rdi=address array, rsi=dword
sizes, rdx=count).
- VideoOut: vblank events, sceVideoOutGetFlipStatus, buffers registered via
sceVideoOutRegisterBuffers are valid flip targets.
- New HLE: libc stdio (fopen/fread/fseek/ftell/fclose/fgets), Dinkumware
_Getpctype ctype table, NpTrophy2 stubs, AMPR PAK sequential-read tracker,
MsgDialog lifecycle, NGS2 alt NIDs + dummy vtable for handle objects,
guarded memset intrinsic, abort()/strcasecmp null-arg recovery.
- Removed investigation-only code (INT3 breakpoints, qfont/mcpp dumps,
error-candidate printf traces, unconditional debug logs).
First rendered frame: Quake (PPSA01880) presents a 1920x1080 guest frame.
* Implemented a guarded native intrinsic (rep movsb) in DirectExecutionBackend to bypass HLE dispatch overhead, while preserving memory safety checks.
* [hle] clock_gettime clock ids, AudioOut2 canary fix, NID rebinds, new offline stubs
- clock_gettime (lLMT9vJAck0): support CLOCK_SECOND and the *_PRECISE/*_FAST
variants instead of returning EINVAL, which games treated as fatal and
retried in a tight loop.
- AudioOut2: context param writes shrunk to the guest-observed layout (the
old 0x80-byte reset smashed the stack canary at +0x60 and killed audio
init); ContextQueryMemory writes the single u64 the caller expects.
- NGS2: dropped wrong alt-NID aliases (they hash to sceImeUpdate,
sceMouseRead, sceSystemGestureUpdateAllTouchRecognizer - now bound in
their real libraries); added sceNgs2PanInit; fixed VoiceGetState NIDs.
- New verified stubs: sceUltInitialize, sceNpUniversalDataSystemDestroyHandle,
sceNpGetOnlineId, sceNpGetNpReachabilityState, sceImeKeyboardOpen,
sceImeKeyboardGetResourceId, sceMouseOpen, sceKernelAprGetFileSize.
- Import gateway: unwind guest workers at dispatch during backend teardown;
env-gated SHARPEMU_LOG_THREAD_MODE tracing.
* [cpu] Isolate guest execution on native worker threads
Guest entry stubs no longer run above CLR-managed frames: each run is handed
to a pooled raw OS thread whose loop is emitted native code. While guest code
executes there is not a single managed frame on the thread and it stays in
preemptive GC mode, so the GC never walks a frame chain interleaved with
guest stubs that carry no CLR unwind info (the ReversePInvokeBadTransition /
UnmanagedCallersOnly FailFast class of crashes on pumped guest threads).
- NativeGuestExecutor: CreateThread + emitted run loop (WaitForSingleObject,
UnmanagedCallersOnly prologue/epilogue, entry stub call, SetEvent). The
prologue rebinds guest TLS, the host-RSP slot, thread affinity and the
Active* ambient per run, so workers carry no guest identity and pool
freely; the orchestrating managed thread parks in a preemptive wait.
- All three entry sites route through RunGuestEntryStub: guest thread
entries, blocked-continuation resumes, and the main ExecuteEntry.
- Teardown stops workers before any executable stub or TLS index they
reference is freed; a worker that will not stop leaks its loop instead of
freeing running code.
- Kill switch: SHARPEMU_DISABLE_NATIVE_GUEST_WORKERS=1 restores the inline
calli path.
261 lines
9.3 KiB
C#
261 lines
9.3 KiB
C#
// Copyright (C) 2026 SharpEmu Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
using SharpEmu.HLE;
|
|
using SharpEmu.Libs.Ampr;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
|
|
namespace SharpEmu.Libs.Kernel;
|
|
|
|
public static class KernelAprCompatExports
|
|
{
|
|
private static readonly ConcurrentDictionary<uint, AprSubmission> _submittedCommandBuffers = new();
|
|
private static int _nextSubmissionId;
|
|
private static int _aprWaitTraceCount;
|
|
private static readonly bool _traceApr =
|
|
string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_AMPR"), "1", StringComparison.Ordinal);
|
|
|
|
private readonly record struct AprSubmission(ulong CommandBuffer, ulong Priority, ulong ResultAddress);
|
|
|
|
[SysAbiExport(
|
|
Nid = "ASoW5WE-UPo",
|
|
ExportName = "sceKernelAprSubmitCommandBufferAndGetResult",
|
|
Target = Generation.Gen4 | Generation.Gen5,
|
|
LibraryName = "libKernel")]
|
|
public static int KernelAprSubmitCommandBufferAndGetResult(CpuContext ctx)
|
|
{
|
|
var commandBuffer = ctx[CpuRegister.Rdi];
|
|
var priority = ctx[CpuRegister.Rsi];
|
|
var resultAddress = ctx[CpuRegister.Rdx];
|
|
var outSubmissionId = ctx[CpuRegister.Rcx];
|
|
|
|
if (commandBuffer == 0)
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
}
|
|
|
|
var submissionId = unchecked((uint)Interlocked.Increment(ref _nextSubmissionId));
|
|
if (submissionId == 0)
|
|
{
|
|
submissionId = unchecked((uint)Interlocked.Increment(ref _nextSubmissionId));
|
|
}
|
|
|
|
_submittedCommandBuffers[submissionId] = new AprSubmission(commandBuffer, priority, resultAddress);
|
|
|
|
var completionResult = AmprExports.CompleteCommandBuffer(ctx, commandBuffer);
|
|
if (completionResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
|
{
|
|
return completionResult;
|
|
}
|
|
|
|
if (outSubmissionId != 0 && !ctx.TryWriteUInt32(outSubmissionId, submissionId))
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
}
|
|
|
|
if (resultAddress != 0 && !TryWriteAprResult(ctx, resultAddress))
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
}
|
|
|
|
TraceApr(ctx, "submit_get_result", submissionId, commandBuffer, priority, resultAddress);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
}
|
|
|
|
[SysAbiExport(
|
|
Nid = "rqwFKI4PAiM",
|
|
ExportName = "sceKernelAprWaitCommandBuffer",
|
|
Target = Generation.Gen4 | Generation.Gen5,
|
|
LibraryName = "libKernel")]
|
|
public static int KernelAprWaitCommandBuffer(CpuContext ctx)
|
|
{
|
|
var submissionId = unchecked((uint)ctx[CpuRegister.Rdi]);
|
|
var waitArg1 = ctx[CpuRegister.Rsi];
|
|
var waitArg2 = ctx[CpuRegister.Rdx];
|
|
|
|
if (!_submittedCommandBuffers.TryRemove(submissionId, out var submission))
|
|
{
|
|
TraceAprWaitFailure(ctx, "wait_missing", submissionId, commandBuffer: 0, waitArg1, waitArg2);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
|
}
|
|
|
|
var resultAddress = ResolveWaitResultAddress(waitArg1, waitArg2, submission.ResultAddress);
|
|
if (resultAddress != 0 && !TryWriteAprResult(ctx, resultAddress))
|
|
{
|
|
TraceAprWaitFailure(ctx, "wait_result_fault", submissionId, submission.CommandBuffer, waitArg1, waitArg2);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
}
|
|
|
|
TraceApr(ctx, "wait", submissionId, submission.CommandBuffer, waitArg1, resultAddress);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
}
|
|
|
|
[SysAbiExport(
|
|
Nid = "eE4Szl8sil8",
|
|
ExportName = "sceKernelAprSubmitCommandBuffer",
|
|
Target = Generation.Gen4 | Generation.Gen5,
|
|
LibraryName = "libKernel")]
|
|
public static int KernelAprSubmitCommandBuffer(CpuContext ctx)
|
|
{
|
|
var commandBuffer = ctx[CpuRegister.Rdi];
|
|
if (commandBuffer == 0)
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
}
|
|
|
|
var submissionId = unchecked((uint)Interlocked.Increment(ref _nextSubmissionId));
|
|
_submittedCommandBuffers[submissionId] = new AprSubmission(commandBuffer, ctx[CpuRegister.Rsi], ResultAddress: 0);
|
|
|
|
var completionResult = AmprExports.CompleteCommandBuffer(ctx, commandBuffer);
|
|
if (completionResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
|
{
|
|
return completionResult;
|
|
}
|
|
|
|
TraceApr(ctx, "submit", submissionId, commandBuffer, ctx[CpuRegister.Rsi], 0);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
}
|
|
|
|
[SysAbiExport(
|
|
Nid = "qvMUCyyaCSI",
|
|
ExportName = "sceKernelAprSubmitCommandBufferAndGetId",
|
|
Target = Generation.Gen4 | Generation.Gen5,
|
|
LibraryName = "libKernel")]
|
|
public static int KernelAprSubmitCommandBufferAndGetId(CpuContext ctx)
|
|
{
|
|
var commandBuffer = ctx[CpuRegister.Rdi];
|
|
var outSubmissionId = ctx[CpuRegister.Rdx];
|
|
if (commandBuffer == 0 || outSubmissionId == 0)
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
}
|
|
|
|
var submissionId = unchecked((uint)Interlocked.Increment(ref _nextSubmissionId));
|
|
_submittedCommandBuffers[submissionId] = new AprSubmission(commandBuffer, ctx[CpuRegister.Rsi], ResultAddress: 0);
|
|
|
|
var completionResult = AmprExports.CompleteCommandBuffer(ctx, commandBuffer);
|
|
if (completionResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
|
{
|
|
return completionResult;
|
|
}
|
|
|
|
if (!ctx.TryWriteUInt32(outSubmissionId, submissionId))
|
|
{
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
}
|
|
|
|
TraceApr(ctx, "submit_get_id", submissionId, commandBuffer, ctx[CpuRegister.Rsi], outSubmissionId);
|
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
}
|
|
|
|
// Success stub: the argument layout is unknown and callers tolerate the
|
|
// empty answer (Quake streams fine), so no output payload is written until
|
|
// the real signature is reversed.
|
|
[SysAbiExport(
|
|
Nid = "WvEu7yl3Ivg",
|
|
ExportName = "sceKernelAprGetFileSize",
|
|
Target = Generation.Gen4 | Generation.Gen5,
|
|
LibraryName = "libKernel")]
|
|
public static int KernelAprGetFileSize(CpuContext ctx)
|
|
{
|
|
return ctx.SetReturn(0);
|
|
}
|
|
|
|
private static bool TryWriteAprResult(CpuContext ctx, ulong resultAddress)
|
|
{
|
|
Span<byte> result = stackalloc byte[sizeof(ulong)];
|
|
result.Clear();
|
|
return ctx.Memory.TryWrite(resultAddress, result);
|
|
}
|
|
|
|
private static ulong ResolveWaitResultAddress(ulong waitArg1, ulong waitArg2, ulong submittedResultAddress)
|
|
{
|
|
if (waitArg2 == 0)
|
|
{
|
|
return submittedResultAddress;
|
|
}
|
|
|
|
if (IsAmprCompletionToken(waitArg1) && waitArg2 <= 0xFFFF)
|
|
{
|
|
return submittedResultAddress;
|
|
}
|
|
|
|
return waitArg2;
|
|
}
|
|
|
|
private static bool IsAmprCompletionToken(ulong value)
|
|
{
|
|
var tag = value >> 56;
|
|
return tag is 0x0C or 0x10;
|
|
}
|
|
|
|
private static void TraceApr(
|
|
CpuContext ctx,
|
|
string operation,
|
|
uint submissionId,
|
|
ulong commandBuffer,
|
|
ulong priority,
|
|
ulong aux)
|
|
{
|
|
if (!_traceApr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var returnRip = 0UL;
|
|
_ = ctx.TryReadUInt64(ctx[CpuRegister.Rsp], out returnRip);
|
|
Console.Error.WriteLine(
|
|
$"[LOADER][TRACE] apr.{operation}: id=0x{submissionId:X8} cmd=0x{commandBuffer:X16} priority=0x{priority:X16} aux=0x{aux:X16} ret=0x{returnRip:X16}");
|
|
if (aux != 0 &&
|
|
ctx.TryReadUInt64(aux, out var result0) &&
|
|
ctx.TryReadUInt64(aux + sizeof(ulong), out var result1))
|
|
{
|
|
Console.Error.WriteLine(
|
|
$"[LOADER][TRACE] apr.{operation}.result: addr=0x{aux:X16} q0=0x{result0:X16} q1=0x{result1:X16}");
|
|
}
|
|
}
|
|
|
|
private static void TraceAprWaitFailure(
|
|
CpuContext ctx,
|
|
string operation,
|
|
uint submissionId,
|
|
ulong commandBuffer,
|
|
ulong priority,
|
|
ulong resultAddress)
|
|
{
|
|
if (!_traceApr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var traceCount = Interlocked.Increment(ref _aprWaitTraceCount);
|
|
if (traceCount > 32 && (traceCount & 0x3FF) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var returnRip = 0UL;
|
|
_ = ctx.TryReadUInt64(ctx[CpuRegister.Rsp], out returnRip);
|
|
Console.Error.WriteLine(
|
|
$"[LOADER][TRACE] apr.{operation}: id=0x{submissionId:X8} cmd=0x{commandBuffer:X16} " +
|
|
$"rsi=0x{priority:X16} rdx=0x{resultAddress:X16} rcx=0x{ctx[CpuRegister.Rcx]:X16} " +
|
|
$"r8=0x{ctx[CpuRegister.R8]:X16} r9=0x{ctx[CpuRegister.R9]:X16} ret=0x{returnRip:X16}");
|
|
TraceReadableQword(ctx, operation, "rsi", priority);
|
|
TraceReadableQword(ctx, operation, "rdx", resultAddress);
|
|
TraceReadableQword(ctx, operation, "rcx", ctx[CpuRegister.Rcx]);
|
|
TraceReadableQword(ctx, operation, "r8", ctx[CpuRegister.R8]);
|
|
}
|
|
|
|
private static void TraceReadableQword(CpuContext ctx, string operation, string name, ulong address)
|
|
{
|
|
if (address == 0 || !ctx.TryReadUInt64(address, out var value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
Console.Error.WriteLine(
|
|
$"[LOADER][TRACE] apr.{operation}.{name}: addr=0x{address:X16} q0=0x{value:X16}");
|
|
}
|
|
}
|