kvm-guest-drivers-windows
141 строка · 4.6 Кб
1/*
2* Main driver file containing DriverEntry and driver related functions
3*
4* Copyright (c) 2019 Virtuozzo International GmbH
5*
6* Redistribution and use in source and binary forms, with or without
7* modification, are permitted provided that the following conditions
8* are met :
9* 1. Redistributions of source code must retain the above copyright
10* notice, this list of conditions and the following disclaimer.
11* 2. Redistributions in binary form must reproduce the above copyright
12* notice, this list of conditions and the following disclaimer in the
13* documentation and / or other materials provided with the distribution.
14* 3. Neither the names of the copyright holders nor the names of their contributors
15* may be used to endorse or promote products derived from this software
16* without specific prior written permission.
17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20* ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27* SUCH DAMAGE.
28*/
29
30#include "precomp.h"31#include "viosock.h"32
33#if defined(EVENT_TRACING)34#include "Driver.tmh"35#endif36
37DRIVER_INITIALIZE DriverEntry;38
39// Context cleanup callbacks generally run at IRQL <= DISPATCH_LEVEL but
40// WDFDRIVER context cleanup is guaranteed to run at PASSIVE_LEVEL.
41// Annotate the prototype to make static analysis happy.
42EVT_WDF_OBJECT_CONTEXT_CLEANUP _IRQL_requires_(PASSIVE_LEVEL) VIOSockEvtDriverContextCleanup;43
44#ifdef ALLOC_PRAGMA45#pragma alloc_text (INIT, DriverEntry)46#pragma alloc_text (PAGE, VIOSockEvtDriverContextCleanup)47#endif48
49NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,50IN PUNICODE_STRING RegistryPath)51{
52NTSTATUS status = STATUS_SUCCESS;53WDF_DRIVER_CONFIG config;54WDF_OBJECT_ATTRIBUTES attributes;55WDFDRIVER Driver;56
57ExInitializeDriverRuntime(DrvRtPoolNxOptIn);58
59InitializeDebugPrints(DriverObject, RegistryPath);60
61TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,62"VirtioSocket driver started...built on %s %s\n", __DATE__, __TIME__);63
64WDF_OBJECT_ATTRIBUTES_INIT(&attributes);65attributes.EvtCleanupCallback = VIOSockEvtDriverContextCleanup;66
67WDF_DRIVER_CONFIG_INIT(&config, VIOSockEvtDeviceAdd);68config.DriverPoolTag = VIOSOCK_DRIVER_MEMORY_TAG;69
70status = WdfDriverCreate(DriverObject,71RegistryPath,72&attributes,73&config,74&Driver);75
76if (!NT_SUCCESS(status)) {77TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,78"WdfDriverCreate failed - 0x%x\n", status);79WPP_CLEANUP(DriverObject);80return status;81}82
83TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT, "<-- %s\n", __FUNCTION__);84return status;85}
86
87VOID
88VIOSockEvtDriverContextCleanup(89IN WDFOBJECT Driver
90)
91{
92PAGED_CODE();93
94TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP, "--> %s\n", __FUNCTION__);95
96WPP_CLEANUP(WdfDriverWdmGetDriverObject((WDFDRIVER)Driver));97}
98
99VOID
100VIOSockTimerStart(101IN PVIOSOCK_TIMER pTimer,102IN LONGLONG Timeout
103)
104{
105LARGE_INTEGER liTicks;106BOOLEAN bSetTimer = FALSE;107
108TraceEvents(TRACE_LEVEL_VERBOSE, DBG_SOCKET, "--> %s\n", __FUNCTION__);109
110if (!Timeout || Timeout == LONGLONG_MAX)111return;112
113ASSERT(Timeout > VIOSOCK_TIMER_TOLERANCE);114if (Timeout <= VIOSOCK_TIMER_TOLERANCE)115Timeout = VIOSOCK_TIMER_TOLERANCE + 1;116
117KeQueryTickCount(&liTicks);118
119++pTimer->StartRefs;120
121if (pTimer->StartTime)122{123LONGLONG Remaining;124
125ASSERT(pTimer->Timeout);126
127Remaining = pTimer->Timeout -128(liTicks.QuadPart - pTimer->StartTime) * KeQueryTimeIncrement();129if (Remaining > Timeout + VIOSOCK_TIMER_TOLERANCE)130bSetTimer = TRUE;131}132else133bSetTimer = TRUE;134
135if (bSetTimer)136{137pTimer->StartTime = liTicks.QuadPart;138pTimer->Timeout = Timeout;139WdfTimerStart(pTimer->Timer, -Timeout);140}141}