Skip to content

Commit

Permalink
Revert KSGLDRV replacements for IVSHMEM
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaivel committed Sep 5, 2023
1 parent 9d8a679 commit 5909979
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 168 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5.0)
project(sharedgl VERSION 0.3.1 LANGUAGES C CXX)
project(sharedgl VERSION 0.4.0 LANGUAGES C CXX)

include_directories(${CMAKE_SOURCE_DIR}/inc)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
Expand Down
56 changes: 28 additions & 28 deletions kernel/windows/Device.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "driver.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE, KSGLDRVCreateDevice)
#pragma alloc_text (PAGE, KSGLDRVEvtDevicePrepareHardware)
#pragma alloc_text (PAGE, KSGLDRVEvtD0Exit)
#pragma alloc_text (PAGE, IVSHMEMCreateDevice)
#pragma alloc_text (PAGE, IVSHMEMEvtDevicePrepareHardware)
#pragma alloc_text (PAGE, IVSHMEMEvtD0Exit)
#endif

NTSTATUS KSGLDRVCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit)
NTSTATUS IVSHMEMCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit)
{
WDF_OBJECT_ATTRIBUTES deviceAttributes;
PDEVICE_CONTEXT deviceContext;
Expand All @@ -20,14 +20,14 @@ NTSTATUS KSGLDRVCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit)

WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = KSGLDRVEvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = KSGLDRVEvtDeviceReleaseHardware;
pnpPowerCallbacks.EvtDeviceD0Entry = KSGLDRVEvtD0Entry;
pnpPowerCallbacks.EvtDeviceD0Exit = KSGLDRVEvtD0Exit;
pnpPowerCallbacks.EvtDevicePrepareHardware = IVSHMEMEvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = IVSHMEMEvtDeviceReleaseHardware;
pnpPowerCallbacks.EvtDeviceD0Entry = IVSHMEMEvtD0Entry;
pnpPowerCallbacks.EvtDeviceD0Exit = IVSHMEMEvtD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

WDF_FILEOBJECT_CONFIG fileConfig;
WDF_FILEOBJECT_CONFIG_INIT(&fileConfig, NULL, NULL, KSGLDRVEvtDeviceFileCleanup);
WDF_FILEOBJECT_CONFIG_INIT(&fileConfig, NULL, NULL, IVSHMEMEvtDeviceFileCleanup);
WdfDeviceInitSetFileObjectConfig(DeviceInit, &fileConfig, WDF_NO_OBJECT_ATTRIBUTES);

status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
Expand All @@ -43,25 +43,25 @@ NTSTATUS KSGLDRVCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit)
KeInitializeSpinLock(&deviceContext->eventListLock);
InitializeListHead(&deviceContext->eventList);

status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_KSGLDRV, NULL);
status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_IVSHMEM, NULL);

if (!NT_SUCCESS(status))
{
DEBUG_ERROR("%s", "Call to WdfDeviceCreateDeviceInterface failed");
return status;
}

status = KSGLDRVQueueInitialize(device);
status = IVSHMEMQueueInitialize(device);
if (!NT_SUCCESS(status))
{
DEBUG_ERROR("%s", "KSGLDRVQueueInitialize failed");
DEBUG_ERROR("%s", "IVSHMEMQueueInitialize failed");
return status;
}

return status;
}

