Microsoft MVP성태의 닷넷 이야기
닷넷: 2316. C# - Port I/O를 이용한 PCI Configuration Space 정보 열람 [링크 복사], [링크+제목 복사],
조회: 2848
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일

(시리즈 글이 4개 있습니다.)
디버깅 기술: 217. WinDbg - PCI 장치 열거
; https://www.sysnet.pe.kr/2/0/13873

닷넷: 2315. C# - PCI 장치 열거 (레지스트리, SetupAPI)
; https://www.sysnet.pe.kr/2/0/13877

닷넷: 2316. C# - Port I/O를 이용한 PCI Configuration Space 정보 열람
; https://www.sysnet.pe.kr/2/0/13881

닷넷: 2317. C# - Memory Mapped I/O를 이용한 PCI Configuration Space 정보 열람
; https://www.sysnet.pe.kr/2/0/13883




C# - Port I/O를 이용한 PCI Configuration Space 정보 열람

지난 글에서,

WinDbg - PCI 장치 열거
; https://www.sysnet.pe.kr/2/0/13873

"PCI Configuration Space" 정보를 Port I/O와 Memory Mapped I/O를 이용해 조회할 수 있다고 했는데요, 이들 중 Port I/O를 이용한 방법을 지난번에 만들어 둔 device driver의 도움을 받아,

커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램
; https://www.sysnet.pe.kr/2/0/12104

C# 코드로 가져와 보겠습니다. ^^




Port를 이용한 PCI Configuration Space 영역을 읽는 방법은 다음과 같습니다.

// 우선 0xcf8 포트로 읽어낼 PCI 장치와 offset을 지정하고,
(port write), 0xcf8, 0x80000000 | (bus << 16) | (device << 11) | (function << 8) | offset

// 0xcfc 포트로 4바이트 데이터를 읽어옵니다.
(port read), 0xcfc

0xcf8 포트에 쓰는 데이터 형식은 지난 글에서도 이미지로 설명한 적이 있는데요,

pci_tree_3.png

// PCI Configuration Address Register에 쓸 데이트 포맷

bit 0~1: 00
bit 2~7: Register Offset
bit 8~10: Function Number
bit 11~15: Device Number
bit 16~23: Bus Number
bit 24~30: Reserved
bit 31: Enable Bit

Bus, Device, Function을 제외하고 Register Offset으로 할당된 비트가 6비트지만, 사실 0~1 비트도 Register offset 영역에 포함되므로 엄밀히는 8비트 영역의 주소를 지정할 수 있습니다. 즉, Port I/O를 이용한 방식에서는 CAM(Configuration Access Mechanism)의 256 바이트까지만 읽어올 수 있고 PCIe에서 확장된 4KB 영역 전체를 읽어낼 수는 없습니다.

게다가 0~1 비트가 무조건 0이라는 점에서 Register offset 주소 지정은 최소 4바이트 단위로 지정해야 한다는 것을 유의하시면 됩니다.




그리하여, PCI 장치마다 0 ~ 256 바이트만큼을 읽어낼 수 있다는 것은 알았고, Configuration Address Register의 나머지 영역에 해당하는 Bus, Device, Function (줄여서 BDF) 값을 알아내는 것이 남았는데요, 즉, 원하는 PCI 장치의 Configuration Space를 읽어오려면, 이들 값을 알고 있어야 합니다.

그래서 지난 글을 통해 PCI 장치를 열거하는 방법을 알아본 건데요,

C# - PCI 장치 열거 (레지스트리, SetupAPI)
; https://www.sysnet.pe.kr/2/0/13877

한 가지 아쉬운 점은, 레지스트리를 이용하든 / SetupAPI를 이용하든 BDF 값을 필드별로 정확히 알아낼 수는 없고 문자열로부터 추출해야 한다는 것입니다.

가령 SetupAPI의 경우 지난 글에서는 SetupDiGetDeviceProperty를 이용해 DEVPROPKEY에 해당하는 값들을 구해왔는데요,

DEVPROPKEY structure
; https://learn.microsoft.com/en-us/windows-hardware/drivers/install/devpropkey

Property Keys
; https://learn.microsoft.com/en-us/windows-hardware/drivers/install/property-keys

"Unified Device Property Model Properties"로 나열되는 적절한 DEVPROPKEY 중에는 (제가 찾아본 한계로) Device, Function 값을 알 수 있는 키는 없었습니다. 대신, SetupDiGetDeviceRegistryProperty를 이용해 SPDRP_LOCATION_INFORMATION을 이용하면 다음과 같이 문자열로 추출하는 것이 가능합니다.

// SPDRP_LOCATION_INFORMATION으로 반환한 예 (문자열)

