Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ void local_mirror_table(const DeviceProxy* device, std::uint64_t gpu_handle) {

std::atomic<std::uint64_t> g_hits[kMaxSlots];
std::atomic<bool> g_first[kMaxSlots];
// The return address of the first call of each slot that reached the generic forwarding branch (0: none yet).
std::atomic<std::uint64_t> g_first_caller[kMaxSlots];
// Names a caller address (set_device_slot_caller_formatter); null: the bare address in hex. gta6rt sets
// rt::shims::module_offset, so this file does not depend on the module table and test targets link it alone.
std::atomic<CallerFormatter> g_caller_formatter{nullptr};

std::string format_caller(std::uint64_t address) {
if (const CallerFormatter formatter = g_caller_formatter.load(std::memory_order_relaxed)) return formatter(address);
char text[32];
std::snprintf(text, sizeof(text), "0x%llx", static_cast<unsigned long long>(address));
return text;
}
std::atomic<bool> g_beyond_warned[kMaxSlots];
std::atomic<std::uint64_t> g_heap_hits[kHeapSlots];
std::atomic<bool> g_heap_first[kHeapSlots];
Expand Down Expand Up @@ -479,11 +491,18 @@ const char* slot_name(unsigned slot) {
}
}

void note_slot(unsigned slot) {
// caller: the title code that called the slot, for the slots the proxy forwards by number (RT-526). The first call
// then also names it as module+offset, so a new Xbox-only slot can be found in the executable without a debugger.
void note_slot(unsigned slot, const void* caller = nullptr) {
if (slot >= kMaxSlots) return;
count_hit(g_hits[slot]);
const bool first = first_call(g_first[slot]);
if (first) {
if (first && caller) {
const auto address = reinterpret_cast<std::uint64_t>(caller);
g_first_caller[slot].store(address, std::memory_order_relaxed);
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call from %s\n", slot, slot_name(slot),
format_caller(address).c_str());
} else if (first) {
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call\n", slot, slot_name(slot));
} else if (slot >= 3 && trace_enabled()) {
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) call\n", slot, slot_name(slot));
Expand Down Expand Up @@ -6022,7 +6041,7 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
reinterpret_cast<D3D12_HEAP_PROPERTIES*>(self), reinterpret_cast<DeviceProxy*>(a1),
static_cast<UINT>(a2), static_cast<D3D12_HEAP_TYPE>(static_cast<std::uint32_t>(a3))));
}
note_slot(Slot);
note_slot(Slot, _ReturnAddress());
// Xbox device slots, before the PC vtable is indexed: the public HLE's 78/79/80 would otherwise reach
// CreateReservedResource2/CreateSampler2 or fall past an 80-slot table, and 48/73 would reach unrelated PC
// methods. The check is before the slot-count refusal because slot 80 is outside a PC ID3D12Device11 table.
Expand Down Expand Up @@ -6689,6 +6708,15 @@ bool device_slot_first_call_seen(unsigned slot) {
return slot < kMaxSlots && g_first[slot].load(std::memory_order_relaxed);
}

std::string device_slot_first_caller(unsigned slot) {
const std::uint64_t address = slot < kMaxSlots ? g_first_caller[slot].load(std::memory_order_relaxed) : 0;
return address ? format_caller(address) : std::string();
}

void set_device_slot_caller_formatter(CallerFormatter formatter) {
g_caller_formatter.store(formatter, std::memory_order_relaxed);
}

unsigned long device_proxy_ref_count(const void* device) {
if (!is_device_proxy(device)) return 0;
if (const ReferenceSettler settle = g_reference_settler.load(std::memory_order_acquire)) settle();
Expand Down Expand Up @@ -6729,6 +6757,7 @@ void reset_device_proxy_stats() {
for (unsigned slot = 0; slot < kMaxSlots; ++slot) {
g_hits[slot].store(0, std::memory_order_relaxed);
g_first[slot].store(false, std::memory_order_relaxed);
g_first_caller[slot].store(0, std::memory_order_relaxed);
g_beyond_warned[slot].store(false, std::memory_order_relaxed);
}
for (unsigned slot = 0; slot < kHeapSlots; ++slot) {
Expand Down
10 changes: 10 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,16 @@ void set_reference_settler(ReferenceSettler settle);
// counts. Calls made before that are not counted (the counter stays 0); they set only the first-call flags.
std::uint64_t device_slot_hits(unsigned slot);
bool device_slot_first_call_seen(unsigned slot);
// For a slot that enters the proxy's generic branch (no compile-time handler of its own; Xbox slots routed at run
// time and slots refused past the PC table included): the caller of its first call since the last
// reset_device_proxy_stats, named by the formatter below, the same text the first-call log line shows; "" before
// one. Diagnostic only: the first-call flag is set before the caller is stored, so a concurrent reader can see
// device_slot_first_call_seen true with this still "".
std::string device_slot_first_caller(unsigned slot);
// Names a caller address in the first-call log line and device_slot_first_caller. gta6rt sets
// rt::shims::module_offset (module+offset); with none set (the default, and in tests) the address is shown in hex.
using CallerFormatter = std::string (*)(std::uint64_t address);
void set_device_slot_caller_formatter(CallerFormatter formatter);
std::uint64_t heap_slot_hits(unsigned slot);
bool heap_slot_first_call_seen(unsigned slot);
void reset_device_proxy_stats();
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ int wmain(int argc, wchar_t** argv) {
leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
leonida::kernel_writes::set_unguarder(&rt::xmem::reopen_unowned);
// The first call of a forwarded device slot names its caller as module+offset (RT-526).
rt::d3d12_xs::set_device_slot_caller_formatter(&rt::shims::module_offset);
if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
: "error: cannot create the trace file\n");
Expand Down
8 changes: 8 additions & 0 deletions runtime/test/xs_proxy_mock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <string>
#include <new>
#include <utility>

Expand Down Expand Up @@ -1820,6 +1821,13 @@ int check_xbox_slots() {
// 78 SetFrameIntervalX(output, interval_us, latency, flags): S_OK, not forwarded to PC slot 78.
EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK);
EXPECT(mock->calls[78] == 0 && xs::device_slot_hits(78) == 1);
// RT-526: the first call of a slot in the generic branch keeps its caller (no formatter here: hex), a reset
// forgets it with the first-call flag, and the next call records it again.
EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0);
xs::reset_device_proxy_stats();
EXPECT(!xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).empty());
EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK);
EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0);
// 79 ScheduleFrameEventX(event, offset_us, pEvent, flags): S_OK, not forwarded to PC slot 79.
EXPECT(hr_of(raw(device, 79, 0, 0, 0x1234, 0)) == S_OK);
EXPECT(mock->calls[79] == 0 && xs::device_slot_hits(79) == 1);
Expand Down