mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-31 23:19:44 +08:00
816ec4ad27
Ports the event-queue rework from the archived silent-hill-minimal branch (968e9606, 9e3abb12, 2be9cfbf, f157a115) onto current upstream. The equeue implementation had not been touched upstream since that branch forked, so none of it had landed. Behaviour fixed: - Per-waiter event reservation. A blocked waiter used to wake on "the queue has any pending event" (TryWake => HasPendingEvents) and then re-read the queue on resume, so a woken waiter could find the event already drained by another waiter and park again with the wake consumed. Events are now reserved to the waiter they are delivered to. - Level-triggered events are preserved instead of being cleared by an unrelated read; only events that declare clear-on-read reset their trigger state. - Queued interrupts are bound to the registration generation that produced them, so an event registered after a queue was reused cannot consume an interrupt raised for the previous registration. - Deleting an equeue now terminates its waiters instead of leaving them blocked on a handle that no longer resolves. - sceKernelTriggerUserEvent stores its third argument in the event's udata (0x18) rather than its data word (0x10). The guest reads it back with sceKernelGetEventUserData, which loads 0x18, so every triggered user event previously read back as 0. This matches the reference behaviour in shadPS4, where TriggerEvent takes udata and sceKernelGetEventUserData returns ev->udata. The upstream test asserting the data word is updated, since it encoded the inconsistency rather than the ABI. KernelPthreadState gains TryGetCurrentThreadIdentity and a new KernelSyncTraceFormatter carries the shared, opt-in diagnostic formatting the ported code calls; both are gated behind the existing trace flag and do no work when it is off. Tests: 662 pass, 0 fail (SharpEmu.Libs.Tests 588 -> 598).
77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
// Copyright (C) 2026 SharpEmu Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
using SharpEmu.HLE;
|
|
|
|
namespace SharpEmu.Libs.Kernel;
|
|
|
|
/// <summary>
|
|
/// Shared formatting for opt-in kernel synchronization diagnostics.
|
|
/// Callers must gate this behind their trace flag so normal synchronization
|
|
/// paths do not allocate strings or walk guest frame chains.
|
|
/// </summary>
|
|
internal static class KernelSyncTraceFormatter
|
|
{
|
|
internal static string FormatContext(CpuContext ctx)
|
|
{
|
|
_ = KernelPthreadState.TryGetCurrentThreadIdentity(out var pthread, out var identity);
|
|
var threadName = identity.Name ?? "<unknown>";
|
|
var returnRip = GuestThreadExecution.TryGetCurrentImportCallFrame(out var importFrame)
|
|
? importFrame.ReturnRip
|
|
: TryReadReturnRip(ctx);
|
|
|
|
return $"thread='{threadName}' pthread=0x{pthread:X16} " +
|
|
$"gth=0x{GuestThreadExecution.CurrentGuestThreadHandle:X16} " +
|
|
$"managed={Environment.CurrentManagedThreadId} ret=0x{returnRip:X16} " +
|
|
$"frames={FormatFrameChain(ctx)}";
|
|
}
|
|
|
|
internal static string FormatCurrentThread()
|
|
{
|
|
_ = KernelPthreadState.TryGetCurrentThreadIdentity(out var pthread, out var identity);
|
|
var threadName = identity.Name ?? Thread.CurrentThread.Name ?? "<unknown>";
|
|
return $"thread='{threadName}' pthread=0x{pthread:X16} " +
|
|
$"gth=0x{GuestThreadExecution.CurrentGuestThreadHandle:X16} " +
|
|
$"managed={Environment.CurrentManagedThreadId}";
|
|
}
|
|
|
|
internal static string FormatFrameChain(CpuContext ctx)
|
|
{
|
|
Span<ulong> returns = stackalloc ulong[4];
|
|
var count = 0;
|
|
var frame = ctx[CpuRegister.Rbp];
|
|
while (count < returns.Length && frame != 0)
|
|
{
|
|
if (!ctx.TryReadUInt64(frame, out var nextFrame) ||
|
|
!ctx.TryReadUInt64(frame + sizeof(ulong), out var returnAddress))
|
|
{
|
|
break;
|
|
}
|
|
|
|
returns[count++] = returnAddress;
|
|
if (nextFrame <= frame || nextFrame - frame > 0x100000)
|
|
{
|
|
break;
|
|
}
|
|
|
|
frame = nextFrame;
|
|
}
|
|
|
|
return count switch
|
|
{
|
|
0 => "none",
|
|
1 => $"0x{returns[0]:X16}",
|
|
2 => $"0x{returns[0]:X16},0x{returns[1]:X16}",
|
|
3 => $"0x{returns[0]:X16},0x{returns[1]:X16},0x{returns[2]:X16}",
|
|
_ => $"0x{returns[0]:X16},0x{returns[1]:X16}," +
|
|
$"0x{returns[2]:X16},0x{returns[3]:X16}",
|
|
};
|
|
}
|
|
|
|
private static ulong TryReadReturnRip(CpuContext ctx)
|
|
{
|
|
_ = ctx.TryReadUInt64(ctx[CpuRegister.Rsp], out var returnRip);
|
|
return returnRip;
|
|
}
|
|
}
|