"PCI bus 0, device 2, function 0"

참고로, 단순히 BDF 값만 알아내는 것이라면 레지스트리를 이용하는 것도 좋은 방법입니다. 실제로 Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ 하위의 PCI 장치를 살펴보면 이런 식으로 LocationInformation 값이 주어지는데요,

@System32\drivers\pci.sys,#65536;PCI bus %1, device %2, function %3;(1,0,1)

마지막의 (1,0,1) 값이 각각 (Bus, Device, Function)을 나타냅니다.




이렇게 해서 해당 머신에 장착된 PCI 장치의 BDF 값을 열거했으면 이제 Port I/O를 이용해 Configuration Space를 읽어오는 것은 어렵지 않습니다.

static unsafe void Main(string[] args)
{
    using (KernelMemoryIO portIo = new KernelMemoryIO())
    {
        if (portIo.IsInitialized == false)
        {
            Console.WriteLine("Failed to open device");
            return;
        }

        foreach (BDF bdf in PciHelper.EnumeatePCI())
        {
            Console.WriteLine($"Bus: {bdf.Bus}, Device: {bdf.Device}, Function: {bdf.Function}");

            List<byte> bytes = new List<byte>();
            for (uint where = 0; where < 256; where += 4)
            {
                uint configCommand = CONFIG_CMD(bdf.Bus, bdf.Device, bdf.Function, where);
                portIo.Outportl(0xcf8, configCommand);

                uint data = portIo.Inportl(0xcfc, out int errorNo);

                if (errorNo != 0)
                {
                    Console.WriteLine($"Error: {errorNo}");
                    break;
                }
                else
                {
                    byte[] buffer = BitConverter.GetBytes(data);
                    bytes.AddRange(buffer);
                }
            }

            for (int i = 0; i < bytes.Count; i++)
            {
                Console.Write($"{bytes[i]:x2} ");
                if (i % 16 == 15)
                {
                    Console.WriteLine();
                }
            }

            Console.WriteLine();
        }
    }
}

위의 프로그램을 Hyper-V Gen1 VM에서 실행하면 대충 이런 결과가 나옵니다.

c:\ex> ConsoleApp1.exe
Bus: 0, Device: 7, Function: 1
86 80 10 71 07 00 00 02 01 00 01 06 00 00 80 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...[생략]...
00 00 00 00 00 00 00 00 30 0f 00 00 00 00 00 00

Bus: 0, Device: 7, Function: 0
86 80 10 71 07 00 00 02 01 00 01 06 00 00 80 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...[생략]...
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Bus: 0, Device: 0, Function: 0
86 80 92 71 06 00 00 02 03 00 00 06 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...[생략]...
00 00 00 00 00 00 00 00 20 0f 00 00 00 00 00 00

참고로, 그것의 첫 64바이트는 포맷이 고정돼 있기 때문에,

pci_config_space_struct_1.png

이에 맞춰 구조체를 정의해 출력하면 좀 더 다듬어진 결과를 얻을 수 있습니다. ^^

byte[] configSpaces = bytes.ToArray();
fixed (byte* ptr = configSpaces)
{
    PCIConfigSpace pciInfo = Marshal.PtrToStructure<PCIConfigSpace>((IntPtr)ptr);
    Console.WriteLine($"VendorId: {pciInfo.VendorId:x4}");
    Console.WriteLine($"DeviceId: {pciInfo.DeviceId:x4}");
}

[System.Runtime.CompilerServices.InlineArray(3)]
public struct ClassCode
{
    private byte _element0; // public 접근을 허용하지만 실용적이지 않음
                            // 필드는 단 한 개만 정의할 수 있음
}

[StructLayout(LayoutKind.Sequential)]
public struct PCIConfigSpace
{
    public ushort VendorId;
    public ushort DeviceId;

    public ushort Command;
    public ushort Status;

    public byte RevisionId;
    public ClassCode ClassCode;

    public byte CacheLineS;
    public byte LatTimer;
    public byte HeaderType;
    public byte BIST;

    public uint BAR1;
    public uint BAR2;
    public uint BAR3;
    public uint BAR4;
    public uint BAR5;
    public uint BAR6;

    public uint CardbusCISPointer;

    public ushort SubsystemVendorId;
    public ushort SubsystemId;

    public uint ExpansionROMBaseAddress;

    public byte CapPointer;
    public byte Reserved1;
    public byte Reserved2;
    public byte Reserved3;

    public uint Reserved;

    public byte InterruptLine;
    public byte InterruptPin;
    public byte MinGnt;
    public byte MaxLat;
}

