ReactOS 0.4.17-dev-1190-g4c812f4
hookhal.c File Reference
#include <pci.h>
#include <debug.h>
Include dependency graph for hookhal.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOLEAN NTAPI PciTranslateBusAddress (IN INTERFACE_TYPE InterfaceType, IN ULONG BusNumber, IN PHYSICAL_ADDRESS BusAddress, OUT PULONG AddressSpace, OUT PPHYSICAL_ADDRESS TranslatedAddress)
 
 _Requires_lock_held_ (PciGlobalLock)
 Finds the PDO of the function in a slot.
 
static VOID NTAPI PciStripPrivateDescriptors (_Inout_ PCM_RESOURCE_LIST ResourceList)
 Removes the device private descriptors from an assigned resource list.
 
static NTSTATUS NTAPI PciAssignAndProgramLegacy (_In_ PPCI_PDO_EXTENSION PdoExtension, _In_ PPCI_COMMON_HEADER PciData, _In_ PUNICODE_STRING RegistryPath, _In_opt_ PUNICODE_STRING DriverClassName, _In_ PDRIVER_OBJECT DriverObject, _In_opt_ PDEVICE_OBJECT DeviceObject, _Out_ PCM_RESOURCE_LIST *Resources)
 Asks the PnP manager for the ranges of a legacy claimed function and programs them. On success Resources receives the assignment, stripped of private descriptors.
 
NTSTATUS NTAPI PciAssignSlotResources (_In_ PUNICODE_STRING RegistryPath, _In_opt_ PUNICODE_STRING DriverClassName, _In_ PDRIVER_OBJECT DriverObject, _In_opt_ PDEVICE_OBJECT DeviceObject, _In_ INTERFACE_TYPE BusType, _In_ ULONG BusNumber, _In_ ULONG SlotNumber, _Inout_ PCM_RESOURCE_LIST *AllocatedResources)
 Assigns resources to a function claimed by a driver outside of PnP.
 
VOID NTAPI PciHookHal (VOID)
 
VOID NTAPI PciRestoreHalHooks (VOID)
 Gives the HAL back the routines PciHookHal replaced.
 

Variables

pHalTranslateBusAddress PcipSavedTranslateBusAddress
 
pHalAssignSlotResources PcipSavedAssignSlotResources
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file hookhal.c.

Function Documentation

◆ _Requires_lock_held_()

_Requires_lock_held_ ( PciGlobalLock  )

Finds the PDO of the function in a slot.

Definition at line 52 of file hookhal.c.

