using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Text;
namespace XFESystem.NativeRuntime.Application;
// Copies only the kernel's versioned fixed integer ABI. It never calls a host
// driver or chooses an acceleration backend from PCI presence or FSP status.
internal static unsafe class NativeGpuDiagnostics
{
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_discovery_info", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong Info(uint field);
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_discovery_record", CallingConvention = CallingConvention.Cdecl)]
private static extern int CopyRecord(uint index, byte* record, uint bytes);
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_boot_probe_info", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong BootInfo(uint field);
internal static string ReadReport(out bool hasRtx5090)
{
hasRtx5090 = false;
const int RecordBytes = 272;
var text = new StringBuilder("XFESystem NVIDIA native port diagnostics\nScope: XFESystemGuest\nCUDA: unavailable\nGPU rasterization: unavailable\n");
if (Info(0) != 1 || Info(1) != RecordBytes || Info(13) != 32 || Info(2) > 8 || BootInfo(0) != 1)
return text.Append("Kernel diagnostic ABI: rejected\n").ToString();
text.Append("Discovery state: ").Append(Info(4)).Append("; devices: ").Append(Info(2)).Append('\n');
Span<byte> data = stackalloc byte[RecordBytes];
for (uint i = 0; i < Info(2); i++)
{
data.Clear();
fixed (byte* target = data)
if (CopyRecord(i, target, RecordBytes) != 1) throw new InvalidDataException("GPU record copy failed.");
if (BinaryPrimitives.ReadUInt32LittleEndian(data) != 1 || BinaryPrimitives.ReadUInt32LittleEndian(data[4..]) != RecordBytes)
throw new InvalidDataException("GPU record ABI mismatch.");
hasRtx5090 |= U32(data, 24) == 0x10de && U32(data, 28) == 0x2b85;
// Read through helpers over the span below instead of sharing
// either runtime's managed structs across the kernel boundary.
text.Append("PCI ").Append(U32(data, 12)).Append(':').Append(U32(data, 16)).Append('.').Append(U32(data, 20))
.Append("; Vendor 0x").Append(U32(data, 24).ToString("X4")).Append("; Device 0x").Append(U32(data, 28).ToString("X4")).Append('\n');
for (int bar = 0; bar < 6; bar++)
{
int offset = 80 + bar * 32;
ulong address = BinaryPrimitives.ReadUInt64LittleEndian(data[offset..]);
ulong bytes = BinaryPrimitives.ReadUInt64LittleEndian(data[(offset + 8)..]);
text.Append("BAR").Append(bar).Append(" 0x").Append(address.ToString("X"))
.Append("; bytes ").Append(bytes).Append("; size source ").Append(U32(data, offset + 24)).Append('\n');
}
}
text.Append("GB202 boot probe state: ").Append(BootInfo(1)).Append("; FSP scratch 0x").Append(BootInfo(2).ToString("X8")).Append('\n');
text.Append("FSP state 5 means secure boot observed; it does not mean GSP/RM/channel/CUDA initialized.\n");
text.Append("This version only queries assigned resources and reads the validated GB202 FSP status register.\n");
return text.ToString();
}
private static uint U32(ReadOnlySpan<byte> data, int offset) => BinaryPrimitives.ReadUInt32LittleEndian(data[offset..]);
}
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Text;
namespace XFESystem.NativeRuntime.Application;
// Copies only the kernel's versioned fixed integer ABI. It never calls a host
// driver or chooses an acceleration backend from PCI presence or FSP status.
internal static unsafe class NativeGpuDiagnostics
{
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_discovery_info", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong Info(uint field);
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_discovery_record", CallingConvention = CallingConvention.Cdecl)]
private static extern int CopyRecord(uint index, byte* record, uint bytes);
[DllImport("xfe_pal", EntryPoint = "xfe_gpu_boot_probe_info", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong BootInfo(uint field);
internal static string ReadReport(out bool hasRtx5090)
{
hasRtx5090 = false;
const int RecordBytes = 272;
var text = new StringBuilder("XFESystem NVIDIA native port diagnostics\nScope: XFESystemGuest\nCUDA: unavailable\nGPU rasterization: unavailable\n");
if (Info(0) != 1 || Info(1) != RecordBytes || Info(13) != 32 || Info(2) > 8 || BootInfo(0) != 1)
return text.Append("Kernel diagnostic ABI: rejected\n").ToString();
text.Append("Discovery state: ").Append(Info(4)).Append("; devices: ").Append(Info(2)).Append('\n');
Span<byte> data = stackalloc byte[RecordBytes];
for (uint i = 0; i < Info(2); i++)
{
data.Clear();
fixed (byte* target = data)
if (CopyRecord(i, target, RecordBytes) != 1) throw new InvalidDataException("GPU record copy failed.");
if (BinaryPrimitives.ReadUInt32LittleEndian(data) != 1 || BinaryPrimitives.ReadUInt32LittleEndian(data[4..]) != RecordBytes)
throw new InvalidDataException("GPU record ABI mismatch.");
hasRtx5090 |= U32(data, 24) == 0x10de && U32(data, 28) == 0x2b85;
// Read through helpers over the span below instead of sharing
// either runtime's managed structs across the kernel boundary.
text.Append("PCI ").Append(U32(data, 12)).Append(':').Append(U32(data, 16)).Append('.').Append(U32(data, 20))
.Append("; Vendor 0x").Append(U32(data, 24).ToString("X4")).Append("; Device 0x").Append(U32(data, 28).ToString("X4")).Append('\n');
for (int bar = 0; bar < 6; bar++)
{
int offset = 80 + bar * 32;
ulong address = BinaryPrimitives.ReadUInt64LittleEndian(data[offset..]);
ulong bytes = BinaryPrimitives.ReadUInt64LittleEndian(data[(offset + 8)..]);
text.Append("BAR").Append(bar).Append(" 0x").Append(address.ToString("X"))
.Append("; bytes ").Append(bytes).Append("; size source ").Append(U32(data, offset + 24)).Append('\n');
}
}
text.Append("GB202 boot probe state: ").Append(BootInfo(1)).Append("; FSP scratch 0x").Append(BootInfo(2).ToString("X8")).Append('\n');
text.Append("FSP state 5 means secure boot observed; it does not mean GSP/RM/channel/CUDA initialized.\n");
text.Append("This version only queries assigned resources and reads the validated GB202 FSP status register.\n");
return text.ToString();
}
private static uint U32(ReadOnlySpan<byte> data, int offset) => BinaryPrimitives.ReadUInt32LittleEndian(data[offset..]);
}