뭐, 이 정도면 (DDK 영역이 아닌) 사용자 모드의 소프트웨어 개발자에게는 차고 넘칠 정도의 정보를 가져온 것이니 굳이 ECAM 4KB 영역까지 읽어낼 필요는 없을 것 같습니다. ^^

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




검색하다 보니, PCI 장치 열거를 통해 해당 환경이 XBox인지 확인하는 코드가 있군요. ^^

PCI 버스 접근
; https://blog.naver.com/kgh2797/90063940615

/* Check for Xbox by identifying device at PCI 0:0:0, if it's
* 0x10de/0x02a5 then we're running on an Xbox */

WRITE_PORT_ULONG((ULONG*) 0xcf8, CONFIG_CMD(0, 0, 0));
PciId = READ_PORT_ULONG((ULONG*) 0xcfc);
if (0x02a510de == PciId)
{
    XboxMachInit(CmdLine);
}
else
{
    PcMachInit(CmdLine);
}

#define CONFIG_CMD(bus, dev_fn, where) \
 (0x80000000 | (((ULONG)(bus)) << 16) | (((dev_fn) & 0x1F) << 11) | (((dev_fn) & 0xE0) << 3) | ((where) & ~3))





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 2/12/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12208정성태4/12/202015932Linux: 29. 리눅스 환경에서 C/C++ 프로그램이 Segmentation fault 에러가 발생한 경우
12207정성태4/2/202015738스크립트: 19. Windows PowerShell의 NonInteractive 모드
12206정성태4/2/202018386오류 유형: 613. 파일 잠금이 바로 안 풀린다면? - The process cannot access the file '...' because it is being used by another process.
12205정성태4/2/202015048스크립트: 18. Powershell에서는 cmd.exe의 명령어를 지원하진 않습니다.
12204정성태4/1/202015059스크립트: 17. Powershell 명령어에 ';' (semi-colon) 문자가 포함된 경우
12203정성태3/18/202017882오류 유형: 612. warning: 'C:\ProgramData/Git/config' has a dubious owner: '...'.
12202정성태3/18/202021144개발 환경 구성: 486. .NET Framework 프로젝트를 위한 GitLab CI/CD Runner 구성
12201정성태3/18/202018369오류 유형: 611. git-credential-manager.exe: Using credentials for username "Personal Access Token". [1]
12200정성태3/18/202018486VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
12199정성태3/17/202016127오류 유형: 610. C# - CodeDomProvider 사용 시 Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '...\f2_6uod0.tmp'.
12198정성태3/17/202019513오류 유형: 609. SQL 서버 접속 시 "Cannot open user default database. Login failed."
12197정성태3/17/202018759VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기 [1]
12196정성태3/17/202015930오류 유형: 608. The ServicedComponent being invoked is not correctly configured (Use regsvcs to re-register).
12195정성태3/16/202018249.NET Framework: 902. C# - 프로세스의 모든 핸들을 열람 - 세 번째 이야기
12194정성태3/16/202020975오류 유형: 607. PostgreSQL - Npgsql.NpgsqlException: sorry, too many clients already
12193정성태3/16/202017866개발 환경 구성: 485. docker - SAP Adaptive Server Enterprise 컨테이너 실행 [1]
12192정성태3/14/202019878개발 환경 구성: 484. docker - Sybase Anywhere 16 컨테이너 실행
12191정성태3/14/202021013개발 환경 구성: 483. docker - OracleXE 컨테이너 실행 [1]
12190정성태3/14/202015559오류 유형: 606. Docker Desktop 업그레이드 시 "The process cannot access the file 'C:\Program Files\Docker\Docker\resources\dockerd.exe' because it is being used by another process."
12189정성태3/13/202021171개발 환경 구성: 482. Facebook OAuth 처리 시 상태 정보 전달 방법과 "유효한 OAuth 리디렉션 URI" 설정 규칙
12188정성태3/13/202025989Windows: 169. 부팅 시점에 실행되는 chkdsk 결과를 확인하는 방법
12187정성태3/12/202015517오류 유형: 605. NtpClient was unable to set a manual peer to use as a time source because of duplicate error on '...'.
12186정성태3/12/202017379오류 유형: 604. The SysVol Permissions for one or more GPOs on this domain controller and not in sync with the permissions for the GPOs on the Baseline domain controller.
12185정성태3/11/202017949오류 유형: 603. The browser service was unable to retrieve a list of servers from the browser master...
12184정성태3/11/202019886오류 유형: 602. Automatic certificate enrollment for local system failed (0x800706ba) The RPC server is unavailable. [3]
12183정성태3/11/202017681오류 유형: 601. Warning: DsGetDcName returned information for \\[...], when we were trying to reach [...].
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...