59{
60 PPCI_FDO_EXTENSION DeviceExtension;
64
65 /* Now search for the extension */
67 while (DeviceExtension)
68 {
69 /* If we found it, break out */
70 if (DeviceExtension->BaseBus == BusNumber) break;
71
72 /* Move to the next device */
73 DeviceExtension = (PPCI_FDO_EXTENSION)DeviceExtension->List.Next;
74 }
75
76 /* Check if the device extension for the bus was found */
77 if (!DeviceExtension)
78 {
79 /* It wasn't, bail out */
80 DPRINT1("PCIX - Could not find PCI bus FDO. Bus Number = 0x%x\n", BusNumber);
81 return NULL;
82 }
83
84 /* Acquire this device's lock */
86 KeWaitForSingleObject(&DeviceExtension->ChildListLock,
89 FALSE,
90 NULL);
91
92 /* Loop every child PDO */
93 for (PdoExtension = DeviceExtension->ChildPdoList;
96 {
97 /* A function that left the slot keeps its PDO until PnP removes it */
98 if (PdoExtension->NotPresent || PdoExtension->ReportedMissing)
99 continue;
100
101 /* Check if the function number and header data matches */
102 if ((PdoExtension->Slot.u.bits.FunctionNumber == PciSlot.u.bits.FunctionNumber) &&
103 (PdoExtension->Slot.u.bits.DeviceNumber == PciSlot.u.bits.DeviceNumber))
104 {
105 /* This is considered to be the same PDO */
106 ASSERT(PdoExtension->Slot.u.AsULONG == PciSlot.u.AsULONG);
107 break;
108 }
109 }
110
111 /* Release this device's lock */
112 KeSetEvent(&DeviceExtension->ChildListLock, IO_NO_INCREMENT, FALSE);
114
115 /* Check if we found something */
116 if (!PdoExtension)
117 {
118 /* Let the debugger know */
119 DPRINT1("PCIX - Could not find PDO for device @ %x.%x.%x\n",
120 BusNumber,
121 PciSlot.u.bits.DeviceNumber,
122 PciSlot.u.bits.FunctionNumber);
123 }
124
125 /* If the search found something, this is non-NULL, otherwise it's NULL */
126 return PdoExtension;
127}
#define DPRINT1
Definition: precomp.h:8
SINGLE_LIST_ENTRY PciFdoExtensionListHead
Definition: fdo.c:18
while(CdLookupNextInitialFileDirent(IrpContext, Fcb, FileContext))
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
struct _PCI_FDO_EXTENSION * PPCI_FDO_EXTENSION
@ PdoExtension
Definition: precomp.h:61
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define KeLeaveCriticalRegion()
Definition: ke_x.h:119
#define KeEnterCriticalRegion()
Definition: ke_x.h:88
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:38
_In_ ULONG _In_ PCI_SLOT_NUMBER PciSlot
Definition: pciidex.h:66
_In_ ULONG BusNumber
Definition: pciidex.h:65
SINGLE_LIST_ENTRY List
Definition: pci.h:243
struct _PCI_PDO_EXTENSION * ChildPdoList
Definition: pci.h:253
BOOLEAN BaseBus
Definition: pci.h:260
KEVENT ChildListLock
Definition: pci.h:252
struct _PCI_SLOT_NUMBER::@5064::@5065 bits
union _PCI_SLOT_NUMBER::@5064 u
struct _SINGLE_LIST_ENTRY * Next
Definition: ntbasedef.h:641
_In_ WDFIORESREQLIST _In_ ULONG SlotNumber
Definition: wdfresource.h:68
#define IO_NO_INCREMENT
Definition: iotypes.h:598
@ Executive
Definition: ketypes.h:467

◆ PciAssignAndProgramLegacy()

static NTSTATUS NTAPI PciAssignAndProgramLegacy ( _In_ PPCI_PDO_EXTENSION  PdoExtension,
_In_ PPCI_COMMON_HEADER  PciData,
_In_ PUNICODE_STRING  RegistryPath,
_In_opt_ PUNICODE_STRING  DriverClassName,
_In_ PDRIVER_OBJECT  DriverObject,
_In_opt_ PDEVICE_OBJECT  DeviceObject,
_Out_ PCM_RESOURCE_LIST *  Resources 
)
static

Asks the PnP manager for the ranges of a legacy claimed function and programs them. On success Resources receives the assignment, stripped of private descriptors.

Definition at line 166 of file hookhal.c.

174{
176 PCM_RESOURCE_LIST Assigned = NULL;
178
179 *Resources = NULL;
180
182 if (!NT_SUCCESS(Status))
183 return Status;
184
185 /* The shared empty list asks for nothing and must not be freed */
188
189 PdoExtension->LegacyDriver = TRUE;
195 &Assigned);
198
199 if (!NT_SUCCESS(Status))
200 {
201 ASSERT(Assigned == NULL);
202 return Status;
203 }
204
205 /* Program the assignment the way a PnP start does */
206 PdoExtension->CommandEnables |= (PCI_ENABLE_IO_SPACE |
210
212 if (!NT_SUCCESS(Status))
213 {
214 if (Assigned)
215 ExFreePoolWithTag(Assigned, 0);
216 return Status;
217 }
218
219 /* The caller only gets the ranges and the interrupt */
220 if (Assigned)
222
223 *Resources = Assigned;
224 return STATUS_SUCCESS;
225}
LONG NTSTATUS
Definition: precomp.h:26
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
NTSTATUS NTAPI PciSetResources(IN PPCI_PDO_EXTENSION PdoExtension, IN BOOLEAN DoReset, IN BOOLEAN SomethingSomethingDarkSide)
Definition: enum.c:2490
NTSTATUS NTAPI PciBuildRequirementsList(IN PPCI_PDO_EXTENSION PdoExtension, IN PPCI_COMMON_HEADER PciData, OUT PIO_RESOURCE_REQUIREMENTS_LIST *Buffer)
Definition: enum.c:756
PIO_RESOURCE_REQUIREMENTS_LIST PciZeroIoResourceRequirements
Definition: enum.c:18
BOOLEAN NTAPI PciComputeNewCurrentSettings(IN PPCI_PDO_EXTENSION PdoExtension, IN PCM_RESOURCE_LIST ResourceList)
Definition: enum.c:55
Status
Definition: gdiplustypes.h:24
static VOID NTAPI PciStripPrivateDescriptors(_Inout_ PCM_RESOURCE_LIST ResourceList)
Removes the device private descriptors from an assigned resource list.
Definition: hookhal.c:135
NTSTATUS NTAPI IoAssignResources(_In_ PUNICODE_STRING RegistryPath, _In_opt_ PUNICODE_STRING DriverClassName, _In_ PDRIVER_OBJECT DriverObject, _In_opt_ PDEVICE_OBJECT DeviceObject, _In_opt_ PIO_RESOURCE_REQUIREMENTS_LIST RequestedResources, _Inout_ PCM_RESOURCE_LIST *AllocatedResources)
Definition: iorsrce.c:1134
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define STATUS_SUCCESS
Definition: shellext.h:65
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2061
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
_In_ WDFIORESREQLIST RequirementsList
Definition: wdfresource.h:65
_In_opt_ PUNICODE_STRING DriverClassName
Definition: halfuncs.h:156
#define PCI_ENABLE_BUS_MASTER
Definition: iotypes.h:3633
#define PCI_ENABLE_IO_SPACE
Definition: iotypes.h:3631
#define PCI_ENABLE_MEMORY_SPACE
Definition: iotypes.h:3632

Referenced by PciAssignSlotResources().

◆ PciAssignSlotResources()

NTSTATUS NTAPI PciAssignSlotResources ( _In_ PUNICODE_STRING  RegistryPath,
_In_opt_ PUNICODE_STRING  DriverClassName,
_In_ PDRIVER_OBJECT  DriverObject,
_In_opt_ PDEVICE_OBJECT  DeviceObject,
_In_ INTERFACE_TYPE  BusType,
_In_ ULONG  BusNumber,
_In_ ULONG  SlotNumber,
_Inout_ PCM_RESOURCE_LIST *  AllocatedResources 
)

Assigns resources to a function claimed by a driver outside of PnP.

Definition at line 232 of file hookhal.c.

241{
242 PCI_COMMON_HEADER PciData;
244 PDEVICE_OBJECT ExistingDeviceObject;
246 PAGED_CODE();
248
250
251 if (BusType != PCIBus)
253
254 /* A removed bus is unlinked under this lock before its PDOs are deleted */
257
258 PdoExtension = PciFindPdoByLocation(BusNumber, SlotNumber);
259 if (!PdoExtension)
261 else if (PdoExtension->DeviceState != PciNotStarted)
263 else
265
266 if (NT_SUCCESS(Status))
267 {
268 ASSERT(DeviceObject != PdoExtension->PhysicalDeviceObject);
269
270 /* The routing goes to the cache first so interrupt arbitration can find it */
273 BusNumber,
275 PciData.u.type0.InterruptLine,
276 PciData.u.type0.InterruptPin,
277 PciData.BaseClass,
278 PciData.SubClass,
279 PdoExtension->ParentFdoExtension->
282 &ExistingDeviceObject);
283 }
284
285 if (NT_SUCCESS(Status))
286 {
288 &PciData,
294
295 /* Give the slot back to the device object cached for it before */
296 if (!NT_SUCCESS(Status))
297 {
298 PciCacheLegacyDeviceRouting(ExistingDeviceObject,
299 BusNumber,
301 PciData.u.type0.InterruptLine,
302 PciData.u.type0.InterruptPin,
303 PciData.BaseClass,
304 PciData.SubClass,
305 PdoExtension->ParentFdoExtension->
308 NULL);
309 }
310 }
311
314 return Status;
315}
#define PAGED_CODE()
PDEVICE_OBJECT PhysicalDeviceObject
Definition: btrfs_drv.h:1157
KEVENT PciGlobalLock
Definition: init.c:20
@ PciNotStarted
Definition: pci.h:168
VOID NTAPI PciReadDeviceConfig(IN PPCI_PDO_EXTENSION DeviceExtension, IN PVOID Buffer, IN ULONG Offset, IN ULONG Length)
Definition: config.c:149
pHalAssignSlotResources PcipSavedAssignSlotResources
Definition: hookhal.c:19
static NTSTATUS NTAPI PciAssignAndProgramLegacy(_In_ PPCI_PDO_EXTENSION PdoExtension, _In_ PPCI_COMMON_HEADER PciData, _In_ PUNICODE_STRING RegistryPath, _In_opt_ PUNICODE_STRING DriverClassName, _In_ PDRIVER_OBJECT DriverObject, _In_opt_ PDEVICE_OBJECT DeviceObject, _Out_ PCM_RESOURCE_LIST *Resources)
Asks the PnP manager for the ranges of a legacy claimed function and programs them....
Definition: hookhal.c:166
_Inout_opt_ PDEVICE_OBJECT _Inout_opt_ PDEVICE_OBJECT _Inout_opt_ PDEVICE_OBJECT _Inout_opt_ PCM_RESOURCE_LIST * AllocatedResources
Definition: ndis.h:4643
#define STATUS_DEVICE_DOES_NOT_EXIST
Definition: ntstatus.h:522
#define STATUS_INVALID_OWNER
Definition: ntstatus.h:420
@ PCIBus
Definition: restypes.h:127
NTSTATUS NTAPI PciCacheLegacyDeviceRouting(IN PDEVICE_OBJECT DeviceObject, IN ULONG BusNumber, IN ULONG SlotNumber, IN UCHAR InterruptLine, IN UCHAR InterruptPin, IN UCHAR BaseClass, IN UCHAR SubClass, IN PDEVICE_OBJECT PhysicalDeviceObject, IN PPCI_PDO_EXTENSION PdoExtension, OUT PDEVICE_OBJECT *pFoundDeviceObject)
Definition: routintf.c:266
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE BusType
Definition: halfuncs.h:159
#define PCI_COMMON_HDR_LENGTH
Definition: iotypes.h:3609

