summaryrefslogtreecommitdiff
path: root/qxldod/compat.cpp
blob: d5a6063d6b984aae2bd7dca2246c30c316e38376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 */

#include "driver.h"
#include "compat.h"

static MapIoSpaceFunc DetectMapIoSpace;
MapIoSpaceFunc *MapIoSpace = DetectMapIoSpace;

typedef NTKERNELAPI PVOID MmMapIoSpaceExFunc(
    _In_ PHYSICAL_ADDRESS PhysicalAddress,
    _In_ SIZE_T           NumberOfBytes,
    _In_ ULONG            Protect
);
static MmMapIoSpaceExFunc *pMmMapIoSpaceEx;

// all functions in this module are paged
#pragma code_seg("PAGE")

// we call MmMapIoSpace only if MmMapIoSpaceEx is not present
// so disable the warning
#pragma warning(push)
#pragma warning(disable:30029)
static PVOID OldMapIoSpace(
    _In_ PHYSICAL_ADDRESS PhysicalAddress,
    _In_ SIZE_T           NumberOfBytes,
    _In_ MEMORY_CACHING_TYPE CacheType,
    _In_ ULONG            Protect
)
{
    PAGED_CODE();
    return MmMapIoSpace(PhysicalAddress, NumberOfBytes, CacheType);
}
#pragma warning(pop)

static PVOID NewMapIoSpace(
    _In_ PHYSICAL_ADDRESS PhysicalAddress,
    _In_ SIZE_T           NumberOfBytes,
    _In_ MEMORY_CACHING_TYPE CacheType,
    _In_ ULONG            Protect
)
{
    PAGED_CODE();
    return pMmMapIoSpaceEx(PhysicalAddress, NumberOfBytes, Protect);
}

static PVOID DetectMapIoSpace(
    _In_ PHYSICAL_ADDRESS PhysicalAddress,
    _In_ SIZE_T           NumberOfBytes,
    _In_ MEMORY_CACHING_TYPE CacheType,
    _In_ ULONG            Protect
)
{
    PAGED_CODE();
    UNICODE_STRING name;
    RtlInitUnicodeString(&name, L"MmMapIoSpaceEx");

    pMmMapIoSpaceEx = (MmMapIoSpaceExFunc*)MmGetSystemRoutineAddress(&name);
    if (pMmMapIoSpaceEx) {
        MapIoSpace = NewMapIoSpace;
    } else {
        MapIoSpace = OldMapIoSpace;
    }
    return MapIoSpace(PhysicalAddress, NumberOfBytes, CacheType, Protect);
}