XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Buffers.Binary;
using System.Text;

namespace XFESystem.NativeRuntime.Application;

// Pure rendering of the kernel's integer ABI; injectable readers let host
// checks verify physical-failure reports without touching a GPU or host driver.
public static unsafe class NativeGpuReportFormatter
{
    public delegate int RecordReader(uint index, byte* record, uint bytes);

    public static string Format(Func<uint, ulong> info, RecordReader copyRecord, Func<uint, ulong> bootInfo,
        out bool hasRtx5090)
    {
        hasRtx5090 = false;
        const int recordBytes = 272;
        ulong count = info(2);
        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 || count > 8 || bootInfo(0) != 1)
            return text.Append("Kernel diagnostic ABI: rejected\n").ToString();
        text.Append("Discovery state: ").Append(info(4)).Append("; devices: ").Append(count).Append('\n');
        if (info(14) == 1)
        {
            text.Append("UEFI resource handoff: ").Append(HandoffState(info(15))).Append(" (").Append(info(15)).Append("); devices: ").Append(info(16)).Append('\n');
            text.Append("RTX 5090 firmware match: ").Append(MatchState(info(18))).Append(" (").Append(info(18)).Append(")\n");
            text.Append("BAR0 firmware resource: ").Append(BarState(info(19))).Append(" (").Append(info(19)).Append(")\n");
        }
        else text.Append("UEFI handoff detail: unavailable in this kernel\n");
        Span<byte> data = stackalloc byte[recordBytes];
        for (uint i = 0; i < count; i++)
        {
            data.Clear();
            fixed (byte* target = data)
                if (copyRecord(i, target, recordBytes) != 1) throw new InvalidDataException("GPU record copy failed.");
            if (U32(data, 0) != 1 || U32(data, 4) != recordBytes)
                throw new InvalidDataException("GPU record ABI mismatch.");
            hasRtx5090 |= U32(data, 24) == 0x10de && U32(data, 28) == 0x2b85;
            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');
            text.Append("PCI command 0x").Append(U32(data, 44).ToString("X4")).Append("; PCIe status ").Append(U32(data, 56)).Append('\n');
            for (int bar = 0; bar < 6; bar++)
            {
                int offset = 80 + bar * 32;
                text.Append("BAR").Append(bar).Append(" 0x").Append(BinaryPrimitives.ReadUInt64LittleEndian(data[offset..]).ToString("X"))
                    .Append("; bytes ").Append(BinaryPrimitives.ReadUInt64LittleEndian(data[(offset + 8)..]))
                    .Append("; size source ").Append(U32(data, offset + 24))
                    .Append("; flags 0x").Append(U32(data, offset + 20).ToString("X2")).Append('\n');
            }
        }
        ulong state = bootInfo(1);
        text.Append("GB202 boot probe state: ").Append(state).Append('\n');
        bool detailed = bootInfo(7) == 1;
        // Earlier kernels have no sampled flag. States 4..7 are only reached
        // after MmioRead32 returns; a zero value at state 2 was never a sample.
        bool sampled = detailed ? bootInfo(6) == 1 : state >= 4 && state <= 7;
        if (sampled) text.Append("FSP scratch 0x").Append(bootInfo(2).ToString("X8")).Append(" (register read completed)\n");
        else text.Append("FSP scratch: not read\n");
        if (state == 2)
            text.Append("Probe stopped before MMIO: ").Append(detailed ? Rejection(bootInfo(5)) : "required BAR0 resource validation failed; detailed reason unavailable").Append('\n');
        if (state == 3) text.Append("Probe stopped before MMIO: register page mapping failed\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 string HandoffState(ulong state) => state switch
    {
        0 => "not attempted", 1 => "invalid boot context", 2 => "EFI system table not in mapped firmware memory",
        3 => "EFI system table signature rejected", 4 => "EFI system table header/CRC rejected",
        5 => "configuration table count rejected", 6 => "configuration table array not mapped",
        7 => "GPU resource table missing", 8 => "duplicate GPU resource table",
        9 => "GPU resource table not mapped", 10 => "GPU resource table schema/CRC rejected",
        11 => "accepted", _ => "unknown status"
    };
    private static string MatchState(ulong state) => state switch
    {
        0 => "not attempted", 1 => "no accepted handoff", 2 => "PCI address not in handoff",
        3 => "PCI identity/command mismatch", 4 => "duplicate PCI address", 5 => "matched", _ => "unknown status"
    };
    private static string BarState(ulong state) => state switch
    {
        0 => "not attempted", 1 => "no accepted firmware descriptor",
        2 => "descriptor index/type/flags rejected", 3 => "live BAR type/width/prefetch mismatch",
        4 => "address/size invalid or conflicts with ReBAR", 5 => "applied", _ => "unknown status"
    };
    private static string Rejection(ulong reason) => reason switch
    {
        1 => "missing output storage", 2 => "invalid boot memory map/device record", 3 => "device ABI mismatch",
        4 => "PCI identity/address/type rejected", 5 => "PCIe capability not validated", 6 => "PCI memory decode disabled",
        7 => "BAR0 index/reserved field rejected", 8 => "BAR0 length has no validated firmware source",
        9 => "BAR0 memory/size flags rejected", 10 => "BAR0 address/length cannot contain the full register page",
        11 => "boot memory map entry invalid", 12 => "BAR0 overlaps protected memory",
        _ => "unknown rejection reason"
    };
    private static uint U32(ReadOnlySpan<byte> data, int offset) => BinaryPrimitives.ReadUInt32LittleEndian(data[offset..]);
}