kvm-guest-drivers-windows

Форк
0
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
#endif
36

37
DRIVER_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.
42
EVT_WDF_OBJECT_CONTEXT_CLEANUP _IRQL_requires_(PASSIVE_LEVEL) VIOSockEvtDriverContextCleanup;
43

44
#ifdef ALLOC_PRAGMA
45
#pragma alloc_text (INIT, DriverEntry)
46
#pragma alloc_text (PAGE, VIOSockEvtDriverContextCleanup)
47
#endif
48

49
NTSTATUS DriverEntry(IN PDRIVER_OBJECT  DriverObject,
50
                     IN PUNICODE_STRING RegistryPath)
51
{
52
    NTSTATUS               status = STATUS_SUCCESS;
53
    WDF_DRIVER_CONFIG      config;
54
    WDF_OBJECT_ATTRIBUTES  attributes;
55
    WDFDRIVER              Driver;
56

57
    ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
58

59
    InitializeDebugPrints(DriverObject, RegistryPath);
60

61
    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,
62
        "VirtioSocket driver started...built on %s %s\n", __DATE__, __TIME__);
63

64
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
65
    attributes.EvtCleanupCallback = VIOSockEvtDriverContextCleanup;
66

67
    WDF_DRIVER_CONFIG_INIT(&config, VIOSockEvtDeviceAdd);
68
    config.DriverPoolTag  = VIOSOCK_DRIVER_MEMORY_TAG;
69

70
    status = WdfDriverCreate(DriverObject,
71
                             RegistryPath,
72
                             &attributes,
73
                             &config,
74
                             &Driver);
75

76
    if (!NT_SUCCESS(status)) {
77
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
78
           "WdfDriverCreate failed - 0x%x\n", status);
79
        WPP_CLEANUP(DriverObject);
80
        return status;
81
    }
82

83
    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT, "<-- %s\n", __FUNCTION__);
84
    return status;
85
}
86

87
VOID
88
VIOSockEvtDriverContextCleanup(
89
    IN WDFOBJECT Driver
90
)
91
{
92
    PAGED_CODE();
93

94
    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP, "--> %s\n", __FUNCTION__);
95

96
    WPP_CLEANUP(WdfDriverWdmGetDriverObject((WDFDRIVER)Driver));
97
}
98

99
VOID
100
VIOSockTimerStart(
101
    IN PVIOSOCK_TIMER   pTimer,
102
    IN LONGLONG         Timeout
103
)
104
{
105
    LARGE_INTEGER liTicks;
106
    BOOLEAN bSetTimer = FALSE;
107

108
    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_SOCKET, "--> %s\n", __FUNCTION__);
109

110
    if (!Timeout || Timeout == LONGLONG_MAX)
111
        return;
112

113
    ASSERT(Timeout > VIOSOCK_TIMER_TOLERANCE);
114
    if (Timeout <= VIOSOCK_TIMER_TOLERANCE)
115
        Timeout = VIOSOCK_TIMER_TOLERANCE + 1;
116

117
    KeQueryTickCount(&liTicks);
118

119
    ++pTimer->StartRefs;
120

121
    if (pTimer->StartTime)
122
    {
123
        LONGLONG Remaining;
124

125
        ASSERT(pTimer->Timeout);
126

127
        Remaining = pTimer->Timeout -
128
            (liTicks.QuadPart - pTimer->StartTime) * KeQueryTimeIncrement();
129
        if (Remaining > Timeout + VIOSOCK_TIMER_TOLERANCE)
130
            bSetTimer = TRUE;
131
    }
132
    else
133
        bSetTimer = TRUE;
134

135
    if (bSetTimer)
136
    {
137
        pTimer->StartTime = liTicks.QuadPart;
138
        pTimer->Timeout = Timeout;
139
        WdfTimerStart(pTimer->Timer, -Timeout);
140
    }
141
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.