2 * The PCI Library -- PCI config space access using NT SysDbg interface
4 * Copyright (c) 2022 Pali Rohár <pali@kernel.org>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "i386-io-windows.h"
19 #ifndef STATUS_UNSUCCESSFUL
20 #define STATUS_UNSUCCESSFUL (NTSTATUS)0xC0000001
22 #ifndef STATUS_NOT_IMPLEMENTED
23 #define STATUS_NOT_IMPLEMENTED (NTSTATUS)0xC0000002
25 #ifndef STATUS_INVALID_INFO_CLASS
26 #define STATUS_INVALID_INFO_CLASS (NTSTATUS)0xC0000003
28 #ifndef STATUS_ACCESS_DENIED
29 #define STATUS_ACCESS_DENIED (NTSTATUS)0xC0000022
31 #ifndef STATUS_DEBUGGER_INACTIVE
32 #define STATUS_DEBUGGER_INACTIVE (NTSTATUS)0xC0000354
36 #define BUS_DATA_TYPE LONG
38 #ifndef PCIConfiguration
39 #define PCIConfiguration (BUS_DATA_TYPE)4
42 #ifndef SYSDBG_COMMAND
43 #define SYSDBG_COMMAND ULONG
45 #ifndef SysDbgReadBusData
46 #define SysDbgReadBusData (SYSDBG_COMMAND)18
48 #ifndef SysDbgWriteBusData
49 #define SysDbgWriteBusData (SYSDBG_COMMAND)19
52 #ifndef SYSDBG_BUS_DATA
53 typedef struct _SYSDBG_BUS_DATA {
57 BUS_DATA_TYPE BusDataType;
60 } SYSDBG_BUS_DATA, *PSYSDBG_BUS_DATA;
61 #define SYSDBG_BUS_DATA SYSDBG_BUS_DATA
64 #ifndef PCI_SLOT_NUMBER
65 typedef struct _PCI_SLOT_NUMBER {
69 ULONG FunctionNumber:3;
74 } PCI_SLOT_NUMBER, *PPCI_SLOT_NUMBER;
75 #define PCI_SLOT_NUMBER PCI_SLOT_NUMBER
78 #ifdef NtSystemDebugControl
79 #undef NtSystemDebugControl
81 static NTSTATUS (NTAPI *MyNtSystemDebugControl)(SYSDBG_COMMAND Command, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength, PULONG ReturnLength);
82 #define NtSystemDebugControl MyNtSystemDebugControl
84 static BOOL debug_privilege_enabled;
85 static LUID luid_debug_privilege;
86 static BOOL revert_only_privilege;
87 static HANDLE revert_token;
90 static int win32_sysdbg_initialized;
93 win32_sysdbg_pci_bus_data(BOOL WriteBusData, BYTE BusNumber, BYTE DeviceNumber, BYTE FunctionNumber, BYTE Address, PVOID Buffer, BYTE BufferSize, PULONG Length)
95 SYSDBG_BUS_DATA sysdbg_cmd;
96 PCI_SLOT_NUMBER pci_slot;
98 if (!NtSystemDebugControl)
99 return STATUS_NOT_IMPLEMENTED;
101 memset(&pci_slot, 0, sizeof(pci_slot));
102 memset(&sysdbg_cmd, 0, sizeof(sysdbg_cmd));
104 sysdbg_cmd.Address = Address;
105 sysdbg_cmd.Buffer = Buffer;
106 sysdbg_cmd.Request = BufferSize;
107 sysdbg_cmd.BusDataType = PCIConfiguration;
108 sysdbg_cmd.BusNumber = BusNumber;
109 pci_slot.u.bits.DeviceNumber = DeviceNumber;
110 pci_slot.u.bits.FunctionNumber = FunctionNumber;
111 sysdbg_cmd.SlotNumber = pci_slot.u.AsULONG;
114 return NtSystemDebugControl(WriteBusData ? SysDbgWriteBusData : SysDbgReadBusData, &sysdbg_cmd, sizeof(sysdbg_cmd), NULL, 0, Length);
118 win32_sysdbg_setup(struct pci_access *a)
120 UINT prev_error_mode;
125 if (win32_sysdbg_initialized)
128 prev_error_mode = change_error_mode(SEM_FAILCRITICALERRORS);
129 ntdll = LoadLibrary(TEXT("ntdll.dll"));
130 change_error_mode(prev_error_mode);
133 a->debug("Cannot open ntdll.dll library.");
137 NtSystemDebugControl = (LPVOID)GetProcAddress(ntdll, "NtSystemDebugControl");
138 if (!NtSystemDebugControl)
140 a->debug("Function NtSystemDebugControl() is not supported.");
147 * Try to read PCI id register from PCI device 00:00.0.
148 * If this device does not exist and NT SysDbg API is working then
149 * NT SysDbg returns STATUS_UNSUCCESSFUL.
151 status = win32_sysdbg_pci_bus_data(FALSE, 0, 0, 0, 0, &id, sizeof(id), &ret_len);
152 if ((status >= 0 && ret_len == sizeof(id)) || status == STATUS_UNSUCCESSFUL)
154 win32_sysdbg_initialized = 1;
157 else if (status != STATUS_ACCESS_DENIED)
159 if (status == STATUS_NOT_IMPLEMENTED || status == STATUS_INVALID_INFO_CLASS)
160 a->debug("NT SysDbg is not supported.");
161 else if (status == STATUS_DEBUGGER_INACTIVE)
162 a->debug("NT SysDbg is disabled.");
164 a->debug("NT SysDbg returned error 0x%lx.", status);
167 NtSystemDebugControl = NULL;
171 a->debug("NT SysDbg returned Access Denied, trying again with Debug privilege...");
173 if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid_debug_privilege))
175 a->debug("Debug privilege is not supported.");
178 NtSystemDebugControl = NULL;
182 if (!enable_privilege(luid_debug_privilege, &revert_token, &revert_only_privilege))
184 a->debug("Cannot enable Debug privilege.");
187 NtSystemDebugControl = NULL;
191 status = win32_sysdbg_pci_bus_data(FALSE, 0, 0, 0, 0, &id, sizeof(id), &ret_len);
192 if ((status >= 0 && ret_len == sizeof(id)) || status == STATUS_UNSUCCESSFUL)
194 a->debug("Succeeded.");
195 debug_privilege_enabled = TRUE;
196 win32_sysdbg_initialized = 1;
200 revert_privilege(luid_debug_privilege, revert_token, revert_only_privilege);
202 revert_only_privilege = FALSE;
206 NtSystemDebugControl = NULL;
208 if (status == STATUS_NOT_IMPLEMENTED || status == STATUS_INVALID_INFO_CLASS)
209 a->debug("NT SysDbg is not supported.");
210 else if (status == STATUS_DEBUGGER_INACTIVE)
211 a->debug("NT SysDbg is disabled.");
212 else if (status == STATUS_ACCESS_DENIED)
213 a->debug("NT SysDbg returned Access Denied.");
215 a->debug("NT SysDbg returned error 0x%lx.", status);
221 win32_sysdbg_detect(struct pci_access *a)
223 if (!win32_sysdbg_setup(a))
230 win32_sysdbg_init(struct pci_access *a)
232 if (!win32_sysdbg_setup(a))
235 a->error("NT SysDbg PCI Bus Data interface cannot be accessed.");
240 win32_sysdbg_cleanup(struct pci_access *a UNUSED)
242 if (!win32_sysdbg_initialized)
245 if (debug_privilege_enabled)
247 revert_privilege(luid_debug_privilege, revert_token, revert_only_privilege);
249 revert_only_privilege = FALSE;
250 debug_privilege_enabled = FALSE;
255 NtSystemDebugControl = NULL;
257 win32_sysdbg_initialized = 0;
261 win32_sysdbg_read(struct pci_dev *d, int pos, byte *buf, int len)
266 if ((unsigned int)d->domain > 0 || (unsigned int)pos > 255 || (unsigned int)(pos+len) > 256)
269 status = win32_sysdbg_pci_bus_data(FALSE, d->bus, d->dev, d->func, pos, buf, len, &ret_len);
270 if (status < 0 || ret_len != (unsigned int)len)
277 win32_sysdbg_write(struct pci_dev *d, int pos, byte *buf, int len)
282 if ((unsigned int)d->domain > 0 || (unsigned int)pos > 255 || (unsigned int)(pos+len) > 256)
285 status = win32_sysdbg_pci_bus_data(TRUE, d->bus, d->dev, d->func, pos, buf, len, &ret_len);
286 if (status < 0 || ret_len != (unsigned int)len)
292 struct pci_methods pm_win32_sysdbg = {
294 "Win32 PCI config space access using NT SysDbg Bus Data interface",
298 win32_sysdbg_cleanup,
300 pci_generic_fill_info,
305 NULL /* cleanup_dev */