Referenced by PciHookHal().

◆ PciHookHal()

VOID NTAPI PciHookHal ( VOID  )

Definition at line 319 of file hookhal.c.

320{
321 /* Save the old HAL routines */
326
327 /* Take over the HAL's Bus Handler functions */
330}
pHalTranslateBusAddress PcipSavedTranslateBusAddress
Definition: hookhal.c:18
NTSTATUS NTAPI PciAssignSlotResources(_In_ PUNICODE_STRING RegistryPath, _In_opt_ PUNICODE_STRING DriverClassName, _In_ PDRIVER_OBJECT DriverObject, _In_opt_ PDEVICE_OBJECT DeviceObject, _In_ INTERFACE_TYPE BusType, _In_ ULONG BusNumber, _In_ ULONG SlotNumber, _Inout_ PCM_RESOURCE_LIST *AllocatedResources)
Assigns resources to a function claimed by a driver outside of PnP.
Definition: hookhal.c:232
BOOLEAN NTAPI PciTranslateBusAddress(IN INTERFACE_TYPE InterfaceType, IN ULONG BusNumber, IN PHYSICAL_ADDRESS BusAddress, OUT PULONG AddressSpace, OUT PPHYSICAL_ADDRESS TranslatedAddress)
Definition: hookhal.c:25
#define HalPciTranslateBusAddress
Definition: halfuncs.h:41
#define HalPciAssignSlotResources
Definition: halfuncs.h:42

