| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <ntddk.h> |
| 6 |
#include <wdf.h> |
| 7 |
#include <initguid.h> |
| 8 |
|
| 9 |
#include "virt2phys.h" |
| 10 |
#include "virt2phys_logic.h" |
| 11 |
#include "virt2phys_trace.h" |
| 12 |
#include "virt2phys.tmh" |
| 13 |
|
| 14 |
DRIVER_INITIALIZE DriverEntry; |
| 15 |
EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload; |
| 16 |
EVT_WDF_DRIVER_DEVICE_ADD virt2phys_driver_EvtDeviceAdd; |
| 17 |
EVT_WDF_IO_IN_CALLER_CONTEXT virt2phys_device_EvtIoInCallerContext; |
| 18 |
|
| 19 |
static NTSTATUS virt2phys_load_params( |
| 20 |
WDFDRIVER driver, struct virt2phys_params *params); |
| 21 |
static VOID virt2phys_on_process_event( |
| 22 |
HANDLE parent_id, HANDLE process_id, BOOLEAN create); |
| 23 |
|
| 24 |
static const ULONG PROCESS_COUNT_LIMIT_DEF = 1 << 4; |
| 25 |
static const ULONG PROCESS_MEMORY_LIMIT_DEF = 16 * (1 << 10); |
| 26 |
|
| 27 |
_Use_decl_annotations_ |
| 28 |
NTSTATUS |
| 29 |
DriverEntry(PDRIVER_OBJECT driver_object, PUNICODE_STRING registry_path) |
| 30 |
{ |
| 31 |
WDF_DRIVER_CONFIG config; |
| 32 |
WDF_OBJECT_ATTRIBUTES attributes; |
| 33 |
WDFDRIVER driver; |
| 34 |
struct virt2phys_params params; |
| 35 |
NTSTATUS status; |
| 36 |
|
| 37 |
PAGED_CODE(); |
| 38 |
|
| 39 |
WDF_OBJECT_ATTRIBUTES_INIT(&attributes); |
| 40 |
WDF_DRIVER_CONFIG_INIT(&config, virt2phys_driver_EvtDeviceAdd); |
| 41 |
config.EvtDriverUnload = virt2phys_driver_unload; |
| 42 |
status = WdfDriverCreate( |
| 43 |
driver_object, registry_path, |
| 44 |
&attributes, &config, &driver); |
| 45 |
if (!NT_SUCCESS(status)) |
| 46 |
return status; |
| 47 |
|
| 48 |
status = virt2phys_load_params(driver, ¶ms); |
| 49 |
if (!NT_SUCCESS(status)) |
| 50 |
return status; |
| 51 |
|
| 52 |
status = virt2phys_init(¶ms); |
| 53 |
if (!NT_SUCCESS(status)) |
| 54 |
return status; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
status = PsSetCreateProcessNotifyRoutine( |
| 67 |
virt2phys_on_process_event, FALSE); |
| 68 |
if (!NT_SUCCESS(status)) |
| 69 |
return status; |
| 70 |
|
| 71 |
WPP_INIT_TRACING(driver_object, registry_path); |
| 72 |
|
| 73 |
return status; |
| 74 |
} |
| 75 |
|
| 76 |
static NTSTATUS |
| 77 |
virt2phys_read_param(WDFKEY key, PCUNICODE_STRING name, ULONG *value, |
| 78 |
ULONG def) |
| 79 |
{ |
| 80 |
NTSTATUS status; |
| 81 |
|
| 82 |
status = WdfRegistryQueryULong(key, name, value); |
| 83 |
if (status == STATUS_OBJECT_NAME_NOT_FOUND) { |
| 84 |
*value = def; |
| 85 |
status = STATUS_SUCCESS; |
| 86 |
} |
| 87 |
return status; |
| 88 |
} |
| 89 |
|
| 90 |
static NTSTATUS |
| 91 |
virt2phys_read_mb(WDFKEY key, PCUNICODE_STRING name, ULONG64 *bytes, |
| 92 |
ULONG def_mb) |
| 93 |
{ |
| 94 |
ULONG mb; |
| 95 |
NTSTATUS status; |
| 96 |
|
| 97 |
status = virt2phys_read_param(key, name, &mb, def_mb); |
| 98 |
if (NT_SUCCESS(status)) |
| 99 |
*bytes = (ULONG64)mb * (1ULL << 20); |
| 100 |
return status; |
| 101 |
} |
| 102 |
|
| 103 |
static NTSTATUS |
| 104 |
virt2phys_load_params(WDFDRIVER driver, struct virt2phys_params *params) |
| 105 |
{ |
| 106 |
static DECLARE_CONST_UNICODE_STRING( |
| 107 |
process_count_limit, L"ProcessCountLimit"); |
| 108 |
static DECLARE_CONST_UNICODE_STRING( |
| 109 |
process_memory_limit, L"ProcessMemoryLimitMB"); |
| 110 |
|
| 111 |
WDFKEY key; |
| 112 |
NTSTATUS status; |
| 113 |
|
| 114 |
status = WdfDriverOpenParametersRegistryKey( |
| 115 |
driver, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &key); |
| 116 |
if (!NT_SUCCESS(status)) |
| 117 |
return status; |
| 118 |
|
| 119 |
status = virt2phys_read_param(key, &process_count_limit, |
| 120 |
¶ms->process_count_limit, PROCESS_COUNT_LIMIT_DEF); |
| 121 |
if (!NT_SUCCESS(status)) |
| 122 |
goto cleanup; |
| 123 |
|
| 124 |
status = virt2phys_read_mb(key, &process_memory_limit, |
| 125 |
¶ms->process_memory_limit, PROCESS_MEMORY_LIMIT_DEF); |
| 126 |
if (!NT_SUCCESS(status)) |
| 127 |
goto cleanup; |
| 128 |
|
| 129 |
cleanup: |
| 130 |
WdfRegistryClose(key); |
| 131 |
return status; |
| 132 |
} |
| 133 |
|
| 134 |
_Use_decl_annotations_ |
| 135 |
VOID |
| 136 |
virt2phys_driver_unload(WDFDRIVER driver) |
| 137 |
{ |
| 138 |
PsSetCreateProcessNotifyRoutine(virt2phys_on_process_event, TRUE); |
| 139 |
|
| 140 |
virt2phys_cleanup(); |
| 141 |
|
| 142 |
WPP_CLEANUP(WdfDriverWdmGetDriverObject(driver)); |
| 143 |
} |
| 144 |
|
| 145 |
_Use_decl_annotations_ |
| 146 |
NTSTATUS |
| 147 |
virt2phys_driver_EvtDeviceAdd(WDFDRIVER driver, PWDFDEVICE_INIT init) |
| 148 |
{ |
| 149 |
WDF_OBJECT_ATTRIBUTES attributes; |
| 150 |
WDFDEVICE device; |
| 151 |
NTSTATUS status; |
| 152 |
|
| 153 |
UNREFERENCED_PARAMETER(driver); |
| 154 |
|
| 155 |
WdfDeviceInitSetIoType( |
| 156 |
init, WdfDeviceIoNeither); |
| 157 |
WdfDeviceInitSetIoInCallerContextCallback( |
| 158 |
init, virt2phys_device_EvtIoInCallerContext); |
| 159 |
|
| 160 |
WDF_OBJECT_ATTRIBUTES_INIT(&attributes); |
| 161 |
|
| 162 |
status = WdfDeviceCreate(&init, &attributes, &device); |
| 163 |
if (!NT_SUCCESS(status)) { |
| 164 |
TraceError("WdfDriverCreate() = %!STATUS!", status); |
| 165 |
return status; |
| 166 |
} |
| 167 |
|
| 168 |
status = WdfDeviceCreateDeviceInterface( |
| 169 |
device, &GUID_DEVINTERFACE_VIRT2PHYS, NULL); |
| 170 |
if (!NT_SUCCESS(status)) { |
| 171 |
TraceError("WdfDeviceCreateDeviceInterface() = %!STATUS!", |
| 172 |
status); |
| 173 |
return status; |
| 174 |
} |
| 175 |
|
| 176 |
return STATUS_SUCCESS; |
| 177 |
} |
| 178 |
|
| 179 |
_Use_decl_annotations_ |
| 180 |
VOID |
| 181 |
virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) |
| 182 |
{ |
| 183 |
WDF_REQUEST_PARAMETERS params; |
| 184 |
ULONG code; |
| 185 |
PVOID *pvirt, virt; |
| 186 |
PHYSICAL_ADDRESS *phys; |
| 187 |
size_t size; |
| 188 |
NTSTATUS status; |
| 189 |
|
| 190 |
UNREFERENCED_PARAMETER(device); |
| 191 |
PAGED_CODE(); |
| 192 |
|
| 193 |
WDF_REQUEST_PARAMETERS_INIT(¶ms); |
| 194 |
WdfRequestGetParameters(request, ¶ms); |
| 195 |
|
| 196 |
if (params.Type != WdfRequestTypeDeviceControl) { |
| 197 |
TraceWarning("Bogus IO request type %lu", params.Type); |
| 198 |
WdfRequestComplete(request, STATUS_NOT_SUPPORTED); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
code = params.Parameters.DeviceIoControl.IoControlCode; |
| 203 |
if (code != IOCTL_VIRT2PHYS_TRANSLATE) { |
| 204 |
TraceWarning("Bogus IO control code %lx", code); |
| 205 |
WdfRequestComplete(request, STATUS_NOT_SUPPORTED); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
status = WdfRequestRetrieveInputBuffer( |
| 210 |
request, sizeof(*pvirt), (PVOID *)&pvirt, &size); |
| 211 |
if (!NT_SUCCESS(status)) { |
| 212 |
TraceWarning("Retrieving input buffer: %!STATUS!", status); |
| 213 |
WdfRequestComplete(request, status); |
| 214 |
return; |
| 215 |
} |
| 216 |
virt = *pvirt; |
| 217 |
|
| 218 |
status = WdfRequestRetrieveOutputBuffer( |
| 219 |
request, sizeof(*phys), (PVOID *)&phys, &size); |
| 220 |
if (!NT_SUCCESS(status)) { |
| 221 |
TraceWarning("Retrieving output buffer: %!STATUS!", status); |
| 222 |
WdfRequestComplete(request, status); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
status = virt2phys_translate(virt, phys); |
| 227 |
if (NT_SUCCESS(status)) |
| 228 |
WdfRequestSetInformation(request, sizeof(*phys)); |
| 229 |
|
| 230 |
TraceInfo("Translate %p to %llx: %!STATUS!", |
| 231 |
virt, phys->QuadPart, status); |
| 232 |
WdfRequestComplete(request, status); |
| 233 |
} |
| 234 |
|
| 235 |
static VOID |
| 236 |
virt2phys_on_process_event( |
| 237 |
HANDLE parent_id, HANDLE process_id, BOOLEAN create) |
| 238 |
{ |
| 239 |
UNREFERENCED_PARAMETER(parent_id); |
| 240 |
|
| 241 |
if (create) |
| 242 |
return; |
| 243 |
|
| 244 |
virt2phys_process_cleanup(process_id); |
| 245 |
} |