PVOID KSGLDRVMmMapIoSpace(
PVOID IVSHMEMMmMapIoSpace(
_In_ PHYSICAL_ADDRESS PhysicalAddress,
_In_ SIZE_T NumberOfBytes
)
Expand Down Expand Up @@ -92,7 +92,7 @@ PVOID KSGLDRVMmMapIoSpace(
}


NTSTATUS KSGLDRVEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIST ResourcesRaw, _In_ WDFCMRESLIST ResourcesTranslated)
NTSTATUS IVSHMEMEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIST ResourcesRaw, _In_ WDFCMRESLIST ResourcesTranslated)
{
PAGED_CODE();
DEBUG_INFO("%s", __FUNCTION__);
Expand Down Expand Up @@ -122,7 +122,7 @@ NTSTATUS KSGLDRVEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS

if (deviceContext->interruptCount > 0)
{
deviceContext->interrupts = (WDFINTERRUPT*)ExAllocatePoolUninitialized(KSGLDRV_NONPAGED_POOL,
deviceContext->interrupts = (WDFINTERRUPT*)ExAllocatePoolUninitialized(IVSHMEM_NONPAGED_POOL,
sizeof(WDFINTERRUPT) * deviceContext->interruptCount, 'sQRI');

if (!deviceContext->interrupts)
Expand All @@ -147,15 +147,15 @@ NTSTATUS KSGLDRVEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS
// control registers
if (memIndex == 0)
{
if (descriptor->u.Memory.Length != sizeof(KSGLDRVDeviceRegisters))
if (descriptor->u.Memory.Length != sizeof(IVSHMEMDeviceRegisters))
{
DEBUG_ERROR("Resource size was %u long when %u was expected",
descriptor->u.Memory.Length, sizeof(KSGLDRVDeviceRegisters));
descriptor->u.Memory.Length, sizeof(IVSHMEMDeviceRegisters));
result = STATUS_DEVICE_HARDWARE_ERROR;
break;
}

deviceContext->devRegisters = (PKSGLDRVDeviceRegisters)KSGLDRVMmMapIoSpace(
deviceContext->devRegisters = (PIVSHMEMDeviceRegisters)IVSHMEMMmMapIoSpace(
descriptor->u.Memory.Start,
descriptor->u.Memory.Length);

Expand Down Expand Up @@ -208,8 +208,8 @@ NTSTATUS KSGLDRVEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS
{
WDF_INTERRUPT_CONFIG irqConfig;
WDF_INTERRUPT_CONFIG_INIT(&irqConfig,
KSGLDRVInterruptISR,
KSGLDRVInterruptDPC);
IVSHMEMInterruptISR,
IVSHMEMInterruptDPC);
#if (NTDDI_VERSION >= NTDDI_WIN8)
irqConfig.InterruptTranslated = descriptor;
irqConfig.InterruptRaw = WdfCmResourceListGetDescriptor(ResourcesRaw, i);
Expand Down Expand Up @@ -247,7 +247,7 @@ NTSTATUS KSGLDRVEvtDevicePrepareHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS
return result;
}

NTSTATUS KSGLDRVEvtDeviceReleaseHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIST ResourcesTranslated)
NTSTATUS IVSHMEMEvtDeviceReleaseHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIST ResourcesTranslated)
{
UNREFERENCED_PARAMETER(ResourcesTranslated);
DEBUG_INFO("%s", __FUNCTION__);
Expand All @@ -257,7 +257,7 @@ NTSTATUS KSGLDRVEvtDeviceReleaseHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS

if (deviceContext->devRegisters)
{
MmUnmapIoSpace(deviceContext->devRegisters, sizeof(PKSGLDRVDeviceRegisters));
MmUnmapIoSpace(deviceContext->devRegisters, sizeof(PIVSHMEMDeviceRegisters));
}

if (deviceContext->shmemMap)
Expand Down Expand Up @@ -289,7 +289,7 @@ NTSTATUS KSGLDRVEvtDeviceReleaseHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS
while (entry != &deviceContext->eventList)
{
_Analysis_assume_(entry != NULL);
PKSGLDRVEventListEntry event = CONTAINING_RECORD(entry, KSGLDRVEventListEntry, ListEntry);
PIVSHMEMEventListEntry event = CONTAINING_RECORD(entry, IVSHMEMEventListEntry, ListEntry);
if (event->event)
{
ObDereferenceObject(event->event);
Expand All @@ -307,15 +307,15 @@ NTSTATUS KSGLDRVEvtDeviceReleaseHardware(_In_ WDFDEVICE Device, _In_ WDFCMRESLIS
return STATUS_SUCCESS;
}

NTSTATUS KSGLDRVEvtD0Entry(_In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE PreviousState)
NTSTATUS IVSHMEMEvtD0Entry(_In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE PreviousState)
{
UNREFERENCED_PARAMETER(Device);
UNREFERENCED_PARAMETER(PreviousState);
DEBUG_INFO("%s", __FUNCTION__);
return STATUS_SUCCESS;
}

NTSTATUS KSGLDRVEvtD0Exit(_In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE PreviousState)
NTSTATUS IVSHMEMEvtD0Exit(_In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE PreviousState)
{
UNREFERENCED_PARAMETER(Device);
UNREFERENCED_PARAMETER(PreviousState);
Expand All @@ -324,7 +324,7 @@ NTSTATUS KSGLDRVEvtD0Exit(_In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE Pre
return STATUS_SUCCESS;
}

BOOLEAN KSGLDRVInterruptISR(_In_ WDFINTERRUPT Interrupt, _In_ ULONG MessageID)
BOOLEAN IVSHMEMInterruptISR(_In_ WDFINTERRUPT Interrupt, _In_ ULONG MessageID)
{
WDFDEVICE device;
PDEVICE_CONTEXT deviceContext;
Expand All @@ -342,7 +342,7 @@ BOOLEAN KSGLDRVInterruptISR(_In_ WDFINTERRUPT Interrupt, _In_ ULONG MessageID)
return TRUE;
}

void KSGLDRVInterruptDPC(_In_ WDFINTERRUPT Interrupt, _In_ WDFOBJECT AssociatedObject)
void IVSHMEMInterruptDPC(_In_ WDFINTERRUPT Interrupt, _In_ WDFOBJECT AssociatedObject)
{
UNREFERENCED_PARAMETER(AssociatedObject);

Expand All @@ -361,7 +361,7 @@ void KSGLDRVInterruptDPC(_In_ WDFINTERRUPT Interrupt, _In_ WDFOBJECT AssociatedO
PLIST_ENTRY entry = deviceContext->eventList.Flink;
while (entry != &deviceContext->eventList)
{
PKSGLDRVEventListEntry event = CONTAINING_RECORD(entry, KSGLDRVEventListEntry, ListEntry);
PIVSHMEMEventListEntry event = CONTAINING_RECORD(entry, IVSHMEMEventListEntry, ListEntry);
PLIST_ENTRY next = entry->Flink;
if (pending & ((LONG64)1 << event->vector))
{
Expand Down
26 changes: 13 additions & 13 deletions kernel/windows/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ EXTERN_C_START
#define MAX_EVENTS 32

#pragma align(push,4)
typedef struct KSGLDRVDeviceRegisters
typedef struct IVSHMEMDeviceRegisters
{
volatile ULONG irqMask;
volatile ULONG irqStatus;
volatile LONG ivProvision;
volatile ULONG doorbell;
volatile UCHAR reserved[240];
}
KSGLDRVDeviceRegisters, *PKSGLDRVDeviceRegisters;
IVSHMEMDeviceRegisters, *PIVSHMEMDeviceRegisters;
#pragma align(pop)

typedef struct KSGLDRVEventListEntry
typedef struct IVSHMEMEventListEntry
{
WDFFILEOBJECT owner;
UINT16 vector;
PRKEVENT event;
BOOLEAN singleShot;
LIST_ENTRY ListEntry;
}
KSGLDRVEventListEntry, *PKSGLDRVEventListEntry;
IVSHMEMEventListEntry, *PIVSHMEMEventListEntry;

#if (NTDDI_VERSION < NTDDI_WIN8)
typedef struct _MM_PHYSICAL_ADDRESS_LIST {
Expand All @@ -36,7 +36,7 @@ typedef struct _MM_PHYSICAL_ADDRESS_LIST {

typedef struct _DEVICE_CONTEXT
{
PKSGLDRVDeviceRegisters devRegisters; // the device registers (BAR0)
PIVSHMEMDeviceRegisters devRegisters; // the device registers (BAR0)

MM_PHYSICAL_ADDRESS_LIST shmemAddr; // physical address of the shared memory (BAR2)
PMDL shmemMDL; // memory discriptor list of the shared memory
Expand All @@ -48,21 +48,21 @@ typedef struct _DEVICE_CONTEXT
LONG64 pendingISR; // flags for ISRs pending processing

KSPIN_LOCK eventListLock; // spinlock for the below event list
KSGLDRVEventListEntry eventBuffer[MAX_EVENTS]; // buffer of pre-allocated events
IVSHMEMEventListEntry eventBuffer[MAX_EVENTS]; // buffer of pre-allocated events
UINT16 eventBufferUsed; // number of events currenty in use
LIST_ENTRY eventList; // pending events to fire
}
DEVICE_CONTEXT, *PDEVICE_CONTEXT;

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, DeviceGetContext)

NTSTATUS KSGLDRVCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit);
NTSTATUS IVSHMEMCreateDevice(_Inout_ PWDFDEVICE_INIT DeviceInit);

EVT_WDF_DEVICE_PREPARE_HARDWARE KSGLDRVEvtDevicePrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE KSGLDRVEvtDeviceReleaseHardware;
EVT_WDF_DEVICE_D0_ENTRY KSGLDRVEvtD0Entry;
EVT_WDF_DEVICE_D0_EXIT KSGLDRVEvtD0Exit;
EVT_WDF_INTERRUPT_ISR KSGLDRVInterruptISR;
EVT_WDF_INTERRUPT_DPC KSGLDRVInterruptDPC;
EVT_WDF_DEVICE_PREPARE_HARDWARE IVSHMEMEvtDevicePrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE IVSHMEMEvtDeviceReleaseHardware;
EVT_WDF_DEVICE_D0_ENTRY IVSHMEMEvtD0Entry;
EVT_WDF_DEVICE_D0_EXIT IVSHMEMEvtD0Exit;
EVT_WDF_INTERRUPT_ISR IVSHMEMInterruptISR;
EVT_WDF_INTERRUPT_DPC IVSHMEMInterruptDPC;

EXTERN_C_END
10 changes: 5 additions & 5 deletions kernel/windows/Driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (PAGE, KSGLDRVEvtDeviceAdd)
#pragma alloc_text (PAGE, KSGLDRVEvtDriverContextCleanup)
#pragma alloc_text (PAGE, IVSHMEMEvtDeviceAdd)
#pragma alloc_text (PAGE, IVSHMEMEvtDriverContextCleanup)
#endif

NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)
Expand All @@ -13,19 +13,19 @@ NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING Regi
WDF_OBJECT_ATTRIBUTES attributes;

WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
WDF_DRIVER_CONFIG_INIT(&config, KSGLDRVEvtDeviceAdd);
WDF_DRIVER_CONFIG_INIT(&config, IVSHMEMEvtDeviceAdd);

status = WdfDriverCreate(DriverObject, RegistryPath, &attributes, &config, WDF_NO_HANDLE);

return status;
}

NTSTATUS KSGLDRVEvtDeviceAdd(_In_ WDFDRIVER Driver, _Inout_ PWDFDEVICE_INIT DeviceInit)
NTSTATUS IVSHMEMEvtDeviceAdd(_In_ WDFDRIVER Driver, _Inout_ PWDFDEVICE_INIT DeviceInit)
{
NTSTATUS status;
UNREFERENCED_PARAMETER(Driver);
PAGED_CODE();

status = KSGLDRVCreateDevice(DeviceInit);
status = IVSHMEMCreateDevice(DeviceInit);
return status;
}
12 changes: 6 additions & 6 deletions kernel/windows/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
#include <initguid.h>

#if (NTDOI_VERSION >= NTDOI_WIN8)
#define KSGLDRV_NONPAGED_POOL NonPagedPoolNx
#define IVSHMEM_NONPAGED_POOL NonPagedPoolNx
#else
#define KSGLDRV_NONPAGED_POOL NonPagedPool
#define IVSHMEM_NONPAGED_POOL NonPagedPool
#endif

#include "device.h"
#include "queue.h"

// using error levels to avoid the debug print filter
#define DEBUG_ERROR(fmt, ...) do { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[E:KSGLDRV] " fmt "\n", ## __VA_ARGS__)); } while (0)
#define DEBUG_INFO(fmt, ...) do { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[I:KSGLDRV] " fmt "\n", ## __VA_ARGS__)); } while (0)
#define DEBUG_ERROR(fmt, ...) do { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[E:IVSHMEM] " fmt "\n", ## __VA_ARGS__)); } while (0)
#define DEBUG_INFO(fmt, ...) do { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[I:IVSHMEM] " fmt "\n", ## __VA_ARGS__)); } while (0)

EXTERN_C_START

Expand All @@ -22,7 +22,7 @@ EXTERN_C_START
//

DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD KSGLDRVEvtDeviceAdd;
EVT_WDF_OBJECT_CONTEXT_CLEANUP KSGLDRVEvtDriverContextCleanup;
EVT_WDF_DRIVER_DEVICE_ADD IVSHMEMEvtDeviceAdd;
EVT_WDF_OBJECT_CONTEXT_CLEANUP IVSHMEMEvtDriverContextCleanup;

EXTERN_C_END
Loading

0 comments on commit 5909979

Please sign in to comment.