Referenced by DriverEntry().

◆ PciRestoreHalHooks()

VOID NTAPI PciRestoreHalHooks ( VOID  )

Gives the HAL back the routines PciHookHal replaced.

Definition at line 337 of file hookhal.c.

Referenced by PciDriverUnload().

◆ PciStripPrivateDescriptors()

static VOID NTAPI PciStripPrivateDescriptors ( _Inout_ PCM_RESOURCE_LIST  ResourceList)
static

Removes the device private descriptors from an assigned resource list.

Definition at line 135 of file hookhal.c.

137{
138 PCM_PARTIAL_RESOURCE_LIST PartialList;
140
141 ASSERT(ResourceList->Count == 1);
142 PartialList = &ResourceList->List[0].PartialResourceList;
143
144 Target = 0;
145 for (Source = 0; Source < PartialList->Count; Source++)
146 {
148 continue;
149
150 if (Target != Source)
151 PartialList->PartialDescriptors[Target] = PartialList->PartialDescriptors[Source];
152
153 Target++;
154 }
155
156 PartialList->Count = Target;
157}
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3169
#define CmResourceTypeDevicePrivate
Definition: restypes.h:112
CM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptors[1]
Definition: restypes.h:100
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
_Must_inspect_result_ _In_ WDFIORESREQLIST _In_opt_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFIORESLIST * ResourceList
Definition: wdfresource.h:309

Referenced by PciAssignAndProgramLegacy().

◆ PciTranslateBusAddress()

BOOLEAN NTAPI PciTranslateBusAddress ( IN INTERFACE_TYPE  InterfaceType,
IN ULONG  BusNumber,
IN PHYSICAL_ADDRESS  BusAddress,
OUT PULONG  AddressSpace,
OUT PPHYSICAL_ADDRESS  TranslatedAddress 
)

Definition at line 25 of file hookhal.c.

30{
31 /* The HAL has the final say on any bus it registered */
37 {
38 return TRUE;
39 }
40
41 /* Buses the HAL never saw still map PCI addresses 1:1 to processor addresses */
42 if (InterfaceType != PCIBus)
43 return FALSE;
44
46 return TRUE;
47}
_Must_inspect_result_ _In_ WDFDEVICE _In_ LPCGUID InterfaceType
Definition: wdffdo.h:463
_In_ ULONG _In_ PHYSICAL_ADDRESS BusAddress
Definition: iofuncs.h:2273
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG _Out_ PPHYSICAL_ADDRESS TranslatedAddress
Definition: iofuncs.h:2275
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG AddressSpace
Definition: iofuncs.h:2274

Referenced by PciHookHal().

Variable Documentation

◆ PcipSavedAssignSlotResources

pHalAssignSlotResources PcipSavedAssignSlotResources

Definition at line 19 of file hookhal.c.

Referenced by PciAssignSlotResources(), PciHookHal(), and PciRestoreHalHooks().

◆ PcipSavedTranslateBusAddress

pHalTranslateBusAddress PcipSavedTranslateBusAddress

Definition at line 18 of file hookhal.c.

Referenced by PciHookHal(), PciRestoreHalHooks(), and PciTranslateBusAddress().